Tanoda
PreviewHand.cs
Go to the documentation of this file.
1using System.Collections;
2using System.Collections.Generic;
3using UnityEngine;
4using UnityEngine.UI;
5
6public class PreviewHand : MonoBehaviour
7{
8 private OffsetHolder offsetHolder;
10 public GameObject handPrefab2, leftHandPrefab2;
11 private GameObject objectToGrab;
12 private Transform attachPoint, attachPointLeft;
13 private Transform originalParent;
14 List<GameObject> cachedGameObjects = new List<GameObject>();
15 List<ActionObject> cachedActionObjects = new List<ActionObject>();
16 public static PreviewHand Instance { get; private set; }
17 private bool editingLeft = false;
18 public Toggle toggle;
19 private ActionObject actionObject;
20 private Vector3 handPosition;
21 private GameObject rightHand, leftHand;
22 Vector3 targetPosition, targetRotation, originalPosition, originalRotation;
23
24#if !UNITY_WEBGL
25 // Start is called before the first frame update
26 void Start()
27 {
28 Instance = this;
29 if (MiscLogicManager.instance.mode != MiscLogicManager.CurrentMode.Training && MiscLogicManager.instance.mode != MiscLogicManager.CurrentMode.Optimization) toggle.gameObject.SetActive(false);
30 rightHand = GameObject.Find("SteamVR_Tracker");
31 leftHand = GameObject.Find("SteamVR_Tracker");
32 }
33 private void Update()
34 {
35 if (rightHand != null)
36 {
37 handPosition = rightHand.transform.position;
38 }
39 }
40 public void EnterOffsetEditor(GameObject objectToGrab, ActionObject action, Vector3 TargetPos, Vector3 TargetRot)
41 {
42 if (objectToGrab == null) return;
43 cachedGameObjects.Add(objectToGrab);
44 cachedActionObjects.Add(action);
45 actionObject = action;
46 targetPosition = TargetPos;
47 targetRotation = TargetRot;
48 originalPosition = objectToGrab.transform.localPosition;
49 originalRotation = objectToGrab.transform.localEulerAngles;
50 if (handPrefab == null)
51 {
52 Debug.LogError("couldn't find glove, offset editor cannot be entered");
53 return;
54 }
55
56 //objectToGrab = pb_Selection.activeGameObject;
57 if (cachedGameObjects[0] == null)
58 {
59 Debug.LogError("no selected object, offset editor cannot be entered");
60 return;
61 }
62
63 Vector3 scale = cachedGameObjects[0].transform.localScale;
64 if (scale.x == 0f || scale.y == 0f || scale.z == 0f)
65 {
66 Debug.LogError("object has at least one scale component set to 0, offset editor cannot be entered");
67 return;
68 }
69
70
71 StartCoroutine(moveTheHand());
72 }
73 public void hideHandPreview(GameObject objectToGrab)
74 {
75 handPrefab.SetActive(false);
76 leftHandPrefab.SetActive(false);
77 handPrefab.transform.SetParent(null, true);
78 leftHandPrefab.transform.SetParent(null, true);
79 if (attachPoint != null)
80 {
81 attachPoint.parent = handPrefab.transform;
82 attachPointLeft.parent = leftHandPrefab.transform;
83 }
84
85 handPrefab.transform.localScale = Vector3.one;
86 leftHandPrefab.transform.localScale = Vector3.one;
87 if (attachPoint != null)
88 {
89 attachPoint.localScale = Vector3.one;
90 attachPointLeft.localScale = Vector3.one;
91 }
92
93 if (cachedGameObjects.Count > 0)
94 {
95 var index = cachedGameObjects.IndexOf(objectToGrab);
96 cachedGameObjects.Remove(objectToGrab);
97 cachedActionObjects.RemoveAt(index);
98 }
99 if (cachedGameObjects.Count > 0) StartCoroutine(moveTheHand());
100 //StopAllCoroutines();
101
102 if (cachedGameObjects.Count == 0)
103 StopAllCoroutines();
104
105 }
106
107 public Vector3 GetHandRootPos(OffsetHolder oh, bool left = false)
108 {
109 var go = oh.gameObject;
110
111 var hr = Instantiate(handPrefab2, null);
112 var hl = Instantiate(leftHandPrefab2, null);
113
114 hr.SetActive(true);
115 hl.SetActive(true);
116
117 var ap = hr.transform.Find("AttachPoint");
118 var apl = hl.transform.Find("AttachPoint");
119
120 ap.SetParent(null, true);
121 apl.SetParent(null, true);
122
123 hr.transform.SetParent(ap, true);
124 hl.transform.SetParent(apl, true);
125
126 ap.position = go.transform.TransformPoint(oh.Offset);
127 ap.rotation = go.transform.rotation * Quaternion.Euler(oh.Rotation);
128 apl.position = go.transform.TransformPoint(oh.LeftOffset);
129 apl.rotation = go.transform.rotation * (Quaternion.Euler(oh.LeftRotation));
130
131 var retval = left ? Macro.FindDeepChild(apl.transform, "Root").position : Macro.FindDeepChild(ap.transform, "Root").position;
132
133 Destroy(ap.gameObject);
134 Destroy(apl.gameObject);
135
136 return retval;
137
138 }
139
140 IEnumerator moveTheHand()
141 {
142 if (cachedGameObjects[0].GetComponent<OffsetHolder>() != null)
143 {
144 offsetHolder = cachedGameObjects[0].GetComponent<OffsetHolder>();
145 bool left = false;
146 if (attachPoint == null)
147 {
148 attachPoint = handPrefab.transform.Find("AttachPoint");
149 attachPointLeft = leftHandPrefab.transform.Find("AttachPoint");
150 }
151
152 if (attachPoint == null) yield break;
153
154 attachPoint.SetParent(null, true);
155 attachPointLeft.SetParent(null, true);
156
157 yield return null;
158 handPrefab.transform.SetParent(attachPoint, true);
159 leftHandPrefab.transform.SetParent(attachPointLeft, true);
160 yield return null;
161
162 if (cachedActionObjects[0] is PositionAction pa && pa.GetHandOffsets(out var o, out var r, out var lo, out var lr))
163 {
164 attachPoint.position = cachedGameObjects[0].transform.TransformPoint(o);
165 attachPoint.rotation = cachedGameObjects[0].transform.rotation * Quaternion.Euler(r);
166 attachPointLeft.position = cachedGameObjects[0].transform.TransformPoint(lo);
167 attachPointLeft.rotation = cachedGameObjects[0].transform.rotation * (Quaternion.Euler(lr));
168 }
169 else
170 {
171 attachPoint.position = cachedGameObjects[0].transform.TransformPoint(offsetHolder.Offset);
172 attachPoint.rotation = cachedGameObjects[0].transform.rotation * Quaternion.Euler(offsetHolder.Rotation);
173 attachPointLeft.position = cachedGameObjects[0].transform.TransformPoint(offsetHolder.LeftOffset);
174 attachPointLeft.rotation = cachedGameObjects[0].transform.rotation * (Quaternion.Euler(offsetHolder.LeftRotation));
175 }
176
177 if (actionObject is MoveAction)
178 {
179 handPrefab.SetActive(true);
180 originalPosition = attachPoint.position;
181 Vector3 originalLocalPosition = attachPoint.transform.position - cachedGameObjects[0].transform.position;
182 float time = 0;
183 float duration = 1f;
184 while (true)
185 {
186 time += Time.deltaTime;
187 if (time >= duration)
188 {
189 time = 0;
190 attachPoint.transform.position = originalPosition;
191 }
192 attachPoint.transform.position = Vector3.Lerp(originalPosition, originalLocalPosition + targetPosition, time / duration);
193
194 yield return null;
195 }
196 }
197
198 }
199 if (cachedGameObjects[0].GetComponent<OffsetHolder>() == null)
200 {
201 var offsetTool = cachedGameObjects[0].GetComponent<ThrowableCanDisable>();
202 bool left = false;
203 if (attachPoint == null)
204 {
205 attachPoint = handPrefab.transform.Find("AttachPoint");
206 attachPointLeft = leftHandPrefab.transform.Find("AttachPoint");
207 }
208
209 if (attachPoint == null) yield break;
210
211 attachPoint.SetParent(null, true);
212 attachPointLeft.SetParent(null, true);
213
214 yield return null;
215 handPrefab.transform.SetParent(attachPoint, true);
216 leftHandPrefab.transform.SetParent(attachPointLeft, true);
217 yield return null;
218
219 if (cachedActionObjects[0] is PositionAction pa && pa.GetHandOffsets(out var o, out var r, out var lo, out var lr))
220 {
221 attachPoint.position = cachedGameObjects[0].transform.TransformPoint(o);
222 attachPoint.rotation = cachedGameObjects[0].transform.rotation * Quaternion.Euler(r);
223 attachPointLeft.position = cachedGameObjects[0].transform.TransformPoint(lo);
224 attachPointLeft.rotation = cachedGameObjects[0].transform.rotation * (Quaternion.Euler(lr));
225 }
226 else
227 {
228 attachPoint.position = cachedGameObjects[0].transform.TransformPoint(offsetTool.attachmentOffset.localPosition);
229 //attachPoint.position = offsetTool.attachmentOffset.position;
230 attachPoint.rotation = cachedGameObjects[0].transform.rotation * Quaternion.Euler(offsetTool.attachmentOffset.localEulerAngles);
231 attachPointLeft.position = cachedGameObjects[0].transform.TransformPoint(offsetTool.attachmentOffsetLeft.localPosition);
232 //attachPointLeft.position = offsetTool.attachmentOffsetLeft.position;
233 attachPointLeft.rotation = cachedGameObjects[0].transform.rotation * (Quaternion.Euler(offsetTool.attachmentOffsetLeft.localEulerAngles));
234
235 }
236
237 if (actionObject is MoveAction)
238 {
239 handPrefab.SetActive(true);
240 originalPosition = attachPoint.position;
241 Vector3 originalLocalPosition = attachPoint.transform.position - cachedGameObjects[0].transform.position;
242 float time = 0;
243 float duration = 1f;
244 while (Vector3.Distance(handPosition, cachedGameObjects[0].transform.position) >= 0.2f)
245 {
246 time += Time.deltaTime;
247 if (time >= duration)
248 {
249 time = 0;
250 attachPoint.transform.position = originalPosition;
251 }
252 attachPoint.transform.position = Vector3.Lerp(originalPosition, originalLocalPosition + targetPosition, time / duration);
253
254 yield return null;
255 }
256 }
257 }
258
259 handPrefab.SetActive(true);
260 if (cachedGameObjects[0].GetComponent<TwoHandGrab>())
261 {
262 leftHandPrefab.SetActive(true);
263 }
264
265 }
266#endif
267 public void TurnOffOnPreviewHand(bool newValue)
268 {
269 turnableHand.SetActive(newValue);
270 leftTurnableHand.SetActive(newValue);
271 }
272}
273
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
Definition: Macro.cs:12
static Transform FindDeepChild(Transform aParent, string aName)
Definition: Macro.cs:149
Vector3 Offset
Definition: OffsetHolder.cs:7
Vector3 LeftRotation
Definition: OffsetHolder.cs:10
Vector3 Rotation
Definition: OffsetHolder.cs:8
Vector3 LeftOffset
Definition: OffsetHolder.cs:9
bool GetHandOffsets(out Vector3 offset, out Vector3 rotation, out Vector3 leftoffset, out Vector3 leftrotation)
GameObject turnableHand
Definition: PreviewHand.cs:9
void TurnOffOnPreviewHand(bool newValue)
Definition: PreviewHand.cs:267
void hideHandPreview(GameObject objectToGrab)
Definition: PreviewHand.cs:73
void EnterOffsetEditor(GameObject objectToGrab, ActionObject action, Vector3 TargetPos, Vector3 TargetRot)
Definition: PreviewHand.cs:40
GameObject handPrefab2
Definition: PreviewHand.cs:10
Toggle toggle
Definition: PreviewHand.cs:18
Vector3 GetHandRootPos(OffsetHolder oh, bool left=false)
Definition: PreviewHand.cs:107
GameObject leftHandPrefab2
Definition: PreviewHand.cs:10
GameObject handPrefab
Definition: PreviewHand.cs:9
GameObject leftHandPrefab
Definition: PreviewHand.cs:9
GameObject leftTurnableHand
Definition: PreviewHand.cs:9
static PreviewHand Instance
Definition: PreviewHand.cs:16