Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions resources/gui/default.theme.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[base] {
font: gui/fonts/BigShouldersDisplay-SemiBold.ttf

# Positive offset moves the font down, negative moves it up.
font_offset: 0.0

[button] {
color: #ffffff
size: 30
Expand Down Expand Up @@ -75,7 +77,7 @@
image.focus: gui/widget/TextEntryBackground.focused.png
}
}

[luaconsole] {
size: 12
font: gui/fonts/RobotoMono-SemiBold.ttf
Expand Down Expand Up @@ -164,5 +166,6 @@
# Stronger font used at a few locations without UI widgets.
[bold] {
font: gui/fonts/BigShouldersDisplay-ExtraBold.ttf
offset: 0.0
}
}
6 changes: 4 additions & 2 deletions src/gui/theme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ bool GuiTheme::loadTheme(const string& name, const string& resource_name)
else
global_style.color = {255, 255, 255, 255};
global_style.font = getFont(input["font"]);
global_style.font_offset = input["font_offset"].toFloat();
global_style.size = input["size"].toFloat();
global_style.sound = input["sound"];
for(unsigned int n=0; n<int(GuiElement::State::COUNT); n++)
Expand Down Expand Up @@ -129,6 +130,8 @@ bool GuiTheme::loadTheme(const string& name, const string& resource_name)
style.states[n].color = toColor(input["color." + postfix]);
if (input.find("font." + postfix) != input.end())
style.states[n].font = getFont(input["font." + postfix]);
if (input.find("font_offset." + postfix) != input.end())
style.states[n].font_offset = input["font_offset." + postfix].toFloat();
if (input.find("size." + postfix) != input.end())
style.states[n].size = input["size." + postfix].toFloat();
if (input.find("sound." + postfix) != input.end())
Expand All @@ -151,9 +154,8 @@ GuiTheme::GuiTheme(const string& name)
fallback_state.font = nullptr;
std::vector<string> fonts = findResources("gui/fonts/*.ttf");
if(fonts.size() > 0)
{
fallback_state.font = getFont(fonts[0]);
}
fallback_state.font_offset = 0.0f;
fallback_state.texture = "";
GuiThemeStyle fallback;
for(unsigned int n=0; n<int(GuiElement::State::COUNT); n++)
Expand Down
8 changes: 5 additions & 3 deletions src/gui/theme.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,26 @@ class GuiThemeStyle
glm::u8vec4 color;
float size; //general size parameter, depends on the widget type what it means.
sp::Font* font;
float font_offset = 0.0f;
string sound; //Sound effect played by the widget on certain actions.
};
StateStyle states[int(GuiElement::State::COUNT)];
const StateStyle& get(GuiElement::State state) const { return states[int(state)]; }
};

/** The Theme class is used by the GuiElement classes to style themselves.

Themes are loaded from a text resource, and referenced from GuiElement classes.
A single theme contains information on how to style different widget elements.

Each element describes the following properties:
- texture
- color
- font
- offset
- size
- sound

With the possibility to distingish with the following states:
- normal: Default state
- disabled: When enabled is false
Expand Down
10 changes: 8 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,9 @@ int main(int argc, char** argv)
soundManager->setMusicVolume(PreferencesManager::get("music_volume", "50").toFloat());
soundManager->setMasterSoundVolume(PreferencesManager::get("sound_volume", "50").toFloat());

main_font = GuiTheme::getCurrentTheme()->getStyle("base")->states[0].font;
bold_font = GuiTheme::getCurrentTheme()->getStyle("bold")->states[0].font;
const auto& active_theme = GuiTheme::getCurrentTheme();
main_font = active_theme->getStyle("base")->get(GuiElement::State::Normal).font;
bold_font = active_theme->getStyle("bold")->get(GuiElement::State::Normal).font;
if (!main_font || !bold_font)
{
LOG(ERROR, "Missing font or bold font.");
Expand All @@ -167,6 +168,11 @@ int main(int argc, char** argv)

sp::RenderTarget::setDefaultFont(main_font);

// Apply baseline offset adjustments to fonts
// Positive values move text down, negative values move text up
main_font->setBaselineOffset(active_theme->getStyle("base")->get(GuiElement::State::Normal).font_offset);
bold_font->setBaselineOffset(active_theme->getStyle("bold")->get(GuiElement::State::Normal).font_offset);

// On Android, this requires the 'record audio' permissions,
// which is always a scary thing for users.
// Since there is no way to access it (yet) via a touchscreen, compile out.
Expand Down
Loading