AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
DialogShell.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 
4 using System;
5 using System.Text;
6 using UnityEngine;
8 using System.Collections.Generic;
9 using HoloToolkit.Unity;
10 
11 #if UNITY_WSA && UNITY_2017_2_OR_NEWER
12 using UnityEngine.XR.WSA;
13 #endif
14 
15 namespace HoloToolkit.UX.Dialog
16 {
20  public class DialogShell : Dialog
21  {
22  [SerializeField]
23  private int maxCharsPerLine = 45;
24 
25  [SerializeField]
26  private TextMesh titleText;
27 
28  [SerializeField]
29  private TextMesh messageText;
30 
31  [SerializeField]
32  private GameObject[] twoButtonSet;
33 
34  private DialogButton buttonPressed;
35 
40  public int MaxCharsPerLine
41  {
42  get
43  {
44  return maxCharsPerLine;
45  }
46 
47  set
48  {
49  maxCharsPerLine = value;
50  }
51  }
52 
56  protected void OnDrawGizmos()
57  {
58  if (messageText != null)
59  {
60  messageText.text = WordWrap(messageText.text, MaxCharsPerLine);
61  }
62  }
63 
67  protected override void FinalizeLayout()
68  {
69  SolverConstantViewSize solver = GetComponent<SolverConstantViewSize>();
70 
71 #if UNITY_WSA && UNITY_2017_2_OR_NEWER
72  // Optimize the content for immersive headset
73  if (HolographicSettings.IsDisplayOpaque)
74  {
75  solver.TargetViewPercentV = 0.35f;
76  }
77 #else
78  solver.TargetViewPercentV = 0.35f;
79 #endif
80  }
81 
85  protected override void GenerateButtons()
86  {
87  //Get List of ButtonTypes that should be created on Dialog
88  List<DialogButtonType> buttonTypes = new List<DialogButtonType>();
89  foreach (DialogButtonType buttonType in Enum.GetValues(typeof(DialogButtonType)))
90  {
91  if (buttonType == DialogButtonType.None)
92  {
93  continue;
94  }
95 
96  // If this button type flag is set
97  if ((buttonType & result.Buttons) == buttonType)
98  {
99  buttonTypes.Add(buttonType);
100  }
101  }
102 
103  twoButtonSet = new GameObject[2];
104 
105  //Find all buttons on dialog...
106  List<DialogButton> buttonsOnDialog = GetAllDialogButtons();
107 
108  //set desired buttons active and the rest inactive
109  SetButtonsActiveStates(buttonsOnDialog, buttonTypes.Count);
110 
111  //set titles and types
112  if (buttonTypes.Count > 0)
113  {
114  // If we have two buttons then do step 1, else 0
115  int step = buttonTypes.Count == 2 ? 1 : 0;
116  for (int i = 0; i < buttonTypes.Count; ++i)
117  {
118  twoButtonSet[i] = buttonsOnDialog[i + step].gameObject;
119  buttonsOnDialog[i + step].SetTitle(buttonTypes[i].ToString());
120  buttonsOnDialog[i + step].ButtonTypeEnum = buttonTypes[i];
121  }
122  }
123  }
124 
125  private void SetButtonsActiveStates(List<DialogButton> buttons, int count)
126  {
127 
128  for (int i = 0; i < buttons.Count; ++i)
129  {
130  var flag1 = (count == 1) && (i == 0);
131  var flag2 = (count == 2) && (i > 0);
132  buttons[i].ParentDialog = this;
133  buttons[i].gameObject.SetActive(flag1 || flag2);
134  }
135  }
136 
137  private List<DialogButton> GetAllDialogButtons()
138  {
139  List<DialogButton> buttonsOnDialog = new List<DialogButton>();
140  for (int i = 0; i < transform.childCount; i++)
141  {
142  Transform child = transform.GetChild(i);
143  if (child.name == "ButtonParent")
144  {
145  for (int childIndex = 0; childIndex < child.transform.childCount; ++childIndex)
146  {
147  Transform t = child.transform.GetChild(childIndex);
148  if (t != null)
149  {
150  DialogButton button = t.GetComponent<DialogButton>();
151  if (button != null)
152  {
153  buttonsOnDialog.Add(button);
154  }
155  }
156  }
157  }
158  }
159  return buttonsOnDialog;
160  }
161 
165  protected override void SetTitleAndMessage()
166  {
167  foreach (Transform child in transform)
168  {
169  if (child != null && child.name == "TitleText")
170  {
171  titleText = child.GetComponent<TextMesh>();
172  }
173  else if (child != null && child.name == "TitleMessage")
174  {
175  messageText = child.GetComponent<TextMesh>();
176  }
177  }
178 
179  if (titleText != null)
180  {
181  titleText.text = Result.Title;
182  }
183 
184  if (messageText != null)
185  {
186  messageText.text = WordWrap(Result.Message, MaxCharsPerLine);
187  }
188  }
189 
196  public static string WordWrap(string text, int maxCharsPerLine)
197  {
198  int pos = 0;
199  int next = 0;
200  StringBuilder stringBuilder = new StringBuilder();
201 
202  if (maxCharsPerLine < 1)
203  {
204  return text;
205  }
206 
207  for (pos = 0; pos < text.Length; pos = next)
208  {
209  int endOfLine = text.IndexOf(Environment.NewLine, pos, StringComparison.Ordinal);
210 
211  if (endOfLine == -1)
212  {
213  next = endOfLine = text.Length;
214  }
215  else
216  {
217  next = endOfLine + Environment.NewLine.Length;
218  }
219 
220  if (endOfLine > pos)
221  {
222  do
223  {
224  int len = endOfLine - pos;
225 
226  if (len > maxCharsPerLine)
227  len = BreakLine(text, pos, maxCharsPerLine);
228 
229  stringBuilder.Append(text, pos, len);
230  stringBuilder.Append(Environment.NewLine);
231 
232  pos += len;
233 
234  while (pos < endOfLine && Char.IsWhiteSpace(text[pos]))
235  {
236  pos++;
237  }
238 
239  } while (endOfLine > pos);
240  }
241  else
242  {
243  stringBuilder.Append(System.Environment.NewLine);
244  }
245  }
246 
247  return stringBuilder.ToString();
248  }
249 
257  public static int BreakLine(string text, int pos, int max)
258  {
259  int i = max - 1;
260 
261  while (i >= 0 && !Char.IsWhiteSpace(text[pos + i]))
262  {
263  i--;
264  }
265 
266  if (i < 0)
267  {
268  return max;
269  }
270 
271  while (i >= 0 && Char.IsWhiteSpace(text[pos + i]))
272  {
273  i--;
274  }
275 
276  return i + 1;
277  }
278 
282  public void DismissDialog()
283  {
284  state = DialogState.InputReceived;
285  }
286  }
287 }
static int BreakLine(string text, int pos, int max)
Method to linebreak text
Definition: DialogShell.cs:257
DialogButtonType
Enum describing the style (caption) of button on a Dialog.
void OnDrawGizmos()
Event handler when Editor gizmos are displayed
Definition: DialogShell.cs:56
ConstantViewSize solver scales to maintain a constant size relative to the view (currently tied to th...
override void SetTitleAndMessage()
Set Title and Text on the Dialog.
Definition: DialogShell.cs:165
Dialog that approximates the look of a HoloLens shell dialog
Definition: DialogShell.cs:20
DialogState
Enum that describes the current state of a Dialog.
Definition: DialogEnums.cs:9
Handling click event and dismiss dialog
Definition: DialogButton.cs:13
static string WordWrap(string text, int maxCharsPerLine)
For the text to wordwrap. For handing long text.
Definition: DialogShell.cs:196
Used to tell simple dialogs which buttons to create And to tell whatever launched the dialog which bu...
Definition: Dialog.cs:17
override void GenerateButtons()
Creates the buttons that are displayed on the dialog.
Definition: DialogShell.cs:85
override void FinalizeLayout()
Runs solver after Dialog is made to center it in view.
Definition: DialogShell.cs:67
void DismissDialog()
Function to destroy the Dialog.
Definition: DialogShell.cs:282