AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
ButtonThemeWidget.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 UnityEngine;
5 using System.Collections;
7 using System;
8 
9 namespace HoloToolkit.Examples.InteractiveElements
10 {
15  {
16  [Tooltip("tag for the color button theme")]
17  public string ColorThemeTag = "defaultColor";
18 
19  [Tooltip("tag for the position button theme")]
20  public string PositionThemeTag = "defaultPosition";
21 
22  [Tooltip("tag for the scale button theme")]
23  public string ScaleThemeTag = "defaultScale";
24 
25  [Tooltip("Color transition animation component: optional")]
27 
28  [Tooltip("position animation component: optional")]
30 
31  [Tooltip("scale animation component: optional")]
33 
34  // themes
35  private ColorInteractiveTheme mColorTheme;
36  private Vector3InteractiveTheme mPositionTheme;
37  private Vector3InteractiveTheme mScaleTheme;
38 
39  // material
40  private Material mMaterial;
41 
42  private string mCheckColorThemeTag = "";
43  private string mCheckPositionThemeTag = "";
44  private string mCheckScaleThemeTag = "";
45 
49  private void Awake()
50  {
51  if (ColorBlender == null)
52  {
53  ColorBlender = GetComponent<ColorTransition>();
54  }
55 
56  if (MovePosition == null)
57  {
58  MovePosition = GetComponent<MoveToPosition>();
59  }
60 
61  if (ScaleSize == null)
62  {
63  ScaleSize = GetComponent<ScaleToValue>();
64  }
65 
66  // get renderer
67  Renderer renderer = GetComponent<Renderer>();
68  if (renderer != null)
69  {
70  mMaterial = renderer.material;
71  }
72  }
73 
74  private void Start()
75  {
76  SetTheme();
77  RefreshIfNeeded();
78  }
79 
80  public override void SetTheme()
81  {
82  if (ColorThemeTag != "")
83  {
84  mColorTheme = GetColorTheme(ColorThemeTag);
85  mCheckColorThemeTag = ColorThemeTag;
86  }
87 
88  if (PositionThemeTag != "")
89  {
90  mPositionTheme = GetVector3Theme(PositionThemeTag);
91  mCheckPositionThemeTag = PositionThemeTag;
92  }
93 
94  if (ScaleThemeTag != "")
95  {
96  mScaleTheme = GetVector3Theme(ScaleThemeTag);
97  mCheckScaleThemeTag = ScaleThemeTag;
98  }
99  }
100 
105  public override void SetState(Interactive.ButtonStateEnum state)
106  {
107  base.SetState(state);
108 
109  if (mColorTheme != null)
110  {
111  if (ColorBlender != null)
112  {
113  ColorBlender.StartTransition(mColorTheme.GetThemeValue(state));
114  }
115  else if(mMaterial != null)
116  {
117  mMaterial.color = mColorTheme.GetThemeValue(state);
118  }
119  }
120 
121  if (mPositionTheme != null)
122  {
123  if (MovePosition != null)
124  {
125  MovePosition.TargetValue = mPositionTheme.GetThemeValue(state);
126  MovePosition.StartRunning();
127  }
128  else
129  {
130  transform.localPosition = mPositionTheme.GetThemeValue(state);
131  }
132  }
133 
134  if (mScaleTheme != null)
135  {
136  if (ScaleSize != null)
137  {
138  ScaleSize.TargetValue = mScaleTheme.GetThemeValue(state);
139  ScaleSize.StartRunning();
140  }
141  else
142  {
143  transform.localScale = mScaleTheme.GetThemeValue(state);
144  }
145  }
146  }
147 
148  private void Update()
149  {
150  if(!mCheckScaleThemeTag.Equals(ScaleThemeTag) || !mCheckPositionThemeTag.Equals(PositionThemeTag) || !mCheckColorThemeTag.Equals(ColorThemeTag))
151  {
152  SetTheme();
153  RefreshIfNeeded();
154  }
155  }
156 
160  private void OnDestroy()
161  {
162  if(mMaterial != null)
163  {
164  GameObject.Destroy(mMaterial);
165  }
166  }
167  }
168 }
void StartRunning()
Start the animation
Definition: ScaleToValue.cs:66
Animates the scaling of an object with eases
Definition: ScaleToValue.cs:14
updates the button colors, position and scale based on the button theme
Type GetThemeValue(Interactive.ButtonStateEnum state)
override void SetTheme()
Sets the themes based on the Theme Tags
A position animation component that supports easing and local position The animation can be triggered...
override void SetState(Interactive.ButtonStateEnum state)
set states or start animations
ButtonStateEnum
A button typically has 8 potential states. We can update visual feedback based on state change...
Definition: Interactive.cs:80
A version of InteractiveWidget that uses an InteractiveTheme to define each state ...
void StartTransition(Color color, string name="")
Fades the color of a material called by name
Interactive exposes basic button type events to the Unity Editor and receives messages from the Gestu...
Definition: Interactive.cs:22
A color blending animation component, handles multiple materials