VN Textbox can now be any size

This commit is contained in:
2022-11-06 11:45:10 -08:00
parent db90bd1476
commit 6f4ab49caa
23 changed files with 1493 additions and 1317 deletions

View File

@ -108,7 +108,7 @@ namespace Dawn {
* Sets a floating point value to the shader.
*
* @param parameter Paramater to set the float ont o.
* @param float Float to bind.
* @param Float to bind.
*/
virtual void setFloat(T parameter, float_t value) = 0;
};

View File

@ -19,20 +19,9 @@
namespace Dawn {
class DawnHost;
class DawnGame {
class IDawnGame {
public:
std::shared_ptr<Scene> scene;
DawnHost &host;
RenderManager renderManager;
AssetManager assetManager;
InputManager inputManager;
TimeManager timeManager;
/**
* Construct a new instance of the DawnGame.
*
*/
DawnGame(DawnHost &host);
/**
* Initialize the game. This is performed by the host at a time that is
@ -43,7 +32,7 @@ namespace Dawn {
* @param host Pointer to the host that is running this game.
* @return The game initialize result.
*/
int32_t init();
virtual int32_t init() = 0;
/**
* Performs a game update operation. This operation should occur exactly
@ -57,11 +46,13 @@ namespace Dawn {
* @param delta Time delta to tick the game by.
* @return The game update result.
*/
int32_t update(float_t delta);
virtual int32_t update(float_t delta) = 0;
/**
* Cleanup the memory of the DawnGame instance.
*/
~DawnGame();
virtual ~IDawnGame() {
}
};
}

View File

@ -125,6 +125,10 @@ void UIBorder::setBorderSize(glm::vec2 borderSize) {
this->updatePositions();
}
glm::vec2 UIBorder::getInnerSize() {
return glm::vec2(this->getWidth(), this->getHeight()) - this->edgeDimensions;
}
glm::vec2 UIBorder::getBorderSize() {
return this->edgeDimensions;
}

View File

@ -17,14 +17,32 @@ namespace Dawn {
glm::vec2 uv1 = glm::vec2(1.0f, 1.0f);
void updatePositions() override;
void drawSelf(UIShader &shader, glm::mat4 selfTransform) override;
public:
Texture *texture;
UIBorder(UICanvas &canvas);
void drawSelf(UIShader &shader, glm::mat4 selfTransform) override;
/**
* Changes the dimensions of the border.
*
* @param borderSize Size of the border.
*/
void setBorderSize(glm::vec2 borderSize);
/**
* Returns the size of the border.
*
* @return Border size of this UI border.
*/
glm::vec2 getBorderSize();
/**
* Returns the size of the area within the border.
*
* @return The inner content area size.
*/
glm::vec2 getInnerSize();
};
}

View File

