Skip to content

Commit

Permalink
fix: ui extension layout issues
Browse files Browse the repository at this point in the history
  • Loading branch information
hoffstadt committed Oct 23, 2024
1 parent 9040146 commit 1059567
Show file tree
Hide file tree
Showing 8 changed files with 589 additions and 270 deletions.
2 changes: 1 addition & 1 deletion extensions/pl_draw_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -1780,7 +1780,7 @@ pl_prepare_font_atlas(plFontAtlas* ptAtlas)
.uHeight = 8u,
.uX = 0u,
.uY = 0u,
.pucBytes = PL_ALLOC(64)
.pucBytes = malloc(64)
};
memset(ptWhiteRect.pucBytes, 255, 64);
pl_sb_push(ptAtlas->_sbtCustomRects, ptWhiteRect);
Expand Down
241 changes: 159 additions & 82 deletions extensions/pl_ui_core.c

Large diffs are not rendered by default.

107 changes: 100 additions & 7 deletions extensions/pl_ui_demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ pl_show_style_editor_window(bool* pbOpen)
pl_separator_text("Window");
pl_slider_float("Horizontal Padding## window", &ptStyle->fWindowHorizontalPadding, 0.0f, 32.0f, 0);
pl_slider_float("Vertical Padding## window", &ptStyle->fWindowVerticalPadding, 0.0f, 32.0f, 0);
pl_slider_int("Border Size## window", &ptStyle->iWindowBorderSize, 0, 1, 0);

pl_vertical_spacing();
pl_separator_text("Scrollbar");
Expand Down Expand Up @@ -174,19 +175,19 @@ pl_show_demo_window(bool* pbOpen)
static const float pfRatios0[] = {1.0f};
pl_layout_row(PL_UI_LAYOUT_ROW_TYPE_DYNAMIC, 0.0f, 1, pfRatios0);

if(pl_collapsing_header("Help", 0))
if(pl_begin_collapsing_header("Help", 0))
{
pl_text("Under construction");
pl_end_collapsing_header();
}

if(pl_collapsing_header("Window Options", 0))
if(pl_begin_collapsing_header("Window Options", 0))
{
pl_text("Under construction");
pl_end_collapsing_header();
}

