added text widget parsing and rendering

This commit is contained in:
2017-02-06 23:32:01 +00:00
parent fd7f62693e
commit 3cc25e7592
7 changed files with 130 additions and 32 deletions

View File

@@ -2,6 +2,7 @@
#include "shape.h"
#include "util.h"
#include "shader.h"
#include "font.h"
enum class kAttribute : uint16_t
{
@@ -25,12 +26,16 @@ enum class kAttribute : uint16_t
Thickness = const_hash("thickness"),
BorderColor = const_hash("border-color"),
Type = const_hash("type"),
Text = const_hash("text"),
FontFace = const_hash("font-face"),
FontSize = const_hash("font-size"),
};
enum class kWidget : uint16_t
{
Border = const_hash("border"),
Shape = const_hash("shape"),
Text = const_hash("text"),
Ref = const_hash("ref"),
};
@@ -46,11 +51,15 @@ class Widget
{
public:
glm::mat4 mvp;
glm::mat4 proj;
glm::mat4 scale;
glm::mat4 pos;
glm::vec4 clip;
virtual std::unique_ptr<Widget> clone() = 0;
virtual void create() { }
virtual void draw() { }
virtual void parse_attributes(kAttribute ka, const tinyxml2::XMLAttribute* attr) { }
virtual void update() { }
};
class WidgetRef : public Widget
@@ -207,6 +216,55 @@ public:
}
};
class WidgetText : public Widget
{
public:
TextMesh m_text_mesh;
std::string m_text;
std::string m_font;
int m_size;
virtual std::unique_ptr<Widget> clone() override
{
return nullptr;
}
virtual void create() override
{
char font[64];
sprintf(font, "%s-%d", m_font.c_str(), m_size);
kFont font_id = (kFont)const_hash(font);
m_text_mesh.create();
m_text_mesh.update(font_id, m_text.c_str());
}
virtual void draw() override
{
ShaderManager::use(kShader::Font);
ShaderManager::u_int(kShaderUniform::Tex, 0);
ShaderManager::u_mat4(kShaderUniform::MVP, mvp);
m_text_mesh.draw();
}
virtual void parse_attributes(kAttribute ka, const tinyxml2::XMLAttribute* attr) override
{
switch (ka)
{
case kAttribute::Text:
m_text = attr->Value();
break;
case kAttribute::FontFace:
m_font = attr->Value();
break;
case kAttribute::FontSize:
m_size = attr->IntValue();
break;
default:
break;
}
}
virtual void update() override
{
mvp = proj * pos;
}
};
class Node
{
friend class LayoutManager;
@@ -252,6 +310,7 @@ public:
void SetWidthP(float value) { YGNodeStyleSetWidthPercent(y_node, value); }
void SetHeight(float value) { YGNodeStyleSetHeight(y_node, value); }
void SetHeightP(float value) { YGNodeStyleSetHeightPercent(y_node, value); }
void SetSize(glm::vec2 value) { SetWidth(value.x); SetHeight(value.y); }
void SetPadding(float t, float r, float b, float l)
{