2023-10-01 17:59:44 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Geode/DefaultInclude.hpp>
|
|
|
|
#include <cocos2d.h>
|
|
|
|
|
|
|
|
namespace geode {
|
2023-10-10 13:19:12 -04:00
|
|
|
enum WrappingMode {
|
|
|
|
NO_WRAP,
|
|
|
|
WORD_WRAP,
|
2024-06-05 14:16:23 -04:00
|
|
|
SPACE_WRAP,
|
2023-10-10 13:19:12 -04:00
|
|
|
CUTOFF_WRAP
|
|
|
|
};
|
|
|
|
|
2023-10-01 17:59:44 -04:00
|
|
|
/**
|
|
|
|
* A class which provides a textarea with proper alignment and some extra features like:
|
|
|
|
*
|
|
|
|
* - Max lines
|
|
|
|
* - Changing all aspects after creation
|
|
|
|
* - Custom text alignment
|
2023-10-10 13:19:12 -04:00
|
|
|
* - Configurable and automatic word wrapping
|
2023-10-01 17:59:44 -04:00
|
|
|
* - Line padding
|
|
|
|
*
|
|
|
|
* Contact me on Discord (\@smjs) if you have any questions, suggestions or bugs.
|
|
|
|
*/
|
|
|
|
class GEODE_DLL SimpleTextArea : public cocos2d::CCNode {
|
2024-01-28 13:27:11 -05:00
|
|
|
public:
|
2024-11-10 10:05:20 -05:00
|
|
|
static SimpleTextArea* create(const std::string& text, const std::string& font = "chatFont.fnt", float scale = 1.0f);
|
|
|
|
static SimpleTextArea* create(const std::string& text, const std::string& font, float scale, float width);
|
2023-10-10 13:19:12 -04:00
|
|
|
|
2023-10-01 17:59:44 -04:00
|
|
|
void setFont(const std::string& font);
|
|
|
|
std::string getFont();
|
2023-10-10 13:19:12 -04:00
|
|
|
void setColor(const cocos2d::ccColor4B& color);
|
|
|
|
cocos2d::ccColor4B getColor();
|
2024-11-10 10:05:20 -05:00
|
|
|
void setAlignment(cocos2d::CCTextAlignment alignment);
|
2023-10-01 17:59:44 -04:00
|
|
|
cocos2d::CCTextAlignment getAlignment();
|
2024-11-10 10:05:20 -05:00
|
|
|
void setWrappingMode(WrappingMode mode);
|
2023-10-10 13:19:12 -04:00
|
|
|
WrappingMode getWrappingMode();
|
2023-10-01 17:59:44 -04:00
|
|
|
void setText(const std::string& text);
|
|
|
|
std::string getText();
|
2024-11-10 10:05:20 -05:00
|
|
|
void setMaxLines(size_t maxLines);
|
2023-10-01 17:59:44 -04:00
|
|
|
size_t getMaxLines();
|
2024-11-10 10:05:20 -05:00
|
|
|
void setWidth(float width);
|
2023-10-01 17:59:44 -04:00
|
|
|
float getWidth();
|
2024-11-10 10:05:20 -05:00
|
|
|
void setScale(float scale) override;
|
2023-10-15 10:48:40 -04:00
|
|
|
float getScale() override;
|
2024-11-10 10:05:20 -05:00
|
|
|
void setLinePadding(float padding);
|
2023-10-01 17:59:44 -04:00
|
|
|
float getLinePadding();
|
|
|
|
std::vector<cocos2d::CCLabelBMFont*> getLines();
|
|
|
|
float getHeight();
|
|
|
|
float getLineHeight();
|
|
|
|
private:
|
2024-11-10 10:05:20 -05:00
|
|
|
static SimpleTextArea* create(const std::string& font, const std::string& text, float scale, float width, const bool artificialWidth);
|
2023-10-01 17:59:44 -04:00
|
|
|
|
2024-11-10 10:05:20 -05:00
|
|
|
bool init(const std::string& font, const std::string& text, float scale, float width, const bool artificialWidth);
|
2024-02-04 11:23:53 -05:00
|
|
|
|
|
|
|
bool m_shouldUpdate = false;
|
|
|
|
bool m_artificialWidth = false;
|
|
|
|
cocos2d::CCMenu* m_container = nullptr;
|
2024-01-28 13:55:28 -05:00
|
|
|
std::string m_font;
|
|
|
|
std::string m_text;
|
|
|
|
std::vector<cocos2d::CCLabelBMFont*> m_lines;
|
2024-02-04 11:23:53 -05:00
|
|
|
cocos2d::ccColor4B m_color = { 0xFF, 0xFF, 0xFF, 0xFF };
|
|
|
|
cocos2d::CCTextAlignment m_alignment = cocos2d::kCCTextAlignmentLeft;
|
|
|
|
WrappingMode m_wrappingMode = WORD_WRAP;
|
|
|
|
size_t m_maxLines = 0;
|
|
|
|
float m_scale = 1.f;
|
|
|
|
float m_lineHeight = 0.f;
|
|
|
|
float m_linePadding = 0.f;
|
2023-10-10 13:19:12 -04:00
|
|
|
|
2024-11-10 10:05:20 -05:00
|
|
|
cocos2d::CCLabelBMFont* createLabel(const std::string& text, float top);
|
2023-10-01 17:59:44 -04:00
|
|
|
float calculateOffset(cocos2d::CCLabelBMFont* label);
|
2024-11-10 10:05:20 -05:00
|
|
|
void charIteration(const std::function<cocos2d::CCLabelBMFont*(cocos2d::CCLabelBMFont* line, char c, float top)>& overflowHandling);
|
2023-10-10 13:19:12 -04:00
|
|
|
void updateLinesNoWrap();
|
2024-06-05 14:16:23 -04:00
|
|
|
void updateLinesWordWrap(bool spaceWrap);
|
2023-10-10 13:19:12 -04:00
|
|
|
void updateLinesCutoffWrap();
|
|
|
|
void updateContainer();
|
2023-10-01 17:59:44 -04:00
|
|
|
};
|
2024-06-01 11:22:50 -04:00
|
|
|
}
|