add ListBorders UI class

This commit is contained in:
HJfod 2024-04-29 17:36:28 +03:00
parent 82d86782fb
commit d33be521cb
2 changed files with 101 additions and 0 deletions
loader
include/Geode/ui
src/ui/nodes

View file

@ -38,10 +38,37 @@ namespace geode {
/**
* Add the rounded comment borders to a node
* @note Use the `ListBorders` class for increased control
*/
GEODE_DLL void addListBorders(
cocos2d::CCNode* to,
cocos2d::CCPoint const& center,
cocos2d::CCSize const& size
);
class GEODE_DLL ListBorders : public cocos2d::CCNode {
protected:
cocos2d::extension::CCScale9Sprite* m_top = nullptr;
cocos2d::extension::CCScale9Sprite* m_bottom = nullptr;
cocos2d::CCSprite* m_left = nullptr;
cocos2d::CCSprite* m_right = nullptr;
float m_topPadding = 7.5f;
float m_bottomPadding = 7.5f;
bool init();
public:
static ListBorders* create();
void setSpriteFrames(const char* topAndBottom, const char* sides, float topPadding = 7.5f);
void setSprites(
cocos2d::extension::CCScale9Sprite* top,
cocos2d::extension::CCScale9Sprite* bottom,
cocos2d::CCSprite* left,
cocos2d::CCSprite* right,
float topPadding = 7.5f,
float bottomPadding = 7.5f
);
void setContentSize(cocos2d::CCSize const& size) override;
};
}

View file

@ -112,3 +112,77 @@ void geode::addListBorders(CCNode* to, CCPoint const& center, CCSize const& size
});
to->addChild(layerRightSpr);
}
bool ListBorders::init() {
if (!CCNode::init())
return false;
this->setAnchorPoint({ .5f, .5f });
this->setSpriteFrames("GJ_commentTop_001.png", "GJ_commentSide_001.png");
return true;
}
ListBorders* ListBorders::create() {
auto ret = new ListBorders();
if (ret && ret->init()) {
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}
void ListBorders::setSpriteFrames(const char* topAndBottom, const char* side, float topPadding) {
this->setSprites(
CCScale9Sprite::createWithSpriteFrameName(topAndBottom, { 0, 0, 240, 10 }),
CCScale9Sprite::createWithSpriteFrameName(topAndBottom, { 0, 0, 240, 10 }),
CCSprite::createWithSpriteFrameName(side),
CCSprite::createWithSpriteFrameName(side),
topPadding,
topPadding
);
m_bottom->setScaleY(-1);
m_right->setFlipX(true);
}
void ListBorders::setSprites(
CCScale9Sprite* top, CCScale9Sprite* bottom,
CCSprite* left, CCSprite* right,
float topPadding, float bottomPadding
) {
if (m_top) m_top->removeFromParent();
if (m_bottom) m_bottom->removeFromParent();
if (m_left) m_left->removeFromParent();
if (m_right) m_right->removeFromParent();
m_top = top;
this->addChildAtPosition(m_top, Anchor::Top, ccp(0, -m_top->getScaledContentHeight() / 3));
m_bottom = bottom;
this->addChildAtPosition(m_bottom, Anchor::Bottom, ccp(0, m_bottom->getScaledContentHeight() / 3));
m_left = left;
this->addChildAtPosition(m_left, Anchor::Left, ccp(0, 0));
m_right = right;
this->addChildAtPosition(m_right, Anchor::Right, ccp(0, 0));
m_topPadding = topPadding;
m_bottomPadding = bottomPadding;
this->setContentSize(m_obContentSize);
}
void ListBorders::setContentSize(CCSize const& size) {
CCNode::setContentSize(size);
m_top->setContentWidth(size.width + m_topPadding);
m_bottom->setContentWidth(size.width + m_bottomPadding);
m_left->setScaleY(
(size.height - m_top->getScaledContentHeight() - m_bottom->getScaledContentHeight()) /
m_left->getScaledContentHeight()
);
m_right->setScaleY(
(size.height - m_top->getScaledContentHeight() - m_bottom->getScaledContentHeight()) /
m_right->getScaledContentHeight()
);
this->updateLayout();
}