Tanoda
ViveController.cs
Go to the documentation of this file.
1using System.Collections;
2using System.Collections.Generic;
3using System.Linq;
4using UnityEngine;
5using UnityEngine.UI;
6#if !UNITY_WEBGL
7using Valve.VR.InteractionSystem;
8using ViveHandTracking;
9using Valve.VR;
10using static Valve.VR.InteractionSystem.Hand;
11#endif
12using GILES;
13
14public class ViveController : MonoBehaviour
15{
16 private bool isGripping = false;
17 private bool isGameObject = false;
18 public GameObject StageManagerCanvas;
19 public GameObject StageManagerWindow;
20 public GameObject Player, HandMenu, ModelBrowser;
24 GameObject SelectedGameObject;
25 GameObject ConnectedAction;
26 GameObject touchedObject;
27#if !UNITY_WEBGL
28 public SteamVR_Action_Boolean gripButton;
29 public SteamVR_Action_Boolean grabGripAction = SteamVR_Input.GetAction<SteamVR_Action_Boolean>("GrabGrip");
30
31#endif
34 private VivePointer chosedController;
35 public GameObject CanvasSelector;
37 bool isGrabbedRight = false;
38 bool isGrabbedLeft = false;
39 GameObject attachedGameObject;
40 bool isSelected = false;
41 Vector3 initialHandPosition1; // first hand position
42 Vector3 initialHandPosition2; // second hand position
43 Quaternion initialObjectRotation; // grabbed object rotation
44 Vector3 initialObjectScale; // grabbed object scale
45 Vector3 initialObjectDirection; // direction of object to midpoint of both hands
46 #if !UNITY_WEBGL
47 AttachmentFlags initialAttachmentFlags; // initial AttachmentFlags
48#endif
49 public bool twoHandGrab = false; // bool, so you know when grabbed with 2 hands
50 private object objectToAttach;
51
52
53 // Start is called before the first frame update
54 void Start()
55 {
56 if (SavedUser.instance.isEditor)
57 {
58 LeftHandModelMask.SetActive(false);
59 RightHandModelMask.SetActive(false);
60 }
61 }
62
63 // Update is called once per frame
64 public void OnCollisionEnter(Collision collision)
65 {
66 if (collision.gameObject)
67 {
68 touchedObject = collision.gameObject;
69 }
70 }
71 void Update()
72 {
73 //if (SavedUser.instance.isEditor)
74 //{
75
76#if !UNITY_WEBGL
77 if (Player.GetComponent<Player>().hands[0].currentAttachedObject)
78 {
79 attachedGameObject = Player.GetComponent<Player>().hands[0].currentAttachedObject;
80 }
81 if (Player.GetComponent<Player>().hands[1].currentAttachedObject)
82 {
83 attachedGameObject = Player.GetComponent<Player>().hands[1].currentAttachedObject;
84 }
85
86 //if (GestureProvider.Current && GestureProvider.Current.engine &&
87 //GestureProvider.Current.engine.State.LeftHand != null && !isGripping)
88 // if (GestureProvider.Current.engine.State.LeftHand.gesture == GestureType.Five)
89 // {
90 // isGripping = true;
91 // StageManagerCanvas.SetActive(true);
92 // }
93 try
94 {
95
96 if (SteamVR_Actions._default.Teleport.GetStateUp(SteamVR_Input_Sources.Any))
97 {
98 if (attachedGameObject)
99 {
100 GameObjectAction = Content.GetComponentsInChildren<GameObjectAction>();
101 foreach (var a in GameObjectAction)
102 {
103 Debug.Log(a.selectedHash);
104 if (attachedGameObject.gameObject == a.GetGameObject() && !isSelected)
105 {
106 SelectedGameObject = a.GetGameObject();
107 if (a.GetOutputAction())
108 {
109 ConnectedAction = a.GetOutputAction();
110 }
111 break;
112 }
113 }
114 }
115 if (!attachedGameObject)
116 {
117 isSelected = false;
118 }
119 if (SteamVR_Actions._default.Teleport.GetStateUp(SteamVR_Input_Sources.Any) && ConnectedAction)
120 {
121 ConnectedAction.GetComponent<PositionAction>().FillCurrentTransform();
122 }
123 }
124
125 if (SteamVR_Actions._default.GrabPinch.GetStateDown(SteamVR_Input_Sources.Any) && LineLeft.activeInHierarchy || SteamVR_Actions._default.GrabPinch.GetStateDown(SteamVR_Input_Sources.Any) && LineRight.activeInHierarchy)
126 {
127 if (SteamVR_Actions._default.GrabPinch.GetStateDown(SteamVR_Input_Sources.LeftHand))
128 {
129 chosedController = vivePointerLeft;
130 }
131 if (SteamVR_Actions._default.GrabPinch.GetStateDown(SteamVR_Input_Sources.RightHand))
132 {
133 chosedController = vivePointer;
134 }
135
136 if (chosedController.hitVC.collider.gameObject.GetComponent<ScreneCapture>())
137 {
139 }
140
141 }
142 Vector2 touch = SteamVR_Actions._default.Touch.GetAxis(SteamVR_Input_Sources.Any);
143 float scroll = touch.y;
144 if (SteamVR_Actions._default.Scroll.GetChanged(SteamVR_Input_Sources.Any))
145 {
146 if (SteamVR_Actions._default.Scroll.GetChanged(SteamVR_Input_Sources.LeftHand))
147 {
148 chosedController = vivePointerLeft;
149 }
150 if (SteamVR_Actions._default.Scroll.GetChanged(SteamVR_Input_Sources.RightHand))
151 {
152 chosedController = vivePointer;
153 }
154 if (chosedController.hitVC.collider.gameObject.GetComponent<ScreneCapture>())
155 {
156 MouseEvent.Scroll((uint)(scroll * 100));
157 }
158
159 }
160 if (SteamVR_Actions._default.GrabPinch.GetStateUp(SteamVR_Input_Sources.Any) && LineLeft.activeInHierarchy || SteamVR_Actions._default.GrabPinch.GetStateUp(SteamVR_Input_Sources.Any) && LineRight.activeInHierarchy)
161 {
162 if (chosedController.hitVC.collider.gameObject.GetComponent<ScreneCapture>())
163 {
165 }
166 else
167 {
168 if (chosedController.hitVC.collider.gameObject.GetComponent<VivePointerTarget>())
169 {
170 chosedController.hitVC.collider.gameObject.GetComponent<VivePointerTarget>().OnPointerClick();
171 }
172 else
173 {
174 chosedController.hitVC.collider.gameObject.GetComponent<VivePointerTarget>().OnPointerClick();
175 }
176 }
177
178 }
179 if (SteamVR_Actions._default.SnapTurnDown.GetStateDown(SteamVR_Input_Sources.Any))
180 {
181 if (SteamVR_Actions._default.SnapTurnDown.GetStateDown(SteamVR_Input_Sources.LeftHand))
182 {
183 chosedController = vivePointerLeft;
184 }
185 if (SteamVR_Actions._default.SnapTurnDown.GetStateDown(SteamVR_Input_Sources.RightHand))
186 {
187 chosedController = vivePointer;
188 }
189 if (chosedController.GetComponentInChildren<ScrollRect>())
190 {
191 chosedController.GetComponentInChildren<ScrollRect>().horizontalScrollbar.value += 0.05f;
192 }
193 }
194 if (SteamVR_Actions._default.SnapTurnUp.GetStateDown(SteamVR_Input_Sources.Any))
195 {
196 if (SteamVR_Actions._default.SnapTurnUp.GetStateDown(SteamVR_Input_Sources.LeftHand))
197 {
198 chosedController = vivePointerLeft;
199 }
200 if (SteamVR_Actions._default.SnapTurnUp.GetStateDown(SteamVR_Input_Sources.RightHand))
201 {
202 chosedController = vivePointer;
203 }
204 if (chosedController.GetComponentInChildren<ScrollRect>())
205 {
206 chosedController.GetComponentInChildren<ScrollRect>().horizontalScrollbar.value -= 0.05f;
207 }
208 }
209 if (SteamVR_Actions._default.Menu.GetStateUp(SteamVR_Input_Sources.RightHand))
210 {
211 if (!HandMenu.activeInHierarchy)
212 {
213 HandMenu.SetActive(true);
214 return;
215 }
216 if (HandMenu.activeInHierarchy)
217 {
218 HandMenu.SetActive(false);
219 return;
220 }
221 }
222 if (SteamVR_Actions._default.Menu.GetStateUp(SteamVR_Input_Sources.LeftHand))
223 {
224 if (!ModelBrowser.activeInHierarchy)
225 {
226 ModelBrowser.SetActive(true);
227 return;
228 }
229 if (ModelBrowser.activeInHierarchy)
230 {
231 ModelBrowser.SetActive(false);
232 return;
233 }
234 }
235
236 if (Player.GetComponent<Player>().hands[1].currentAttachedObject && !isGrabbedRight)
237 {
238 initialHandPosition1 = Player.GetComponent<Player>().hands[1].transform.position;
239 initialObjectScale = attachedGameObject.transform.localScale;
240 isGrabbedRight = true;
241
242 }
243
244 if (Player.GetComponent<Player>().hands[0].currentAttachedObject && !isGrabbedLeft)
245 {
246 initialHandPosition2 = Player.GetComponent<Player>().hands[0].transform.position;
247 isGrabbedLeft = true;
248 }
249 if (!Player.GetComponent<Player>().hands[1].currentAttachedObject)
250 {
251 isGrabbedRight = false;
252 }
253
254 if (!Player.GetComponent<Player>().hands[0].currentAttachedObject)
255 {
256 isGrabbedLeft = false;
257 }
258 if (CanvasSelector.transform.GetChild(0).GetComponent<Button>() && SteamVR_Actions._default.GrabGrip.GetStateDown(SteamVR_Input_Sources.Any))
259 {
260 pb_Selection.SetSelection(attachedGameObject);
261 CanvasSelector.transform.GetChild(0).GetComponent<Button>().onClick.Invoke();
262 }
263
264 if (Player.GetComponent<Player>().hands[1].currentAttachedObject && Player.GetComponent<Player>().hands[0].currentAttachedObject)
265 {
266 int xDistance = (int)Player.GetComponent<Player>().hands[1].transform.localPosition.x - (int)Player.GetComponent<Player>().hands[0].transform.localPosition.x;
267
268 float initialGrabDistance = Vector3.Distance(initialHandPosition1, initialHandPosition2);
269 Vector3 rightHandPosition = Player.GetComponent<Player>().hands[1].transform.localPosition;
270 Vector3 leftHandPosition = Player.GetComponent<Player>().hands[0].transform.localPosition;
271 float currentGrabDistance = Vector3.Distance(rightHandPosition, leftHandPosition);
272 float p = (currentGrabDistance / initialGrabDistance);
273 Vector3 newScale = new Vector3(p * initialObjectScale.x, p * initialObjectScale.y, p * initialObjectScale.z);
274 attachedGameObject.transform.localScale = newScale;
275 }
276 }
277 catch (System.Exception)
278 {
279 //No VR headset present 90%
280 Destroy(this);
281 }
282#endif
283
284 }
285
286 //}
287
288}
UnityEngine.UI.Button Button
Definition: Pointer.cs:7
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
static void SetSelection(IEnumerable< GameObject > selection)
Definition: pb_Selection.cs:92
GameObject GetGameObject()
static void Scroll(uint value)
Definition: MouseEvent.cs:102
static void Mouseup()
Definition: MouseEvent.cs:98
static void MouseDown()
Definition: MouseEvent.cs:94
GameObject ModelBrowser
GameObject LeftHandModelMask
GameObject CanvasSelector
void OnCollisionEnter(Collision collision)
GameObject Content
GameObject ScrollContent
GameObject Player
GameObject StageManagerWindow
SteamVR_Action_Boolean grabGripAction
SteamVR_Action_Boolean gripButton
Controller Controller
GameObject HandMenu
GameObject LineRight
GameObject LeftController
VivePointer vivePointerLeft
pb_StageManagerButton StgBtn
GameObject RightHandModelMask
GameObject LineLeft
GameObject RightController
VivePointer vivePointer
GameObject StageManagerCanvas