Ejemplo n.º 1
0
        protected override void OnStartRunning()
        {
            base.OnStartRunning();
            if (initialized)
            {
                return;
            }

            // must init after window
            InputHTMLNativeCalls.JSInitInput();
            firstTouch  = -1;
            initialized = true;
        }
Ejemplo n.º 2
0
        protected override void OnUpdate()
        {
            // handle lost/reset
            if (InputHTMLNativeCalls.JSGetCanvasLost())
            {
                m_inputState.Clear();
                InputHTMLNativeCalls.JSResetStreams();
                InputHTMLNativeCalls.JSInitInput();
                firstTouch = -1;
                return;
            }

            if (InputHTMLNativeCalls.JSGetFocusLost())
            {
                m_inputState.Clear();
                InputHTMLNativeCalls.JSResetStreams();
                firstTouch = -1;
                return;
            }

            // advance input state
            base.OnUpdate();

            // keys
            int keyStreamLen;

            unsafe
            {
                fixed(int *ptr = streamBuf)
                {
                    keyStreamLen = InputHTMLNativeCalls.JSGetKeyStream(streamBuf.Length, ptr);
                }
            }

            for (int i = 0; i < keyStreamLen; i += 2)
            {
                int     action        = streamBuf[i];
                int     key           = streamBuf[i + 1];
                KeyCode translatedKey = TranslateKey(key);
                if (translatedKey == KeyCode.None)
                {
                    continue;
                }
                if (action == 0)
                {
                    m_inputState.KeyUp(translatedKey);
                }
                else if (action == 1)
                {
                    m_inputState.KeyDown(translatedKey);
                }
            }

            // mouse (move + up/down)
            int mouseStreamLen;

            unsafe
            {
                fixed(int *ptr = streamBuf)
                {
                    mouseStreamLen = InputHTMLNativeCalls.JSGetMouseStream(streamBuf.Length, ptr);
                }
            }

            for (int i = 0; i < mouseStreamLen; i += 4)
            {
                int ev     = streamBuf[i];
                int button = streamBuf[i + 1];
                int x      = streamBuf[i + 2];
                int y      = streamBuf[i + 3];
                if (ev == 0)
                {
                    m_inputState.MouseUp(button);
                }
                else if (ev == 1)
                {
                    m_inputState.MouseDown(button);
                }
                m_inputState.mouseX = x;
                m_inputState.mouseY = y;
            }

            if (mouseStreamLen != 0)
            {
                m_inputState.hasMouse = true;
            }

            // touches
            int touchStreamLen;

            unsafe
            {
                fixed(int *ptr = streamBuf)
                {
                    touchStreamLen = InputHTMLNativeCalls.JSGetTouchStream(streamBuf.Length, ptr);
                }
            }
            for (int i = 0; i < touchStreamLen; i += 4)
            {
                int        ev = streamBuf[i];
                int        id = streamBuf[i + 1];
                int        x  = streamBuf[i + 2];
                int        y  = streamBuf[i + 3];
                TouchState phase;
                switch (ev)
                {
                case 0:
                    phase = TouchState.Ended;
                    break;

                case 1:
                    phase = TouchState.Began;
                    break;

                case 2:
                    phase = TouchState.Moved;
                    break;

                case 3:
                    phase = TouchState.Canceled;
                    break;

                default:
                    continue;
                }
                m_inputState.TouchEvent(id, phase, x, y);

                // simulate mouse from touch as well
                if (!m_inputState.hasMouse)
                {
                    if (ev == 0 || ev == 3)
                    {
                        if (firstTouch == id)
                        {
                            m_inputState.MouseUp(0);
                            firstTouch = -1;
                        }
                    }
                    else if (ev == 1)
                    {
                        if (firstTouch == -1)
                        {
                            firstTouch = id;
                            m_inputState.MouseDown(0);
                        }
                    }

                    if (firstTouch == id)
                    {
                        m_inputState.mouseX = x;
                        m_inputState.mouseY = y;
                    }
                }
            }
            InputHTMLNativeCalls.JSResetStreams();
        }