GUI2/Main Menu: handle tip formatting in definition (fixes #9662)

This commit is contained in:
Charles Dang 2025-02-09 23:58:25 -05:00
parent 96f8e7604f
commit fcbbabd646
3 changed files with 50 additions and 39 deletions

View File

@ -126,7 +126,7 @@ where
[label]
id = "source"
definition = "title_screen_tip"
definition = "title_screen_tip_plain"
text_alignment = "right"
wrap = true

View File

@ -5,7 +5,7 @@
### Defines the following labels
### - title_screen_tip, the one for the tips in the title screen, with a maximum width
#define _GUI_RESOLUTION RESOLUTION FONT_SIZE FONT_STYLE FONT_COLOR_ENABLED FONT_COLOR_DISABLED
#define _GUI_RESOLUTION RESOLUTION FONT_SIZE FONT_STYLE FONT_COLOR_ENABLED FONT_COLOR_DISABLED _ATTRIBUTES
[resolution]
{RESOLUTION}
@ -38,6 +38,9 @@
color = {FONT_COLOR_ENABLED}
text = "(text)"
text_markup = "(text_markup)"
{_ATTRIBUTES}
[/text]
[/draw]
@ -60,6 +63,9 @@
color = {FONT_COLOR_DISABLED}
text = "(text)"
text_markup = "(text_markup)"
{_ATTRIBUTES}
[/text]
[/draw]
@ -79,6 +85,36 @@
()
("255,255,255,255")
({GUI__FONT_COLOR_DISABLED__DEFAULT})
(
[attribute]
name = "font_family"
start = 0
end = 1
value = "script"
[/attribute]
[attribute]
name = "font_size"
start = 0
end = 1
value = {GUI_FONT_SIZE_VERY_LARGE}
[/attribute]
)
}
[/label_definition]
[label_definition]
id = "title_screen_tip_plain"
description = "Label used for the tips in the title screen"
{_GUI_RESOLUTION
()
({GUI_FONT_SIZE_DEFAULT})
()
("255,255,255,255")
({GUI__FONT_COLOR_DISABLED__DEFAULT})
()
}
[/label_definition]

View File

@ -194,43 +194,18 @@ void title_screen::init_callbacks()
//
// Tip-of-the-day browser
//
multi_page* tip_pages = find_widget<multi_page>("tips", false, false);
if(tip_pages != nullptr) {
std::vector<game_tip> tips = tip_of_the_day::shuffle(settings::tips);
if(tips.empty()) {
WRN_CF << "There are no tips of day available.";
}
for(const auto& tip : tips) {
widget_item widget;
widget_data page;
widget["use_markup"] = "true";
// Use pango markup to insert drop cap
// Example: Lawful units -> <span ...>L</span>awful units
// If tip starts with a tag, we need to insert the <span> after it
// then insert the </span> tag after the first character of the text
// after markup. Assumes that the tags themselves don't
// contain non-ASCII characters.
// Example: <i>Lawful</i> units -> <i><span ...>L</span>awful</i> units
const std::string& script_font = font::get_font_families(font::FONT_SCRIPT);
std::string tip_text = tip.text().str();
std::size_t pos = 0;
while (pos < tip_text.size() && tip_text.at(pos) == '<') {
pos = tip_text.find_first_of(">", pos) + 1;
}
utf8::insert(tip_text, pos+1, "</span>");
utf8::insert(tip_text, pos, "<span font_family='" + script_font + "' font_size='xx-large'>");
widget["label"] = tip_text;
page.emplace("tip", widget);
widget["label"] = tip.source();
page.emplace("source", widget);
tip_pages->add_page(page);
if(auto tip_pages = find_widget<multi_page>("tips", false, false)) {
for(const game_tip& tip : tip_of_the_day::shuffle(settings::tips)) {
tip_pages->add_page({
{ "tip", {
{ "use_markup", "true" },
{ "label", tip.text() }
}},
{ "source", {
{ "use_markup", "true" },
{ "label", tip.source() }
}}
});
}
update_tip(true);