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

@ -1,195 +1,195 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "TrueTypeFont.hpp"
#ifndef STB_TRUETYPE_IMPLEMENTATION
#define STB_TRUETYPE_IMPLEMENTATION
#include <stb_truetype.h>
#endif
using namespace Dawn;
void TrueTypeFont::bakeQuad(truetypequad_t *quad,float_t *x,float_t *y,char c){
stbtt_GetBakedQuad(
this->characterData,
this->texture.getWidth(), this->texture.getHeight(),
((int32_t)c) - TRUETYPE_FIRST_CHAR,
x, y, quad,
TRUETYPE_FILL_MODE
);
}
float_t TrueTypeFont::getScale(float_t scale) {
return scale / this->fontSize;
}
float_t TrueTypeFont::getSpaceSize(float_t fontSize) {
return mathRoundFloat(this->fontSize * 0.3f);
}
float_t TrueTypeFont::getInitialLineHeight(float_t fontSize) {
return 8.0f;
}
void TrueTypeFont::buffer(
std::string text,
float_t fontSize,
float_t maxWidth,
Mesh &mesh,
struct FontMeasure *info
) {
auto stringLength = text.length();
if(stringLength == 0) {
info->length = 0;
info->realLength = 0;
info->lines.clear();
info->height = 0;
info->width = 0;
info->height = 0.0f;
info->lineHeight = 0.0f;
mesh.createBuffers(0, 0);
return;
}
auto quads = new truetypequad_t[stringLength];
// Get the font scale
auto scale = this->getScale(fontSize);
// Adjust the max width to match the scale, and allow "no max width".
maxWidth = maxWidth == -1 ? 9999999 : maxWidth * (1 / scale);
// Which index in the original text var is the current word from
int32_t wordStart = 0;
// Setup Scales
info->length = 0;
info->realLength = 0;
info->lines.clear();
info->lineHeight = this->getLineHeight(fontSize) * scale;
// Prepare the line counters
info->addLine(0, 0);
// Reset Dimensions
info->width = info->height = 0;
// Setup the initial loop state, and X/Y coords for the quad.
int32_t i = 0;
float_t x = 0;
float_t y = this->getInitialLineHeight(fontSize);
float_t wordX = 0;
char c;
while(c = text[i++]) {
info->length++;
// When space, start of new word about to begin
if(c == FONT_SPACE) {
x += this->getSpaceSize(fontSize);
// Did this space cause a newline?
if(x > maxWidth) {
info->addLine(info->realLength, 0);
y += this->getLineHeight(fontSize);
x = 0;
}
wordX = x;
wordStart = info->realLength;
continue;
}
// New line.
if(c == FONT_NEWLINE) {
info->addLine(info->realLength, 0);
wordStart = info->realLength;
y += this->getLineHeight(fontSize);
x = 0;
continue;
}
// Generate the quad.
auto quad = quads + info->realLength;
this->bakeQuad(quad, &x, &y, c);
// Now measure the width of the line (take the right side of that quad)
if(quad->x1 > maxWidth) {
// We've exceeded the edge, go back to the start of the word and newline.
x = quad->x1 - wordX;
for(auto j = wordStart; j <= info->realLength; j++) {
quad = quads + j;
quad->x0 -= wordX;
quad->x1 -= wordX;
quad->y0 += this->getLineHeight(fontSize);
quad->y1 += this->getLineHeight(fontSize);
}
// Go back to the previous (still current) line and remove the chars
info->lines[info->lines.size() - 1].length -= info->realLength - wordStart;
// Next line begins with this word
y += this->getLineHeight(fontSize);
info->addLine(wordStart, info->realLength-wordStart);
wordX = 0;
}
info->lines[info->lines.size() - 1].length++;
info->realLength++;
}
// Initialize primitive
mesh.createBuffers(
QUAD_VERTICE_COUNT * info->realLength,
QUAD_INDICE_COUNT * info->realLength
);
for(auto j = 0; j < info->realLength; j++) {
auto quad = quads + j;
// Scale the Quad
if(scale != 1.0) {
quad->x0 *= scale;
quad->x1 *= scale;
quad->y0 *= scale;
quad->y1 *= scale;
}
// Update the dimensions.
info->width = mathMax<float_t>(info->width, quad->x1);
info->height = mathMax<float_t>(info->height, quad->y1);
// Buffer the quad.
QuadMesh::bufferQuadMesh(&mesh,
glm::vec2(quad->x0, quad->y0), glm::vec2(quad->s0, quad->t0),
glm::vec2(quad->x1, quad->y1), glm::vec2(quad->s1, quad->t1),
j * QUAD_VERTICE_COUNT, j * QUAD_INDICE_COUNT
);
}
delete quads;
}
Texture & TrueTypeFont::getTexture() {
return this->texture;
}
void TrueTypeFont::draw(Mesh &mesh, int32_t startchar, int32_t length) {
mesh.draw(
MESH_DRAW_MODE_TRIANGLES,
startchar * QUAD_INDICE_COUNT,
length == -1 ? length : length * QUAD_INDICE_COUNT
);
}
float_t TrueTypeFont::getLineHeight(float_t fontSize) {
return 13.0f;
}
float_t TrueTypeFont::getDefaultFontSize() {
return (float_t)this->fontSize;
}
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "TrueTypeFont.hpp"
#ifndef STB_TRUETYPE_IMPLEMENTATION
#define STB_TRUETYPE_IMPLEMENTATION
#include <stb_truetype.h>
#endif
using namespace Dawn;
void TrueTypeFont::bakeQuad(truetypequad_t *quad,float_t *x,float_t *y,char c){
stbtt_GetBakedQuad(
this->characterData,
this->texture.getWidth(), this->texture.getHeight(),
((int32_t)c) - TRUETYPE_FIRST_CHAR,
x, y, quad,
TRUETYPE_FILL_MODE
);
}
float_t TrueTypeFont::getScale(float_t scale) {
return scale / this->fontSize;
}
float_t TrueTypeFont::getSpaceSize(float_t fontSize) {
return mathRoundFloat(this->fontSize * 0.3f);
}
float_t TrueTypeFont::getInitialLineHeight(float_t fontSize) {
return 8.0f;
}
void TrueTypeFont::buffer(
std::string text,
float_t fontSize,
float_t maxWidth,
Mesh &mesh,
struct FontMeasure *info
) {
auto stringLength = text.length();
if(stringLength == 0) {
info->length = 0;
info->realLength = 0;
info->lines.clear();
info->height = 0;
info->width = 0;
info->height = 0.0f;
info->lineHeight = 0.0f;
mesh.createBuffers(0, 0);
return;
}
auto quads = new truetypequad_t[stringLength];
// Get the font scale
auto scale = this->getScale(fontSize);
// Adjust the max width to match the scale, and allow "no max width".
maxWidth = maxWidth == -1 ? 9999999 : maxWidth * (1 / scale);
// Which index in the original text var is the current word from
int32_t wordStart = 0;
// Setup Scales
info->length = 0;
info->realLength = 0;
info->lines.clear();
info->lineHeight = this->getLineHeight(fontSize) * scale;
// Prepare the line counters
info->addLine(0, 0);
// Reset Dimensions
info->width = info->height = 0;
// Setup the initial loop state, and X/Y coords for the quad.
int32_t i = 0;
float_t x = 0;
float_t y = this->getInitialLineHeight(fontSize);
float_t wordX = 0;
char c;
while(c = text[i++]) {
info->length++;
// When space, start of new word about to begin
if(c == FONT_SPACE) {
x += this->getSpaceSize(fontSize);
// Did this space cause a newline?
if(x > maxWidth) {
info->addLine(info->realLength, 0);
y += this->getLineHeight(fontSize);
x = 0;
}
wordX = x;
wordStart = info->realLength;
continue;
}
// New line.
if(c == FONT_NEWLINE) {
info->addLine(info->realLength, 0);
wordStart = info->realLength;
y += this->getLineHeight(fontSize);
x = 0;
continue;
}
// Generate the quad.
auto quad = quads + info->realLength;
this->bakeQuad(quad, &x, &y, c);
// Now measure the width of the line (take the right side of that quad)
if(quad->x1 > maxWidth) {
// We've exceeded the edge, go back to the start of the word and newline.
x = quad->x1 - wordX;
for(auto j = wordStart; j <= info->realLength; j++) {
quad = quads + j;
quad->x0 -= wordX;
quad->x1 -= wordX;
quad->y0 += this->getLineHeight(fontSize);
quad->y1 += this->getLineHeight(fontSize);
}
// Go back to the previous (still current) line and remove the chars
info->lines[info->lines.size() - 1].length -= info->realLength - wordStart;
// Next line begins with this word
y += this->getLineHeight(fontSize);
info->addLine(wordStart, info->realLength-wordStart);
wordX = 0;
}
info->lines[info->lines.size() - 1].length++;
info->realLength++;
}
// Initialize primitive
mesh.createBuffers(
QUAD_VERTICE_COUNT * info->realLength,
QUAD_INDICE_COUNT * info->realLength
);
for(auto j = 0; j < info->realLength; j++) {
auto quad = quads + j;
// Scale the Quad
if(scale != 1.0) {
quad->x0 *= scale;
quad->x1 *= scale;
quad->y0 *= scale;
quad->y1 *= scale;
}
// Update the dimensions.
info->width = mathMax<float_t>(info->width, quad->x1);
info->height = mathMax<float_t>(info->height, quad->y1);
// Buffer the quad.
QuadMesh::bufferQuadMesh(&mesh,
glm::vec2(quad->x0, quad->y0), glm::vec2(quad->s0, quad->t0),
glm::vec2(quad->x1, quad->y1), glm::vec2(quad->s1, quad->t1),
j * QUAD_VERTICE_COUNT, j * QUAD_INDICE_COUNT
);
}
delete quads;
}
Texture & TrueTypeFont::getTexture() {
return this->texture;
}
void TrueTypeFont::draw(Mesh &mesh, int32_t startchar, int32_t length) {
mesh.draw(
MESH_DRAW_MODE_TRIANGLES,
startchar * QUAD_INDICE_COUNT,
length == -1 ? length : length * QUAD_INDICE_COUNT
);
}
float_t TrueTypeFont::getLineHeight(float_t fontSize) {
return 13.0f;
}
float_t TrueTypeFont::getDefaultFontSize() {
return (float_t)this->fontSize;
}

