diff --git a/src/base/base_input.h b/src/base/base_controller.h similarity index 67% rename from src/base/base_input.h rename to src/base/base_controller.h index 64f4cee3..61b4dd9b 100644 --- a/src/base/base_input.h +++ b/src/base/base_controller.h @@ -1,5 +1,5 @@ //////////////////////////////////////////////////////////// -//~ Button types +//~ Controller button types Enum(Btn) { @@ -110,40 +110,49 @@ Enum(Btn) }; //////////////////////////////////////////////////////////// -//~ Input types +//~ Controller event types -Enum(InputKind) +Enum(ControllerEventKind) { - InputKind_None, + ControllerEventKind_None, - InputKind_ButtonDown, - InputKind_ButtonUp, + ControllerEventKind_ButtonDown, + ControllerEventKind_ButtonUp, - InputKind_CursorMove, - InputKind_MouseMove, + ControllerEventKind_CursorMove, + ControllerEventKind_MouseMove, - InputKind_Text, + ControllerEventKind_Text, - InputKind_Quit, + ControllerEventKind_Quit, - InputKind_Count + ControllerEventKind_Count }; -Struct(Input) +Struct(ControllerEvent) { - InputKind kind; + ControllerEventKind kind; - /* InputKind_ButtonDown */ - /* InputKind_ButtonUp */ + /* ControllerEventKind_ButtonDown */ + /* ControllerEventKind_ButtonUp */ Btn button; b32 is_repeat; - /* InputKind_Text */ + /* ControllerEventKind_Text */ u32 text_codepoint; - /* InputKind_CursorMove */ + /* ControllerEventKind_CursorMove */ Vec2I32 cursor_pos; - /* InputKind_MouseMove */ + /* ControllerEventKind_MouseMove */ Vec2I32 mouse_delta; + + /* Should be incremented by systems that want to consume the input */ + u32 consumed; +}; + +Struct(ControllerEventsArray) +{ + u64 count; + ControllerEvent *events; }; diff --git a/src/base/base_inc.h b/src/base/base_inc.h index c30846dc..4a6047d5 100644 --- a/src/base/base_inc.h +++ b/src/base/base_inc.h @@ -24,7 +24,7 @@ # include "base_entry.h" # include "base_bitbuff.h" # include "base_resource.h" -# include "base_input.h" +# include "base_controller.h" #elif LanguageIsGpu # include "base_math_gpu.h" #endif diff --git a/src/pp/pp.c b/src/pp/pp.c index 5ff560ae..1044be26 100644 --- a/src/pp/pp.c +++ b/src/pp/pp.c @@ -388,15 +388,14 @@ void PP_UpdateUser(void) g->window_restore.len = src.len; } - u64 inputs_count = window_frame.inputs_count; - Input *inputs = window_frame.inputs; + ControllerEventsArray controller_events = window_frame.controller_events; g->screen_size = window_frame.draw_size; g->screen_size.x = MaxI32(g->screen_size.x, 1); g->screen_size.y = MaxI32(g->screen_size.y, 1); //- Begin UI - UI_BeginBuild(); + UI_BeginBuild(&controller_events); UI_Push(LayoutAxis, Axis_Y); if (window_frame.forced_top) { @@ -538,9 +537,9 @@ void PP_UpdateUser(void) } } - //- Process inputs into user bind state + //- Process controller events into user bind state { - __profn("Process inputs"); + __profn("Process controller events"); /* Reset bind pressed / released states */ for (u32 i = 0; i < countof(g->bind_states); ++i) @@ -550,38 +549,38 @@ void PP_UpdateUser(void) }; } - for (u64 idx = 0; idx < inputs_count; ++idx) + for (u64 idx = 0; idx < controller_events.count; ++idx) { - Input *input = &inputs[idx]; - if (input->kind == InputKind_Quit) + ControllerEvent *event = &controller_events.events[idx]; + if (event->kind == ControllerEventKind_Quit) { SignalExit(0); } - if (input->kind == InputKind_ButtonUp) + if (event->kind == ControllerEventKind_ButtonUp) { /* Escape quit */ - if (input->button == Btn_Esc) + if (event->button == Btn_Esc) { SignalExit(0); } } /* Update mouse pos */ - if (input->kind == InputKind_CursorMove) + if (event->kind == ControllerEventKind_CursorMove) { - g->screen_cursor = Vec2FromFields(input->cursor_pos); + g->screen_cursor = Vec2FromFields(event->cursor_pos); } /* Update bind states */ - if ((input->kind == InputKind_ButtonDown || input->kind == InputKind_ButtonUp)) + if ((event->kind == ControllerEventKind_ButtonDown || event->kind == ControllerEventKind_ButtonUp)) { - Btn button = input->button; + Btn button = event->button; button = button >= Btn_Count ? Btn_None : button; PP_BindKind bind = g_binds[button]; if (bind) { - b32 pressed = input->kind == InputKind_ButtonDown; + b32 pressed = event->kind == ControllerEventKind_ButtonDown; #if 0 b32 out_of_bounds = button >= Btn_M1 && button <= Btn_M5 && (g->ui_cursor.x < 0 || @@ -597,7 +596,7 @@ void PP_UpdateUser(void) if (!out_of_bounds) { ++g->bind_states[bind].num_presses_and_repeats; - if (input->is_repeat) + if (event->is_repeat) { ++g->bind_states[bind].num_repeats; } @@ -1941,22 +1940,16 @@ void PP_UpdateUser(void) /* Draw lister */ if (g->debug_lister) { - g->debug_lister_pos = g->ui_cursor; - Vec2 size = VEC2(400, 500); - Vec2 pos = g->debug_lister_pos; - pos.x -= size.x / 2; - UI_SetNext(LayoutAxis, Axis_Y); - UI_SetNext(Tint, Alpha32F(0xFFFFFFFF, 0.5)); - UI_SetNext(BackgroundColor, Rgba32F(0.3, 0.3, 0.3, 1)); - UI_SetNext(BorderColor, Rgba32F(0.4, 0.4, 0.4, 1)); + UI_SetNext(BackgroundColor, Rgba32F(0.075, 0.075, 0.075, 0.99)); + UI_SetNext(BorderColor, Rgba32F(0.2, 0.2, 0.2, 1)); UI_SetNext(Border, 2); - UI_SetNext(Rounding, 0.25); + UI_SetNext(Rounding, 0.1); UI_SetNext(Parent, pp_root_box); UI_SetNext(Flags, UI_BoxFlag_Floating | UI_BoxFlag_ClampFloatingX | UI_BoxFlag_ClampFloatingY); - UI_SetNext(FloatingPos, pos); + UI_SetNext(FloatingPos, g->debug_lister_pos); UI_SetNext(Width, UI_PIX(size.x, 0)); UI_SetNext(Height, UI_PIX(size.y, 0)); UI_Box *lister_box = UI_BuildBox(UI_NilKey); diff --git a/src/ui/ui_core.c b/src/ui/ui_core.c index 547d329c..61ac7d64 100644 --- a/src/ui/ui_core.c +++ b/src/ui/ui_core.c @@ -253,7 +253,7 @@ void UI_SetBackgroundTexture(UI_Box *box, GPU_Resource *texture, Vec2 uv0, Vec2 //////////////////////////////////////////////////////////// //~ Begin build -void UI_BeginBuild(void) +void UI_BeginBuild(ControllerEventsArray *controller_events) { UI_SharedState *g = &UI_shared_state; @@ -312,7 +312,7 @@ void UI_BeginBuild(void) if (!g->back_build_arena) { /* Back buffer not initialized, swap again */ - UI_BeginBuild(); + UI_BeginBuild(controller_events); } } diff --git a/src/ui/ui_core.h b/src/ui/ui_core.h index a17c5fc3..e422d21f 100644 --- a/src/ui/ui_core.h +++ b/src/ui/ui_core.h @@ -255,7 +255,7 @@ void UI_SetBackgroundTexture(UI_Box *box, GPU_Resource *texture, Vec2 uv0, Vec2 //////////////////////////////////////////////////////////// //~ Begin build -void UI_BeginBuild(void); +void UI_BeginBuild(ControllerEventsArray *controller_events); //////////////////////////////////////////////////////////// //~ End build diff --git a/src/window/window.h b/src/window/window.h index ab20f85e..ac397df9 100644 --- a/src/window/window.h +++ b/src/window/window.h @@ -34,8 +34,7 @@ Struct(WND_Frame) WND_Handle window_handle; /* User input since last update */ - u64 inputs_count; - Input *inputs; + ControllerEventsArray controller_events; /* Window info */ Vec2I32 draw_size; diff --git a/src/window/window_win32/window_win32.c b/src/window/window_win32/window_win32.c index ea83a158..74bedec5 100644 --- a/src/window/window_win32/window_win32.c +++ b/src/window/window_win32/window_win32.c @@ -106,7 +106,7 @@ JobDef(WND_W32_ProcessMessagesForever, sig, id) { WND_W32_SharedState *g = &WND_W32_shared_state; WND_W32_Window *window = &g->window; - window->w2u_inputs_arena = AcquireArena(Gibi(64)); + window->w2u_events_arena = AcquireArena(Gibi(64)); //- Init hwnd HWND hwnd = 0; @@ -167,11 +167,11 @@ JobDef(WND_W32_ProcessMessagesForever, sig, id) //////////////////////////////////////////////////////////// //~ Message processing -void WND_W32_PushInput(WND_W32_Window *window, Input input) +void WND_W32_PushEvent(WND_W32_Window *window, ControllerEvent event) { LockTicketMutex(&window->w2u_tm); { - *PushStructNoZero(window->w2u_inputs_arena, Input) = input; + *PushStructNoZero(window->w2u_events_arena, ControllerEvent) = event; } UnlockTicketMutex(&window->w2u_tm); } @@ -196,7 +196,7 @@ LRESULT CALLBACK WND_W32_WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM l case WM_CLOSE: case WM_DESTROY: { - WND_W32_PushInput(window, (Input) { .kind = InputKind_Quit }); + WND_W32_PushEvent(window, (ControllerEvent) { .kind = ControllerEventKind_Quit }); } break; //- Keyboard button @@ -206,21 +206,21 @@ LRESULT CALLBACK WND_W32_WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM l case WM_KEYDOWN: { WORD vk_code = LOWORD(wparam); - Input input = ZI; + ControllerEvent event = ZI; if (msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN) { - input.kind = InputKind_ButtonDown; - input.is_repeat = (lparam & 0x40000000) != 0; + event.kind = ControllerEventKind_ButtonDown; + event.is_repeat = (lparam & 0x40000000) != 0; } else if (msg == WM_KEYUP || msg == WM_SYSKEYUP) { - input.kind = InputKind_ButtonUp; + event.kind = ControllerEventKind_ButtonUp; } if (vk_code < countof(g->vk_to_btn)) { - input.button = g->vk_to_btn[vk_code]; + event.button = g->vk_to_btn[vk_code]; } - WND_W32_PushInput(window, input); + WND_W32_PushEvent(window, event); if (msg == WM_SYSKEYUP || msg == WM_SYSKEYDOWN) { result = DefWindowProcW(hwnd, msg, wparam, lparam); @@ -268,10 +268,10 @@ LRESULT CALLBACK WND_W32_WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM l } if ((codepoint >= 32 && codepoint != 127) || codepoint == '\t' || codepoint == '\n') { - Input input = ZI; - input.kind = InputKind_Text; - input.text_codepoint = codepoint; - WND_W32_PushInput(window, input); + ControllerEvent event = ZI; + event.kind = ControllerEventKind_Text; + event.text_codepoint = codepoint; + WND_W32_PushEvent(window, event); } } break; @@ -285,42 +285,42 @@ LRESULT CALLBACK WND_W32_WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM l case WM_RBUTTONDOWN: case WM_XBUTTONDOWN: { - Input input = ZI; + ControllerEvent event = ZI; b32 is_down = msg == WM_LBUTTONDOWN || msg == WM_MBUTTONDOWN || msg == WM_RBUTTONDOWN || msg == WM_XBUTTONDOWN; if (is_down) { - input.kind = InputKind_ButtonDown; + event.kind = ControllerEventKind_ButtonDown; SetCapture(hwnd); } else { - input.kind = InputKind_ButtonUp; + event.kind = ControllerEventKind_ButtonUp; ReleaseCapture(); } switch (msg) { - case WM_LBUTTONUP: case WM_LBUTTONDOWN: input.button = Btn_M1; break; - case WM_RBUTTONUP: case WM_RBUTTONDOWN: input.button = Btn_M2; break; - case WM_MBUTTONUP: case WM_MBUTTONDOWN: input.button = Btn_M3; break; + case WM_LBUTTONUP: case WM_LBUTTONDOWN: event.button = Btn_M1; break; + case WM_RBUTTONUP: case WM_RBUTTONDOWN: event.button = Btn_M2; break; + case WM_MBUTTONUP: case WM_MBUTTONDOWN: event.button = Btn_M3; break; case WM_XBUTTONUP: case WM_XBUTTONDOWN: { u32 wparam_xbutton = GET_XBUTTON_WPARAM(wparam); if (wparam_xbutton == XBUTTON1) { - input.button = Btn_M4; + event.button = Btn_M4; } else if (wparam_xbutton == XBUTTON2) { - input.button = Btn_M5; + event.button = Btn_M5; } } break; } - if (input.button) + if (event.button) { - WND_W32_PushInput(window, input); + WND_W32_PushEvent(window, event); } } break; @@ -333,8 +333,8 @@ LRESULT CALLBACK WND_W32_WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM l for (i32 i = 0; i < (dir * delta); i += WHEEL_DELTA) { /* Send a button down & button up event simultaneously */ - WND_W32_PushInput(window, (Input) { .kind = InputKind_ButtonDown, .button = btn }); - WND_W32_PushInput(window, (Input) { .kind = InputKind_ButtonUp, .button = btn }); + WND_W32_PushEvent(window, (ControllerEvent) { .kind = ControllerEventKind_ButtonDown, .button = btn }); + WND_W32_PushEvent(window, (ControllerEvent) { .kind = ControllerEventKind_ButtonUp, .button = btn }); } } break; @@ -343,10 +343,10 @@ LRESULT CALLBACK WND_W32_WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM l { i32 x = GET_X_LPARAM(lparam); i32 y = GET_Y_LPARAM(lparam); - Input input = ZI; - input.kind = InputKind_CursorMove; - input.cursor_pos = VEC2I32(x, y); - WND_W32_PushInput(window, input); + ControllerEvent event = ZI; + event.kind = ControllerEventKind_CursorMove; + event.cursor_pos = VEC2I32(x, y); + WND_W32_PushEvent(window, event); } break; //- Raw mouse move @@ -370,10 +370,10 @@ LRESULT CALLBACK WND_W32_WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM l { i32 x = raw.data.mouse.lLastX; i32 y = raw.data.mouse.lLastY; - Input input = ZI; - input.kind = InputKind_MouseMove; - input.mouse_delta = VEC2I32(x, y); - WND_W32_PushInput(window, input); + ControllerEvent event = ZI; + event.kind = ControllerEventKind_MouseMove; + event.mouse_delta = VEC2I32(x, y); + WND_W32_PushEvent(window, event); } } EndScratch(scratch); @@ -424,15 +424,15 @@ WND_Frame WND_BeginFrame(Arena *arena) window->first_cmd = 0; window->last_cmd = 0; - /* Pop inputs */ + /* Pop user input */ { LockTicketMutex(&window->w2u_tm); { - Input *src = (Input *)ArenaBase(window->w2u_inputs_arena); - result.inputs_count = ArenaCount(window->w2u_inputs_arena, Input); - result.inputs = PushStructsNoZero(arena, Input, result.inputs_count); - CopyStructs(result.inputs, src, result.inputs_count); - ResetArena(window->w2u_inputs_arena); + ControllerEvent *src = (ControllerEvent *)ArenaBase(window->w2u_events_arena); + result.controller_events.count = ArenaCount(window->w2u_events_arena, ControllerEvent); + result.controller_events.events = PushStructsNoZero(arena, ControllerEvent, result.controller_events.count); + CopyStructs(result.controller_events.events, src, result.controller_events.count); + ResetArena(window->w2u_events_arena); } UnlockTicketMutex(&window->w2u_tm); } diff --git a/src/window/window_win32/window_win32.h b/src/window/window_win32/window_win32.h index 38ef4b4e..4e1bf0a9 100644 --- a/src/window/window_win32/window_win32.h +++ b/src/window/window_win32/window_win32.h @@ -21,7 +21,7 @@ Struct(WND_W32_Window) /* Window -> User */ /* Reads outside of window thread must lock */ TicketMutex w2u_tm; - Arena *w2u_inputs_arena; + Arena *w2u_events_arena; }; //////////////////////////////////////////////////////////// @@ -82,5 +82,5 @@ JobDecl(WND_W32_ProcessMessagesForever, EmptySig); //////////////////////////////////////////////////////////// //~ Message processing -void WND_W32_PushInput(WND_W32_Window *window, Input input); +void WND_W32_PushEvent(WND_W32_Window *window, ControllerEvent event); LRESULT CALLBACK WND_W32_WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);