@ -81,6 +81,11 @@ void UIComponent::updatePositions() {
(*it)->updatePositions();
++it;
}
// Fire event
eventAlignmentUpdated.invoke(
this->width, this->height, this->relativeX, this->relativeY
);
}
float_t UIComponent::getWidth() {

View File

@ -33,6 +33,9 @@ namespace Dawn {
std::vector<UIComponent*> children;
UIComponent *parent = nullptr;
// Events
Event<float_t, float_t, float_t, float_t> eventAlignmentUpdated;
// I currently don't support rotation or scale. Not because I can't but
// because it's basically un-necessary. Unity does support rotation but
// it doesn't affect how the alignment side of things work (similar to how
@ -46,6 +49,15 @@ namespace Dawn {
*/
virtual void updatePositions();
/**
* Intended to be overwritten by subclass. Called by the draw method to
* ask this child to draw.
*
* @param uiShader UI Shader for the child to use.
* @param selfTransform Self alignment transform.
*/
virtual void drawSelf(UIShader &uiShader, glm::mat4 selfTransform) = 0;
public:
UICanvas &canvas;
@ -57,26 +69,60 @@ namespace Dawn {
* @return Width of the component.
*/
float_t getWidth();
/**
* Returns the calculated height, based on the internal alignment values.
*
* @return Height of the component.
*/
float_t getHeight();
/**
* Returns the X position, relative to this components' parent.
*
* @return Relative X position.
*/
float_t getRelativeX();
/**
* Returns the Y position, relative to this components' parent.
*
* @return Relative Y position.
*/
float_t getRelativeY();
void setTransform(
/**
* Updates the transformation for this component.
*
* @param xAlign X axis alignment method.
* @param yAlign Y axis alignment method.
* @param alignment Alignment parameters, changes depending on the align.
* @param z Z position (relative to screen).
*/
virtual void setTransform(
UIComponentAlign xAlign,
UIComponentAlign yAlign,
glm::vec4 alignment,
float_t z
);
// virtual void update() = 0;
void draw(UIShader &uiShader, glm::mat4 parentTransform);
virtual void drawSelf(UIShader &uiShader, glm::mat4 selfTransform) = 0;
/**
* Adds a child to this UI Component.
*
* @param child Child UI Component to add.
*/
void addChild(UIComponent *child);
/**
* Removes a child from this UI Component.
*
* @param child Child to remove.
*/
void removeChild(UIComponent *child);
virtual ~UIComponent();
friend class UICanvas;
};
}

View File

@ -61,3 +61,13 @@ void UILabel::drawSelf(UIShader &shader, glm::mat4 selfTransform) {
this->font->draw(this->mesh, this->startQuad, this->quadCount);
}
void UILabel::setTransform(
UIComponentAlign xAlign,
UIComponentAlign yAlign,
glm::vec4 alignment,
float_t z
) {
this->needsRebuffering = true;
UIComponent::setTransform(xAlign, yAlign, alignment, z);
}

View File

@ -29,6 +29,12 @@ namespace Dawn {
UILabel(UICanvas &canvas);
void drawSelf(UIShader &shader, glm::mat4 selfTransform) override;
void setTransform(
UIComponentAlign xAlign,
UIComponentAlign yAlign,
glm::vec4 alignment,
float_t z
) override;
/**
* Internal method to force the font mesh to be recreated.

View File

@ -12,6 +12,7 @@ namespace Dawn {
class UISprite : public UIComponent {
protected:
void updatePositions() override;
void drawSelf(UIShader &uiShader, glm::mat4 selfTransform) override;
public:
Mesh mesh;
@ -23,6 +24,5 @@ namespace Dawn {
* @param canvas Canvas that this sprite belongs to.
*/
UISprite(UICanvas &canvas);
void drawSelf(UIShader &uiShader, glm::mat4 selfTransform) override;
};
}

View File

@ -3,6 +3,11 @@
# 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)

View File

@ -3,6 +3,10 @@
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "game/DawnGame.hpp"
#include "scene/components/Components.hpp"
#include "VisualNovelManager.hpp"
using namespace Dawn;
VisualNovelManager::VisualNovelManager() {
}

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

View File

@ -21,8 +21,6 @@ VisualNovelTextbox::VisualNovelTextbox(UICanvas &canvas) :
this->label.startQuad = 0;
this->label.quadCount = 0;
this->updateSelfTransform();
this->canvas.getScene().eventSceneUnpausedUpdate.addListener(
this, &VisualNovelTextbox::textboxOnSceneUpdate
);
@ -60,11 +58,11 @@ void VisualNovelTextbox::textboxOnSceneUpdate() {
if(this->hasRevealedAllCurrentCharacters()) {
if(this->hasRevealedAllCharacters()) {
if(game.inputManager.isPressed(INPUT_BIND_ACCEPT)) {
std::cout << "Bruh" << std::endl;
this->eventClose.invoke();
}
} else {
if(game.inputManager.isPressed(INPUT_BIND_ACCEPT)) {
this->lineCurrent += VISUAL_NOVEL_TEXTBOX_LINES_MAX;
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);
@ -85,29 +83,35 @@ void VisualNovelTextbox::textboxOnSceneUpdate() {
),
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;
}
this->label.quadCount = (int32_t)mathFloorFloat(this->timeCharacter);
auto newTimeCharacter = (int32_t)mathFloorFloat(this->timeCharacter);
if(newTimeCharacter == lastTimeCharacter) return;
this->label.quadCount = newTimeCharacter;
this->eventCharacterRevealed.invoke();
}
void VisualNovelTextbox::updateSelfTransform() {
float_t height;
int32_t VisualNovelTextbox::getCountOfVisibleLines() {
int32_t i = 1;
glm::vec2 innerSize = this->border.getInnerSize() - this->labelPadding;
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
);
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) {}
@ -121,14 +125,12 @@ void VisualNovelTextbox::setBorder(Texture *texture, glm::vec2 dimensions) {
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() {
@ -137,7 +139,7 @@ bool_t VisualNovelTextbox::hasRevealedAllCurrentCharacters() {
int32_t i = this->lineCurrent;
i < mathMin<int32_t>(
this->label.measure.getLineCount(),
this->lineCurrent + VISUAL_NOVEL_TEXTBOX_LINES_MAX
this->lineCurrent + this->getCountOfVisibleLines()
);
i++
) {
@ -148,7 +150,7 @@ bool_t VisualNovelTextbox::hasRevealedAllCurrentCharacters() {
bool_t VisualNovelTextbox::hasRevealedAllCharacters() {
return (
this->lineCurrent + VISUAL_NOVEL_TEXTBOX_LINES_MAX >=
this->lineCurrent + this->getCountOfVisibleLines() >=
this->label.measure.getLineCount()
);
}

View File

@ -11,7 +11,6 @@
#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 {
@ -23,13 +22,31 @@ namespace Dawn {
float_t timeCharacter = 0.0f;
void updatePositions() override;
void drawSelf(UIShader &shader, glm::mat4 selfTransform) override;
/**
* Listens for scene updates.
*/
void textboxOnSceneUpdate();
void updateSelfTransform();
/**
* 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 drawSelf(UIShader &shader, glm::mat4 selfTransform) override;
void setFont(Font *font);
void setBorder(Texture *texture, glm::vec2 dimensions);

View File

@ -17,3 +17,4 @@ target_include_directories(${DAWN_TARGET_NAME}
# Subdirs
add_subdirectory(game)
add_subdirectory(ui)

View File

@ -6,7 +6,7 @@
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
DawnPokerGame.cpp
DawnGame.cpp
)
tool_texture(texture_test texture_test.png texture_test)

View File

@ -3,7 +3,7 @@
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "DawnPokerGame.hpp"
#include "DawnGame.hpp"
#include "asset/assets/TextureAsset.hpp"
#include "asset/assets/TrueTypeAsset.hpp"
#include "visualnovel/ui/VisualNovelTextbox.hpp"
@ -41,16 +41,12 @@ int32_t DawnGame::init() {
auto textbox = canvas->addElement<VisualNovelTextbox>();
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(
// UI_COMPONENT_ALIGN_START,
// UI_COMPONENT_ALIGN_START,
// glm::vec4(0, 0, 200, 200),
// 0
// );
// sprite->texture = &asset->font.getTexture();
textbox->setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus leo odio, egestas nec imperdiet ac, placerat eget quam. Nam tellus justo, aliquam sed porta quis, ullamcorper in libero. Proin auctor eget elit nec rutrum. Vestibulum tincidunt sem vel nisi sagittis, sed imperdiet eros aliquet. Fusce a ultrices augue, at auctor lacus. Sed lobortis, ante vitae vehicula egestas, lorem turpis cursus dui, sit amet egestas mauris ligula non ipsum. Pellentesque scelerisque posuere lorem sit amet tempor. Praesent ac hendrerit mi. Nulla mollis diam vitae vestibulum aliquam. Nullam metus justo, viverra sed risus eu, tincidunt sodales lacus. Quisque efficitur accumsan posuere. Aliquam posuere volutpat diam quis lacinia. Nullam blandit nulla vestibulum mi placerat varius. Proin egestas lacus nec pellentesque iaculis. Vestibulum ex metus, congue in eleifend et, scelerisque a nulla. Pellentesque cursus lectus sed arcu efficitur tincidunt. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nulla a felis non velit fermentum ullamcorper.", 40);
textbox->setTransform(
UI_COMPONENT_ALIGN_START, UI_COMPONENT_ALIGN_START,
glm::vec4(100, 100, 300, 300),
0.0f
);
return DAWN_GAME_INIT_RESULT_SUCCESS;
}
@ -66,7 +62,3 @@ int32_t DawnGame::update(float_t delta) {
return DAWN_GAME_UPDATE_RESULT_SUCCESS;
}
DawnGame::~DawnGame() {
}

View File

@ -0,0 +1,23 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "game/_DawnGame.hpp"
#include "scene/components/Components.hpp"
namespace Dawn {
class DawnGame : public IDawnGame {
public:
DawnHost &host;
RenderManager renderManager;
AssetManager assetManager;
InputManager inputManager;
TimeManager timeManager;
DawnGame(DawnHost &host);
int32_t init() override;
int32_t update(float_t delta) override;
};
}

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
PokerGameTextbox.cpp
)

View File

@ -0,0 +1,12 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "PokerGameTextbox.hpp"
using namespace Dawn;
std::shared_ptr<VisualNovelTextbox> PokerGameTextbox::makeTextbox() {
return nullptr;
}

View File

@ -0,0 +1,14 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "visualnovel/ui/VisualNovelTextbox.hpp"
namespace Dawn {
class PokerGameTextbox {
public:
static std::shared_ptr<VisualNovelTextbox> makeTextbox();
};
}