added node alignment and text color
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -11,9 +11,6 @@ class App
|
||||
Sampler sampler;
|
||||
Texture2D tex;
|
||||
LayoutManager layout;
|
||||
Plane plane;
|
||||
Font font;
|
||||
TextMesh text;
|
||||
public:
|
||||
static App I;
|
||||
float width;
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
std::map<kFont, Font> 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<uint8_t[]>(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)
|
||||
|
||||
@@ -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<stbtt_bakedchar> chars;
|
||||
|
||||
bool load(const char* ttf);
|
||||
bool load(const char* ttf, int sz);
|
||||
};
|
||||
|
||||
class FontManager
|
||||
@@ -27,7 +28,7 @@ public:
|
||||
static std::map<kFont, Font> 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);
|
||||
};
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<Widget> clone() override
|
||||
{
|
||||
return nullptr;
|
||||
auto ret = std::make_unique<WidgetText>();
|
||||
*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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user