Ejemplo n.º 1
0
    // 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
         */
    }
Ejemplo n.º 2
0
    IEnumerator MouseRoutine(int index)
    {
        mouseIndex.Add(index);

        //init tap variables
        float   startTime = Time.time;
        Vector2 startPos  = Input.mousePosition;
        Vector2 lastPos   = startPos;
        bool    longTap   = false;

        //init charge variables
        _ChargeState chargeState     = _ChargeState.Clear;
        int          chargeDir       = 1;
        float        chargeConst     = 0;
        float        startTimeCharge = Time.time;
        Vector2      startPosCharge  = Input.mousePosition;

        yield return(null);

        while (mouseIndex.Contains(index))
        {
            Vector2 curPos = Input.mousePosition;

            if (Time.time - startTimeCharge > minChargeTime && chargeState == _ChargeState.Clear)
            {
                chargeState = _ChargeState.Charging;
                float       chargedValue = Mathf.Clamp(chargeConst + chargeDir * ((Time.time - startTimeCharge) / maxChargeTime), 0, 1);
                ChargedInfo cInfo        = new ChargedInfo(curPos, chargedValue, index, true);
                Gesture.ChargeStart(cInfo);

                startPosCharge = curPos;
            }
            else if (chargeState == _ChargeState.Charging)
            {
                if (Vector3.Distance(curPos, startPosCharge) > 5)
                {
                    chargeState = _ChargeState.Clear;
                    float       chargedValue = Mathf.Clamp(chargeConst + chargeDir * ((Time.time - startTimeCharge) / maxChargeTime), 0, 1);
                    ChargedInfo cInfo        = new ChargedInfo(lastPos, chargedValue, index, true);
                    Gesture.ChargeEnd(cInfo);
                }
                else
                {
                    float       chargedValue = Mathf.Clamp(chargeConst + chargeDir * ((Time.time - startTimeCharge) / maxChargeTime), 0, 1);
                    ChargedInfo cInfo        = new ChargedInfo(curPos, chargedValue, index, true);

                    if (chargeMode == _ChargeMode.PingPong)
                    {
                        if (chargedValue == 1 || chargedValue == 0)
                        {
                            chargeDir *= -1;
                            if (chargeDir == 1)
                            {
                                chargeConst = 0;
                            }
                            else if (chargeDir == -1)
                            {
                                chargeConst = 1;
                            }
                            startTimeCharge = Time.time;
                        }
                        Gesture.Charging(cInfo);
                    }
                    else
                    {
                        if (chargedValue < 1.0f)
                        {
                            Gesture.Charging(cInfo);
                        }
                        else
                        {
                            cInfo.percent = 1.0f;

                            if (chargeMode == _ChargeMode.Once)
                            {
                                chargeState = _ChargeState.Charged;
                                Gesture.ChargeEnd(cInfo);
                                startTimeCharge = Mathf.Infinity;
                                chargedValue    = 0;
                            }
                            else if (chargeMode == _ChargeMode.Clamp)
                            {
                                chargeState = _ChargeState.Charged;
                                Gesture.Charging(cInfo);
                            }
                            else if (chargeMode == _ChargeMode.Loop)
                            {
                                chargeState = _ChargeState.Clear;
                                Gesture.ChargeEnd(cInfo);
                                startTimeCharge = Time.time;
                            }
                        }
                    }
                }
            }

            if (!longTap && Time.time - startTime > longTapTime && Vector2.Distance(lastPos, startPos) < 5)
            {
                Gesture.LongTap(new Tap(curPos, 1, index, true));
                longTap = true;
            }

            lastPos = curPos;

            yield return(null);
        }

        //check for shortTap
        //if(Time.time-startTime<=shortTapTime && Vector2.Distance(lastPos, startPos)<5){ // TAKEN OUT BY ME!! Want full on taps for Windows - won't be using taps for shooting in iOS anyhow
        if (Time.time - startTime <= shortTapTime)
        {
            //Gesture.ShortTap(startPos);
            CheckMultiTapMouse(index, startPos);
        }

        //check for charge
        if (chargeState == _ChargeState.Charging || (chargeState == _ChargeState.Charged && chargeMode != _ChargeMode.Once))
        {
            float       chargedValue = Mathf.Clamp(chargeConst + chargeDir * ((Time.time - startTimeCharge) / maxChargeTime), 0, 1);
            ChargedInfo cInfo        = new ChargedInfo(lastPos, chargedValue, index, true);
            Gesture.ChargeEnd(cInfo);
        }
    }
