VN Textbox updated

This commit is contained in:
2022-11-02 00:42:16 -07:00
parent f5e8dd9a8e
commit db90bd1476
8 changed files with 147 additions and 42 deletions

View File

@ -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();
}

View File

@ -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();
};

View File

@ -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);

View File

@ -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);
}

View File

@ -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.
*

View File

@ -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<int32_t>(
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() {

View File

@ -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();
};
}

View File

@ -39,13 +39,9 @@ int32_t DawnGame::init() {
}
auto textbox = canvas->addElement<VisualNovelTextbox>();
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<UISprite>();
// sprite->setTransform(