Tanoda
Pointer.cs
Go to the documentation of this file.
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using UnityEngine;
7using Button = UnityEngine.UI.Button;
8#if !UNITY_WEBGL
9using Valve.VR;
10using UnityEngine.UI;
11#endif
12#if DANA
13using ViveHandTracking;
14#endif
15
16public class Pointer : MonoBehaviour
17{
18 public float defaultLength = 3.0f;
19 public float time;
20 private bool IsSelected = false;
21 public EventSystem eventSystem = null;
22 public StandaloneInputModule inputModule = null;
23 public LayerMask interactableMask = 0;
24 public UnityAction<Vector3, bool> OnPointerUpdate = null;
25 private Camera playerCamera;
26 private LineRenderer lineRenderer = null;
27 private GameObject currentObject = null;
29#if !UNITY_WEBGL
30 public SteamVR_Action_Boolean uiInteractAction = SteamVR_Input.GetAction<SteamVR_Action_Boolean>("InteractUI");
31#endif
32#if DANA
33 public bool IsLeft = false;
34 public GestureType selectGesture = GestureType.Like;
35#endif
36
37 private void Awake()
38 {
39 lineRenderer = GetComponent<LineRenderer>();
40 playerCamera = GetComponent<Camera>();
41 }
42
43 private void Update()
44 {
45 if (
46#if DANA
47 SavedUser.instance.isEditor ||
48#endif
49 !QuizManager.instance.gameObject.activeSelf && DobotEditorManager.instance == null &&
50 !StageCanvas.instance.gameObject.activeSelf && DobotEditorManager.instance == null)
51 {
52 GetComponentInChildren<SpriteRenderer>().enabled = false;
53 lineRenderer.enabled = false;
54
55 return;
56 }
57
58 UpdatePointer();
59 if (currentObject && currentObject.GetComponent<PointerInteractable>())
60 {
61 GetComponentInChildren<SpriteRenderer>().enabled = true;
62#if DANA
63 if (GestureProvider.Current.engine.State.LeftHand.gesture == GestureType.Victory && Time.time > time)
64 {
65 //currentObject.GetComponent<PointerInteractable>()?.OnPointerClick(null);
66 //Input.GetKey(KeyCode.Mouse0);
67
68 //ExecuteEvents.Execute(buttonToFire.gameObject, new BaseEventData(EventSystem.current), ExecuteEvents.submitHandler);
69 time = Time.time+1.0f;
70 }
71
72 // GestureResult result = IsLeft ? GestureProvider.LeftHand : GestureProvider.RightHand;
73 //if (result != null && result.gesture == selectGesture)
74 //{
75 // currentObject.GetComponent<PointerInteractable>()?.OnPointerClick(null);
76 // }
77#else
78#if !UNITY_WEBGL
79 if (uiInteractAction.stateDown)
80 {
81 currentObject.GetComponent<PointerInteractable>()?.OnPointerDown(null);
82 }
83
84 if (uiInteractAction.stateUp)
85 {
86 currentObject.GetComponent<PointerInteractable>()?.OnPointerUp(null);
87 currentObject.GetComponent<PointerInteractable>()?.OnPointerClick(null);
88 }
89#endif
90#endif
91 }
92 else
93 {
94 GetComponentInChildren<SpriteRenderer>().enabled = false;
95 }
96 }
97 IEnumerator ClickButton(Button b)
98 {
99 var pointer = new PointerEventData(EventSystem.current);
100
101 ExecuteEvents.Execute(b.gameObject, pointer, ExecuteEvents.pointerEnterHandler);
102 ExecuteEvents.Execute(b.gameObject, pointer, ExecuteEvents.submitHandler);
103
104 yield return new WaitForSeconds(0.1f);
105 ExecuteEvents.Execute(b.gameObject, pointer, ExecuteEvents.pointerExitHandler);
106 }
107 IEnumerator WaitFrame()
108 {
109 yield return null;
110 }
111 private void UpdatePointer()
112 {
113 Vector3 endPosition = GetEnd();
114
115 UpdateLength(endPosition);
116 UpdatePointerStatus(endPosition);
117 }
118
119 private void UpdateLength(Vector3 endPosition)
120 {
121 lineRenderer.SetPosition(0, transform.position);
122 lineRenderer.SetPosition(1, endPosition);
123 }
124
125 private void UpdatePointerStatus(Vector3 hitPoint)
126 {
127 bool hit = false;
128
129 if (currentObject != null)
130 hit = interactableMask == (interactableMask | (1 << currentObject.layer));
131
132 if (OnPointerUpdate != null)
133 OnPointerUpdate(hitPoint, hit);
134 }
135
136 private Vector3 GetEnd()
137 {
138 float distance = GetDistance();
139
140
141 if (distance != 0.0f)
142 return CalculateEnd(distance);
143
144 return CalculateEnd(defaultLength);
145 }
146
147 private float GetDistance()
148 {
149 PointerEventData eventData = new PointerEventData(eventSystem);
150 List<RaycastResult> results = new List<RaycastResult>();
151
153 if (inputModule.inputOverride)
154 {
155 eventData.position = inputModule.inputOverride.mousePosition;
156 }
157 else
158 {
159 eventData.position = new Vector2(playerCamera.pixelWidth / 2, playerCamera.pixelHeight / 2);
160 }
161 eventSystem.RaycastAll(eventData, results);
162
163 RaycastResult closestResult = FindFirstRaycast(results);
164 float distance = closestResult.distance;
165 var backupObject = currentObject;
166 currentObject = closestResult.gameObject;
167
168 if (!currentObject)
169 {
170 var rchits = Physics.RaycastAll(transform.position, transform.forward, 5, interactableMask);
171 var closest = 9999.9f;
172 foreach (var raycastHit in rchits)
173 {
174 if (raycastHit.transform && closest > raycastHit.distance)
175 {
176 if (!raycastHit.transform.gameObject.GetComponent<PointerInteractable>())
177 {
178 continue;
179 }
180 closest = raycastHit.distance;
181 currentObject = raycastHit.transform.gameObject;
182 }
183 }
184
185 if (closest == 9999.9f)
186 {
187 currentObject = null;
188 closest = 0.0f;
189 }
190
191 if (backupObject != null && currentObject != backupObject)
192 {
193 backupObject.GetComponent<PointerInteractable>()?.OnPointerExit(null);
194 }
195
196 closest = Mathf.Clamp(closest, 0.0f, defaultLength);
197 lineRenderer.enabled = closest != defaultLength && closest != 0;
198 return closest;
199 }
200 else
201 {
202 if (currentObject != null && closestResult.gameObject != currentObject)
203 {
204 currentObject.GetComponent<PointerInteractable>()?.OnPointerExit(null);
205 }
206 }
207
208 distance = Mathf.Clamp(distance, 0.0f, defaultLength);
209
210 lineRenderer.enabled = distance != defaultLength && distance != 0;
211
212 //MyEventSystem.instance.enabled = lineRenderer.enabled;
213
214 return distance;
215 }
216
217 private RaycastResult FindFirstRaycast(List<RaycastResult> results)
218 {
219 foreach (RaycastResult result in results)
220 {
221 if (!result.gameObject || !HasParentQuiz(result.gameObject))
222 continue;
223
224 return result;
225 }
226
227 return new RaycastResult();
228 }
229
230 private bool HasParentQuiz(GameObject go)
231 {
232 try
233 {
234 if (go.CompareTag("Quiz"))
235 {
236 return true;
237 }
238 }
239 catch (Exception)
240 {
241 return false;
242 }
243
244 return false;
245 }
246
247 private Vector3 CalculateEnd(float length)
248 {
249 return transform.position + transform.forward * length;
250 }
251}
UnityEngine.UI.Button Button
Definition: Pointer.cs:7
static DobotEditorManager instance
float defaultLength
Definition: Pointer.cs:18
SteamVR_Action_Boolean uiInteractAction
Definition: Pointer.cs:30
float time
Definition: Pointer.cs:19
EventSystem eventSystem
Definition: Pointer.cs:21
UnityAction< Vector3, bool > OnPointerUpdate
Definition: Pointer.cs:24
Button buttonToFire
Definition: Pointer.cs:28
StandaloneInputModule inputModule
Definition: Pointer.cs:22
LayerMask interactableMask
Definition: Pointer.cs:23