VN System improved.

This commit is contained in:
2022-11-19 13:42:04 -08:00
parent 1e8dfa7388
commit 4eeecced2f
28 changed files with 577 additions and 154 deletions

View File

@ -10,4 +10,5 @@ target_sources(${DAWN_TARGET_NAME}
)
# Subdirs
add_subdirectory(events)
add_subdirectory(ui)

View File

@ -7,6 +7,72 @@
using namespace Dawn;
VisualNovelManager::VisualNovelManager() {
VisualNovelManager::VisualNovelManager(SceneItem *item) :
SceneItemComponent(item)
{
this->uiCanvas = nullptr;
this->textBox = nullptr;
}
void VisualNovelManager::onStart() {
SceneItemComponent::onStart();
this->uiCanvas = getScene()->findComponent<UICanvas>();
assertNotNull(this->uiCanvas);
this->textBox = this->uiCanvas->findElement<VisualNovelTextbox>();
assertNotNull(this->textBox);
this->getScene()->eventSceneUnpausedUpdate.addListener(this, &VisualNovelManager::onUnpausedUpdate);
}
void VisualNovelManager::onUnpausedUpdate() {
if(this->currentEvent == nullptr) return;
if(!this->currentEvent->hasStarted) {
this->currentEvent->start();
}
if(this->currentEvent->update()) return;
auto oldCurrent = this->currentEvent;
this->currentEvent = this->currentEvent->end();
delete oldCurrent;
}
VisualNovelManager::~VisualNovelManager() {
if(this->currentEvent != nullptr) {
delete this->currentEvent;
}
this->getScene()->eventSceneUnpausedUpdate.removeListener(this, &VisualNovelManager::onUnpausedUpdate);
}
// Visual Novel Event
IVisualNovelEvent::IVisualNovelEvent(VisualNovelManager *man) {
assertNotNull(man);
this->manager = man;
}
void IVisualNovelEvent::start() {
this->onStart();
this->hasStarted = true;
}
bool_t IVisualNovelEvent::update() {
return this->onUpdate();
}
IVisualNovelEvent * IVisualNovelEvent::end() {
this->onEnd();
if(this->doNext != nullptr) {
auto next = this->doNext;
this->doNext = nullptr;
return next;
}
}
IVisualNovelEvent::~IVisualNovelEvent() {
if(this->doNext != nullptr) {
delete this->doNext;
}
}

View File

@ -4,13 +4,84 @@
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnlibs.hpp"
#include "scene/SceneItemComponent.hpp"
#include "visualnovel/ui/VisualNovelTextbox.hpp"
namespace Dawn {
class VisualNovelManager {
protected:
class IVisualNovelEvent;
class VisualNovelManager : public SceneItemComponent {
private:
UICanvas *uiCanvas;
IVisualNovelEvent* currentEvent = nullptr;
/** Event listener for unpaused scene updates. */
void onUnpausedUpdate();
public:
VisualNovelManager();
VisualNovelTextbox *textBox;
/**
* Constructs a visual novel manager, scene item component.
*
* @param item Item that the VN manager belongs to.
*/
VisualNovelManager(SceneItem *item);
/**
* Sets the currently active visual novel event. This is assumed to be
* the only way to handle events (no multiples currently).
*
* @param event Event to set.
*/
template <class T>
T * setEvent(T *event) {
assertNotNull(event);
this->currentEvent = event;
return event;
}
/**
* Override to the SceneItemComponent on start event.
*
*/
void onStart() override;
/**
* Dispose / Cleanup the VN manager.
*
*/
~VisualNovelManager();
friend class IVisualNovelEvent;
};
class IVisualNovelEvent {
protected:
VisualNovelManager *manager;
IVisualNovelEvent *doNext = nullptr;
bool_t hasStarted;
virtual void onStart() = 0;
virtual bool_t onUpdate() = 0;
virtual void onEnd() = 0;
public:
IVisualNovelEvent(VisualNovelManager *manager);
template<class T>
T * then(T *next) {
assertNotNull(next);
this->doNext = next;
return next;
}
void start();
bool_t update();
IVisualNovelEvent * end();
virtual ~IVisualNovelEvent();
friend class VisualNovelManager;
};
}

View File

@ -0,0 +1,10 @@
# 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
VisualNovelTextboxEvent.cpp
)

View File

@ -0,0 +1,28 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "VisualNovelTextboxEvent.hpp"
using namespace Dawn;
VisualNovelTextboxEvent::VisualNovelTextboxEvent(
VisualNovelManager *manager,
std::string text
) : IVisualNovelEvent(manager) {
this->text = text;
}
void VisualNovelTextboxEvent::onStart() {
this->manager->textBox->setText(this->text);
this->manager->textBox->show();
}
bool_t VisualNovelTextboxEvent::onUpdate() {
return this->manager->textBox->isVisible();
}
void VisualNovelTextboxEvent::onEnd() {
}

View File

@ -0,0 +1,24 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "visualnovel/VisualNovelManager.hpp"
namespace Dawn {
class VisualNovelTextboxEvent : public IVisualNovelEvent {
protected:
std::string text;
void onStart() override;
bool_t onUpdate() override;
void onEnd() override;
public:
VisualNovelTextboxEvent(
VisualNovelManager *manager,
std::string text
);
};
}

View File

@ -10,14 +10,27 @@ using namespace Dawn;
VisualNovelTextbox::VisualNovelTextbox(UICanvas *canvas) :
UIComponent(canvas),
selfParent(canvas),
border(canvas),
label(canvas)
{
// Self Parent
this->selfParent.setTransform(
UI_COMPONENT_ALIGN_STRETCH, UI_COMPONENT_ALIGN_STRETCH,
glm::vec4(0, 0, 0, 0),
0.0f
);
// Border
this->addChild(&this->border);
this->selfParent.addChild(&this->border);
this->border.setTransform(
UI_COMPONENT_ALIGN_STRETCH, UI_COMPONENT_ALIGN_STRETCH,
glm::vec4(0, 0, 0, 0),
0.0f
);
// Label
this->addChild(&this->label);
this->selfParent.addChild(&this->label);
this->label.startQuad = 0;
this->label.quadCount = 0;
@ -26,18 +39,30 @@ VisualNovelTextbox::VisualNovelTextbox(UICanvas *canvas) :
);
}
void VisualNovelTextbox::show() {
if(this->isVisible()) return;
this->visible = true;
this->addChild(&this->selfParent);
std::cout << "Showing" << std::endl;
}
void VisualNovelTextbox::hide() {
if(!this->isVisible()) return;
this->visible = false;
this->removeChild(&this->selfParent);
this->eventHidden.invoke();
}
bool_t VisualNovelTextbox::isVisible() {
return this->visible;
}
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,
@ -58,7 +83,7 @@ void VisualNovelTextbox::textboxOnSceneUpdate() {
if(this->hasRevealedAllCurrentCharacters()) {
if(this->hasRevealedAllCharacters()) {
if(game->inputManager.isPressed(INPUT_BIND_ACCEPT)) {
this->eventClose.invoke();
this->hide();
}
} else {
if(game->inputManager.isPressed(INPUT_BIND_ACCEPT)) {
@ -114,7 +139,9 @@ int32_t VisualNovelTextbox::getCountOfVisibleLines() {
return this->label.measure.getLineCount();
}
void VisualNovelTextbox::drawSelf(UIShader *shader, glm::mat4 self) {}
void VisualNovelTextbox::drawSelf(UIShader *shader, glm::mat4 self) {
}
void VisualNovelTextbox::setBorder(Texture *texture, glm::vec2 dimensions) {
this->border.texture = texture;
@ -133,6 +160,21 @@ void VisualNovelTextbox::setText(std::string text, float_t fontSize) {
this->label.updateMesh();
}
void VisualNovelTextbox::setText(std::string text) {
this->label.setText(text);
this->label.updateMesh();
}
void VisualNovelTextbox::setFontSize(float_t fontSize) {
this->label.setFontSize(fontSize);
this->label.updateMesh();
}
void VisualNovelTextbox::setLabelPadding(glm::vec2 padding) {
this->labelPadding = padding;
this->updatePositions();
}
bool_t VisualNovelTextbox::hasRevealedAllCurrentCharacters() {
int32_t quadsTotal = 0;
for(

View File

@ -7,6 +7,7 @@
#include "ui/UISprite.hpp"
#include "ui/UIBorder.hpp"
#include "ui/UILabel.hpp"
#include "ui/UIEmpty.hpp"
#include "util/mathutils.hpp"
#define VISUAL_NOVEL_TEXTBOX_SPEED 25.0f
@ -17,9 +18,11 @@ namespace Dawn {
private:
int32_t lineCurrent = 0;
glm::vec2 labelPadding = glm::vec2(0, 0);
UIEmpty selfParent;
UIBorder border;
UILabel label;
float_t timeCharacter = 0.0f;
bool_t visible = false;
void updatePositions() override;
void drawSelf(UIShader *shader, glm::mat4 selfTransform) override;
@ -42,7 +45,8 @@ namespace Dawn {
Event<> eventCurrentCharactersRevealed;
Event<> eventNewPage;
Event<> eventAllCharactersRevealed;
Event<> eventClose;
Event<> eventHidden;
Event<> eventVisible;
/**
* Constructs a VN Textbox.
@ -51,6 +55,10 @@ namespace Dawn {
*/
VisualNovelTextbox(UICanvas *canvas);
void show();
void hide();
bool_t isVisible();
/**
* Sets the font for this vn textbox. Passed to the underlying label.
*
@ -74,6 +82,28 @@ namespace Dawn {
*/
void setText(std::string text, float_t fontSize);
/**
* Sets the string (label) for this textbox.
*
* @param text Text to set.
*/
void setText(std::string text);
/**
* Sets the font size to use.
*
* @param fontSize Font size to use.
*/
void setFontSize(float_t fontSize);
/**
* Sets the padding of the label. This will increase the spacing between
* the text and the border.
*
* @param padding Padding to set.
*/
void setLabelPadding(glm::vec2 padding);
/**
* Returns true if all of the characters that can be made visible for the
* current textbox size have finished revealing, or false if not.