예제 #1
0
        protected override void OnUpdate()
        {
            if (!initialized)
            {
                return;
            }

            base.OnUpdate(); // advances input state one frame
            unsafe
            {
                // key, scancode, action, mods
                int  keyStreamLen = 0;
                int *keyStream    = GLFWNativeCalls.getKeyStream(ref keyStreamLen);
                for (int i = 0; i < keyStreamLen; i += 4)
                {
                    int     key           = keyStream[i];
                    int     scancode      = keyStream[i + 1];
                    int     action        = keyStream[i + 2];
                    int     mods          = keyStream[i + 3];
                    KeyCode translatedKey = TranslateKey(key, scancode, mods);
                    if (translatedKey == KeyCode.None)
                    {
                        continue;
                    }
                    if (action == GLFW_RELEASE)
                    {
                        m_inputState.KeyUp(translatedKey);
                    }
                    else if (action == GLFW_PRESS)
                    {
                        m_inputState.KeyDown(translatedKey);
                    }
                }

                // button, action, mods
                int  mouseButtonStreamLen = 0;
                int *mouseButtonStream    = GLFWNativeCalls.getMouseButtonStream(ref mouseButtonStreamLen);
                for (int i = 0; i < mouseButtonStreamLen; i += 3)
                {
                    int button = mouseButtonStream[i];
                    int action = mouseButtonStream[i + 1];
                    int mods   = mouseButtonStream[i + 2];
                    if (action == GLFW_RELEASE)
                    {
                        m_inputState.MouseUp(button);
                    }
                    else if (action == GLFW_PRESS)
                    {
                        m_inputState.MouseDown(button);
                    }
                }

                // position
                int  mousePosStreamLen = 0;
                int *mousePosStream    = GLFWNativeCalls.getMousePosStream(ref mousePosStreamLen);
                for (int i = 0; i < mousePosStreamLen; i += 2)
                {
                    m_inputState.mouseX = mousePosStream[i];
                    m_inputState.mouseY = mousePosStream[i + 1];
                }

                if (mouseButtonStreamLen != 0 || mousePosStreamLen != 0)
                {
                    m_inputState.hasMouse = true;
                }
            }

            GLFWNativeCalls.resetStreams();
        }
예제 #2
0
        protected override void OnUpdate()
        {
            if (!initialized)
            {
                return;
            }

            base.OnUpdate(); // advances input state one frame

            GLFWNativeCalls.lockStreams();

            if (GLFWNativeCalls.getWindowLostFocus() != 0)
            {
                ExitMouseMode();
            }

            m_inputState.mouseDeltaX  = 0;
            m_inputState.mouseDeltaY  = 0;
            m_inputState.scrollDeltaX = 0;
            m_inputState.scrollDeltaY = 0;

            unsafe
            {
                // key, scancode, action, mods
                int  keyStreamLen = 0;
                int *keyStream    = GLFWNativeCalls.getKeyStream(ref keyStreamLen);
                for (int i = 0; i < keyStreamLen; i += 4)
                {
                    int key      = keyStream[i];
                    int scancode = keyStream[i + 1];
                    int action   = keyStream[i + 2];
                    int mods     = keyStream[i + 3];
                    if (key == GLFW_KEY_ESCAPE)
                    {
                        ExitMouseMode();
                    }

                    KeyCode translatedKey = TranslateKey(key, scancode, mods);
                    if (translatedKey == KeyCode.None)
                    {
                        continue;
                    }
                    if (action == GLFW_RELEASE)
                    {
                        m_inputState.KeyUp(translatedKey);
                    }
                    else if (action == GLFW_PRESS)
                    {
                        m_inputState.KeyDown(translatedKey);
                    }
                }

                // button, action, mods
                int  mouseButtonStreamLen = 0;
                int *mouseButtonStream    = GLFWNativeCalls.getMouseButtonStream(ref mouseButtonStreamLen);
                for (int i = 0; i < mouseButtonStreamLen; i += 3)
                {
                    int button = mouseButtonStream[i];
                    int action = mouseButtonStream[i + 1];
                    int mods   = mouseButtonStream[i + 2];
                    if (action == GLFW_RELEASE)
                    {
                        m_inputState.MouseUp(button);
                    }
                    else if (action == GLFW_PRESS)
                    {
                        m_inputState.MouseDown(button);
                        EnterMouseMode();
                    }
                }

                int    mouseScrollStreamLen = 0;
                float *mouseScrollStream    = GLFWNativeCalls.getMouseScrollStream(ref mouseScrollStreamLen);
                for (int i = 0; i < mouseScrollStreamLen; i += 2)
                {
                    float scrollX = mouseScrollStream[i];
                    float scrollY = mouseScrollStream[i + 1];
                    m_inputState.scrollDeltaX = scrollX;
                    m_inputState.scrollDeltaY = scrollY;
                }

                // position
                // Ensure mouse movement isn't detected if cursor is meant to be locked
                // but isn't (lost focus and never clicked back in the window).
                int  mousePosStreamLen = 0;
                int *mousePosStream    = GLFWNativeCalls.getMousePosStream(ref mousePosStreamLen);
                if (mouseCursorModeSaved == mouseCursorModeActive)
                {
                    for (int i = 0; i < mousePosStreamLen; i += 2)
                    {
                        int nextX = mousePosStream[i];
                        int nextY = mousePosStream[i + 1];

                        if (!mouseInitDelta)
                        {
                            m_inputState.mouseDeltaX += nextX - m_inputState.mouseX;
                            m_inputState.mouseDeltaY += nextY - m_inputState.mouseY;
                        }

                        m_inputState.mouseX = nextX;
                        m_inputState.mouseY = nextY;
                        mouseInitDelta      = false;
                    }
                }

                if (mouseButtonStreamLen != 0 || mousePosStreamLen != 0 || mouseScrollStreamLen != 0)
                {
                    m_inputState.hasMouse = true;
                }
            }

            GLFWNativeCalls.unlockAndResetStreams();
        }