// Copyright (c) 2023 Dominic Masters // // This software is released under the MIT License. // https://opensource.org/licenses/MIT #include "UILabel.hpp" using namespace Dawn; void UILabel::getSelfQuads(UICanvas &ctx) { std::vector quads; if(this->texture == nullptr || this->text.empty()) return; glm::vec4 quad; glm::vec2 pos = glm::vec2(0, this->texture->fontSize); bool_t lastCharWasSpace = false; for(size_t i = 0; i < text.size(); i++) { wchar_t c = text[i]; auto info = texture->getCharacterData(c); // Newline(s) if(c == L'\n') { pos.x = 0; pos.y += this->texture->fontSize; continue; } // Spaces if(c == L' ') { pos.x += info.advance.x; lastCharWasSpace = true; continue; } // Word Wrap if(wordWrap) { if(lastCharWasSpace) { // Scan ahead to next space float_t wordWidth = pos.x;// Start at current position and scan ahead. for(size_t j = i; j < text.size(); j++) { wchar_t c2 = text[j]; if(c2 == L' ' || c2 == L'\n') { break;// If we hit another space, we are OK. } // Will this character fit on the row? If not the whole word will wrap. auto info2 = texture->getCharacterData(c); wordWidth += info.advance.x; if(wordWidth > size.x) { pos.x = 0; pos.y += this->texture->fontSize; break; } } lastCharWasSpace = false; } } else if(pos.x + info.size.x > position.x + size.x) { // Not word wrap, but instead just overflow characters. pos.x = 0; pos.y += this->texture->fontSize; } ctx.addQuad( { position.x + pos.x + info.offset.x, position.y + pos.y + info.offset.y, position.x + pos.x + info.size.x + info.offset.x, position.y + pos.y + info.size.y + info.offset.y }, { info.quad.x, info.quad.y, info.quad.z, info.quad.w }, COLOR_WHITE, UIShaderQuadStyle::FONT, texture->texture ); pos += info.advance; } } std::shared_ptr UILabel::getFont() { return this->texture; } std::wstring UILabel::getText() { return this->text; } void UILabel::setFont(std::shared_ptr texture) { this->texture = texture; } void UILabel::setText(const std::wstring &text) { this->text = text; }