diff --git a/src/dawn/display/font/FontMeasure.cpp b/src/dawn/display/font/FontMeasure.cpp index d1b69c99..157331af 100644 --- a/src/dawn/display/font/FontMeasure.cpp +++ b/src/dawn/display/font/FontMeasure.cpp @@ -19,6 +19,10 @@ int32_t FontMeasure::getQuadCount() { return this->realLength; } +float_t FontMeasure::getHeightOfLineCount(int32_t lineCount) { + return this->lineHeight * lineCount; +} + size_t FontMeasure::getLineCount() { return this->lines.size(); } diff --git a/src/dawn/display/font/FontMeasure.hpp b/src/dawn/display/font/FontMeasure.hpp index 7d2b67c6..edd847a9 100644 --- a/src/dawn/display/font/FontMeasure.hpp +++ b/src/dawn/display/font/FontMeasure.hpp @@ -28,6 +28,9 @@ namespace Dawn { /** Dimensions of the string */ float_t width, height; + /** Height of a single line */ + float_t lineHeight; + /** * Internal method that adds a line to the text buffer process. * @@ -40,6 +43,7 @@ namespace Dawn { float_t getHeight(); int32_t getQuadsOnLine(int32_t line); int32_t getQuadIndexOnLine(int32_t line); + float_t getHeightOfLineCount(int32_t lineCount); size_t getLineCount(); int32_t getQuadCount(); }; diff --git a/src/dawn/display/font/TrueTypeFont.cpp b/src/dawn/display/font/TrueTypeFont.cpp index f3328099..fe9a5f31 100644 --- a/src/dawn/display/font/TrueTypeFont.cpp +++ b/src/dawn/display/font/TrueTypeFont.cpp @@ -49,10 +49,10 @@ void TrueTypeFont::buffer( info->length = 0; info->realLength = 0; info->lines.clear(); - info->lines[0].start = 0; - info->lines[0].length = 0; info->height = 0; info->width = 0; + info->height = 0.0f; + info->lineHeight = 0.0f; mesh.createBuffers(0, 0); return; } @@ -72,6 +72,7 @@ void TrueTypeFont::buffer( info->length = 0; info->realLength = 0; info->lines.clear(); + info->lineHeight = this->getLineHeight(fontSize) * scale; // Prepare the line counters info->addLine(0, 0); diff --git a/src/dawn/ui/UILabel.cpp b/src/dawn/ui/UILabel.cpp index 8602d770..fb4fb85d 100644 --- a/src/dawn/ui/UILabel.cpp +++ b/src/dawn/ui/UILabel.cpp @@ -19,6 +19,7 @@ void UILabel::updatePositions() { void UILabel::updateMesh() { if(!this->needsRebuffering) return; if(this->font == nullptr) return; + if(this->text.size() == 0) return; this->font->buffer( this->text, @@ -51,12 +52,12 @@ void UILabel::setFontSize(float_t fontSize) { } void UILabel::drawSelf(UIShader &shader, glm::mat4 selfTransform) { - if(this->font == nullptr) return; + if(this->font == nullptr || this->text.size() == 0) return; this->updateMesh(); shader.setUIColor(this->textColor); shader.setUIModel(selfTransform); shader.setUITexture(&this->font->getTexture()); - this->font->draw(this->mesh, this->startQuad, this->endQuad); + this->font->draw(this->mesh, this->startQuad, this->quadCount); } \ No newline at end of file diff --git a/src/dawn/ui/UILabel.hpp b/src/dawn/ui/UILabel.hpp index 942f8e4e..fe3f725f 100644 --- a/src/dawn/ui/UILabel.hpp +++ b/src/dawn/ui/UILabel.hpp @@ -19,15 +19,10 @@ namespace Dawn { void updatePositions() override; - /** - * Internal method to force the font mesh to be recreated. - */ - void updateMesh(); - public: struct FontMeasure measure; int32_t startQuad = 0; - int32_t endQuad = -1; + int32_t quadCount = -1; /** The colour of this label */ struct Color textColor = COLOR_MAGENTA; @@ -35,6 +30,11 @@ namespace Dawn { UILabel(UICanvas &canvas); void drawSelf(UIShader &shader, glm::mat4 selfTransform) override; + /** + * Internal method to force the font mesh to be recreated. + */ + void updateMesh(); + /** * Returns the current text that the label has. * diff --git a/src/dawn/visualnovel/ui/VisualNovelTextbox.cpp b/src/dawn/visualnovel/ui/VisualNovelTextbox.cpp index 49d3b385..190ab6ec 100644 --- a/src/dawn/visualnovel/ui/VisualNovelTextbox.cpp +++ b/src/dawn/visualnovel/ui/VisualNovelTextbox.cpp @@ -18,32 +18,24 @@ VisualNovelTextbox::VisualNovelTextbox(UICanvas &canvas) : // Label this->addChild(&this->label); - this->label.setText("The quick brown fox jumps over the lazy dog."); this->label.startQuad = 0; - this->label.endQuad = 1; - - this->updatePositions(); + this->label.quadCount = 0; + + this->updateSelfTransform(); this->canvas.getScene().eventSceneUnpausedUpdate.addListener( this, &VisualNovelTextbox::textboxOnSceneUpdate ); } -void VisualNovelTextbox::textboxOnSceneUpdate() { - DawnGame &game = this->canvas.getGame(); - if(game.inputManager.isDown(INPUT_BIND_ACCEPT)) { - this->timeCharacter += game.timeManager.delta * VISUAL_NOVEL_TEXTBOX_SPEED_FASTER; - } else { - this->timeCharacter += game.timeManager.delta * VISUAL_NOVEL_TEXTBOX_SPEED; - } - this->label.endQuad = (int32_t)mathFloorFloat(this->timeCharacter); -} - void VisualNovelTextbox::updatePositions() { UIComponent::updatePositions(); + this->lineCurrent = 0; + this->timeCharacter = 0; + this->border.setTransform( - UI_COMPONENT_ALIGN_STRETCH, UI_COMPONENT_ALIGN_STRETCH, + UI_COMPONENT_ALIGN_STRETCH, UI_COMPONENT_ALIGN_STRETCH, glm::vec4(0, 0, 0, 0), 0.0f ); @@ -57,10 +49,108 @@ void VisualNovelTextbox::updatePositions() { ), 1.0f ); + + this->label.startQuad = 0; + this->label.quadCount = 0; } -void VisualNovelTextbox::drawSelf(UIShader &shader, glm::mat4 self) { - +void VisualNovelTextbox::textboxOnSceneUpdate() { + DawnGame &game = this->canvas.getGame(); + + if(this->hasRevealedAllCurrentCharacters()) { + if(this->hasRevealedAllCharacters()) { + if(game.inputManager.isPressed(INPUT_BIND_ACCEPT)) { + std::cout << "Bruh" << std::endl; + } + } else { + if(game.inputManager.isPressed(INPUT_BIND_ACCEPT)) { + this->lineCurrent += VISUAL_NOVEL_TEXTBOX_LINES_MAX; + this->label.startQuad = 0; + for(int32_t i = 0; i < this->lineCurrent; i++) { + this->label.startQuad += this->label.measure.getQuadsOnLine(i); + } + this->label.quadCount = 0; + this->timeCharacter = 0.0f; + + this->label.setTransform( + UI_COMPONENT_ALIGN_STRETCH, + UI_COMPONENT_ALIGN_STRETCH, + glm::vec4( + glm::vec2( + this->border.getBorderSize().x + this->labelPadding.x, + this->border.getBorderSize().y + this->labelPadding.y - + this->label.measure.getHeightOfLineCount(this->lineCurrent) + ), + this->border.getBorderSize() + this->labelPadding + ), + 1.0f + ); + } + } + return; + } + + if(game.inputManager.isDown(INPUT_BIND_ACCEPT)) { + this->timeCharacter += game.timeManager.delta * VISUAL_NOVEL_TEXTBOX_SPEED_FASTER; + } else { + this->timeCharacter += game.timeManager.delta * VISUAL_NOVEL_TEXTBOX_SPEED; + } + this->label.quadCount = (int32_t)mathFloorFloat(this->timeCharacter); +} + +void VisualNovelTextbox::updateSelfTransform() { + float_t height; + + height = this->label.measure.getHeightOfLineCount(VISUAL_NOVEL_TEXTBOX_LINES_MAX); + + this->setTransform( + UI_COMPONENT_ALIGN_STRETCH, UI_COMPONENT_ALIGN_START, + glm::vec4(0, 0, 0, height + (this->border.getBorderSize().y * 2)), + 0 + ); +} + +void VisualNovelTextbox::drawSelf(UIShader &shader, glm::mat4 self) {} + +void VisualNovelTextbox::setBorder(Texture *texture, glm::vec2 dimensions) { + this->border.texture = texture; + this->border.setBorderSize(dimensions); + this->updatePositions(); +} + +void VisualNovelTextbox::setFont(Font *font) { + this->label.setFont(font); + this->label.updateMesh(); + this->updateSelfTransform(); +} + +void VisualNovelTextbox::setText(std::string text, float_t fontSize) { + this->label.setText(text); + this->label.setFontSize(fontSize); + this->label.updateMesh(); + this->updateSelfTransform(); +} + +bool_t VisualNovelTextbox::hasRevealedAllCurrentCharacters() { + int32_t quadsTotal = 0; + for( + int32_t i = this->lineCurrent; + i < mathMin( + this->label.measure.getLineCount(), + this->lineCurrent + VISUAL_NOVEL_TEXTBOX_LINES_MAX + ); + i++ + ) { + quadsTotal += this->label.measure.getQuadsOnLine(i); + } + return mathFloorFloat(this->timeCharacter) >= quadsTotal; +} + +bool_t VisualNovelTextbox::hasRevealedAllCharacters() { + return ( + this->lineCurrent + VISUAL_NOVEL_TEXTBOX_LINES_MAX >= + this->label.measure.getLineCount() + ); } VisualNovelTextbox::~VisualNovelTextbox() { diff --git a/src/dawn/visualnovel/ui/VisualNovelTextbox.hpp b/src/dawn/visualnovel/ui/VisualNovelTextbox.hpp index d1c7ef76..30debc6b 100644 --- a/src/dawn/visualnovel/ui/VisualNovelTextbox.hpp +++ b/src/dawn/visualnovel/ui/VisualNovelTextbox.hpp @@ -11,24 +11,33 @@ #define VISUAL_NOVEL_TEXTBOX_SPEED 25.0f #define VISUAL_NOVEL_TEXTBOX_SPEED_FASTER 40.0f +#define VISUAL_NOVEL_TEXTBOX_LINES_MAX 4 namespace Dawn { class VisualNovelTextbox : public UIComponent { private: + int32_t lineCurrent = 0; glm::vec2 labelPadding = glm::vec2(0, 0); - - void updatePositions() override; - - void textboxOnSceneUpdate(); - - public: - float_t timeCharacter = 0.0f; UIBorder border; UILabel label; - + float_t timeCharacter = 0.0f; + + void updatePositions() override; + void textboxOnSceneUpdate(); + void updateSelfTransform(); + + public: VisualNovelTextbox(UICanvas &canvas); + void drawSelf(UIShader &shader, glm::mat4 selfTransform) override; + void setFont(Font *font); + void setBorder(Texture *texture, glm::vec2 dimensions); + void setText(std::string text, float_t fontSize); + + bool_t hasRevealedAllCurrentCharacters(); + bool_t hasRevealedAllCharacters(); + ~VisualNovelTextbox(); }; } \ No newline at end of file diff --git a/src/dawnpokergame/game/DawnPokerGame.cpp b/src/dawnpokergame/game/DawnPokerGame.cpp index 90b63026..7aa6bf0c 100644 --- a/src/dawnpokergame/game/DawnPokerGame.cpp +++ b/src/dawnpokergame/game/DawnPokerGame.cpp @@ -39,13 +39,9 @@ int32_t DawnGame::init() { } auto textbox = canvas->addElement(); - textbox->border.texture = assetTexture->texture.get(); - textbox->label.setFont(&assetFont->font); - textbox->label.setFontSize(40); - textbox->setTransform( - UI_COMPONENT_ALIGN_STRETCH, UI_COMPONENT_ALIGN_END, - glm::vec4(0, 250, 0, 0), 0.0f - ); + textbox->setBorder(assetTexture->texture.get(), glm::vec2(16, 16)); + textbox->setFont(&assetFont->font); + textbox->setText("1\n2\n3\n4\n5\n6\n7\n8\n9\n10", 40); // auto sprite = canvas->addElement(); // sprite->setTransform(