From 34cd86416a403766e8967402a387f7abb29448f0 Mon Sep 17 00:00:00 2001 From: lnd3 Date: Thu, 22 Aug 2024 08:17:14 +0200 Subject: [PATCH] Introduce layout config for auto sizing to parent size. --- .../include/rendering/ui/UIContainer.h | 24 ++++++++---- .../include/rendering/ui/UICreator.h | 4 +- .../source/common/ui/UIContainer.cpp | 38 ++++++++++++++----- .../rendering/source/common/ui/UICreator.cpp | 4 +- .../rendering/source/common/ui/UIVisitors.cpp | 7 ++++ 5 files changed, 55 insertions(+), 22 deletions(-) diff --git a/packages/rendering/include/rendering/ui/UIContainer.h b/packages/rendering/include/rendering/ui/UIContainer.h index b50b2c55..66746740 100644 --- a/packages/rendering/include/rendering/ui/UIContainer.h +++ b/packages/rendering/include/rendering/ui/UIContainer.h @@ -28,7 +28,13 @@ namespace l::ui { Bottom = 2 }; - enum class UILayout { + enum class UILayoutH { + Fixed = 0, + Scaled = 1, + Parent = 2 + }; + + enum class UILayoutV { Fixed = 0, Scaled = 1, Parent = 2 @@ -83,7 +89,8 @@ namespace l::ui { float mBorder = 3.0f; UIAlignH mAlignH = UIAlignH::Left; UIAlignV mAlignV = UIAlignV::Top; - UILayout mLayout = UILayout::Fixed; + UILayoutH mLayoutH = UILayoutH::Fixed; + UILayoutV mLayoutV = UILayoutV::Fixed; }; struct ContainerArea { @@ -141,7 +148,7 @@ namespace l::ui { worldPos.x = parentPos.x + (mPosition.x + mSize.x * 0.5f - contentSize.x * 0.5f) * mScale * parentScale; break; case UIAlignH::Right: - worldPos.x = parentPos.x + (mPosition.x - mLayout.mBorder * 2.0f + mSize.x - contentSize.x) * mScale * parentScale; + worldPos.x = parentPos.x + (mPosition.x - mLayout.mBorder + mSize.x - contentSize.x) * mScale * parentScale; break; } switch (alignV) { @@ -152,7 +159,7 @@ namespace l::ui { worldPos.y = parentPos.y + (mPosition.y + mSize.y * 0.5f - contentSize.y * 0.5f) * mScale * parentScale; break; case UIAlignV::Bottom: - worldPos.y = parentPos.y + (mPosition.y - mLayout.mBorder * 2.0f + mSize.y - contentSize.y) * mScale * parentScale; + worldPos.y = parentPos.y + (mPosition.y - mLayout.mBorder + mSize.y - contentSize.y) * mScale * parentScale; break; } return worldPos; @@ -160,8 +167,8 @@ namespace l::ui { ImVec2 GetWorldSizeLayout(float parentScale) const { ImVec2 worldSize; - worldSize.x = (mSize.x - mLayout.mBorder * 2.0f) * mScale * parentScale; - worldSize.y = (mSize.y - mLayout.mBorder * 2.0f) * mScale * parentScale; + worldSize.x = (mSize.x - mLayout.mBorder) * mScale * parentScale; + worldSize.y = (mSize.y - mLayout.mBorder) * mScale * parentScale; return worldSize; } }; @@ -234,12 +241,13 @@ namespace l::ui { class UIContainer { public: - UIContainer(uint32_t flags = 0, UIRenderType renderType = UIRenderType::Rect, UIAlignH alignH = UIAlignH::Left, UIAlignV alignV = UIAlignV::Top, UILayout layout = UILayout::Fixed) : mConfigFlags(flags) { + UIContainer(uint32_t flags = 0, UIRenderType renderType = UIRenderType::Rect, UIAlignH alignH = UIAlignH::Left, UIAlignV alignV = UIAlignV::Top, UILayoutH layoutH = UILayoutH::Fixed, UILayoutV layoutV = UILayoutV::Fixed) : mConfigFlags(flags) { mArea.mRender.mType = renderType; mArea.mLayout.mBorder = 3.0f; mArea.mLayout.mAlignH = alignH; mArea.mLayout.mAlignV = alignV; - mArea.mLayout.mLayout = layout; + mArea.mLayout.mLayoutH = layoutH; + mArea.mLayout.mLayoutV = layoutV; } ~UIContainer() = default; diff --git a/packages/rendering/include/rendering/ui/UICreator.h b/packages/rendering/include/rendering/ui/UICreator.h index 0108128d..8c1c5fec 100644 --- a/packages/rendering/include/rendering/ui/UICreator.h +++ b/packages/rendering/include/rendering/ui/UICreator.h @@ -28,7 +28,7 @@ namespace l::ui { else if constexpr (std::is_same_v) { id += "UISplit"; } - else if constexpr (std::is_same_v) { + else if constexpr (std::is_same_v) { id += "UILayout"; } else { @@ -45,7 +45,7 @@ namespace l::ui { UICreator() = default; ~UICreator() = default; - UIHandle CreateContainer(uint32_t flags, UIRenderType renderType = UIRenderType::Rect, UIAlignH alignH = UIAlignH::Left, UIAlignV alignV = UIAlignV::Top, UILayout layout = UILayout::Fixed); + UIHandle CreateContainer(uint32_t flags, UIRenderType renderType = UIRenderType::Rect, UIAlignH alignH = UIAlignH::Left, UIAlignV alignV = UIAlignV::Top, UILayoutH layoutH = UILayoutH::Fixed, UILayoutV layoutV = UILayoutV::Fixed); UIHandle CreateSplit(uint32_t flags, bool horizontalSplit = true); protected: diff --git a/packages/rendering/source/common/ui/UIContainer.cpp b/packages/rendering/source/common/ui/UIContainer.cpp index 69723f68..1f82957e 100644 --- a/packages/rendering/source/common/ui/UIContainer.cpp +++ b/packages/rendering/source/common/ui/UIContainer.cpp @@ -157,13 +157,22 @@ namespace l::ui { //current.mPosition = mArea.GetWorldPos(parent.mScale, parent.mPosition); //current.mSize = mArea.GetWorldSize(parent.mScale); auto& layout = GetContainerArea().mLayout; - switch (layout.mLayout) { - case UILayout::Fixed: + switch (layout.mLayoutH) { + case UILayoutH::Fixed: break; - case UILayout::Scaled: + case UILayoutH::Scaled: break; - case UILayout::Parent: - SetSize(parent.GetLocalSize()); + case UILayoutH::Parent: + mArea.mSize.x = parent.GetLocalSize().x; + break; + } + switch (layout.mLayoutV) { + case UILayoutV::Fixed: + break; + case UILayoutV::Scaled: + break; + case UILayoutV::Parent: + mArea.mSize.y = parent.GetLocalSize().y; break; } @@ -194,13 +203,22 @@ namespace l::ui { // an anchor rather than a container, therefore it has to align within it and size auto& layout = GetContainerArea().mLayout; - switch (layout.mLayout) { - case UILayout::Fixed: + switch (layout.mLayoutH) { + case UILayoutH::Fixed: + break; + case UILayoutH::Scaled: + break; + case UILayoutH::Parent: + mArea.mSize.y = parent.GetLocalSize().y; + break; + } + switch (layout.mLayoutV) { + case UILayoutV::Fixed: break; - case UILayout::Scaled: + case UILayoutV::Scaled: break; - case UILayout::Parent: - SetSize(parent.GetLocalSize()); + case UILayoutV::Parent: + mArea.mSize.x = parent.GetLocalSize().x; break; } diff --git a/packages/rendering/source/common/ui/UICreator.cpp b/packages/rendering/source/common/ui/UICreator.cpp index 3f371bbb..66678aca 100644 --- a/packages/rendering/source/common/ui/UICreator.cpp +++ b/packages/rendering/source/common/ui/UICreator.cpp @@ -4,8 +4,8 @@ namespace l::ui { - UIHandle UICreator::CreateContainer(uint32_t flags, UIRenderType renderType, UIAlignH alignH, UIAlignV alignV, UILayout layout) { - std::unique_ptr container = std::make_unique(flags, renderType, alignH, alignV, layout); + UIHandle UICreator::CreateContainer(uint32_t flags, UIRenderType renderType, UIAlignH alignH, UIAlignV alignV, UILayoutH layoutH, UILayoutV layoutV) { + std::unique_ptr container = std::make_unique(flags, renderType, alignH, alignV, layoutH, layoutV); std::string id = CreateUniqueId(); container->SetId(id); diff --git a/packages/rendering/source/common/ui/UIVisitors.cpp b/packages/rendering/source/common/ui/UIVisitors.cpp index e4f6ce8e..992fef71 100644 --- a/packages/rendering/source/common/ui/UIVisitors.cpp +++ b/packages/rendering/source/common/ui/UIVisitors.cpp @@ -166,9 +166,16 @@ bool UIMove::Visit(UIContainer& container, const InputState& input, const Contai case l::ui::UIRenderType::Spline: break; case l::ui::UIRenderType::Text: + + //const ImVec2 text_pos(window->DC.CursorPos.x, window->DC.CursorPos.y + window->DC.CurrLineTextBaseOffset); + //const float wrap_pos_x = window->DC.TextWrapPos; + + // if (window->DC.CurrentColumns) + if (!container.GetDisplayName().empty()) { nameStart = container.GetDisplayName().data(); nameEnd = container.GetDisplayName().data() + container.GetDisplayName().size(); + container.SetSize(ImGui::CalcTextSize(nameStart, nameEnd)); mDrawList->AddText(ImGui::GetDefaultFont(), 13.0f * parent.mScale, p1, color, nameStart, nameEnd); } break;