Refactor window data to struct

This commit is contained in:
Doyle Thai 2016-08-20 22:30:02 +10:00
parent 1df75c54fc
commit aa34bbefc3
4 changed files with 59 additions and 25 deletions

View File

@ -21,6 +21,14 @@ i32 common_strcmp(const char *a, const char *b)
return ((*a < *b) ? -1 : 1); 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) char *common_memset(char *const ptr, const i32 value, const i32 numBytes)
{ {
for (i32 i = 0; i < numBytes; i++) for (i32 i = 0; i < numBytes; i++)

View File

@ -343,9 +343,18 @@ void worldTraveller_gameInit(GameState *state, v2 windowSize)
state->uiState.keyMod = keycode_null; state->uiState.keyMod = keycode_null;
state->uiState.keyChar = 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.playWorldAudio = FALSE;
state->config.showDebugDisplay = TRUE; state->config.showDebugDisplay = TRUE;
assetInit(state); assetInit(state);
rendererInit(state, windowSize); rendererInit(state, windowSize);
entityInit(state, windowSize); entityInit(state, windowSize);
@ -1022,7 +1031,6 @@ typedef struct BattleState
GLOBAL_VAR BattleState battleState = {0}; GLOBAL_VAR BattleState battleState = {0};
v2 lastFrameP = {0, 0};
void worldTraveller_gameUpdateAndRender(GameState *state, f32 dt) void worldTraveller_gameUpdateAndRender(GameState *state, f32 dt)
{ {
if (dt >= 1.0f) dt = 1.0f; if (dt >= 1.0f) dt = 1.0f;
@ -1348,45 +1356,47 @@ void worldTraveller_gameUpdateAndRender(GameState *state, f32 dt)
if (toggleShowingStatMenu) if (toggleShowingStatMenu)
{ {
i32 statMenuID = 99; WindowState *statMenu = &state->uiState.statMenuState;
// TODO(doyle): Define pushing/placing text within a coordinate system, // TODO(doyle): Define pushing/placing text within a coordinate system,
// i.e. relative to an elements position // 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( b32 windowClickedAndHeld = userInterface_window(
&state->uiState, &state->arena, assetManager, renderer, font, &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 // 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"); DEBUG_PUSH_VAR("Delta Pos %4.2f, %4.2f", deltaP, "v2");
statMenu->rect.pos = v2_add(deltaP, statMenu->rect.pos);
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);
} }
else else
{ {
lastFrameWindowWasHeld = TRUE; statMenu->prevFrameWindowHeld = TRUE;
} }
lastFrameP = state->input.mouseP; statMenu->prevFramePos = state->input.mouseP;
}
else
{
lastFrameWindowWasHeld = FALSE;
} }
} }
LOCAL_PERSIST i32 scrollValue = 30; LOCAL_PERSIST i32 scrollValue = 30;

View File

@ -30,6 +30,7 @@ typedef double f64;
i32 common_strlen(const char *const string); i32 common_strlen(const char *const string);
i32 common_strcmp(const char *a, const char *b); 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); char *common_memset(char *const ptr, const i32 value, const i32 numBytes);
// Max buffer size should be 11 for 32 bit integers // Max buffer size should be 11 for 32 bit integers

View File

@ -27,6 +27,19 @@ typedef struct UiItem
enum UiType type; enum UiType type;
} UiItem; } UiItem;
typedef struct WindowState
{
i32 id;
char title[64];
Rect rect;
v2 prevFramePos;
b32 prevFrameWindowHeld;
b32 windowHeld;
} WindowState;
typedef struct UiState typedef struct UiState
{ {
UiItem uiList[128]; UiItem uiList[128];
@ -40,6 +53,8 @@ typedef struct UiState
enum KeyCode keyEntered; enum KeyCode keyEntered;
enum KeyCode keyMod; enum KeyCode keyMod;
enum KeyCode keyChar; enum KeyCode keyChar;
WindowState statMenuState;
} UiState; } UiState;
i32 userInterface_window(UiState *const uiState, MemoryArena *const arena, i32 userInterface_window(UiState *const uiState, MemoryArena *const arena,