Skip to content

Commit

Permalink
Add ui links for connecting container input and output.
Browse files Browse the repository at this point in the history
  • Loading branch information
linuscu committed Aug 23, 2024
1 parent de578f2 commit 4c99ba2
Show file tree
Hide file tree
Showing 4 changed files with 311 additions and 224 deletions.
41 changes: 31 additions & 10 deletions packages/rendering/include/rendering/ui/UIContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ namespace l::ui {
}

// Used in visitors only and parent scale is already premultiplied
ImVec2 Transform(const ImVec2& p, ImVec2 rootPos = ImVec2()) const {
ImVec2 Transform(const ImVec2& p, ImVec2 screenRootPos = ImVec2()) const {
ImVec2 transformed;
transformed.x = rootPos.x + mPosition.x + p.x * mScale;
transformed.y = rootPos.y + mPosition.y + p.y * mScale;
transformed.x = screenRootPos.x + mPosition.x + p.x * mScale;
transformed.y = screenRootPos.y + mPosition.y + p.y * mScale;
return transformed;
}

Expand Down Expand Up @@ -180,14 +180,16 @@ namespace l::ui {
bool Overlap(const ImVec2& p, const ImVec2& pMin, const ImVec2& pMax);
bool Overlap(const ImVec2& p, const ImVec2& pMin, const ImVec2& pMax, const ContainerArea& parent);
bool OverlapScreenRect(const ImVec2& p, const ImVec2& pCenter, const ImVec2& offset, const ContainerArea& parent);
bool OverlapScreenCircle(const ImVec2& p, const ImVec2& pCenter, float radii, const ContainerArea& parent);
bool OverlapCircle(const ImVec2& p, const ImVec2& pCenter, float radii);

class UIContainer;

class UIVisitor {
public:
virtual ~UIVisitor() = default;

virtual bool Active(const InputState&) {
virtual bool Active(UIContainer&, const InputState&) {
return true;
}
virtual bool Visit(UIContainer&, const InputState&, const ContainerArea&) {
Expand All @@ -196,6 +198,9 @@ namespace l::ui {
virtual void Debug(bool on = true) {
mDebug = on;
}
virtual bool ShouldUpdateContainer() {
return false;
}

protected:
bool mDebug = false;
Expand Down Expand Up @@ -240,8 +245,8 @@ namespace l::ui {
mContainer = nullptr;
}

T* get() {
return reinterpret_cast<T*>(mContainer);
UIContainer* get() {
return mContainer;
}

T* operator->() {
Expand All @@ -266,7 +271,7 @@ namespace l::ui {
virtual ~UIContainer() = default;

bool Accept(UIVisitor& visitor, const InputState& input, UITraversalMode mode = UITraversalMode::AllBFS);
virtual bool Accept(UIVisitor& visitor, const InputState& input, const ContainerArea& parent, UITraversalMode mode = UITraversalMode::AllBFS);
virtual bool Accept(UIVisitor& visitor, const InputState& input, const ContainerArea& contentArea, UITraversalMode mode = UITraversalMode::AllBFS);
virtual void Add(UIContainer* container, int32_t i = -1);

template<class T>
Expand All @@ -276,6 +281,17 @@ namespace l::ui {

virtual void Remove(int32_t i);

template<class T>
void Remove(UIHandle<T>& handle) {
for (auto it = mContent.begin(); it != mContent.end();it++) {
auto containerPtr = *it;
if (containerPtr == handle.get()) {
mContent.erase(it);
break;
}
}
}

void Move(ImVec2 localChange);
void Resize(ImVec2 localChange);
void Rescale(float localChange);
Expand All @@ -289,6 +305,7 @@ namespace l::ui {
void SetDisplayName(std::string_view id);
void SetId(std::string_view id);
void SetContainerArea(const ContainerArea& area);
void SetLayoutArea(const ContainerArea& area);
void SetParent(UIContainer* input);
void SetCoParent(UIContainer* input);
UIContainer* GetParent();
Expand All @@ -299,6 +316,7 @@ namespace l::ui {
ImVec2 GetSize(bool untransformed = false);
float GetScale();
ContainerArea& GetContainerArea();
const ContainerArea& GetLayoutArea() const;

void DebugLog();

Expand All @@ -325,9 +343,12 @@ namespace l::ui {
uint32_t mConfigFlags = 0; // Active visitor flags
uint32_t mNotificationFlags = 0; // Notification flags for ux feedback (resizing box animation etc)

UIContainer* mParent;
UIContainer* mCoParent; // when a container is influenced by two parent in a specific way defined by the type of container and the visitor
ContainerArea mAreaT;

UIContainer* mParent = nullptr;
UIContainer* mCoParent = nullptr; // when a container is influenced by two parent in a specific way defined by the type of container and the visitor
std::vector<UIContainer*> mContent;
std::vector<ContainerArea> mContentAreas;
};

enum class UISplitMode {
Expand All @@ -349,7 +370,7 @@ namespace l::ui {
}
~UISplit() = default;

virtual bool Accept(UIVisitor& visitor, const InputState& input, const ContainerArea& parent, UITraversalMode mode);
virtual bool Accept(UIVisitor& visitor, const InputState& input, const ContainerArea& contentArea, UITraversalMode mode);
protected:
UISplitMode mSplitMode;
};
Expand Down
18 changes: 14 additions & 4 deletions packages/rendering/include/rendering/ui/UIVisitors.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,23 @@

namespace l::ui {

class UIUpdate : public UIVisitor {
public:
UIUpdate() {}
~UIUpdate() = default;

virtual bool ShouldUpdateContainer();
};

class UIZoom : public UIVisitor {
public:
virtual bool Active(const InputState& input);
virtual bool Active(UIContainer& container, const InputState& input);
virtual bool Visit(UIContainer& container, const InputState& input, const ContainerArea& parent);
};

class UIDrag : public UIVisitor {
public:
virtual bool Active(const InputState& input);
virtual bool Active(UIContainer& container, const InputState& input);
virtual bool Visit(UIContainer& container, const InputState& input, const ContainerArea& parent);
protected:
bool mDragging = false;
Expand All @@ -31,7 +39,7 @@ namespace l::ui {

class UIMove : public UIVisitor {
public:
virtual bool Active(const InputState& input);
virtual bool Active(UIContainer& container, const InputState& input);
virtual bool Visit(UIContainer& container, const InputState& input, const ContainerArea& parent);
protected:
bool mMoving = false;
Expand Down Expand Up @@ -59,15 +67,17 @@ namespace l::ui {

class UILinkIO : public UIVisitor {
public:
virtual bool Active(UIContainer& container, const InputState& input);

UILinkIO(UICreator* creator = nullptr) : mCreator(creator) {}
~UILinkIO() = default;

virtual bool Visit(UIContainer& container, const InputState& input, const ContainerArea& parent);
protected:
bool mDragging = false;
bool mPossibleLinkImminent = false;
UIHandle<UIContainer> mLinkContainer;
UICreator* mCreator = nullptr;
float mResizeAreaSize = 8.0f;
};

}
Loading

0 comments on commit 4c99ba2

Please sign in to comment.