From aa34bbefc39b7cc7d271ed6d549d73bfb47f4b02 Mon Sep 17 00:00:00 2001 From: Doyle Thai Date: Sat, 20 Aug 2016 22:30:02 +1000 Subject: [PATCH] Refactor window data to struct --- src/Common.c | 8 ++++ src/WorldTraveller.c | 60 +++++++++++++++++------------ src/include/Dengine/Common.h | 1 + src/include/Dengine/UserInterface.h | 15 ++++++++ 4 files changed, 59 insertions(+), 25 deletions(-) diff --git a/src/Common.c b/src/Common.c index 73b0728..fb66c51 100644 --- a/src/Common.c +++ b/src/Common.c @@ -21,6 +21,14 @@ i32 common_strcmp(const char *a, const char *b) return ((*a < *b) ? -1 : 1); } +char *common_strncpy(char *dest, const char *src, i32 numChars) +{ + for (i32 i = 0; i < numChars; i++) + dest[i] = src[i]; + + return dest; +} + char *common_memset(char *const ptr, const i32 value, const i32 numBytes) { for (i32 i = 0; i < numBytes; i++) diff --git a/src/WorldTraveller.c b/src/WorldTraveller.c index 2b38718..97e6e04 100644 --- a/src/WorldTraveller.c +++ b/src/WorldTraveller.c @@ -343,9 +343,18 @@ void worldTraveller_gameInit(GameState *state, v2 windowSize) state->uiState.keyMod = keycode_null; state->uiState.keyChar = keycode_null; + common_strncpy(state->uiState.statMenuState.title, "Stat Menu", + common_strlen("Stat Menu")); + state->uiState.statMenuState.id = 99; + state->uiState.statMenuState.rect.pos = V2(300, 400); + state->uiState.statMenuState.rect.size = V2(300, 400); + state->uiState.statMenuState.prevFrameWindowHeld = FALSE; + state->uiState.statMenuState.windowHeld = FALSE; + state->config.playWorldAudio = FALSE; state->config.showDebugDisplay = TRUE; + assetInit(state); rendererInit(state, windowSize); entityInit(state, windowSize); @@ -1022,7 +1031,6 @@ typedef struct BattleState GLOBAL_VAR BattleState battleState = {0}; -v2 lastFrameP = {0, 0}; void worldTraveller_gameUpdateAndRender(GameState *state, f32 dt) { if (dt >= 1.0f) dt = 1.0f; @@ -1348,45 +1356,47 @@ void worldTraveller_gameUpdateAndRender(GameState *state, f32 dt) if (toggleShowingStatMenu) { - i32 statMenuID = 99; - + WindowState *statMenu = &state->uiState.statMenuState; // TODO(doyle): Define pushing/placing text within a coordinate system, // i.e. relative to an elements position - char *menuTitle = "Stat Menu"; - - LOCAL_PERSIST Rect inventoryRect = {{300, 400}, {300, 400}}; - LOCAL_PERSIST lastFrameWindowWasHeld = FALSE; - b32 windowClickedAndHeld = userInterface_window( &state->uiState, &state->arena, assetManager, renderer, font, - state->input, statMenuID, inventoryRect, menuTitle); + state->input, statMenu->id, statMenu->rect, statMenu->title); - if (windowClickedAndHeld) + // NOTE(doyle): windowClickedAndHeld captures mouse click within the UI + // bounds, but if the user drags the mouse outside the bounds quicker + // than the game updates then we use a second flag which only + // "unclicks" when the mouse is let go + if (windowClickedAndHeld) statMenu->windowHeld = TRUE; + + if (statMenu->windowHeld) { - if (lastFrameWindowWasHeld) + if (!state->input.keys[keycode_mouseLeft].endedDown) + { + statMenu->windowHeld = FALSE; + statMenu->prevFrameWindowHeld = FALSE; + } + } + + if (statMenu->windowHeld) + { + // NOTE(doyle): If this is the first window click we don't process + // movement and instead store the frame position to delta from it + if (statMenu->prevFrameWindowHeld) { // NOTE(doyle): Window clicked and held - v2 deltaP = v2_sub(state->input.mouseP, lastFrameP); + v2 deltaP = v2_sub(state->input.mouseP, statMenu->prevFramePos); DEBUG_PUSH_VAR("Delta Pos %4.2f, %4.2f", deltaP, "v2"); - - f32 mouseMoveThreshold = 5.0f; - - if (ABS(deltaP.x) <= mouseMoveThreshold) deltaP.x = 0.0f; - if (ABS(deltaP.y) <= mouseMoveThreshold) deltaP.y = 0.0f; - - inventoryRect.pos = v2_add(deltaP, inventoryRect.pos); + statMenu->rect.pos = v2_add(deltaP, statMenu->rect.pos); } else { - lastFrameWindowWasHeld = TRUE; + statMenu->prevFrameWindowHeld = TRUE; } - lastFrameP = state->input.mouseP; - } - else - { - lastFrameWindowWasHeld = FALSE; + statMenu->prevFramePos = state->input.mouseP; } + } LOCAL_PERSIST i32 scrollValue = 30; diff --git a/src/include/Dengine/Common.h b/src/include/Dengine/Common.h index 90a2f15..91d6260 100644 --- a/src/include/Dengine/Common.h +++ b/src/include/Dengine/Common.h @@ -30,6 +30,7 @@ typedef double f64; i32 common_strlen(const char *const string); i32 common_strcmp(const char *a, const char *b); +char *common_strncpy(char *dest, const char *src, i32 numChars); char *common_memset(char *const ptr, const i32 value, const i32 numBytes); // Max buffer size should be 11 for 32 bit integers diff --git a/src/include/Dengine/UserInterface.h b/src/include/Dengine/UserInterface.h index 2ac9a0d..fa24f5b 100644 --- a/src/include/Dengine/UserInterface.h +++ b/src/include/Dengine/UserInterface.h @@ -27,6 +27,19 @@ typedef struct UiItem enum UiType type; } UiItem; +typedef struct WindowState +{ + i32 id; + char title[64]; + + Rect rect; + v2 prevFramePos; + + b32 prevFrameWindowHeld; + b32 windowHeld; + +} WindowState; + typedef struct UiState { UiItem uiList[128]; @@ -40,6 +53,8 @@ typedef struct UiState enum KeyCode keyEntered; enum KeyCode keyMod; enum KeyCode keyChar; + + WindowState statMenuState; } UiState; i32 userInterface_window(UiState *const uiState, MemoryArena *const arena,