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");
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);

View File

@@ -852,24 +852,27 @@ void App::dialog_whatsnew(bool force_show)
if (success)
{
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));
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()

View File

@@ -6,7 +6,7 @@
#include "util.h"
#include "app.h"
std::map<kFont, Font> FontManager::m_fonts;
std::map<std::string, Font> 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);

View File

@@ -3,12 +3,6 @@
#include "util.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
{
public:
@@ -33,11 +27,11 @@ public:
class FontManager
{
public:
static std::map<kFont, Font> m_fonts;
static std::map<std::string, Font> 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();
};

View File

@@ -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;

View File

@@ -60,7 +60,7 @@ void NodeDialogCloud::load_thumbs_thread()
align->SetAlign(YGAlignCenter);
align->SetJustify(YGJustifyCenter);
auto* text = align->add_child<NodeText>();
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);

View File

@@ -42,7 +42,7 @@ std::future<bool> NodeRemotePage::load_url(const std::string& url,
align->SetAlign(YGAlignCenter);
align->SetJustify(YGJustifyCenter);
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...");
m_url = url;

View File

@@ -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();

View File

@@ -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();
};

View File

@@ -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();

View File

@@ -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;