Example #1
0
 private void ProcessGestures(List <GestureSample> gestures)
 {
     if (OnGesture != null)
     {
         CCRect viewPort = CCDrawManager.ViewPortRect;
         foreach (GestureSample g in gestures)
         {
             CCPoint pos  = CCPoint.Zero;
             CCPoint pos2 = CCPoint.Zero;
             if (viewPort.ContainsPoint(g.Position.X, g.Position.Y))
             {
                 pos = CCDrawManager.ScreenToWorld(g.Position.X, g.Position.Y);
             }
             if (viewPort.ContainsPoint(g.Position2.X, g.Position2.Y))
             {
                 pos2 = CCDrawManager.ScreenToWorld(g.Position2.X, g.Position2.Y);
             }
             CCPoint   delta  = new CCPoint(g.Delta.X, g.Delta.Y);
             CCPoint   delta2 = new CCPoint(g.Delta2.X, g.Delta2.Y);
             CCGesture cg     = new CCGesture(g.GestureType, g.Timestamp, pos, pos2, delta, delta2);
             OnGesture(cg);
         }
     }
 }
Example #2
0
        private void ProcessTouch()
        {
            if (m_pDelegate != null)
            {
                newTouches.Clear();
                movedTouches.Clear();
                endedTouches.Clear();

                CCRect  viewPort = CCDrawManager.ViewPortRect;
                CCPoint pos;

                // TODO: allow configuration to treat the game pad as a touch device.

#if WINDOWS || WINDOWSGL || MACOS
                _prevMouseState = _lastMouseState;
                _lastMouseState = Mouse.GetState();

                if (_prevMouseState.LeftButton == ButtonState.Released && _lastMouseState.LeftButton == ButtonState.Pressed)
                {
#if NETFX_CORE
                    pos = TransformPoint(_lastMouseState.X, _lastMouseState.Y);
                    pos = CCDrawManager.ScreenToWorld(pos.X, pos.Y);
#else
                    pos = CCDrawManager.ScreenToWorld(_lastMouseState.X, _lastMouseState.Y);
#endif
                    _lastMouseId++;
                    m_pTouches.AddLast(new CCTouch(_lastMouseId, pos.X, pos.Y));
                    m_pTouchMap.Add(_lastMouseId, m_pTouches.Last);
                    newTouches.Add(m_pTouches.Last.Value);

                    m_bCaptured = true;
                }
                else if (_prevMouseState.LeftButton == ButtonState.Pressed && _lastMouseState.LeftButton == ButtonState.Pressed)
                {
                    if (m_pTouchMap.ContainsKey(_lastMouseId))
                    {
                        if (_prevMouseState.X != _lastMouseState.X || _prevMouseState.Y != _lastMouseState.Y)
                        {
#if NETFX_CORE
                            pos = TransformPoint(_lastMouseState.X, _lastMouseState.Y);
                            pos = CCDrawManager.ScreenToWorld(pos.X, pos.Y);
#else
                            pos = CCDrawManager.ScreenToWorld(_lastMouseState.X, _lastMouseState.Y);
#endif
                            movedTouches.Add(m_pTouchMap[_lastMouseId].Value);
                            m_pTouchMap[_lastMouseId].Value.SetTouchInfo(_lastMouseId, pos.X, pos.Y);
                        }
                    }
                }
                else if (_prevMouseState.LeftButton == ButtonState.Pressed && _lastMouseState.LeftButton == ButtonState.Released)
                {
                    if (m_pTouchMap.ContainsKey(_lastMouseId))
                    {
                        endedTouches.Add(m_pTouchMap[_lastMouseId].Value);
                        m_pTouches.Remove(m_pTouchMap[_lastMouseId]);
                        m_pTouchMap.Remove(_lastMouseId);
                    }
                }
#endif

                TouchCollection touchCollection = TouchPanel.GetState();

                foreach (TouchLocation touch in touchCollection)
                {
                    switch (touch.State)
                    {
                    case TouchLocationState.Pressed:
                        if (m_pTouchMap.ContainsKey(touch.Id))
                        {
                            break;
                        }

                        if (viewPort.ContainsPoint(touch.Position.X, touch.Position.Y))
                        {
                            pos = CCDrawManager.ScreenToWorld(touch.Position.X, touch.Position.Y);

                            m_pTouches.AddLast(new CCTouch(touch.Id, pos.X, pos.Y));
                            m_pTouchMap.Add(touch.Id, m_pTouches.Last);
                            newTouches.Add(m_pTouches.Last.Value);
                        }
                        break;

                    case TouchLocationState.Moved:
                        LinkedListNode <CCTouch> existingTouch;
                        if (m_pTouchMap.TryGetValue(touch.Id, out existingTouch))
                        {
                            pos = CCDrawManager.ScreenToWorld(touch.Position.X, touch.Position.Y);
                            var delta = existingTouch.Value.LocationInView - pos;
                            if (delta.LengthSQ > 1.0f)
                            {
                                movedTouches.Add(existingTouch.Value);
                                existingTouch.Value.SetTouchInfo(touch.Id, pos.X, pos.Y);
                            }
                        }
                        break;

                    case TouchLocationState.Released:
                        if (m_pTouchMap.TryGetValue(touch.Id, out existingTouch))
                        {
                            endedTouches.Add(existingTouch.Value);
                            m_pTouches.Remove(existingTouch);
                            m_pTouchMap.Remove(touch.Id);
                        }
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }

                if (newTouches.Count > 0)
                {
                    m_pDelegate.TouchesBegan(newTouches);
                }

                if (movedTouches.Count > 0)
                {
                    m_pDelegate.TouchesMoved(movedTouches);
                }

                if (endedTouches.Count > 0)
                {
                    m_pDelegate.TouchesEnded(endedTouches);
                }
            }
        }
Example #3
0
        private void ProcessTouch(TouchCollection touchCollection)
        {
            if (m_pDelegate != null)
            {
                newTouches.Clear();
                movedTouches.Clear();
                endedTouches.Clear();

                CCRect  viewPort = CCDrawManager.ViewPortRect;
                CCPoint pos;

                foreach (TouchLocation touch in touchCollection)
                {
                    switch (touch.State)
                    {
                    case TouchLocationState.Pressed:
                        if (m_pTouchMap.ContainsKey(touch.Id))
                        {
                            break;
                        }

                        if (viewPort.ContainsPoint(touch.Position.X, touch.Position.Y))
                        {
                            pos = CCDrawManager.ScreenToWorld(touch.Position.X, touch.Position.Y);

                            m_pTouches.AddLast(new CCTouch(touch.Id, pos.X, pos.Y));
                            m_pTouchMap.Add(touch.Id, m_pTouches.Last);
                            newTouches.Add(m_pTouches.Last.Value);
                        }
                        break;

                    case TouchLocationState.Moved:
                        LinkedListNode <CCTouch> existingTouch;
                        if (m_pTouchMap.TryGetValue(touch.Id, out existingTouch))
                        {
                            pos = CCDrawManager.ScreenToWorld(touch.Position.X, touch.Position.Y);
                            var delta = existingTouch.Value.LocationInView - pos;
                            if (delta.LengthSQ > 1.0f)
                            {
                                movedTouches.Add(existingTouch.Value);
                                existingTouch.Value.SetTouchInfo(touch.Id, pos.X, pos.Y);
                            }
                        }
                        break;

                    case TouchLocationState.Released:
                        if (m_pTouchMap.TryGetValue(touch.Id, out existingTouch))
                        {
                            endedTouches.Add(existingTouch.Value);
                            m_pTouches.Remove(existingTouch);
                            m_pTouchMap.Remove(touch.Id);
                        }
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }

                if (newTouches.Count > 0)
                {
                    m_pDelegate.TouchesBegan(newTouches);
                }

                if (movedTouches.Count > 0)
                {
                    m_pDelegate.TouchesMoved(movedTouches);
                }

                if (endedTouches.Count > 0)
                {
                    m_pDelegate.TouchesEnded(endedTouches);
                }
            }
        }
Example #4
0
        private void ProcessMouse(MouseState mouse)
        {
            if (m_pDelegate != null)
            {
                newTouches.Clear();
                movedTouches.Clear();
                endedTouches.Clear();

                CCRect  viewPort = CCDrawManager.ViewPortRect;
                CCPoint pos;

                _prevMouseState = _lastMouseState;
                _lastMouseState = mouse;

                if (_prevMouseState.LeftButton == ButtonState.Released && _lastMouseState.LeftButton == ButtonState.Pressed)
                {
#if NETFX_CORE
                    pos = TransformPoint(_lastMouseState.X, _lastMouseState.Y);
                    pos = CCDrawManager.ScreenToWorld(pos.X, pos.Y);
#else
                    pos = CCDrawManager.ScreenToWorld(_lastMouseState.X, _lastMouseState.Y);
#endif
                    _lastMouseId++;
                    m_pTouches.AddLast(new CCTouch(_lastMouseId, pos.X, pos.Y));
                    m_pTouchMap.Add(_lastMouseId, m_pTouches.Last);
                    newTouches.Add(m_pTouches.Last.Value);

                    m_bCaptured = true;
                }
                else if (_prevMouseState.LeftButton == ButtonState.Pressed && _lastMouseState.LeftButton == ButtonState.Pressed)
                {
                    if (m_pTouchMap.ContainsKey(_lastMouseId))
                    {
                        if (_prevMouseState.X != _lastMouseState.X || _prevMouseState.Y != _lastMouseState.Y)
                        {
#if NETFX_CORE
                            pos = TransformPoint(_lastMouseState.X, _lastMouseState.Y);
                            pos = CCDrawManager.ScreenToWorld(pos.X, pos.Y);
#else
                            pos = CCDrawManager.ScreenToWorld(_lastMouseState.X, _lastMouseState.Y);
#endif
                            movedTouches.Add(m_pTouchMap[_lastMouseId].Value);
                            m_pTouchMap[_lastMouseId].Value.SetTouchInfo(_lastMouseId, pos.X, pos.Y);
                        }
                    }
                }
                else if (_prevMouseState.LeftButton == ButtonState.Pressed && _lastMouseState.LeftButton == ButtonState.Released)
                {
                    if (m_pTouchMap.ContainsKey(_lastMouseId))
                    {
                        endedTouches.Add(m_pTouchMap[_lastMouseId].Value);
                        m_pTouches.Remove(m_pTouchMap[_lastMouseId]);
                        m_pTouchMap.Remove(_lastMouseId);
                    }
                }
                if (newTouches.Count > 0)
                {
                    m_pDelegate.TouchesBegan(newTouches);
                }

                if (movedTouches.Count > 0)
                {
                    m_pDelegate.TouchesMoved(movedTouches);
                }

                if (endedTouches.Count > 0)
                {
                    m_pDelegate.TouchesEnded(endedTouches);
                }
            }
        }