Esempio n. 1
0
        public static void Update()
        {
            touchCollection = TouchPanel.GetState();

            // 古いタッチ情報をtouchesから削除
            if (touches.Count > 0)
            {
                foreach (int id in touches.Keys)
                {
                    if (!touchCollection.FindById(id, out dummyLocation))
                    {
                        if (touches[id].State == TouchState.End)
                        {
                            TouchInfo dummyInfo;
                            touches.TryRemove(id, out dummyInfo);
                        }
                        else
                        {
                            touches[id].State = TouchState.End;
                        }
                    }
                }
            }

            // 新しいタッチ情報をtouchesに追加、継続しているタッチ情報を更新
            foreach (TouchLocation location in touchCollection)
            {
                if (touches.ContainsKey(location.Id))
                {
                    touches[location.Id].Update(location);
                }
                else
                {
                    touches.TryAdd(
                        location.Id,
                        new TouchInfo(location)
                        );
                }
            }
            foreach (int id in touches.Keys)
            {
                if (!touchCollection.FindById(id, out dummyLocation))
                {
                    if (touches[id].State == TouchState.End)
                    {
                        TouchInfo dummyInfo;
                        touches.TryRemove(id, out dummyInfo);
                    }
                    else
                    {
                        touches[id].State = TouchState.End;
                    }
                }
                Console.WriteLine(touches[id].Positions[0].ToString());
            }
        }
Esempio n. 2
0
        private void FillTouchCollection(NSSet touches)
        {
            UITouch [] touchesArray = touches.ToArray <UITouch>();

            for (int i = 0; i < touchesArray.Length; i++)
            {
                //Get IOS touch
                UITouch touch = touchesArray[i];

                //Get position touch
                Vector2 position           = new Vector2(touch.LocationInView(touch.View));
                Vector2 translatedPosition = GetOffsetPosition(position);

                TouchLocation   tlocation;
                TouchCollection collection = TouchPanel.Collection;
                int             index;
                switch (touch.Phase)
                {
                case UITouchPhase.Stationary:
                case UITouchPhase.Moved:
                    index = collection.FindById(touch.Handle.ToInt32(), out tlocation);
                    if (index >= 0)
                    {
                        tlocation.State    = TouchLocationState.Moved;
                        tlocation.Position = translatedPosition;
                        collection[index]  = tlocation;
                    }
                    break;

                case UITouchPhase.Began:
                    tlocation = new TouchLocation(touch.Handle.ToInt32(), TouchLocationState.Pressed, translatedPosition);
                    collection.Add(tlocation);
                    break;

                case UITouchPhase.Ended:
                    index = collection.FindById(touch.Handle.ToInt32(), out tlocation);
                    if (index >= 0)
                    {
                        tlocation.State   = TouchLocationState.Released;
                        collection[index] = tlocation;
                    }
                    break;

                default:
                    break;
                }
            }
        }
Esempio n. 3
0
        void AnalyzeTouch(float time)
        {
            TouchCollection touch = TouchPanel.GetState();

            for (int idx = 0; idx < touch.Count; ++idx)
            {
                var tp = touch[idx];
                AnalyzeTouchPoint(ref tp, time);
            }

            var keys = _elements.Keys;

            for (int idx = 0; idx < keys.Count;)
            {
                int id = keys.ElementAt(idx);

                TouchLocation tl;

                if (id != MouseId && !touch.FindById(id, out tl))
                {
                    ProcessUp(id, _elements[id].Position, DateTime.Now);
                    CancelTouch(id);
                }
                else
                {
                    ++idx;
                }
            }
        }
Esempio n. 4
0
        private Vector2?TouchPosition(TouchCollection touchLocationState)
        {
            TouchLocation touchLocation;

            if (touchLocationState.FindById(TouchID, out touchLocation))
            {
                return(touchLocation.Position);
            }

            return(null);
        }
