示例#1
0
 public abstract bool IsTouchFallingThrough(TouchData td, bool isRelevant);
示例#2
0
        public void UpdateTouchData(Touch[] touches, float tDelta)
        {
#if UNITY_ANDROID
            // remove tds whose touches are already gone
            //List<int> keysTouchData = touchData.Keys.ToList();
            //List<int> idsTouches = touches.Select(i => i.fingerId).ToList();

            //foreach(int id in keysTouchData)
            //{
            //    if(!idsTouches.Contains(id))
            //    {
            //        if(!touchData.ContainsKey(id)) MobileConsole.Say("remove CANT BE :(");
            //        touchData.Remove(id);
            //    }
            //}

            foreach (Touch touch in touches)
            {
                int idTouch = touch.fingerId;

                //if(!touchData.ContainsKey(touch.fingerId))
                //{
                //    MobileConsole.Say("skip if not relevant in td");
                //    break;
                //}

                //td = touchData[touch.fingerId];

                switch (touch.phase)
                {
                case TouchPhase.Began:
                {
                    if (touchData.ContainsKey(idTouch))
                    {
                        //DevCon.Say("ERROR - contains began");
                    }
                    else
                    {
                        TouchData tdBegan = new TouchData();

                        tdBegan.phasePointer = PhasePointer.BEGAN;
                        tdBegan.idTouch      = touch.fingerId;
                        tdBegan.posOrigin    = touch.position;
                        tdBegan.position     = touch.position;
                        tdBegan.posPrev      = touch.position;

                        MonoBehaviour monoHit;
                        Vector3       posHit;
                        if (GetObjsHit(touch.position, out posHit, out monoHit))
                        {
                            tdBegan.monoOrigin  = monoHit;
                            tdBegan.monoCurrent = monoHit;
                            tdBegan.posHit      = posHit;
                        }

                        touchData.Add(touch.fingerId, tdBegan);
                    }

                    break;
                }

                case TouchPhase.Moved:
                {
                    if (!touchData.ContainsKey(idTouch))
                    {
                        //DevCon.Say("ERROR - not contained move");
                    }
                    else
                    {
                        TouchData td = touchData[idTouch];
                        td.phasePointer = PhasePointer.MOVED;
                        td.timeTouch   += tDelta;
                        td.timeStatic   = 0f;

                        td.posPrev    = td.position;
                        td.position   = touch.position;
                        td.delta      = td.position - td.posPrev;
                        td.deltaSwipe = td.position - td.posOrigin;
                        td.distSwipe  = td.deltaSwipe.magnitude;
                        td.distTouch += td.delta.magnitude;

                        td.monoPrev = td.monoCurrent;

                        MonoBehaviour monoHit;
                        Vector3       posHit;
                        if (GetObjsHit(touch.position, out posHit, out monoHit))
                        {
                            td.monoCurrent = monoHit;
                            td.posHit      = posHit;
                        }
                        else
                        {
                            td.monoCurrent = null;
                        }

                        if (td.monoCurrent != null)
                        {
                            td.timeOnObject += tDelta;
                        }

                        if (td.monoCurrent != td.monoPrev)
                        {
                            td.timeOnObject = 0f;
                            td.hasSentHold  = false;
                        }
                    }
                    break;
                }

                case TouchPhase.Stationary:
                {
                    if (!touchData.ContainsKey(idTouch))
                    {
                        //DevCon.Say("ERROR - not contained stationary");
                    }
                    else
                    {
                        TouchData td = touchData[idTouch];
                        td.phasePointer  = PhasePointer.STATIC;
                        td.timeTouch    += tDelta;
                        td.timeStatic   += tDelta;
                        td.timeOnObject += tDelta;
                        td.delta         = Vector2.zero;
                    }
                    break;
                }

                case TouchPhase.Canceled:
                case TouchPhase.Ended:
                {
                    if (!touchData.ContainsKey(idTouch))
                    {
                        //DevCon.Say("ERROR - not contained canceled, ended");
                    }
                    else
                    {
                        TouchData td = touchData[idTouch];
                        td.phasePointer = PhasePointer.ENDED;
                        td.removeAtEnd  = true;
                    }
                    break;
                }

                default:
                {
                    //DevCon.Say("ERROR - phase default");
                    break;
                }
                }
            }
#endif

#if UNITY_EDITOR
            if (Input.GetMouseButtonDown(idButtonMouse))
            {
                if (touchData.ContainsKey(idButtonMouse))
                {
                    //DevCon.Say("ERROR - contains began");
                }
                else
                {
                    TouchData tdBegan = new TouchData();

                    tdBegan.idTouch   = idButtonMouse;
                    tdBegan.posOrigin = Input.mousePosition;
                    tdBegan.position  = Input.mousePosition;
                    tdBegan.posPrev   = Input.mousePosition;

                    MonoBehaviour monoHit;
                    Vector3       posHit;
                    if (GetObjsHit(Input.mousePosition, out posHit, out monoHit))
                    {
                        tdBegan.monoOrigin  = monoHit;
                        tdBegan.monoCurrent = monoHit;
                        tdBegan.posHit      = posHit;
                    }

                    touchData.Add(idButtonMouse, tdBegan);
                }
            }
            else if (Input.GetMouseButton(idButtonMouse))
            {
                if (!touchData.ContainsKey(idButtonMouse))
                {
                    //DevCon.Say("ERROR - not contains previous");
                }
                else
                {
                    TouchData td = touchData[idButtonMouse];
                    if (td.posPrev == (Vector2)Input.mousePosition)
                    {
                        // stationary
                        td.phasePointer  = PhasePointer.STATIC;
                        td.timeTouch    += tDelta;
                        td.timeStatic   += tDelta;
                        td.timeOnObject += tDelta;
                        td.delta         = Vector2.zero;
                    }
                    else
                    {
                        // moved
                        td.phasePointer = PhasePointer.MOVED;
                        td.timeTouch   += tDelta;
                        td.timeStatic   = 0f;

                        td.posPrev    = td.position;
                        td.position   = Input.mousePosition;
                        td.delta      = td.position - td.posPrev;
                        td.deltaSwipe = td.position - td.posOrigin;
                        td.distSwipe  = td.deltaSwipe.magnitude;
                        td.distTouch += td.delta.magnitude;

                        td.monoPrev = td.monoCurrent;

                        MonoBehaviour monoHit;
                        Vector3       posHit;
                        if (GetObjsHit(Input.mousePosition, out posHit, out monoHit))
                        {
                            td.monoCurrent = monoHit;
                            td.posHit      = posHit;
                        }
                        else
                        {
                            td.monoCurrent = null;
                        }

                        if (td.monoCurrent != null)
                        {
                            td.timeOnObject += tDelta;
                        }

                        if (td.monoCurrent != td.monoPrev)
                        {
                            td.timeOnObject = 0f;
                            td.hasSentHold  = false;
                        }
                    }
                }
            }
            else if (Input.GetMouseButtonUp(idButtonMouse))
            {
                if (!touchData.ContainsKey(idButtonMouse))
                {
                    //DevCon.Say("ERROR - not contained canceled, ended");
                }
                else
                {
                    TouchData td = touchData[idButtonMouse];
                    td.phasePointer = PhasePointer.ENDED;
                    td.removeAtEnd  = true;
                }
            }
#endif
        }
