/// <summary> /// Asynchronous method taking in button input and sending it to the current selected tool /// </summary> /// <returns></returns> private IEnumerator Button() { int maxButtons = trackerButtonList.getMaxButtons(); while (true) { Vector3 origin = wandObject.transform.position; Vector3 direction = wandObject.transform.forward; //i tracks the number of the current button for (int i = 0; i < maxButtons; ++i) { //Current value of the button bool curValue = VRPN.vrpnButton(trackerAddress, i); TrackerButton currentButton = trackerButtonList.MapButton(i); //If the previous state is true and the current value is false it is a button click if (buttonState.ContainsKey(currentButton) && buttonState[currentButton] && !curValue) { //Fire the event toolManager.handleButtonClick(currentButton, origin, direction); //hasStarted = false; hit = new RaycastHit(); //temp; } //If the current and previous are true then it is a buttondrag event else if (buttonState.ContainsKey(currentButton) && buttonState[currentButton] && curValue && (currentButton == TrackerButton.Trigger)) { if (hit.distance > 0) { //Fire the event toolManager.handleButtonDrag(currentButton, hit, offset, origin, direction); } } //If the previous is false and the current is true else if (!(buttonState.ContainsKey(currentButton) && buttonState[currentButton]) && curValue && (currentButton == TrackerButton.Trigger)) { Physics.Raycast(origin, direction * rayLength, out hit); if (hit.distance > 0) { offset = hit.transform.position - hit.point; } toolManager.handleButtonDown(currentButton, hit, origin, direction); } //update the previous value buttonState[currentButton] = curValue; } yield return(null); } }
/// <summary> /// Gets the latest pushed button on tracker from vrpn. Stops after a preset amount of time (2 seconds currently). /// </summary> /// <returns>The number of the pushed button (0...n-1) or -1 if no button is pushed on tracker.</returns> public int GetPushedButton() { lastButtonPressed = -1; for (int ii = 0; ii < MAX_LOOPS_BUTTON_CHECK; ii++) { for (int jj = 0; jj < maxButtons; jj++) { bool btnValue = VRPN.vrpnButton(trackerAddress, jj, ii); if (btnValue) { lastButtonPressed = jj; } } if (lastButtonPressed > -1) { return(lastButtonPressed); } Thread.Sleep(SLEEP_TIMEOUT); } return(-1); }