From 5f4bb3316c994c3ceec7a72b58134e2caf773b81 Mon Sep 17 00:00:00 2001 From: Klemen Plestenjak Date: Sun, 7 Jan 2024 14:18:53 +0100 Subject: [PATCH] Add capture IO flags to UI, for better input handling --- engine/breeze/ui/ui.c | 48 +++++++++++++++++++++++++++++++++++++++---- engine/breeze/ui/ui.h | 9 ++++++++ game/main.c | 4 ++++ 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/engine/breeze/ui/ui.c b/engine/breeze/ui/ui.c index 2955fbb..004fa0a 100644 --- a/engine/breeze/ui/ui.c +++ b/engine/breeze/ui/ui.c @@ -66,6 +66,11 @@ typedef struct BzUI { u64 currFrame; u64 keyIdCount; // Per-frame + + bool captureMouse; + bool captureKeyboard; + bool capturedMouse; + bool capturedKeyboard; } BzUI; BzUIKey bzUIKeyNull() { @@ -122,6 +127,9 @@ BzUI *bzUICreate() { ui->strArena = bzStackAllocCreate(10240); hmput(ui->nodeMap, ui->root->key, ui->root); + + ui->captureMouse = true; + ui->captureKeyboard = true; return ui; } void bzUIDestroy(BzUI *ui) { @@ -142,6 +150,27 @@ void bzUIDestroy(BzUI *ui) { bzFree(ui); } +bool bzUICapturedMouse(BzUI *ui) { + return ui->capturedMouse; +} +bool bzUICapturedKeyboard(BzUI *ui) { + return ui->capturedKeyboard; +} + +bool bzUIGetCaptureMouse(BzUI *ui) { + return ui->captureMouse; +} +bool bzUIGetCaptureKeyboard(BzUI *ui) { + return ui->captureKeyboard; +} + +void bzUISetCaptureMouse(BzUI *ui, bool state) { + ui->captureMouse = state; +} +void bzUISetCaptureKeyboard(BzUI *ui, bool state) { + ui->captureKeyboard = state; +} + void bzUIBegin(BzUI *ui, i32 width, i32 height) { bzArrayClear(ui->nodeStack); bzArrayPush(ui->nodeStack, ui->root); @@ -375,11 +404,20 @@ static void updateNodeInteraction(BzUI *ui, BzUINode *node, Vector2 mouse) { BZ_ASSERT(node); bool hovered = CheckCollisionPointRec(mouse, getNodeRect(node)); + bool clicked = IsMouseButtonPressed(MOUSE_BUTTON_LEFT); + if (!ui->captureMouse) { + hovered = false; + clicked = false; + } + if (hovered) { + ui->capturedMouse = true; + } + node->interaction = (BzUIInteraction) { - .pressed = hovered && IsMouseButtonPressed(MOUSE_BUTTON_LEFT), - .down = hovered && IsMouseButtonDown(MOUSE_BUTTON_LEFT), - .released = hovered && IsMouseButtonReleased(MOUSE_BUTTON_LEFT), - .clicked = hovered && IsMouseButtonReleased(MOUSE_BUTTON_LEFT), + .pressed = hovered && clicked, + .down = hovered && clicked, + .released = hovered && clicked, + .clicked = hovered && clicked, .hovering = hovered }; @@ -478,6 +516,8 @@ void bzUIEnd(BzUI *ui) { pruneStale(ui, ui->root); calculateSizes(ui, ui->root); calculatePositions(ui, ui->root, 0, 0); + ui->capturedMouse = false; + ui->capturedKeyboard = false; updateNodeInteraction(ui, ui->root, GetMousePosition()); renderNode(ui, ui->root); diff --git a/engine/breeze/ui/ui.h b/engine/breeze/ui/ui.h index 09783a9..7bb2dea 100644 --- a/engine/breeze/ui/ui.h +++ b/engine/breeze/ui/ui.h @@ -180,6 +180,15 @@ BzUIKey bzUIKeyFromString(const char *str); BzUI *bzUICreate(); void bzUIDestroy(BzUI *ui); +bool bzUICapturedMouse(BzUI *ui); +bool bzUICapturedKeyboard(BzUI *ui); + +bool bzUIGetCaptureMouse(BzUI *ui); +bool bzUIGetCaptureKeyboard(BzUI *ui); + +void bzUISetCaptureMouse(BzUI *ui, bool state); +void bzUISetCaptureKeyboard(BzUI *ui, bool state); + void bzUIBegin(BzUI *ui, i32 width, i32 height); void bzUIEnd(BzUI *ui); diff --git a/game/main.c b/game/main.c index 81110b1..13ef15a 100644 --- a/game/main.c +++ b/game/main.c @@ -296,6 +296,10 @@ void update(float dt, void *userData) { BZ_ASSERT(game->stackAlloc.allocated == 0); bzStackAllocReset(&game->stackAlloc); + ImGuiIO *io = igGetIO(); + bzUISetCaptureMouse(UI, !io->WantCaptureMouse); + bzUISetCaptureKeyboard(UI, !io->WantCaptureKeyboard); + updateInputState(input, game->camera, dt); switch (game->screen) {