const std = @import("std"); const assert = std.debug.assert; const sokol = @import("sokol"); const lib = @import("lib"); const Frame = lib.Frame; const Nanoseconds = lib.Nanoseconds; const Vec2 = lib.Math.Vec2; const Input = @This(); pub const Event = union(enum) { mouse_pressed: struct { button: lib.MouseButton, position: Vec2, }, mouse_released: struct { button: lib.MouseButton, position: Vec2, }, mouse_move: Vec2, mouse_enter: Vec2, mouse_leave, mouse_scroll: Vec2, key_pressed: struct { code: lib.KeyCode, repeat: bool }, key_released: lib.KeyCode, window_resize, char: u21, }; mouse_position: ?Vec2, pub const initial = Input{ .mouse_position = null }; pub fn processEvent(self: *Input, frame: *Frame, event: Event) void { switch (event) { .key_pressed => |opts| { if (!opts.repeat) { frame.keyboard.press(opts.code, frame.time_ns); } }, .key_released => |key_code| { frame.keyboard.release(key_code); }, .mouse_leave => { frame.keyboard.releaseAll(); assert(self.mouse_position != null); self.mouse_position = null; frame.mouse_button = .empty; }, .mouse_enter => |pos| { assert(self.mouse_position == null); self.mouse_position = pos; }, .mouse_move => |pos| { assert(self.mouse_position != null); self.mouse_position = pos; }, .mouse_pressed => |opts| { assert(self.mouse_position != null); self.mouse_position = opts.position; frame.mouse_button.press(opts.button, frame.time_ns); }, .mouse_released => |opts| { assert(self.mouse_position != null); self.mouse_position = opts.position; frame.mouse_button.release(opts.button); }, else => {} } } pub fn getKeyCodeFromSokol(key_code: sokol.app.Keycode) ?lib.KeyCode { return switch (key_code) { .SPACE => .SPACE, .APOSTROPHE => .APOSTROPHE, .COMMA => .COMMA, .MINUS => .MINUS, .PERIOD => .PERIOD, .SLASH => .SLASH, ._0 => ._0, ._1 => ._1, ._2 => ._2, ._3 => ._3, ._4 => ._4, ._5 => ._5, ._6 => ._6, ._7 => ._7, ._8 => ._8, ._9 => ._9, .SEMICOLON => .SEMICOLON, .EQUAL => .EQUAL, .A => .A, .B => .B, .C => .C, .D => .D, .E => .E, .F => .F, .G => .G, .H => .H, .I => .I, .J => .J, .K => .K, .L => .L, .M => .M, .N => .N, .O => .O, .P => .P, .Q => .Q, .R => .R, .S => .S, .T => .T, .U => .U, .V => .V, .W => .W, .X => .X, .Y => .Y, .Z => .Z, .LEFT_BRACKET => .LEFT_BRACKET, .BACKSLASH => .BACKSLASH, .RIGHT_BRACKET => .RIGHT_BRACKET, .GRAVE_ACCENT => .GRAVE_ACCENT, .WORLD_1 => .WORLD_1, .WORLD_2 => .WORLD_2, .ESCAPE => .ESCAPE, .ENTER => .ENTER, .TAB => .TAB, .BACKSPACE => .BACKSPACE, .INSERT => .INSERT, .DELETE => .DELETE, .RIGHT => .RIGHT, .LEFT => .LEFT, .DOWN => .DOWN, .UP => .UP, .PAGE_UP => .PAGE_UP, .PAGE_DOWN => .PAGE_DOWN, .HOME => .HOME, .END => .END, .CAPS_LOCK => .CAPS_LOCK, .SCROLL_LOCK => .SCROLL_LOCK, .NUM_LOCK => .NUM_LOCK, .PRINT_SCREEN => .PRINT_SCREEN, .PAUSE => .PAUSE, .F1 => .F1, .F2 => .F2, .F3 => .F3, .F4 => .F4, .F5 => .F5, .F6 => .F6, .F7 => .F7, .F8 => .F8, .F9 => .F9, .F10 => .F10, .F11 => .F11, .F12 => .F12, .F13 => .F13, .F14 => .F14, .F15 => .F15, .F16 => .F16, .F17 => .F17, .F18 => .F18, .F19 => .F19, .F20 => .F20, .F21 => .F21, .F22 => .F22, .F23 => .F23, .F24 => .F24, .F25 => .F25, .KP_0 => .KP_0, .KP_1 => .KP_1, .KP_2 => .KP_2, .KP_3 => .KP_3, .KP_4 => .KP_4, .KP_5 => .KP_5, .KP_6 => .KP_6, .KP_7 => .KP_7, .KP_8 => .KP_8, .KP_9 => .KP_9, .KP_DECIMAL => .KP_DECIMAL, .KP_DIVIDE => .KP_DIVIDE, .KP_MULTIPLY => .KP_MULTIPLY, .KP_SUBTRACT => .KP_SUBTRACT, .KP_ADD => .KP_ADD, .KP_ENTER => .KP_ENTER, .KP_EQUAL => .KP_EQUAL, .LEFT_SHIFT => .LEFT_SHIFT, .LEFT_CONTROL => .LEFT_CONTROL, .LEFT_ALT => .LEFT_ALT, .LEFT_SUPER => .LEFT_SUPER, .RIGHT_SHIFT => .RIGHT_SHIFT, .RIGHT_CONTROL => .RIGHT_CONTROL, .RIGHT_ALT => .RIGHT_ALT, .RIGHT_SUPER => .RIGHT_SUPER, .MENU => .MENU, else => null }; } pub fn getMouseButtonFromSokol(mouse_button: sokol.app.Mousebutton) ?lib.MouseButton { return switch (mouse_button) { .RIGHT => .right, .MIDDLE => .middle, .LEFT => .left, else => null }; }