if(pl_collapsing_header("Widgets", 0))
if(pl_begin_collapsing_header("Widgets", 0))
{
if(pl_tree_node("Basic", 0))
{
Expand Down Expand Up @@ -468,7 +469,7 @@ pl_show_demo_window(bool* pbOpen)
pl_end_collapsing_header();
}

if(pl_collapsing_header("Scrolling", 0))
if(pl_begin_collapsing_header("Scrolling", 0))
{
const float pfRatios2[] = {0.5f, 0.50f};
const float pfRatios3[] = {600.0f};
Expand Down Expand Up @@ -524,7 +525,7 @@ pl_show_demo_window(bool* pbOpen)
pl_end_collapsing_header();
}

if(pl_collapsing_header("Layout Systems", 0))
if(pl_begin_collapsing_header("Layout Systems", 0))
{
pl_text("General Notes");
pl_text(" - systems ordered by increasing flexibility");
Expand All @@ -549,7 +550,7 @@ pl_show_demo_window(bool* pbOpen)
pl_separator_text("Example");
pl_layout_dynamic(fWidgetHeight, (uint32_t)iWidgetCount);
pl_vertical_spacing();
for(int i = 0; i < iWidgetCount * 2 - 1; i++)
for(int i = 0; i < iWidgetCount * 2; i++)
{
pl_sb_sprintf(gptCtx->sbcTempBuffer, "Button %d", i);
pl_button(gptCtx->sbcTempBuffer);
Expand Down Expand Up @@ -579,7 +580,7 @@ pl_show_demo_window(bool* pbOpen)
pl_separator_text("Example");
pl_layout_static(fWidgetHeight, fWidgetWidth, (uint32_t)iWidgetCount);
pl_vertical_spacing();
for(int i = 0; i < iWidgetCount * 2 - 1; i++)
for(int i = 0; i < iWidgetCount * 2; i++)
{
pl_sb_sprintf(gptCtx->sbcTempBuffer, "Button %d", i);
pl_button(gptCtx->sbcTempBuffer);
Expand Down Expand Up @@ -813,6 +814,98 @@ pl_show_demo_window(bool* pbOpen)

pl_tree_pop();
}

if(pl_tree_node("Misc. Testing", 0))
{
const float pfRatios[] = {1.0f};
const float pfRatios2[] = {0.5f, 0.5f};
const float pfRatios3[] = {0.5f * 0.5f, 0.25f * 0.5f, 0.25f * 0.5f};
pl_layout_row(PL_UI_LAYOUT_ROW_TYPE_DYNAMIC, 0.0f, 2, pfRatios2);
if(pl_begin_collapsing_header("Information", 0))
{
pl_text("Pilot Light %s", PILOT_LIGHT_VERSION);
#ifdef PL_METAL_BACKEND
pl_text("Graphics Backend: Metal");
#elif PL_VULKAN_BACKEND
pl_text("Graphics Backend: Vulkan");
#else
pl_text("Graphics Backend: Unknown");
#endif

pl_layout_row(PL_UI_LAYOUT_ROW_TYPE_DYNAMIC, 0.0f, 3, pfRatios3);
if(pl_begin_collapsing_header("sub0", 0))
{
pl_text("Pilot Light %s", PILOT_LIGHT_VERSION);
pl_end_collapsing_header();
}
if(pl_begin_collapsing_header("sub1", 0))
{
pl_text("Pilot Light %s", PILOT_LIGHT_VERSION);
pl_text("Pilot Light %s", PILOT_LIGHT_VERSION);
pl_end_collapsing_header();
}
if(pl_begin_collapsing_header("sub2", 0))
{
pl_text("Pilot Light %s", PILOT_LIGHT_VERSION);
pl_text("Pilot Light %s", PILOT_LIGHT_VERSION);
pl_text("Pilot Light %s", PILOT_LIGHT_VERSION);
pl_end_collapsing_header();
}

pl_end_collapsing_header();
}
if(pl_begin_collapsing_header("App Options", 0))
{
pl_checkbox("Freeze Culling Camera", NULL);
int iCascadeCount = 2;
pl_slider_int("Sunlight Cascades", &iCascadeCount, 1, 4, 0);

pl_end_collapsing_header();
}

if(pl_begin_collapsing_header("Graphics", 0))
{
pl_checkbox("Freeze Culling Camera", NULL);
int iCascadeCount = 2;
pl_slider_int("Sunlight Cascades", &iCascadeCount, 1, 4, 0);

pl_end_collapsing_header();
}
if(pl_begin_tab_bar("tab bar2", 0))
{
if(pl_begin_tab("tab0000000000", 0))
{
pl_checkbox("Entities", NULL);
pl_end_tab();
}
if(pl_begin_tab("tab1", 0))
{
pl_checkbox("Profiling", NULL);
pl_checkbox("Profiling", NULL);
pl_checkbox("Profiling", NULL);
pl_checkbox("Profiling", NULL);
pl_end_tab();
}
pl_end_tab_bar();
}

pl_layout_row(PL_UI_LAYOUT_ROW_TYPE_DYNAMIC, 0.0f, 1, pfRatios);
if(pl_begin_collapsing_header("Tools", 0))
{
pl_checkbox("Device Memory Analyzer", NULL);
pl_checkbox("Device Memory Analyzer", NULL);
pl_end_collapsing_header();
}

if(pl_begin_collapsing_header("Debug", 0))
{
pl_button("resize");
pl_checkbox("Always Resize", NULL);
pl_end_collapsing_header();
}

pl_tree_pop();
}
pl_end_collapsing_header();
}
}
Expand Down
2 changes: 1 addition & 1 deletion extensions/pl_ui_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pl_load_ui_api(void)
.slider_int_f = pl_slider_int_f,
.drag_float = pl_drag_float,
.drag_float_f = pl_drag_float_f,
.begin_collapsing_header = pl_collapsing_header,
.begin_collapsing_header = pl_begin_collapsing_header,
.end_collapsing_header = pl_end_collapsing_header,
.tree_node = pl_tree_node,
.tree_node_f = pl_tree_node_f,
Expand Down
4 changes: 2 additions & 2 deletions extensions/pl_ui_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,8 @@ typedef struct _plUiClipper
uint32_t uDisplayEnd;

// [INTERNAL]
float _fItemHeight;
float _fStartPosY;
float _fItemHeight;
float _fStartPosY;
} plUiClipper;

