internal void ProcessKeyboardEvent(SDL.SDL_KeyboardEvent e) { Key key; bool pressed = e.state != 0; if (KeyMap.TryGetValue(e.keysym.scancode, out key)) { state.SetKeyState(key, (byte)e.keysym.scancode, pressed); } }
bool IsAxisBind(IntPtr gamecontroller, GameControllerAxis axis) { GameControllerButtonBind bind = SDL.GameControllerGetBindForAxis(gamecontroller, axis); return bind.BindType == GameControllerBindType.Axis; }
public void SetPosition(double x, double y) { SDL.WarpMouseInWindow(IntPtr.Zero, (int)x, (int)y); }
static void ProcessWindowEvent(Sdl2NativeWindow window, SDL.SDL_WindowEvent e) { switch (e.windowEvent) { case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_CLOSE: var close_args = new System.ComponentModel.CancelEventArgs(); window.Closing(window, close_args); if (!close_args.Cancel) { window.Closed(window, EventArgs.Empty); //window.DestroyWindow(); } break; case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_ENTER: window.MouseEnter(window, EventArgs.Empty); break; case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_LEAVE: window.MouseLeave(window, EventArgs.Empty); break; case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_EXPOSED: // do nothing break; case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_FOCUS_GAINED: window.is_focused = true; window.FocusedChanged(window, EventArgs.Empty); break; case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_FOCUS_LOST: window.is_focused = false; window.FocusedChanged(window, EventArgs.Empty); break; case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_HIDDEN: window.is_visible = false; window.VisibleChanged(window, EventArgs.Empty); break; case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_SHOWN: window.is_visible = true; window.VisibleChanged(window, EventArgs.Empty); break; case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_MAXIMIZED: window.previous_window_state = window.window_state; window.window_state = OpenTK.WindowState.Maximized; window.WindowStateChanged(window, EventArgs.Empty); break; case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_MINIMIZED: window.previous_window_state = window.window_state; window.window_state = OpenTK.WindowState.Minimized; window.WindowStateChanged(window, EventArgs.Empty); break; case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_RESTORED: window.window_state = window.previous_window_state; window.WindowStateChanged(window, EventArgs.Empty); break; case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_MOVED: window.Move(window, EventArgs.Empty); break; case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_RESIZED: case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_SIZE_CHANGED: window.Resize(window, EventArgs.Empty); break; default: Debug.Print("SDL2 unhandled event: {0}", e.type); break; } }
static Key TranslateKey(Keycode key) { Scancode scan = SDL.GetScancodeFromKey(key); return(TranslateKey(scan)); }
void GrabCursor(bool grab) { SDL.ShowCursor(!grab); SDL.SetWindowGrab(window.Handle, grab); SDL.SetRelativeMouseMode(grab); }
static void ProcessWheelEvent(Sdl2NativeWindow window, SDL.SDL_Event ev) { window.mouse.Wheel += ev.wheel.y; }
static void ProcessButtonEvent(Sdl2NativeWindow window, SDL.SDL_Event ev) { bool button_pressed = ev.button.state == SDL.SDL_PRESSED; // We need MouseUp events to be reported even if they occur // outside the window. SetWindowGrab ensures we get them. if (window.CursorVisible) { SDL.SDL_SetWindowGrab(window.window.Handle, button_pressed ? SDL.SDL_bool.SDL_TRUE : SDL.SDL_bool.SDL_FALSE); } switch (ev.button.button) { case (byte)SDL.SDL_BUTTON_LEFT: window.mouse[MouseButton.Left] = button_pressed; break; case (byte)SDL.SDL_BUTTON_MIDDLE: window.mouse[MouseButton.Middle] = button_pressed; break; case (byte)SDL.SDL_BUTTON_RIGHT: window.mouse[MouseButton.Right] = button_pressed; break; case (byte)SDL.SDL_BUTTON_X1: window.mouse[MouseButton.Button1] = button_pressed; break; case (byte)SDL.SDL_BUTTON_X2: window.mouse[MouseButton.Button2] = button_pressed; break; } }
static void ProcessKeyEvent(Sdl2NativeWindow window, SDL.SDL_Event ev) { bool key_pressed = ev.key.state == SDL.SDL_PRESSED; var key = ev.key.keysym; window.keyboard.SetKey(TranslateKey(key.scancode), (uint)key.scancode, key_pressed); }
static Key TranslateKey(SDL.SDL_Scancode scan) { Key result = Key.Unknown; if (map.ContainsKey(scan)) { result = map[scan]; } return result; }
static Key TranslateKey(SDL.SDL_Keycode key) { SDL.SDL_Scancode scan = SDL.SDL_GetScancodeFromKey(key); return TranslateKey(scan); }
public void ProcessMouseEvent(SDL.SDL_MouseButtonEvent button) { bool pressed = button.state == SDL.SDL_PRESSED; SetButtonState(TranslateButton(button.button), pressed); }
public void ProcessMouseEvent(SDL.SDL_MouseMotionEvent motion) { state.X += motion.xrel; state.Y += motion.yrel; }
public void ProcessWheelEvent(SDL.SDL_MouseWheelEvent wheel) { state.WheelPrecise += wheel.y; }
bool IsButtonBind(IntPtr gamecontroller, GameControllerButton button) { GameControllerButtonBind bind = SDL.GameControllerGetBindForButton(gamecontroller, button); return bind.BindType == GameControllerBindType.Button; }
static void ProcessMotionEvent(Sdl2NativeWindow window, SDL.SDL_Event ev) { window.mouse.Position = new Point(ev.motion.x, ev.motion.y); }
public void ProcessControllerEvent(ControllerDeviceEvent ev) { int id = ev.Which; if (id < 0) { Debug.Print("[SDL2] Invalid controller id {0} in {1}", id, ev.Type); return; } switch (ev.Type) { case EventType.CONTROLLERDEVICEADDED: IntPtr handle = SDL.GameControllerOpen(id); if (handle != IntPtr.Zero) { // The id variable here corresponds to a device_id between 0 and Sdl.NumJoysticks(). // It is only used in the ADDED event. All other events use an instance_id which increases // monotonically in each ADDED event. // The idea is that device_id refers to the n-th connected joystick, whereas instance_id // refers to the actual hardware device behind the n-th joystick. // Yes, it's confusing. int device_id = id; int instance_id = last_controllers_instance++; Sdl2GamePad pad = new Sdl2GamePad(handle); IntPtr joystick = SDL.GameControllerGetJoystick(handle); if (joystick != IntPtr.Zero) { pad.Capabilities = new GamePadCapabilities( GamePadType.GamePad, GetBoundAxes(joystick), GetBoundButtons(joystick), true); pad.State.SetConnected(true); // Connect this device and add the relevant device index if (controllers.Count <= id) { controllers.Add(pad); } else { controllers[device_id] = pad; } sdl_instanceid_to_controllers.Add(instance_id, device_id); } else { Debug.Print("[SDL2] Failed to retrieve joystick from game controller. Error: {0}", SDL.GetError()); } } break; case EventType.CONTROLLERDEVICEREMOVED: if (IsControllerInstanceValid(id)) { int instance_id = id; int device_id = sdl_instanceid_to_controllers[instance_id]; controllers[device_id].State.SetConnected(false); sdl_instanceid_to_controllers.Remove(device_id); } else { Debug.Print("[SDL2] Invalid game controller instance {0} in {1}", id, ev.Type); } break; case EventType.CONTROLLERDEVICEREMAPPED: if (IsControllerInstanceValid(id)) { // Todo: what should we do in this case? } else { Debug.Print("[SDL2] Invalid game controller instance {0} in {1}", id, ev.Type); } break; } }
Rectangle TranslateBounds(SDL.SDL_Rect rect) { return new Rectangle(rect.x, rect.y, rect.w, rect.h); }