// #endif private static void touchGistureProceed(ref TouchItem touchItem) { if (tryParseDot(ref touchItem)) { touchItem.gesture = TouchItem.TouchGesture.Dot; if (TapEvent != null && !BlockEvents) { TapEvent(touchItem, new TouchManagerEventArgs(touchItem)); } GetGameObject(touchItem); return; } if (tryParseSwipe(ref touchItem)) { touchItem.gesture = TouchItem.TouchGesture.Swipe; if (SwipeEvent != null && !BlockEvents) { SwipeEvent(touchItem, new TouchManagerEventArgs(touchItem)); } return; } touchItem.gesture = TouchItem.TouchGesture.Other; }
static bool tryParseDot(ref TouchItem touchItem) { // if (touchItem.positions.Count > 4) // return false; if (touchItem.distanceX + touchItem.distanceY > 4) { return(false); } return(true); }
private static void GetGameObject(TouchItem touchItem) { if (GameObjectTapEvent != null && !BlockEvents) { Ray ray = Camera.main.ScreenPointToRay(touchItem.position); RaycastHit hit; if (Physics.Raycast(ray, out hit, 10f)) { GameObjectTapEvent(hit.collider.gameObject, new TouchManagerEventArgs(touchItem)); } } }
void OnTap(TouchItem touchItem) { RaycastHit hitInfo = new RaycastHit(); if (Physics.Raycast(Camera.mainCamera.ScreenPointToRay(touchItem.position), out hitInfo)) { if (hitInfo.transform.GetComponent(typeof(ITouchable)) != null) { ((ITouchable)hitInfo.transform.GetComponent(typeof(ITouchable))).OnTouch(Camera.mainCamera.ScreenToWorldPoint(touchItem.position)); } } }
static bool tryParseSwipe(ref TouchItem touchItem) { if (touchItem.positions.Count < 4) { return(false); } float dx = touchItem.position.x - touchItem.startPosition.x, dy = touchItem.position.y - touchItem.startPosition.y; float k = Mathf.Abs(dx / dy); if (k > 1) // 1/Mathf.Sqrt(3) { if (dx > 0) { if (SwipeRightEvent != null && !BlockEvents) { SwipeRightEvent(touchItem, new TouchManagerEventArgs(touchItem)); } } else { if (SwipeLeftEvent != null && !BlockEvents) { SwipeLeftEvent(touchItem, new TouchManagerEventArgs(touchItem)); } } return(true); } else { if (dy > 0) { if (SwipeUpEvent != null && !BlockEvents) { SwipeUpEvent(touchItem, new TouchManagerEventArgs(touchItem)); } } else { if (SwipeDownEvent != null && !BlockEvents) { SwipeDownEvent(touchItem, new TouchManagerEventArgs(touchItem)); } } return(true); } }
private void UpdatePosition(ref TouchItem touch) { bFirstClick = false; Vector2 mousePosition = Input.mousePosition; touch.deltaPosition = mousePosition - touch.position; if (touch.deltaPosition.sqrMagnitude > 0f) { touch.phase = TouchPhaseEnum.MOVED; } else { touch.phase = TouchPhaseEnum.STATIONARY; } touch.position = mousePosition; }
static bool tryParseSwipe(ref TouchItem touchItem) { if (touchItem.positions.Count < 4) return false; float dx = touchItem.position.x - touchItem.startPosition.x, dy = touchItem.position.y - touchItem.startPosition.y; float k = Mathf.Abs(dx / dy); if (k > 1) // 1/Mathf.Sqrt(3) { if (dx > 0) { if (SwipeRightEvent != null && !BlockEvents) SwipeRightEvent(touchItem, new TouchManagerEventArgs(touchItem)); } else { if (SwipeLeftEvent != null && !BlockEvents) SwipeLeftEvent(touchItem, new TouchManagerEventArgs(touchItem)); } return true; } else { if (dy > 0) { if (SwipeUpEvent != null && !BlockEvents) SwipeUpEvent(touchItem, new TouchManagerEventArgs(touchItem)); } else { if (SwipeDownEvent != null && !BlockEvents) SwipeDownEvent(touchItem, new TouchManagerEventArgs(touchItem)); } return true; } }
static bool tryParseDot(ref TouchItem touchItem) { // if (touchItem.positions.Count > 4) // return false; if (touchItem.distanceX + touchItem.distanceY > 4) return false; return true; }
static bool tryParseCircle(ref TouchItem touchItem) { int count = touchItem.positions.Count; //print("positions count is " + count); if (count < 5) return false; float cx = 0, cy = 0; foreach (Vector2 pos in touchItem.positions) { cx += pos.x; cy += pos.y; } cx /= count; cy /= count; Vector2 center = new Vector2(cx, cy); float midleRadius = 0, minRadius = float.MaxValue, maxRadius = 0; foreach (Vector2 pos in touchItem.positions) { float rad = Vector2.Distance(center, pos); midleRadius += rad; if (rad < minRadius) minRadius = rad; if (rad > maxRadius) maxRadius = rad; } midleRadius /= count; short pF = axisPart((Vector2)touchItem.positions[1] - (Vector2)touchItem.positions[0]); int wrongsCW = 0, wrongsCCW = 0; int partChCW = 0, partChCCW = 0; short cur = pF; string poss = ""; for (int i = 2; i < count; i++) { short next = axisPart((Vector2)touchItem.positions[i] - (Vector2)touchItem.positions[i - 1]); int cw = axisNext(cur, next, true), ccw = axisNext(cur, next, false); poss += next.ToString(); if (cw <= -1) wrongsCW++; if (ccw <= -1) wrongsCCW++; if (cw == 1) partChCW++; if (ccw == 1) partChCCW++; cur = next; } partChCW = Mathf.RoundToInt((float)partChCW / 4f); partChCCW = Mathf.RoundToInt((float)partChCCW / 4f); int wrongs = Mathf.Min(wrongsCCW, wrongsCW), partsCh = Mathf.Max(partChCW, partChCCW); if (wrongs == wrongsCCW) partsCh = -partsCh; wrongs = (int)((float)wrongs / count * 100); //print(poss); //print("wrongs: " + wrongs + "% rotates:" + partsCh); if (wrongs > 25) { //print("to many wrongs"); return false; } int[] counts = new int[4]; foreach (Vector2 pos in touchItem.positions) counts[axisPart(pos - center)]++; int midlePartsCount = 0; foreach (int x in counts) midlePartsCount += x; midlePartsCount /= 4; foreach (int x in counts) if (Mathf.Abs(midlePartsCount - x) > 5) { //print("incorrect parts count"); return false; } float minRadiusD = float.MaxValue, maxRadiusD = 0; foreach (Vector2 pos in touchItem.positions) { float curDelta = Vector2.Distance(pos, center) / midleRadius * 100; if (curDelta > maxRadiusD) maxRadiusD = curDelta; if (curDelta < minRadiusD) minRadiusD = curDelta; } //print("min radius delta: " + minRadiusD + "% max radius delta: " + maxRadiusD + "%"); if (minRadiusD < 45 || maxRadiusD > 160) { //print("wrong radiuses"); return false; } //print("circle!!"); GistureCircle param = new GistureCircle(); param.center = center; param.radius = midleRadius; param.rotates = partsCh; touchItem.gistureParams = param; return true; }
// #endif private static void touchGistureProceed(ref TouchItem touchItem) { if (tryParseDot(ref touchItem)) { touchItem.gesture = TouchItem.TouchGesture.Dot; if (TapEvent != null && !BlockEvents) TapEvent(touchItem, new TouchManagerEventArgs(touchItem)); GetGameObject(touchItem); return; } if (tryParseSwipe(ref touchItem)) { touchItem.gesture = TouchItem.TouchGesture.Swipe; if (SwipeEvent != null && !BlockEvents) SwipeEvent(touchItem, new TouchManagerEventArgs(touchItem)); return; } touchItem.gesture = TouchItem.TouchGesture.Other; }
//#if UNITY_IPHONE private static int ProcessRealTouches() { count = Input.touchCount; for (int i = 0; i < count; i++) { Touch touch = Input.GetTouch(i); TouchPhase phase = touch.phase; int fingerId = touch.fingerId; if (fingerId >= MAX_TOUCHES) { fingerId = fingerId % MAX_TOUCHES; } touchItem = touchCache[fingerId]; if (!touchItem.valid) phase = TouchPhase.Began; // Cache the touch data. touchItem.deltaPosition = touch.deltaPosition; touchItem.position = touch.position; if (touch.deltaPosition.magnitude > sqrDelta) touchItem.positions.Add(touch.position); try { switch (phase) { case TouchPhase.Ended: touchItem.phase = TouchPhaseEnum.ENDED; touchGistureProceed(ref touchItem); if (TouchEndedEvent != null && !BlockEvents) TouchEndedEvent(touch, new TouchManagerEventArgs(touchItem)); break; case TouchPhase.Canceled: touchItem.phase = TouchPhaseEnum.CANCELED; break; case TouchPhase.Began: touchItem.phase = TouchPhaseEnum.BEGAN; touchItem.startPosition = touch.position; touchItem.positions = new ArrayList(); touchItem.gesture = TouchItem.TouchGesture.Other; touchItem.distanceX = 0; touchItem.distanceY = 0; touchItem.valid = true; if (TouchBeganEvent != null && !BlockEvents) TouchBeganEvent(touch, new TouchManagerEventArgs(touchItem)); break; case TouchPhase.Moved: touchItem.phase = TouchPhaseEnum.MOVED; touchItem.distanceX += Mathf.Abs(touch.deltaPosition.x); touchItem.distanceY += Mathf.Abs(touch.deltaPosition.y); //float dx = touchItem.position.x - touchItem.startPosition.x, // dy = touchItem.position.y - touchItem.startPosition.y; float dx = touch.deltaPosition.x, dy = touch.deltaPosition.y; float dk = 2f; Debug.Log(dx * dx + dy * dy > dk * dk); if (dx * dx + dy * dy > dk * dk) { if (TouchMoveEvent != null && !BlockEvents) { TouchMoveEvent (touch, new TouchManagerEventArgs (touchItem)); } if (Mathf.Abs(dx / dy) > 1f) { if (dx > 0) { if (MoveRightEvent != null && !BlockEvents) MoveRightEvent(touch, new TouchManagerEventArgs(touchItem)); } else { if (MoveLeftEvent != null && !BlockEvents) MoveLeftEvent(touch, new TouchManagerEventArgs(touchItem)); } } else { if (dy > 0) { if (MoveUpEvent != null && !BlockEvents) MoveUpEvent(touch, new TouchManagerEventArgs(touchItem)); } else { if (MoveDownEvent != null && !BlockEvents) MoveDownEvent(touch, new TouchManagerEventArgs(touchItem)); } } } break; case TouchPhase.Stationary: touchItem.phase = TouchPhaseEnum.STATIONARY; break; } } finally { touchCache[fingerId] = touchItem; } } return count; }
private static void GetGameObject(TouchItem touchItem) { if (GameObjectTapEvent != null && !BlockEvents) { Ray ray = Camera.main.ScreenPointToRay(touchItem.position); RaycastHit hit; if (Physics.Raycast(ray, out hit, 10f)) GameObjectTapEvent(hit.collider.gameObject, new TouchManagerEventArgs(touchItem)); } }
public TouchManagerEventArgs(TouchItem touch) { Touch = touch; }
static bool tryParseCircle(ref TouchItem touchItem) { int count = touchItem.positions.Count; //print("positions count is " + count); if (count < 5) { return(false); } float cx = 0, cy = 0; foreach (Vector2 pos in touchItem.positions) { cx += pos.x; cy += pos.y; } cx /= count; cy /= count; Vector2 center = new Vector2(cx, cy); float midleRadius = 0, minRadius = float.MaxValue, maxRadius = 0; foreach (Vector2 pos in touchItem.positions) { float rad = Vector2.Distance(center, pos); midleRadius += rad; if (rad < minRadius) { minRadius = rad; } if (rad > maxRadius) { maxRadius = rad; } } midleRadius /= count; short pF = axisPart((Vector2)touchItem.positions[1] - (Vector2)touchItem.positions[0]); int wrongsCW = 0, wrongsCCW = 0; int partChCW = 0, partChCCW = 0; short cur = pF; string poss = ""; for (int i = 2; i < count; i++) { short next = axisPart((Vector2)touchItem.positions[i] - (Vector2)touchItem.positions[i - 1]); int cw = axisNext(cur, next, true), ccw = axisNext(cur, next, false); poss += next.ToString(); if (cw <= -1) { wrongsCW++; } if (ccw <= -1) { wrongsCCW++; } if (cw == 1) { partChCW++; } if (ccw == 1) { partChCCW++; } cur = next; } partChCW = Mathf.RoundToInt((float)partChCW / 4f); partChCCW = Mathf.RoundToInt((float)partChCCW / 4f); int wrongs = Mathf.Min(wrongsCCW, wrongsCW), partsCh = Mathf.Max(partChCW, partChCCW); if (wrongs == wrongsCCW) { partsCh = -partsCh; } wrongs = (int)((float)wrongs / count * 100); //print(poss); //print("wrongs: " + wrongs + "% rotates:" + partsCh); if (wrongs > 25) { //print("to many wrongs"); return(false); } int[] counts = new int[4]; foreach (Vector2 pos in touchItem.positions) { counts[axisPart(pos - center)]++; } int midlePartsCount = 0; foreach (int x in counts) { midlePartsCount += x; } midlePartsCount /= 4; foreach (int x in counts) { if (Mathf.Abs(midlePartsCount - x) > 5) { //print("incorrect parts count"); return(false); } } float minRadiusD = float.MaxValue, maxRadiusD = 0; foreach (Vector2 pos in touchItem.positions) { float curDelta = Vector2.Distance(pos, center) / midleRadius * 100; if (curDelta > maxRadiusD) { maxRadiusD = curDelta; } if (curDelta < minRadiusD) { minRadiusD = curDelta; } } //print("min radius delta: " + minRadiusD + "% max radius delta: " + maxRadiusD + "%"); if (minRadiusD < 45 || maxRadiusD > 160) { //print("wrong radiuses"); return(false); } //print("circle!!"); GistureCircle param = new GistureCircle(); param.center = center; param.radius = midleRadius; param.rotates = partsCh; touchItem.gistureParams = param; return(true); }
//#if UNITY_IPHONE private static int ProcessRealTouches() { count = Input.touchCount; for (int i = 0; i < count; i++) { Touch touch = Input.GetTouch(i); TouchPhase phase = touch.phase; int fingerId = touch.fingerId; if (fingerId >= MAX_TOUCHES) { fingerId = fingerId % MAX_TOUCHES; } touchItem = touchCache[fingerId]; if (!touchItem.valid) { phase = TouchPhase.Began; } // Cache the touch data. touchItem.deltaPosition = touch.deltaPosition; touchItem.position = touch.position; if (touch.deltaPosition.magnitude > sqrDelta) { touchItem.positions.Add(touch.position); } try { switch (phase) { case TouchPhase.Ended: touchItem.phase = TouchPhaseEnum.ENDED; touchGistureProceed(ref touchItem); if (TouchEndedEvent != null && !BlockEvents) { TouchEndedEvent(touch, new TouchManagerEventArgs(touchItem)); } break; case TouchPhase.Canceled: touchItem.phase = TouchPhaseEnum.CANCELED; break; case TouchPhase.Began: touchItem.phase = TouchPhaseEnum.BEGAN; touchItem.startPosition = touch.position; touchItem.positions = new ArrayList(); touchItem.gesture = TouchItem.TouchGesture.Other; touchItem.distanceX = 0; touchItem.distanceY = 0; touchItem.valid = true; if (TouchBeganEvent != null && !BlockEvents) { TouchBeganEvent(touch, new TouchManagerEventArgs(touchItem)); } break; case TouchPhase.Moved: touchItem.phase = TouchPhaseEnum.MOVED; touchItem.distanceX += Mathf.Abs(touch.deltaPosition.x); touchItem.distanceY += Mathf.Abs(touch.deltaPosition.y); //float dx = touchItem.position.x - touchItem.startPosition.x, // dy = touchItem.position.y - touchItem.startPosition.y; float dx = touch.deltaPosition.x, dy = touch.deltaPosition.y; float dk = 2f; Debug.Log(dx * dx + dy * dy > dk * dk); if (dx * dx + dy * dy > dk * dk) { if (TouchMoveEvent != null && !BlockEvents) { TouchMoveEvent(touch, new TouchManagerEventArgs(touchItem)); } if (Mathf.Abs(dx / dy) > 1f) { if (dx > 0) { if (MoveRightEvent != null && !BlockEvents) { MoveRightEvent(touch, new TouchManagerEventArgs(touchItem)); } } else { if (MoveLeftEvent != null && !BlockEvents) { MoveLeftEvent(touch, new TouchManagerEventArgs(touchItem)); } } } else { if (dy > 0) { if (MoveUpEvent != null && !BlockEvents) { MoveUpEvent(touch, new TouchManagerEventArgs(touchItem)); } } else { if (MoveDownEvent != null && !BlockEvents) { MoveDownEvent(touch, new TouchManagerEventArgs(touchItem)); } } } } break; case TouchPhase.Stationary: touchItem.phase = TouchPhaseEnum.STATIONARY; break; } } finally { touchCache[fingerId] = touchItem; } } return(count); }