#endif // PL_UI_EXT_H
42 changes: 24 additions & 18 deletions extensions/pl_ui_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ typedef struct _plUiStyle
float fIndentSize;
float fWindowHorizontalPadding;
float fWindowVerticalPadding;
int iWindowBorderSize;
float fScrollbarSize;
float fSliderSize;
plVec2 tItemSpacing;
Expand Down Expand Up @@ -256,16 +257,15 @@ typedef struct _plUiLayoutRow
{
plUiLayoutRowType tType; // determines if width/height is relative or absolute (PL_UI_LAYOUT_ROW_TYPE_*)
plUiLayoutSystemType tSystemType; // this rows layout strategy
float fWidth; // widget width (could be relative or absolute)
float fHeight; // widget height (could be relative or absolute)
float fSpecifiedHeight; // height user passed in
float fVerticalOffset; // used by space layout system (system 6) to temporary offset widget placement
float fHorizontalOffset; // offset where the next widget should start from (usually previous widget + item spacing)
float fWidgetWidth; // widget width (could be relative or absolute)
float fWidgetHeight; // widget height (could be relative or absolute)
float fWidgetXOffset; // offset where the next widget should start from (usually previous widget + item spacing)
float fWidgetYOffset; // used by space layout system (system 6) to temporary offset widget placement
uint32_t uColumns; // number of columns in row
uint32_t uCurrentColumn; // current column
float fMaxWidth; // maximum row width (to help set max cursor position)
float fMaxHeight; // maximum row height (to help set next row position + max cursor position)
float fRowStartX; // offset where the row starts from (think of a tab bar being the second widget in the row)
const float* pfSizesOrRatios; // size or rations when using array layout system (system 4)
uint32_t uStaticEntryCount; // number of static entries when using template layout system (system 5)
uint32_t uDynamicEntryCount; // number of dynamic entries when using template layout system (system 5)
Expand Down Expand Up @@ -322,20 +322,22 @@ typedef struct _plUiStorage
// [SECTION] plUiWindow
//-----------------------------------------------------------------------------

typedef struct _plUiCursorStackItem
{
plVec2 tPos;
} plUiCursorStackItem;

typedef struct _plUiTempWindowData
{
float fTitleBarHeight; // titlebar height
plVec2 tCursorStartPos; // position where widgets begin drawing (could be outside window if scrolling)
plVec2 tCursorMaxPos; // maximum cursor position (could be outside window if scrolling)
plVec2 tRowCursorPos; // current row starting position
plUiLayoutRow tLayoutRow; // current layout row to use
uint32_t uTreeDepth; // current level inside trees
float fExtraIndent; // extra indent added by pl_indent
plUiLayoutRow tCurrentLayoutRow; // current layout row to use
plVec2 tRowPos; // current row starting position
float fAccumRowX; // additional indent due to a parent (like tab bar) not being the first item in a row
float fTitleBarHeight; // titlebar height

// template layout system
float fTempMinWidth;
float fTempStaticWidth;
float fTempMinWidth; // template layout system
float fTempStaticWidth; // template layout system
} plUiTempWindowData;

typedef struct _plUiWindow
Expand Down Expand Up @@ -369,6 +371,9 @@ typedef struct _plUiWindow
plUiTempWindowData tTempData; // temporary data reset at the beginning of frame
plUiWindow** sbtChildWindows; // child windows if any (reset every frame)
plUiLayoutRow* sbtRowStack; // row stack for containers to push parents row onto and pop when they exist (reset every frame)
plUiCursorStackItem* sbtCursorStack;
float* sbfAvailableSizeStack;
float* sbfMaxCursorYStack;
plUiLayoutSortLevel* sbtTempLayoutSort; // blah
uint32_t* sbuTempLayoutIndexSort; // blah
plUiLayoutRowEntry* sbtRowTemplateEntries; // row template entries (shared and reset every frame)
Expand Down Expand Up @@ -486,16 +491,17 @@ static bool pl__is_item_hoverable (const plRect* ptBox, uint32

// layouts
static plVec2 pl__calculate_item_size(float fDefaultHeight);
static void pl__advance_cursor (float fWidth, float fHeight);
static void pl__smart_advance_cursor(float fWidth, float fHeight);
static void pl__advance_cursor(plVec2 tOffset);
static void pl__set_cursor(plVec2 tPos);

static inline plVec2
pl__get_cursor_pos(void)
{
plVec2 tResult = {
gptCtx->ptCurrentWindow->tTempData.tRowPos.x + gptCtx->ptCurrentWindow->tTempData.fAccumRowX + gptCtx->ptCurrentWindow->tTempData.tCurrentLayoutRow.fHorizontalOffset + (float)gptCtx->ptCurrentWindow->tTempData.uTreeDepth * gptCtx->tStyle.fIndentSize,
gptCtx->ptCurrentWindow->tTempData.tRowPos.y + gptCtx->ptCurrentWindow->tTempData.tCurrentLayoutRow.fVerticalOffset
return (plVec2) {
gptCtx->ptCurrentWindow->tTempData.tRowCursorPos.x + gptCtx->ptCurrentWindow->tTempData.tLayoutRow.fWidgetXOffset,
gptCtx->ptCurrentWindow->tTempData.tRowCursorPos.y + gptCtx->ptCurrentWindow->tTempData.tLayoutRow.fWidgetYOffset
};
return tResult;
}

// misc
Expand Down
Loading

0 comments on commit 1059567

Please sign in to comment.