Esempio n. 5
0
        /// <summary>
        /// Gets the touch Position of this touch.
        /// </summary>
        /// <param name="touches">The touch collection.</param>
        /// <returns>The vector position of this touch or NULL.</returns>
        private Vector2?touchPosition(TouchCollection touches)
        {
            TouchLocation touchLocation;

            if (touches.FindById(touchId, out touchLocation))
            {
                return(touchLocation.Position);
            }

            return(null);
        }
        public MouseFrameState CalculateFrameState(TouchCollection currentTouches)
        {
            var result = new SingleTouchFrameState();

            if (this.currentMainTouchId.HasValue)
            {
                currentTouches.FindById(this.currentMainTouchId.Value, out var currTouch);
                if (this.prevTouches.FindById(currTouch.Id, out var prevTouch))
                {
                    if (!this.touchCommitted)
                    {
                        result = new SingleTouchFrameState(true, false, currTouch.Position.ToPoint(),
                                                           currTouch.Position - prevTouch.Position);
                        this.touchCommitted = true;
                    }
                    else
                    {
                        result = new SingleTouchFrameState(false, false, currTouch.Position.ToPoint(),
                                                           currTouch.Position - prevTouch.Position);
                    }
                }
                else
                {
                    result = new SingleTouchFrameState(false, true, currTouch.Position.ToPoint(),
                                                       currTouch.Position - prevTouch.Position);
                    this.currentMainTouchId = null;
                    this.touchCommitted     = false;
                }
            }
            else
            {
                var prevToCurrent = new Dictionary <TouchLocation, TouchLocation>();
                foreach (var currentTouch in currentTouches)
                {
                    if (this.prevTouches.FindById(currentTouch.Id, out var prevTouch))
                    {
                        prevToCurrent.Add(prevTouch, currentTouch);
                    }
                    else
                    {
                        // Pick a current touch that hasn't been seen before, they are now our main touch
                        this.currentMainTouchId = currentTouch.Id;
                        this.touchCommitted     = false;
                        result = new SingleTouchFrameState(false, false, currentTouch.Position.ToPoint(), Vector2.Zero);
                        break;
                    }
                }
            }

            this.prevTouches = currentTouches;

            return(result);
        }
Esempio n. 7
0
        public void WorksWhenConstructedEmpty()
        {
            TouchCollection collection = new TouchCollection();

            Assert.AreEqual(0, collection.Count);
            foreach (var touch in collection)
                Assert.Fail("Shouldn't have any touches in an empty collection");

            Assert.AreEqual(-1, collection.IndexOf(new TouchLocation()));

            TouchLocation touchLocation;
            Assert.False(collection.FindById(1, out touchLocation));
        }
Esempio n. 8
0
        public void WorksWhenConstructedEmpty()
        {
            TouchCollection collection = new TouchCollection();

            Assert.AreEqual(0, collection.Count);
            foreach (var touch in collection)
            {
                Assert.Fail("Shouldn't have any touches in an empty collection");
            }

            Assert.AreEqual(-1, collection.IndexOf(new TouchLocation()));

            TouchLocation touchLocation;

            Assert.False(collection.FindById(1, out touchLocation));
        }
        public void ReadInput()
        {
            var oldCollection = _touches;
            _touches = TouchPanel.GetState();

            _releasedTouches.Clear();
            _handledInputs.Clear();
            TouchLocation ignore;
            foreach (var oldTouch in oldCollection)
            {
                if (!_touches.FindById(oldTouch.Id, out ignore))
                {
                    _releasedTouches[oldTouch.Id] = new TouchLocation(oldTouch.Id, TouchLocationState.Released,
                                                           oldTouch.Position, oldTouch.State, oldTouch.Position);
                }
            }
        }
