Mercurial > hg > Game > Games
comparison Orchestland/Assets/LeapMotion/Widgets/Scripts/Button/ButtonBase.cs @ 1:f7675884f2a1
Add Orchestland project
author | Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 17 Jul 2015 23:09:20 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:347d21cdfc22 | 1:f7675884f2a1 |
---|---|
1 using UnityEngine; | |
2 using System; | |
3 using System.Collections; | |
4 | |
5 namespace LMWidgets | |
6 { | |
7 public abstract class ButtonBase : LeapPhysicsSpring, BinaryInteractionHandler<bool> | |
8 { | |
9 // Binary Interaction Handler - Fires when interaction with the widget starts. | |
10 public event EventHandler<LMWidgets.EventArg<bool>> StartHandler; | |
11 // Binary Interaction Handler - Fires when interaction with the widget ends. | |
12 public event EventHandler<LMWidgets.EventArg<bool>> EndHandler; | |
13 | |
14 public float triggerDistance = 0.025f; | |
15 public float cushionThickness = 0.005f; | |
16 | |
17 protected float scaled_spring_; | |
18 protected float scaled_trigger_distance_; | |
19 protected float scaled_cushion_thickness_; | |
20 | |
21 protected float min_distance_; | |
22 protected float max_distance_; | |
23 | |
24 protected float m_localTriggerDistance; | |
25 protected float m_localCushionThickness; | |
26 protected bool m_isPressed = false; | |
27 | |
28 protected virtual void buttonReleased () | |
29 { | |
30 FireButtonEnd (); | |
31 } | |
32 | |
33 protected virtual void buttonPressed () | |
34 { | |
35 FireButtonStart (); | |
36 } | |
37 | |
38 protected void FireButtonStart (bool value = true) | |
39 { | |
40 EventHandler<LMWidgets.EventArg<bool>> handler = StartHandler; | |
41 if (handler != null) { | |
42 handler (this, new LMWidgets.EventArg<bool> (value)); | |
43 } | |
44 } | |
45 | |
46 protected void FireButtonEnd (bool value = false) | |
47 { | |
48 EventHandler<LMWidgets.EventArg<bool>> handler = EndHandler; | |
49 if (handler != null) { | |
50 handler (this, new LMWidgets.EventArg<bool> (value)); | |
51 } | |
52 } | |
53 | |
54 | |
55 /// <summary> | |
56 /// Returns the fraction of position of the button between rest and trigger. 0.0 = At Rest. 1.0 = At Triggered Distance. | |
57 /// </summary> | |
58 /// <returns>fraction</returns> | |
59 public float GetFraction() | |
60 { | |
61 if (triggerDistance == 0.0f) | |
62 return 0.0f; | |
63 else | |
64 { | |
65 float fraction = transform.localPosition.z / m_localTriggerDistance; | |
66 return Mathf.Clamp(fraction, 0.0f, 1.0f); | |
67 } | |
68 } | |
69 | |
70 /// <summary> | |
71 /// Constrain the button to the z-axis | |
72 /// </summary> | |
73 protected override void ApplyConstraints() | |
74 { | |
75 Vector3 localPosition = transform.localPosition; | |
76 localPosition.x = 0.0f; | |
77 localPosition.y = 0.0f; | |
78 localPosition.z = Mathf.Max(localPosition.z, 0.0f); | |
79 transform.localPosition = localPosition; | |
80 } | |
81 | |
82 /// <summary> | |
83 /// Check if the button is being pressed or not | |
84 /// </summary> | |
85 private void CheckTrigger() | |
86 { | |
87 float scale = transform.lossyScale.z; | |
88 m_localTriggerDistance = triggerDistance / scale; | |
89 m_localCushionThickness = Mathf.Clamp(cushionThickness / scale, 0.0f, m_localTriggerDistance - 0.001f); | |
90 if (m_isPressed == false) | |
91 { | |
92 if (transform.localPosition.z > m_localTriggerDistance) | |
93 { | |
94 m_isPressed = true; | |
95 buttonPressed(); | |
96 } | |
97 } | |
98 else if (m_isPressed == true) | |
99 { | |
100 if (transform.localPosition.z < (m_localTriggerDistance - m_localCushionThickness)) | |
101 { | |
102 m_isPressed = false; | |
103 buttonReleased(); | |
104 } | |
105 } | |
106 } | |
107 | |
108 protected virtual void Start() | |
109 { | |
110 cushionThickness = Mathf.Clamp(cushionThickness, 0.0f, triggerDistance - 0.001f); | |
111 } | |
112 | |
113 protected virtual void Update() | |
114 { | |
115 CheckTrigger(); | |
116 } | |
117 } | |
118 } |