From eb1962e05e0e0033f1382f186a8735d7beca877a Mon Sep 17 00:00:00 2001 From: Doyle Thai Date: Thu, 4 Aug 2016 21:17:16 +1000 Subject: [PATCH] IMGUI use data from Gamestate, fix pointInRect bug Wrong if logic causing point-in-rect to be valid if only x or y is valid, where correct logic is both x and y need to be within the rect. --- .clang-format | 3 +- src/Debug.c | 2 +- src/WorldTraveller.c | 72 ++++++++++++++++------------ src/dengine.c | 6 +-- src/include/Dengine/Platform.h | 2 +- src/include/Dengine/WorldTraveller.h | 3 +- 6 files changed, 50 insertions(+), 38 deletions(-) diff --git a/.clang-format b/.clang-format index a600b74..f17055f 100644 --- a/.clang-format +++ b/.clang-format @@ -18,7 +18,7 @@ # AllowAllParametersOfDeclarationOnNextLine: true, AllowShortBlocksOnASingleLine: false, - AllowShortIfStatementsOnASingleLine: false, + AllowShortIfStatementsOnASingleLine: true, AllowShortLoopsOnASingleLine: false, # BinPackArguments: true, @@ -42,4 +42,5 @@ # Cpp11BracedListStyle: true, Standard: Cpp11, + # } diff --git a/src/Debug.c b/src/Debug.c index 7944f54..1ddbf26 100644 --- a/src/Debug.c +++ b/src/Debug.c @@ -432,7 +432,7 @@ void debug_drawUi(GameState *state, f32 dt) DEBUG_PUSH_VAR("FreeEntityIndex: %d", world->freeEntityIndex, "i32"); DEBUG_PUSH_VAR("glDrawArray Calls: %d", GLOBAL_debug.callCount[debugcallcount_drawArrays], "i32"); - DEBUG_PUSH_VAR("Mouse Pos: %06.2f, %06.2f", state->input.mouse, "v2"); + DEBUG_PUSH_VAR("Mouse Pos: %06.2f, %06.2f", state->input.mouseP, "v2"); i32 debug_bAllocated = state->arena.bytesAllocated; DEBUG_PUSH_VAR("TotalMemoryAllocated: %db", debug_bAllocated, "i32"); diff --git a/src/WorldTraveller.c b/src/WorldTraveller.c index ce57f03..95d03ba 100644 --- a/src/WorldTraveller.c +++ b/src/WorldTraveller.c @@ -82,9 +82,6 @@ INTERNAL void rendererInit(GameState *state, v2 windowSize) // TODO(doyle): Remove and implement own random generator! #include #include - -GLOBAL_VAR UiState uiState = {0}; - void worldTraveller_gameInit(GameState *state, v2 windowSize) { AssetManager *assetManager = &state->assetManager; @@ -345,9 +342,6 @@ void worldTraveller_gameInit(GameState *state, v2 windowSize) #endif srand(CAST(u32)(time(NULL))); - - uiState.mouseP = &state->input.mouse; - uiState.mouseIsDown = &state->input.mouseLeft; } INTERNAL inline v4 getEntityScreenRect(Entity entity) @@ -884,28 +878,35 @@ INTERNAL void sortWorldEntityList(World *world, i32 numDeadEntities) INTERNAL b32 pointInRect(Rect rect, v2 point) { - if (point.x < rect.pos.x || point.x > rect.pos.x + rect.size.w || - point.y > rect.pos.y + rect.size.h || point.y < rect.pos.y) - return FALSE; - return TRUE; + b32 outsideOfRectX = FALSE; + if (point.x < rect.pos.x || point.x > (rect.pos.x + rect.size.w)) + outsideOfRectX = TRUE; + + b32 outsideOfRectY = FALSE; + if (point.y < rect.pos.y || point.y > (rect.pos.y + rect.size.h)) + outsideOfRectY = TRUE; + + if (outsideOfRectX || outsideOfRectY) return FALSE; + else return TRUE; } -i32 button(AssetManager *assetManager, Renderer *renderer, i32 id, Rect rect, - v2 mouseP) +INTERNAL i32 button(UiState *uiState, AssetManager *assetManager, + Renderer *renderer, KeyInput input, i32 id, Rect rect) { - if (pointInRect(rect, mouseP)) + if (pointInRect(rect, input.mouseP)) { - uiState.hotItem = id; - if (uiState.activeItem == 0 && uiState.mouseIsDown) - uiState.activeItem = id; + DEBUG_PUSH_STRING("POINT IN RECT"); + uiState->hotItem = id; + if (uiState->activeItem == 0 && input.mouseLeft) + uiState->activeItem = id; } RenderTex renderTex = renderer_createNullRenderTex(assetManager); renderer_staticRect(renderer, v2_add(V2(8, 8), rect.pos), rect.size, V2(0, 0), 0, renderTex, V4(0, 0, 0, 1)); - if (uiState.hotItem == id) + if (uiState->hotItem == id) { - if (uiState.activeItem == id) + if (uiState->activeItem == id) { renderer_staticRect(renderer, v2_add(V2(2, 2), rect.pos), rect.size, V2(0, 0), 0, renderTex, V4(1, 1, 1, 1)); @@ -922,6 +923,13 @@ i32 button(AssetManager *assetManager, Renderer *renderer, i32 id, Rect rect, V2(0, 0), 0, renderTex, V4(0.5f, 0.5f, 0.5f, 1)); } + if (!input.mouseLeft && + uiState->hotItem == id && + uiState->activeItem == id) + { + return 1; + } + return 0; } @@ -1171,21 +1179,25 @@ void worldTraveller_gameUpdateAndRender(GameState *state, f32 dt) hero->stats->busyDuration = 0; } + // INIT IMGUI + state->uiState.hotItem = 0; + /* Draw ui */ - RenderTex nullRenderTex = renderer_createNullRenderTex(assetManager); - renderer_staticRect(renderer, state->input.mouse, hero->hitboxSize, - V2(0, 0), 0, nullRenderTex, V4(0.5f, 0, 0, 0.5f)); + Rect buttonRectA = {V2(300, 500), V2(100, 50)}; + button(&state->uiState, assetManager, renderer, state->input, 1, + buttonRectA); -#if 0 - RenderTex renderTex = renderer_createNullRenderTex(assetManager); - v2 buttonP = V2(500, 500); - v2 buttonSize = V2(100, 100); - renderer_staticRect(renderer, v2_add(V2(8, 8), buttonP), buttonSize, - V2(0, 0), 0, renderTex, V4(0, 0, 0, 1)); -#endif + Rect buttonRectB = {V2(500, 500), V2(100, 50)}; + button(&state->uiState, assetManager, renderer, state->input, 2, + buttonRectB); - Rect buttonRect = {V2(500, 500), V2(100, 100)}; - button(assetManager, renderer, 1, buttonRect, *uiState.mouseP); + Rect buttonRectC = {V2(700, 500), V2(100, 50)}; + button(&state->uiState, assetManager, renderer, state->input, 3, + buttonRectC); + + // RESET IMGUI + if (!state->input.mouseLeft) state->uiState.activeItem = 0; + else if (state->uiState.activeItem == 0) state->uiState.activeItem = -1; /* Draw hero avatar */ TexAtlas *heroAtlas = asset_getTextureAtlas(assetManager, texlist_hero); diff --git a/src/dengine.c b/src/dengine.c index 754178f..cd42f51 100644 --- a/src/dengine.c +++ b/src/dengine.c @@ -51,9 +51,9 @@ INTERNAL void mouseCallback(GLFWwindow *window, double xPos, double yPos) GameState *game = CAST(GameState *)(glfwGetWindowUserPointer(window)); // NOTE(doyle): x(0), y(0) of mouse starts from the top left of window - v2 windowSize = game->renderer.size; - f32 flipYPos = windowSize.h - CAST(f32) yPos; - game->input.mouse = V2(CAST(f32) xPos, flipYPos); + v2 windowSize = game->renderer.size; + f32 flipYPos = windowSize.h - CAST(f32) yPos; + game->input.mouseP = V2(CAST(f32) xPos, flipYPos); } INTERNAL void mouseButtonCallback(GLFWwindow *window, int button, int action, diff --git a/src/include/Dengine/Platform.h b/src/include/Dengine/Platform.h index ee3f5aa..ffb20c1 100644 --- a/src/include/Dengine/Platform.h +++ b/src/include/Dengine/Platform.h @@ -20,7 +20,7 @@ enum KeyCodes typedef struct KeyInput { - v2 mouse; + v2 mouseP; union { b32 keys[keycode_count - 1]; diff --git a/src/include/Dengine/WorldTraveller.h b/src/include/Dengine/WorldTraveller.h index c5bbd31..56d3e3c 100644 --- a/src/include/Dengine/WorldTraveller.h +++ b/src/include/Dengine/WorldTraveller.h @@ -15,8 +15,6 @@ typedef struct UiState { - v2 *mouseP; - b32 *mouseIsDown; i32 hotItem; i32 activeItem; } UiState; @@ -55,6 +53,7 @@ typedef struct GameState AssetManager assetManager; AudioManager audioManager; MemoryArena arena; + UiState uiState; } GameState; void worldTraveller_gameInit(GameState *state, v2 windowSize);