From 0905827b8d697f2dee79ebaf6af89dabad00dfe5 Mon Sep 17 00:00:00 2001 From: omigamedev Date: Sun, 1 Dec 2019 15:00:10 +0100 Subject: [PATCH] replace kFont with std::string --- src/app.cpp | 5 +++-- src/app_dialogs.cpp | 29 ++++++++++++++++------------- src/font.cpp | 8 ++++---- src/font.h | 16 +++++----------- src/node_button.cpp | 2 -- src/node_dialog_cloud.cpp | 2 +- src/node_remote_page.cpp | 2 +- src/node_text.cpp | 6 ++---- src/node_text.h | 6 +++--- src/node_text_input.cpp | 4 +--- src/node_text_input.h | 4 ++-- 11 files changed, 38 insertions(+), 46 deletions(-) diff --git a/src/app.cpp b/src/app.cpp index 979360f..3625ccf 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -169,8 +169,9 @@ void App::initAssets() LOG("initializing assets"); FontManager::init(); LOG("initializing assets loading fonts"); - FontManager::load(kFont::Arial_11, "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-17", "data/fonts/Roboto-Regular.ttf", 17, 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"); sampler.create(GL_NEAREST); diff --git a/src/app_dialogs.cpp b/src/app_dialogs.cpp index 4024c76..1d91557 100644 --- a/src/app_dialogs.cpp +++ b/src/app_dialogs.cpp @@ -852,24 +852,27 @@ void App::dialog_whatsnew(bool force_show) if (success) { int last_id = Settings::value_or("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)); - 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("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("whatsnew-id", whatsnew->m_page_id); + Settings::save(); + whatsnew->destroy(); + }); + if (force_show) + layout[main_id]->add_child(whatsnew); } void App::dialog_shortcuts() diff --git a/src/font.cpp b/src/font.cpp index 46c2a38..31c269a 100644 --- a/src/font.cpp +++ b/src/font.cpp @@ -6,7 +6,7 @@ #include "util.h" #include "app.h" -std::map FontManager::m_fonts; +std::map FontManager::m_fonts; Sampler FontManager::m_sampler; bool Font::load(const std::string& ttf, int font_size, float font_scale) @@ -59,12 +59,12 @@ void FontManager::init() 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); } -const Font& FontManager::get(kFont id) +const Font& FontManager::get(const std::string& id) { return m_fonts[id]; } @@ -136,7 +136,7 @@ bool TextMesh::create() 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; auto& f = FontManager::get(id); diff --git a/src/font.h b/src/font.h index fd723b2..197d763 100644 --- a/src/font.h +++ b/src/font.h @@ -3,12 +3,6 @@ #include "util.h" #include -enum class kFont : uint16_t -{ - Arial_11 = const_hash("arial-11"), - Arial_30 = const_hash("arial-30"), -}; - class Font { public: @@ -33,11 +27,11 @@ public: class FontManager { public: - static std::map m_fonts; + static std::map m_fonts; static Sampler m_sampler; static void init(); - static bool load(kFont id, const char* ttf, int sz, float scale); - static const Font& get(kFont id); + static bool load(const std::string& id, const char* ttf, int sz, float scale); + static const Font& get(const std::string& id); static void invalidate() { m_fonts.clear(); } static void change_scale(float scale); }; @@ -56,10 +50,10 @@ public: int font_array_count = 0; int max_width = 0; GLuint font_buffers[2] = {0, 0}; - kFont font_id; + std::string font_id; glm::vec2 bb = { 0, 0 }; glm::vec4 cur_box; bool create(); - void update(kFont id, const std::string& text); + void update(const std::string& id, const std::string& text); void draw(); }; diff --git a/src/node_button.cpp b/src/node_button.cpp index 84eaa23..adacc41 100644 --- a/src/node_button.cpp +++ b/src/node_button.cpp @@ -39,8 +39,6 @@ void NodeButton::init() m_border->init(); m_border->m_color = color_normal; m_text->init(); - m_text->m_font = "arial"; - m_text->m_font_size = 11; m_border->SetAlign(YGAlignCenter); m_border->SetJustify(YGJustifyCenter); m_border->m_mouse_ignore = false; diff --git a/src/node_dialog_cloud.cpp b/src/node_dialog_cloud.cpp index 7b13ab0..f9bce95 100644 --- a/src/node_dialog_cloud.cpp +++ b/src/node_dialog_cloud.cpp @@ -60,7 +60,7 @@ void NodeDialogCloud::load_thumbs_thread() align->SetAlign(YGAlignCenter); align->SetJustify(YGJustifyCenter); auto* text = align->add_child(); - text->set_font(kFont::Arial_30); + text->set_font("arial-30"); text->set_text("Connecting to the server..."); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &res); diff --git a/src/node_remote_page.cpp b/src/node_remote_page.cpp index 7131704..44e5dba 100644 --- a/src/node_remote_page.cpp +++ b/src/node_remote_page.cpp @@ -42,7 +42,7 @@ std::future NodeRemotePage::load_url(const std::string& url, align->SetAlign(YGAlignCenter); align->SetJustify(YGJustifyCenter); auto text = align->add_child_ref(); - text->set_font(kFont::Arial_30); + text->set_font("arial-30"); text->set_text("Connecting to the server..."); m_url = url; diff --git a/src/node_text.cpp b/src/node_text.cpp index eca02e2..6961b03 100644 --- a/src/node_text.cpp +++ b/src/node_text.cpp @@ -31,16 +31,14 @@ void NodeText::create() Node::create(); if (!m_font.empty()) { - char font[64]; - sprintf(font, "%s-%d", m_font.c_str(), m_font_size); - font_id = (kFont)const_hash(font); + font_id = fmt::format("{}-{}", m_font, m_font_size); m_text_mesh.create(); m_text_mesh.update(font_id, m_text); update_layout(); } } -void NodeText::set_font(kFont fontID) +void NodeText::set_font(const std::string& fontID) { font_id = fontID; m_text_mesh.create(); diff --git a/src/node_text.h b/src/node_text.h index c4ee6d2..1b71c06 100644 --- a/src/node_text.h +++ b/src/node_text.h @@ -15,8 +15,8 @@ public: std::string m_text; std::string m_font = "arial"; glm::vec4 m_color{ 1, 1, 1, 1 }; - int m_font_size = 11; - kFont font_id; + int m_font_size = 17; + std::string font_id; bool m_multiline = false; glm::vec2 m_off = { 0, 0 }; @@ -32,6 +32,6 @@ public: void set_text(const std::string& s); void set_text_format(const char* fmt, ...); - void set_font(kFont fontID); + void set_font(const std::string& fontID); void update_layout(); }; diff --git a/src/node_text_input.cpp b/src/node_text_input.cpp index 620c496..0951edf 100644 --- a/src/node_text_input.cpp +++ b/src/node_text_input.cpp @@ -184,9 +184,7 @@ void NodeTextInput::create() NodeBorder::create(); if (!m_font.empty()) { - char font[64]; - sprintf(font, "%s-%d", m_font.c_str(), m_font_size); - font_id = (kFont)const_hash(font); + font_id = fmt::format("{}-{}", m_font, m_font_size); m_text_mesh.create(); m_text_mesh.update(font_id, m_text); update_layout(); diff --git a/src/node_text_input.h b/src/node_text_input.h index 46d71a4..fe8d87e 100644 --- a/src/node_text_input.h +++ b/src/node_text_input.h @@ -18,8 +18,8 @@ public: std::string m_text; std::string m_font = "arial"; glm::vec4 m_color{ 1, 1, 1, 1 }; - int m_font_size = 11; - kFont font_id; + int m_font_size = 17; + std::string font_id; bool m_multiline = false; bool m_cursor_visible = false;