Fixed nested parent sizes.

main
sixtenhugosson 2023-06-23 14:38:15 +02:00
parent 15bb3ef384
commit c9f125cc56
8 changed files with 155 additions and 133 deletions

View File

@ -80,6 +80,16 @@ typedef intptr_t smm;
#define PointerToU64(x) ((u64)(void *)x) #define PointerToU64(x) ((u64)(void *)x)
#define U64ToPointer(x) ((void *)(u64)x) #define U64ToPointer(x) ((void *)(u64)x)
#define FindIndexOfElement(Index, Array, Default, ToFind)\
Index = Default;\
for(s64 ___ = 0; ___ < ArrayCount(Array); ++___)\
{\
if(Array[___] == ToFind)\
{\
Index = ___; break;\
}\
}\
//- sixten: Min, max & clamp //- sixten: Min, max & clamp
#define Minimum(A, B) (((A)<(B))?(A):(B)) #define Minimum(A, B) (((A)<(B))?(A):(B))

View File

@ -81,8 +81,12 @@ VN_UPDATE_AND_RENDER(VN_UpdateAndRender)
AnimationCurve_NewFrame(&State->AnimationCurveState, Input->dtForFrame); AnimationCurve_NewFrame(&State->AnimationCurveState, Input->dtForFrame);
UI_NewFrame(&State->UI, Input->EventList, Input->MouseP); UI_NewFrame(&State->UI, Input->EventList, Input->MouseP);
Workspace_Update(&State->Workspace, RenderCommands, Input, State->GlyphAtlas); UI_BeginBuild(RenderCommands->RenderDim);
UI_SignalFromBox(UI_GetState()->ContainerNode); // sixten(TODO): Move elsewhere. {
Workspace_Update(&State->Workspace, RenderCommands, Input, State->GlyphAtlas);
}
UI_EndBuild(State->GlyphAtlas);
for(platform_event *Event = Input->EventList->First; for(platform_event *Event = Input->EventList->First;
Event != 0; Event != 0;
@ -90,7 +94,7 @@ VN_UPDATE_AND_RENDER(VN_UpdateAndRender)
{ {
if(Event->Type == PlatformEvent_WindowClose) if(Event->Type == PlatformEvent_WindowClose)
{ {
Config_WriteFile(State->Config); Config_WriteFile(State->Config, StrLit("config.vn"));
Input->ExitRequested = true; Input->ExitRequested = true;
} }

View File

@ -207,7 +207,7 @@ static void Config_ReadFile(config *Config, string Path)
ReleaseScratch(Scratch); ReleaseScratch(Scratch);
} }
static void Config_WriteFile(config *Config) static void Config_WriteFile(config *Config, string Path)
{ {
string_list Out = {}; string_list Out = {};
temporary_memory Scratch = GetScratch(); temporary_memory Scratch = GetScratch();
@ -276,7 +276,7 @@ static void Config_WriteFile(config *Config)
string FinalOut = JoinStringList(&Out, Scratch.Arena); string FinalOut = JoinStringList(&Out, Scratch.Arena);
platform_file_handle Handle = Platform.OpenFile(StrLit("config.vn"), PlatformAccess_Write); platform_file_handle Handle = Platform.OpenFile(Path, PlatformAccess_Write);
if(Handle.IsValid) if(Handle.IsValid)
{ {
Platform.WriteFile(Handle, FinalOut.Data, 0, FinalOut.Count); Platform.WriteFile(Handle, FinalOut.Data, 0, FinalOut.Count);

View File

@ -422,82 +422,6 @@ static ui_signal UI_SignalFromBox(ui_box *Box)
return(Signal); return(Signal);
} }
static void UI_CalculateStandaloneSize(ui_box *Box, axis2 Axis, glyph_atlas *Atlas)
{
for(ui_box *Child = Box->First;
Child != 0;
Child = Child->Next)
{
UI_CalculateStandaloneSize(Child, Axis, Atlas);
}
if(Box->SemanticSize[Axis].Type == UI_SizeType_Pixels)
{
Box->ComputedDim.E[Axis] = Box->SemanticSize[Axis].Value;
}
else if(Box->SemanticSize[Axis].Type == UI_SizeType_TextContent)
{
if(Axis == Axis2_X)
{
Box->ComputedDim.E[Axis] = CalculateRasterizedTextWidth(Atlas, Box->Font, Box->FontSize, Box->String);
}
else if(Axis == Axis2_Y)
{
Box->ComputedDim.E[Axis] = CalculateRasterizedTextHeight(Atlas, Box->Font, Box->FontSize, Box->String);
}
else
{
InvalidCodepath;
}
Box->ComputedDim.E[Axis] += Box->SemanticSize[Axis].Value;
}
}
static void UI_CalculateUpwardsDependentSize(ui_box *Box, axis2 Axis)
{
if(Box->SemanticSize[Axis].Type == UI_SizeType_PercentOfParent)
{
Box->ComputedDim.E[Axis] = Box->Parent->ComputedDim.E[Axis]*Box->SemanticSize[Axis].Value;
}
for(ui_box *Child = Box->First;
Child != 0;
Child = Child->Next)
{
UI_CalculateUpwardsDependentSize(Child, Axis);
}
}
static void UI_CalculateDownwardsDependentSize(ui_box *Box, axis2 Axis)
{
for(ui_box *Child = Box->First;
Child != 0;
Child = Child->Next)
{
UI_CalculateDownwardsDependentSize(Child, Axis);
}
if(Box->SemanticSize[Axis].Type == UI_SizeType_ChildrenSum)
{
for(ui_box *Child = Box->First;
Child;
Child = Child->Next)
{
if(Box->LayoutAxis == Axis)
{
Box->ComputedDim.E[Axis] += Child->ComputedDim.E[Axis];
}
else
{
Box->ComputedDim.E[Axis] = Maximum(Box->ComputedDim.E[Axis], Child->ComputedDim.E[Axis]);
}
Box->ComputedDim.E[Axis] *= Box->SemanticSize[Axis].Value;
}
}
}
static void UI_SolveSizeViolations(ui_box *Box, axis2 Axis) static void UI_SolveSizeViolations(ui_box *Box, axis2 Axis)
{ {
r32 TotalSpace = 0; r32 TotalSpace = 0;
@ -584,16 +508,6 @@ static void UI_SolveSizeViolations(ui_box *Box, axis2 Axis)
} }
} }
} }
for(ui_box *Child = Box->First;
Child;
Child = Child->Next)
{
Child->Rect.Min.E[Axis] = Box->Rect.Min.E[Axis] + Child->ComputedRelativeP.E[Axis];
Child->Rect.Max.E[Axis] = Child->Rect.Min.E[Axis] + Child->ComputedDim.E[Axis];
UI_SolveSizeViolations(Child, Axis);
}
} }
inline void UI_BeginTooltip(void) inline void UI_BeginTooltip(void)
@ -708,6 +622,101 @@ static void UI_DrawBox(ui_box *Box, render_group *Group, glyph_atlas *GlyphAtlas
} }
} }
static r32 UI_CalculateChildrenSum(ui_box *Box, axis2 Axis, glyph_atlas *Atlas);
static r32 UI_CalculateBoxSize(ui_box *Box, axis2 Axis, glyph_atlas *Atlas)
{
r32 Result = 0;
ui_box *Parent = Box->Parent;
switch(Box->SemanticSize[Axis].Type)
{
case UI_SizeType_Pixels:
{
Result = Box->SemanticSize[Axis].Value;
} break;
case UI_SizeType_TextContent:
{
Result = ((Axis == Axis2_X) ?
CalculateRasterizedTextWidth(Atlas, Box->Font, Box->FontSize, Box->String) :
CalculateRasterizedTextHeight(Atlas, Box->Font, Box->FontSize, Box->String)) +
Box->SemanticSize[Axis].Value;
} break;
case UI_SizeType_PercentOfParent:
{
if(Parent && Parent->SemanticSize[Axis].Type != UI_SizeType_ChildrenSum)
{
Result = Parent->ComputedDim.E[Axis]*Box->SemanticSize[Axis].Value;
}
else
{
// sixten(NOTE): A box that depends on the size of the parent can not be allowed if the parent depends
// on the children sum.
InvalidCodepath;
}
} break;
case UI_SizeType_ChildrenSum:
{
Result = UI_CalculateChildrenSum(Box, Axis, Atlas)*Box->SemanticSize[Axis].Value;
} break;
InvalidDefaultCase;
}
return(Result);
}
static r32 UI_CalculateChildrenSum(ui_box *Box, axis2 Axis, glyph_atlas *Atlas)
{
r32 Result = 0;
if(Box->LayoutAxis == Axis)
{
for(ui_box *Child = Box->First;
Child != 0;
Child = Child->Next)
{
Result += UI_CalculateBoxSize(Child, Axis, Atlas);
}
}
else
{
for(ui_box *Child = Box->First;
Child != 0;
Child = Child->Next)
{
Result = Max(Result, UI_CalculateBoxSize(Child, Axis, Atlas));
}
}
return(Result);
}
static void UI_LayoutBox(ui_box *Box, axis2 Axis, glyph_atlas *Atlas)
{
for(ui_box *Child = Box->First;
Child != 0;
Child = Child->Next)
{
Child->ComputedDim.E[Axis] = UI_CalculateBoxSize(Child, Axis, Atlas);
}
UI_SolveSizeViolations(Box, Axis);
for(ui_box *Child = Box->First;
Child;
Child = Child->Next)
{
Child->Rect.Min.E[Axis] = Box->Rect.Min.E[Axis] + Child->ComputedRelativeP.E[Axis];
Child->Rect.Max.E[Axis] = Child->Rect.Min.E[Axis] + Child->ComputedDim.E[Axis];
UI_LayoutBox(Child, Axis, Atlas);
}
}
static void UI_BeginBuild(v2 ScreenDim) static void UI_BeginBuild(v2 ScreenDim)
{ {
ui *UI = UI_GetState(); ui *UI = UI_GetState();
@ -741,6 +750,8 @@ static void UI_EndBuild(glyph_atlas *GlyphAtlas)
{ {
ui *UI = UI_GetState(); ui *UI = UI_GetState();
UI_SignalFromBox(UI_GetState()->ContainerNode);
if(UI->NextHotSet) if(UI->NextHotSet)
{ {
UI->Hot = UI->NextHot; UI->Hot = UI->NextHot;
@ -760,17 +771,8 @@ static void UI_EndBuild(glyph_atlas *GlyphAtlas)
UI_PopFont(); UI_PopFont();
UI_PopFontSize(); UI_PopFontSize();
UI_CalculateStandaloneSize(UI->RootNode, Axis2_X, GlyphAtlas); UI_LayoutBox(UI->RootNode, Axis2_X, GlyphAtlas);
UI_CalculateStandaloneSize(UI->RootNode, Axis2_Y, GlyphAtlas); UI_LayoutBox(UI->RootNode, Axis2_Y, GlyphAtlas);
UI_CalculateUpwardsDependentSize(UI->RootNode, Axis2_X);
UI_CalculateUpwardsDependentSize(UI->RootNode, Axis2_Y);
UI_CalculateDownwardsDependentSize(UI->RootNode, Axis2_X);
UI_CalculateDownwardsDependentSize(UI->RootNode, Axis2_Y);
UI_SolveSizeViolations(UI->RootNode, Axis2_X);
UI_SolveSizeViolations(UI->RootNode, Axis2_Y);
} }
static void UI_RenderFrame(render_group *Group, glyph_atlas *GlyphAtlas) static void UI_RenderFrame(render_group *Group, glyph_atlas *GlyphAtlas)

View File

@ -178,7 +178,7 @@ static ui_signal UI_Checkbox(string String, b32 *Checked)
v4 TextColor = UI_TopTextColor(); v4 TextColor = UI_TopTextColor();
TextColor.a = OpacityTransition; TextColor.a = OpacityTransition;
UI_Size(UI_Em(1, 1), UI_Em(1, 1)) UI_Font(Font_Icons) UI_TextColor(TextColor) UI_CornerRadius(2) UI_Size(UI_Em(1, 1), UI_Em(1, 1)) UI_Font(Font_Icons) UI_TextColor(TextColor)
UI_MakeBoxF(UI_BoxFlag_DrawBorder|UI_BoxFlag_DrawText, "%U", FontIcon_Cancel); UI_MakeBoxF(UI_BoxFlag_DrawBorder|UI_BoxFlag_DrawText, "%U", FontIcon_Cancel);
UI_Size(UI_TextContent(15, 1), UI_Em(1, 1)) UI_Label(String); UI_Size(UI_TextContent(15, 1), UI_Em(1, 1)) UI_Label(String);
@ -189,9 +189,11 @@ static ui_signal UI_Checkbox(string String, b32 *Checked)
{ {
Platform.SetCursor(PlatformCursor_Hand); Platform.SetCursor(PlatformCursor_Hand);
} }
if(Signal.Pressed) if(Signal.Pressed)
{ {
*Checked = !*Checked; *Checked = !*Checked;
} }
return(Signal); return(Signal);
} }

View File

@ -419,8 +419,6 @@ static void Workspace_BuildPanelHeader(workspace *Workspace, workspace_panel *Pa
Workspace_BuildTabItem(Workspace, Panel, View); Workspace_BuildTabItem(Workspace, Panel, View);
} }
UI_Spacer(UI_Percent(1, 0));
// sixten: Panel Close Button // sixten: Panel Close Button
if(Panel != Workspace->RootPanel) if(Panel != Workspace->RootPanel)
{ {
@ -705,10 +703,7 @@ static void Workspace_Init(workspace *Workspace)
{ {
Workspace->RootPanel = Workspace->CurrentPanel = Workspace_CreateNewPanel(Workspace, 0); Workspace->RootPanel = Workspace->CurrentPanel = Workspace_CreateNewPanel(Workspace, 0);
// sixten(TEMP): Add mock views. Workspace_CreateNewView(Workspace_View_Startup, Workspace->RootPanel);
{
Workspace_CreateNewView(Workspace_View_Startup, Workspace->RootPanel);
}
// sixten: Setup keybinds // sixten: Setup keybinds
{ {
@ -739,7 +734,6 @@ static void Workspace_Update(workspace *Workspace, vn_render_commands *RenderCom
} }
// sixten: Build the UI. // sixten: Build the UI.
UI_BeginBuild(RenderCommands->RenderDim);
{ {
Workspace_BuildToolbar(Workspace, Input->dtForFrame); Workspace_BuildToolbar(Workspace, Input->dtForFrame);
@ -749,5 +743,4 @@ static void Workspace_Update(workspace *Workspace, vn_render_commands *RenderCom
Workspace_BuildDragPayload(Workspace, Input); Workspace_BuildDragPayload(Workspace, Input);
} }
UI_EndBuild(GlyphAtlas);
} }

View File

@ -361,6 +361,8 @@ static void Workspace_BuildSettings(workspace *Workspace, workspace_view *View)
UI_Spacer(UI_Pixels(30, 1)); UI_Spacer(UI_Pixels(30, 1));
Workspace_BuildSettingsTabButton(Settings, "General", Workspace_Settings_General); Workspace_BuildSettingsTabButton(Settings, "General", Workspace_Settings_General);
UI_Spacer(UI_Pixels(30, 1)); UI_Spacer(UI_Pixels(30, 1));
Workspace_BuildSettingsTabButton(Settings, "Theme", Workspace_Settings_Theme);
UI_Spacer(UI_Pixels(30, 1));
Workspace_BuildSettingsTabButton(Settings, "Developer", Workspace_Settings_Developer); Workspace_BuildSettingsTabButton(Settings, "Developer", Workspace_Settings_Developer);
UI_Spacer(UI_Pixels(150, 1)); UI_Spacer(UI_Pixels(150, 1));
@ -384,38 +386,44 @@ static void Workspace_BuildSettings(workspace *Workspace, workspace_view *View)
UI_Font(Font_Bold) UI_FontSize(36) UI_LabelF("General"); UI_Font(Font_Bold) UI_FontSize(36) UI_LabelF("General");
char *Alternatives[] = {"60 Hz", "120 Hz", "144 Hz", "Uncapped", "V-Sync"}; char *Alternatives[] = {"60 Hz", "120 Hz", "144 Hz", "Uncapped", "V-Sync"};
s64 AlternativeMapping[] = {60, 120, 144, -1, 0};
s32 DropdownSelected;
FindIndexOfElement(DropdownSelected, AlternativeMapping, 0, Workspace->Input->RefreshRate);
persist b32 DropdownOpen = false; persist b32 DropdownOpen = false;
s32 DropdownSelected;
switch(Workspace->Input->RefreshRate)
{
case 60: { DropdownSelected = 0; } break;
case 120:{ DropdownSelected = 1; } break;
case 144:{ DropdownSelected = 2; } break;
case -1: { DropdownSelected = 3; } break;
case 0: { DropdownSelected = 4; } break;
default: { DropdownSelected = 0; } break;
}
if(UI_DropdownSelection(Alternatives, ArrayCount(Alternatives), &DropdownOpen, &DropdownSelected)) if(UI_DropdownSelection(Alternatives, ArrayCount(Alternatives), &DropdownOpen, &DropdownSelected))
{ {
switch(DropdownSelected) Workspace->Input->RefreshRate = AlternativeMapping[DropdownSelected];
{
case 0: { Workspace->Input->RefreshRate = 60; } break;
case 1: { Workspace->Input->RefreshRate = 120; } break;
case 2: { Workspace->Input->RefreshRate = 144; } break;
case 3: { Workspace->Input->RefreshRate = -1; } break;
case 4: { Workspace->Input->RefreshRate = 0; } break;
InvalidDefaultCase;
}
DropdownOpen = false; DropdownOpen = false;
} }
UI_Spacer(UI_Pixels(50, 1)); UI_Spacer(UI_Pixels(50, 1));
} }
if(!Category || (Category == Workspace_Settings_Theme))
{
UI_Font(Font_Bold) UI_FontSize(36) UI_LabelF("Theme");
UI_SetNextSize(UI_Percent(1, 1), UI_ChildrenSum(1, 1));
UI_SetNextCornerRadius(3);
UI_Parent(UI_MakeBoxF(UI_BoxFlag_DrawBorder, "Theme Lister"))
UI_Size(UI_Percent(1, 1), UI_Em(2, 1))
{
UI_ButtonF("Hello");
UI_ButtonF("Line");
UI_ButtonF("Paint");
UI_ButtonF("Color");
UI_ButtonF("Design");
UI_ButtonF("Address");
UI_ButtonF("Brightness");
}
UI_Spacer(UI_Pixels(50, 1));
}
if(!Category || (Category == Workspace_Settings_Developer)) if(!Category || (Category == Workspace_Settings_Developer))
{ {
UI_Font(Font_Bold) UI_FontSize(36) UI_LabelF("Developer"); UI_Font(Font_Bold) UI_FontSize(36) UI_LabelF("Developer");
@ -437,7 +445,7 @@ static void Workspace_BuildView(workspace *Workspace, workspace_view *View)
r32 ViewHighlightTransition = r32 ViewHighlightTransition =
AnimationCurve_AnimateValueF(Workspace_ViewIsCurrent(Workspace, View), 0, 0.25, "Workspace View Highlight %p", View); AnimationCurve_AnimateValueF(Workspace_ViewIsCurrent(Workspace, View), 0, 0.25, "Workspace View Highlight %p", View);
UI_SetNextBorderColor(LinearBlend(Theme_BorderColor, Theme_HighlightBorderColor, ViewHighlightTransition)); UI_SetNextBorderColor(LinearBlend(Theme_BorderColor, Theme_HighlightBorderColor, ViewHighlightTransition));
UI_SetNextBackgroundColor(Theme_BackgroundColor); UI_PushBackgroundColor(Theme_BackgroundColor);
UI_SetNextCornerRadius(3); UI_SetNextCornerRadius(3);
ui_box *ViewBox = UI_MakeBoxF(UI_BoxFlag_Clickable | ui_box *ViewBox = UI_MakeBoxF(UI_BoxFlag_Clickable |
@ -476,5 +484,7 @@ static void Workspace_BuildView(workspace *Workspace, workspace_view *View)
} }
} }
UI_PopBackgroundColor();
UI_SignalFromBox(ViewBox); UI_SignalFromBox(ViewBox);
} }

View File

@ -46,6 +46,7 @@ enum workspace_settings_category
{ {
Workspace_Settings_All, Workspace_Settings_All,
Workspace_Settings_General, Workspace_Settings_General,
Workspace_Settings_Theme,
Workspace_Settings_Developer, Workspace_Settings_Developer,
}; };