Skip to content

Commit

Permalink
Fix a few things with uicontainer.
Browse files Browse the repository at this point in the history
  • Loading branch information
linuscu committed Aug 20, 2024
1 parent 502d112 commit f4ba16b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
35 changes: 29 additions & 6 deletions packages/rendering/include/rendering/ui/UIContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@

namespace l::ui {

enum class UIAlignH {
Left = 0,
Center = 1,
Right = 2
};

enum class UIAlignV {
Top = 0,
Middle = 1,
Bottom = 2
};

struct InputState {
ImVec2 mRootPos;
ImVec2 mCurPos;
Expand All @@ -29,10 +41,17 @@ namespace l::ui {
}
};

struct ContainerLayout {
float mBorder = 3.0f;
UIAlignH mAlignH = UIAlignH::Left;
UIAlignV mAlignV = UIAlignV::Top;
};

struct ContainerArea {
ImVec2 mPosition;
ImVec2 mSize;
ImVec2 mSize = ImVec2(20.0f, 20.0f);
float mScale = 1.0f;
ContainerLayout mLayout;

ImVec2 GetPositionAtSize() const {
return ImVec2(mPosition.x + mSize.x, mPosition.y + mSize.y);
Expand Down Expand Up @@ -79,22 +98,25 @@ namespace l::ui {
const uint32_t UIContainer_InputFlag = 0x00000200;
const uint32_t UIContainer_OutputFlag = 0x00000400;

class UIDraw;

class UIContainer {
public:
UIContainer(std::string name, uint32_t flags = 0) : mName(name), mConfigFlags(flags) {}
UIContainer(std::string_view name, uint32_t flags = 0) : mName(name), mConfigFlags(flags) {}
~UIContainer() = default;

bool Accept(UIVisitor& visitor, const InputState& input, uint32_t flags = 0);
bool Accept(UIVisitor& visitor, const InputState& input, const ContainerArea& parent, uint32_t flags = 0);
void Add(UIContainer* container, int32_t i = -1);
void Remove(int32_t i);
virtual bool Accept(UIVisitor& visitor, const InputState& input, uint32_t flags = 0);
virtual bool Accept(UIVisitor& visitor, const InputState& input, const ContainerArea& parent, uint32_t flags = 0);
virtual void Add(UIContainer* container, int32_t i = -1);
virtual void Remove(int32_t i);

void Move(ImVec2 localChange);
void Resize(ImVec2 localChange);
void Rescale(float localChange);
void ClearNotifications();
void Notification(uint32_t flag);
bool HasNotification(uint32_t flag);
bool HasConfigFlag(uint32_t flag);
void SetPosition(ImVec2 p);
void SetSize(ImVec2 s);
void SetContainerArea(const ContainerArea& area);
Expand All @@ -104,6 +126,7 @@ namespace l::ui {
float GetScale();
void DebugLog();

friend UIVisitor;
protected:
std::string mName;
ContainerArea mArea;
Expand Down
24 changes: 16 additions & 8 deletions packages/rendering/source/common/ui/UIContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ namespace l::ui {
current.mScale = mArea.mScale * parent.mScale;

// scale local position with accumulated world scale and add to accumulated world position
current.mPosition.x = (parent.mPosition.x + mArea.mPosition.x * mArea.mScale) * parent.mScale;
current.mPosition.y = (parent.mPosition.y + mArea.mPosition.y * mArea.mScale) * parent.mScale;
current.mPosition.x = parent.mPosition.x + (mArea.mPosition.x * mArea.mScale) * parent.mScale;
current.mPosition.y = parent.mPosition.y + (mArea.mPosition.y * mArea.mScale) * parent.mScale;

// scale local size with accumulated world scale
current.mSize.x = mArea.mSize.x * mArea.mScale * parent.mScale;
Expand Down Expand Up @@ -112,6 +112,10 @@ namespace l::ui {
return (mNotificationFlags & flag) == flag;
}

bool UIContainer::HasConfigFlag(uint32_t flag) {
return (mConfigFlags & flag) == flag;
}

void UIContainer::SetPosition(ImVec2 p) {
mArea.mPosition = p;
}
Expand Down Expand Up @@ -264,6 +268,7 @@ namespace l::ui {
if (mDebugLog) {
container.DebugLog();
}

ImVec2 pTopLeft = container.GetPosition();
ImVec2 pLowRight = container.GetPositionAtSize();

Expand All @@ -275,13 +280,16 @@ namespace l::ui {

mDrawList->AddRect(p1, p2, col, 2.0f, ImDrawFlags_RoundCornersAll, 2.0f);

ImVec2 p3 = parent.Transform(pLowRight, ImVec2(input.mRootPos.x - 3.0f, input.mRootPos.y - 3.0f));
ImVec2 p4 = parent.Transform(pLowRight, ImVec2(input.mRootPos.x + 3.0f, input.mRootPos.y + 3.0f));
if (container.HasNotification(ui::UIContainer_ResizeFlag)) {
p3 = parent.Transform(pLowRight, ImVec2(input.mRootPos.x - 5.0f, input.mRootPos.y - 5.0f));
p4 = parent.Transform(pLowRight, ImVec2(input.mRootPos.x + 5.0f, input.mRootPos.y + 5.0f));
if (container.HasConfigFlag(ui::UIContainer_ResizeFlag)) {
ImVec2 p3 = parent.Transform(pLowRight, ImVec2(input.mRootPos.x - 3.0f, input.mRootPos.y - 3.0f));
ImVec2 p4 = parent.Transform(pLowRight, ImVec2(input.mRootPos.x + 3.0f, input.mRootPos.y + 3.0f));
if (container.HasNotification(ui::UIContainer_ResizeFlag)) {
p3 = parent.Transform(pLowRight, ImVec2(input.mRootPos.x - 5.0f, input.mRootPos.y - 5.0f));
p4 = parent.Transform(pLowRight, ImVec2(input.mRootPos.x + 5.0f, input.mRootPos.y + 5.0f));
}
mDrawList->AddRectFilled(p3, p4, col);
}
mDrawList->AddRectFilled(p3, p4, col);

return false;
}

Expand Down

0 comments on commit f4ba16b

Please sign in to comment.