Add basic scrollbar imgui code

This commit is contained in:
Doyle Thai 2016-08-10 01:43:26 +10:00
parent f85ab5c86d
commit 2d7262aa22
3 changed files with 96 additions and 0 deletions

View File

@ -885,8 +885,24 @@ INTERNAL i32 button(UiState *uiState, AssetManager *assetManager,
} }
RenderTex renderTex = renderer_createNullRenderTex(assetManager); RenderTex renderTex = renderer_createNullRenderTex(assetManager);
/* If no widget has keyboard focus, take it */
if (uiState->kbdItem == 0)
uiState->kbdItem = id;
/* If we have keyboard focus, show it */
if (uiState->kbdItem == id)
{
// Draw outline
renderer_staticRect(renderer, v2_add(V2(-6, -6), rect.pos),
v2_add(V2(20, 20), rect.size), V2(0, 0), 0, renderTex,
V4(1.0f, 0, 0, 1));
}
// Draw shadow
renderer_staticRect(renderer, v2_add(V2(8, 8), rect.pos), rect.size, renderer_staticRect(renderer, v2_add(V2(8, 8), rect.pos), rect.size,
V2(0, 0), 0, renderTex, V4(0, 0, 0, 1)); 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)
@ -906,6 +922,24 @@ INTERNAL i32 button(UiState *uiState, AssetManager *assetManager,
V2(0, 0), 0, renderTex, V4(0.5f, 0.5f, 0.5f, 1)); V2(0, 0), 0, renderTex, V4(0.5f, 0.5f, 0.5f, 1));
} }
// After renderering before click check, see if we need to process keys
if (uiState->kbdItem == id)
{
if (input.space)
{
// Set focus to nothing and let next widget get focus
uiState->kbdItem = 0;
if (input->leftShift)
uiState->kbdItem = uiState->lastWidget;
// Clear key state so next widget doesn't auto grab
input->space = FALSE;
input->leftShift = FALSE;
}
}
uiState->lastWidget = id;
if (!input.mouseLeft && if (!input.mouseLeft &&
uiState->hotItem == id && uiState->hotItem == id &&
uiState->activeItem == id) uiState->activeItem == id)
@ -916,6 +950,56 @@ INTERNAL i32 button(UiState *uiState, AssetManager *assetManager,
return 0; return 0;
} }
INTERNAL i32 scrollBar(UiState *uiState, AssetManager *assetManager,
Renderer *renderer, KeyInput input, i32 id, Rect rect,
i32 *value, i32 max)
{
i32 yPos = ((256 - 16) * (*value)) / max;
Rect scrollRect;
scrollRect.pos = v2_add(rect.pos, V2i(8, 8));
scrollRect.size = V2(16, 255);
if (math_pointInRect(scrollRect, input.mouseP))
{
uiState->hotItem = id;
if (uiState->activeItem == 0 && input.mouseLeft)
uiState->activeItem = id;
}
v2 scrollBarSize = V2(32, 256 + 16);
RenderTex renderTex = renderer_createNullRenderTex(assetManager);
renderer_staticRect(renderer, rect.pos, scrollBarSize, V2(0, 0), 0,
renderTex, V4(0.5f, 0.5f, 0.5f, 1));
if (uiState->hotItem == id || uiState->activeItem == id)
{
renderer_staticRect(renderer, v2_add(V2i(8, 8 + yPos), rect.pos),
V2(16, 16), V2(0, 0), 0, renderTex,
V4(1.0f, 0, 0, 1));
}
else
{
renderer_staticRect(renderer, v2_add(V2i(8, 8 + yPos), rect.pos),
V2(16, 16), V2(0, 0), 0, renderTex,
V4(0, 1.0f, 0, 1));
}
if (uiState->activeItem == id)
{
i32 mousePos = CAST(i32)(input.mouseP.y - (rect.pos.y + 8));
if (mousePos < 0) mousePos = 0;
if (mousePos > 255) mousePos = 255;
i32 v = (mousePos * max) / 255;
if (v != *value)
{
*value = v;
return 1;
}
}
return 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;
@ -1178,6 +1262,11 @@ void worldTraveller_gameUpdateAndRender(GameState *state, f32 dt)
button(&state->uiState, assetManager, renderer, state->input, 3, button(&state->uiState, assetManager, renderer, state->input, 3,
buttonRectC); buttonRectC);
LOCAL_PERSIST i32 scrollValue = 30;
Rect scrollRectA = {V2(900, 500), V2(16, 255)};
scrollBar(&state->uiState, assetManager, renderer, state->input, 3,
scrollRectA, &scrollValue, 160);
// RESET IMGUI // RESET IMGUI
if (!state->input.mouseLeft) state->uiState.activeItem = 0; if (!state->input.mouseLeft) state->uiState.activeItem = 0;
else if (state->uiState.activeItem == 0) state->uiState.activeItem = -1; else if (state->uiState.activeItem == 0) state->uiState.activeItem = -1;

View File

@ -14,6 +14,7 @@ enum KeyCodes
keycode_left, keycode_left,
keycode_right, keycode_right,
keycode_space, keycode_space,
keycode_leftShift,
keycode_mouseLeft, keycode_mouseLeft,
keycode_count, keycode_count,
}; };

View File

@ -17,6 +17,12 @@ typedef struct UiState
{ {
i32 hotItem; i32 hotItem;
i32 activeItem; i32 activeItem;
i32 kbdItem;
i32 keyEntered;
i32 keyMod;
i32 lastWidget;
} UiState; } UiState;
typedef struct World typedef struct World