Esempio n. 10
0
        //private Vector2 GetOffsetPosition(Vector2 position)
        //{
        //    Vector2 translatedPosition = position;
        //    switch (CurrentOrientation)
        //    {
        //        case DisplayOrientation.Portrait:
        //            break;
        //        case DisplayOrientation.LandscapeRight:
        //            translatedPosition = new Vector2(ClientBounds.Height - position.Y, position.X);
        //            break;
        //        case DisplayOrientation.LandscapeLeft:
        //            translatedPosition = new Vector2(position.Y, ClientBounds.Width - position.X);
        //            break;
        //        case DisplayOrientation.PortraitUpsideDown:
        //            translatedPosition = new Vector2(ClientBounds.Width - position.X, ClientBounds.Height - position.Y);
        //            break;
        //    }
        //    return translatedPosition * UIScreen.MainScreen.Scale;
        //}

        public bool OnTouch(View v, MotionEvent e)
        {
            TouchLocation   tlocation;
            TouchCollection collection = TouchPanel.Collection;

            Vector2 position = Vector2.Zero;

            position.X = e.GetX(e.ActionIndex);
            position.Y = e.GetY(e.ActionIndex);
            int id = e.GetPointerId(e.ActionIndex);
            int index;

            switch (e.ActionMasked)
            {
            // DOWN
            case 0:
            case 5:
                tlocation = new TouchLocation(id, TouchLocationState.Pressed, position);
                collection.Add(tlocation);
                break;

            // UP
            case 1:
            case 6:
                index = collection.FindById(e.GetPointerId(e.ActionIndex), out tlocation);
                if (index >= 0)
                {
                    tlocation.State   = TouchLocationState.Released;
                    collection[index] = tlocation;
                }
                break;

            // MOVE
            case 2:
                for (int i = 0; i < e.PointerCount; i++)
                {
                    id         = e.GetPointerId(i);
                    position.X = e.GetX(i);
                    position.Y = e.GetY(i);
                    index      = collection.FindById(id, out tlocation);
                    if (index >= 0)
                    {
                        tlocation.State    = TouchLocationState.Moved;
                        tlocation.Position = position;
                        collection[index]  = tlocation;
                    }
                }
                break;

            // CANCEL, OUTSIDE
            case 3:
            case 4:
                index = collection.FindById(id, out tlocation);
                if (index >= 0)
                {
                    tlocation.State   = TouchLocationState.Invalid;
                    collection[index] = tlocation;
                }
                break;
            }

            return(true);

            //for (int i = 0; i < e.PointerCount; i++)
            //{
            //    Vector2 position = Vector2.Zero;
            //    position.X = e.GetX(i);
            //    position.Y = e.GetY(i);

            //    TouchLocation tlocation;
            //    TouchCollection collection = TouchPanel.Collection;

            //    int index;
            //    int id = e.GetPointerId(i);

            //    switch (e.ActionMasked)
            //    {
            //        //case MotionEventActions.Down:
            //        case 0:
            //            //Log.Debug("TESTING", string.Format("DOWN: ActionIndex({5}) ActionMasked({6}) BlobCount=[{3}] BlobID=[{2}] BlobPos=({0:N},{1:N})", position.X, position.Y, id, e.PointerCount, e.Action, e.ActionIndex, e.ActionMasked));
            //            tlocation = new TouchLocation(id, TouchLocationState.Pressed, position);
            //            collection.Add(tlocation);
            //            break;
            //        //case MotionEventActions.PointerDown:
            //        case 5:
            //            //Log.Debug("TESTING", string.Format("DOWN: ActionIndex({5}) ActionMasked({6}) BlobCount=[{3}] BlobID=[{2}] BlobPos=({0:N},{1:N})", position.X, position.Y, id, e.PointerCount, e.Action, e.ActionIndex, e.ActionMasked));
            //            tlocation = new TouchLocation(e.GetPointerId(e.ActionIndex), TouchLocationState.Pressed, position);
            //            collection.Add(tlocation);
            //            break;
            //        //case MotionEventActions.Up:
            //        case 1:
            //            index = collection.FindById(id, out tlocation);
            //            if (index >= 0)
            //            {
            //                tlocation.State = TouchLocationState.Released;
            //                collection[index] = tlocation;
            //            }
            //            break;
            //        //case MotionEventActions.PointerUp:
            //        case 6:
            //            //Log.Debug("TESTING", string.Format("UP: ActionIndex({5}) ActionMasked({6}) BlobCount=[{3}] BlobID=[{2}] BlobPos=({0:N},{1:N})", position.X, position.Y, id, e.PointerCount, e.Action, e.ActionIndex, e.ActionMasked));
            //            index = collection.FindById(e.GetPointerId(e.ActionIndex), out tlocation);
            //            if (index >= 0)
            //            {
            //                tlocation.State = TouchLocationState.Released;
            //                collection[index] = tlocation;
            //            }
            //            break;
            //        //case MotionEventActions.Move:
            //        case 2:
            //            //Log.Debug("TESTING", string.Format("MOVE: Count=[{3}] ID=[{2}] ({0:N},{1:N})", position.X, position.Y, id, e.PointerCount));
            //            index = collection.FindById(id, out tlocation);
            //            if (index >= 0)
            //            {
            //                tlocation.State = TouchLocationState.Moved;
            //                tlocation.Position = position;
            //                collection[index] = tlocation;
            //            }
            //            break;
            //        default:
            //            //Log.Debug("TESTING", string.Format("OTHER {4}: ActionIndex({5}) ActionMasked({6}) BlobCount=[{3}] BlobID=[{2}] BlobPos=({0:N},{1:N})", position.X, position.Y, id, e.PointerCount, e.Action, e.ActionIndex, e.ActionMasked));
            //            index = collection.FindById(id, out tlocation);
            //            if (index >= 0)
            //            {
            //                tlocation.State = TouchLocationState.Released;
            //                collection[index] = tlocation;
            //            }
            //            break;
            //    }
            //}
            //return true;

            //TouchLocationState state = TouchLocationState.Invalid;

            //if (e.Action == MotionEventActions.Cancel)
            //{
            //    state = TouchLocationState.Invalid;
            //}
            //if (e.Action == MotionEventActions.Up)
            //{
            //    state = TouchLocationState.Released;
            //}
            //if (e.Action == MotionEventActions.Move)
            //{
            //    state = TouchLocationState.Moved;
            //    Mouse.SetPosition((int)e.GetX(), (int)e.GetY());
            //}
            //if (e.Action == MotionEventActions.Down)
            //{
            //    state = TouchLocationState.Pressed;
            //    Mouse.SetPosition((int)e.GetX(), (int)e.GetY());
            //}

            //TouchLocation tprevious;
            //TouchLocation tlocation;
            //Vector2 position = new Vector2(e.GetX(), e.GetY());
            //Vector2 translatedPosition = position;
            //if (state != TouchLocationState.Pressed && _previousTouches.TryGetValue(e.Handle, out tprevious))
            //{
            //    tlocation = new TouchLocation(e.Handle.ToInt32(), state, translatedPosition, e.Pressure, tprevious.State, tprevious.Position, tprevious.Pressure);
            //}
            //else
            //{
            //    tlocation = new TouchLocation(e.Handle.ToInt32(), state, translatedPosition, e.Pressure);
            //}

            //TouchPanel.Collection.Clear();
            //TouchPanel.Collection.Add(tlocation);

            //if (state != TouchLocationState.Released)
            //    _previousTouches[e.Handle] = tlocation;
            //else
            //    _previousTouches.Remove(e.Handle);
            //return true;

            //////////
            //TouchLocationState state = TouchLocationState.Invalid;

            //if (e.Action == MotionEventActions.Cancel)
            //{
            //    state = TouchLocationState.Invalid;
            //}
            //if (e.Action == MotionEventActions.Up)
            //{
            //    state = TouchLocationState.Released;
            //}
            //if (e.Action == MotionEventActions.Move)
            //{
            //    state = TouchLocationState.Moved;
            //    Mouse.SetPosition((int)e.GetX(), (int)e.GetY());
            //}
            //if (e.Action == MotionEventActions.Down)
            //{
            //    state = TouchLocationState.Pressed;
            //    Mouse.SetPosition((int)e.GetX(), (int)e.GetY());
            //}

            //TouchLocation tprevious;
            //TouchLocation tlocation;
            //Vector2 position = new Vector2(e.GetX(), e.GetY());
            //Vector2 translatedPosition = position;

            //switch (CurrentOrientation)
            //{
            //    case DisplayOrientation.Portrait:
            //        break;
            //    case DisplayOrientation.LandscapeRight:
            //        translatedPosition = new Vector2(ClientBounds.Height - position.Y, position.X);
            //        break;
            //    case DisplayOrientation.LandscapeLeft:
            //        translatedPosition = new Vector2(position.Y, ClientBounds.Width - position.X);
            //        break;
            //    case DisplayOrientation.PortraitUpsideDown:
            //        translatedPosition = new Vector2(ClientBounds.Width - position.X, ClientBounds.Height - position.Y);
            //        break;
            //}


            //if (state != TouchLocationState.Pressed && _previousTouches.TryGetValue(e.Handle, out tprevious))
            //{
            //    tlocation = new TouchLocation(e.Handle.ToInt32(), state, translatedPosition, e.Pressure, tprevious.State, tprevious.Position, tprevious.Pressure);
            //}
            //else
            //{
            //    tlocation = new TouchLocation(e.Handle.ToInt32(), state, translatedPosition, e.Pressure);
            //}

            //TouchPanel.Collection.Clear();
            //TouchPanel.Collection.Add(tlocation);

            //if (state != TouchLocationState.Released)
            //    _previousTouches[e.Handle] = tlocation;
            //else
            //    _previousTouches.Remove(e.Handle);

            //GamePad.Instance.Update(e);

            //return true;
        }
