Added a padding node

This commit is contained in:
SMJSGaming 2024-09-01 18:22:34 +02:00
parent 1f2b2b44a2
commit 7aa9e774c4
3 changed files with 347 additions and 21 deletions

View file

@ -1,21 +1,22 @@
#pragma once
#include "ui/BasedButton.hpp"
#include "ui/Border.hpp"
#include "ui/ColorPickPopup.hpp"
#include "ui/EnterLayerEvent.hpp"
#include "ui/BasedButtonSprite.hpp"
#include "ui/IconButtonSprite.hpp"
#include "ui/InputNode.hpp"
#include "ui/General.hpp"
#include "ui/ListView.hpp"
#include "ui/MDPopup.hpp"
#include "ui/MDTextArea.hpp"
#include "ui/Notification.hpp"
#include "ui/Popup.hpp"
#include "ui/SceneManager.hpp"
#include "ui/ScrollLayer.hpp"
#include "ui/SelectList.hpp"
#include "ui/Scrollbar.hpp"
#include "ui/TextArea.hpp"
#include "ui/TextRenderer.hpp"
#pragma once
#include "ui/BasedButton.hpp"
#include "ui/Border.hpp"
#include "ui/ColorPickPopup.hpp"
#include "ui/EnterLayerEvent.hpp"
#include "ui/BasedButtonSprite.hpp"
#include "ui/IconButtonSprite.hpp"
#include "ui/InputNode.hpp"
#include "ui/General.hpp"
#include "ui/ListView.hpp"
#include "ui/MDPopup.hpp"
#include "ui/MDTextArea.hpp"
#include "ui/Notification.hpp"
#include "ui/PaddingNode.hpp"
#include "ui/Popup.hpp"
#include "ui/SceneManager.hpp"
#include "ui/ScrollLayer.hpp"
#include "ui/SelectList.hpp"
#include "ui/Scrollbar.hpp"
#include "ui/TextArea.hpp"
#include "ui/TextRenderer.hpp"

View file

@ -0,0 +1,133 @@
#pragma once
#include <cocos2d.h>
namespace geode {
class GEODE_DLL PaddingNode : public cocos2d::CCNode {
public:
enum class PaddingType {
// The container will have its size adjusted to fit the padding
Contained,
// This node will have its size adjusted to fit the padding
Uncontained
};
struct Padding {
operator float() const;
operator cocos2d::CCPoint() const;
float top;
float right;
float bottom;
float left;
Padding();
Padding(cocos2d::CCPoint padding);
Padding(const float x, const float y);
Padding(const float top, const float right, const float bottom, const float left);
};
static PaddingNode* create(cocos2d::CCNode* container, const Padding& padding = {}, const PaddingType type = PaddingType::Contained);
/**
* Sets the padding of the node
*/
virtual void setPadding(const Padding& padding);
/**
* Gets the padding of the node
*/
Padding getPadding() const;
/**
* Gets the average padding of the node
*/
float getAveragePadding() const;
/**
* Gets the total padding of the node
*/
float getTotalPadding() const;
/**
* Sets the padding on both the x-axis sides
*/
virtual void setPaddingX(const float x);
/**
* Gets the average padding on the x-axis
*/
float getPaddingX() const;
/**
* Gets the total padding on the x-axis
*/
float getTotalPaddingX() const;
/**
* Sets the padding on both the y-axis sides
*/
virtual void setPaddingY(const float y);
/**
* Gets the average padding on the y-axis
*/
float getPaddingY() const;
/**
* Gets the total padding on the y-axis
*/
float getTotalPaddingY() const;
/**
* Sets the padding on the top side
*/
virtual void setPaddingTop(const float top);
/**
* Gets the padding on the top side
*/
float getPaddingTop() const;
/**
* Sets the padding on the right side
*/
virtual void setPaddingRight(const float right);
/**
* Gets the padding on the right side
*/
float getPaddingRight() const;
/**
* Sets the padding on the bottom side
*/
virtual void setPaddingBottom(const float bottom);
/**
* Gets the padding on the bottom side
*/
float getPaddingBottom() const;
/**
* Sets the padding on the left side
*/
virtual void setPaddingLeft(const float left);
/**
* Gets the padding on the left side
*/
float getPaddingLeft() const;
/**
* Sets the padding type of the node
*/
virtual void setPaddingType(const PaddingType type);
/**
* Gets the padding type of the node
*/
PaddingType getPaddingType() const;
/**
* Sets the container of the node
*/
virtual void setContainer(cocos2d::CCNode* content);
/**
* Gets the container of the node
*/
cocos2d::CCNode* getContainer() const;
/**
* Gets the container size with the padding added
*/
cocos2d::CCSize getPaddedContainerSize() const;
protected:
Padding m_padding;
cocos2d::CCNode* m_container;
PaddingType m_type;
PaddingNode(cocos2d::CCNode* container, PaddingNode::Padding padding = {}, const PaddingType type = PaddingType::Contained);
bool init() override;
virtual void updatePadding();
};
}

