Tanoda
LeapRTS.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;
10
11namespace Leap.Unity {
12
17 public class LeapRTS : MonoBehaviour {
18
19 public enum RotationMethod {
20 None,
21 Single,
22 Full
23 }
24
25 [SerializeField]
26 private PinchDetector _pinchDetectorA;
28 get {
29 return _pinchDetectorA;
30 }
31 set {
32 _pinchDetectorA = value;
33 }
34 }
35
36 [SerializeField]
37 private PinchDetector _pinchDetectorB;
39 get {
40 return _pinchDetectorB;
41 }
42 set {
43 _pinchDetectorB = value;
44 }
45 }
46
47 [SerializeField]
48 private RotationMethod _oneHandedRotationMethod;
49
50 [SerializeField]
51 private RotationMethod _twoHandedRotationMethod;
52
53 [SerializeField]
54 private bool _allowScale = true;
55
56 [Header("GUI Options")]
57 [SerializeField]
58 private KeyCode _toggleGuiState = KeyCode.None;
59
60 [SerializeField]
61 private bool _showGUI = true;
62
63 private Transform _anchor;
64
65 private float _defaultNearClip;
66
67 void Start() {
68// if (_pinchDetectorA == null || _pinchDetectorB == null) {
69// Debug.LogWarning("Both Pinch Detectors of the LeapRTS component must be assigned. This component has been disabled.");
70// enabled = false;
71// }
72
73 GameObject pinchControl = new GameObject("RTS Anchor");
74 _anchor = pinchControl.transform;
75 _anchor.transform.parent = transform.parent;
76 transform.parent = _anchor;
77 }
78
79 void Update() {
80 if (Input.GetKeyDown(_toggleGuiState)) {
81 _showGUI = !_showGUI;
82 }
83
84 bool didUpdate = false;
85 if(_pinchDetectorA != null)
86 didUpdate |= _pinchDetectorA.DidChangeFromLastFrame;
87 if(_pinchDetectorB != null)
88 didUpdate |= _pinchDetectorB.DidChangeFromLastFrame;
89
90 if (didUpdate) {
91 transform.SetParent(null, true);
92 }
93
94 if (_pinchDetectorA != null && _pinchDetectorA.IsActive &&
95 _pinchDetectorB != null &&_pinchDetectorB.IsActive) {
96 transformDoubleAnchor();
97 } else if (_pinchDetectorA != null && _pinchDetectorA.IsActive) {
98 transformSingleAnchor(_pinchDetectorA);
99 } else if (_pinchDetectorB != null && _pinchDetectorB.IsActive) {
100 transformSingleAnchor(_pinchDetectorB);
101 }
102
103 if (didUpdate) {
104 transform.SetParent(_anchor, true);
105 }
106 }
107
108 void OnGUI() {
109 if (_showGUI) {
110 GUILayout.Label("One Handed Settings");
111 doRotationMethodGUI(ref _oneHandedRotationMethod);
112 GUILayout.Label("Two Handed Settings");
113 doRotationMethodGUI(ref _twoHandedRotationMethod);
114 _allowScale = GUILayout.Toggle(_allowScale, "Allow Two Handed Scale");
115 }
116 }
117
118 private void doRotationMethodGUI(ref RotationMethod rotationMethod) {
119 GUILayout.BeginHorizontal();
120
121 GUI.color = rotationMethod == RotationMethod.None ? Color.green : Color.white;
122 if (GUILayout.Button("No Rotation")) {
123 rotationMethod = RotationMethod.None;
124 }
125
126 GUI.color = rotationMethod == RotationMethod.Single ? Color.green : Color.white;
127 if (GUILayout.Button("Single Axis")) {
128 rotationMethod = RotationMethod.Single;
129 }
130
131 GUI.color = rotationMethod == RotationMethod.Full ? Color.green : Color.white;
132 if (GUILayout.Button("Full Rotation")) {
133 rotationMethod = RotationMethod.Full;
134 }
135
136 GUI.color = Color.white;
137
138 GUILayout.EndHorizontal();
139 }
140
141 private void transformDoubleAnchor() {
142 _anchor.position = (_pinchDetectorA.Position + _pinchDetectorB.Position) / 2.0f;
143
144 switch (_twoHandedRotationMethod) {
145 case RotationMethod.None:
146 break;
147 case RotationMethod.Single:
148 Vector3 p = _pinchDetectorA.Position;
149 p.y = _anchor.position.y;
150 _anchor.LookAt(p);
151 break;
152 case RotationMethod.Full:
153 Quaternion pp = Quaternion.Lerp(_pinchDetectorA.Rotation, _pinchDetectorB.Rotation, 0.5f);
154 Vector3 u = pp * Vector3.up;
155 _anchor.LookAt(_pinchDetectorA.Position, u);
156 break;
157 }
158
159 if (_allowScale) {
160 _anchor.localScale = Vector3.one * Vector3.Distance(_pinchDetectorA.Position, _pinchDetectorB.Position);
161 }
162 }
163
164 private void transformSingleAnchor(PinchDetector singlePinch) {
165 _anchor.position = singlePinch.Position;
166
167 switch (_oneHandedRotationMethod) {
168 case RotationMethod.None:
169 break;
170 case RotationMethod.Single:
171 Vector3 p = singlePinch.Rotation * Vector3.right;
172 p.y = _anchor.position.y;
173 _anchor.LookAt(p);
174 break;
175 case RotationMethod.Full:
176 _anchor.rotation = singlePinch.Rotation;
177 break;
178 }
179
180 _anchor.localScale = Vector3.one;
181 }
182 }
183}
UnityEngine.Color Color
Definition: TestScript.cs:32
Vector3 Position
Returns the position value of the detected pinch or grab. If a pinch or grab is not currently being d...
virtual bool DidChangeFromLastFrame
Returns whether or not the value of IsPinching is different than the value reported during the previo...
Quaternion Rotation
Returns the rotation value of the detected pinch or grab. If a pinch or grab is not currently being d...
Use this component on a Game Object to allow it to be manipulated by a pinch gesture....
Definition: LeapRTS.cs:17
PinchDetector PinchDetectorA
Definition: LeapRTS.cs:27
PinchDetector PinchDetectorB
Definition: LeapRTS.cs:38
A basic utility class to aid in creating pinch based actions. Once linked with a HandModelBase,...