VN Textbox can now be any size
This commit is contained in:
@ -1,8 +1,13 @@
|
||||
# Copyright (c) 2022 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
|
||||
# Subdirs
|
||||
# Copyright (c) 2022 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Sources
|
||||
target_sources(${DAWN_TARGET_NAME}
|
||||
PRIVATE
|
||||
VisualNovelManager.cpp
|
||||
)
|
||||
|
||||
# Subdirs
|
||||
add_subdirectory(ui)
|
12
src/dawn/visualnovel/VisualNovelManager.cpp
Normal file
12
src/dawn/visualnovel/VisualNovelManager.cpp
Normal file
@ -0,0 +1,12 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "VisualNovelManager.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
VisualNovelManager::VisualNovelManager() {
|
||||
|
||||
}
|
16
src/dawn/visualnovel/VisualNovelManager.hpp
Normal file
16
src/dawn/visualnovel/VisualNovelManager.hpp
Normal file
@ -0,0 +1,16 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "dawnlibs.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class VisualNovelManager {
|
||||
protected:
|
||||
|
||||
public:
|
||||
VisualNovelManager();
|
||||
};
|
||||
}
|
@ -1,160 +1,162 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "VisualNovelTextbox.hpp"
|
||||
#include "game/DawnGame.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
VisualNovelTextbox::VisualNovelTextbox(UICanvas &canvas) :
|
||||
UIComponent(canvas),
|
||||
border(canvas),
|
||||
label(canvas)
|
||||
{
|
||||
// Border
|
||||
this->addChild(&this->border);
|
||||
|
||||
// Label
|
||||
this->addChild(&this->label);
|
||||
this->label.startQuad = 0;
|
||||
this->label.quadCount = 0;
|
||||
|
||||
this->updateSelfTransform();
|
||||
|
||||
this->canvas.getScene().eventSceneUnpausedUpdate.addListener(
|
||||
this, &VisualNovelTextbox::textboxOnSceneUpdate
|
||||
);
|
||||
}
|
||||
|
||||
void VisualNovelTextbox::updatePositions() {
|
||||
UIComponent::updatePositions();
|
||||
|
||||
this->lineCurrent = 0;
|
||||
this->timeCharacter = 0;
|
||||
|
||||
this->border.setTransform(
|
||||
UI_COMPONENT_ALIGN_STRETCH, UI_COMPONENT_ALIGN_STRETCH,
|
||||
glm::vec4(0, 0, 0, 0),
|
||||
0.0f
|
||||
);
|
||||
|
||||
this->label.setTransform(
|
||||
UI_COMPONENT_ALIGN_STRETCH,
|
||||
UI_COMPONENT_ALIGN_STRETCH,
|
||||
glm::vec4(
|
||||
this->border.getBorderSize() + this->labelPadding,
|
||||
this->border.getBorderSize() + this->labelPadding
|
||||
),
|
||||
1.0f
|
||||
);
|
||||
|
||||
this->label.startQuad = 0;
|
||||
this->label.quadCount = 0;
|
||||
}
|
||||
|
||||
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() {
|
||||
this->canvas.getScene().eventSceneUnpausedUpdate.removeListener(
|
||||
this, &VisualNovelTextbox::textboxOnSceneUpdate
|
||||
);
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "VisualNovelTextbox.hpp"
|
||||
#include "game/DawnGame.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
VisualNovelTextbox::VisualNovelTextbox(UICanvas &canvas) :
|
||||
UIComponent(canvas),
|
||||
border(canvas),
|
||||
label(canvas)
|
||||
{
|
||||
// Border
|
||||
this->addChild(&this->border);
|
||||
|
||||
// Label
|
||||
this->addChild(&this->label);
|
||||
this->label.startQuad = 0;
|
||||
this->label.quadCount = 0;
|
||||
|
||||
this->canvas.getScene().eventSceneUnpausedUpdate.addListener(
|
||||
this, &VisualNovelTextbox::textboxOnSceneUpdate
|
||||
);
|
||||
}
|
||||
|
||||
void VisualNovelTextbox::updatePositions() {
|
||||
UIComponent::updatePositions();
|
||||
|
||||
this->lineCurrent = 0;
|
||||
this->timeCharacter = 0;
|
||||
|
||||
this->border.setTransform(
|
||||
UI_COMPONENT_ALIGN_STRETCH, UI_COMPONENT_ALIGN_STRETCH,
|
||||
glm::vec4(0, 0, 0, 0),
|
||||
0.0f
|
||||
);
|
||||
|
||||
this->label.setTransform(
|
||||
UI_COMPONENT_ALIGN_STRETCH,
|
||||
UI_COMPONENT_ALIGN_STRETCH,
|
||||
glm::vec4(
|
||||
this->border.getBorderSize() + this->labelPadding,
|
||||
this->border.getBorderSize() + this->labelPadding
|
||||
),
|
||||
1.0f
|
||||
);
|
||||
|
||||
this->label.startQuad = 0;
|
||||
this->label.quadCount = 0;
|
||||
}
|
||||
|
||||
void VisualNovelTextbox::textboxOnSceneUpdate() {
|
||||
DawnGame &game = this->canvas.getGame();
|
||||
|
||||
if(this->hasRevealedAllCurrentCharacters()) {
|
||||
if(this->hasRevealedAllCharacters()) {
|
||||
if(game.inputManager.isPressed(INPUT_BIND_ACCEPT)) {
|
||||
this->eventClose.invoke();
|
||||
}
|
||||
} else {
|
||||
if(game.inputManager.isPressed(INPUT_BIND_ACCEPT)) {
|
||||
this->lineCurrent += this->getCountOfVisibleLines();
|
||||
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
|
||||
);
|
||||
this->eventNewPage.invoke();
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
auto lastTimeCharacter = (int32_t)mathFloorFloat(this->timeCharacter);
|
||||
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;
|
||||
}
|
||||
auto newTimeCharacter = (int32_t)mathFloorFloat(this->timeCharacter);
|
||||
if(newTimeCharacter == lastTimeCharacter) return;
|
||||
|
||||
this->label.quadCount = newTimeCharacter;
|
||||
this->eventCharacterRevealed.invoke();
|
||||
}
|
||||
|
||||
int32_t VisualNovelTextbox::getCountOfVisibleLines() {
|
||||
int32_t i = 1;
|
||||
glm::vec2 innerSize = this->border.getInnerSize() - this->labelPadding;
|
||||
|
||||
for(i = this->label.measure.getLineCount(); i > 0; --i) {
|
||||
if(innerSize.y >= this->label.measure.getHeightOfLineCount(i)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return this->label.measure.getLineCount();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
void VisualNovelTextbox::setText(std::string text, float_t fontSize) {
|
||||
this->label.setText(text);
|
||||
this->label.setFontSize(fontSize);
|
||||
this->label.updateMesh();
|
||||
}
|
||||
|
||||
bool_t VisualNovelTextbox::hasRevealedAllCurrentCharacters() {
|
||||
int32_t quadsTotal = 0;
|
||||
for(
|
||||
int32_t i = this->lineCurrent;
|
||||
i < mathMin<int32_t>(
|
||||
this->label.measure.getLineCount(),
|
||||
this->lineCurrent + this->getCountOfVisibleLines()
|
||||
);
|
||||
i++
|
||||
) {
|
||||
quadsTotal += this->label.measure.getQuadsOnLine(i);
|
||||
}
|
||||
return mathFloorFloat(this->timeCharacter) >= quadsTotal;
|
||||
}
|
||||
|
||||
bool_t VisualNovelTextbox::hasRevealedAllCharacters() {
|
||||
return (
|
||||
this->lineCurrent + this->getCountOfVisibleLines() >=
|
||||
this->label.measure.getLineCount()
|
||||
);
|
||||
}
|
||||
|
||||
VisualNovelTextbox::~VisualNovelTextbox() {
|
||||
this->canvas.getScene().eventSceneUnpausedUpdate.removeListener(
|
||||
this, &VisualNovelTextbox::textboxOnSceneUpdate
|
||||
);
|
||||
}
|
@ -1,43 +1,60 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "ui/UISprite.hpp"
|
||||
#include "ui/UIBorder.hpp"
|
||||
#include "ui/UILabel.hpp"
|
||||
#include "util/mathutils.hpp"
|
||||
|
||||
#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);
|
||||
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();
|
||||
};
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "ui/UISprite.hpp"
|
||||
#include "ui/UIBorder.hpp"
|
||||
#include "ui/UILabel.hpp"
|
||||
#include "util/mathutils.hpp"
|
||||
|
||||
#define VISUAL_NOVEL_TEXTBOX_SPEED 25.0f
|
||||
#define VISUAL_NOVEL_TEXTBOX_SPEED_FASTER 40.0f
|
||||
|
||||
namespace Dawn {
|
||||
class VisualNovelTextbox : public UIComponent {
|
||||
private:
|
||||
int32_t lineCurrent = 0;
|
||||
glm::vec2 labelPadding = glm::vec2(0, 0);
|
||||
UIBorder border;
|
||||
UILabel label;
|
||||
float_t timeCharacter = 0.0f;
|
||||
|
||||
void updatePositions() override;
|
||||
void drawSelf(UIShader &shader, glm::mat4 selfTransform) override;
|
||||
|
||||
/**
|
||||
* Listens for scene updates.
|
||||
*/
|
||||
void textboxOnSceneUpdate();
|
||||
|
||||
/**
|
||||
* Returns the count of visible lines within the textbox. Mostly used for
|
||||
* when we need to decide how to wrap.
|
||||
*
|
||||
* @return The count of visible lines.
|
||||
*/
|
||||
int32_t getCountOfVisibleLines();
|
||||
|
||||
public:
|
||||
Event<> eventCharacterRevealed;
|
||||
Event<> eventCurrentCharactersRevealed;
|
||||
Event<> eventNewPage;
|
||||
Event<> eventAllCharactersRevealed;
|
||||
Event<> eventClose;
|
||||
|
||||
VisualNovelTextbox(UICanvas &canvas);
|
||||
|
||||
|
||||
|
||||
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();
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user