Esempio n. 11
0
        private Vector2? TouchPosition(TouchCollection touchLocationState)
        {
            TouchLocation touchLocation;
            if (touchLocationState.FindById(TouchID, out touchLocation))
            {
                return touchLocation.Position;
            }

            return null;
        }
Esempio n. 12
0
        public static void Update()
        {
            _lastMouseState       = _currentMouseState;
            _currentMouseState    = Mouse.GetState();
            _lastKeyboardState    = _currentKeyboardState;
            _currentKeyboardState = Keyboard.GetState();

#if WINDOWS_PHONE
            gestureCount = 0;
            while (TouchPanel.IsGestureAvailable)
            {
                gestures[gestureCount++] = TouchPanel.ReadGesture();
            }

            TouchCollection tc = TouchPanel.GetState();
            _activeTouches = 0;
            mouseDowns[0]  = false;
            mouseUps[0]    = false;
            // Check old touches to see if they are gone
            for (int i = 0; i < _touches.Length; i++)
            {
                TouchLocation tl;
                Touch         t = _touches[i];
                if (tc.FindById(t.fingerId, out tl))
                {
                    Vector2 position = new Vector2(tl.Position.X, Camera.FullScreen.Height - tl.Position.Y);
                    t.deltaPosition = position - t.position;
                    t.deltaTime     = Time.deltaTime;
                    t.position      = position;
                    t.cleanPosition = tl.Position;
                    if ((t.phase = ToPhase(tl.State)) == TouchPhase.Moved && t.deltaPosition == Vector2.zero)
                    {
                        t.phase = TouchPhase.Stationary;
                    }
                    if (t.phase == TouchPhase.Ended)
                    {
                        mouseUps[0] = true;
                    }
                    if (_activeTouches != i)
                    {
                        _touches[i].fingerId = -1;
                    }
                    _touches[_activeTouches++] = t;
                }
                else
                {
                    _touches[i].fingerId = -1;
                }
            }
            // Add new touches
            for (int i = 0; i < tc.Count; i++)
            {
                TouchLocation tl = tc[i];
                if (tl.State == TouchLocationState.Invalid)
                {
                    continue;
                }
                bool existing = false;
                for (int j = 0; j < _activeTouches; j++)
                {
                    if (_touches[j].fingerId == tl.Id)
                    {
                        existing = true;
                        break;
                    }
                }
                if (existing)
                {
                    continue;
                }
                Touch t = new Touch()
                {
                    fingerId = tl.Id, position = new Vector2(tl.Position.X, Camera.FullScreen.Height - tl.Position.Y), cleanPosition = tl.Position, phase = ToPhase(tl.State)
                };
                if (t.phase == TouchPhase.Began)
                {
                    mouseDowns[0] = true;
                }
                _touches[_activeTouches++] = t;
            }
#else
            UpdateMouseStates();
#endif
            UpdateGamepadStates();
        }
