2using System.Collections;
3using System.Collections.Generic;
4using System.Runtime.Serialization;
6using NaughtyAttributes;
11using System.Globalization;
13using Valve.VR.InteractionSystem;
21 internal Vector3 targetPosition, targetRotation;
22 internal bool activated;
24 internal Vector3 currpos, currrot;
25 internal GameObject highlightObject, highlightTool;
26 [SerializeField]
private GameObject canvasSelector;
29 private GameObject[] generatedTriggers;
30 internal GameObject finishBtn, inspector, canvas, stageManagerWindow;
31 private Coroutine mouseOver;
32 internal float time = 0.0f;
34 internal float holdTime = 2.0f;
35 private float currTime;
36 private float realTime = 0.0f;
39 private GameObject ProgressCanvas;
40 private Camera VRCamera;
41 internal bool isPenalty =
false;
42 private bool inProgress =
false;
44 private List<GameObject> serials;
45 private List<string> serialIds;
46 internal bool low =
false;
47 internal bool high =
false;
50 private Valve.VR.SteamVR_TrackedObjectWithEvents selectedTracked;
53 internal string selectedSerial;
55 private Coroutine progressRoutine;
61 currTime = holdTime * 2;
64 serials =
new List<GameObject>();
65 serialIds =
new List<string>();
66 var allTrackers = FindObjectsOfType<Valve.VR.SteamVR_TrackedObjectWithEvents>();
68 foreach (var trackedDevice
in allTrackers)
70 if (!
string.IsNullOrEmpty(trackedDevice.SerialID))
72 serials.Add(trackedDevice.gameObject);
73 trackedDevice.name = trackedDevice.SerialID;
74 serialIds.Add(trackedDevice.SerialID);
78 drop.AddOptions(serialIds);
80 canvasSelector = GameObject.Find(
"Canvas_selector");
81 stageManagerWindow = GameObject.Find(
"StageManager Window");
84 canvas = GameObject.Find(
"Canvas");
85 var trs = canvas.GetComponentsInChildren<Transform>(
true);
86 foreach (var t
in trs)
87 if (t.name ==
"Inspector")
88 inspector = t.gameObject;
90 trs = inspector.GetComponentsInChildren<Transform>(
true);
91 foreach (var t
in trs)
92 if (t.name ==
"FinishToolPos_Btn")
93 finishBtn = t.gameObject;
96 VRCamera = GameObject.Find(
"VRCamera")?.GetComponent<Camera>();
98 VRCamera = Camera.current;
100 ProgressCanvas = GameObject.Find(
"Canvas_ProgressBar").gameObject;
106 if (targetPosition ==
default)
return;
109 mouseOver = StartCoroutine(ShowHighLight());
115 StopCoroutine(mouseOver);
116 Destroy(highlightObject);
117 Destroy(highlightTool);
126 if (!inspector.activeSelf)
127 inspector.SetActive(
true);
129 var btnOffset = finishBtn.transform.parent.GetComponent<RectTransform>().rect.width;
131 finishBtn.GetComponent<RectTransform>().anchoredPosition =
new Vector2(-(btnOffset + spacing), 0);
133 finishBtn.SetActive(
true);
138 finishBtn.SetActive(
false);
147 var ci = CultureInfo.CurrentCulture;
148 var nfi = ci.NumberFormat;
151 var single = Convert.ToSingle(
holdInput.text, ci);
153 holdTime =
float.Parse(value, ci);
157 Debug.LogWarning(
"not parsable number(?) in holdTime inputfield!");
160 holdInput.text = holdTime.ToString(
"F2");
171 GameObject tool =
null;
173 tool = serials[
drop.value];
174 tool.GetComponent<Valve.VR.SteamVR_TrackedObjectWithEvents>().enabled =
false;
179 currpos = tool.transform.position;
180 currrot = tool.transform.eulerAngles;
181 GameObject.Find(
"Btn_StageMgr").GetComponent<
Button>().onClick.Invoke();
183 finishBtn.GetComponent<
Button>().onClick.RemoveAllListeners();
184 finishBtn.GetComponent<
Button>().onClick.AddListener(OnFinishBtn);
192 var tool = serials[
drop.value];
193 SaveTarget(tool.transform);
194 tool.GetComponent<Valve.VR.SteamVR_TrackedObjectWithEvents>().enabled =
true;
200 void SaveTarget(Transform t)
202 targetPosition = t.position;
203 targetRotation = t.eulerAngles;
208 if (
Math.Abs(t.position.x - targetPosition.x) > 0.05f)
return false;
209 if (
Math.Abs(t.position.y - targetPosition.y) > 0.05f)
return false;
210 if (
Math.Abs(t.position.z - targetPosition.z) > 0.09f)
return false;
217 if (Quaternion.Angle(t.rotation, Quaternion.Euler(targetRotation)) < 25)
return true;
225 if (checker >= 2)
return true;
231 if (checker >= 2)
return true;
238 var result = eulerAngles - Mathf.CeilToInt(eulerAngles / 360f) * 360f;
239 if (result < 0) result += 360f;
248 if (base.isInFieldOfView(GetToolObject())) base.actionRecognized =
true;
251 if (!activated || done)
return;
253 var tool = GetToolObject();
259 bool isRunning =
false;
260 if (tool.GetComponent<
TwoHandGrab>() ==
null) isRunning =
false;
263 var t = tool.transform;
266 if (!highlightObject )
268 StartCoroutine(ShowHighLight());
278 if (
InGoodPos(t) && selectedTracked && selectedTracked.IsTriggered)
281 ProgressCanvas.transform.SetParent(t,
true);
282 time += Time.deltaTime;
283 if (progressRoutine ==
null)
285 Image Progress = ProgressCanvas.GetComponentInChildren<
Image>(
true);
286 ProgressCanvas.transform.localPosition =
new Vector3(0, 0, 0);
287 ProgressCanvas.transform.position +=
new Vector3(0, 0.1f, 0);
288 progressRoutine = StartCoroutine(ProgressBar(holdTime, Progress, ProgressCanvas));
301 }
else if (selectedTracked && !selectedTracked.IsTriggered)
304 if (progressRoutine !=
null)
306 StopCoroutine(progressRoutine);
307 StartCoroutine(hideProgressBar());
308 progressRoutine =
null;
310 Image progressImage = ProgressCanvas.GetComponentInChildren<
Image>(
true);
311 if (progressImage.fillAmount > 0 && !
InGoodPos(t))
324 private IEnumerator ProgressBar(
float value,
Image progressImage, GameObject progressCanvas)
326 float time = realTime;
329 progressCanvas.transform.GetChild(0).gameObject.SetActive(
true);
331 Color yellow =
new Color(250 / 255f, 250 / 255f, 6 / 255f);
332 Color red =
new Color(250 / 255f, 13 / 255f, 6 / 255f);
333 Color green =
new Color(11 / 255f, 250 / 255f, 6 / 255f);
335 while (inProgress && progressImage.fillAmount < 1)
337 time += Time.deltaTime;
338 if (progressImage.fillAmount <= 0.30f)
340 progressImage.color = yellow;
348 fill = progressImage.fillAmount - 0.3f;
349 if (progressImage.fillAmount >= 0.30f && progressImage.fillAmount <= 0.36f)
351 progressImage.color =
Color.Lerp(yellow, green, fill / 0.06f);
357 fill = progressImage.fillAmount - 0.58f;
358 if (progressImage.fillAmount >= 0.58f && progressImage.fillAmount <= 0.64f)
360 progressImage.color =
Color.Lerp(green, red, fill / 0.06f);
367 if (progressImage.fillAmount <= 0.36f)
369 progressImage.fillAmount = Mathf.Lerp(0, 0.36f, time / ((value) * 0.9f));
372 deltaTime = time - ((value) * 0.9f);
373 if (progressImage.fillAmount >= 0.36f && progressImage.fillAmount <= 0.64f)
375 progressImage.fillAmount = Mathf.Lerp(0.36f, 0.64f, deltaTime / (value * 2 * 0.1f));
377 deltaTime = time - ((value) * 1.1f);
378 if (progressImage.fillAmount >= 0.64f)
380 progressImage.fillAmount = Mathf.Lerp(0.64f, 1, deltaTime / ((value * 2) - (value * 1.1f)));
389 private IEnumerator hideProgressBar()
392 ProgressCanvas.transform.GetChild(0).gameObject.SetActive(
false);
395 private GameObject GetToolObject()
397 GameObject tool =
null;
400 if (serials.Count == 0)
402 var allTrackers = FindObjectsOfType<Valve.VR.SteamVR_TrackedObjectWithEvents>();
404 foreach (var trackedDevice
in allTrackers)
406 if (!
string.IsNullOrEmpty(trackedDevice.SerialID))
408 serials.Add(trackedDevice.gameObject);
409 trackedDevice.name = trackedDevice.SerialID;
410 serialIds.Add(trackedDevice.SerialID);
414 drop.AddOptions(serialIds);
417 if (serials.Count == 0)
421 tool = serials[
drop.value];
422 selectedTracked = tool.GetComponent<Valve.VR.SteamVR_TrackedObjectWithEvents>();
427 internal IEnumerator ShowHighLight()
429 if (highlightObject) Destroy(highlightObject);
433 yield
return new WaitForEndOfFrame();
435 if (done) yield
break;
436 if (serials.Count == 0) yield
break;
441 cachedGO = serials[
drop.value];
443 highlightObject = Instantiate(cachedGO, targetPosition,
Quaternion.Euler(targetRotation), cachedGO.transform.parent);
444 highlightObject.transform.position = targetPosition;
445 highlightObject.transform.eulerAngles = targetRotation;
448 Destroy(highlightObject.GetComponent<Valve.VR.SteamVR_TrackedObjectWithEvents>());
451 Destroy(highlightObject.GetComponent<Interactable>());
454 var allCollider = highlightObject.GetComponentsInChildren<Collider>();
455 var allRb = highlightObject.GetComponentsInChildren<Rigidbody>();
457 foreach (var c
in allCollider)
460 foreach (var c
in allRb)
463 Destroy(highlightObject.GetComponent<
HashHolder>());
465 yield
return new WaitForEndOfFrame();
469 Destroy(highlightObject);
473 highlightObject.transform.SetPositionAndRotation(targetPosition,
Quaternion.Euler(targetRotation));
474 highlightObject.transform.position = targetPosition;
475 highlightObject.transform.eulerAngles = targetRotation;
478 highlightObject.transform.localScale *= 1.001f;
480 highlightObject.transform.SetParent(
null,
true);
481 highlightObject.SetActive(
true);
482 MatChange(highlightObject);
483 if (highlightTool) highlightTool.SetActive(
false);
486 private IEnumerator ShowPreview(Vector3 TargetPosition, GameObject TargetObject)
489 yield
return new WaitForEndOfFrame();
499 private IEnumerator ShowToolHighLight()
501 if (highlightTool) Destroy(highlightTool);
505 yield
return new WaitForEndOfFrame();
510 cachedGO = serials[
drop.value];
512 if (cachedGO ==
null)
Debug.LogError(
"ShowToolHighlight tool is null!");
514 highlightTool = Instantiate(cachedGO, cachedGO.transform.position, cachedGO.transform.rotation,
null);
517 Destroy(highlightTool.GetComponent<Valve.VR.SteamVR_TrackedObjectWithEvents>());
520 Destroy(highlightTool.GetComponent<Interactable>());
522 var allCollider = highlightTool.GetComponentsInChildren<Collider>();
523 var allRb = highlightTool.GetComponentsInChildren<Rigidbody>();
525 foreach (var c
in allCollider)
528 foreach (var c
in allRb)
531 Destroy(highlightTool.GetComponent<
HashHolder>());
533 yield
return new WaitForEndOfFrame();
535 highlightTool.transform.SetPositionAndRotation(cachedGO.transform.position, cachedGO.transform.rotation);
538 highlightTool.transform.localScale *= 1.001f;
540 highlightTool.SetActive(
true);
542 MatChange(highlightTool,
true);
544 private void MatChange(GameObject go,
bool tool =
false)
546 var mrs = go.GetComponentsInChildren<MeshRenderer>();
547 foreach (var meshRenderer
in mrs)
549 var list =
new List<Material>();
550 for (var i = 0; i < meshRenderer.materials.Length; i++)
553 meshRenderer.materials = list.ToArray();
556 internal bool nextActionIsTheSame()
561 foreach (var conn
in connections)
566 foreach (var act
in actions)
570 if (selectedSerial == ta.selectedSerial)
582 private IEnumerator Cleanup()
585 Destroy(highlightTool);
586 Destroy(highlightObject);
588 Destroy(highlightTool);
589 Destroy(highlightObject);
591 Destroy(highlightTool);
592 Destroy(highlightObject);
594 Destroy(highlightTool);
595 Destroy(highlightObject);
598 private new void OnDestroy()
600 Destroy(highlightTool);
601 Destroy(highlightObject);
607 GetToolObject()?.SetActive(
true);
614 Destroy(highlightObject);
617 penaltyTime = Mathf.Abs(holdTime - realTime);
621 ProgressCanvas?.transform.SetParent(
null,
true);
622 TriggerOutput(
outPuts[0].name);
625 Destroy(highlightTool);
626 Destroy(highlightObject);
628 ProgressCanvas.transform.SetParent(
null,
true);
631 private void TriggerOutput()
633 TriggerOutput(
outPuts[0].name);
636 Destroy(highlightTool);
643 ProgressCanvas.GetComponentInChildren<
Image>(
true).fillAmount = 0;
646 StartCoroutine(ShowToolHighLight());
649 StartCoroutine(ShowPreview(GetToolObject().transform.position, GetToolObject()));
650 var tool = GetToolObject();
655 StartCoroutine(LookAtCanvas());
666 var ci = CultureInfo.CurrentCulture;
667 var nfi = ci.NumberFormat;
673 taktTime =
float.Parse(value, ci);
677 Debug.LogWarning(
"not parsable number(?) in holdTime inputfield!");
683 private IEnumerator LookAtCanvas()
687 ProgressCanvas.transform.LookAt(VRCamera.transform);
692 public override void GetObjectData(SerializationInfo info, StreamingContext context)
694 base.GetObjectData(info, context);
696 info.AddValue(
"targetPosition", targetPosition, typeof(Vector3));
697 info.AddValue(
"targetRotation", targetRotation, typeof(Vector3));
699 info.AddValue(
"selectedSerial", selectedSerial);
700 if (holdTime ==
default || holdTime == 0) holdTime = 2.0f;
701 info.AddValue(
"holdTime", holdTime, typeof(
float));
702 info.AddValue(
"taktTime", taktTime, typeof(
float));
705 public RealToolAction(SerializationInfo info, StreamingContext context) : base(info, context)
707 targetPosition = (Vector3)info.GetValue(
"targetPosition", typeof(Vector3));
708 targetRotation = (Vector3)info.GetValue(
"targetRotation", typeof(Vector3));
710 selectedSerial = info.GetString(
"selectedSerial");
711 holdTime = info.GetSingle(
"holdTime");
712 foreach (var e
in info)
714 if (e.Name ==
"taktTime") taktTime = (float)info.GetValue(
"taktTime", typeof(
float));
UnityEngine.UI.Button Button
System.Drawing.Image Image
List< GameObject > outPuts
ActionObject[] GetActionsByIn(string id)
List< string > GetOutputConnectionsToObject(string id)
Connection GetConnectionById(string id)
static Controller Instance
void StartFollowing(Vector3 EndPosition, GameObject TargetObject)
static void SetSelection(IEnumerable< GameObject > selection)
static PenaltyCounter Instance
void hideHandPreview(GameObject objectToGrab)
void EnterOffsetEditor(GameObject objectToGrab, ActionObject action, Vector3 TargetPos, Vector3 TargetRot)
static PreviewHand Instance
static SoundManager instance