Revert "working on a layout rewrite"

This reverts commit ecbfd5ad0f.
This commit is contained in:
HJfod 2023-02-04 11:28:33 +02:00
parent ecbfd5ad0f
commit d23563a9c0
7 changed files with 80 additions and 250 deletions

View file

@ -921,14 +921,9 @@ public:
* has been added, call updateLayout * has been added, call updateLayout
* @param layout Layout to set to this node * @param layout Layout to set to this node
* @param apply Whether to call updateLayout now or not * @param apply Whether to call updateLayout now or not
* @param respectAnchor If true, if the target node is
* isIgnoreAnchorPointForPosition, then it is set to false and the children
* are automatically moved to match where they should be positioned.
* Visually, this should result in no difference; however, when dealing with
* CCLayers / CCMenus, this will change where the children are located
* @note Geode addition * @note Geode addition
*/ */
GEODE_DLL void setLayout(Layout* layout, bool apply = true, bool respectAnchor = true); GEODE_DLL void setLayout(Layout* layout, bool apply = true);
/** /**
* Get the Layout for this node * Get the Layout for this node
* @returns The current layout, or nullptr if no layout is set * @returns The current layout, or nullptr if no layout is set

View file

@ -18,20 +18,15 @@ class CCNode;
* RowLayout, ColumnLayout, and GridLayout, but if you need a different kind * RowLayout, ColumnLayout, and GridLayout, but if you need a different kind
* of layout you can inherit from the Layout class. * of layout you can inherit from the Layout class.
*/ */
class GEODE_DLL Layout { class Layout {
protected:
static CCArray* getNodesToPosition(CCNode* forNode);
public: public:
/** /**
* Automatically apply the layout's positioning on a set of nodes * Automatically apply the layout's positioning on a set of nodes
* @param on Node to apply the layout on. Position's the node's children * @param nodes Nodes to position
* according to the layout. The content size of the node should be * @param availableSize Give hints to the layout about how much space is
* respected as a boundary the layout shouldn't overflow. The node may be * available. Note that the layout may still overflow
* rescaled to better fit its contents. Layouts should respect nodes that
* have their PositionHint marked as absolute
*/ */
virtual void apply(CCNode* on) = 0; virtual void apply(CCArray* nodes, CCSize const& availableSize) = 0;
}; };
/** /**
@ -61,62 +56,30 @@ enum class Alignment {
*/ */
class GEODE_DLL RowLayout : public Layout { class GEODE_DLL RowLayout : public Layout {
protected: protected:
std::optional<Alignment> m_vAlignment = Alignment::Center; Alignment m_alignment = Alignment::Center;
std::optional<float> m_alignVertically;
float m_gap; float m_gap;
std::optional<float> m_maxAutoScale = std::nullopt;
bool m_reverse = false;
bool m_fitInside = false;
public: public:
void apply(CCNode* on) override; void apply(CCArray* nodes, CCSize const& availableSize) override;
/** /**
* Create a new RowLayout. Note that this class is not automatically * Create a new RowLayout. Note that this class is not automatically
* managed by default, so you must assign it to a CCNode or manually * managed by default, so you must assign it to a CCNode or manually
* manage the memory yourself. See the chainable setters on RowLayout for * manage the memory yourself.
* what options you can customize for the layout * @param gap Space between nodes
* @param alignVertically Whether to align the nodes vertically, and if so,
* what Y position to align them at
* @returns Created RowLayout * @returns Created RowLayout
*/ */
static RowLayout* create(); static RowLayout* create(
float gap = 5.f,
std::optional<float> alignVertically = std::nullopt
);
/** RowLayout* setAlignment(Alignment align);
* Sets where to align nodes on the Y-axis. If nullopt, the
* nodes' Y-position will not be affected, and the height of the node this
* layout applies to isn't altered. If an alignment is given, the height
* of the node this layout applies to is shrunk to fit the height of the
* nodes and no more. Any nodes that don't fit inside this space are
* aligned based on the value
* @param align Value
* @returns The same RowLayout this was applied on
*/
RowLayout* setVAlignment(std::optional<Alignment> align);
/**
* The spacing between the children of the node this layout applies to.
* Measured as the space between their edges, not centres
*/
RowLayout* setGap(float gap); RowLayout* setGap(float gap);
/** RowLayout* setAlignVertically(std::optional<float> align);
* Whether to reverse the direction of the children in this layout or not
*/
RowLayout* setReverse(bool reverse);
/**
* If a value is provided, then the node this layout applies to may be
* automatically rescaled to fit its contents better. By default the value
* is nullopt, which means that the layout doesn't affect the node's scale
* in any way, and any nodes that might overflow will be squished using
* other methods. If the value is set, the layout assumes that the scaled
* content size of the target node is what the content should be fit
* inside of, and scales to fit that space. If the value is nullopt, the
* unscaled content size is used instead
*/
RowLayout* setMaxAutoScale(std::optional<float> scale);
/**
* If true, the children of the node this layout is applied to will be
* contained entirely within the bounds of the node's content size. If
* false, the children's positions will be within the bounds, but they may
* visually overflow depending on their anchor point
*/
RowLayout* setFitInside(bool fit);
}; };
/** /**
@ -124,59 +87,21 @@ public:
*/ */
class GEODE_DLL ColumnLayout : public Layout { class GEODE_DLL ColumnLayout : public Layout {
protected: protected:
std::optional<Alignment> m_hAlignment = Alignment::Center; Alignment m_alignment = Alignment::Center;
std::optional<float> m_alignHorizontally;
float m_gap; float m_gap;
std::optional<float> m_maxAutoScale = std::nullopt;
bool m_reverse = false;
bool m_fitInside = false;
public: public:
void apply(CCNode* on) override; void apply(CCArray* nodes, CCSize const& availableSize) override;
/** static ColumnLayout* create(
* Create a new ColumnLayout. Note that this class is not automatically float gap = 5.f,
* managed by default, so you must assign it to a CCNode or manually std::optional<float> alignHorizontally = std::nullopt
* manage the memory yourself. See the chainable setters on RowLayout for );
* what options you can customize for the layout
* @returns Created ColumnLayout
*/
static ColumnLayout* create();
/** ColumnLayout* setAlignment(Alignment align);
* Sets where to align nodes on the X-axis. If nullopt, the
* nodes' X-position will not be affected, and the width of the node this
* layout applies to isn't altered. If an alignment is given, the width
* of the node this layout applies to is shrunk to fit the width of the
* nodes and no more. Any nodes that don't fit inside this space are
* aligned based on the value
* @param align Value
* @returns The same RowLayout this was applied on
*/
ColumnLayout* setHAlignment(std::optional<Alignment> align);
/**
* The spacing between the children of the node this layout applies to.
* Measured as the space between their edges, not centres
*/
ColumnLayout* setGap(float gap); ColumnLayout* setGap(float gap);
/** ColumnLayout* setAlignHorizontally(std::optional<float> align);
* Whether to reverse the direction of the children in this layout or not
*/
ColumnLayout* setReverse(bool reverse);
/**
* If a value is provided, then the node this layout applies to may be
* automatically rescaled to fit its contents better. By default the value
* is nullopt, which means that the layout doesn't affect the node's scale
* in any way, and any nodes that might overflow will be squished using
* other methods
*/
ColumnLayout* setMaxAutoScale(std::optional<float> scale);
/**
* If true, the children of the node this layout is applied to will be
* contained entirely within the bounds of the node's content size. If
* false, the children's positions will be within the bounds, but they may
* visually overflow depending on their anchor point
*/
ColumnLayout* setFitInside(bool fit);
}; };
/** /**
@ -212,7 +137,7 @@ protected:
std::optional<size_t> m_rowSize; std::optional<size_t> m_rowSize;
public: public:
void apply(CCNode* on) override; void apply(CCArray* nodes, CCSize const& availableSize) override;
static GridLayout* create( static GridLayout* create(
std::optional<size_t> rowSize, std::optional<size_t> rowSize,

View file

@ -1,7 +1,5 @@
#include <cocos2d.h> #include <cocos2d.h>
#include <Geode/utils/cocos.hpp> #include <Geode/utils/cocos.hpp>
#include <Geode/utils/ranges.hpp>
#include <Geode/loader/Log.hpp>
USE_GEODE_NAMESPACE(); USE_GEODE_NAMESPACE();
@ -11,58 +9,17 @@ void CCNode::swapChildIndices(CCNode* first, CCNode* second) {
m_pChildren->exchangeObject(first, second); m_pChildren->exchangeObject(first, second);
} }
CCArray* Layout::getNodesToPosition(CCNode* on) { void RowLayout::apply(CCArray* nodes, CCSize const& availableSize) {
auto filtered = CCArray::create();
for (auto& child : CCArrayExt<CCNode>(on->getChildren())) {
if (child->getPositionHint() != PositionHint::Absolute) {
filtered->addObject(child);
}
}
return filtered;
}
void RowLayout::apply(CCNode* on) {
auto nodes = getNodesToPosition(on);
if (m_reverse) {
nodes->reverseObjects();
}
auto availableWidth = m_maxAutoScale.has_value() ?
on->getScaledContentSize().width :
on->getContentSize().width;
size_t ix = 0;
float totalWidth = .0f; float totalWidth = .0f;
size_t ix = 0;
for (auto& node : CCArrayExt<CCNode*>(nodes)) { for (auto& node : CCArrayExt<CCNode*>(nodes)) {
// if no need to fit fully inside, figure out what part may overflow totalWidth += node->getScaledContentSize().width;
// for first item
if (ix == 0 && !m_fitInside) {
totalWidth += node->getScaledContentSize().width
* (1.f - node->getAnchorPoint().x);
}
// if no need to fit fully inside, figure out what part may overflow
// for last item
else if (ix == nodes->count() - 1 && !m_fitInside) {
totalWidth += node->getScaledContentSize().width
* node->getAnchorPoint().x;
}
// otherwise either we need to fit fully inside or this node is not
// at the start or end
else {
totalWidth += node->getScaledContentSize().width;
}
if (ix) { if (ix) {
totalWidth += m_gap; totalWidth += m_gap;
} }
ix++; ix++;
} }
auto squeeze = availableSize.width / totalWidth;
if (squeeze > 1.f) {
squeeze = 1.f;
}
float pos; float pos;
switch (m_alignment) { switch (m_alignment) {
default: default:
@ -83,16 +40,22 @@ void RowLayout::apply(CCNode* on) {
if (m_alignVertically) { if (m_alignVertically) {
node->setPositionY(m_alignVertically.value()); node->setPositionY(m_alignVertically.value());
} }
pos += (sw + m_gap) * squeeze; pos += sw + m_gap;
} }
} }
RowLayout* RowLayout::create() { RowLayout* RowLayout::create(
return new RowLayout(); float gap,
std::optional<float> alignVertically
) {
auto ret = new RowLayout;
ret->m_gap = gap;
ret->m_alignVertically = alignVertically;
return ret;
} }
RowLayout* RowLayout::setVAlignment(std::optional<Alignment> align) { RowLayout* RowLayout::setAlignment(Alignment align) {
m_vAlignment = align; m_alignment = align;
return this; return this;
} }
@ -101,26 +64,14 @@ RowLayout* RowLayout::setGap(float gap) {
return this; return this;
} }
RowLayout* RowLayout::setReverse(bool reverse) { RowLayout* RowLayout::setAlignVertically(std::optional<float> align) {
m_reverse = reverse; m_alignVertically = align;
return this; return this;
} }
RowLayout* RowLayout::setMaxAutoScale(std::optional<float> scale) { void ColumnLayout::apply(CCArray* nodes, CCSize const& availableSize) {
m_maxAutoScale = scale;
return this;
}
RowLayout* RowLayout::setFitInside(bool fit) {
m_fitInside = fit;
return this;
}
void ColumnLayout::apply(CCNode* on) {
float totalHeight = .0f; float totalHeight = .0f;
size_t ix = 0; size_t ix = 0;
auto nodes = getNodesToPosition(on);
auto availableSize = on->getScaledContentSize();
for (auto& node : CCArrayExt<CCNode*>(nodes)) { for (auto& node : CCArrayExt<CCNode*>(nodes)) {
totalHeight += node->getScaledContentSize().height; totalHeight += node->getScaledContentSize().height;
if (ix) { if (ix) {
@ -128,14 +79,6 @@ void ColumnLayout::apply(CCNode* on) {
} }
} }
auto squeeze = availableSize.height / totalHeight;
if (squeeze > 1.f) {
squeeze = 1.f;
}
if (totalHeight > availableSize.height) {
totalHeight = availableSize.height;
}
float pos; float pos;
switch (m_alignment) { switch (m_alignment) {
default: default:
@ -143,37 +86,35 @@ void ColumnLayout::apply(CCNode* on) {
case Alignment::Begin: pos = -totalHeight; break; case Alignment::Begin: pos = -totalHeight; break;
case Alignment::End: pos = 0.f; break; case Alignment::End: pos = 0.f; break;
} }
if (m_reverse) {
nodes->reverseObjects();
}
log::debug("start pos: {}", pos);
log::debug("squeeze: {}", squeeze);
for (auto& node : CCArrayExt<CCNode*>(nodes)) { for (auto& node : CCArrayExt<CCNode*>(nodes)) {
auto sh = node->getScaledContentSize().height; auto sh = node->getScaledContentSize().height;
float disp; float disp;
switch (m_alignment) { switch (m_alignment) {
default: default:
case Alignment::Center: disp = sh * node->getAnchorPoint().y; break; case Alignment::Center: disp = sh * node->getAnchorPoint().y; break;
case Alignment::Begin: disp = (m_reverse ? 0.f : sh); break; case Alignment::Begin: disp = sh; break;
case Alignment::End: disp = (m_reverse ? sh : 0.f); break; case Alignment::End: disp = 0.f; break;
} }
log::debug("positioning at: {}", pos + disp);
node->setPositionY(pos + disp); node->setPositionY(pos + disp);
if (m_alignHorizontally) { if (m_alignHorizontally) {
node->setPositionX(m_alignHorizontally.value()); node->setPositionX(m_alignHorizontally.value());
} }
auto opos = pos; pos += sh + m_gap;
pos += (sh + m_gap) * squeeze;
log::debug("pos: {} -> {}", opos, pos);
} }
} }
ColumnLayout* ColumnLayout::create() { ColumnLayout* ColumnLayout::create(
return new ColumnLayout(); float gap,
std::optional<float> alignHorizontally
) {
auto ret = new ColumnLayout;
ret->m_gap = gap;
ret->m_alignHorizontally = alignHorizontally;
return ret;
} }
ColumnLayout* ColumnLayout::setHAlignment(std::optional<Alignment> align) { ColumnLayout* ColumnLayout::setAlignment(Alignment align) {
m_hAlignment = align; m_alignment = align;
return this; return this;
} }
@ -182,22 +123,12 @@ ColumnLayout* ColumnLayout::setGap(float gap) {
return this; return this;
} }
ColumnLayout* ColumnLayout::setReverse(bool reverse) { ColumnLayout* ColumnLayout::setAlignHorizontally(std::optional<float> align) {
m_reverse = reverse; m_alignHorizontally = align;
return this; return this;
} }
ColumnLayout* ColumnLayout::setMaxAutoScale(std::optional<float> scale) { void GridLayout::apply(CCArray* nodes, CCSize const& availableSize) {
m_maxAutoScale = scale;
return this;
}
ColumnLayout* ColumnLayout::setFitInside(bool fit) {
m_fitInside = fit;
return this;
}
void GridLayout::apply(CCNode* on) {
// todo // todo
} }

View file

@ -118,13 +118,7 @@ CCNode* CCNode::getChildByIDRecursive(std::string const& id) {
return nullptr; return nullptr;
} }
void CCNode::setLayout(Layout* layout, bool apply, bool respectAnchor) { void CCNode::setLayout(Layout* layout, bool apply) {
if (respectAnchor && this->isIgnoreAnchorPointForPosition()) {
for (auto child : CCArrayExt<CCNode>(m_pChildren)) {
child->setPosition(child->getPosition() + this->getScaledContentSize());
}
this->ignoreAnchorPointForPosition(false);
}
GeodeNodeMetadata::set(this)->m_layout.reset(layout); GeodeNodeMetadata::set(this)->m_layout.reset(layout);
if (apply) { if (apply) {
this->updateLayout(); this->updateLayout();
@ -137,7 +131,15 @@ Layout* CCNode::getLayout() {
void CCNode::updateLayout() { void CCNode::updateLayout() {
if (auto layout = GeodeNodeMetadata::set(this)->m_layout.get()) { if (auto layout = GeodeNodeMetadata::set(this)->m_layout.get()) {
layout->apply(this); // nodes with absolute position should never be rearranged
auto filtered = CCArray::create();
for (auto& child : CCArrayExt<CCNode>(m_pChildren)) {
if (child->getPositionHint() != PositionHint::Absolute) {
filtered->addObject(child);
}
}
layout->apply(filtered, m_obContentSize);
filtered->release();
} }
} }

View file

@ -29,9 +29,9 @@ $register_ids(CreatorLayer) {
detachAndCreateMenu( detachAndCreateMenu(
this, this,
"top-right-menu", "top-right-menu",
ColumnLayout::create(), ColumnLayout::create(5.f, 0.f)->setAlignment(Alignment::Begin),
lockBtn lockBtn
)->setAnchorPoint({ 0.f, 0.f }); );
} }
// move treasure room button to its own menu // move treasure room button to its own menu

View file

@ -3,7 +3,6 @@
#include <Geode/Bindings.hpp> #include <Geode/Bindings.hpp>
#include <Geode/modify/EditLevelLayer.hpp> #include <Geode/modify/EditLevelLayer.hpp>
#include <Geode/utils/cocos.hpp> #include <Geode/utils/cocos.hpp>
#include <Geode/ui/BasedButtonSprite.hpp>
USE_GEODE_NAMESPACE(); USE_GEODE_NAMESPACE();
@ -46,30 +45,8 @@ $register_ids(EditLevelLayer) {
); );
detachAndCreateMenu( detachAndCreateMenu(
this, "folder-menu", ColumnLayout::create(), menu->getChildByID("folder-button") menu, "folder-menu", ColumnLayout::create(), menu->getChildByID("folder-button")
); );
menu->setPosition(
menu->getPositionX() + static_cast<CCNode*>(
menu->getChildren()->firstObject()
)->getPositionX(),
285.f
);
menu->setLayout(
ColumnLayout::create()
->setGap(7.f)
->setReverse(true)
->setAlignment(Alignment::Begin)
->setAlignHorizontally(0.f)
);
menu->setZOrder(1);
for (int i = 0; i < rand() % 4; i++) {
auto btn = CircleButtonSprite::create(
CCLabelBMFont::create(std::to_string(i).c_str(), "bigFont.fnt")
);
menu->addChild(btn);
}
menu->updateLayout();
} }
if (auto menu = this->getChildByID("back-button-menu")) setIDSafe(menu, 0, "back-button"); if (auto menu = this->getChildByID("back-button-menu")) setIDSafe(menu, 0, "back-button");

View file

@ -44,7 +44,7 @@ $register_ids(MenuLayer) {
pfp->setPositionHint(PositionHint::Absolute); pfp->setPositionHint(PositionHint::Absolute);
} }
menu->setLayout(RowLayout::create()->setGap(18.f)); menu->setLayout(RowLayout::create(18.f, 0.f));
} }
// bottom menu // bottom menu
if (auto menu = getChildOfType<CCMenu>(this, 1)) { if (auto menu = getChildOfType<CCMenu>(this, 1)) {
@ -57,10 +57,10 @@ $register_ids(MenuLayer) {
// move daily chest to its own menu // move daily chest to its own menu
if (auto dailyChest = setIDSafe(menu, -1, "daily-chest-button")) { if (auto dailyChest = setIDSafe(menu, -1, "daily-chest-button")) {
detachAndCreateMenu(this, "right-side-menu", ColumnLayout::create(), dailyChest); detachAndCreateMenu(this, "right-side-menu", ColumnLayout::create(0.f, 0.f), dailyChest);
} }
menu->setLayout(RowLayout::create()); menu->setLayout(RowLayout::create(5.f, ach->getPositionY()));
} }
// social media menu // social media menu
if (auto menu = getChildOfType<CCMenu>(this, 2)) { if (auto menu = getChildOfType<CCMenu>(this, 2)) {
@ -79,8 +79,8 @@ $register_ids(MenuLayer) {
if (auto closeBtn = setIDSafe(menu, 1, "close-button")) { if (auto closeBtn = setIDSafe(menu, 1, "close-button")) {
detachAndCreateMenu( detachAndCreateMenu(
this, "close-menu", RowLayout::create(), closeBtn this, "close-menu", RowLayout::create(5.f, 0.f)->setAlignment(Alignment::Begin), closeBtn
)->setAnchorPoint({ 0.f, .5f }); );
} }
} }
} }