Ejemplo n.º 3
0
    public IEnumerator Routine(TapDetector tapD)
    {
        tapDetector = tapD;

        triggerTime     = Time.time;
        startTimeCharge = Time.time;

        yield return(new WaitForSeconds(0.075f));

        if (indexes.Count < 2)
        {
            routineEnded = true;
            yield break;
        }
        else
        {
            count = indexes.Count;

            posAvg = Vector2.zero;
            foreach (Vector2 p in positions)
            {
                posAvg += p;
            }
            posAvg /= positions.Count;

            posList = new Vector2[positions.Count];
            positions.CopyTo(posList);
            indexList = new int[indexes.Count];
            indexes.CopyTo(indexList);
        }

        bool isOn = true;

        float liftTime = -1;

        while (isOn)
        {
            for (int i = 0; i < indexes.Count; i++)
            {
                Touch touch = Gesture.GetTouch(indexes[i]);
                if (touch.phase == TouchPhase.Moved)
                {
                    isOn = false;
                }

                if (touch.position == Vector2.zero)
                {
                    if (indexes.Count == count)
                    {
                        liftTime = Time.time;
                    }
                    indexes.RemoveAt(i);
                    i--;
                }
            }

            if (Time.time - startTimeCharge > tapDetector.minChargeTime && chargeState == _ChargeState.Clear)
            {
                chargeState = _ChargeState.Charging;
                float       chargedValue = Mathf.Clamp(chargeConst + chargeDir * ((Time.time - startTimeCharge) / tapDetector.maxChargeTime), 0, 1);
                ChargedInfo cInfo        = new ChargedInfo(posAvg, posList, chargedValue, indexList);
                Gesture.ChargeStart(cInfo);
            }
            else if (chargeState == _ChargeState.Charging)
            {
                float       chargedValue = Mathf.Clamp(chargeConst + chargeDir * ((Time.time - startTimeCharge) / tapDetector.maxChargeTime), 0, 1);
                ChargedInfo cInfo        = new ChargedInfo(posAvg, posList, chargedValue, indexList);

                if (tapDetector.chargeMode == _ChargeMode.PingPong)
                {
                    if (chargedValue == 1 || chargedValue == 0)
                    {
                        chargeDir *= -1;
                        if (chargeDir == 1)
                        {
                            chargeConst = 0;
                        }
                        else if (chargeDir == -1)
                        {
                            chargeConst = 1;
                        }
                        startTimeCharge = Time.time;
                    }

                    Gesture.Charging(cInfo);
                }
                else
                {
                    if (chargedValue < 1.0f)
                    {
                        Gesture.Charging(cInfo);
                    }
                    else
                    {
                        cInfo.percent = 1.0f;

                        if (tapDetector.chargeMode == _ChargeMode.Once)
                        {
                            chargeState = _ChargeState.Charged;
                            Gesture.ChargeEnd(cInfo);
                            startTimeCharge = Mathf.Infinity;
                            chargedValue    = 0;
                        }
                        else if (tapDetector.chargeMode == _ChargeMode.Clamp)
                        {
                            chargeState = _ChargeState.Charged;
                            Gesture.Charging(cInfo);
                        }
                        else if (tapDetector.chargeMode == _ChargeMode.Loop)
                        {
                            chargeState = _ChargeState.Clear;
                            Gesture.ChargeEnd(cInfo);
                            startTimeCharge = Time.time;
                        }
                    }
                }
            }

            if (!longTap && Time.time - triggerTime > tapDetector.longTapTime)
            {
                if (indexes.Count == count)
                {
                    Vector2[] posList = new Vector2[positions.Count];
                    positions.CopyTo(posList);

                    Tap tap = new Tap(1, count, posList, indexList);
                    Gesture.LongTap(tap);
                    longTap = true;
                }
            }

            if (indexes.Count < count)
            {
                if (Time.time - liftTime > 0.075f || indexes.Count == 0)
                {
                    if (indexes.Count == 0)
                    {
                        if (liftTime - triggerTime < tapDetector.shortTapTime + 0.1f)
                        {
                            Vector2[] posList = new Vector2[positions.Count];
                            positions.CopyTo(posList);
                            //Tap tap=new Tap(1, count, posList);
                            //Gesture.MFShortTap(tap);
                            tapDetector.CheckMultiTapMFTouch(count, posList, indexList);
                        }
                    }
                    isOn = false;
                    break;
                }
            }

            yield return(null);
        }

        if (chargeState == _ChargeState.Charging || (chargeState == _ChargeState.Charged && tapDetector.chargeMode != _ChargeMode.Once))
        {
            float       chargedValue = Mathf.Clamp(chargeConst + chargeDir * ((Time.time - startTimeCharge) / tapDetector.maxChargeTime), 0, 1);
            ChargedInfo cInfo        = new ChargedInfo(posAvg, posList, chargedValue, indexList);
            Gesture.ChargeEnd(cInfo);
        }

        routineEnded = true;
    }
