Skip to content

Commit

Permalink
Introduce layout config for auto sizing to parent size.
Browse files Browse the repository at this point in the history
  • Loading branch information
linuscu committed Aug 22, 2024
1 parent e439b84 commit 34cd864
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 22 deletions.
24 changes: 16 additions & 8 deletions packages/rendering/include/rendering/ui/UIContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -152,16 +159,16 @@ 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;
}

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;
}
};
Expand Down Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions packages/rendering/include/rendering/ui/UICreator.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace l::ui {
else if constexpr (std::is_same_v<T, UISplit>) {
id += "UISplit";
}
else if constexpr (std::is_same_v<T, UILayout>) {
else if constexpr (std::is_same_v<T, UILayoutH>) {
id += "UILayout";
}
else {
Expand All @@ -45,7 +45,7 @@ namespace l::ui {
UICreator() = default;
~UICreator() = default;

UIHandle<UIContainer> CreateContainer(uint32_t flags, UIRenderType renderType = UIRenderType::Rect, UIAlignH alignH = UIAlignH::Left, UIAlignV alignV = UIAlignV::Top, UILayout layout = UILayout::Fixed);
UIHandle<UIContainer> 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<UISplit> CreateSplit(uint32_t flags, bool horizontalSplit = true);

protected:
Expand Down
38 changes: 28 additions & 10 deletions packages/rendering/source/common/ui/UIContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/rendering/source/common/ui/UICreator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace l::ui {

UIHandle<UIContainer> UICreator::CreateContainer(uint32_t flags, UIRenderType renderType, UIAlignH alignH, UIAlignV alignV, UILayout layout) {
std::unique_ptr<UIContainer> container = std::make_unique<UIContainer>(flags, renderType, alignH, alignV, layout);
UIHandle<UIContainer> UICreator::CreateContainer(uint32_t flags, UIRenderType renderType, UIAlignH alignH, UIAlignV alignV, UILayoutH layoutH, UILayoutV layoutV) {
std::unique_ptr<UIContainer> container = std::make_unique<UIContainer>(flags, renderType, alignH, alignV, layoutH, layoutV);

std::string id = CreateUniqueId<UIContainer>();
container->SetId(id);
Expand Down
7 changes: 7 additions & 0 deletions packages/rendering/source/common/ui/UIVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 34cd864

Please sign in to comment.