Font working

This commit is contained in:
2022-10-24 22:28:53 -07:00
parent 98760ae494
commit eea8f5f693
15 changed files with 132 additions and 32 deletions

View File

@ -33,7 +33,7 @@ float_t TrueTypeFont::getSpaceSize(float_t fontSize) {
}
float_t TrueTypeFont::getInitialLineHeight(float_t fontSize) {
return mathRoundFloat(this->getScale(fontSize) * 11.0f);
return 8.0f;
}
void TrueTypeFont::buffer(
@ -185,7 +185,7 @@ void TrueTypeFont::draw(Mesh &mesh, int32_t startchar, int32_t length) {
}
float_t TrueTypeFont::getLineHeight(float_t fontSize) {
return mathRoundFloat(this->getScale(fontSize) * 14.0f);
return 13.0f;
}
float_t TrueTypeFont::getDefaultFontSize() {

View File

@ -14,7 +14,7 @@ namespace Dawn {
#define TRUETYPE_NUM_CHARS 96
/** Refer to STBTT docs for OpenGL Fill Mode v d3d Fill Modes */
#define TRUETYPE_FILL_MODE 1
#define TRUETYPE_FILL_MODE 0
typedef stbtt_bakedchar truetypechar_t;
typedef stbtt_aligned_quad truetypequad_t;

View File

@ -101,6 +101,6 @@ namespace Dawn {
* @param parameter parameter to set the texture on to.
* @param texture Texture to bind to the parameter.
*/
virtual void setTexture(T parameter, std::shared_ptr<Texture> texture)=0;
virtual void setTexture(T parameter, Texture *texture) = 0;
};
}

View File

@ -26,7 +26,7 @@ namespace Dawn {
std::map<shaderparameter_t, bool_t> boolValues;
std::map<shaderparameter_t, glm::mat4> matrixValues;
std::map<shaderparameter_t, glm::vec3> vec3Values;
std::map<shaderparameter_t, std::shared_ptr<Texture>> textureValues;
std::map<shaderparameter_t, Texture*> textureValues;
/**
* Material component constructor.

View File

@ -7,5 +7,6 @@
target_sources(${DAWN_TARGET_NAME}
PRIVATE
UIComponent.cpp
UILabel.cpp
UISprite.cpp
)

57
src/dawn/ui/UILabel.cpp Normal file
View File

@ -0,0 +1,57 @@
// 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::updateMesh() {
if(!this->needsRebuffering) return;
if(this->font == nullptr) return;
this->font->buffer(
this->text,
this->fontSize,
-1,
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) return;
this->updateMesh();
shader.setUIColor(this->textColor);
shader.setUIModel(selfTransform);
shader.setUITexture(&this->font->getTexture());
this->font->draw(this->mesh, 0, -1);
}

36
src/dawn/ui/UILabel.hpp Normal file
View File

@ -0,0 +1,36 @@
// 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;
struct FontMeasure measure;
bool_t needsRebuffering = true;
Font *font = nullptr;
std::string text = "";
float_t fontSize = 10.0f;
void updateMesh();
public:
struct Color textColor = COLOR_MAGENTA;
UILabel(UICanvas &canvas);
std::string getText();
void setFont(Font *font);
void setText(std::string text);
void setFontSize(float_t fontSize);
void drawSelf(UIShader &shader, glm::mat4 selfTransform);
};
}

View File

@ -29,6 +29,7 @@ void UISprite::drawSelf(UIShader &uiShader, glm::mat4 selfTransform) {
uiShader.setUIModel(selfTransform);
uiShader.setUIModel(glm::mat4(1.0f));
uiShader.setUIColor(COLOR_WHITE);
uiShader.setUITexture(this->texture);
this->mesh.draw(MESH_DRAW_MODE_TRIANGLES, 0, -1);
}

View File

@ -6,6 +6,7 @@
#pragma once
#include "UIComponent.hpp"
#include "display/mesh/QuadMesh.hpp"
#include "display/Texture.hpp"
namespace Dawn {
class UISprite : public UIComponent {
@ -14,6 +15,7 @@ namespace Dawn {
public:
Mesh mesh;
Texture *texture;
UISprite(UICanvas &canvas);