View File

@ -1,83 +1,83 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "Font.hpp"
namespace Dawn {
/** Which character (ASCII) to start the font from */
#define TRUETYPE_FIRST_CHAR 32
/** How many characters (after the first char) to generate */
#define TRUETYPE_NUM_CHARS 96
/** Refer to STBTT docs for OpenGL Fill Mode v d3d Fill Modes */
#define TRUETYPE_FILL_MODE 0
typedef stbtt_bakedchar truetypechar_t;
typedef stbtt_aligned_quad truetypequad_t;
class TrueTypeFont : public Font {
protected:
/**
* Calculate the quad information for a given character.
*
* @param font Font to get the character from.
* @param quad Pointer to the quad to store the quad information in.
* @param x Pointer to the X position for the quad.
* @param y Pointer to the Y position for the quad.
* @param c Character to get the quad and position information for.
*/
void bakeQuad(truetypequad_t *quad, float_t *x, float_t *y, char c);
/**
* Returns the font scale to use for rendering your desired font size at a
* font size that is different than the precompiled font size for this
* true type font. For example, let's say you render your font size at 36
* when you are precompiling it, but rather than creating a new font just
* to add a size 24, you can instead use this method to get the scale to
* use to downscale your font to match.
*
* @param font TrueType font to get the scale of.
* @param fontSize Font size you desire.
* @return The scale used to get the font size that will match.
*/
float_t getScale(float_t fontSize);
/**
* Returns the size of a space character for a given font.
*
* @param fontSize Font size of the font to get the space size for.
* @return The size of the space character.
*/
float_t getSpaceSize(float_t fontSize);
/**
* Returns the initial line height of a font.
*
* @param fontSize Font size for the font to get the line height of.
* @return The line height initial value.
*/
float_t getInitialLineHeight(float_t fontSize);
public:
Texture texture;
int32_t fontSize;
truetypechar_t characterData[TRUETYPE_NUM_CHARS];
void buffer(
std::string text,
float_t fontSize,
float_t maxWidth,
Mesh &mesh,
struct FontMeasure *info
) override;
Texture & getTexture() override;
void draw(Mesh &mesh, int32_t startCharacter, int32_t length) override;
float_t getLineHeight(float_t fontSize) override;
float_t getDefaultFontSize() override;
};
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "Font.hpp"
namespace Dawn {
/** Which character (ASCII) to start the font from */
#define TRUETYPE_FIRST_CHAR 32
/** How many characters (after the first char) to generate */
#define TRUETYPE_NUM_CHARS 96
/** Refer to STBTT docs for OpenGL Fill Mode v d3d Fill Modes */
#define TRUETYPE_FILL_MODE 0
typedef stbtt_bakedchar truetypechar_t;
typedef stbtt_aligned_quad truetypequad_t;
class TrueTypeFont : public Font {
protected:
/**
* Calculate the quad information for a given character.
*
* @param font Font to get the character from.
* @param quad Pointer to the quad to store the quad information in.
* @param x Pointer to the X position for the quad.
* @param y Pointer to the Y position for the quad.
* @param c Character to get the quad and position information for.
*/
void bakeQuad(truetypequad_t *quad, float_t *x, float_t *y, char c);
/**
* Returns the font scale to use for rendering your desired font size at a
* font size that is different than the precompiled font size for this
* true type font. For example, let's say you render your font size at 36
* when you are precompiling it, but rather than creating a new font just
* to add a size 24, you can instead use this method to get the scale to
* use to downscale your font to match.
*
* @param font TrueType font to get the scale of.
* @param fontSize Font size you desire.
* @return The scale used to get the font size that will match.
*/
float_t getScale(float_t fontSize);
/**
* Returns the size of a space character for a given font.
*
* @param fontSize Font size of the font to get the space size for.
* @return The size of the space character.
*/
float_t getSpaceSize(float_t fontSize);
/**
* Returns the initial line height of a font.
*
* @param fontSize Font size for the font to get the line height of.
* @return The line height initial value.
*/
float_t getInitialLineHeight(float_t fontSize);
public:
Texture texture;
int32_t fontSize;
truetypechar_t characterData[TRUETYPE_NUM_CHARS];
void buffer(
std::string text,
float_t fontSize,
float_t maxWidth,
Mesh &mesh,
struct FontMeasure *info
) override;
Texture & getTexture() override;
void draw(Mesh &mesh, int32_t startCharacter, int32_t length) override;
float_t getLineHeight(float_t fontSize) override;
float_t getDefaultFontSize() override;
};
}

View File

@ -1,115 +1,115 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "display/Texture.hpp"
namespace Dawn {
class Material;
enum ShaderParameterType {
SHADER_PARAMETER_TYPE_MATRIX,
SHADER_PARAMETER_TYPE_BOOLEAN,
SHADER_PARAMETER_TYPE_COLOR,
SHADER_PARAMETER_TYPE_VECTOR3,
SHADER_PARAMETER_TYPE_TEXTURE,
SHADER_PARAMETER_TYPE_FLOAT
};
template<typename T>
class IShader {
public:
/**
* Attaches the supplied shader as the current shader.
*/
virtual void bind() = 0;
/**
* Requested by the Material to set the default parameters of the shader.
* Each parameter really should have a default value set so that there is
* no nullptr's or other issues.
*
* @param material Material to set the default parameters on to.
*/
virtual void setDefaultParameters(Material &material) = 0;
/**
* Requested by the render pipeline (typically) to set global level (once
* per frame) parameters.
*
* @param projection Projection matrix of the current viewport.
* @param view View matrix of the current viewport.
*/
virtual void setGlobalParameters(
glm::mat4 projection,
glm::mat4 view
) = 0;
/**
* Requested by the render pipeline (typically) to set mesh-level params.
* This may be performed multiple times per frame.
*
* @param position Matrix of the position of the mesh.
*/
virtual void setMeshParameters(glm::mat4 position) = 0;
/**
* Retreives the list of all parameters that the shader supports. This
* should not include the GLOBAL shader parameters (listed above) since
* those will be modified by the engine directly.
*
* @return Key-Value-Pair of Shader parameters and their type.
*/
virtual std::map<T, enum ShaderParameterType> getParameters() = 0;
/**
* Set's a specific shader parameter to a matrix.
*
* @param parameter parameter on the shader to set.
* @param matrix Matrix to apply.
*/
virtual void setMatrix(T parameter, glm::mat4 matrix) = 0;
/**
* Attaches a boolean to a shader.
*
* @param parameter parameter to set.
* @param value Value to set.
*/
virtual void setBoolean(T parameter, bool_t value) = 0;
/**
* Set a color on to the shader.
*
* @param parameter parameter to set the color to.
* @param color Color to set.
*/
virtual void setColor(T parameter, struct Color color) = 0;
/**
* Set a 3D vector on to the shader.
*
* @param parameter parameter to set the vector to.
* @param vector Vector to set.
*/
virtual void setVector3(T parameter, glm::vec3 vector) = 0;
/**
* Attaches a texture to the currently bound shader.
*
* @param parameter parameter to set the texture on to.
* @param texture Texture to bind to the parameter.
*/
virtual void setTexture(T parameter, Texture *texture) = 0;
/**
* Sets a floating point value to the shader.
*
* @param parameter Paramater to set the float ont o.
* @param float Float to bind.
*/
virtual void setFloat(T parameter, float_t value) = 0;
};
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "display/Texture.hpp"
namespace Dawn {
class Material;
enum ShaderParameterType {
SHADER_PARAMETER_TYPE_MATRIX,
SHADER_PARAMETER_TYPE_BOOLEAN,
SHADER_PARAMETER_TYPE_COLOR,
SHADER_PARAMETER_TYPE_VECTOR3,
SHADER_PARAMETER_TYPE_TEXTURE,
SHADER_PARAMETER_TYPE_FLOAT
};
template<typename T>
class IShader {
public:
/**
* Attaches the supplied shader as the current shader.
*/
virtual void bind() = 0;
/**
* Requested by the Material to set the default parameters of the shader.
* Each parameter really should have a default value set so that there is
* no nullptr's or other issues.
*
* @param material Material to set the default parameters on to.
*/
virtual void setDefaultParameters(Material &material) = 0;
/**
* Requested by the render pipeline (typically) to set global level (once
* per frame) parameters.
*
* @param projection Projection matrix of the current viewport.
* @param view View matrix of the current viewport.
*/
virtual void setGlobalParameters(
glm::mat4 projection,
glm::mat4 view
) = 0;
/**
* Requested by the render pipeline (typically) to set mesh-level params.
* This may be performed multiple times per frame.
*
* @param position Matrix of the position of the mesh.
*/
virtual void setMeshParameters(glm::mat4 position) = 0;
/**
* Retreives the list of all parameters that the shader supports. This
* should not include the GLOBAL shader parameters (listed above) since
* those will be modified by the engine directly.
*
* @return Key-Value-Pair of Shader parameters and their type.
*/
virtual std::map<T, enum ShaderParameterType> getParameters() = 0;
/**
* Set's a specific shader parameter to a matrix.
*
* @param parameter parameter on the shader to set.
* @param matrix Matrix to apply.
*/
virtual void setMatrix(T parameter, glm::mat4 matrix) = 0;
/**
* Attaches a boolean to a shader.
*
* @param parameter parameter to set.
* @param value Value to set.
*/
virtual void setBoolean(T parameter, bool_t value) = 0;
/**
* Set a color on to the shader.
*
* @param parameter parameter to set the color to.
* @param color Color to set.
*/
virtual void setColor(T parameter, struct Color color) = 0;
/**
* Set a 3D vector on to the shader.
*
* @param parameter parameter to set the vector to.
* @param vector Vector to set.
*/
virtual void setVector3(T parameter, glm::vec3 vector) = 0;
/**
* Attaches a texture to the currently bound shader.
*
* @param parameter parameter to set the texture on to.
* @param texture Texture to bind to the parameter.
*/
virtual void setTexture(T parameter, Texture *texture) = 0;
/**
* Sets a floating point value to the shader.
*
* @param parameter Paramater to set the float ont o.
* @param Float to bind.
*/
virtual void setFloat(T parameter, float_t value) = 0;
};
}

View File

@ -1,67 +1,58 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnlibs.hpp"
#include "scene/Scene.hpp"
#include "display/RenderManager.hpp"
#include "asset/AssetManager.hpp"
#include "input/InputManager.hpp"
#include "time/TimeManager.hpp"
#include "input/InputBinds.hpp"
#define DAWN_GAME_INIT_RESULT_SUCCESS 0
#define DAWN_GAME_UPDATE_RESULT_SUCCESS 0
#define DAWN_GAME_UPDATE_RESULT_EXIT 1
namespace Dawn {
class DawnHost;
class DawnGame {
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
* deemed to have the host ready for the game's initialization. This will
* return an initialize result, where DAWN_GAME_INIT_RESULT_SUCCESS is
* the only "successful" result, anything else is deemed a failure state.
*
* @param host Pointer to the host that is running this game.
* @return The game initialize result.
*/
int32_t init();
/**
* Performs a game update operation. This operation should occur exactly
* once per frame, synchronously on the main thread. Updates can only
* have two valid exit results, either DAWN_GAME_UPDATE_RESULT_SUCCESS for
* a successful update, and request that we continue to update, or
* DAWN_GAME_UPDATE_RESULT_EXIT for a successful update but to request
* that no more update operations occur. Any other result is considered a
* failure state.
*
* @param delta Time delta to tick the game by.
* @return The game update result.
*/
int32_t update(float_t delta);
/**
* Cleanup the memory of the DawnGame instance.
*/
~DawnGame();
};
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnlibs.hpp"
#include "scene/Scene.hpp"
#include "display/RenderManager.hpp"
#include "asset/AssetManager.hpp"
#include "input/InputManager.hpp"
#include "time/TimeManager.hpp"
#include "input/InputBinds.hpp"
#define DAWN_GAME_INIT_RESULT_SUCCESS 0
#define DAWN_GAME_UPDATE_RESULT_SUCCESS 0
#define DAWN_GAME_UPDATE_RESULT_EXIT 1
namespace Dawn {
class DawnHost;
class IDawnGame {
public:
std::shared_ptr<Scene> scene;
/**
* Initialize the game. This is performed by the host at a time that is
* deemed to have the host ready for the game's initialization. This will
* return an initialize result, where DAWN_GAME_INIT_RESULT_SUCCESS is
* the only "successful" result, anything else is deemed a failure state.
*
* @param host Pointer to the host that is running this game.
* @return The game initialize result.
*/
virtual int32_t init() = 0;
/**
* Performs a game update operation. This operation should occur exactly
* once per frame, synchronously on the main thread. Updates can only
* have two valid exit results, either DAWN_GAME_UPDATE_RESULT_SUCCESS for
* a successful update, and request that we continue to update, or
* DAWN_GAME_UPDATE_RESULT_EXIT for a successful update but to request
* that no more update operations occur. Any other result is considered a
* failure state.
*
* @param delta Time delta to tick the game by.
* @return The game update result.
*/
virtual int32_t update(float_t delta) = 0;
/**
* Cleanup the memory of the DawnGame instance.
*/
virtual ~IDawnGame() {
}
};
}

View File

@ -1,130 +1,134 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "UIBorder.hpp"
using namespace Dawn;
UIBorder::UIBorder(UICanvas &canvas) : UIComponent(canvas) {
this->mesh.createBuffers(QUAD_VERTICE_COUNT * 9, QUAD_INDICE_COUNT * 9);
this->texture = new Texture();
this->texture->setSize(3, 3);
struct Color pixels[9] = {
COLOR_WHITE, COLOR_RED, COLOR_BLUE,
COLOR_GREEN, COLOR_MAGENTA, COLOR_BLACK,
COLOR_CORNFLOWER_BLUE, COLOR_WHITE, COLOR_MAGENTA
};
this->texture->buffer(pixels);
this->updatePositions();
}
void UIBorder::updatePositions() {
UIComponent::updatePositions();
glm::vec2 oneThird = this->uv1 / 3.0f;
glm::vec2 overallDimensions = glm::vec2(this->getWidth(), this->getHeight());
glm::vec2 innerDimensions = overallDimensions - (this->edgeDimensions * 2.0f);
// Top Left.
QuadMesh::bufferQuadMesh(&this->mesh,
glm::vec2(0, 0),
this->uv0,
edgeDimensions,
this->uv0 + oneThird,
0, 0
);
// Top Center
QuadMesh::bufferQuadMesh(&this->mesh,
glm::vec2(edgeDimensions.x, 0),
this->uv0 + glm::vec2(oneThird.x, 0),
glm::vec2(edgeDimensions.x + innerDimensions.x, edgeDimensions.y),
this->uv0 + glm::vec2(oneThird.x * 2.0f, oneThird.y),
QUAD_VERTICE_COUNT, QUAD_INDICE_COUNT
);
// Top Right
QuadMesh::bufferQuadMesh(&this->mesh,
glm::vec2(edgeDimensions.x + innerDimensions.x, 0),
this->uv0 + glm::vec2(oneThird.x * 2.0f, 0),
glm::vec2(overallDimensions.x, edgeDimensions.y),
glm::vec2(this->uv1.x, this->uv0.y + oneThird.y),
QUAD_VERTICE_COUNT * 2, QUAD_INDICE_COUNT * 2
);
// Middle Left
QuadMesh::bufferQuadMesh(&this->mesh,
glm::vec2(0, edgeDimensions.y),
this->uv0 + glm::vec2(0, oneThird.y),
glm::vec2(edgeDimensions.x, edgeDimensions.y + innerDimensions.y),
this->uv0 + glm::vec2(oneThird.x, oneThird.y * 2.0f),
QUAD_VERTICE_COUNT * 3, QUAD_INDICE_COUNT * 3
);
// Center
QuadMesh::bufferQuadMesh(&this->mesh,
edgeDimensions,
this->uv0 + oneThird,
edgeDimensions + innerDimensions,
this->uv0 + (oneThird * 2.0f),
QUAD_VERTICE_COUNT * 4, QUAD_INDICE_COUNT * 4
);
// Middle Right
QuadMesh::bufferQuadMesh(&this->mesh,
edgeDimensions + glm::vec2(innerDimensions.x, 0),
this->uv0 + glm::vec2(oneThird.x * 2.0f, oneThird.y),
edgeDimensions + innerDimensions + glm::vec2(edgeDimensions.x, 0),
this->uv1 - glm::vec2(0.0f, oneThird.y),
QUAD_VERTICE_COUNT * 5, QUAD_INDICE_COUNT * 5
);
// Bottom Left
QuadMesh::bufferQuadMesh(&this->mesh,
glm::vec2(0.0f, edgeDimensions.y + innerDimensions.y),
this->uv0 + glm::vec2(0.0f, uv1.y - oneThird.y),
glm::vec2(edgeDimensions.x, overallDimensions.y),
this->uv1 - glm::vec2(oneThird.x * 2.0f, 0.0f),
QUAD_VERTICE_COUNT * 6, QUAD_INDICE_COUNT * 6
);
// Bottom Center
QuadMesh::bufferQuadMesh(&this->mesh,
edgeDimensions + glm::vec2(0.0f, innerDimensions.y),
this->uv1 - oneThird,
overallDimensions - glm::vec2(edgeDimensions.x, 0.0f),
this->uv1 - glm::vec2(oneThird.x, 0.0f),
QUAD_VERTICE_COUNT * 7, QUAD_INDICE_COUNT * 7
);
// Bottom Right
QuadMesh::bufferQuadMesh(&this->mesh,
overallDimensions - edgeDimensions,
this->uv1 - oneThird,
overallDimensions,
this->uv1,
QUAD_VERTICE_COUNT * 8, QUAD_INDICE_COUNT * 8
);
}
void UIBorder::drawSelf(UIShader &shader, glm::mat4 transform) {
if(this->texture == nullptr) return;
shader.setUIColor(COLOR_WHITE);
shader.setUIModel(transform);
shader.setUITexture(this->texture);
this->mesh.draw(MESH_DRAW_MODE_TRIANGLES, 0, -1);
}
void UIBorder::setBorderSize(glm::vec2 borderSize) {
this->edgeDimensions = borderSize;
this->updatePositions();
}
glm::vec2 UIBorder::getBorderSize() {
return this->edgeDimensions;
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "UIBorder.hpp"
using namespace Dawn;
UIBorder::UIBorder(UICanvas &canvas) : UIComponent(canvas) {
this->mesh.createBuffers(QUAD_VERTICE_COUNT * 9, QUAD_INDICE_COUNT * 9);
this->texture = new Texture();
this->texture->setSize(3, 3);
struct Color pixels[9] = {
COLOR_WHITE, COLOR_RED, COLOR_BLUE,
COLOR_GREEN, COLOR_MAGENTA, COLOR_BLACK,
COLOR_CORNFLOWER_BLUE, COLOR_WHITE, COLOR_MAGENTA
};
this->texture->buffer(pixels);
this->updatePositions();
}
void UIBorder::updatePositions() {
UIComponent::updatePositions();
glm::vec2 oneThird = this->uv1 / 3.0f;
glm::vec2 overallDimensions = glm::vec2(this->getWidth(), this->getHeight());
glm::vec2 innerDimensions = overallDimensions - (this->edgeDimensions * 2.0f);
// Top Left.
QuadMesh::bufferQuadMesh(&this->mesh,
glm::vec2(0, 0),
this->uv0,
edgeDimensions,
this->uv0 + oneThird,
0, 0
);
// Top Center
QuadMesh::bufferQuadMesh(&this->mesh,
glm::vec2(edgeDimensions.x, 0),
this->uv0 + glm::vec2(oneThird.x, 0),
glm::vec2(edgeDimensions.x + innerDimensions.x, edgeDimensions.y),
this->uv0 + glm::vec2(oneThird.x * 2.0f, oneThird.y),
QUAD_VERTICE_COUNT, QUAD_INDICE_COUNT
);
// Top Right
QuadMesh::bufferQuadMesh(&this->mesh,
glm::vec2(edgeDimensions.x + innerDimensions.x, 0),
this->uv0 + glm::vec2(oneThird.x * 2.0f, 0),
glm::vec2(overallDimensions.x, edgeDimensions.y),
glm::vec2(this->uv1.x, this->uv0.y + oneThird.y),
QUAD_VERTICE_COUNT * 2, QUAD_INDICE_COUNT * 2
);
// Middle Left
QuadMesh::bufferQuadMesh(&this->mesh,
glm::vec2(0, edgeDimensions.y),
this->uv0 + glm::vec2(0, oneThird.y),
glm::vec2(edgeDimensions.x, edgeDimensions.y + innerDimensions.y),
this->uv0 + glm::vec2(oneThird.x, oneThird.y * 2.0f),
QUAD_VERTICE_COUNT * 3, QUAD_INDICE_COUNT * 3
);
// Center
QuadMesh::bufferQuadMesh(&this->mesh,
edgeDimensions,
this->uv0 + oneThird,
edgeDimensions + innerDimensions,
this->uv0 + (oneThird * 2.0f),
QUAD_VERTICE_COUNT * 4, QUAD_INDICE_COUNT * 4
);
// Middle Right
QuadMesh::bufferQuadMesh(&this->mesh,
edgeDimensions + glm::vec2(innerDimensions.x, 0),
this->uv0 + glm::vec2(oneThird.x * 2.0f, oneThird.y),
edgeDimensions + innerDimensions + glm::vec2(edgeDimensions.x, 0),
this->uv1 - glm::vec2(0.0f, oneThird.y),
QUAD_VERTICE_COUNT * 5, QUAD_INDICE_COUNT * 5
);
// Bottom Left
QuadMesh::bufferQuadMesh(&this->mesh,
glm::vec2(0.0f, edgeDimensions.y + innerDimensions.y),
this->uv0 + glm::vec2(0.0f, uv1.y - oneThird.y),
glm::vec2(edgeDimensions.x, overallDimensions.y),
this->uv1 - glm::vec2(oneThird.x * 2.0f, 0.0f),
QUAD_VERTICE_COUNT * 6, QUAD_INDICE_COUNT * 6
);
// Bottom Center
QuadMesh::bufferQuadMesh(&this->mesh,
edgeDimensions + glm::vec2(0.0f, innerDimensions.y),
this->uv1 - oneThird,
overallDimensions - glm::vec2(edgeDimensions.x, 0.0f),
this->uv1 - glm::vec2(oneThird.x, 0.0f),
QUAD_VERTICE_COUNT * 7, QUAD_INDICE_COUNT * 7
);
// Bottom Right
QuadMesh::bufferQuadMesh(&this->mesh,
overallDimensions - edgeDimensions,
this->uv1 - oneThird,
overallDimensions,
this->uv1,
QUAD_VERTICE_COUNT * 8, QUAD_INDICE_COUNT * 8
);
}
void UIBorder::drawSelf(UIShader &shader, glm::mat4 transform) {
if(this->texture == nullptr) return;
shader.setUIColor(COLOR_WHITE);
shader.setUIModel(transform);
shader.setUITexture(this->texture);
this->mesh.draw(MESH_DRAW_MODE_TRIANGLES, 0, -1);
}
void UIBorder::setBorderSize(glm::vec2 borderSize) {
this->edgeDimensions = 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

@ -1,30 +1,48 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "UIComponent.hpp"
#include "display/mesh/QuadMesh.hpp"
#include "display/Texture.hpp"
namespace Dawn {
class UIBorder : public UIComponent {
private:
Mesh mesh;
glm::vec2 edgeDimensions = glm::vec2(8.0f, 8.0f);
glm::vec2 uv0 = glm::vec2(0.0f, 0.0f);
glm::vec2 uv1 = glm::vec2(1.0f, 1.0f);
void updatePositions() override;
public:
Texture *texture;
UIBorder(UICanvas &canvas);
void drawSelf(UIShader &shader, glm::mat4 selfTransform) override;
void setBorderSize(glm::vec2 borderSize);
glm::vec2 getBorderSize();
};
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "UIComponent.hpp"
#include "display/mesh/QuadMesh.hpp"
#include "display/Texture.hpp"
namespace Dawn {
class UIBorder : public UIComponent {
private:
Mesh mesh;
glm::vec2 edgeDimensions = glm::vec2(8.0f, 8.0f);
glm::vec2 uv0 = glm::vec2(0.0f, 0.0f);
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);
/**
* 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

@ -1,153 +1,158 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "UIComponent.hpp"
using namespace Dawn;
UIComponent::UIComponent(UICanvas &canvas) :
canvas(canvas)
{
}
void UIComponent::updatePositions() {
// X Alignment
if(this->alignX == UI_COMPONENT_ALIGN_STRETCH) {
if(this->parent == nullptr) {
this->width = this->canvas.getWidth();
} else {
this->width = this->parent->getWidth();
}
this->relativeX = this->alignment[0];
this->width -= (this->alignment[0] + this->alignment[2]);
} else if(this->alignX == UI_COMPONENT_ALIGN_START) {
this->relativeX = this->alignment[0];
this->width = this->alignment[2];
} else if(this->alignX == UI_COMPONENT_ALIGN_END) {
this->width = this->alignment[0];
if(this->parent == nullptr) {
this->relativeX = this->canvas.getWidth();
} else {
this->relativeX = this->parent->getWidth();
}
this->relativeX -= this->width;
this->relativeX -= this->alignment[2];
} else if(this->alignX == UI_COMPONENT_ALIGN_MIDDLE) {
this->width = this->alignment[2];
if(this->parent == nullptr) {
this->relativeX = this->canvas.getWidth();
} else {
this->relativeX = this->parent->getWidth();
}
this->relativeX = (this->relativeX / 2.0f) - (this->width / 2.0f) + this->alignment[0];
}
// Y Alignment
if(this->alignY == UI_COMPONENT_ALIGN_STRETCH) {
if(this->parent == nullptr) {
this->height = this->canvas.getHeight();
} else {
this->height = this->parent->getHeight();
}
this->relativeY = this->alignment[1];
this->height -= (this->alignment[1] + this->alignment[3]);
} else if(this->alignY == UI_COMPONENT_ALIGN_START) {
this->relativeY = this->alignment[1];
this->height = this->alignment[3];
} else if(this->alignY == UI_COMPONENT_ALIGN_END) {
this->height = this->alignment[1];
if(this->parent == nullptr) {
this->relativeY = this->canvas.getHeight();
} else {
this->relativeY = this->parent->getHeight();
}
this->relativeY -= this->height;
this->relativeY -= this->alignment[3];
} else if(this->alignY == UI_COMPONENT_ALIGN_MIDDLE) {
this->height = this->alignment[3];
if(this->parent == nullptr) {
this->relativeY = this->canvas.getHeight();
} else {
this->relativeY = this->parent->getHeight();
}
this->relativeY = (this->relativeY / 2.0f) - (this->height / 2.0f) + this->alignment[1];
}
// Update children
auto it = this->children.begin();
while(it != this->children.end()) {
(*it)->updatePositions();
++it;
}
}
float_t UIComponent::getWidth() {
return this->width;
}
float_t UIComponent::getHeight() {
return this->height;
}
float_t UIComponent::getRelativeX() {
return this->relativeX;
}
float_t UIComponent::getRelativeY() {
return this->relativeY;
}
void UIComponent::setTransform(
UIComponentAlign xAlign,
UIComponentAlign yAlign,
glm::vec4 alignment,
float_t z
) {
this->alignX = xAlign;
this->alignY = yAlign;
this->alignment = alignment;
this->z = z;
this->updatePositions();
}
void UIComponent::draw(UIShader &uiShader, glm::mat4 parentTransform) {
// Calculate self transform matrix
glm::mat4 selfTransform = parentTransform * glm::translate(
glm::mat4(1.0f), glm::vec3(this->relativeX, this->relativeY, this->z)
);
// Draw Self
this->drawSelf(uiShader, selfTransform);
// Render children
auto it = this->children.begin();
while(it != this->children.end()) {
(*it)->draw(uiShader, selfTransform);
++it;
}
}
void UIComponent::addChild(UIComponent *child) {
if(child->parent == this) return;
if(child->parent != nullptr) child->parent->removeChild(child);
this->children.push_back(child);
child->parent = this;
}
void UIComponent::removeChild(UIComponent *child) {
if(child->parent != this) throw "Invalid child";
auto it = this->children.begin();
while(it != this->children.end()) {
if(*it == child) {
this->children.erase(it);
break;
}
++it;
}
}
UIComponent::~UIComponent() {
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "UIComponent.hpp"
using namespace Dawn;
UIComponent::UIComponent(UICanvas &canvas) :
canvas(canvas)
{
}
void UIComponent::updatePositions() {
// X Alignment
if(this->alignX == UI_COMPONENT_ALIGN_STRETCH) {
if(this->parent == nullptr) {
this->width = this->canvas.getWidth();
} else {
this->width = this->parent->getWidth();
}
this->relativeX = this->alignment[0];
this->width -= (this->alignment[0] + this->alignment[2]);
} else if(this->alignX == UI_COMPONENT_ALIGN_START) {
this->relativeX = this->alignment[0];
this->width = this->alignment[2];
} else if(this->alignX == UI_COMPONENT_ALIGN_END) {
this->width = this->alignment[0];
if(this->parent == nullptr) {
this->relativeX = this->canvas.getWidth();
} else {
this->relativeX = this->parent->getWidth();
}
this->relativeX -= this->width;
this->relativeX -= this->alignment[2];
} else if(this->alignX == UI_COMPONENT_ALIGN_MIDDLE) {
this->width = this->alignment[2];
if(this->parent == nullptr) {
this->relativeX = this->canvas.getWidth();
} else {
this->relativeX = this->parent->getWidth();
}
this->relativeX = (this->relativeX / 2.0f) - (this->width / 2.0f) + this->alignment[0];
}
// Y Alignment
if(this->alignY == UI_COMPONENT_ALIGN_STRETCH) {
if(this->parent == nullptr) {
this->height = this->canvas.getHeight();
} else {
this->height = this->parent->getHeight();
}
this->relativeY = this->alignment[1];
this->height -= (this->alignment[1] + this->alignment[3]);
} else if(this->alignY == UI_COMPONENT_ALIGN_START) {
this->relativeY = this->alignment[1];
this->height = this->alignment[3];
} else if(this->alignY == UI_COMPONENT_ALIGN_END) {
this->height = this->alignment[1];
if(this->parent == nullptr) {
this->relativeY = this->canvas.getHeight();
} else {
this->relativeY = this->parent->getHeight();
}
this->relativeY -= this->height;
this->relativeY -= this->alignment[3];
} else if(this->alignY == UI_COMPONENT_ALIGN_MIDDLE) {
this->height = this->alignment[3];
if(this->parent == nullptr) {
this->relativeY = this->canvas.getHeight();
} else {
this->relativeY = this->parent->getHeight();
}
this->relativeY = (this->relativeY / 2.0f) - (this->height / 2.0f) + this->alignment[1];
}
// Update children
auto it = this->children.begin();
while(it != this->children.end()) {
(*it)->updatePositions();
++it;
}
// Fire event
eventAlignmentUpdated.invoke(
this->width, this->height, this->relativeX, this->relativeY
);
}
float_t UIComponent::getWidth() {
return this->width;
}
float_t UIComponent::getHeight() {
return this->height;
}
float_t UIComponent::getRelativeX() {
return this->relativeX;
}
float_t UIComponent::getRelativeY() {
return this->relativeY;
}
void UIComponent::setTransform(
UIComponentAlign xAlign,
UIComponentAlign yAlign,
glm::vec4 alignment,
float_t z
) {
this->alignX = xAlign;
this->alignY = yAlign;
this->alignment = alignment;
this->z = z;
this->updatePositions();
}
void UIComponent::draw(UIShader &uiShader, glm::mat4 parentTransform) {
// Calculate self transform matrix
glm::mat4 selfTransform = parentTransform * glm::translate(
glm::mat4(1.0f), glm::vec3(this->relativeX, this->relativeY, this->z)
);
// Draw Self
this->drawSelf(uiShader, selfTransform);
// Render children
auto it = this->children.begin();
while(it != this->children.end()) {
(*it)->draw(uiShader, selfTransform);
++it;
}
}
void UIComponent::addChild(UIComponent *child) {
if(child->parent == this) return;
if(child->parent != nullptr) child->parent->removeChild(child);
this->children.push_back(child);
child->parent = this;
}
void UIComponent::removeChild(UIComponent *child) {
if(child->parent != this) throw "Invalid child";
auto it = this->children.begin();
while(it != this->children.end()) {
if(*it == child) {
this->children.erase(it);
break;
}
++it;
}
}
UIComponent::~UIComponent() {
}

View File

@ -1,82 +1,128 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/components/ui/UICanvas.hpp"
#include "scene/Scene.hpp"
#include "display/Color.hpp"
#include "display/shader/UIShader.hpp"
namespace Dawn {
enum UIComponentAlign {
UI_COMPONENT_ALIGN_START,
UI_COMPONENT_ALIGN_MIDDLE,
UI_COMPONENT_ALIGN_END,
UI_COMPONENT_ALIGN_STRETCH
};
class UIComponent {
protected:
// Calculated (and cached) values
float_t width = 1;
float_t height = 1;
float_t relativeX = 0;
float_t relativeY = 0;
// Setting values
UIComponentAlign alignX = UI_COMPONENT_ALIGN_START;
UIComponentAlign alignY = UI_COMPONENT_ALIGN_START;
glm::vec4 alignment = glm::vec4(0, 0, 32, 32);
float_t z = 0;
std::vector<UIComponent*> children;
UIComponent *parent = nullptr;
// 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
// CSS would handle things) When I need to support these I will add the
// code but right now it's not necessary
/**
* Updates the cached/stored values based on the setting internal values.
* You should watchdog this if you intend to do something when values are
* updated, e.g. if you need to resize a quad, or something.
*/
virtual void updatePositions();
public:
UICanvas &canvas;
UIComponent(UICanvas &canvas);
/**
* Returns the calculated width, based on the internal alignment values.
*
* @return Width of the component.
*/
float_t getWidth();
float_t getHeight();
float_t getRelativeX();
float_t getRelativeY();
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;
void addChild(UIComponent *child);
void removeChild(UIComponent *child);
virtual ~UIComponent();
friend class UICanvas;
};
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/components/ui/UICanvas.hpp"
#include "scene/Scene.hpp"
#include "display/Color.hpp"
#include "display/shader/UIShader.hpp"
namespace Dawn {
enum UIComponentAlign {
UI_COMPONENT_ALIGN_START,
UI_COMPONENT_ALIGN_MIDDLE,
UI_COMPONENT_ALIGN_END,
UI_COMPONENT_ALIGN_STRETCH
};
class UIComponent {
protected:
// Calculated (and cached) values
float_t width = 1;
float_t height = 1;
float_t relativeX = 0;
float_t relativeY = 0;
// Setting values
UIComponentAlign alignX = UI_COMPONENT_ALIGN_START;
UIComponentAlign alignY = UI_COMPONENT_ALIGN_START;
glm::vec4 alignment = glm::vec4(0, 0, 32, 32);
float_t z = 0;
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
// CSS would handle things) When I need to support these I will add the
// code but right now it's not necessary
/**
* Updates the cached/stored values based on the setting internal values.
* You should watchdog this if you intend to do something when values are
* updated, e.g. if you need to resize a quad, or something.
*/
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;
UIComponent(UICanvas &canvas);
/**
* Returns the calculated width, based on the internal alignment values.
*
* @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();
/**
* 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
);
void draw(UIShader &uiShader, glm::mat4 parentTransform);
/**
* 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

@ -1,63 +1,73 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "UILabel.hpp"
using namespace Dawn;
UILabel::UILabel(UICanvas &canvas) : UIComponent(canvas) {
}
void UILabel::updatePositions() {
UIComponent::updatePositions();
this->updateMesh();
}
void UILabel::updateMesh() {
if(!this->needsRebuffering) return;
if(this->font == nullptr) return;
if(this->text.size() == 0) return;
this->font->buffer(
this->text,
this->fontSize,
this->getWidth(),
this->mesh,
&this->measure
);
this->needsRebuffering = false;
}
std::string UILabel::getText() {
return this->text;
}
void UILabel::setFont(Font *font) {
this->font = font;
this->needsRebuffering = true;
}
void UILabel::setText(std::string text) {
this->text = text;
this->needsRebuffering = true;
}
void UILabel::setFontSize(float_t fontSize) {
this->fontSize = fontSize;
this->needsRebuffering = true;
}
void UILabel::drawSelf(UIShader &shader, glm::mat4 selfTransform) {
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->quadCount);
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "UILabel.hpp"
using namespace Dawn;
UILabel::UILabel(UICanvas &canvas) : UIComponent(canvas) {
}
void UILabel::updatePositions() {
UIComponent::updatePositions();
this->updateMesh();
}
void UILabel::updateMesh() {
if(!this->needsRebuffering) return;
if(this->font == nullptr) return;
if(this->text.size() == 0) return;
this->font->buffer(
this->text,
this->fontSize,
this->getWidth(),
this->mesh,
&this->measure
);
this->needsRebuffering = false;
}
std::string UILabel::getText() {
return this->text;
}
void UILabel::setFont(Font *font) {
this->font = font;
this->needsRebuffering = true;
}
void UILabel::setText(std::string text) {
this->text = text;
this->needsRebuffering = true;
}
void UILabel::setFontSize(float_t fontSize) {
this->fontSize = fontSize;
this->needsRebuffering = true;
}
void UILabel::drawSelf(UIShader &shader, glm::mat4 selfTransform) {
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->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

@ -1,67 +1,73 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "UIComponent.hpp"
#include "display/mesh/QuadMesh.hpp"
#include "display/font/Font.hpp"
namespace Dawn {
class UILabel : public UIComponent {
private:
Mesh mesh;
bool_t needsRebuffering = true;
Font *font = nullptr;
std::string text = "";
float_t fontSize = 10.0f;
void updatePositions() override;
public:
struct FontMeasure measure;
int32_t startQuad = 0;
int32_t quadCount = -1;
/** The colour of this label */
struct Color textColor = COLOR_MAGENTA;
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.
*
* @return The current label string.
*/
std::string getText();
/**
* Set the font to use for the label.
*
* @param font Font to use.
*/
void setFont(Font *font);
/**
* Sets the text for the label to use.
*
* @param text Text for the label to use.
*/
void setText(std::string text);
/**
* Sets / Updates the font size for the label.
*
* @param fontSize Font size to use.
*/
void setFontSize(float_t fontSize);
};
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "UIComponent.hpp"
#include "display/mesh/QuadMesh.hpp"
#include "display/font/Font.hpp"
namespace Dawn {
class UILabel : public UIComponent {
private:
Mesh mesh;
bool_t needsRebuffering = true;
Font *font = nullptr;
std::string text = "";
float_t fontSize = 10.0f;
void updatePositions() override;
public:
struct FontMeasure measure;
int32_t startQuad = 0;
int32_t quadCount = -1;
/** The colour of this label */
struct Color textColor = COLOR_MAGENTA;
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.
*/
void updateMesh();
/**
* Returns the current text that the label has.
*
* @return The current label string.
*/
std::string getText();
/**
* Set the font to use for the label.
*
* @param font Font to use.
*/
void setFont(Font *font);
/**
* Sets the text for the label to use.
*
* @param text Text for the label to use.
*/
void setText(std::string text);
/**
* Sets / Updates the font size for the label.
*
* @param fontSize Font size to use.
*/
void setFontSize(float_t fontSize);
};
}

View File

@ -1,28 +1,28 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "UIComponent.hpp"
#include "display/mesh/QuadMesh.hpp"
#include "display/Texture.hpp"
namespace Dawn {
class UISprite : public UIComponent {
protected:
void updatePositions() override;
public:
Mesh mesh;
Texture *texture;
/**
* UI Sprite Constructor, use the UIElement / UIComponent create instead.
*
* @param canvas Canvas that this sprite belongs to.
*/
UISprite(UICanvas &canvas);
void drawSelf(UIShader &uiShader, glm::mat4 selfTransform) override;
};
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "UIComponent.hpp"
#include "display/mesh/QuadMesh.hpp"
#include "display/Texture.hpp"
namespace Dawn {
class UISprite : public UIComponent {
protected:
void updatePositions() override;
void drawSelf(UIShader &uiShader, glm::mat4 selfTransform) override;
public:
Mesh mesh;
Texture *texture;
/**
* UI Sprite Constructor, use the UIElement / UIComponent create instead.
*
* @param canvas Canvas that this sprite belongs to.
*/
UISprite(UICanvas &canvas);
};
}

View File

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

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 "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

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

View File

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