VN Textbox Scroll initial version

This commit is contained in:
2023-05-20 22:13:22 -07:00
parent d3ac3b688b
commit f8ca5f2e17
16 changed files with 347 additions and 72 deletions

View File

@ -19,7 +19,7 @@ int32_t FontMeasure::getQuadCount() {
return this->realLength;
}
float_t FontMeasure::getHeightOfLineCount(int32_t lineCount) {
float_t FontMeasure::getHeightOfLineCount(size_t lineCount) {
assertTrue(lineCount > 0);
return this->lineHeight * lineCount;
}
@ -28,13 +28,13 @@ size_t FontMeasure::getLineCount() {
return this->lines.size();
}
int32_t FontMeasure::getQuadsOnLine(int32_t line) {
int32_t FontMeasure::getQuadsOnLine(size_t line) {
assertTrue(line >= 0);
assertTrue(line < this->lines.size());
return this->lines[line].length;
}
int32_t FontMeasure::getQuadIndexOnLine(int32_t line) {
int32_t FontMeasure::getQuadIndexOnLine(size_t line) {
assertTrue(line >= 0);
assertTrue(line < this->lines.size());
return this->lines[line].start;

View File

@ -60,7 +60,7 @@ namespace Dawn {
* @param line Which line to get the count of quads for.
* @return Count of quads on that line.
*/
int32_t getQuadsOnLine(int32_t line);
int32_t getQuadsOnLine(size_t line);
/**
* Returns the index, of which quad is the first quad on the given line.
@ -68,7 +68,7 @@ namespace Dawn {
* @param line Line to get the quad index of.
* @return The quad index of that line.
*/
int32_t getQuadIndexOnLine(int32_t line);
int32_t getQuadIndexOnLine(size_t line);
/**
* Returns the height of the count of lines provided.
@ -76,7 +76,7 @@ namespace Dawn {
* @param lineCount Count of lines to get the height of.
* @return Height of the given count of lines.
*/
float_t getHeightOfLineCount(int32_t lineCount);
float_t getHeightOfLineCount(size_t lineCount);
/**
* Returns the count of lines in this string.

View File

@ -7,4 +7,5 @@
target_sources(${DAWN_TARGET_NAME}
PRIVATE
VNManager.cpp
VNTextboxScroller.cpp
)

View File

@ -0,0 +1,110 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "VNTextboxScroller.hpp"
#include "game/DawnGame.hpp"
using namespace Dawn;
VNTextboxScroller::VNTextboxScroller(SceneItem *item) :
SceneItemComponent(item),
label(nullptr)
{
}
void VNTextboxScroller::onStart() {
assertNotNull(label);
std::function<void()> x = [&]{
this->lineCurrent = 0;
this->timeCharacter = 0;
this->label->startQuad = 0;
this->label->quadCount = 0;
};
x();
useEvent([&](float_t delta){
auto game = this->getGame();
this->timeCharacter += delta;
if(this->hasRevealedAllCurrentCharacters()) {
if(this->hasRevealedAllCharacters()) {
if(game->inputManager.isPressed(INPUT_BIND_ACCEPT)) {
std::cout << "HIDE" << std::endl;
}
} 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
// ),
// 5.0f
// );
// this->eventNewPage.invoke();
}
}
return;
}
auto lastTimeCharacter = mathFloor<int32_t>(this->timeCharacter);
if(game->inputManager.isDown(INPUT_BIND_ACCEPT)) {
this->timeCharacter += game->timeManager.delta * VN_TEXTBOX_SPEED_FASTER;
} else {
this->timeCharacter += game->timeManager.delta * VN_TEXTBOX_SPEED;
}
this->label->quadCount = mathFloor<int32_t>(this->timeCharacter);
// this->eventCharacterRevealed.invoke();
}, getScene()->eventSceneUpdate);
}
size_t VNTextboxScroller::getCountOfVisibleLines() {
float_t y = this->label->getHeight();
size_t i = 1;
for(i = this->label->measure.getLineCount(); i > 0; --i) {
if(y >= this->label->measure.getHeightOfLineCount(i)) {
return i;
}
}
return this->label->measure.getLineCount();
}
bool_t VNTextboxScroller::hasRevealedAllCurrentCharacters() {
int32_t quadsTotal = 0;
for(
size_t i = this->lineCurrent;
i < mathMin<size_t>(
this->label->measure.getLineCount(),
this->lineCurrent + this->getCountOfVisibleLines()
);
i++
) {
quadsTotal += this->label->measure.getQuadsOnLine(i);
}
return mathFloor<int32_t>(this->timeCharacter) >= quadsTotal;
}
bool_t VNTextboxScroller::hasRevealedAllCharacters() {
return (
this->lineCurrent + this->getCountOfVisibleLines() >=
this->label->measure.getLineCount()
);
}

View File

@ -0,0 +1,53 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/SceneItemComponent.hpp"
#include "scene/components/ui/UILabel.hpp"
#include "input/InputManager.hpp"
#define VN_TEXTBOX_SPEED 25.0f
#define VN_TEXTBOX_SPEED_FASTER 40.0f
namespace Dawn {
class VNTextboxScroller : public SceneItemComponent {
protected:
public:
// @optional
StateProperty<UILabel*> label;
size_t lineCurrent = 0;
float_t timeCharacter = 0.0f;
VNTextboxScroller(SceneItem *item);
virtual void onStart() override;
/**
* 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.
*/
size_t getCountOfVisibleLines();
/**
* Returns true if all of the characters that can be made visible for the
* current textbox size have finished revealing, or false if not.
*
* @return True if above statement is met.
*/
bool_t hasRevealedAllCurrentCharacters();
/**
* Returns true only when every character passed previously in setText
* has been revealed by scrolling.
*
* @return True if above statement is true.
*/
bool_t hasRevealedAllCharacters();
};
}

View File

@ -14,13 +14,6 @@ namespace Dawn {
protected:
void onStart() override {
std::cout << "TEXT: " << text << std::endl;
useEvent([&](float_t delta) {
if(getScene()->game->inputManager.isPressed(INPUT_BIND_ACCEPT)) {
std::cout << "Text Advance" << std::endl;
this->next();
}
}, getScene()->eventSceneUpdate);
}
};
}

View File

@ -13,7 +13,9 @@ UILabel::UILabel(SceneItem *item) :
text(""),
fontSize(10.0f),
font(&item->scene->game->renderManager.defaultFont),
maxWidth(UI_LABEL_MAX_WIDTH_ALIGN)
maxWidth(UI_LABEL_MAX_WIDTH_ALIGN),
startQuad(0),
quadCount(-1)
{
}
@ -30,7 +32,12 @@ void UILabel::updateMesh() {
if(!this->hasText()) return;
float_t width = this->maxWidth;
assertTrue(width == UI_LABEL_MAX_WIDTH_NONE || width == UI_LABEL_MAX_WIDTH_ALIGN || width > 0);
assertTrue(
width == UI_LABEL_MAX_WIDTH_NONE ||
width == UI_LABEL_MAX_WIDTH_ALIGN ||
width > 0
);
if(width == UI_LABEL_MAX_WIDTH_ALIGN) {
auto dimensional = this->getParentDimensional();
auto align = (glm::vec4)this->alignment;
@ -56,6 +63,8 @@ void UILabel::updateMesh() {
);
this->needsRebuffering = false;
this->eventFontRebuffered.invoke();
}
std::vector<struct ShaderPassItem> UILabel::getPassItems(
@ -100,7 +109,7 @@ void UILabel::onStart() {
useEffect([&]{
alignmentNeedsUpdating = true;
}, { &fontSize, &font, &text, &maxWidth });
}, { &fontSize, &font, &text, &maxWidth, &startQuad, &quadCount });
useEffect([&]{
needsRebuffering = true;

View File

@ -39,10 +39,14 @@ namespace Dawn {
StateProperty<float_t> maxWidth;
/* @optional */
struct Color textColor = COLOR_WHITE;
// @optional
StateProperty<int32_t> startQuad;
// @optional
StateProperty<int32_t> quadCount;
StateEvent<> eventFontRebuffered;
struct FontMeasure measure;
int32_t startQuad = 0;
int32_t quadCount = -1;
UILabel(SceneItem *item);