View file

@ -0,0 +1,192 @@
#include <Geode/ui/PaddingNode.hpp>
using namespace cocos2d;
geode::PaddingNode::Padding::operator float() const {
return (top + right + bottom + left) / 4;
}
geode::PaddingNode::Padding::operator CCPoint() const {
return { (left + right) / 2, (top + bottom) / 2 };
}
geode::PaddingNode::Padding::Padding() :
top(0),
right(0),
bottom(0),
left(0) { }
geode::PaddingNode::Padding::Padding(CCPoint padding) :
top(padding.y),
right(padding.x),
bottom(padding.y),
left(padding.x) { }
geode::PaddingNode::Padding::Padding(const float x, const float y) :
top(y),
right(x),
bottom(y),
left(x) { }
geode::PaddingNode::Padding::Padding(const float top, const float right, const float bottom, const float left) :
top(top),
right(right),
bottom(bottom),
left(left) { }
geode::PaddingNode* geode::PaddingNode::create(CCNode* container, const Padding& padding, const PaddingType type) {
geode::PaddingNode* node = new geode::PaddingNode(container, padding, type);
if (node && node->init()) {
node->autorelease();
return node;
} else {
CC_SAFE_DELETE(node);
return nullptr;
}
}
geode::PaddingNode::PaddingNode(CCNode* container, geode::PaddingNode::Padding padding, const PaddingType type) :
m_container(container),
m_padding(padding),
m_type(type) { }
bool geode::PaddingNode::init() {
if (!CCNode::init()) {
return false;
}
this->addChild(m_container);
this->updatePadding();
return true;
}
void geode::PaddingNode::updatePadding() {
if (m_type == PaddingType::Contained) {
const CCSize size = this->getContentSize();
m_container->setContentSize({
size.width - this->getTotalPaddingX(),
size.height - this->getTotalPaddingY()
});
} else {
const CCSize containerSize = m_container->getContentSize();
this->setContentSize({
containerSize.width + this->getTotalPaddingX(),
containerSize.height + this->getTotalPaddingY()
});
}
m_container->setAnchorPoint({ 0, 0 });
m_container->setPosition({
m_padding.left,
m_padding.bottom
});
}
void geode::PaddingNode::setPadding(const Padding& padding) {
m_padding = padding;
this->updatePadding();
}
geode::PaddingNode::Padding geode::PaddingNode::getPadding() const {
return m_padding;
}
float geode::PaddingNode::getAveragePadding() const {
return m_padding;
}
float geode::PaddingNode::getTotalPadding() const {
return m_padding.top + m_padding.right + m_padding.bottom + m_padding.left;
}
void geode::PaddingNode::setPaddingX(const float x) {
m_padding.left = m_padding.right = x;
this->updatePadding();
}
float geode::PaddingNode::getPaddingX() const {
return this->getTotalPaddingX() / 2;
}
float geode::PaddingNode::getTotalPaddingX() const {
return m_padding.left + m_padding.right;
}
void geode::PaddingNode::setPaddingY(const float y) {
m_padding.top = m_padding.bottom = y;
this->updatePadding();
}
float geode::PaddingNode::getPaddingY() const {
return this->getTotalPaddingY() / 2;
}
float geode::PaddingNode::getTotalPaddingY() const {
return m_padding.top + m_padding.bottom;
}
void geode::PaddingNode::setPaddingTop(const float top) {
m_padding.top = top;
this->updatePadding();
}
float geode::PaddingNode::getPaddingTop() const {
return m_padding.top;
}
void geode::PaddingNode::setPaddingRight(const float right) {
m_padding.right = right;
this->updatePadding();
}
float geode::PaddingNode::getPaddingRight() const {
return m_padding.right;
}
void geode::PaddingNode::setPaddingBottom(const float bottom) {
m_padding.bottom = bottom;
this->updatePadding();
}
float geode::PaddingNode::getPaddingBottom() const {
return m_padding.bottom;
}
void geode::PaddingNode::setPaddingLeft(const float left) {
m_padding.left = left;
this->updatePadding();
}
float geode::PaddingNode::getPaddingLeft() const {
return m_padding.left;
}
void geode::PaddingNode::setPaddingType(const PaddingType type) {
m_type = type;
this->updatePadding();
}
geode::PaddingNode::PaddingType geode::PaddingNode::getPaddingType() const {
return m_type;
}
void geode::PaddingNode::setContainer(CCNode* content) {
m_container->removeFromParent();
m_container = content;
this->updatePadding();
}
CCNode* geode::PaddingNode::getContainer() const {
return m_container;
}
CCSize geode::PaddingNode::getPaddedContainerSize() const {
return m_container->getContentSize() + CCSize(this->getTotalPaddingX(), this->getTotalPaddingY());
}