示例#3
0
 // module can decide
 public abstract bool IsTouchRelevant(TouchData td);
        public override bool SearchEvent(List <TouchData> touchDatas, float tDelta)
        {
#if UNITY_ANDROID
            Vector3 vecMove       = Vector3.zero;
            Vector2 posTouchLocal = Vector2.zero;

            // needs to be calculated in first frame, cant be constructor
            if (sizeRectBack == 0)
            {
                sizeRectBack = rectTransBack.rect.width;
            }
            if (sizeRectFront == 0)
            {
                sizeRectFront = rectTransFront.rect.width;
            }
            if (sizeRectBack != 0 && sizeRectFront != 0)
            {
                deviationStickMax  = (sizeRectBack - sizeRectFront) / 2;
                radiusBack         = (sizeRectBack / 2);
                deviationStickMax  = radiusBack - (sizeRectFront / 2);
                radiusCenter       = radiusBack * RATIO_CENTER;
                radiusCenterHalf   = radiusCenter / 2;
                distTouchAcceptMax = radiusBack * RATIO_TOUCH_ACCEPT;
            }

            timeSinceTap += tDelta;
            if (eventModuleTap.SearchEvent(touchDatas, tDelta))
            {
                timeSinceTap = 0f;
            }

            // assumed that all tds are relevant and legal
            foreach (TouchData td in touchDatas)
            {
                Vector2 posTouch = imgBack.transform.InverseTransformPoint(td.position);
                if (posTouch.magnitude > deviationStickMax)
                {
                    posTouchLocal = (posTouch - rectTransBack.rect.center).normalized * deviationStickMax;
                }
                else
                {
                    posTouchLocal = posTouch;
                }

                Vector2 vecTouch  = posTouchLocal - rectTransBack.rect.center;
                float   distTouch = vecTouch.magnitude;

                //if(distTouch < distTouchAcceptMax)
                //{
                int dirTouch = Vec2Dir(vecTouch);

                if (distTouch < radiusCenter)
                {
                    if (timeSinceTap < TIME_TAP_BACK_MAX)
                    {
                        startedBack = true;
                    }

                    // TODO remove backmove because bad paradigm
                    vecMove.z++;
                    //if(startedBack) vecMove.z--;
                    //else vecMove.z++;
                }
                else
                {
                    if (dirTouch == 0)
                    {
                        vecMove.x++;
                    }
                    if (dirTouch == 1)
                    {
                        vecMove.x--;
                    }
                    if (dirTouch == 2)
                    {
                        vecMove.y++;
                    }
                    if (dirTouch == 3)
                    {
                        vecMove.y--;
                    }
                    startedBack = false;
                }
                // }
            }

            Vector2 posStickLocal = imgBack.transform.InverseTransformPoint(imgFront.transform.position);

            bool keptStatic = posTouchLocal.magnitude < radiusCenter;

            Vector2 vecToTarget       = ((keptStatic)? Vector2.zero : posTouchLocal) - posStickLocal;
            Vector2 vecToTargetScaled = vecToTarget * RATIO_SPEED_STICK;
            rectTransFront.anchoredPosition += vecToTargetScaled;

            if (vecMove != Vector3.zero)
            {
                vecMove.Normalize();
                (reciever as IEventHandlerVirtualJoystick).OnMoveSixAxis(vecMove);
                return(true);
            }
            else
            {
                startedBack = false;
                tdLegal     = null;
            }
#endif

#if UNITY_EDITOR
            if (Input.GetMouseButton(0))
            {
                Vector2 posTouch = imgBack.transform.InverseTransformPoint(Input.mousePosition);
                if (posTouch.magnitude < deviationStickMax * 3)
                {
                    (reciever as IEventHandlerVirtualJoystick).OnMoveSixAxis(new Vector3(0, 0, 1));
                }
            }
#endif

            return(false);
        }
 public override bool IsTouchFallingThrough(TouchData td, bool isRelevant)
 {
     return(!isRelevant);
 }
 public override bool IsTouchRelevant(TouchData td)
 {
     // better radius max for quit
     // started over image
     return(UtilityInput.IsPointOverAlpha(imgBack, td.posOrigin));
 }
 public override bool IsTouchRelevant(TouchData td)
 {
     return(UtilityInput.IsPointOverAlphas(buttons, td.position));
 }
 public override bool IsTouchRelevant(TouchData td)
 {
     // every touch is relevant, no need to narrow down
     return(true);
 }