|
| 1 | +#include <Geode/ui/TextArea.hpp> |
| 2 | + |
| 3 | +using namespace geode::prelude; |
| 4 | + |
| 5 | +SimpleTextArea* SimpleTextArea::create(const std::string& font, const std::string& text, const float scale = 1) { |
| 6 | + return SimpleTextArea::create(font, text, scale, 500, false); |
| 7 | +} |
| 8 | + |
| 9 | +SimpleTextArea* SimpleTextArea::create(const std::string& font, const std::string& text, const float scale, const float width) { |
| 10 | + return SimpleTextArea::create(font, text, scale, width, true); |
| 11 | +} |
| 12 | + |
| 13 | +SimpleTextArea* SimpleTextArea::create(const std::string& font, const std::string& text, const float scale, const float width, const bool artificialWidth) { |
| 14 | + SimpleTextArea* instance = new SimpleTextArea(font, text, scale, width, artificialWidth); |
| 15 | + |
| 16 | + if (instance && instance->init()) { |
| 17 | + instance->autorelease(); |
| 18 | + |
| 19 | + return instance; |
| 20 | + } else { |
| 21 | + CC_SAFE_DELETE(instance); |
| 22 | + |
| 23 | + return nullptr; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +SimpleTextArea::SimpleTextArea(const std::string& font, const std::string& text, const float scale, const float width, const bool artificialWidth) { |
| 28 | + m_font = font; |
| 29 | + m_text = text; |
| 30 | + m_maxLines = 0; |
| 31 | + m_scale = scale; |
| 32 | + m_linePadding = 0; |
| 33 | + m_alignment = kCCTextAlignmentLeft; |
| 34 | + m_artificialWidth = artificialWidth; |
| 35 | + m_container = CCMenu::create(); |
| 36 | + |
| 37 | + this->setAnchorPoint({ 0.5f, 0.5f }); |
| 38 | + m_container->setPosition({ 0, 0 }); |
| 39 | + m_container->setAnchorPoint({ 0, 1 }); |
| 40 | + m_container->setContentSize({ width, 0 }); |
| 41 | + |
| 42 | + this->addChild(m_container); |
| 43 | + this->updateContents(); |
| 44 | +} |
| 45 | + |
| 46 | +void SimpleTextArea::setFont(const std::string& font) { |
| 47 | + m_font = font; |
| 48 | + |
| 49 | + this->updateContents(); |
| 50 | +} |
| 51 | + |
| 52 | +std::string SimpleTextArea::getFont() { |
| 53 | + return m_font; |
| 54 | +} |
| 55 | + |
| 56 | +void SimpleTextArea::setAlignment(const CCTextAlignment alignment) { |
| 57 | + m_alignment = alignment; |
| 58 | + |
| 59 | + this->updateContents(); |
| 60 | +} |
| 61 | + |
| 62 | +CCTextAlignment SimpleTextArea::getAlignment() { |
| 63 | + return m_alignment; |
| 64 | +} |
| 65 | + |
| 66 | +void SimpleTextArea::setText(const std::string& text) { |
| 67 | + m_text = text; |
| 68 | + |
| 69 | + this->updateContents(); |
| 70 | +} |
| 71 | + |
| 72 | +std::string SimpleTextArea::getText() { |
| 73 | + return m_text; |
| 74 | +} |
| 75 | + |
| 76 | +void SimpleTextArea::setMaxLines(const size_t maxLines) { |
| 77 | + m_maxLines = maxLines; |
| 78 | + |
| 79 | + this->updateContents(); |
| 80 | +} |
| 81 | + |
| 82 | +size_t SimpleTextArea::getMaxLines() { |
| 83 | + return m_maxLines; |
| 84 | +} |
| 85 | + |
| 86 | +void SimpleTextArea::setWidth(const float width) { |
| 87 | + m_artificialWidth = true; |
| 88 | + |
| 89 | + this->setContentSize({ width, this->getContentSize().height }); |
| 90 | + m_container->setContentSize(this->getContentSize()); |
| 91 | +} |
| 92 | + |
| 93 | +float SimpleTextArea::getWidth() { |
| 94 | + return m_container->getContentSize().width; |
| 95 | +} |
| 96 | + |
| 97 | +void SimpleTextArea::setScale(const float scale) { |
| 98 | + m_scale = scale; |
| 99 | + |
| 100 | + this->updateContents(); |
| 101 | +} |
| 102 | + |
| 103 | +float SimpleTextArea::getScale() { |
| 104 | + return m_scale; |
| 105 | +} |
| 106 | + |
| 107 | +void SimpleTextArea::setLinePadding(const float padding) { |
| 108 | + m_linePadding = padding; |
| 109 | + |
| 110 | + this->updateContents(); |
| 111 | +} |
| 112 | + |
| 113 | +float SimpleTextArea::getLinePadding() { |
| 114 | + return m_linePadding; |
| 115 | +} |
| 116 | + |
| 117 | +std::vector<CCLabelBMFont*> SimpleTextArea::getLines() { |
| 118 | + return m_lines; |
| 119 | +} |
| 120 | + |
| 121 | +float SimpleTextArea::getHeight() { |
| 122 | + return m_container->getContentSize().height; |
| 123 | +} |
| 124 | + |
| 125 | +float SimpleTextArea::getLineHeight() { |
| 126 | + return m_lineHeight; |
| 127 | +} |
| 128 | + |
| 129 | +CCLabelBMFont* SimpleTextArea::createLabel(const std::string& text, const float top) { |
| 130 | + CCLabelBMFont* label = CCLabelBMFont::create(text.c_str(), m_font.c_str()); |
| 131 | + |
| 132 | + label->setScale(m_scale); |
| 133 | + label->setPosition({ 0, top }); |
| 134 | + |
| 135 | + return label; |
| 136 | +} |
| 137 | + |
| 138 | +CCLabelBMFont* SimpleTextArea::moveOverflow(CCLabelBMFont* line, const char c, const float top) { |
| 139 | + const std::string text = line->getString(); |
| 140 | + const char back = text.back(); |
| 141 | + const bool lastIsSpace = back == ' '; |
| 142 | + CCLabelBMFont* newLine = this->createLabel(std::string(!lastIsSpace, back).append(std::string(c != ' ', c)), top); |
| 143 | + |
| 144 | + if (!lastIsSpace) { |
| 145 | + if (text[text.size() - 2] == ' ') { |
| 146 | + line->setString(text.substr(0, text.size() - 1).c_str()); |
| 147 | + } else { |
| 148 | + line->setString((text.substr(0, text.size() - 1) + '-').c_str()); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + m_lines.push_back(newLine); |
| 153 | + |
| 154 | + return newLine; |
| 155 | +} |
| 156 | + |
| 157 | +float SimpleTextArea::calculateOffset(CCLabelBMFont* label) { |
| 158 | + return m_linePadding + label->getContentSize().height * m_scale; |
| 159 | +} |
| 160 | + |
| 161 | +void SimpleTextArea::updateLines() { |
| 162 | + float top = 0; |
| 163 | + CCLabelBMFont* line = this->createLabel("", top); |
| 164 | + m_lines = { line }; |
| 165 | + |
| 166 | + for (const char c : m_text) { |
| 167 | + if (m_maxLines && m_lines.size() > m_maxLines) { |
| 168 | + CCLabelBMFont* last = m_lines.at(m_maxLines - 1); |
| 169 | + const std::string text = last->getString(); |
| 170 | + |
| 171 | + m_lines.pop_back(); |
| 172 | + last->setString(text.substr(0, text.size() - 3).append("...").c_str()); |
| 173 | + |
| 174 | + break; |
| 175 | + } else if (c == '\n') { |
| 176 | + line = this->createLabel("", top -= this->calculateOffset(line)); |
| 177 | + |
| 178 | + m_lines.push_back(line); |
| 179 | + } else if (m_artificialWidth && line->getContentSize().width >= this->getWidth()) { |
| 180 | + line = this->moveOverflow(line, c, top -= this->calculateOffset(line)); |
| 181 | + } else { |
| 182 | + const std::string text = line->getString(); |
| 183 | + |
| 184 | + line->setString((text + c).c_str()); |
| 185 | + } |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +void SimpleTextArea::updateContents() { |
| 190 | + this->updateLines(); |
| 191 | + const size_t lineCount = m_lines.size(); |
| 192 | + const float width = this->getWidth(); |
| 193 | + |
| 194 | + if (lineCount > 0) { |
| 195 | + m_lineHeight = m_lines.back()->getContentSize().height * m_scale; |
| 196 | + } else { |
| 197 | + m_lineHeight = 0; |
| 198 | + } |
| 199 | + |
| 200 | + float height = m_lineHeight * lineCount + m_linePadding * (lineCount - 1); |
| 201 | + |
| 202 | + this->setContentSize({ width, height }); |
| 203 | + m_container->setContentSize(this->getContentSize()); |
| 204 | + m_container->removeAllChildren(); |
| 205 | + |
| 206 | + for (CCLabelBMFont* line : m_lines) { |
| 207 | + const float y = height + line->getPositionY(); |
| 208 | + |
| 209 | + switch (m_alignment) { |
| 210 | + case kCCTextAlignmentLeft: { |
| 211 | + line->setAnchorPoint({ 0, 1 }); |
| 212 | + line->setPosition({ 0, y }); |
| 213 | + } break; |
| 214 | + case kCCTextAlignmentCenter: { |
| 215 | + line->setAnchorPoint({ 0.5f, 1 }); |
| 216 | + line->setPosition({ width / 2, y }); |
| 217 | + } break; |
| 218 | + case kCCTextAlignmentRight: { |
| 219 | + line->setAnchorPoint({ 1, 1 }); |
| 220 | + line->setPosition({ width, y }); |
| 221 | + } break; |
| 222 | + } |
| 223 | + |
| 224 | + m_container->addChild(line); |
| 225 | + } |
| 226 | +} |
0 commit comments