Refactor of layout manager to add support for all menus (dirty unstable)

This commit is contained in:
IlyaShurupov 2024-10-20 12:07:16 +03:00
parent 23fe07d3d3
commit c57041a48e
21 changed files with 274 additions and 167 deletions

View file

@ -26,11 +26,20 @@ namespace tp {
explicit WidgetLayout(Widget* widget) { mWidget = widget; }
virtual ~WidgetLayout() = default;
virtual void updateLayout(bool vertical) {}
public:
// picks own position and size
// modifies only own area
virtual void pickRect(bool vertical) {}
virtual void clampRect() {}
[[nodiscard]] virtual RectF getAvailableChildArea() const;
// arranges all children layouts inside itself
// modifies children areas
virtual void arrangeChildren(bool vertical) {}
// area that any child can have
[[nodiscard]] virtual RectF availableChildArea() const;
// minimal size base on the content of the layout
[[nodiscard]] virtual RectF minContentArea() const;
public:
const Vec2F& getMinSize();
@ -39,6 +48,8 @@ namespace tp {
[[nodiscard]] const Vec2<SizePolicy>& getSizePolicy() const;
void setSizePolicy(SizePolicy x, SizePolicy y);
static WidgetLayout* lay(Widget* w);
public:
[[nodiscard]] const RectF& getArea() const;
[[nodiscard]] RectF getAnimatedArea() const;
@ -49,6 +60,7 @@ namespace tp {
public:
void clampMinMaxSize();
void pickMinimalRect(bool dir);
[[nodiscard]] RangeF pickRange(const RangeF& current, const RangeF& child, const RangeF& parent, bool v) const;
[[nodiscard]] RangeF clampRange(const RangeF& current, const RangeF& child, const RangeF& parent, bool v) const;

View file

@ -61,6 +61,7 @@ namespace tp {
void setSizePolicy(SizePolicy x, SizePolicy y);
void setEnabled(bool val) { mFlags.set(ENABLED, val); }
[[nodiscard]] bool getEnabled() const { return mFlags.get(ENABLED); }
WidgetLayout* getLayout();
[[nodiscard]] const WidgetLayout* getLayout() const;

View file

@ -10,17 +10,21 @@ namespace tp {
public:
explicit BasicLayout(Widget* widget) : WidgetLayout(widget) {}
void updateLayout(bool vertical) override;
void arrangeChildren(bool vertical) override;
void pickRect(bool vertical) override;
void clampRect() override;
[[nodiscard]] RectF getAvailableChildArea() const override;
[[nodiscard]] RectF availableChildArea() const override;
[[nodiscard]] RectF minContentArea() const override;
void setLayoutPolicy(LayoutPolicy layout) { mLayoutPolicy = layout; }
private:
static bool canChange(Widget*, bool);
private:
void adjustLayout(bool vertical);
static halnf changeChildSize(Widget*, halnf diff, bool vertical);
static halnf shrinkLayoutSize(WidgetLayout* widget, halnf diff, bool vertical);
private:
LayoutPolicy mLayoutPolicy = LayoutPolicy::Vertical;

View file

@ -43,8 +43,7 @@ namespace tp {
public:
void pickRect(bool vertical) override {}
void clampRect() override {};
void updateLayout(bool vertical) override;
void arrangeChildren(bool vertical) override;
void adjustChildrenRect();
public:

View file

@ -10,9 +10,8 @@ namespace tp {
public:
explicit OverlayLayout(Widget* widget) : WidgetLayout(widget) {}
void updateLayout(bool vertical) override;
void arrangeChildren(bool vertical) override;
void pickRect(bool vertical) override {}
void clampRect() override {}
};
}

View file

@ -12,8 +12,8 @@ namespace tp {
setSizePolicy(SizePolicy::Expanding, SizePolicy::Expanding);
}
void updateLayout(bool vertical) override;
[[nodiscard]] RectF getAvailableChildArea() const override;
void arrangeChildren(bool vertical) override;
[[nodiscard]] RectF availableChildArea() const override;
private:
void updateWidgetRects(const RectF& area, Widget* content, ScrollableBarWidget* scroller) const;

View file

@ -13,10 +13,8 @@ namespace tp {
}
void process(const EventHandler& events) override;
void draw(Canvas& canvas) override;
[[nodiscard]] bool needsNextFrame() const override;
[[nodiscard]] bool propagateEventsToChildren() const override;
@ -28,35 +26,4 @@ namespace tp {
FloatingLayout* layout();
[[nodiscard]] const FloatingLayout* layout() const;
};
class FloatingMenu : public FloatingWidget {
public:
FloatingMenu();
public:
void addToMenu(Widget* widget) {
widget->setSizePolicy(SizePolicy::Expanding, SizePolicy::Minimal);
mContentWidget.addChild(widget);
}
const List<Widget*>& getContent() {
return mContentWidget.getChildren();
}
void clearChildren() {
mContentWidget.clear();
}
void setText(const std::string& text);
private:
// VerticalLayout mMenuLayout;
Widget mBodyLayout;
Widget mContentWidget;
ScrollableBarWidget mScrollBar;
LabelWidget mHeader;
// ButtonWidget mTestButton;
};
}

View file

@ -0,0 +1,60 @@
#pragma once
#include "FloatingWidget.hpp"
namespace tp {
class CollapsingMenuWidget : public Widget {
public:
CollapsingMenuWidget();
void process(const EventHandler& events) override;
void draw(Canvas& canvas) override;
[[nodiscard]] bool processesEvents() const override { return true; }
public:
void setCollapsed(bool val);
LabelWidget* getHeader() { return &mHeader; }
Widget* getContainer() { return &mContent; }
private:
bool mHover = false;
LabelWidget mHeader;
Widget mContent;
private:
halnf rounding = 5;
RGBA colorHover = RGBA(0.7);
RGBA colorBG = RGBA(0.3);
};
class FloatingScrollableMenu : public FloatingWidget {
public:
FloatingScrollableMenu();
public:
void addToMenu(Widget* widget) {
widget->setSizePolicy(SizePolicy::Expanding, SizePolicy::Minimal);
mContentWidget.addChild(widget);
}
const List<Widget*>& getContent() {
return mContentWidget.getChildren();
}
void clearChildren() {
mContentWidget.clear();
}
void setText(const std::string& text);
private:
// VerticalLayout mMenuLayout;
Widget mBodyLayout;
Widget mContentWidget;
ScrollableBarWidget mScrollBar;
LabelWidget mHeader;
// ButtonWidget mTestButton;
};
}