AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
Dialog.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
5 using System;
6 using System.Collections;
7 using UnityEngine;
8 
9 namespace HoloToolkit.UX.Dialog
10 {
17  public abstract class Dialog : InteractionReceiver
18  {
22  [SerializeField]
23  protected Transform buttonParent;
24 
25  protected DialogResult result;
26 
27  protected DialogState state = DialogState.Uninitialized;
28  public DialogState State
29  {
30  get
31  {
32  return state;
33  }
34  set
35  {
36  state = value;
37  }
38  }
39 
43  public Action<DialogResult> OnClosed;
44 
48  public DialogResult Result
49  {
50  get
51  {
52  return result;
53  }
54  }
55 
56  protected void Launch(DialogResult newResult)
57  {
58  if (State != DialogState.Uninitialized)
59  {
60  return;
61  }
62 
63  result = newResult;
64  StartCoroutine(RunDialogOverTime());
65  }
66 
71  protected IEnumerator RunDialogOverTime()
72  {
73  // Create our buttons and set up our message
74  GenerateButtons();
75  SetTitleAndMessage();
76  FinalizeLayout();
77 
78  // Open dialog
79  State = DialogState.Opening;
80  yield return StartCoroutine(OpenDialog());
81  State = DialogState.WaitingForInput;
82  // Wait for input
83  while (State == DialogState.WaitingForInput)
84  {
85  UpdateDialog();
86  yield return null;
87  }
88  // Close dialog
89  State = DialogState.Closing;
90  yield return StartCoroutine(CloseDialog());
91  State = DialogState.Closed;
92  // Callback
93  if (OnClosed != null)
94  {
95  OnClosed(result);
96  }
97  // Wait a moment to give scripts a chance to respond
98  yield return null;
99  // Destroy ourselves
100  GameObject.Destroy(gameObject);
101  yield break;
102  }
103 
109  protected virtual IEnumerator OpenDialog()
110  {
111  yield break;
112  }
113 
118  protected virtual IEnumerator CloseDialog()
119  {
120  yield break;
121  }
122 
127  protected virtual void UpdateDialog()
128  {
129  return;
130  }
131 
135  protected abstract void GenerateButtons();
136 
141  protected abstract void FinalizeLayout();
142 
147  protected abstract void SetTitleAndMessage();
148 
155  public static Dialog Open(GameObject dialogPrefab, DialogResult result)
156  {
157  GameObject dialogGo = GameObject.Instantiate(dialogPrefab) as GameObject;
158  Dialog dialog = dialogGo.GetComponent<Dialog>();
159 
160  dialog.Launch(result);
161  return dialog;
162  }
163 
172  public static Dialog Open(GameObject dialogPrefab, DialogButtonType buttons, string title, string message)
173  {
174  GameObject dialogGameObject = GameObject.Instantiate(dialogPrefab) as GameObject;
175  Dialog dialog = dialogGameObject.GetComponent<Dialog>();
176 
177  DialogResult result = new DialogResult
178  {
179  Buttons = buttons,
180  Title = title,
181  Message = message
182  };
183 
184  dialog.Launch(result);
185  return dialog;
186  }
187  }
188 }
DialogButtonType
Enum describing the style (caption) of button on a Dialog.
void Launch(DialogResult newResult)
Definition: Dialog.cs:56
static Dialog Open(GameObject dialogPrefab, DialogResult result)
Instantiates a dialog and passes it a result
Definition: Dialog.cs:155
Action< DialogResult > OnClosed
Called after user has clicked a button and the dialog has finished closing
Definition: Dialog.cs:43
IEnumerator RunDialogOverTime()
Opens dialog, waits for input, then closes
Definition: Dialog.cs:71
DialogState
Enum that describes the current state of a Dialog.
Definition: DialogEnums.cs:9
Transform buttonParent
Where the instantiated buttons will be placed
Definition: Dialog.cs:23
static Dialog Open(GameObject dialogPrefab, DialogButtonType buttons, string title, string message)
Instantiates a dialog and passes a generated result
Definition: Dialog.cs:172
virtual IEnumerator CloseDialog()
Closes the dialog - state must be set to Closed afterwards
Definition: Dialog.cs:118
An interaction receiver is simply a component that attached to a list of interactable objects and doe...
Used to tell simple dialogs which buttons to create And to tell whatever launched the dialog which bu...
Definition: Dialog.cs:17
virtual void UpdateDialog()
Perform any updates (animation, tagalong, etc) here This will be called every frame while waiting for...
Definition: Dialog.cs:127
virtual IEnumerator OpenDialog()
Opens the dialog - state must be set to WaitingForInput afterwards Overridden in inherited class...
Definition: Dialog.cs:109