public void PumpInput(IInputHandler inputHandler) { var mods = MakeModifiers((int)SDL.SDL_GetModState()); var scrollDelta = 0; inputHandler.ModifierKeys(mods); MouseInput?pendingMotion = null; SDL.SDL_Event e; while (SDL.SDL_PollEvent(out e) != 0) { switch (e.type) { case SDL.SDL_EventType.SDL_QUIT: Game.Exit(); break; case SDL.SDL_EventType.SDL_WINDOWEVENT: { switch (e.window.windowEvent) { case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_FOCUS_LOST: Game.HasInputFocus = false; break; case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_FOCUS_GAINED: Game.HasInputFocus = true; break; } break; } case SDL.SDL_EventType.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, scrollDelta, pos, mods, MultiTapDetection.DetectFromMouse(e.button.button, pos))); break; } case SDL.SDL_EventType.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, scrollDelta, pos, mods, MultiTapDetection.InfoFromMouse(e.button.button))); break; } case SDL.SDL_EventType.SDL_MOUSEMOTION: { pendingMotion = new MouseInput( MouseInputEvent.Move, lastButtonBits, scrollDelta, new int2(e.motion.x, e.motion.y), mods, 0); break; } case SDL.SDL_EventType.SDL_MOUSEWHEEL: { int x, y; SDL.SDL_GetMouseState(out x, out y); scrollDelta = e.wheel.y; inputHandler.OnMouseInput(new MouseInput(MouseInputEvent.Scroll, MouseButton.None, scrollDelta, new int2(x, y), Modifiers.None, 0)); break; } case SDL.SDL_EventType.SDL_TEXTINPUT: { var rawBytes = new byte[SDL.SDL_TEXTINPUTEVENT_TEXT_SIZE]; unsafe { Marshal.Copy((IntPtr)e.text.text, rawBytes, 0, SDL.SDL_TEXTINPUTEVENT_TEXT_SIZE); } inputHandler.OnTextInput(Encoding.UTF8.GetString(rawBytes, 0, Array.IndexOf(rawBytes, (byte)0))); break; } case SDL.SDL_EventType.SDL_KEYDOWN: case SDL.SDL_EventType.SDL_KEYUP: { var keyCode = (Keycode)e.key.keysym.sym; var type = e.type == SDL.SDL_EventType.SDL_KEYDOWN ? KeyInputEvent.Down : KeyInputEvent.Up; var tapCount = e.type == SDL.SDL_EventType.SDL_KEYDOWN ? MultiTapDetection.DetectFromKeyboard(keyCode) : MultiTapDetection.InfoFromKeyboard(keyCode); var keyEvent = new KeyInput { Event = type, Key = keyCode, Modifiers = mods, UnicodeChar = (char)e.key.keysym.sym, MultiTapCount = tapCount }; // Special case workaround for windows users if (e.key.keysym.sym == SDL.SDL_Keycode.SDLK_F4 && mods.HasModifier(Modifiers.Alt) && Platform.CurrentPlatform == PlatformType.Windows) { Game.Exit(); } else { inputHandler.OnKeyInput(keyEvent); } break; } } } if (pendingMotion != null) { inputHandler.OnMouseInput(pendingMotion.Value); pendingMotion = null; } ErrorHandler.CheckGlError(); }
public void PumpInput(IInputHandler inputHandler) { var mods = MakeModifiers((int)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_EventType.SDL_QUIT: Game.Exit(); break; case SDL.SDL_EventType.SDL_WINDOWEVENT: { switch (e.window.windowEvent) { case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_FOCUS_LOST: Game.HasInputFocus = false; break; case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_FOCUS_GAINED: Game.HasInputFocus = true; break; } break; } case SDL.SDL_EventType.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_EventType.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_EventType.SDL_MOUSEMOTION: { pendingMotion = new MouseInput( MouseInputEvent.Move, lastButtonBits, new int2(e.motion.x, e.motion.y), mods, 0); break; } case SDL.SDL_EventType.SDL_MOUSEWHEEL: { // Retain compatibility with existing bogus behavior // TODO: Implement real scroll behavior after we drop SDL 1.2 support if (e.wheel.y == 0) { break; } int x, y; SDL.SDL_GetMouseState(out x, out y); var button = e.wheel.y < 0 ? MouseButton.WheelDown : MouseButton.WheelUp; inputHandler.OnMouseInput(new MouseInput(MouseInputEvent.Down, button, new int2(x, y), Modifiers.None, 0)); break; } case SDL.SDL_EventType.SDL_TEXTINPUT: { string input; unsafe { var data = new byte[SDL.SDL_TEXTINPUTEVENT_TEXT_SIZE]; var i = 0; for (; i < SDL.SDL_TEXTINPUTEVENT_TEXT_SIZE; i++) { var b = e.text.text[i]; if (b == '\0') { break; } data[i] = b; } input = Encoding.UTF8.GetString(data, 0, i); } inputHandler.OnTextInput(input); break; } case SDL.SDL_EventType.SDL_KEYDOWN: case SDL.SDL_EventType.SDL_KEYUP: { var keyCode = (Keycode)e.key.keysym.sym; var type = e.type == SDL.SDL_EventType.SDL_KEYDOWN ? KeyInputEvent.Down : KeyInputEvent.Up; var tapCount = e.type == SDL.SDL_EventType.SDL_KEYDOWN ? MultiTapDetection.DetectFromKeyboard(keyCode) : MultiTapDetection.InfoFromKeyboard(keyCode); var keyEvent = new KeyInput { Event = type, Key = keyCode, Modifiers = mods, UnicodeChar = (char)e.key.keysym.sym, MultiTapCount = tapCount }; // Special case workaround for windows users if (e.key.keysym.sym == SDL.SDL_Keycode.SDLK_F4 && mods.HasModifier(Modifiers.Alt) && Platform.CurrentPlatform == PlatformType.Windows) { Game.Exit(); } else { inputHandler.OnKeyInput(keyEvent); } break; } } } if (pendingMotion != null) { inputHandler.OnMouseInput(pendingMotion.Value); pendingMotion = null; } ErrorHandler.CheckGlError(); }
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(); }
public void PumpInput(Sdl2PlatformWindow device, IInputHandler inputHandler, int2?lockedMousePosition) { var mods = MakeModifiers((int)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_EventType.SDL_QUIT: Game.Exit(); break; case SDL.SDL_EventType.SDL_WINDOWEVENT: { switch (e.window.windowEvent) { case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_FOCUS_LOST: device.HasInputFocus = false; break; case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_FOCUS_GAINED: device.HasInputFocus = true; break; // Triggered when moving between displays with different DPI settings case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_SIZE_CHANGED: device.WindowSizeChanged(); break; } break; } case SDL.SDL_EventType.SDL_MOUSEBUTTONDOWN: { if (pendingMotion != null) { inputHandler.OnMouseInput(pendingMotion.Value); pendingMotion = null; } var button = MakeButton(e.button.button); lastButtonBits |= button; var input = lockedMousePosition ?? new int2(e.button.x, e.button.y); var pos = EventPosition(device, input.X, input.Y); inputHandler.OnMouseInput(new MouseInput( MouseInputEvent.Down, button, pos, int2.Zero, mods, MultiTapDetection.DetectFromMouse(e.button.button, pos))); break; } case SDL.SDL_EventType.SDL_MOUSEBUTTONUP: { if (pendingMotion != null) { inputHandler.OnMouseInput(pendingMotion.Value); pendingMotion = null; } var button = MakeButton(e.button.button); lastButtonBits &= ~button; var input = lockedMousePosition ?? new int2(e.button.x, e.button.y); var pos = EventPosition(device, input.X, input.Y); inputHandler.OnMouseInput(new MouseInput( MouseInputEvent.Up, button, pos, int2.Zero, mods, MultiTapDetection.InfoFromMouse(e.button.button))); break; } case SDL.SDL_EventType.SDL_MOUSEMOTION: { var mousePos = new int2(e.motion.x, e.motion.y); var input = lockedMousePosition ?? mousePos; var pos = EventPosition(device, input.X, input.Y); var delta = lockedMousePosition == null ? EventPosition(device, e.motion.xrel, e.motion.yrel) : mousePos - lockedMousePosition.Value; pendingMotion = new MouseInput( MouseInputEvent.Move, lastButtonBits, pos, delta, mods, 0); break; } case SDL.SDL_EventType.SDL_MOUSEWHEEL: { int x, y; SDL.SDL_GetMouseState(out x, out y); var pos = EventPosition(device, x, y); inputHandler.OnMouseInput(new MouseInput(MouseInputEvent.Scroll, MouseButton.None, pos, new int2(0, e.wheel.y), mods, 0)); break; } case SDL.SDL_EventType.SDL_TEXTINPUT: { var rawBytes = new byte[SDL.SDL_TEXTINPUTEVENT_TEXT_SIZE]; unsafe { Marshal.Copy((IntPtr)e.text.text, rawBytes, 0, SDL.SDL_TEXTINPUTEVENT_TEXT_SIZE); } inputHandler.OnTextInput(Encoding.UTF8.GetString(rawBytes, 0, Array.IndexOf(rawBytes, (byte)0))); break; } case SDL.SDL_EventType.SDL_KEYDOWN: case SDL.SDL_EventType.SDL_KEYUP: { var keyCode = (Keycode)e.key.keysym.sym; var type = e.type == SDL.SDL_EventType.SDL_KEYDOWN ? KeyInputEvent.Down : KeyInputEvent.Up; var tapCount = e.type == SDL.SDL_EventType.SDL_KEYDOWN ? MultiTapDetection.DetectFromKeyboard(keyCode, mods) : MultiTapDetection.InfoFromKeyboard(keyCode, mods); var keyEvent = new KeyInput { Event = type, Key = keyCode, Modifiers = mods, UnicodeChar = (char)e.key.keysym.sym, MultiTapCount = tapCount, IsRepeat = e.key.repeat != 0 }; // Special case workaround for windows users if (e.key.keysym.sym == SDL.SDL_Keycode.SDLK_F4 && mods.HasModifier(Modifiers.Alt) && Platform.CurrentPlatform == PlatformType.Windows) { Game.Exit(); } else { inputHandler.OnKeyInput(keyEvent); } break; } } } if (pendingMotion != null) { inputHandler.OnMouseInput(pendingMotion.Value); pendingMotion = null; } }
public void Process(UpdateContext context, EcsContext ecs) { ecs.Query((ref Transform t, ref MouseInput c) => { if ((!InputConsumed || c.HandleConsumed || _consumedBy == c) && WithinInputArea(context.Scene, _mousePos, t, c)) { // Handle mouse enter if (!_mouseEntered.Contains(c)) { _mouseEntered.Add(c); c.OnMouseEnter?.Invoke(); } if (_mouseState.LeftButton == ButtonState.Pressed) { c.ButtonHeld = true; } else { c.ButtonHeld = false; } // Handle click if (_mouseState.LeftButton == ButtonState.Pressed && _prevMouseState.LeftButton == ButtonState.Released) { c.OnClick?.Invoke(MouseToAreaPos(context.Scene, _mousePos, t, c)); if (_focused != c) { c.OnFocusEnter?.Invoke(); _focused?.OnFocusExit?.Invoke(); _focused = c; } } // Handle scroll var scroll = _mouseState.ScrollWheelValue - _prevMouseState.ScrollWheelValue; if (scroll != 0) { c.OnScroll?.Invoke(_mousePos, scroll); } // Handle movement if (_mousePos != _prevMousePos) { var curAreaPos = MouseToAreaPos(context.Scene, _mousePos, t, c); var prevAreaPos = MouseToAreaPos(context.Scene, _prevMousePos, t, c); c.OnMove?.Invoke(curAreaPos, curAreaPos - prevAreaPos); } // Mark input as consumed if (c.ConsumeInput) { _consumedBy = c; InputConsumed = true; } } else if (_mouseEntered.Contains(c)) { c.OnMouseExit?.Invoke(); _mouseEntered.Remove(c); } }, reverse: true); }
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(); }
public string GetCursor(World world, int2 xy, MouseInput mi) { _lastMouseInput = mi; mi.Button = MouseButton.Left; return OrderInner(world, xy, mi).Any() ? "chrono-select" : "move-blocked"; }