From 539949d3961340686958e46b0d3eedcb13095a54 Mon Sep 17 00:00:00 2001 From: "sixten.hugosson" Date: Sat, 17 Jun 2023 23:06:25 +0200 Subject: [PATCH] Corrected dropdown behaviour --- code/build.bat | 6 ++-- code/gen_ui.cpp | 2 +- code/vn.cpp | 2 ++ code/vn_config.cpp | 0 code/vn_config.h | 29 ++++++++++++++++++ code/vn_math.h | 12 ++++++++ code/vn_platform.h | 10 +++++-- code/vn_workspace.cpp | 16 ++++------ code/vn_workspace_view.cpp | 61 ++++++++++++++++++++++++++++++++------ code/win32_main.cpp | 9 +++++- config/config.vn | 0 todo.txt | 2 -- 12 files changed, 120 insertions(+), 29 deletions(-) create mode 100644 code/vn_config.cpp create mode 100644 code/vn_config.h create mode 100644 config/config.vn diff --git a/code/build.bat b/code/build.bat index 30b1ffe..3d0b4d3 100644 --- a/code/build.bat +++ b/code/build.bat @@ -1,13 +1,13 @@ @echo off -set CommonCompilerOptions=/Zi /FC /nologo /DVN_INTERNAL=1 /DVN_SLOW=1 /Oi /W4 /WL /WX /wd4201 /wd4305 /wd4244 /wd4100 /wd4505 /std:c++17 +set CommonCompilerOptions=/Zi /FC /nologo /DVN_INTERNAL=1 /DVN_SLOW=1 /Oi /W4 /WL /WX /wd4996 /wd4201 /wd4305 /wd4244 /wd4100 /wd4505 /std:c++17 if not exist "../build" mkdir "../build" pushd "../build/" -REM cl %CommonCompilerOptions% ../code/gen.cpp -REM gen.exe +rem cl %CommonCompilerOptions% ../code/gen.cpp +rem gen.exe cl %CommonCompilerOptions% ../code/vn.cpp /LD /link /export:VN_UpdateAndRender /incremental:no cl %CommonCompilerOptions% ../code/win32_main.cpp /link user32.lib gdi32.lib winmm.lib opengl32.lib diff --git a/code/gen_ui.cpp b/code/gen_ui.cpp index a3377b2..f37d52c 100644 --- a/code/gen_ui.cpp +++ b/code/gen_ui.cpp @@ -63,7 +63,7 @@ static void GenUI(void) fprintf(Out, "{\n"); fprintf(Out, "\tui *UI = UI_GetState();\n"); fprintf(Out, "\tAssert(UI->Stacks.%sStackUsed > 0);\n", Stack.Name); - fprintf(Out, "\t--UI->Stacks.%sStackUsed;\n", Stack.Name, Stack.Name); + fprintf(Out, "\t--UI->Stacks.%sStackUsed;\n", Stack.Name); fprintf(Out, "}\n"); fprintf(Out, "\n"); diff --git a/code/vn.cpp b/code/vn.cpp index c7d0c72..8d892f2 100644 --- a/code/vn.cpp +++ b/code/vn.cpp @@ -38,6 +38,8 @@ VN_UPDATE_AND_RENDER(VN_UpdateAndRender) State->GlyphAtlas = CreateGlyphAtlas(RenderCommands); + //Config_AddS64(&State->Config, StrLit("Platform/RefreshRate"), &Input->RefreshRate); + Workspace_Init(&State->Workspace); } diff --git a/code/vn_config.cpp b/code/vn_config.cpp new file mode 100644 index 0000000..e69de29 diff --git a/code/vn_config.h b/code/vn_config.h new file mode 100644 index 0000000..f8a98d8 --- /dev/null +++ b/code/vn_config.h @@ -0,0 +1,29 @@ +/* date = June 17th 2023 10:48 pm */ + +#ifndef VN_CONFIG_H +#define VN_CONFIG_H + +struct config_entry +{ + string Name; + buffer Target; + + config_entry *Next; + config_entry *Prev; +}; + +struct config +{ + memory_arena Arena; + + config_entry *First; + config_entry *Last; +}; + +static void Config_ReadFile(config *Config, string Path); +static void Config_WriteFile(config *Config); + +static void Config_AddEntry(config *Config, string Name, buffer Target); +static void Config_AddS64(config *Config, string Name, s64 *Target); + +#endif //VN_CONFIG_H diff --git a/code/vn_math.h b/code/vn_math.h index 68b20cf..1d596e5 100644 --- a/code/vn_math.h +++ b/code/vn_math.h @@ -5,12 +5,24 @@ #include "math.h" +inline r32 Round(r32 Value) +{ + r32 Result = roundf(Value); + return(Result); +} + inline r32 Floor(r32 Value) { r32 Result = floorf(Value); return(Result); } +inline r32 Ceil(r32 Value) +{ + r32 Result = ceilf(Value); + return(Result); +} + inline r32 Pow(r32 Base, r32 Exponent) { r32 Result = powf(Base, Exponent); diff --git a/code/vn_platform.h b/code/vn_platform.h index ed690ab..86981fa 100644 --- a/code/vn_platform.h +++ b/code/vn_platform.h @@ -5,7 +5,7 @@ #include "vn_core.h" -// sixten: Services the platform provides to the game +// sixten: Services the platform provides to the application struct platform_memory_block { @@ -151,7 +151,7 @@ struct platform_event_list static platform_api Platform; -// sixten: Services that the render backend provides to the game +// sixten: Services that the render backend provides to the application #include "vn_render.h" @@ -187,16 +187,20 @@ struct vn_render_commands #include "vn_string.h" #include "vn_thread_context.h" -// sixten: Services the game provides to the platform +// sixten: Services the application provides to the platform struct vn_input { + // sixten: Platform to application platform_event_list *EventList; v2 MouseP; v2 dMouseP; r32 dtForFrame; + + // sixten: Application to platform + s32 TargetRefreshRate; }; struct vn_memory diff --git a/code/vn_workspace.cpp b/code/vn_workspace.cpp index 1e76f6b..6dabdda 100644 --- a/code/vn_workspace.cpp +++ b/code/vn_workspace.cpp @@ -197,22 +197,19 @@ static void Workspace_BuildToolbar(workspace *Workspace, r32 dtForFrame) if(Workspace_BuildMenuItem(FontIcon_None, "Welcome", "").Clicked) { - workspace_view *NewView = Workspace_CreateNewView(Workspace_View_Startup, CurrentPanel); - DLLInsertLast(CurrentPanel->FirstView, CurrentPanel->LastView, NewView); + Workspace_CreateNewView(Workspace_View_Startup, CurrentPanel); Workspace->Menu = ToolbarMenu_None; } if(Workspace_BuildMenuItem(FontIcon_None, "Editor", "").Clicked) { - workspace_view *NewView = Workspace_CreateNewView(Workspace_View_Editor, CurrentPanel); - DLLInsertLast(CurrentPanel->FirstView, CurrentPanel->LastView, NewView); + Workspace_CreateNewView(Workspace_View_Editor, CurrentPanel); Workspace->Menu = ToolbarMenu_None; } if(Workspace_BuildMenuItem(FontIcon_Wrench, "Settings", "").Clicked) { - workspace_view *NewView = Workspace_CreateNewView(Workspace_View_Settings, CurrentPanel); - DLLInsertLast(CurrentPanel->FirstView, CurrentPanel->LastView, NewView); + Workspace_CreateNewView(Workspace_View_Settings, CurrentPanel); Workspace->Menu = ToolbarMenu_None; } @@ -712,8 +709,7 @@ static void Workspace_Init(workspace *Workspace) // sixten(TEMP): Add mock views. { - workspace_view *View1 = Workspace_CreateNewView(Workspace_View_Startup, Workspace->RootPanel); - DLLInsertLast(Workspace->RootPanel->FirstView, Workspace->RootPanel->LastView, View1); + Workspace_CreateNewView(Workspace_View_Startup, Workspace->RootPanel); } // sixten: Setup keybinds @@ -725,8 +721,8 @@ static void Workspace_Init(workspace *Workspace) } } -static void Workspace_Update(workspace *Workspace, vn_render_commands *RenderCommands, - vn_input *Input, glyph_atlas *GlyphAtlas) +static void Workspace_Update(workspace *Workspace, vn_render_commands *RenderCommands, vn_input *Input, + glyph_atlas *GlyphAtlas) { Workspace->EventList = Input->EventList; diff --git a/code/vn_workspace_view.cpp b/code/vn_workspace_view.cpp index c886a7f..d9eb635 100644 --- a/code/vn_workspace_view.cpp +++ b/code/vn_workspace_view.cpp @@ -15,6 +15,13 @@ inline workspace_view *Workspace_CreateNewView(workspace_view_type Type, workspa { View->Data = PushSize(&View->Arena, sizeof(workspace_view_settings)); } break; } + DLLInsertLast(Parent->FirstView, Parent->LastView, View); + + if(View->Parent) + { + Parent->CurrentView = View; + } + return(View); } @@ -239,14 +246,18 @@ static void Workspace_BuildSettingsTabButton(workspace_view_settings *Settings, } } -static void UI_DropdownSelection(char **Alternatives, s32 AlternativeCount, b32 *Open, s32 *Selected) +static b32 UI_DropdownSelection(char **Alternatives, s32 AlternativeCount, b32 *Open, s32 *Selected) { + b32 Result = false; + UI_SetNextLayoutAxis(Axis2_X); UI_Parent(UI_MakeBoxF(0, "")) { UI_LabelF("Refresh Rate:"); UI_Spacer(UI_Pixels(10, 1)); + b32 ActiveInDropdown = false; + UI_SetNextWidth(UI_Pixels(200, 1)); UI_SetNextCornerRadius(4); UI_SetNextLayoutAxis(Axis2_X); @@ -263,35 +274,63 @@ static void UI_DropdownSelection(char **Alternatives, s32 AlternativeCount, b32 UI_Width(UI_Pixels(25, 1)) UI_Font(Font_Icons) UI_LabelF("%U", FontIcon_DownDir); } - ui_signal Signal = UI_SignalFromBox(DropdownBox); - if(Signal.Pressed) + ui_signal DropdownSignal = UI_SignalFromBox(DropdownBox); + if(DropdownSignal.Pressed) { - *Open = true; + *Open = !(*Open); } - if(*Open) + if(AreEqual(UI_GetActive(), DropdownBox->Key)) + { + ActiveInDropdown = true; + } + + r32 OpenTransition = AnimationCurve_AnimateValueF(*Open, 0, 0.175, "UI Dropdown %p%p", Alternatives, Open); + + if(OpenTransition > 0.1) { UI_Tooltip { UI_SetNextFixedP(V2(DropdownBox->Rect.Min.x, DropdownBox->Rect.Max.y)); UI_SetNextCornerRadius(4); UI_SetNextWidth(UI_Pixels(200, 1)); - UI_SetNextHeight(UI_ChildrenSum(AnimationCurve_AnimateValueF(1, 0, 0.3, "UI Dropdown %p%p", Alternatives, Open), 1)); + UI_SetNextHeight(UI_ChildrenSum(OpenTransition, 1)); UI_Parent(UI_MakeBoxF(UI_BoxFlag_Clip | UI_BoxFlag_DrawDropShadow | UI_BoxFlag_FloatingX | UI_BoxFlag_FloatingY, "Dropdown Contents")) { + UI_PushWidth(UI_Percent(1, 1)); + for(s64 Index = 0; - Index < AlternativeCount; + Index < Round(AlternativeCount*OpenTransition); ++Index) { - UI_Width(UI_Percent(1, 1)) UI_ButtonF(Alternatives[Index]); + ui_signal ButtonSignal = UI_ButtonF(Alternatives[Index]); + if(AreEqual(UI_GetActive(), ButtonSignal.Box->Key)) + { + ActiveInDropdown = true; + } + + if(ButtonSignal.Pressed) + { + *Selected = Index; + Result = true; + } } + + UI_PopWidth(); } } } + + if(!ActiveInDropdown && !AreEqual(UI_GetActive(), UI_EmptyKey())) + { + *Open = false; + } } + + return(Result); } static void Workspace_BuildSettings(workspace *Workspace, workspace_view *View) @@ -348,7 +387,11 @@ static void Workspace_BuildSettings(workspace *Workspace, workspace_view *View) persist b32 DropdownOpen = false; persist s32 DropdownSelected = 0; - UI_DropdownSelection(Alternatives, ArrayCount(Alternatives), &DropdownOpen, &DropdownSelected); + + if(UI_DropdownSelection(Alternatives, ArrayCount(Alternatives), &DropdownOpen, &DropdownSelected)) + { + DropdownOpen = false; + } UI_Spacer(UI_Pixels(50, 1)); } diff --git a/code/win32_main.cpp b/code/win32_main.cpp index cb2414b..b612852 100644 --- a/code/win32_main.cpp +++ b/code/win32_main.cpp @@ -658,11 +658,18 @@ int WinMain(HINSTANCE Instance, HINSTANCE PreviousInstance, LPSTR CommandLine, i LoadedCode.UpdateAndRender(&ThreadContext, &Memory, &Input, &RenderCommands); } + b32 UseVSync = (Input.TargetRefreshRate == 0); + wglSwapIntervalEXT(UseVSync); + OpenGL_EndFrame(&OpenGLContext, &RenderCommands); wglSwapLayerBuffers(DeviceContext, WGL_SWAP_MAIN_PLANE); } - Win32_EnforceFrameRate(CurrentTime, 144.0); + b32 ShouldLimitFrameRate = (Input.TargetRefreshRate > 0); + if(ShouldLimitFrameRate) + { + Win32_EnforceFrameRate(CurrentTime, Input.TargetRefreshRate); + } } } else diff --git a/config/config.vn b/config/config.vn new file mode 100644 index 0000000..e69de29 diff --git a/todo.txt b/todo.txt index 3baabdf..28efaf3 100644 --- a/todo.txt +++ b/todo.txt @@ -1,8 +1,6 @@ This is a list of things that needs doing in a SUGGESTED order. * UI - - Draggable panels - - Fix weird grey draggable arena between panels at the headers. - Settings / Preferences view. (Including saving and loading of these settings/preferences) * Rendering