Newer
Older
HoloAnatomy / Assets / HoloToolkit / Input / Scripts / Cursor / InteractiveMeshCursor.cs
SURFACEBOOK2\jackwynne on 25 May 2018 5 KB v1
  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. {
  10. /// <summary>
  11. /// A cursor that looks and acts more like the shell cursor.
  12. /// A two part cursor with visual feedback for all cursor states
  13. /// </summary>
  14. public class InteractiveMeshCursor : Cursor
  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.  
  40. /// <summary>
  41. /// internal state and element management
  42. /// </summary>
  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. private void Awake()
  55. {
  56. mAwakeScale = transform.localScale;
  57. }
  58.  
  59. /// <summary>
  60. /// Decide which element (ring or dot) should be visible and at what scale
  61. /// </summary>
  62. /// <param name="state"></param>
  63. public override void OnCursorStateChange(CursorStateEnum state)
  64. {
  65. base.OnCursorStateChange(state);
  66.  
  67. // the cursor state has changed, reset the animation timer
  68. if (mHasHand != this.IsHandVisible || mIsDown != this.IsInputSourceDown || mHasHover != (this.TargetedObject != null))
  69. {
  70. mTimer = 0;
  71. }
  72.  
  73. mHasHand = this.IsHandVisible;
  74. mIsDown = this.IsInputSourceDown;
  75. mHasHover = this.TargetedObject != null;
  76.  
  77. mTargetScale = mBaseScale * DefaultScale;
  78. bool showRing = false;
  79.  
  80. switch (state)
  81. {
  82. case CursorStateEnum.None:
  83. break;
  84. case CursorStateEnum.Observe:
  85. break;
  86. case CursorStateEnum.ObserveHover:
  87. showRing = true;
  88. break;
  89. case CursorStateEnum.Interact:
  90. showRing = true;
  91. mTargetScale = mBaseScale * DownScale;
  92. break;
  93. case CursorStateEnum.InteractHover:
  94. showRing = true;
  95. mTargetScale = mBaseScale * UpScale;
  96. break;
  97. case CursorStateEnum.Select:
  98. mTargetScale = mBaseScale * UpScale;
  99. break;
  100. case CursorStateEnum.Release:
  101. break;
  102. case CursorStateEnum.Contextual:
  103. break;
  104. default:
  105. break;
  106. }
  107.  
  108. if (!mIsVisible)
  109. {
  110. return;
  111. }
  112.  
  113. Ring.SetActive(showRing);
  114. Dot.SetActive(!showRing);
  115.  
  116. // added observation of CursorModifier
  117. if (TargetedCursorModifier != null && mHasHover)
  118. {
  119. ElementVisibility(!TargetedCursorModifier.GetCursorVisibility());
  120. }
  121. }
  122.  
  123. /// <summary>
  124. /// scale the cursor elements
  125. /// </summary>
  126. protected override void UpdateCursorTransform()
  127. {
  128. base.UpdateCursorTransform();
  129.  
  130. // animate scale of ring and dot
  131. if (mTimer < ScaleTime)
  132. {
  133. mTimer += Time.deltaTime;
  134. if (mTimer > ScaleTime)
  135. {
  136. mTimer = ScaleTime;
  137. }
  138.  
  139. Ring.transform.localScale = Vector3.Lerp(mBaseScale * DefaultScale, mTargetScale, mTimer/ScaleTime);
  140. Dot.transform.localScale = Vector3.Lerp(mBaseScale * DefaultScale, mTargetScale, mTimer / ScaleTime);
  141. }
  142.  
  143. // handle scale of main cursor go
  144. float distance = Vector3.Distance(GazeManager.Instance.GazeOrigin, transform.position);
  145. float smoothscaling = 1 - DefaultCursorDistance * DistanceScaleFactor;
  146. transform.localScale = mAwakeScale * (distance * DistanceScaleFactor + smoothscaling);
  147. }
  148.  
  149. /// <summary>
  150. /// override the base class for custom visibility
  151. /// </summary>
  152. /// <param name="visible"></param>
  153. public override void SetVisibility(bool visible)
  154. {
  155. base.SetVisibility(visible);
  156.  
  157. mIsVisible = visible;
  158. ElementVisibility(visible);
  159.  
  160. if (visible)
  161. {
  162. OnCursorStateChange(CursorState);
  163. }
  164. }
  165.  
  166. /// <summary>
  167. /// controls the visibility of cursor elements in one place
  168. /// </summary>
  169. /// <param name="visible"></param>
  170. private void ElementVisibility(bool visible)
  171. {
  172. if (Ring != null)
  173. {
  174. Ring.SetActive(visible);
  175. }
  176.  
  177. if (Dot != null)
  178. {
  179. Dot.SetActive(visible);
  180. }
  181.  
  182. if (Light != null)
  183. {
  184. Light.SetActive(visible);
  185. }
  186. }
  187. }
  188. }