Tanoda
PhysicsUI.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using UnityEngine;
12using UnityEngine.UI;
13using System;
14using System.Collections.Generic;
15
16namespace Leap.Unity.InputModule {
20 public class PhysicsUI : MonoBehaviour {
21 [Tooltip("The physically-enabled body of the button")]
23 public Transform ButtonFace;
24 [Tooltip("OPTIONAL: If you have a dropshadow image that you would like to opacity fade upon compression, add one here")]
26 public UnityEngine.UI.Image Shadow;
27
28 private float MaxShadowOpacity;
29 private Rigidbody body;
30 private SpringJoint SpringJoint;
31 private Vector3 InitialLocalPosition;
32 private Vector3 PhysicsPosition = Vector3.zero;
33 private Vector3 PhysicsVelocity = Vector3.zero;
34 private bool physicsOccurred = false;
35 private bool isDepressed = false;
36 private bool prevDepressed = false;
37 private PointerEventData pointerEvent;
38
39 //Reset the Positions of the UI Elements on both Start and Quit
40 void Start() {
41 if (ButtonFace != null) {
42 if (Shadow != null) {
43 MaxShadowOpacity = Shadow.color.a;
44 Shadow.color = new Color(Shadow.color.r, Shadow.color.g, Shadow.color.b, 0f);
45 }
46
47 body = ButtonFace.GetComponent<Rigidbody>();
48 SpringJoint = ButtonFace.GetComponent<SpringJoint>();
49 InitialLocalPosition = ButtonFace.localPosition;
50
51 pointerEvent = new PointerEventData(EventSystem.current);
52 pointerEvent.button = PointerEventData.InputButton.Left;
53 RaycastResult result = new RaycastResult();
54 result.gameObject = gameObject;
55 pointerEvent.pointerCurrentRaycast = result;
56 pointerEvent.pointerPress = gameObject;
57 pointerEvent.rawPointerPress = gameObject;
58 ButtonFace.localPosition = new Vector3(InitialLocalPosition.x, InitialLocalPosition.y, SpringJoint.connectedAnchor.z);
59 PhysicsPosition = transform.TransformPoint(new Vector3(InitialLocalPosition.x, InitialLocalPosition.y, SpringJoint.connectedAnchor.z));
60 body.position = PhysicsPosition;
61 } else {
62 Debug.LogWarning("Ensure that you have a UI Element allotted in the Layer Transform!");
63 }
64 }
65
66 void FixedUpdate() {
67 if (!physicsOccurred) {
68 physicsOccurred = true;
69 body.position = PhysicsPosition;
70 body.velocity = PhysicsVelocity;
71 }
72 }
73
74 void Update() {
75 pointerEvent.position = Camera.main.WorldToScreenPoint(ButtonFace.transform.position);
76 if (physicsOccurred) {
77 physicsOccurred = false;
78
79 Vector3 localPhysicsPosition = transform.InverseTransformPoint(body.position);
80 localPhysicsPosition = new Vector3(InitialLocalPosition.x, InitialLocalPosition.y, localPhysicsPosition.z);
81 Vector3 newWorldPhysicsPosition = transform.TransformPoint(localPhysicsPosition);
82 PhysicsPosition = newWorldPhysicsPosition;
83
84 Vector3 localPhysicsVelocity = transform.InverseTransformDirection(body.velocity);
85 localPhysicsVelocity = new Vector3(0f, 0f, localPhysicsVelocity.z);
86 Vector3 newWorldPhysicsVelocity = transform.TransformDirection(localPhysicsVelocity);
87 PhysicsVelocity = newWorldPhysicsVelocity;
88
89 if (localPhysicsPosition.z > 0) {
90 ButtonFace.localPosition = new Vector3(InitialLocalPosition.x, InitialLocalPosition.y, 0f);
91 PhysicsVelocity = PhysicsVelocity / 2f;
92 isDepressed = true;
93 } else if (localPhysicsPosition.z < SpringJoint.connectedAnchor.z * 2f) {
94 ButtonFace.localPosition = new Vector3(InitialLocalPosition.x, InitialLocalPosition.y, SpringJoint.connectedAnchor.z * 2f);
95 PhysicsPosition = ButtonFace.position;
96 isDepressed = false;
97 } else {
98 ButtonFace.localPosition = localPhysicsPosition;
99 isDepressed = false;
100 }
101
102 if (SpringJoint && Shadow != null) {
103 float LayerHeight = Mathf.Abs(ButtonFace.localPosition.z);
104 float RestingHeight = Mathf.Abs(SpringJoint.connectedAnchor.z);
105 Shadow.color = new Color(Shadow.color.r, Shadow.color.g, Shadow.color.b, Mathf.Lerp(0f, MaxShadowOpacity, 1 - (Mathf.Abs(LayerHeight - RestingHeight) / RestingHeight)));
106 }
107 }
108
109 if (isDepressed && !prevDepressed) {
110 prevDepressed = true;
111 ExecuteEvents.Execute(gameObject, pointerEvent, ExecuteEvents.pointerEnterHandler);
112 ExecuteEvents.Execute(gameObject, pointerEvent, ExecuteEvents.pointerDownHandler);
113 } else if (!isDepressed && prevDepressed) {
114 prevDepressed = false;
115 ExecuteEvents.Execute(gameObject, pointerEvent, ExecuteEvents.pointerExitHandler);
116 ExecuteEvents.Execute(gameObject, pointerEvent, ExecuteEvents.pointerClickHandler);
117 ExecuteEvents.Execute(gameObject, pointerEvent, ExecuteEvents.pointerUpHandler);
118 }
119 }
120 }
121}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
UnityEngine.Color Color
Definition: TestScript.cs:32
UnityEngine.UI.Image Shadow
Definition: PhysicsUI.cs:26