Started to setup input events based off sokol

Will replace the input polling done with raylib.

Going to also provide the more robust input tracking for consuming events with the UI interactions
This commit is contained in:
2024-06-17 03:35:53 -04:00
parent 05ffaf432d
commit 425a642fd3
16 changed files with 382 additions and 52 deletions

View File

@ -1 +1,12 @@
package sectr
package sectr
InputBindCallback :: #type proc(user_ptr : rawptr)
InputBind :: struct
{
user_ptr : rawptr,
callback : InputBindCallback,
}

View File

@ -1 +1,63 @@
package sectr
InputEventType :: enum u32 {
Key_Pressed,
Key_Released,
Mouse_Pressed,
Mouse_Released,
Mouse_Scroll,
Mouse_Move,
Mouse_Enter,
Mouse_Leave,
Unicode,
}
InputEvent :: struct
{
frame_id : u64,
type : InputEventType,
key : KeyCode,
modifiers : ModifierCodeFlags,
mouse : struct {
btn : MouseBtn,
pos : Vec2,
delta : Vec2,
scroll : Vec2,
},
codepoint : rune,
// num_touches : u32,
// touches : Touchpoint,
_sokol_frame_id : u64,
}
InputKeyEvent :: struct {
frame_id : u64,
type : InputEventType,
key : KeyCode,
modifiers : ModifierCodeFlags,
}
InputMouseEvent :: struct {
frame_id : u64,
type : InputEventType,
key : KeyCode,
modifiers : ModifierCodeFlags,
}
// Note(Ed): There is a staged_input_events : Array(InputEvent), in the state.odin's State struct
append_staged_input_events :: #force_inline proc() {
// TODO(Ed) : Add guards for multi-threading
state := get_state()
array_append( & state.staged_input_events, event )
}
pull_staged_input_events :: proc( input : ^InputState, staged_events : ^Array(InputEvent) )
{
// TODO(Ed) : Add guards for multi-threading
}

View File