Esempio n. 13
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (task.IsCompleted)
            {
                if (cartridge == null)
                {
                    cartridge = task.Result;
                    _gameboy.LoadCartridge(cartridge);
                    thread.Start();
                }
                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                {
                    Close();
                    Exit();
                }

                dpad.Reset();
                start.Reset();
                select.Reset();
                btnB.Reset();
                btnA.Reset();

                TouchCollection tc = TouchPanel.GetState();

                foreach (TouchLocation tl in tc)
                {
                    if (dpad.BeingHandled && tl.Id == dpad.TLIndex)
                    {
                        continue;
                    }

                    int mx = (int)tl.Position.X, my = (int)tl.Position.Y;
                    if (tl.State == TouchLocationState.Pressed)
                    {
                        dpad.SetInput(mx, my, tl.Id);
                    }
                    start.HandleInput(mx, my);
                    select.HandleInput(mx, my);
                    btnB.HandleInput(mx, my);
                    btnA.HandleInput(mx, my);
                }

                if (dpad.BeingHandled)
                {
                    bool stillValid = tc.FindById(dpad.TLIndex, out var tl);
                    if (!stillValid || tl.State == TouchLocationState.Released)
                    {
                        dpad.ResetInput();
                    }
                    else
                    {
                        dpad.HandleInput((int)tl.Position.X, (int)tl.Position.Y);
                    }
                }

                float deadzone = .35f;
                _gameboy.SetInput(Input.Button.Up, dpad.YAxis < -deadzone);
                _gameboy.SetInput(Input.Button.Down, dpad.YAxis > deadzone);
                _gameboy.SetInput(Input.Button.Left, dpad.XAxis < -deadzone);
                _gameboy.SetInput(Input.Button.Right, dpad.XAxis > deadzone);

                _gameboy.SetInput(Input.Button.Start, start.Down);
                _gameboy.SetInput(Input.Button.Select, select.Down);
                _gameboy.SetInput(Input.Button.B, btnB.Down);
                _gameboy.SetInput(Input.Button.A, btnA.Down);

                //_gameboy.ExecuteFrame();

                if (_gameboy.GetFrameBufferCount() > 0)
                {
                    int[] frameBuffer = _gameboy.DequeueFrameBuffer();

                    for (int i = 0; i < frameBuffer.Length; i += 4)
                    {
                        colors[i / 4] = new Color(frameBuffer[i], frameBuffer[i + 1], frameBuffer[i + 2], frameBuffer[i + 3]);
                    }

                    _frame.SetData <Color>(colors);
                }
            }

            base.Update(gameTime);
        }