private void ProcessLostTouches() { // handle lost touches due to Unity bugs, Unity can not send touch end states // and it appears that even the id's of touches can change in WebGL foreach (GestureTouch t in previousTouches) { if (!currentTouches.Contains(t)) { tempTouches.Add(t); } } foreach (DigitalRubyShared.GestureRecognizer g in gestures) { bool reset = false; foreach (GestureTouch t in g.CurrentTrackedTouches) { if (!currentTouches.Contains(t)) { tempTouches.Add(t); reset = true; } } if (reset) { g.Reset(); } } foreach (GestureTouch t in tempTouches) { // only end touch here, as end touch removes from previousTouches list GestureTouch tmp = t; FingersEndTouch(ref tmp, true); previousTouches.Remove(tmp); } tempTouches.Clear(); }
private void AddMouseTouch(int index, int pointerId, float x, float y) { // add a touch from the mouse float prevX = mousePrev[index].Key; float prevY = mousePrev[index].Value; prevX = (prevX == float.MinValue ? x : prevX); prevY = (prevY == float.MinValue ? y : prevY); GestureTouch g = new GestureTouch(pointerId, x, y, prevX, prevY, 0.0f, x, y); if (Input.GetMouseButton(index)) { mousePrev[index] = new KeyValuePair <float, float>(x, y); if (isMouseDown[index]) { touchesMoved.Add(g); //Debug.LogFormat("Mouse Move: {0},{1}, Idx: {2}", x, y, index); } else { touchesBegan.Add(g); isMouseDown[index] = true; //Debug.LogFormat("Mouse Down: {0},{1}, Idx: {2}", x, y, index); } } else if (isMouseDown[index]) { mousePrev[index] = new KeyValuePair <float, float>(float.MinValue, float.MinValue); touchesEnded.Add(g); isMouseDown[index] = false; //Debug.LogFormat("Mouse Up: {0},{1}, Idx: {2}", x, y, index); } }
/// <summary> /// Reset state - all touches and tracking are cleared /// </summary> /// <param name="clearGestures">True to clear out all gestures, false otherwise</param> public void ResetState(bool clearGestures) { for (int i = gestures.Count - 1; i >= 0; i--) { if (gestures[i] == null) { gestures.RemoveAt(i); } else { gestures[i].Reset(); } } if (clearGestures) { gestures.Clear(); } availableShowTouches.Clear(); availableShowTouches.AddRange(TouchCircles); foreach (GameObject obj in TouchCircles) { if (obj != null) { obj.SetActive(false); } } foreach (GameObject obj in origTouchCircles) { if (obj != null) { obj.SetActive(false); } } shownTouches.Clear(); currentTouches.Clear(); previousTouches.Clear(); touchesBegan.Clear(); touchesMoved.Clear(); touchesEnded.Clear(); gameObjectsForTouch.Clear(); captureRaycastResults.Clear(); filteredTouches.Clear(); touches.Clear(); previousTouchPositions.Clear(); rotateAngle = 0.0f; pinchScale = 1.0f; rotatePinch1 = new GestureTouch(); rotatePinch2 = new GestureTouch(); lastMouseWheelTime = System.DateTime.MinValue; // cleanup deleted pass through objects for (int i = PassThroughObjects.Count - 1; i >= 0; i--) { if (PassThroughObjects[i] == null) { PassThroughObjects.RemoveAt(i); } } if (VirtualTouchResetHandler != null) { VirtualTouchResetHandler(); } }
private void ProcessMouseWheel() { // if the mouse is not setup or the user doesn't want the mouse treated as touches, return right away if (!Input.mousePresent || !TreatMousePointerAsFinger) { return; } // the mouse wheel will act as a rotate and pinch / zoom Vector2 delta = Input.mouseScrollDelta; float scrollDelta = (delta.y == 0.0f ? delta.x : delta.y) * MouseWheelDeltaMultiplier; float threshold = DeviceInfo.UnitsToPixels(MouseDistanceInUnitsForScaleAndRotate * 0.5f); // add type 1 = moved, 2 = begin, 3 = ended, 4 = none int addType1 = 4; int addType2 = 4; // left or right control initial down means begin if (!RequireControlKeyForMouseZoom) { if (delta == Vector2.zero) { if (lastMouseWheelTime != System.DateTime.MinValue) { if ((System.DateTime.UtcNow - lastMouseWheelTime).TotalSeconds < 1.0f) { // continue zooming pinchScale = Mathf.Max(0.35f, pinchScale + scrollDelta); addType1 = 1; } else { // stop zooming lastMouseWheelTime = System.DateTime.MinValue; addType1 = 3; } } } else if (lastMouseWheelTime == System.DateTime.MinValue) { // start zooming addType1 = 2; lastMouseWheelTime = System.DateTime.UtcNow; } else { // continue zooming pinchScale = Mathf.Max(0.35f, pinchScale + scrollDelta); addType1 = 1; lastMouseWheelTime = System.DateTime.UtcNow; } } else if (Input.GetKeyDown(KeyCode.LeftControl) || Input.GetKeyDown(KeyCode.RightControl)) { // initial start of scale addType1 = 2; } // left or right control still down means move else if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) { pinchScale = Mathf.Max(0.35f, pinchScale + scrollDelta); addType1 = 1; } // left or right control initial up means end else if (Input.GetKeyUp(KeyCode.LeftControl) || Input.GetKeyUp(KeyCode.RightControl)) { addType1 = 3; } // left or right shift initial down means begin if (Input.GetKeyDown(KeyCode.LeftShift) || Input.GetKeyDown(KeyCode.RightShift)) { addType2 = 2; } // left or right shift still down means move else if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) { rotateAngle += scrollDelta; addType2 = 1; } // left or right shift initial up means end else if (Input.GetKeyUp(KeyCode.LeftShift) || Input.GetKeyUp(KeyCode.RightShift)) { addType2 = 3; } // use the minimum add type so that moves are preferred over begins and begins are preferred over ends int addType = Mathf.Min(addType1, addType2); // no begins, moves or ends, set defaults and end if (addType == 4) { pinchScale = 1.0f; rotateAngle = 0.0f; return; } // calculate rotation float x = Input.mousePosition.x; float y = Input.mousePosition.y; float xRot1 = x - threshold; float yRot1 = y; float xRot2 = x + threshold; float yRot2 = y; float distance = threshold * pinchScale; xRot1 = x - distance; yRot1 = y; xRot2 = x + distance; yRot2 = y; RotateAroundPoint(ref xRot1, ref yRot1, x, y, rotateAngle); RotateAroundPoint(ref xRot2, ref yRot2, x, y, rotateAngle); #if DEBUG if (scrollDelta != 0.0f) { //Debug.LogFormat("Mouse delta: {0}", scrollDelta); } #endif // calculate rotation and zoom based on mouse values if (addType == 1) { // moved rotatePinch1 = new GestureTouch(int.MaxValue - 5, xRot1, yRot1, rotatePinch1.X, rotatePinch1.Y, 0.0f, null, TouchPhase.Moved); rotatePinch2 = new GestureTouch(int.MaxValue - 6, xRot2, yRot2, rotatePinch2.X, rotatePinch2.Y, 0.0f, null, TouchPhase.Moved); FingersProcessTouch(ref rotatePinch1); FingersProcessTouch(ref rotatePinch2); } else if (addType == 2) { // begin rotatePinch1 = new GestureTouch(int.MaxValue - 5, xRot1, yRot1, xRot1, yRot1, 0.0f, null, TouchPhase.Began); rotatePinch2 = new GestureTouch(int.MaxValue - 6, xRot2, yRot2, xRot2, yRot2, 0.0f, null, TouchPhase.Began); FingersProcessTouch(ref rotatePinch1); FingersProcessTouch(ref rotatePinch2); } else if (addType == 3) { // end rotatePinch1 = new GestureTouch(int.MaxValue - 5, xRot1, yRot1, xRot1, yRot1, 0.0f, null, TouchPhase.Ended); rotatePinch2 = new GestureTouch(int.MaxValue - 6, xRot2, yRot2, xRot2, yRot2, 0.0f, null, TouchPhase.Ended); FingersProcessTouch(ref rotatePinch1); FingersProcessTouch(ref rotatePinch2); } }
private void FingersContinueTouch(ref GestureTouch g) { touchesMoved.Add(g); previousTouchPositions[g.Id] = new Vector2(g.X, g.Y); }
private void ProcessMouseWheel() { // if the mouse is not setup or the user doesn't want the mouse treated as touches, return right away if (!Input.mousePresent || !TreatMousePointerAsFinger) { return; } // the mouse wheel will act as a rotate and pinch / zoom const float threshold = 100.0f; const float deltaModifier = 0.025f; Vector2 delta = Input.mouseScrollDelta; float scrollDelta = (delta.y == 0.0f ? delta.x : delta.y) * deltaModifier; // add type 1 = moved, 2 = begin, 3 = ended, 4 = none int addType1 = 4; int addType2 = 4; // left or right control initial down means begin if (Input.GetKeyDown(KeyCode.LeftControl) || Input.GetKeyDown(KeyCode.RightControl)) { addType1 = 2; } // left or right control still down means move else if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) { pinchScale += scrollDelta; addType1 = 1; } // left or right control initial up means end else if (Input.GetKeyUp(KeyCode.LeftControl) || Input.GetKeyUp(KeyCode.RightControl)) { addType1 = 3; } // left or right shift initial down means begin if (Input.GetKeyDown(KeyCode.LeftShift) || Input.GetKeyDown(KeyCode.RightShift)) { addType2 = 2; } // left or right shift still down means move else if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) { rotateAngle += scrollDelta; addType2 = 1; } // left or right shift initial up means end else if (Input.GetKeyUp(KeyCode.LeftShift) || Input.GetKeyUp(KeyCode.RightShift)) { addType2 = 3; } // use the minimum add type so that moves are preferred over begins and begins are preferred over ends int addType = Mathf.Min(addType1, addType2); // no begins, moves or ends, set defaults and end if (addType == 4) { pinchScale = 1.0f; rotateAngle = 0.0f; return; } // calculate rotation float x = Input.mousePosition.x; float y = Input.mousePosition.y; float xRot1 = x - threshold; float yRot1 = y; float xRot2 = x + threshold; float yRot2 = y; float distance = threshold * pinchScale; xRot1 = x - distance; yRot1 = y; xRot2 = x + distance; yRot2 = y; RotateAroundPoint(ref xRot1, ref yRot1, x, y, rotateAngle); RotateAroundPoint(ref xRot2, ref yRot2, x, y, rotateAngle); #if DEBUG if (scrollDelta != 0.0f) { //Debug.LogFormat("Mouse delta: {0}", scrollDelta); } #endif // calculate rotation and zoom based on mouse values if (addType == 1) { // moved rotatePinch1 = new GestureTouch(int.MaxValue - 5, xRot1, yRot1, rotatePinch1.X, rotatePinch1.Y, 0.0f, xRot1, yRot1); rotatePinch2 = new GestureTouch(int.MaxValue - 6, xRot2, yRot2, rotatePinch2.X, rotatePinch2.Y, 0.0f, xRot2, yRot2); touchesMoved.Add(rotatePinch1); touchesMoved.Add(rotatePinch2); } else if (addType == 2) { // begin rotatePinch1 = new GestureTouch(int.MaxValue - 5, xRot1, yRot1, xRot1, yRot1, 0.0f, xRot1, yRot1); rotatePinch2 = new GestureTouch(int.MaxValue - 6, xRot2, yRot2, xRot2, yRot2, 0.0f, xRot1, yRot1); touchesBegan.Add(rotatePinch1); touchesBegan.Add(rotatePinch2); } else if (addType == 3) { // end touchesEnded.Add(rotatePinch1); touchesEnded.Add(rotatePinch2); } }
private void AddTouch(int id, Vector2 position, UnityEngine.TouchPhase phase, float pressure = 1.0f) { GestureTouch touch = FingersScript.Instance.GestureTouchFromVirtualTouch(id, position, phase, pressure); virtualTouches[id] = touch; }