@ -31,6 +31,8 @@ MouseBtn :: enum u32 {
Back = 0x5,
Extra = 0x6,
Invalid = 0x100,
count
}
@ -40,7 +42,11 @@ KeyboardState :: struct #raw_union {
ignored : DigitalBtn,
__0x02_0x07_Unassigned__ : [ 6 * size_of( DigitalBtn)] u8,
// GFLW / Sokol
menu,
world_1, world_2 : DigitalBtn,
__0x05_0x07_Unassigned__ : [ 3 * size_of( DigitalBtn)] u8,
tab, backspace : DigitalBtn,
@ -61,8 +67,7 @@ KeyboardState :: struct #raw_union {
right_shift,
right_control : DigitalBtn,
__0x19_Unassigned__ : [ 1 * size_of( DigitalBtn )] u8,
print_screen,
pause,
escape,
home,
@ -136,9 +141,27 @@ KeyboardState :: struct #raw_union {
F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12 : DigitalBtn,
insert, delete : DigitalBtn,
F13, F14, F15, F16, F17, F18, F19, F20, F21, F22, F23, F24, F25 : DigitalBtn,
}
}
ModifierCode :: enum u32 {
Shift,
Control,
Alt,
Left_Mouse,
Right_Mouse,
Middle_Mouse,
Left_Shift,
Right_Shift,
Left_Control,
Right_Control,
Left_Alt,
Right_Alt,
}
ModifierCodeFlags :: bit_set[ModifierCode; u32]
MouseState :: struct {
using _ : struct #raw_union {
btns : [16] DigitalBtn,
@ -161,11 +184,9 @@ InputState :: struct {
keyboard : KeyboardState,
mouse : MouseState,
keyboard_events : KeyboardEvents,
}
events : Array(InputEvent),
key_events : Array(InputKeyEvent),
mouse_events : Array(InputMouseEvent),
// TODO(Ed): Whats the lifetime of these events? (So far we're picking per full-frame)
KeyboardEvents :: struct {
keys_pressed : Array(KeyCode),
chars_pressed : Array(rune),
codes_pressed : Array(rune),
}

View File

@ -1 +1,109 @@
package sectr
package sectr
import "base:runtime"
import "core:os"
import "core:c/libc"
import sokol_app "thirdparty:sokol/app"
to_modifiers_code_from_sokol :: proc( sokol_modifiers : u32 ) -> ( modifiers : ModifierCodeFlags )
{
if sokol_modifiers & sokol_app.MODIFIER_SHIFT != 0 {
modifiers |= { .Shift }
}
if sokol_modifiers & sokol_app.MODIFIER_CTRL != 0 {
modifiers |= { .Control }
}
if sokol_modifiers & sokol_app.MODIFIER_ALT != 0 {
modifiers |= { .Alt }
}
if sokol_modifiers & sokol_app.MODIFIER_LMB != 0 {
modifiers |= { .Left_Mouse }
}
if sokol_modifiers & sokol_app.MODIFIER_RMB != 0 {
modifiers |= { .Right_Mouse }
}
if sokol_modifiers & sokol_app.MODIFIER_MMB != 0 {
modifiers |= { .Middle_Mouse }
}
if sokol_modifiers & sokol_app.MODIFIER_LSHIFT != 0 {
modifiers |= { .Left_Shift }
}
if sokol_modifiers & sokol_app.MODIFIER_RSHIFT != 0 {
modifiers |= { .Right_Shift }
}
if sokol_modifiers & sokol_app.MODIFIER_LCTRL != 0 {
modifiers |= { .Left_Control }
}
if sokol_modifiers & sokol_app.MODIFIER_RCTRL != 0 {
modifiers |= { .Right_Control }
}
if sokol_modifiers & sokol_app.MODIFIER_LALT != 0 {
modifiers |= { .Left_Alt }
}
if sokol_modifiers & sokol_app.MODIFIER_RALT != 0 {
modifiers |= { .Right_Alt }
}
return
}
to_key_from_sokol :: proc( sokol_key : sokol_app.Keycode ) -> ( key : KeyCode )
{
world_code_offset :: i32(sokol_app.Keycode.WORLD_1) - i32(KeyCode.world_1)
arrow_code_offset :: i32(sokol_app.Keycode.RIGHT) - i32(KeyCode.right)
func_row_code_offset :: i32(sokol_app.Keycode.F1) - i32(KeyCode.F1)
func_extra_code_offset :: i32(sokol_app.Keycode.F13) - i32(KeyCode.F25)
keypad_num_offset :: i32(sokol_app.Keycode.KP_0) - i32(KeyCode.kpad_0)
switch sokol_key {
case .INVALID ..= .GRAVE_ACCENT : key = transmute(KeyCode) sokol_key
case .WORLD_1, .WORLD_2 : key = transmute(KeyCode) (i32(sokol_key) - world_code_offset)
case .ESCAPE : key = .escape
case .ENTER : key = .enter
case .TAB : key = .tab
case .BACKSPACE : key = .backspace
case .INSERT : key = .insert
case .DELETE : key = .delete
case .RIGHT ..= .UP : key = transmute(KeyCode) (i32(sokol_key) - arrow_code_offset)
case .PAGE_UP : key = .page_up
case .PAGE_DOWN : key = .page_down
case .HOME : key = .home
case .END : key = .end
case .CAPS_LOCK : key = .caps_lock
case .SCROLL_LOCK : key = .scroll_lock
case .NUM_LOCK : key = .num_lock
case .PRINT_SCREEN : key = .print_screen
case .PAUSE : key = .pause
case .F1 ..= .F12 : key = transmute(KeyCode) (i32(sokol_key) - func_row_code_offset)
case .F13 ..= .F25 : key = transmute(KeyCode) (i32(sokol_key) - func_extra_code_offset)
case .KP_0 ..= .KP_9 : key = transmute(KeyCode) (i32(sokol_key) - keypad_num_offset)
case .KP_DECIMAL : key = .kpad_decimal
case .KP_DIVIDE : key = .kpad_divide
case .KP_MULTIPLY : key = .kpad_multiply
case .KP_SUBTRACT : key = .kpad_minus
case .KP_ADD : key = .kpad_plus
case .KP_ENTER : key = .kpad_enter
case .KP_EQUAL : key = .kpad_equals
case .LEFT_SHIFT : key = .left_shift
case .LEFT_CONTROL : key = .left_control
case .LEFT_ALT : key = .left_alt
case .LEFT_SUPER : key = .ignored
case .RIGHT_SHIFT : key = .right_shift
case .RIGHT_CONTROL : key = .right_control
case .RIGHT_ALT : key = .right_alt
case .RIGHT_SUPER : key = .ignored
case .MENU : key = .menu
}
return
}
to_mouse_btn_from_sokol :: proc( sokol_mouse : sokol_app.Mousebutton ) -> ( btn : MouseBtn )
{
switch sokol_mouse {
case .LEFT : btn = .Left
case .MIDDLE : btn = .Middle
case .RIGHT : btn = .Right
case .INVALID : btn = .Invalid
}
return
}

View File

@ -6,9 +6,10 @@ KeyCode :: enum u32 {
null = 0x00,
ignored = 0x01,
// 0x02
// 0x03
// 0x04
menu = 0x02,
world_1 = 0x03,
world_2 = 0x04,
// 0x05
// 0x06
// 0x07
@ -36,8 +37,7 @@ KeyCode :: enum u32 {
right_shift = 0x17,
right_control = 0x18,
// 0x19
print_screen = 0x19,
pause = 0x1A,
escape = '\x1B', // 0x1B
home = 0x1C,
@ -150,5 +150,19 @@ KeyCode :: enum u32 {
insert = 0x7E,
delete = 0x7F,
count = 0x80,
F13 = 0x80,
F14 = 0x81,
F15 = 0x82,
F16 = 0x83,
F17 = 0x84,
F18 = 0x85,
F19 = 0x86,
F20 = 0x87,
F21 = 0x88,
F22 = 0x89,
F23 = 0x8A,
F24 = 0x8B,
F25 = 0x8C,
count = 0x8D,
}