Ejemplo n.º 4
0
    IEnumerator FingerRoutine(int index)
    {
        fingerIndex.Add(index);

        //init tap variables
        Touch   touch     = Gesture.GetTouch(index);
        float   startTime = Time.time;
        Vector2 startPos  = touch.position;
        Vector2 lastPos   = startPos;
        bool    longTap   = false;

        //init charge variables
        _ChargeState chargeState     = _ChargeState.Clear;
        int          chargeDir       = 1;
        int          chargeConst     = 0;
        float        startTimeCharge = Time.time;
        Vector2      startPosCharge  = touch.position;

        //yield return null;

        while (true)
        {
            touch = Gesture.GetTouch(index);
            if (touch.position == Vector2.zero)
            {
                break;
            }

            Vector2 curPos = touch.position;

            if (Time.time - startTimeCharge > minChargeTime && chargeState == _ChargeState.Clear)
            {
                chargeState = _ChargeState.Charging;
                float       chargedValue = Mathf.Clamp(chargeConst + chargeDir * ((Time.time - startTimeCharge) / maxChargeTime), 0, 1);
                ChargedInfo cInfo        = new ChargedInfo(curPos, chargedValue, index, false);
                Gesture.ChargeStart(cInfo);

                startPosCharge = curPos;
            }
            else if (chargeState == _ChargeState.Charging)
            {
                if (Vector3.Distance(curPos, startPosCharge) > 15)
                {
                    chargeState = _ChargeState.Clear;
                    float       chargedValue = Mathf.Clamp(chargeConst + chargeDir * ((Time.time - startTimeCharge) / maxChargeTime), 0, 1);
                    ChargedInfo cInfo        = new ChargedInfo(lastPos, chargedValue, index, false);
                    Gesture.ChargeEnd(cInfo);
                }
                else
                {
                    float       chargedValue = Mathf.Clamp(chargeConst + chargeDir * ((Time.time - startTimeCharge) / maxChargeTime), 0, 1);
                    ChargedInfo cInfo        = new ChargedInfo(curPos, chargedValue, index, false);

                    if (chargeMode == _ChargeMode.PingPong)
                    {
                        if (chargedValue == 1 || chargedValue == 0)
                        {
                            chargeDir *= -1;
                            if (chargeDir == 1)
                            {
                                chargeConst = 0;
                            }
                            else if (chargeDir == -1)
                            {
                                chargeConst = 1;
                            }
                            startTimeCharge = Time.time;
                        }

                        Gesture.Charging(cInfo);
                    }
                    else
                    {
                        if (chargedValue < 1.0f)
                        {
                            Gesture.Charging(cInfo);
                        }
                        else
                        {
                            cInfo.percent = 1.0f;

                            if (chargeMode == _ChargeMode.Once)
                            {
                                chargeState = _ChargeState.Charged;
                                Gesture.ChargeEnd(cInfo);
                                startTimeCharge = Mathf.Infinity;
                                chargedValue    = 0;
                            }
                            else if (chargeMode == _ChargeMode.Clamp)
                            {
                                chargeState = _ChargeState.Charged;
                                Gesture.Charging(cInfo);
                            }
                            else if (chargeMode == _ChargeMode.Loop)
                            {
                                chargeState = _ChargeState.Clear;
                                Gesture.ChargeEnd(cInfo);
                                startTimeCharge = Time.time;
                            }
                        }
                    }
                }
            }

            if (!longTap && Time.time - startTime > longTapTime && Vector2.Distance(lastPos, startPos) < 5)
            {
                //new Tap(multiTapMFTouch[index].count, fCount, posL)
                //Gesture.LongTap(new Tap(multiTapMFTouch[index].count, fCount, posL));
                Gesture.LongTap(new Tap(curPos, 1, index, false));
                //Gesture.LongTap(startPos);
                longTap = true;
            }

            lastPos = curPos;

            yield return(null);
        }

        //check for shortTap
        //if(Time.time-startTime<=shortTapTime && Vector2.Distance(lastPos, startPos)<5){
        if (Time.time - startTime <= shortTapTime)
        {
            CheckMultiTapTouch(index, startPos);
        }

        //check for charge
        if (chargeState == _ChargeState.Charging || (chargeState == _ChargeState.Charged && chargeMode != _ChargeMode.Once))
        {
            float       chargedValue = Mathf.Clamp(chargeConst + chargeDir * ((Time.time - startTimeCharge) / maxChargeTime), 0, 1);
            ChargedInfo cInfo        = new ChargedInfo(lastPos, chargedValue, index, false);
            Gesture.ChargeEnd(cInfo);
        }

        fingerIndex.Remove(index);
    }