replace kFont with std::string

This commit is contained in:
2019-12-01 15:00:10 +01:00
parent 0ee3f1d125
commit 0905827b8d
11 changed files with 38 additions and 46 deletions

View File

@@ -169,8 +169,9 @@ void App::initAssets()
LOG("initializing assets"); LOG("initializing assets");
FontManager::init(); FontManager::init();
LOG("initializing assets loading fonts"); LOG("initializing assets loading fonts");
FontManager::load(kFont::Arial_11, "data/fonts/Roboto-Regular.ttf", 17, display_density * zoom); FontManager::load("arial-17", "data/fonts/Roboto-Regular.ttf", 17, display_density * zoom);
FontManager::load(kFont::Arial_30, "data/fonts/Roboto-Regular.ttf", 30, display_density * zoom); FontManager::load("arial-20", "data/fonts/Roboto-Regular.ttf", 20, display_density * zoom);
FontManager::load("arial-30", "data/fonts/Roboto-Regular.ttf", 30, display_density * zoom);
LOG("initializing assets create sampler"); LOG("initializing assets create sampler");
sampler.create(GL_NEAREST); sampler.create(GL_NEAREST);

View File

@@ -852,24 +852,27 @@ void App::dialog_whatsnew(bool force_show)
if (success) if (success)
{ {
int last_id = Settings::value_or<Serializer::Integer>("whatsnew-id", 0); int last_id = Settings::value_or<Serializer::Integer>("whatsnew-id", 0);
if ((force_show || whatsnew->m_page_id <= g_version_build) && whatsnew->m_page_id > last_id) if (force_show || (whatsnew->m_page_id <= g_version_build && whatsnew->m_page_id > last_id))
{ {
whatsnew->set_title(fmt::format("What's new in version {}", g_version_number)); whatsnew->set_title(fmt::format("What's new in version {}", g_version_number));
layout[main_id]->add_child(whatsnew); if (!force_show)
layout[main_id]->add_child(whatsnew);
} }
whatsnew->add_button("Reload", 120, [this, whatsnew](Node*) {
whatsnew->reload();
});
whatsnew->add_button("Read Later", 120, [this, whatsnew](Node*) {
whatsnew->destroy();
});
whatsnew->add_button("Close", 100, [this, whatsnew](Node*) {
Settings::set<Serializer::Integer>("whatsnew-id", whatsnew->m_page_id);
Settings::save();
whatsnew->destroy();
});
} }
}); });
whatsnew->add_button("Reload", 120, [this, whatsnew](Node*) {
whatsnew->reload();
});
whatsnew->add_button("Read Later", 120, [this, whatsnew](Node*) {
whatsnew->destroy();
});
whatsnew->add_button("Close", 100, [this, whatsnew](Node*) {
Settings::set<Serializer::Integer>("whatsnew-id", whatsnew->m_page_id);
Settings::save();
whatsnew->destroy();
});
if (force_show)
layout[main_id]->add_child(whatsnew);
} }
void App::dialog_shortcuts() void App::dialog_shortcuts()

View File

@@ -6,7 +6,7 @@
#include "util.h" #include "util.h"
#include "app.h" #include "app.h"
std::map<kFont, Font> FontManager::m_fonts; std::map<std::string, Font> FontManager::m_fonts;
Sampler FontManager::m_sampler; Sampler FontManager::m_sampler;
bool Font::load(const std::string& ttf, int font_size, float font_scale) bool Font::load(const std::string& ttf, int font_size, float font_scale)
@@ -59,12 +59,12 @@ void FontManager::init()
m_sampler.create(); m_sampler.create();
} }
bool FontManager::load(kFont id, const char* ttf, int sz, float scale) bool FontManager::load(const std::string& id, const char* ttf, int sz, float scale)
{ {
return m_fonts[id].load(ttf, sz, scale); return m_fonts[id].load(ttf, sz, scale);
} }
const Font& FontManager::get(kFont id) const Font& FontManager::get(const std::string& id)
{ {
return m_fonts[id]; return m_fonts[id];
} }
@@ -136,7 +136,7 @@ bool TextMesh::create()
return true; return true;
} }
void TextMesh::update(kFont id, const std::string& text) void TextMesh::update(const std::string& id, const std::string& text)
{ {
font_id = id; font_id = id;
auto& f = FontManager::get(id); auto& f = FontManager::get(id);

View File

@@ -3,12 +3,6 @@
#include "util.h" #include "util.h"
#include <stb/stb_truetype.h> #include <stb/stb_truetype.h>
enum class kFont : uint16_t
{
Arial_11 = const_hash("arial-11"),
Arial_30 = const_hash("arial-30"),
};
class Font class Font
{ {
public: public:
@@ -33,11 +27,11 @@ public:
class FontManager class FontManager
{ {
public: public:
static std::map<kFont, Font> m_fonts; static std::map<std::string, Font> m_fonts;
static Sampler m_sampler; static Sampler m_sampler;
static void init(); static void init();
static bool load(kFont id, const char* ttf, int sz, float scale); static bool load(const std::string& id, const char* ttf, int sz, float scale);
static const Font& get(kFont id); static const Font& get(const std::string& id);
static void invalidate() { m_fonts.clear(); } static void invalidate() { m_fonts.clear(); }
static void change_scale(float scale); static void change_scale(float scale);
}; };
@@ -56,10 +50,10 @@ public:
int font_array_count = 0; int font_array_count = 0;
int max_width = 0; int max_width = 0;
GLuint font_buffers[2] = {0, 0}; GLuint font_buffers[2] = {0, 0};
kFont font_id; std::string font_id;
glm::vec2 bb = { 0, 0 }; glm::vec2 bb = { 0, 0 };
glm::vec4 cur_box; glm::vec4 cur_box;
bool create(); bool create();
void update(kFont id, const std::string& text); void update(const std::string& id, const std::string& text);
void draw(); void draw();
}; };

View File

@@ -39,8 +39,6 @@ void NodeButton::init()
m_border->init(); m_border->init();
m_border->m_color = color_normal; m_border->m_color = color_normal;
m_text->init(); m_text->init();
m_text->m_font = "arial";
m_text->m_font_size = 11;
m_border->SetAlign(YGAlignCenter); m_border->SetAlign(YGAlignCenter);
m_border->SetJustify(YGJustifyCenter); m_border->SetJustify(YGJustifyCenter);
m_border->m_mouse_ignore = false; m_border->m_mouse_ignore = false;

View File

@@ -60,7 +60,7 @@ void NodeDialogCloud::load_thumbs_thread()
align->SetAlign(YGAlignCenter); align->SetAlign(YGAlignCenter);
align->SetJustify(YGJustifyCenter); align->SetJustify(YGJustifyCenter);
auto* text = align->add_child<NodeText>(); auto* text = align->add_child<NodeText>();
text->set_font(kFont::Arial_30); text->set_font("arial-30");
text->set_text("Connecting to the server..."); text->set_text("Connecting to the server...");
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &res); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &res);

