subere23

Chp9 MoveTool

Oct 1st, 2017
759
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using HoloToolkit.Unity.InputModule;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class MoveTool : MonoBehaviour, IFocusable, IInputHandler, ISourceStateHandler, IManipulationHandler
  7. {
  8.  
  9. public Transform HostTransform;
  10.  
  11. [Range(0.01f, 1.0f)]
  12. public float PositionLerpSpeed = 0.2f;
  13.  
  14.  
  15. [Range(0.01f, 1.0f)]
  16. public float RotationLerpSpeed = 0.2f;
  17.  
  18. public float DistanceScale = 8f;
  19.  
  20. public bool IsDraggingEnabled = true;
  21.  
  22. private bool isDragging;
  23. private bool isGazed;
  24.  
  25. private Vector3 manipulationEventData;
  26. private Vector3 manipulationDelta;
  27.  
  28. private Camera mainCamera;
  29. private IInputSource currentInputSource;
  30. private uint currentInputSourceId;
  31.  
  32. void Start()
  33. {
  34. if (HostTransform == null)
  35. {
  36. //This is temporary so we dont get a null exception.
  37. HostTransform = transform;
  38. }
  39.  
  40. mainCamera = Camera.main;
  41. }
  42.  
  43. void Update()
  44. {
  45. Quaternion currentRot = HostTransform.transform.rotation;
  46.  
  47. if (NRSRManager.FocusedObject != null)
  48. {
  49. HostTransform = NRSRManager.FocusedObject.transform;
  50. }
  51. else
  52. {
  53. return;
  54. }
  55. if (IsDraggingEnabled && isDragging)
  56. {
  57. HostTransform.position = Vector3.Lerp(HostTransform.position, HostTransform.position + manipulationDelta, PositionLerpSpeed);
  58. }
  59. }
  60.  
  61. public void StartDragging()
  62. {
  63. if (!IsDraggingEnabled)
  64. {
  65. return;
  66. }
  67.  
  68. if (isDragging)
  69. {
  70. return;
  71. }
  72. NRSRManager.holdSelectedObject_UsingTransformTool = true;
  73. }
  74.  
  75. public void StopDragging()
  76. {
  77. if (!isDragging)
  78. {
  79. NRSRManager.holdSelectedObject_UsingTransformTool = false;
  80. return;
  81. }
  82.  
  83. InputManager.Instance.PopModalInputHandler();
  84. isDragging = false;
  85. currentInputSource = null;
  86. }
  87.  
  88. public void OnFocusEnter()
  89. {
  90. if (!IsDraggingEnabled)
  91. {
  92. return;
  93. }
  94. if (isGazed)
  95. {
  96. return;
  97. }
  98. isGazed = true;
  99. }
  100.  
  101. public void OnFocusExit()
  102. {
  103. if (!IsDraggingEnabled)
  104. {
  105. return;
  106. }
  107. if (!isGazed)
  108. {
  109. return;
  110. }
  111. isGazed = false;
  112. }
  113.  
  114. public void OnInputDown(InputEventData eventData)
  115. {
  116.  
  117. if (isDragging)
  118. {
  119. return;
  120. }
  121.  
  122. if (!eventData.InputSource.SupportsInputInfo(eventData.SourceId, SupportedInputInfo.Position))
  123. {
  124. return;
  125. }
  126.  
  127. if (!IsDraggingEnabled)
  128. {
  129. return;
  130. }
  131. InputManager.Instance.PushModalInputHandler(gameObject);
  132.  
  133. isDragging = true;
  134. currentInputSource = eventData.InputSource;
  135. currentInputSourceId = eventData.SourceId;
  136. StartDragging();
  137. }
  138.  
  139. public void OnInputUp(InputEventData eventData)
  140. {
  141. Debug.Log("OnInputUp");
  142. if (currentInputSource != null &&
  143. eventData.SourceId == currentInputSourceId)
  144. {
  145. StopDragging();
  146. }
  147. }
  148.  
  149. public void OnSourceDetected(SourceStateEventData eventData)
  150. {
  151.  
  152. }
  153.  
  154. public void OnSourceLost(SourceStateEventData eventData)
  155. {
  156. if (currentInputSource != null && eventData.SourceId == currentInputSourceId)
  157. {
  158. StopDragging();
  159. }
  160. }
  161.  
  162. public void OnManipulationStarted(ManipulationEventData eventData)
  163. {
  164. Debug.LogFormat("OnManipulationStarted\r\nSource: {0} SourceId: {1}\r\nCumulativeDelta: {2} {3} {4}",
  165. eventData.InputSource,
  166. eventData.SourceId,
  167. eventData.CumulativeDelta.x,
  168. eventData.CumulativeDelta.y,
  169. eventData.CumulativeDelta.z);
  170. manipulationEventData = eventData.CumulativeDelta;
  171. }
  172.  
  173. public void OnManipulationUpdated(ManipulationEventData eventData)
  174. {
  175. Debug.LogFormat("OnManipulationUpdated\r\nSource: {0} SourceId: {1}\r\nCumulativeDelta: {2} {3} {4}",
  176. eventData.InputSource,
  177. eventData.SourceId,
  178. eventData.CumulativeDelta.x,
  179. eventData.CumulativeDelta.y,
  180. eventData.CumulativeDelta.z);
  181.  
  182. Vector3 delta = eventData.CumulativeDelta - manipulationEventData;
  183. manipulationDelta = delta * DistanceScale;
  184. manipulationEventData = eventData.CumulativeDelta;
  185.  
  186.  
  187. }
  188.  
  189. public void OnManipulationCompleted(ManipulationEventData eventData)
  190. {
  191. manipulationEventData = Vector3.zero;
  192. manipulationDelta = Vector3.zero;
  193. }
  194.  
  195. public void OnManipulationCanceled(ManipulationEventData eventData)
  196. {
  197.  
  198. }
  199.  
  200. }
RAW Paste Data