// Update is called once per frame void Update() { InputEvent inputEvent = new InputEvent(); if (Input.touchCount == 1) { Touch touch = Input.touches[0]; _InputState state; if (touch.phase == TouchPhase.Began) { state = _InputState.Down; } else if (touch.phase == TouchPhase.Ended) { state = _InputState.Up; } else { state = _InputState.On; } inputEvent = new InputEvent(touch.position, _InputType.Touch, state); } else if (Input.touchCount == 0) { if (Input.GetMouseButtonDown(0)) { inputEvent = new InputEvent(Input.mousePosition, _InputType.Mouse1, _InputState.Down); } else if (Input.GetMouseButton(0)) { inputEvent = new InputEvent(Input.mousePosition, _InputType.Mouse1, _InputState.On); } else if (Input.GetMouseButtonUp(0)) { inputEvent = new InputEvent(Input.mousePosition, _InputType.Mouse1, _InputState.Up); } else if (Input.GetMouseButtonDown(1)) { inputEvent = new InputEvent(Input.mousePosition, _InputType.Mouse2, _InputState.Down); } else if (Input.GetMouseButton(1)) { inputEvent = new InputEvent(Input.mousePosition, _InputType.Mouse2, _InputState.On); } else if (Input.GetMouseButtonUp(1)) { inputEvent = new InputEvent(Input.mousePosition, _InputType.Mouse2, _InputState.Up); } } if (inputEvent.inputType != _InputType.None) { Vector2 curPos = inputEvent.pos; if (inputEvent.inputState == _InputState.Down) { tapStartTime = Time.time; startPos = curPos; longTap = false; posShifted = false; } else if (inputEvent.inputState == _InputState.On) { if (Vector2.Distance(curPos, startPos) > maxLTapSpacing) { posShifted = true; } if (Time.time - tapStartTime > minChargeTime) { if (chargeState == _ChargeState.Clear) { chargeState = _ChargeState.Charged; } chargedValue = Mathf.Min(1, (Time.time - tapStartTime) / maxChargeTime); ChargedInfo cInfo = new ChargedInfo(curPos, chargedValue); Gesture.Charging(cInfo); } if (!longTap && !posShifted && Time.time - tapStartTime > 1f) { longTap = true; Gesture.LongTap(startPos); } lastPos = curPos; } else if (inputEvent.inputState == _InputState.Up) { if (Time.time - tapStartTime < shortTapTime) { if (Time.time - lastShortTapTime < doubleTapTime) { if (dTapState == _DTapState.Clear) { dTapState = _DTapState.Tap1; } else if (dTapState == _DTapState.Tap1) { if (Vector2.Distance(lastPos, lastShortTapPos) < maxDTapPosSpacing) { dTapState = _DTapState.Clear; Gesture.DoubleTap((startPos + lastShortTapPos) / 2); } } } else { dTapState = _DTapState.Tap1; } lastShortTapTime = Time.time; lastShortTapPos = lastPos; Gesture.ShortTap(startPos); } if (chargeState == _ChargeState.Charged) { ChargedInfo cInfo = new ChargedInfo(lastPos, chargedValue); Gesture.ChargeEnd(cInfo); chargedValue = 0; chargeState = _ChargeState.Clear; } } } /* #if UNITY_IPHONE || UNITY_ANDROID * if(Input.touchCount==1){ * Vector2 touchPos=Input.touches[0].position; * * //touch down * if(firstTouch){ * firstTouch=false; * tapStartTime=Time.time; * startPos=touchPos; * longTap=false; * posShifted=false; * } * //on touch * else{ * if(Vector2.Distance(Input.mousePosition, startPos)>maxLTapSpacing) posShifted=true; * * if(Time.time-tapStartTime>minChargeTime){ * if(chargeState==_ChargeState.Clear) chargeState=_ChargeState.Charged; * chargedValue=Mathf.Min(1, (Time.time-tapStartTime)/maxChargeTime); * ChargedInfo cInfo=new ChargedInfo(touchPos, chargedValue); * Gesture.Charging(cInfo); * } * * if(!longTap && !posShifted && Time.time-tapStartTime>1f){ * longTap=true; * Gesture.LongTap(startPos); * } * } * * lastTouchPos=touchPos; * } * else if(Input.touchCount==0){ * //touch up * if(!firstTouch){ * firstTouch=true; * if(Time.time-tapStartTime<shortTapTime){ * if(Time.time-lastShortTapTime<doubleTapTime){ * if(dTapState==_DTapState.Clear){ * dTapState=_DTapState.Tap1; * } * else if(dTapState==_DTapState.Tap1){ * if(Vector2.Distance(lastTouchPos, lastShortTapPos)<maxDTapPosSpacing){ * * dTapState=_DTapState.Clear; * * Gesture.DoubleTap((startPos+lastShortTapPos)/2); * } * } * } * else{ * dTapState=_DTapState.Tap1; * } * * lastShortTapTime=Time.time; * lastShortTapPos=lastTouchPos; * Gesture.ShortTap(startPos); * } * } * * if(chargeState==_ChargeState.Charged){ * ChargedInfo cInfo=new ChargedInfo(lastTouchPos, chargedValue); * Gesture.ChargeEnd(cInfo); * * chargedValue=0; * chargeState=_ChargeState.Clear; * } * } #endif * #if (!UNITY_IPHONE && !UNITY_ANDROID) //|| UNITY_EDITOR * if(Input.GetMouseButtonDown(0)){ * tapStartTime=Time.time; * startPos=Input.mousePosition; * longTap=false; * posShifted=false; * } * * if(Input.GetMouseButton(0)){ * * if(Vector2.Distance(Input.mousePosition, startPos)>5) posShifted=true; * * if(Time.time-tapStartTime>minChargeTime){ * if(chargeState==_ChargeState.Clear) chargeState=_ChargeState.Charged; * chargedValue=Mathf.Min(1, (Time.time-tapStartTime)/maxChargeTime); * ChargedInfo cInfo=new ChargedInfo(Input.mousePosition, chargedValue); * Gesture.Charging(cInfo); * } * * if(!longTap && !posShifted && Time.time-tapStartTime>1f){ * longTap=true; * Gesture.LongTap(startPos); * } * * lastTouchPos=Input.mousePosition; * } * * if(Input.GetMouseButtonUp(0)){ * if(Time.time-tapStartTime<shortTapTime){ * * if(Time.time-lastShortTapTime<doubleTapTime){ * if(dTapState==_DTapState.Clear){ * dTapState=_DTapState.Tap1; * } * else if(dTapState==_DTapState.Tap1){ * if(Vector2.Distance(lastTouchPos, lastShortTapPos)<10){ * * dTapState=_DTapState.Clear; * * Gesture.DoubleTap((startPos+lastShortTapPos)/2); * * } * } * } * else{ * dTapState=_DTapState.Tap1; * } * * lastShortTapTime=Time.time; * lastShortTapPos=Input.mousePosition; * Gesture.ShortTap(startPos); * * } * * if(chargeState==_ChargeState.Charged){ * ChargedInfo cInfo=new ChargedInfo(Input.mousePosition, chargedValue); * Gesture.ChargeEnd(cInfo); * * chargedValue=0; * chargeState=_ChargeState.Clear; * } * } #endif */ }
// Update is called once per frame void Update () { #if UNITY_IPHONE || UNITY_ANDROID if(Input.touchCount==1){ Vector2 touchPos=Input.touches[0].position; //touch down if(firstTouch){ firstTouch=false; tapStartTime=Time.time; startPos=touchPos; longTap=false; posShifted=false; } //on touch else{ if(Vector2.Distance(Input.mousePosition, startPos)>maxLTapSpacing) posShifted=true; if(Time.time-tapStartTime>minChargeTime){ if(chargeState==_ChargeState.Clear) chargeState=_ChargeState.Charged; chargedValue=Mathf.Min(1, (Time.time-tapStartTime)/maxChargeTime); ChargedInfo cInfo=new ChargedInfo(touchPos, chargedValue); Gesture.Charging(cInfo); } if(!longTap && !posShifted && Time.time-tapStartTime>1f){ longTap=true; Gesture.LongTap(startPos); } } lastTouchPos=touchPos; } else if(Input.touchCount==0){ //touch up if(!firstTouch){ firstTouch=true; if(Time.time-tapStartTime<shortTapTime){ if(Time.time-lastShortTapTime<doubleTapTime){ if(dTapState==_DTapState.Clear){ dTapState=_DTapState.Tap1; } else if(dTapState==_DTapState.Tap1){ if(Vector2.Distance(lastTouchPos, lastShortTapPos)<maxDTapPosSpacing){ dTapState=_DTapState.Clear; Gesture.DoubleTap((startPos+lastShortTapPos)/2); } } } else{ dTapState=_DTapState.Tap1; } lastShortTapTime=Time.time; lastShortTapPos=lastTouchPos; Gesture.ShortTap(startPos); } } if(chargeState==_ChargeState.Charged){ ChargedInfo cInfo=new ChargedInfo(lastTouchPos, chargedValue); Gesture.ChargeEnd(cInfo); chargedValue=0; chargeState=_ChargeState.Clear; } } #endif #if (!UNITY_IPHONE && !UNITY_ANDROID) //|| UNITY_EDITOR if(Input.GetMouseButtonDown(0)){ tapStartTime=Time.time; startPos=Input.mousePosition; longTap=false; posShifted=false; } if(Input.GetMouseButton(0)){ if(Vector2.Distance(Input.mousePosition, startPos)>5) posShifted=true; if(Time.time-tapStartTime>minChargeTime){ if(chargeState==_ChargeState.Clear) chargeState=_ChargeState.Charged; chargedValue=Mathf.Min(1, (Time.time-tapStartTime)/maxChargeTime); ChargedInfo cInfo=new ChargedInfo(Input.mousePosition, chargedValue); Gesture.Charging(cInfo); } if(!longTap && !posShifted && Time.time-tapStartTime>1f){ longTap=true; Gesture.LongTap(startPos); } lastTouchPos=Input.mousePosition; } if(Input.GetMouseButtonUp(0)){ if(Time.time-tapStartTime<shortTapTime){ if(Time.time-lastShortTapTime<doubleTapTime){ if(dTapState==_DTapState.Clear){ dTapState=_DTapState.Tap1; } else if(dTapState==_DTapState.Tap1){ if(Vector2.Distance(lastTouchPos, lastShortTapPos)<10){ dTapState=_DTapState.Clear; Gesture.DoubleTap((startPos+lastShortTapPos)/2); } } } else{ dTapState=_DTapState.Tap1; } lastShortTapTime=Time.time; lastShortTapPos=Input.mousePosition; Gesture.ShortTap(startPos); } if(chargeState==_ChargeState.Charged){ ChargedInfo cInfo=new ChargedInfo(Input.mousePosition, chargedValue); Gesture.ChargeEnd(cInfo); chargedValue=0; chargeState=_ChargeState.Clear; } } #endif }
void Update() { if (Input.touchCount == 2) { Touch touch1 = Input.touches[0]; Touch touch2 = Input.touches[1]; Vector2 pos1 = touch1.position; Vector2 pos2 = touch2.position; Vector2 delta1 = pos1 - lastTouchPos1; Vector2 delta2 = pos2 - lastTouchPos2; if (firstTouch) { firstTouch = false; //for rotate initPos1 = pos1; initPos2 = pos2; initGradient = (pos1 - pos2).normalized; float curX = pos1.x - pos2.x; float curY = pos1.y - pos2.y; float prevAngle = Gesture.VectorToAngle(new Vector2(curX, curY)); //for tap tapStartTime = Time.time; startPos = (pos1 + pos2) / 2; longTap = false; posShifted = false; } else { if (Vector2.Distance(Input.mousePosition, startPos) > 5) { posShifted = true; } if (Time.time - tapStartTime > minChargeTime) { charged = true; float chargeValue = Mathf.Min(1, (Time.time - tapStartTime) / maxChargeTime); ChargedInfo cInfo = new ChargedInfo((pos1 + pos2) / 2, chargeValue); Gesture.DFCharging(cInfo); } if (!longTap && !posShifted && Time.time - tapStartTime > 1f) { longTap = true; Gesture.DFLongTap((pos1 + pos2) / 2); } } if (touch1.phase == TouchPhase.Moved && touch2.phase == TouchPhase.Moved) { float dot = Vector2.Dot(delta1, delta2); if (dot < 0) { Vector2 grad1 = (pos1 - initPos1).normalized; Vector2 grad2 = (pos2 - initPos2).normalized; float dot1 = Vector2.Dot(grad1, initGradient); float dot2 = Vector2.Dot(grad2, initGradient); //rotate if (dot1 < 0.7f && dot2 < 0.7f) { float curX = pos1.x - pos2.x; float curY = pos1.y - pos2.y; float curAngle = Gesture.VectorToAngle(new Vector2(curX, curY)); float val = Mathf.DeltaAngle(curAngle, prevAngle); if (Mathf.Abs(val) > 0) { AddRotVal(val); } float valueAvg = GetAverageValue(); Gesture.Rotate(valueAvg); prevAngle = curAngle; } //pinch else { Vector2 curDist = pos1 - pos2; Vector2 prevDist = (pos1 - delta1) - (pos2 - delta2); float pinch = prevDist.magnitude - curDist.magnitude; Gesture.Pinch(pinch); } } //drag if (dot > 2) { dragging = true; Vector2 posAvg = (pos1 + pos2) / 2; Vector2 dir = (delta1 + delta2) / 2; DragInfo dragInfo = new DragInfo(-1, posAvg, dir); Gesture.DualFingerDragging(dragInfo); } } lastTouchPos1 = pos1; lastTouchPos2 = pos2; } else if (Input.touchCount == 0) { //~ if(!firstTouch){ //~ firstTouch=true; //~ } if (!firstTouch) { firstTouch = true; //for tap if (Time.time - tapStartTime < shortTapTime) { if (Time.time - lastShortTapTime < doubleTapTime) { if (dTapState == _DTapState.Clear) { dTapState = _DTapState.Tap1; } else if (dTapState == _DTapState.Tap1) { if (Vector2.Distance(lastTouchPos1, lastShortTapPos) < maxDTapPosSpacing) { dTapState = _DTapState.Clear; Gesture.DFDoubleTap((startPos + lastShortTapPos) / 2); } } } else { dTapState = _DTapState.Tap1; } lastShortTapTime = Time.time; lastShortTapPos = (lastTouchPos1 + lastTouchPos2) / 2; Gesture.DFShortTap(startPos); } if (dragging) { dragging = false; Gesture.DualFingerDraggingEnd((lastTouchPos1 + lastTouchPos2) / 2); } if (charged) { charged = false; float chargeValue = Mathf.Min(1, (Time.time - tapStartTime) / maxChargeTime); ChargedInfo cInfo = new ChargedInfo((lastTouchPos1 + lastTouchPos2) / 2, chargeValue); Gesture.DFChargeEnd(cInfo); } } } }