Refactor layout and size policies - dirty
This commit is contained in:
parent
e71b8a5230
commit
89cca1b90e
8 changed files with 284 additions and 96 deletions
|
|
@ -1,7 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include "SimpleWidgets.hpp"
|
||||
#include "LayoutWidget.hpp"
|
||||
|
||||
namespace tp {
|
||||
class FloatingWidget : public Widget {
|
||||
|
|
@ -10,7 +9,7 @@ namespace tp {
|
|||
setDebug("float", { 0.0, 0.9, 0.1, 1 });
|
||||
|
||||
mSizePolicy = { SizePolicy::Fixed, SizePolicy::Fixed };
|
||||
mLayoutPolicy = LayoutPolicy::Horizontally;
|
||||
mLayoutPolicy = LayoutPolicy::Horizontal;
|
||||
}
|
||||
|
||||
void process(const EventHandler& events) override;
|
||||
|
|
@ -54,8 +53,8 @@ namespace tp {
|
|||
mHeader.setSizePolicy(SizePolicy::Expanding, SizePolicy::Minimal);
|
||||
mBodyLayout.setSizePolicy(SizePolicy::Expanding, SizePolicy::Expanding);
|
||||
|
||||
setLayoutPolicy(LayoutPolicy::Vertically);
|
||||
mBodyLayout.setLayoutPolicy(LayoutPolicy::Vertically);
|
||||
setLayoutPolicy(LayoutPolicy::Vertical);
|
||||
mBodyLayout.setLayoutPolicy(LayoutPolicy::Vertical);
|
||||
}
|
||||
|
||||
public:
|
||||
|
|
|
|||
67
WidgetsNew/public/LayoutPolicies.hpp
Normal file
67
WidgetsNew/public/LayoutPolicies.hpp
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
#pragma once
|
||||
|
||||
#include "Rect.hpp"
|
||||
|
||||
namespace tp {
|
||||
class Widget;
|
||||
|
||||
enum class SizePolicy {
|
||||
Fixed,
|
||||
Expanding,
|
||||
Minimal,
|
||||
};
|
||||
|
||||
enum class LayoutPolicy {
|
||||
Passive,
|
||||
Vertical,
|
||||
Horizontal,
|
||||
};
|
||||
|
||||
class WidgetLayout {
|
||||
public:
|
||||
explicit WidgetLayout(Widget* widget) { mWidget = widget; }
|
||||
|
||||
virtual void adjustWidget() = 0;
|
||||
|
||||
virtual void pickRect();
|
||||
virtual void clampRect();
|
||||
virtual void adjustChildrenRect();
|
||||
void adjustLayout(bool vertical);
|
||||
halnf changeChildSize(Widget*, halnf diff, bool vertical);
|
||||
|
||||
RangeF pickRange(const RangeF& current, const RangeF& children, const RangeF& parent, bool vertical);
|
||||
RangeF clampRange(const RangeF& current, const RangeF& children, const RangeF& parent, bool vertical);
|
||||
|
||||
void clampMinMaxSize();
|
||||
|
||||
RectF getChildrenEnclosure();
|
||||
RectF getParentEnclosure();
|
||||
|
||||
protected:
|
||||
const RectF& getArea();
|
||||
void setArea(const RectF& area);
|
||||
|
||||
private:
|
||||
Widget* mWidget = nullptr;
|
||||
};
|
||||
|
||||
class BasicLayout : public WidgetLayout {
|
||||
public:
|
||||
BasicLayout() = default;
|
||||
|
||||
void adjustWidget() override;
|
||||
|
||||
private:
|
||||
|
||||
|
||||
private:
|
||||
LayoutPolicy mLayoutPolicy = LayoutPolicy::Vertical;
|
||||
Vec2<SizePolicy> mSizePolicy = { SizePolicy::Fixed, SizePolicy::Fixed };
|
||||
|
||||
Vec2F mMinSize = { 50, 50 };
|
||||
Vec2F mMaxSize = { FLT_MAX / 2, FLT_MAX / 2 };
|
||||
|
||||
halnf mLayoutGap = 5;
|
||||
halnf mLayoutMargin = 10;
|
||||
};
|
||||
}
|
||||
|
|
@ -1,56 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include "Widget.hpp"
|
||||
|
||||
namespace tp {
|
||||
class DesktopLayout : public Widget {
|
||||
public:
|
||||
DesktopLayout() { setDebug("desktop layout", { 0, 0, 0.9, 0.9 }); }
|
||||
|
||||
void draw(Canvas& canvas) override {
|
||||
// canvas.rect(getRelativeArea(), RGBA(0, 0, 0, 1));
|
||||
}
|
||||
|
||||
void pickRect() override {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
void adjustChildrenRect() override {
|
||||
// todo : adjust by viewport pos
|
||||
}
|
||||
|
||||
[[nodiscard]] bool processesEvents() const override { return false; }
|
||||
};
|
||||
|
||||
|
||||
class VerticalLayout : public Widget {
|
||||
public:
|
||||
VerticalLayout() {
|
||||
setDebug("vertical", { 0.8, 0.1, 0.1, 0.9 });
|
||||
}
|
||||
|
||||
void draw(Canvas& canvas) override {
|
||||
// canvas.frame(getRelativeArea(), RGBA(1));
|
||||
}
|
||||
|
||||
void adjustChildrenRect() override {
|
||||
auto content = &mChildren;
|
||||
auto area = getRelativeArea();
|
||||
|
||||
auto iterRect = area.shrink(mPadding);
|
||||
iterRect.height = ((iterRect.height + mGap) / (float) content->size()) - mGap;
|
||||
|
||||
for (auto widget :(*content)) {
|
||||
widget->setArea(iterRect);
|
||||
iterRect.y += iterRect.height + mGap;
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] bool processesEvents() const override { return false; }
|
||||
|
||||
private:
|
||||
halnf mGap = 10;
|
||||
halnf mPadding = 10;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "SpringAnimations.hpp"
|
||||
#include "LayoutPolicies.hpp"
|
||||
|
||||
#include "EventHandler.hpp"
|
||||
#include "Graphics.hpp"
|
||||
|
|
@ -13,19 +14,6 @@ namespace tp {
|
|||
class Widget {
|
||||
friend class RootWidget;
|
||||
|
||||
public:
|
||||
enum class SizePolicy {
|
||||
Fixed,
|
||||
Expanding,
|
||||
Minimal,
|
||||
};
|
||||
|
||||
enum class LayoutPolicy {
|
||||
Passive,
|
||||
Vertically,
|
||||
Horizontally,
|
||||
};
|
||||
|
||||
public:
|
||||
Widget();
|
||||
virtual ~Widget() = default;
|
||||
|
|
@ -70,7 +58,7 @@ namespace tp {
|
|||
|
||||
void clampMinMaxSize();
|
||||
|
||||
RectF getChildrenEnclosure();
|
||||
RectF getChildrenEnclosure() const;
|
||||
RectF getParentEnclosure();
|
||||
void adjustLayout(bool vertical);
|
||||
halnf changeChildSize(Widget*, halnf diff, bool vertical);
|
||||
|
|
@ -86,7 +74,7 @@ namespace tp {
|
|||
public:
|
||||
[[nodiscard]] RectF getArea() const;
|
||||
[[nodiscard]] RectF getAreaT() const;
|
||||
[[nodiscard]] RectF getAreaCache() const;
|
||||
[[nodiscard]] const RectF& getAreaCache() const;
|
||||
|
||||
[[nodiscard]] RectF getRelativeArea() const;
|
||||
[[nodiscard]] RectF getRelativeAreaT() const;
|
||||
|
|
@ -96,6 +84,8 @@ namespace tp {
|
|||
void setAreaCache(const RectF& area);
|
||||
|
||||
protected:
|
||||
friend class WidgetLayout;
|
||||
|
||||
Widget* mParent = nullptr;
|
||||
|
||||
std::vector<Widget*> mChildren;
|
||||
|
|
@ -119,7 +109,6 @@ namespace tp {
|
|||
RectF mAreaCache;
|
||||
|
||||
// debug
|
||||
int inOutCallbacks = 0;
|
||||
std::string mName = "widget base";
|
||||
Vec2F mLocalPoint;
|
||||
Vec2F mGlobalPoint;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue