Example #1
0
        public void PumpInput(IInputHandler inputHandler)
        {
            Game.HasInputFocus = 0 != (Sdl.SDL_GetAppState() & Sdl.SDL_APPINPUTFOCUS);

            var mods = MakeModifiers(Sdl.SDL_GetModState());

            inputHandler.ModifierKeys(mods);
            MouseInput?pendingMotion = null;

            Sdl.SDL_Event e;
            while (Sdl.SDL_PollEvent(out e) != 0)
            {
                switch (e.type)
                {
                case Sdl.SDL_QUIT:
                    OpenRA.Game.Exit();
                    break;

                case Sdl.SDL_MOUSEBUTTONDOWN:
                {
                    if (pendingMotion != null)
                    {
                        inputHandler.OnMouseInput(pendingMotion.Value);
                        pendingMotion = null;
                    }

                    var button = MakeButton(e.button.button);
                    lastButtonBits |= button;

                    var pos = new int2(e.button.x, e.button.y);

                    inputHandler.OnMouseInput(new MouseInput(
                                                  MouseInputEvent.Down, button, pos, mods,
                                                  MultiTapDetection.DetectFromMouse(e.button.button, pos)
                                                  ));
                } break;

                case Sdl.SDL_MOUSEBUTTONUP:
                {
                    if (pendingMotion != null)
                    {
                        inputHandler.OnMouseInput(pendingMotion.Value);
                        pendingMotion = null;
                    }

                    var button = MakeButton(e.button.button);
                    lastButtonBits &= ~button;

                    var pos = new int2(e.button.x, e.button.y);
                    inputHandler.OnMouseInput(new MouseInput(
                                                  MouseInputEvent.Up, button, pos, mods,
                                                  MultiTapDetection.InfoFromMouse(e.button.button)
                                                  ));
                } break;

                case Sdl.SDL_MOUSEMOTION:
                {
                    pendingMotion = new MouseInput(
                        MouseInputEvent.Move,
                        lastButtonBits,
                        new int2(e.motion.x, e.motion.y),
                        mods, 0);
                } break;

                case Sdl.SDL_KEYDOWN:
                {
                    var keyName = Sdl.SDL_GetKeyName(e.key.keysym.sym);

                    var keyEvent = new KeyInput
                    {
                        Event         = KeyInputEvent.Down,
                        Modifiers     = mods,
                        UnicodeChar   = (char)e.key.keysym.unicode,
                        KeyName       = Sdl.SDL_GetKeyName(e.key.keysym.sym),
                        VirtKey       = e.key.keysym.sym,
                        MultiTapCount = MultiTapDetection.DetectFromKeyboard(keyName)
                    };

                    if (!HandleSpecialKey(keyEvent))
                    {
                        inputHandler.OnKeyInput(keyEvent);
                    }
                } break;

                case Sdl.SDL_KEYUP:
                {
                    var keyName = Sdl.SDL_GetKeyName(e.key.keysym.sym);

                    var keyEvent = new KeyInput
                    {
                        Event         = KeyInputEvent.Up,
                        Modifiers     = mods,
                        UnicodeChar   = (char)e.key.keysym.unicode,
                        KeyName       = Sdl.SDL_GetKeyName(e.key.keysym.sym),
                        VirtKey       = e.key.keysym.sym,
                        MultiTapCount = MultiTapDetection.InfoFromKeyboard(keyName)
                    };

                    inputHandler.OnKeyInput(keyEvent);
                } break;
                }
            }

            if (pendingMotion != null)
            {
                inputHandler.OnMouseInput(pendingMotion.Value);
                pendingMotion = null;
            }

            ErrorHandler.CheckGlError();
        }
Example #2
0
        public void PumpInput(IInputHandler inputHandler)
        {
            Game.HasInputFocus = 0 != (Sdl.SDL_GetAppState() & Sdl.SDL_APPINPUTFOCUS);

            var mods = MakeModifiers(Sdl.SDL_GetModState());

            inputHandler.ModifierKeys(mods);
            MouseInput?pendingMotion = null;

            Sdl.SDL_Event e;
            while (Sdl.SDL_PollEvent(out e) != 0)
            {
                switch (e.type)
                {
                case Sdl.SDL_QUIT:
                    Game.Exit();
                    break;

                case Sdl.SDL_MOUSEBUTTONDOWN:
                {
                    if (pendingMotion != null)
                    {
                        inputHandler.OnMouseInput(pendingMotion.Value);
                        pendingMotion = null;
                    }

                    var button = MakeButton(e.button.button);
                    lastButtonBits |= button;

                    var pos = new int2(e.button.x, e.button.y);

                    inputHandler.OnMouseInput(new MouseInput(
                                                  MouseInputEvent.Down, button, pos, mods,
                                                  MultiTapDetection.DetectFromMouse(e.button.button, pos)));

                    break;
                }

                case Sdl.SDL_MOUSEBUTTONUP:
                {
                    if (pendingMotion != null)
                    {
                        inputHandler.OnMouseInput(pendingMotion.Value);
                        pendingMotion = null;
                    }

                    var button = MakeButton(e.button.button);
                    lastButtonBits &= ~button;

                    var pos = new int2(e.button.x, e.button.y);
                    inputHandler.OnMouseInput(new MouseInput(
                                                  MouseInputEvent.Up, button, pos, mods,
                                                  MultiTapDetection.InfoFromMouse(e.button.button)));

                    break;
                }

                case Sdl.SDL_MOUSEMOTION:
                {
                    pendingMotion = new MouseInput(
                        MouseInputEvent.Move, lastButtonBits,
                        new int2(e.motion.x, e.motion.y), mods, 0);

                    break;
                }

                case Sdl.SDL_KEYDOWN:
                case Sdl.SDL_KEYUP:
                {
                    // Drop unknown keys
                    Keycode keyCode;
                    if (!KeyRemap.TryGetValue(e.key.keysym.sym, out keyCode))
                    {
                        // Try parsing it as text
                        var c = (char)e.key.keysym.unicode;
                        if (IsValidInput(c))
                        {
                            inputHandler.OnTextInput(c.ToString());
                        }

                        break;
                    }

                    var type = e.type == Sdl.SDL_KEYDOWN ?
                               KeyInputEvent.Down : KeyInputEvent.Up;

                    var tapCount = e.type == Sdl.SDL_KEYDOWN ?
                                   MultiTapDetection.DetectFromKeyboard(keyCode) :
                                   MultiTapDetection.InfoFromKeyboard(keyCode);

                    var keyEvent = new KeyInput
                    {
                        Event         = type,
                        Key           = keyCode,
                        Modifiers     = mods,
                        UnicodeChar   = (char)e.key.keysym.unicode,
                        MultiTapCount = tapCount
                    };

                    // Special case workaround for windows users
                    if (e.key.keysym.sym == Sdl.SDLK_F4 && mods.HasModifier(Modifiers.Alt) &&
                        Platform.CurrentPlatform == PlatformType.Windows)
                    {
                        Game.Exit();
                    }
                    else
                    {
                        inputHandler.OnKeyInput(keyEvent);
                    }

                    if (IsValidInput(keyEvent.UnicodeChar))
                    {
                        inputHandler.OnTextInput(keyEvent.UnicodeChar.ToString());
                    }

                    break;
                }
                }
            }

            if (pendingMotion != null)
            {
                inputHandler.OnMouseInput(pendingMotion.Value);
                pendingMotion = null;
            }

            ErrorHandler.CheckGlError();
        }