AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
AppBarButton.cs
Go to the documentation of this file.
1 //
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 // Licensed under the MIT License. See LICENSE in the project root for license information.
4 //
6 using UnityEngine;
7 
8 namespace HoloToolkit.Unity.UX
9 {
13  public class AppBarButton : MonoBehaviour
14  {
15  private ButtonIconProfile customIconProfile;
16  private AppBar.ButtonTemplate template;
17  private Vector3 targetPosition;
18  private Vector3 defaultOffset;
19  private Vector3 hiddenOffset;
20  private Vector3 manipulationOffset;
21  private CompoundButton cButton;
22  private Renderer highlightMeshRenderer;
23  private CompoundButtonText text;
24  private CompoundButtonIcon icon;
25  private AppBar parentToolBar;
26  private bool initialized = false;
27 
28  public const float ButtonWidth = 0.12f;
29  public const float ButtonDepth = 0.0001f;
30  const float MoveSpeed = 5f;
31 
32  public void Initialize(AppBar newParentToolBar, AppBar.ButtonTemplate newTemplate, ButtonIconProfile newCustomProfile)
33  {
34  template = newTemplate;
35  customIconProfile = newCustomProfile;
36  parentToolBar = newParentToolBar;
37 
38  cButton = GetComponent<CompoundButton>();
39  cButton.MainRenderer.enabled = false;
40  text = GetComponent<CompoundButtonText>();
41  text.Text = template.Text;
42  icon = GetComponent<CompoundButtonIcon>();
43  highlightMeshRenderer = cButton.GetComponent<CompoundButtonMesh>().Renderer;
44 
45  if (customIconProfile != null)
46  {
47  icon.Profile = customIconProfile;
48  icon.IconName = string.Empty;
49  }
50  icon.IconName = template.Icon;
51  initialized = true;
52  Hide();
53 
54  if (newTemplate.EventTarget != null)
55  {
56  // Register the button with its target interactable
57  newTemplate.EventTarget.Registerinteractable(gameObject);
58  } else
59  {
60  // Register the button with the parent app bar
61  newParentToolBar.Registerinteractable(gameObject);
62  }
63  }
64 
65  protected void OnEnable ()
66  {
67  Hide();
68  }
69 
70  protected void Update()
71  {
72  if (!initialized)
73  return;
74 
75  RefreshOffsets();
76 
77  switch (parentToolBar.State)
78  {
79  case AppBar.AppBarStateEnum.Default:
80  // Show hide, adjust, remove buttons
81  // The rest are hidden
82  targetPosition = defaultOffset;
83  switch (template.Type)
84  {
85  case AppBar.ButtonTypeEnum.Hide:
86  case AppBar.ButtonTypeEnum.Remove:
87  case AppBar.ButtonTypeEnum.Adjust:
88  case AppBar.ButtonTypeEnum.Custom:
89  Show();
90  break;
91 
92  default:
93  Hide();
94  break;
95  }
96  break;
97 
98  case AppBar.AppBarStateEnum.Hidden:
99  // Show show button
100  // The rest are hidden
101  targetPosition = hiddenOffset;
102  switch (template.Type)
103  {
104  case AppBar.ButtonTypeEnum.Show:
105  Show();
106  break;
107 
108  default:
109  Hide();
110  break;
111  }
112  break;
113 
114  case AppBar.AppBarStateEnum.Manipulation:
115  // Show done / remove buttons
116  // The rest are hidden
117  targetPosition = manipulationOffset;
118  switch (template.Type)
119  {
120  case AppBar.ButtonTypeEnum.Done:
121  case AppBar.ButtonTypeEnum.Remove:
122  Show();
123  break;
124 
125  default:
126  Hide();
127  break;
128  }
129  break;
130  }
131 
132  transform.localPosition = Vector3.Lerp(transform.localPosition, targetPosition, 0.5f);
133  }
134 
135  private void Hide()
136  {
137  if (!initialized)
138  return;
139 
140  icon.Alpha = 0f;
141  text.DisableText = true;
142  cButton.enabled = false;
143  highlightMeshRenderer.enabled = false;
144  cButton.gameObject.layer = LayerMask.NameToLayer("Ignore Raycast");
145  }
146 
147  private void Show()
148  {
149  if (!initialized)
150  return;
151 
152  icon.Alpha = 1f;
153  text.DisableText = false;
154  cButton.enabled = true;
155  highlightMeshRenderer.enabled = true;
156  cButton.gameObject.layer = LayerMask.NameToLayer("UI");
157  }
158 
159  private void RefreshOffsets()
160  {
161  // Apply offset based on total number of buttons
162  float xDefaultOffset = (parentToolBar.NumDefaultButtons / 2) * ButtonWidth;
163  float xManipulationOffset = (parentToolBar.NumManipulationButtons / 2) * ButtonWidth;
164 
165  // For odd numbers of buttons, add an additional 1/2 button offset
166  if (parentToolBar.NumDefaultButtons > 1 && parentToolBar.NumDefaultButtons % 2 == 0)
167  {
168  xDefaultOffset -= (ButtonWidth / 2);
169  }
170  if (parentToolBar.NumManipulationButtons > 1 && parentToolBar.NumManipulationButtons % 2 == 0)
171  {
172  xManipulationOffset -= (ButtonWidth / 2);
173  }
174 
175  defaultOffset = new Vector3(
176  template.DefaultPosition * ButtonWidth - xDefaultOffset,
177  0f,
178  template.DefaultPosition * ButtonDepth);
179  manipulationOffset = new Vector3(
180  template.ManipulationPosition * ButtonWidth - xManipulationOffset,
181  0f,
182  template.ManipulationPosition * ButtonDepth);
183  hiddenOffset = Vector3.zero;
184  }
185 
186  }
187 }
Button logic for the App Bar. Determines position of the button in the App Bar, visibility based on t...
Definition: AppBarButton.cs:13
Mesh button is a mesh renderer interactable with state data for button state
AppBarStateEnum State
Definition: AppBar.cs:146
The base class for button icon profiles
Class used for building toolbar buttons (not yet in use)
Definition: AppBar.cs:56
float Alpha
Property to use in IconMaterial for alpha control Useful for animating icon transparency ...
Concrete version of Button class used with other CompoundButton scripts (e.g., CompoundButtonMesh) Al...
virtual void Registerinteractable(GameObject interactable)
Register an interactable with this receiver.
Logic for the App Bar. Generates buttons, manages states.
Definition: AppBar.cs:17
void Initialize(AppBar newParentToolBar, AppBar.ButtonTemplate newTemplate, ButtonIconProfile newCustomProfile)
Definition: AppBarButton.cs:32