AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
InteractiveMeshCursor.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.Collections;
5 using System.Collections.Generic;
6 using UnityEngine;
7 
8 namespace HoloToolkit.Unity.InputModule
9 {
15  {
16  [Tooltip("The ring or outer element")]
17  public GameObject Ring;
18 
19  [Tooltip("Inner cursor element")]
20  public GameObject Dot;
21 
22  [Tooltip("Point light")]
23  public GameObject Light;
24 
25  [Tooltip("The scale factor to soften the distance scaling, we want the cursor to scale in the distance, but not disappear.")]
26  public float DistanceScaleFactor = 0.3f;
27 
28  [Tooltip("The scale both elements will be at their default state")]
29  public float DefaultScale = 0.75f;
30 
31  [Tooltip("The scale both elements will when pressed")]
32  public float DownScale = 0.5f;
33 
34  [Tooltip("The scale both elements will a hand is visible")]
35  public float UpScale = 1;
36 
37  [Tooltip("Time to scale between states")]
38  public float ScaleTime = 0.5f;
39 
43  private float mTimer = 0;
44 
45  private bool mHasHover = false;
46  private bool mHasHand = false;
47  private bool mIsDown = false;
48  private Vector3 mBaseScale = new Vector3(1, 1, 1);
49  private Vector3 mTargetScale;
50  private bool mIsVisible = true;
51 
52  private Vector3 mAwakeScale;
53 
54  protected override void Awake()
55  {
56  base.Awake();
57 
58  mAwakeScale = transform.localScale;
59  }
60 
65  public override void OnCursorStateChange(CursorStateEnum state)
66  {
67  base.OnCursorStateChange(state);
68 
69  // the cursor state has changed, reset the animation timer
70  if (mHasHand != this.IsHandVisible || mIsDown != this.IsInputSourceDown || mHasHover != (this.TargetedObject != null))
71  {
72  mTimer = 0;
73  }
74 
75  mHasHand = this.IsHandVisible;
76  mIsDown = this.IsInputSourceDown;
77  mHasHover = this.TargetedObject != null;
78 
79  mTargetScale = mBaseScale * DefaultScale;
80  bool showRing = false;
81 
82  switch (state)
83  {
84  case CursorStateEnum.None:
85  break;
86  case CursorStateEnum.Observe:
87  break;
88  case CursorStateEnum.ObserveHover:
89  showRing = true;
90  break;
91  case CursorStateEnum.Interact:
92  showRing = true;
93  mTargetScale = mBaseScale * DownScale;
94  break;
95  case CursorStateEnum.InteractHover:
96  showRing = true;
97  mTargetScale = mBaseScale * UpScale;
98  break;
99  case CursorStateEnum.Select:
100  mTargetScale = mBaseScale * UpScale;
101  break;
102  case CursorStateEnum.Release:
103  break;
104  case CursorStateEnum.Contextual:
105  break;
106  default:
107  break;
108  }
109 
110  if (!mIsVisible)
111  {
112  return;
113  }
114 
115  Ring.SetActive(showRing);
116  Dot.SetActive(!showRing);
117 
118  // added observation of CursorModifier
119  if (TargetedCursorModifier != null && mHasHover)
120  {
121  ElementVisibility(!TargetedCursorModifier.GetCursorVisibility());
122  }
123  }
124 
128  protected override void UpdateCursorTransform()
129  {
130  base.UpdateCursorTransform();
131 
132  // animate scale of ring and dot
133  if (mTimer < ScaleTime)
134  {
135  mTimer += Time.deltaTime;
136  if (mTimer > ScaleTime)
137  {
138  mTimer = ScaleTime;
139  }
140 
141  Ring.transform.localScale = Vector3.Lerp(mBaseScale * DefaultScale, mTargetScale, mTimer/ScaleTime);
142  Dot.transform.localScale = Vector3.Lerp(mBaseScale * DefaultScale, mTargetScale, mTimer / ScaleTime);
143  }
144 
145  // handle scale of main cursor go
146  float distance = Vector3.Distance(GazeManager.Instance.GazeOrigin, transform.position);
147  float smoothscaling = 1 - DefaultCursorDistance * DistanceScaleFactor;
148  transform.localScale = mAwakeScale * (distance * DistanceScaleFactor + smoothscaling);
149  }
150 
155  public override void SetVisibility(bool visible)
156  {
157  base.SetVisibility(visible);
158 
159  mIsVisible = visible;
160  ElementVisibility(visible);
161 
162  if (visible)
163  {
164  OnCursorStateChange(CursorState);
165  }
166  }
167 
172  private void ElementVisibility(bool visible)
173  {
174  if (Ring != null)
175  {
176  Ring.SetActive(visible);
177  }
178 
179  if (Dot != null)
180  {
181  Dot.SetActive(visible);
182  }
183 
184  if (Light != null)
185  {
186  Light.SetActive(visible);
187  }
188  }
189  }
190 }
The gaze manager manages everything related to a gaze ray that can interact with other objects...
Definition: GazeManager.cs:13
A cursor that looks and acts more like the shell cursor. A two part cursor with visual feedback for a...
static T Instance
Returns the Singleton instance of the classes type. If no instance is found, then we search for an in...
Definition: Singleton.cs:26
override void OnCursorStateChange(CursorStateEnum state)
Decide which element (ring or dot) should be visible and at what scale
override void UpdateCursorTransform()
scale the cursor elements
override void SetVisibility(bool visible)
override the base class for custom visibility
CursorStateEnum
Enum for current cursor state