From 83e59573e0b58f3314625045fbddf44749a52e5e Mon Sep 17 00:00:00 2001 From: omigamedev Date: Tue, 7 Feb 2017 13:39:17 +0000 Subject: [PATCH] added node alignment and text color --- data/layout.xml | 43 ++++++++++++++++++++++++++----------------- engine/app.cpp | 15 ++++----------- engine/app.h | 3 --- engine/font.cpp | 8 ++++---- engine/font.h | 5 +++-- engine/layout.cpp | 30 ++++++++++++++++++++++++++++++ engine/layout.h | 18 +++++++++++++++++- 7 files changed, 84 insertions(+), 38 deletions(-) diff --git a/data/layout.xml b/data/layout.xml index 576da8a..bfd31cb 100644 --- a/data/layout.xml +++ b/data/layout.xml @@ -1,10 +1,14 @@ - + + + - + + + @@ -25,24 +29,26 @@ - - - - - - + + + + + + + + - + - + - + @@ -55,14 +61,17 @@ - + - - - - + + + + + - + + + diff --git a/engine/app.cpp b/engine/app.cpp index 74116d2..c93d968 100644 --- a/engine/app.cpp +++ b/engine/app.cpp @@ -65,11 +65,11 @@ void App::init() static const char* shader_font_f = "#version 150\n" "uniform sampler2D tex;" + "uniform vec4 col;" "in vec2 uv;" "out vec4 frag;" "void main(){" - //" frag = vec4(1,0,0,1);" - " frag = texture(tex, uv);" + " frag = vec4(texture(tex, uv).r * col.rgb, col.a);" "}"; #ifdef _WIN32 @@ -99,7 +99,8 @@ void App::init() const char* ttf = "/Library/Fonts/Arial.ttf"; #endif FontManager::init(); - FontManager::load(kFont::Arial_11, ttf); + FontManager::load(kFont::Arial_11, ttf, 13); + FontManager::load(kFont::Arial_30, ttf, 30); layout.load("data/layout.xml"); //layout["main"].update(width, height); @@ -114,10 +115,6 @@ void App::init() if (!tex.load("data/uvs.jpg")) printf("error loading image\n"); - text.create(); - text.update(kFont::Arial_11, "hello font"); - plane.create<1>(400, 400); - glEnable(GL_TEXTURE_2D); glDisable(GL_DEPTH_TEST); glPointSize(5); @@ -168,10 +165,6 @@ void App::update(float dt) glm::mat4 proj = glm::ortho(0.f, width, height, 0.f, -1.f, 1.f); glm::mat4 tran = glm::translate(glm::vec3(200, 200, 0)); - ShaderManager::use(kShader::Font); - ShaderManager::u_int(kShaderUniform::Tex, 0); - ShaderManager::u_mat4(kShaderUniform::MVP, proj * tran); - text.draw(); /* static int i = 0; i = (i + 1) % 32; diff --git a/engine/app.h b/engine/app.h index b039a7c..23b82c0 100644 --- a/engine/app.h +++ b/engine/app.h @@ -11,9 +11,6 @@ class App Sampler sampler; Texture2D tex; LayoutManager layout; - Plane plane; - Font font; - TextMesh text; public: static App I; float width; diff --git a/engine/font.cpp b/engine/font.cpp index 9d5469d..9fc2407 100644 --- a/engine/font.cpp +++ b/engine/font.cpp @@ -5,7 +5,7 @@ std::map FontManager::m_fonts; Sampler FontManager::m_sampler; -bool Font::load(const char* ttf) +bool Font::load(const char* ttf, int font_size) { FILE* font_file = fopen(ttf, "rb"); if (font_file) @@ -18,7 +18,7 @@ bool Font::load(const char* ttf) assert(bytes==sz); auto bitmap = std::make_unique(w*h); chars.resize(num_chars); - int ret = stbtt_BakeFontBitmap(data.get(), 0, 50, bitmap.get(), w, h, start_char, num_chars, chars.data()); + int ret = stbtt_BakeFontBitmap(data.get(), 0, (float)font_size, bitmap.get(), w, h, start_char, num_chars, chars.data()); font_tex.create(w, h, GL_RED, bitmap.get()); fclose(font_file); return true; @@ -31,9 +31,9 @@ void FontManager::init() m_sampler.create(); } -bool FontManager::load(kFont id, const char *ttf) +bool FontManager::load(kFont id, const char* ttf, int sz) { - return m_fonts[id].load(ttf); + return m_fonts[id].load(ttf, sz); } const Font& FontManager::get(kFont id) diff --git a/engine/font.h b/engine/font.h index 7835354..350f430 100644 --- a/engine/font.h +++ b/engine/font.h @@ -5,6 +5,7 @@ enum class kFont : uint16_t { Arial_11 = const_hash("arial-11"), + Arial_30 = const_hash("arial-30"), }; class Font @@ -18,7 +19,7 @@ public: Texture2D font_tex; std::vector chars; - bool load(const char* ttf); + bool load(const char* ttf, int sz); }; class FontManager @@ -27,7 +28,7 @@ public: static std::map m_fonts; static Sampler m_sampler; static void init(); - static bool load(kFont id, const char* ttf); + static bool load(kFont id, const char* ttf, int sz); static const Font& get(kFont id); }; diff --git a/engine/layout.cpp b/engine/layout.cpp index 6161441..edb6201 100644 --- a/engine/layout.cpp +++ b/engine/layout.cpp @@ -127,6 +127,36 @@ void Node::parse_attributes(kAttribute ka, const tinyxml2::XMLAttribute* attr) case kAttribute::FlexWrap: YGNodeStyleSetFlexWrap(y_node, attr->IntValue() ? YGWrapWrap : YGWrapNoWrap); break; + case kAttribute::Justify: + { + YGJustify v = YGJustifyFlexStart; + if (strcmp("center", attr->Value()) == 0) + v = YGJustifyCenter; + else if (strcmp("flex-start", attr->Value()) == 0) + v = YGJustifyFlexStart; + else if (strcmp("flex-end", attr->Value()) == 0) + v = YGJustifyFlexEnd; + else if (strcmp("space-around", attr->Value()) == 0) + v = YGJustifySpaceAround; + else if (strcmp("space-between", attr->Value()) == 0) + v = YGJustifySpaceBetween; + YGNodeStyleSetJustifyContent(y_node, v); + break; + } + case kAttribute::Align: + { + YGAlign v = YGAlignStretch; + if (strcmp("stretch", attr->Value()) == 0) + v = YGAlignStretch; + else if (strcmp("flex-start", attr->Value()) == 0) + v = YGAlignFlexStart; + else if (strcmp("flex-end", attr->Value()) == 0) + v = YGAlignFlexEnd; + else if (strcmp("center", attr->Value()) == 0) + v = YGAlignCenter; + YGNodeStyleSetAlignItems(y_node, v); + break; + } case kAttribute::Padding: { glm::vec4 pad; diff --git a/engine/layout.h b/engine/layout.h index 9e461bf..72cc0b3 100644 --- a/engine/layout.h +++ b/engine/layout.h @@ -29,6 +29,8 @@ enum class kAttribute : uint16_t Text = const_hash("text"), FontFace = const_hash("font-face"), FontSize = const_hash("font-size"), + Justify = const_hash("justify"), + Align = const_hash("align"), }; enum class kWidget : uint16_t @@ -222,10 +224,13 @@ public: TextMesh m_text_mesh; std::string m_text; std::string m_font; + glm::vec4 m_color{ 1, 1, 1, 1 }; int m_size; virtual std::unique_ptr clone() override { - return nullptr; + auto ret = std::make_unique(); + *ret = *this; + return std::move(ret); } virtual void create() override { @@ -240,6 +245,7 @@ public: ShaderManager::use(kShader::Font); ShaderManager::u_int(kShaderUniform::Tex, 0); ShaderManager::u_mat4(kShaderUniform::MVP, mvp); + ShaderManager::u_vec4(kShaderUniform::Col, m_color); m_text_mesh.draw(); } virtual void parse_attributes(kAttribute ka, const tinyxml2::XMLAttribute* attr) override @@ -255,6 +261,16 @@ public: case kAttribute::FontSize: m_size = attr->IntValue(); break; + case kAttribute::Color: + { + glm::vec4 pad; + int n = sscanf(attr->Value(), "%f %f %f %f", &pad.x, &pad.y, &pad.z, &pad.w); + if (n == 1) + m_color = glm::vec4(pad.x); + else + m_color = pad; + break; + } default: break; }