From d33be521cbf4117fec45ba56a148f0dbb68b0c65 Mon Sep 17 00:00:00 2001 From: HJfod <60038575+HJfod@users.noreply.github.com> Date: Mon, 29 Apr 2024 17:36:28 +0300 Subject: [PATCH] add ListBorders UI class --- loader/include/Geode/ui/General.hpp | 27 +++++++++++ loader/src/ui/nodes/General.cpp | 74 +++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) diff --git a/loader/include/Geode/ui/General.hpp b/loader/include/Geode/ui/General.hpp index 3214e71b..e8b6acbc 100644 --- a/loader/include/Geode/ui/General.hpp +++ b/loader/include/Geode/ui/General.hpp @@ -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; + }; } diff --git a/loader/src/ui/nodes/General.cpp b/loader/src/ui/nodes/General.cpp index be47b48e..12d1888e 100644 --- a/loader/src/ui/nodes/General.cpp +++ b/loader/src/ui/nodes/General.cpp @@ -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(); +}