Refactor idea done unstable
This commit is contained in:
parent
89cca1b90e
commit
3e37f0cd3d
4 changed files with 160 additions and 382 deletions
|
|
@ -1,10 +1,11 @@
|
||||||
|
#include <algorithm>
|
||||||
#include "LayoutPolicies.hpp"
|
#include "LayoutPolicies.hpp"
|
||||||
|
|
||||||
#include "Widget.hpp"
|
#include "Widget.hpp"
|
||||||
|
|
||||||
using namespace tp;
|
using namespace tp;
|
||||||
|
|
||||||
const RectF& WidgetLayout::getArea() {
|
const RectF& WidgetLayout::getArea() const {
|
||||||
return mWidget->getAreaCache();
|
return mWidget->getAreaCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -12,7 +13,15 @@ void WidgetLayout::setArea(const RectF& area) {
|
||||||
mWidget->setAreaCache(area);
|
mWidget->setAreaCache(area);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WidgetLayout::pickRect() {
|
Widget* WidgetLayout::parent() const { return mWidget->mParent; }
|
||||||
|
|
||||||
|
const std::vector<Widget*>& WidgetLayout::children() const { return mWidget->mChildren; }
|
||||||
|
|
||||||
|
const Vec2F& BasicLayout::getMinSize() { return mMinSize; }
|
||||||
|
|
||||||
|
void BasicLayout::setMinSize(const Vec2F& size) { mMinSize = size; }
|
||||||
|
|
||||||
|
void BasicLayout::pickRect() {
|
||||||
auto current = getArea();
|
auto current = getArea();
|
||||||
auto children = getChildrenEnclosure();
|
auto children = getChildrenEnclosure();
|
||||||
auto parent = getParentEnclosure();
|
auto parent = getParentEnclosure();
|
||||||
|
|
@ -25,7 +34,7 @@ void WidgetLayout::pickRect() {
|
||||||
setArea(newArea);
|
setArea(newArea);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WidgetLayout::clampRect() {
|
void BasicLayout::clampRect() {
|
||||||
auto current = getArea();
|
auto current = getArea();
|
||||||
auto children = getChildrenEnclosure();
|
auto children = getChildrenEnclosure();
|
||||||
auto parent = getParentEnclosure();
|
auto parent = getParentEnclosure();
|
||||||
|
|
@ -36,10 +45,10 @@ void WidgetLayout::clampRect() {
|
||||||
setArea(RectF(rangeX, rangeY));
|
setArea(RectF(rangeX, rangeY));
|
||||||
}
|
}
|
||||||
|
|
||||||
void WidgetLayout::adjustChildrenRect() {
|
void BasicLayout::adjustChildrenRect() {
|
||||||
if (mWidget->mChildren.empty()) return;
|
if (children().empty()) return;
|
||||||
|
|
||||||
switch (mWidget->mLayoutPolicy) {
|
switch (mLayoutPolicy) {
|
||||||
case LayoutPolicy::Passive: break;
|
case LayoutPolicy::Passive: break;
|
||||||
case LayoutPolicy::Vertical: adjustLayout(true); break;
|
case LayoutPolicy::Vertical: adjustLayout(true); break;
|
||||||
case LayoutPolicy::Horizontal: adjustLayout(false); break;
|
case LayoutPolicy::Horizontal: adjustLayout(false); break;
|
||||||
|
|
@ -47,42 +56,45 @@ void WidgetLayout::adjustChildrenRect() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
halnf WidgetLayout::changeChildSize(tp::Widget* widget, halnf diff, bool vertical) {
|
halnf BasicLayout::changeChildSize(tp::Widget* widget, halnf diff, bool vertical) {
|
||||||
auto prevSize = widget->getAreaCache().size[vertical];
|
auto prevSize = widget->getAreaCache().size[vertical];
|
||||||
{
|
{
|
||||||
auto area = widget->getAreaCache();
|
auto area = widget->getAreaCache();
|
||||||
area.size[vertical] += diff;
|
area.size[vertical] += diff;
|
||||||
widget->setAreaCache(area);
|
widget->setAreaCache(area);
|
||||||
widget->clampMinMaxSize();
|
|
||||||
|
// FIXME : widget->clampMinMaxSize();
|
||||||
}
|
}
|
||||||
auto newSize = widget->getAreaCache().size[vertical];
|
auto newSize = widget->getAreaCache().size[vertical];
|
||||||
|
|
||||||
return newSize - prevSize;
|
return newSize - prevSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WidgetLayout::adjustLayout(bool vertical) {
|
void BasicLayout::adjustLayout(bool vertical) {
|
||||||
std::vector<std::pair<Widget*, bool>> contributors;
|
std::vector<std::pair<Widget*, bool>> contributors;
|
||||||
Vec2F contentSize = { 0, 0 };
|
Vec2F contentSize = { 0, 0 };
|
||||||
Vec2F availableSize = getRelativeAreaT().size;
|
Vec2F availableSize = getArea().size;
|
||||||
|
|
||||||
for (auto child : mChildren) {
|
for (auto child : children()) {
|
||||||
if (child->mSizePolicy[!vertical] != SizePolicy::Minimal) {
|
// FIXME :
|
||||||
child->pickRect();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (child->mSizePolicy[vertical] == SizePolicy::Expanding) {
|
// if (child->mSizePolicy[!vertical] != SizePolicy::Minimal) {
|
||||||
|
// child->pickRect();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if (child->mSizePolicy[vertical] == SizePolicy::Expanding) {
|
||||||
contributors.emplace_back( child, true );
|
contributors.emplace_back( child, true );
|
||||||
|
|
||||||
auto area = child->getAreaCache();
|
auto area = child->getAreaCache();
|
||||||
area.size[vertical] = 0;
|
area.size[vertical] = 0;
|
||||||
child->setAreaCache(area);
|
child->setAreaCache(area);
|
||||||
child->clampRect();
|
// child->clampRect();
|
||||||
|
|
||||||
}
|
//}
|
||||||
contentSize += child->getAreaCache().size;
|
contentSize += child->getAreaCache().size;
|
||||||
}
|
}
|
||||||
|
|
||||||
availableSize -= mLayoutGap * ((halnf) mChildren.size() - 1) + mLayoutMargin * 2;
|
availableSize -= mLayoutGap * ((halnf) children().size() - 1) + mLayoutMargin * 2;
|
||||||
|
|
||||||
auto diff = availableSize - contentSize;
|
auto diff = availableSize - contentSize;
|
||||||
|
|
||||||
|
|
@ -111,7 +123,7 @@ void WidgetLayout::adjustLayout(bool vertical) {
|
||||||
|
|
||||||
// arrange
|
// arrange
|
||||||
halnf iterPos = mLayoutMargin;
|
halnf iterPos = mLayoutMargin;
|
||||||
for (auto child : mChildren) {
|
for (auto child : children()) {
|
||||||
auto area = child->getAreaCache();
|
auto area = child->getAreaCache();
|
||||||
area.pos[vertical] = iterPos;
|
area.pos[vertical] = iterPos;
|
||||||
iterPos += area.size[vertical] + mLayoutGap;
|
iterPos += area.size[vertical] + mLayoutGap;
|
||||||
|
|
@ -123,7 +135,7 @@ void WidgetLayout::adjustLayout(bool vertical) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
RangeF WidgetLayout::pickRange(const RangeF& current, const RangeF& children, const RangeF& parent, bool vertical) {
|
RangeF BasicLayout::pickRange(const RangeF& current, const RangeF& children, const RangeF& parent, bool vertical) const {
|
||||||
RangeF out;
|
RangeF out;
|
||||||
|
|
||||||
switch (mSizePolicy[vertical]) {
|
switch (mSizePolicy[vertical]) {
|
||||||
|
|
@ -136,17 +148,17 @@ RangeF WidgetLayout::pickRange(const RangeF& current, const RangeF& children, co
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WidgetLayout::clampMinMaxSize() {
|
void BasicLayout::clampMinMaxSize() {
|
||||||
auto current = getAreaCache();
|
auto current = getArea();
|
||||||
current.size.clamp(mMinSize, mMaxSize);
|
current.size.clamp(mMinSize, mMaxSize);
|
||||||
setAreaCache(current);
|
setArea(current);
|
||||||
}
|
}
|
||||||
|
|
||||||
RangeF WidgetLayout::clampRange(const RangeF& current, const RangeF& children, const RangeF& parent, bool vertical) {
|
RangeF BasicLayout::clampRange(const RangeF& current, const RangeF& child, const RangeF& parent, bool vertical) const {
|
||||||
auto out = current;
|
auto out = current;
|
||||||
|
|
||||||
if (!mChildren.empty()) {
|
if (!children().empty()) {
|
||||||
auto clampedChild = children;
|
auto clampedChild = child;
|
||||||
clampedChild.clamp(parent);
|
clampedChild.clamp(parent);
|
||||||
out.clamp(clampedChild, parent);
|
out.clamp(clampedChild, parent);
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -162,29 +174,33 @@ RangeF WidgetLayout::clampRange(const RangeF& current, const RangeF& children, c
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
RectF WidgetLayout::getChildrenEnclosure() const {
|
RectF BasicLayout::getChildrenEnclosure() const {
|
||||||
RectF out;
|
RectF out;
|
||||||
|
|
||||||
if (mChildren.empty()) {
|
if (children().empty()) {
|
||||||
out = { getAreaCache().center(), { 0, 0 } };
|
out = { getArea().center(), { 0, 0 } };
|
||||||
} else {
|
} else {
|
||||||
out = mChildren.front()->getAreaCache();
|
out = children().front()->getAreaCache();
|
||||||
for (auto child : mChildren) {
|
for (auto child : children()) {
|
||||||
out.expand(child->getAreaCache());
|
out.expand(child->getAreaCache());
|
||||||
}
|
}
|
||||||
out.pos += getAreaCache().pos;
|
out.pos += getArea().pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
RectF WidgetLayout::getParentEnclosure() {
|
RectF BasicLayout::getParentEnclosure() const {
|
||||||
DEBUG_ASSERT(mParent);
|
DEBUG_ASSERT(parent())
|
||||||
if (!mParent) return { { 0, 0 }, mMaxSize };
|
if (!parent()) return { { 0, 0 }, mMaxSize };
|
||||||
|
|
||||||
auto out = mParent->getRelativeAreaT();
|
// FIXME :
|
||||||
if (mParent->mLayoutPolicy != LayoutPolicy::Passive) {
|
// auto out = parent()->getRelativeAreaT();
|
||||||
return out.scaleFromCenter(mParent->mLayoutMargin, true);
|
|
||||||
}
|
// if (parent()->mLayoutPolicy != LayoutPolicy::Passive) {
|
||||||
return out;
|
// return out.scaleFromCenter(parent()->mLayoutMargin, true);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return out;
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
@ -4,104 +4,14 @@
|
||||||
|
|
||||||
using namespace tp;
|
using namespace tp;
|
||||||
|
|
||||||
WidgetManagerInterface* Widget::getRoot() {
|
|
||||||
Widget* iter = mParent;
|
|
||||||
while (iter && iter->mParent) {
|
|
||||||
iter = iter->mParent;
|
|
||||||
}
|
|
||||||
return dynamic_cast<WidgetManagerInterface*>(iter);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Widget::triggerWidgetUpdate(const char* reason) {
|
|
||||||
if (auto root = getRoot()) {
|
|
||||||
root->updateWidget(this, reason);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Widget::Widget() {
|
Widget::Widget() {
|
||||||
mArea.setTargetRect({ 100, 100, 10, 10, });
|
mArea.setTargetRect({ 100, 100, 10, 10 });
|
||||||
|
setLayout(new BasicLayout(this));
|
||||||
mArea.endAnimation();
|
mArea.endAnimation();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Widget::setAreaCache(const tp::RectF& area) {
|
Widget::~Widget() {
|
||||||
mAreaCache = area;
|
delete mLayout;
|
||||||
}
|
|
||||||
|
|
||||||
void Widget::setArea(const RectF& area) {
|
|
||||||
if (mArea.getTargetRect() == area) return;
|
|
||||||
|
|
||||||
mArea.setTargetRect(area);
|
|
||||||
triggerWidgetUpdate("new area");
|
|
||||||
}
|
|
||||||
|
|
||||||
RectF Widget::getArea() const {
|
|
||||||
return mArea.getCurrentRect();
|
|
||||||
}
|
|
||||||
|
|
||||||
RectF Widget::getAreaT() const {
|
|
||||||
return mArea.getTargetRect();
|
|
||||||
}
|
|
||||||
|
|
||||||
const RectF& Widget::getAreaCache() const {
|
|
||||||
return mAreaCache;
|
|
||||||
}
|
|
||||||
|
|
||||||
RectF Widget::getRelativeArea() const {
|
|
||||||
return { {}, mArea.getCurrentRect().size };
|
|
||||||
}
|
|
||||||
|
|
||||||
RectF Widget::getRelativeAreaT() const {
|
|
||||||
return { {}, mArea.getTargetRect().size };
|
|
||||||
}
|
|
||||||
|
|
||||||
RectF Widget::getRelativeAreaCache() const {
|
|
||||||
return { {}, mAreaCache.size };
|
|
||||||
}
|
|
||||||
|
|
||||||
void Widget::endAnimations() {
|
|
||||||
mArea.endAnimation();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Widget::processesEvents() const {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Widget::updateAnimations() {
|
|
||||||
mArea.updateCurrentRect();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Widget::needsNextFrame() const {
|
|
||||||
return !mArea.shouldEndTransition(); // || mInFocus;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Widget::bringToFront() {
|
|
||||||
if (!mParent) return;
|
|
||||||
auto& order = mParent->mDepthOrder;
|
|
||||||
auto node = order.find(this);
|
|
||||||
DEBUG_ASSERT(node)
|
|
||||||
order.detach(node);
|
|
||||||
order.pushFront(node);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Widget::bringToBack() {
|
|
||||||
if (!mParent) return;
|
|
||||||
auto& order = mParent->mDepthOrder;
|
|
||||||
auto node = order.find(this);
|
|
||||||
DEBUG_ASSERT(node)
|
|
||||||
order.detach(node);
|
|
||||||
order.pushBack(node);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Widget::mouseEnter() {
|
|
||||||
mInFocus = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Widget::mouseLeave() {
|
|
||||||
mInFocus = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Widget::propagateEventsToChildren() const {
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Widget::addChild(Widget* child) {
|
void Widget::addChild(Widget* child) {
|
||||||
|
|
@ -134,200 +44,76 @@ void Widget::removeChild(Widget* child) {
|
||||||
child->triggerWidgetUpdate("parent changed");
|
child->triggerWidgetUpdate("parent changed");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Widget::setSizePolicy(SizePolicy x, SizePolicy y) {
|
void Widget::endAnimations() { mArea.endAnimation(); }
|
||||||
mSizePolicy = { x, y };
|
bool Widget::processesEvents() const { return false; }
|
||||||
triggerWidgetUpdate("chane size policy");
|
void Widget::updateAnimations() { mArea.updateCurrentRect(); }
|
||||||
|
bool Widget::needsNextFrame() const { return !mArea.shouldEndTransition(); }
|
||||||
|
|
||||||
|
void Widget::bringToFront() {
|
||||||
|
if (!mParent) return;
|
||||||
|
auto& order = mParent->mDepthOrder;
|
||||||
|
auto node = order.find(this);
|
||||||
|
DEBUG_ASSERT(node)
|
||||||
|
order.detach(node);
|
||||||
|
order.pushFront(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Widget::setLayoutPolicy(LayoutPolicy layoutPolicy) {
|
void Widget::bringToBack() {
|
||||||
mLayoutPolicy = layoutPolicy;
|
if (!mParent) return;
|
||||||
triggerWidgetUpdate("new layout");
|
auto& order = mParent->mDepthOrder;
|
||||||
|
auto node = order.find(this);
|
||||||
|
DEBUG_ASSERT(node)
|
||||||
|
order.detach(node);
|
||||||
|
order.pushBack(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
const Vec2F& Widget::getMinSize() { return mMinSize; }
|
void Widget::mouseEnter() { mInFocus = true; }
|
||||||
|
void Widget::mouseLeave() { mInFocus = false; }
|
||||||
|
bool Widget::propagateEventsToChildren() const { return true; }
|
||||||
|
|
||||||
void Widget::setMinSize(const Vec2F& size) {
|
void Widget::setLayout(tp::WidgetLayout* layout) {
|
||||||
mMinSize = size;
|
mLayout = layout;
|
||||||
triggerWidgetUpdate("new min size");
|
triggerWidgetUpdate("chane layout");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Widget::pickRect() {
|
WidgetLayout* Widget::getLayout() { return mLayout; }
|
||||||
auto current = getAreaCache();
|
|
||||||
auto children = getChildrenEnclosure();
|
|
||||||
auto parent = getParentEnclosure();
|
|
||||||
|
|
||||||
auto rangeX = pickRange(current.getRangeX(), children.getRangeX(), parent.getRangeX(), false);
|
WidgetManagerInterface* Widget::getRoot() {
|
||||||
auto rangeY = pickRange(current.getRangeY(), children.getRangeY(), parent.getRangeY(), true);
|
Widget* iter = mParent;
|
||||||
|
while (iter && iter->mParent) {
|
||||||
auto newArea = RectF(rangeX, rangeY);
|
iter = iter->mParent;
|
||||||
|
|
||||||
for (auto child : mChildren) {
|
|
||||||
child->setAreaCache(RectF(child->getAreaCache()).move(current.pos - newArea.pos));
|
|
||||||
}
|
}
|
||||||
|
return dynamic_cast<WidgetManagerInterface*>(iter);
|
||||||
setAreaCache(newArea);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Widget::clampRect() {
|
void Widget::triggerWidgetUpdate(const char* reason) {
|
||||||
auto current = getAreaCache();
|
if (auto root = getRoot()) {
|
||||||
auto children = getChildrenEnclosure();
|
root->updateWidget(this, reason);
|
||||||
auto parent = getParentEnclosure();
|
|
||||||
|
|
||||||
auto rangeX = clampRange(current.getRangeX(), children.getRangeX(), parent.getRangeX(), false);
|
|
||||||
auto rangeY = clampRange(current.getRangeY(), children.getRangeY(), parent.getRangeY(), true);
|
|
||||||
|
|
||||||
setAreaCache(RectF(rangeX, rangeY));
|
|
||||||
}
|
|
||||||
|
|
||||||
void Widget::adjustChildrenRect() {
|
|
||||||
if (mChildren.empty()) return;
|
|
||||||
|
|
||||||
switch (mLayoutPolicy) {
|
|
||||||
case LayoutPolicy::Passive: break;
|
|
||||||
case LayoutPolicy::Vertical: adjustLayout(true); break;
|
|
||||||
case LayoutPolicy::Horizontal: adjustLayout(false); break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Widget::setAreaCache(const tp::RectF& area) { mAreaCache = area; }
|
||||||
|
|
||||||
halnf Widget::changeChildSize(tp::Widget* widget, halnf diff, bool vertical) {
|
void Widget::setArea(const RectF& area) {
|
||||||
auto prevSize = widget->getAreaCache().size[vertical];
|
if (mArea.getTargetRect() == area) return;
|
||||||
{
|
|
||||||
auto area = widget->getAreaCache();
|
|
||||||
area.size[vertical] += diff;
|
|
||||||
widget->setAreaCache(area);
|
|
||||||
widget->clampMinMaxSize();
|
|
||||||
}
|
|
||||||
auto newSize = widget->getAreaCache().size[vertical];
|
|
||||||
|
|
||||||
return newSize - prevSize;
|
mArea.setTargetRect(area);
|
||||||
|
triggerWidgetUpdate("new area");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Widget::adjustLayout(bool vertical) {
|
RectF Widget::getArea() const { return mArea.getCurrentRect(); }
|
||||||
std::vector<std::pair<Widget*, bool>> contributors;
|
|
||||||
Vec2F contentSize = { 0, 0 };
|
|
||||||
Vec2F availableSize = getRelativeAreaT().size;
|
|
||||||
|
|
||||||
for (auto child : mChildren) {
|
RectF Widget::getAreaT() const { return mArea.getTargetRect(); }
|
||||||
if (child->mSizePolicy[!vertical] != SizePolicy::Minimal) {
|
|
||||||
child->pickRect();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (child->mSizePolicy[vertical] == SizePolicy::Expanding) {
|
const RectF& Widget::getAreaCache() const { return mAreaCache; }
|
||||||
contributors.emplace_back( child, true );
|
|
||||||
|
|
||||||
auto area = child->getAreaCache();
|
RectF Widget::getRelativeArea() const { return { {}, mArea.getCurrentRect().size }; }
|
||||||
area.size[vertical] = 0;
|
|
||||||
child->setAreaCache(area);
|
|
||||||
child->clampRect();
|
|
||||||
|
|
||||||
}
|
RectF Widget::getRelativeAreaT() const { return { {}, mArea.getTargetRect().size }; }
|
||||||
contentSize += child->getAreaCache().size;
|
|
||||||
}
|
|
||||||
|
|
||||||
availableSize -= mLayoutGap * ((halnf) mChildren.size() - 1) + mLayoutMargin * 2;
|
RectF Widget::getRelativeAreaCache() const { return { {}, mAreaCache.size }; }
|
||||||
|
|
||||||
auto diff = availableSize - contentSize;
|
void Widget::setDebug(const char* name, RGBA col) {
|
||||||
|
mDebug.id = name;
|
||||||
// expand or contract as much as possible
|
mDebug.col = col;
|
||||||
while (!contributors.empty() && (diff[vertical] != 0)) {
|
|
||||||
auto quota = diff / (halnf) contributors.size();
|
|
||||||
|
|
||||||
for (auto& contributor : contributors) {
|
|
||||||
if (!contributor.second) continue;
|
|
||||||
|
|
||||||
// contributor.first->endAnimations();
|
|
||||||
auto contribution = changeChildSize(contributor.first, quota[vertical], vertical);
|
|
||||||
|
|
||||||
if (contribution == 0) {
|
|
||||||
contributor.second = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
diff[vertical] -= contribution;
|
|
||||||
}
|
|
||||||
|
|
||||||
contributors.erase(
|
|
||||||
std::remove_if(contributors.begin(), contributors.end(), [](auto node) { return !node.second; }),
|
|
||||||
contributors.end()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// arrange
|
|
||||||
halnf iterPos = mLayoutMargin;
|
|
||||||
for (auto child : mChildren) {
|
|
||||||
auto area = child->getAreaCache();
|
|
||||||
area.pos[vertical] = iterPos;
|
|
||||||
iterPos += area.size[vertical] + mLayoutGap;
|
|
||||||
child->setAreaCache(area);
|
|
||||||
|
|
||||||
|
|
||||||
// child->updateAnimations();
|
|
||||||
// child->triggerWidgetUpdate("layout changed");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
RangeF Widget::pickRange(const RangeF& current, const RangeF& children, const RangeF& parent, bool vertical) {
|
|
||||||
RangeF out;
|
|
||||||
|
|
||||||
switch (mSizePolicy[vertical]) {
|
|
||||||
case SizePolicy::Fixed: out = current; break;
|
|
||||||
case SizePolicy::Expanding: out = parent; break;
|
|
||||||
case SizePolicy::Minimal: out = children; break;
|
|
||||||
}
|
|
||||||
|
|
||||||
out = clampRange(out, children, parent, vertical);
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Widget::clampMinMaxSize() {
|
|
||||||
auto current = getAreaCache();
|
|
||||||
current.size.clamp(mMinSize, mMaxSize);
|
|
||||||
setAreaCache(current);
|
|
||||||
}
|
|
||||||
|
|
||||||
RangeF Widget::clampRange(const RangeF& current, const RangeF& children, const RangeF& parent, bool vertical) {
|
|
||||||
auto out = current;
|
|
||||||
|
|
||||||
if (!mChildren.empty()) {
|
|
||||||
auto clampedChild = children;
|
|
||||||
clampedChild.clamp(parent);
|
|
||||||
out.clamp(clampedChild, parent);
|
|
||||||
} else {
|
|
||||||
out.clamp(parent);
|
|
||||||
}
|
|
||||||
|
|
||||||
// clamp min max sizes
|
|
||||||
auto len = clamp(current.size(), mMinSize[vertical], mMaxSize[vertical]);
|
|
||||||
if (len != current.size()) {
|
|
||||||
out.resizeFromCenter(len);
|
|
||||||
}
|
|
||||||
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
RectF Widget::getChildrenEnclosure() const {
|
|
||||||
RectF out;
|
|
||||||
|
|
||||||
if (mChildren.empty()) {
|
|
||||||
out = { getAreaCache().center(), { 0, 0 } };
|
|
||||||
} else {
|
|
||||||
out = mChildren.front()->getAreaCache();
|
|
||||||
for (auto child : mChildren) {
|
|
||||||
out.expand(child->getAreaCache());
|
|
||||||
}
|
|
||||||
out.pos += getAreaCache().pos;
|
|
||||||
}
|
|
||||||
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
RectF Widget::getParentEnclosure() {
|
|
||||||
DEBUG_ASSERT(mParent);
|
|
||||||
if (!mParent) return { { 0, 0 }, mMaxSize };
|
|
||||||
|
|
||||||
auto out = mParent->getRelativeAreaT();
|
|
||||||
if (mParent->mLayoutPolicy != LayoutPolicy::Passive) {
|
|
||||||
return out.scaleFromCenter(mParent->mLayoutMargin, true);
|
|
||||||
}
|
|
||||||
return out;
|
|
||||||
}
|
}
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Rect.hpp"
|
#include "Rect.hpp"
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace tp {
|
namespace tp {
|
||||||
class Widget;
|
class Widget;
|
||||||
|
|
@ -20,26 +21,17 @@ namespace tp {
|
||||||
class WidgetLayout {
|
class WidgetLayout {
|
||||||
public:
|
public:
|
||||||
explicit WidgetLayout(Widget* widget) { mWidget = widget; }
|
explicit WidgetLayout(Widget* widget) { mWidget = widget; }
|
||||||
|
virtual ~WidgetLayout() = default;
|
||||||
|
|
||||||
virtual void adjustWidget() = 0;
|
virtual void pickRect() = 0;
|
||||||
|
virtual void clampRect() = 0;
|
||||||
virtual void pickRect();
|
virtual void adjustChildrenRect() = 0;
|
||||||
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:
|
protected:
|
||||||
const RectF& getArea();
|
[[nodiscard]] const RectF& getArea() const;
|
||||||
void setArea(const RectF& area);
|
void setArea(const RectF& area);
|
||||||
|
[[nodiscard]] Widget* parent() const;
|
||||||
|
[[nodiscard]] const std::vector<Widget*>& children() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Widget* mWidget = nullptr;
|
Widget* mWidget = nullptr;
|
||||||
|
|
@ -47,12 +39,27 @@ namespace tp {
|
||||||
|
|
||||||
class BasicLayout : public WidgetLayout {
|
class BasicLayout : public WidgetLayout {
|
||||||
public:
|
public:
|
||||||
BasicLayout() = default;
|
explicit BasicLayout(Widget* widget) : WidgetLayout(widget) {}
|
||||||
|
|
||||||
void adjustWidget() override;
|
void pickRect() override;
|
||||||
|
void clampRect() override;
|
||||||
|
void adjustChildrenRect() override;
|
||||||
|
|
||||||
|
public:
|
||||||
|
const Vec2F& getMinSize();
|
||||||
|
void setMinSize(const Vec2F& size);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void adjustLayout(bool vertical);
|
||||||
|
static halnf changeChildSize(Widget*, halnf diff, bool vertical);
|
||||||
|
|
||||||
|
[[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;
|
||||||
|
|
||||||
|
void clampMinMaxSize();
|
||||||
|
|
||||||
|
[[nodiscard]] RectF getChildrenEnclosure() const;
|
||||||
|
[[nodiscard]] RectF getParentEnclosure() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
LayoutPolicy mLayoutPolicy = LayoutPolicy::Vertical;
|
LayoutPolicy mLayoutPolicy = LayoutPolicy::Vertical;
|
||||||
|
|
|
||||||
|
|
@ -10,66 +10,42 @@
|
||||||
|
|
||||||
namespace tp {
|
namespace tp {
|
||||||
class WidgetManagerInterface;
|
class WidgetManagerInterface;
|
||||||
|
class WidgetLayout;
|
||||||
|
|
||||||
class Widget {
|
class Widget {
|
||||||
friend class RootWidget;
|
friend class RootWidget;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Widget();
|
Widget();
|
||||||
virtual ~Widget() = default;
|
virtual ~Widget();
|
||||||
|
|
||||||
void setDebug(const char* name, RGBA col) {
|
|
||||||
mName = name;
|
|
||||||
mDebugColor = col;
|
|
||||||
}
|
|
||||||
|
|
||||||
void addChild(Widget* child);
|
void addChild(Widget* child);
|
||||||
void removeChild(Widget* child);
|
void removeChild(Widget* child);
|
||||||
|
|
||||||
WidgetManagerInterface* getRoot();
|
|
||||||
|
|
||||||
void triggerWidgetUpdate(const char* reason = nullptr);
|
|
||||||
|
|
||||||
void bringToFront();
|
void bringToFront();
|
||||||
void bringToBack();
|
void bringToBack();
|
||||||
|
|
||||||
void setSizePolicy(SizePolicy x, SizePolicy y);
|
void setLayout(WidgetLayout* layout);
|
||||||
void setLayoutPolicy(LayoutPolicy layoutPolicy);
|
WidgetLayout* getLayout();
|
||||||
|
|
||||||
const Vec2F& getMinSize();
|
protected:
|
||||||
void setMinSize(const Vec2F& size);
|
|
||||||
|
|
||||||
public:
|
|
||||||
virtual void process(const EventHandler& events) {}
|
virtual void process(const EventHandler& events) {}
|
||||||
virtual void draw(Canvas& canvas) {}
|
virtual void draw(Canvas& canvas) {}
|
||||||
virtual void drawOverlay(Canvas& canvas) {}
|
virtual void drawOverlay(Canvas& canvas) {}
|
||||||
|
virtual void endAnimations();
|
||||||
|
virtual void updateAnimations();
|
||||||
|
|
||||||
|
[[nodiscard]] virtual bool processesEvents() const;
|
||||||
|
[[nodiscard]] virtual bool propagateEventsToChildren() const;
|
||||||
|
[[nodiscard]] virtual bool needsNextFrame() const;
|
||||||
|
|
||||||
virtual void mouseEnter();
|
virtual void mouseEnter();
|
||||||
virtual void mouseLeave();
|
virtual void mouseLeave();
|
||||||
|
|
||||||
// size policies
|
protected:
|
||||||
virtual void pickRect();
|
void setDebug(const char* name, RGBA col);
|
||||||
virtual void clampRect();
|
WidgetManagerInterface* getRoot();
|
||||||
virtual void adjustChildrenRect();
|
void triggerWidgetUpdate(const char* reason = nullptr);
|
||||||
|
|
||||||
// resizing
|
|
||||||
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() const;
|
|
||||||
RectF getParentEnclosure();
|
|
||||||
void adjustLayout(bool vertical);
|
|
||||||
halnf changeChildSize(Widget*, halnf diff, bool vertical);
|
|
||||||
|
|
||||||
[[nodiscard]] virtual bool processesEvents() const;
|
|
||||||
[[nodiscard]] virtual bool propagateEventsToChildren() const;
|
|
||||||
|
|
||||||
[[nodiscard]] virtual bool needsNextFrame() const;
|
|
||||||
|
|
||||||
virtual void endAnimations();
|
|
||||||
virtual void updateAnimations();
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
[[nodiscard]] RectF getArea() const;
|
[[nodiscard]] RectF getArea() const;
|
||||||
|
|
@ -84,7 +60,7 @@ namespace tp {
|
||||||
void setAreaCache(const RectF& area);
|
void setAreaCache(const RectF& area);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
friend class WidgetLayout;
|
friend WidgetLayout;
|
||||||
|
|
||||||
Widget* mParent = nullptr;
|
Widget* mParent = nullptr;
|
||||||
|
|
||||||
|
|
@ -93,30 +69,23 @@ namespace tp {
|
||||||
|
|
||||||
// relative to the parent
|
// relative to the parent
|
||||||
SpringRect mArea;
|
SpringRect mArea;
|
||||||
|
RectF mAreaCache;
|
||||||
|
|
||||||
// resizing
|
WidgetLayout* mLayout = nullptr;
|
||||||
LayoutPolicy mLayoutPolicy = LayoutPolicy::Passive;
|
|
||||||
Vec2<SizePolicy> mSizePolicy = { SizePolicy::Fixed, SizePolicy::Fixed };
|
|
||||||
Vec2F mMinSize = { 50, 50 };
|
|
||||||
Vec2F mMaxSize = { FLT_MAX / 2, FLT_MAX / 2 };
|
|
||||||
|
|
||||||
bool mInFocus = false;
|
bool mInFocus = false;
|
||||||
|
|
||||||
halnf mLayoutGap = 5;
|
|
||||||
halnf mLayoutMargin = 10;
|
|
||||||
|
|
||||||
// cache
|
|
||||||
RectF mAreaCache;
|
|
||||||
|
|
||||||
// debug
|
// debug
|
||||||
std::string mName = "widget base";
|
struct {
|
||||||
Vec2F mLocalPoint;
|
std::string id = "widget base";
|
||||||
Vec2F mGlobalPoint;
|
RGBA col = { 1, 1, 1, 0.3 };
|
||||||
RGBA mDebugColor = { 1, 1, 1, 0.3 };
|
std::string triggerReason = "none";
|
||||||
std::string mTriggerReason = "none";
|
Vec2F pLocal;
|
||||||
|
Vec2F pGlobal;
|
||||||
|
} mDebug;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct WidgetManagerInterface : public Widget {
|
struct WidgetManagerInterface : public Widget {
|
||||||
virtual void updateWidget(Widget*, const char* reason = nullptr) = 0;
|
virtual void updateWidget(Widget*, const char* reason) = 0;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue