Add capture IO flags to UI, for better input handling

This commit is contained in:
2024-01-07 14:18:53 +01:00
parent b8b36d9cb7
commit 5f4bb3316c
3 changed files with 57 additions and 4 deletions

View File

@@ -66,6 +66,11 @@ typedef struct BzUI {
u64 currFrame; u64 currFrame;
u64 keyIdCount; // Per-frame u64 keyIdCount; // Per-frame
bool captureMouse;
bool captureKeyboard;
bool capturedMouse;
bool capturedKeyboard;
} BzUI; } BzUI;
BzUIKey bzUIKeyNull() { BzUIKey bzUIKeyNull() {
@@ -122,6 +127,9 @@ BzUI *bzUICreate() {
ui->strArena = bzStackAllocCreate(10240); ui->strArena = bzStackAllocCreate(10240);
hmput(ui->nodeMap, ui->root->key, ui->root); hmput(ui->nodeMap, ui->root->key, ui->root);
ui->captureMouse = true;
ui->captureKeyboard = true;
return ui; return ui;
} }
void bzUIDestroy(BzUI *ui) { void bzUIDestroy(BzUI *ui) {
@@ -142,6 +150,27 @@ void bzUIDestroy(BzUI *ui) {
bzFree(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) { void bzUIBegin(BzUI *ui, i32 width, i32 height) {
bzArrayClear(ui->nodeStack); bzArrayClear(ui->nodeStack);
bzArrayPush(ui->nodeStack, ui->root); bzArrayPush(ui->nodeStack, ui->root);
@@ -375,11 +404,20 @@ static void updateNodeInteraction(BzUI *ui, BzUINode *node, Vector2 mouse) {
BZ_ASSERT(node); BZ_ASSERT(node);
bool hovered = CheckCollisionPointRec(mouse, getNodeRect(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) { node->interaction = (BzUIInteraction) {
.pressed = hovered && IsMouseButtonPressed(MOUSE_BUTTON_LEFT), .pressed = hovered && clicked,
.down = hovered && IsMouseButtonDown(MOUSE_BUTTON_LEFT), .down = hovered && clicked,
.released = hovered && IsMouseButtonReleased(MOUSE_BUTTON_LEFT), .released = hovered && clicked,
.clicked = hovered && IsMouseButtonReleased(MOUSE_BUTTON_LEFT), .clicked = hovered && clicked,
.hovering = hovered .hovering = hovered
}; };
@@ -478,6 +516,8 @@ void bzUIEnd(BzUI *ui) {
pruneStale(ui, ui->root); pruneStale(ui, ui->root);
calculateSizes(ui, ui->root); calculateSizes(ui, ui->root);
calculatePositions(ui, ui->root, 0, 0); calculatePositions(ui, ui->root, 0, 0);
ui->capturedMouse = false;
ui->capturedKeyboard = false;
updateNodeInteraction(ui, ui->root, GetMousePosition()); updateNodeInteraction(ui, ui->root, GetMousePosition());
renderNode(ui, ui->root); renderNode(ui, ui->root);

View File

@@ -180,6 +180,15 @@ BzUIKey bzUIKeyFromString(const char *str);
BzUI *bzUICreate(); BzUI *bzUICreate();
void bzUIDestroy(BzUI *ui); 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 bzUIBegin(BzUI *ui, i32 width, i32 height);
void bzUIEnd(BzUI *ui); void bzUIEnd(BzUI *ui);

View File

@@ -296,6 +296,10 @@ void update(float dt, void *userData) {
BZ_ASSERT(game->stackAlloc.allocated == 0); BZ_ASSERT(game->stackAlloc.allocated == 0);
bzStackAllocReset(&game->stackAlloc); bzStackAllocReset(&game->stackAlloc);
ImGuiIO *io = igGetIO();
bzUISetCaptureMouse(UI, !io->WantCaptureMouse);
bzUISetCaptureKeyboard(UI, !io->WantCaptureKeyboard);
updateInputState(input, game->camera, dt); updateInputState(input, game->camera, dt);
switch (game->screen) { switch (game->screen) {