View File

@@ -42,7 +42,7 @@ std::future<bool> NodeRemotePage::load_url(const std::string& url,
align->SetAlign(YGAlignCenter); align->SetAlign(YGAlignCenter);
align->SetJustify(YGJustifyCenter); align->SetJustify(YGJustifyCenter);
auto text = align->add_child_ref<NodeText>(); auto text = align->add_child_ref<NodeText>();
text->set_font(kFont::Arial_30); text->set_font("arial-30");
text->set_text("Connecting to the server..."); text->set_text("Connecting to the server...");
m_url = url; m_url = url;

View File

@@ -31,16 +31,14 @@ void NodeText::create()
Node::create(); Node::create();
if (!m_font.empty()) if (!m_font.empty())
{ {
char font[64]; font_id = fmt::format("{}-{}", m_font, m_font_size);
sprintf(font, "%s-%d", m_font.c_str(), m_font_size);
font_id = (kFont)const_hash(font);
m_text_mesh.create(); m_text_mesh.create();
m_text_mesh.update(font_id, m_text); m_text_mesh.update(font_id, m_text);
update_layout(); update_layout();
} }
} }
void NodeText::set_font(kFont fontID) void NodeText::set_font(const std::string& fontID)
{ {
font_id = fontID; font_id = fontID;
m_text_mesh.create(); m_text_mesh.create();

View File

@@ -15,8 +15,8 @@ public:
std::string m_text; std::string m_text;
std::string m_font = "arial"; std::string m_font = "arial";
glm::vec4 m_color{ 1, 1, 1, 1 }; glm::vec4 m_color{ 1, 1, 1, 1 };
int m_font_size = 11; int m_font_size = 17;
kFont font_id; std::string font_id;
bool m_multiline = false; bool m_multiline = false;
glm::vec2 m_off = { 0, 0 }; glm::vec2 m_off = { 0, 0 };
@@ -32,6 +32,6 @@ public:
void set_text(const std::string& s); void set_text(const std::string& s);
void set_text_format(const char* fmt, ...); void set_text_format(const char* fmt, ...);
void set_font(kFont fontID); void set_font(const std::string& fontID);
void update_layout(); void update_layout();
}; };

View File

@@ -184,9 +184,7 @@ void NodeTextInput::create()
NodeBorder::create(); NodeBorder::create();
if (!m_font.empty()) if (!m_font.empty())
{ {
char font[64]; font_id = fmt::format("{}-{}", m_font, m_font_size);
sprintf(font, "%s-%d", m_font.c_str(), m_font_size);
font_id = (kFont)const_hash(font);
m_text_mesh.create(); m_text_mesh.create();
m_text_mesh.update(font_id, m_text); m_text_mesh.update(font_id, m_text);
update_layout(); update_layout();

View File

@@ -18,8 +18,8 @@ public:
std::string m_text; std::string m_text;
std::string m_font = "arial"; std::string m_font = "arial";
glm::vec4 m_color{ 1, 1, 1, 1 }; glm::vec4 m_color{ 1, 1, 1, 1 };
int m_font_size = 11; int m_font_size = 17;
kFont font_id; std::string font_id;
bool m_multiline = false; bool m_multiline = false;
bool m_cursor_visible = false; bool m_cursor_visible = false;