rich label: support specifying text color, font and style in resolution

This allows the rich_label definitions in rich_label_default to work.
This commit is contained in:
Subhraman Sarkar 2024-11-28 23:04:05 +05:30
parent 2e0f8c453e
commit 2477618306
4 changed files with 63 additions and 4 deletions

View file

@ -20,6 +20,8 @@
max_width = 0
max_height = 0
text_font_color_enabled = {FONT_COLOR_ENABLED}
text_font_color_disabled = {FONT_COLOR_DISABLED}
text_font_family = {FONT_FAMILY}
text_font_size = {FONT_SIZE}
text_font_style = {FONT_STYLE}
@ -51,8 +53,8 @@
({FONT_FAMILY})
({GUI_FONT_SIZE_{FONT_SIZE}})
({FONT_STYLE})
({GUI__FONT_COLOR_ENABLED__{FONT_COLOR} ALPHA=""})
({GUI__FONT_COLOR_DISABLED__{FONT_COLOR} ALPHA=""})
({GUI__FONT_COLOR_ENABLED__{FONT_COLOR} ALPHA="255"})
({GUI__FONT_COLOR_DISABLED__{FONT_COLOR} ALPHA="255"})
}
{_GUI_RESOLUTION
@ -60,8 +62,8 @@
({FONT_FAMILY})
({GUI_SCALE_RESOLUTION {GUI_FONT_SIZE_{FONT_SIZE}}})
({FONT_STYLE})
({GUI__FONT_COLOR_ENABLED__{FONT_COLOR} ALPHA=""})
({GUI__FONT_COLOR_DISABLED__{FONT_COLOR} ALPHA=""})
({GUI__FONT_COLOR_ENABLED__{FONT_COLOR} ALPHA="255"})
({GUI__FONT_COLOR_DISABLED__{FONT_COLOR} ALPHA="255"})
}
[/rich_label_definition]

View file

@ -219,6 +219,9 @@
{DEFAULT_KEY "text_font_family" font_family "sans"}
{DEFAULT_KEY "text_font_size" f_unsigned 0}
{DEFAULT_KEY "text_font_style" font_style ""}
{DEFAULT_KEY "text_font_color_enabled" color ""}
{DEFAULT_KEY "text_font_color_disabled" color ""}
{DEFAULT_KEY "debug_border_color" color ""}
{DEFAULT_KEY "window_height" unsigned 0}
{DEFAULT_KEY "window_width" unsigned 0}
[/tag]

View file

@ -734,7 +734,10 @@ std::pair<config, point> rich_label::get_parsed_text(
void rich_label::default_text_config(config* txt_ptr, t_string text) {
if (txt_ptr != nullptr) {
(*txt_ptr)["text"] = text;
(*txt_ptr)["color"] = text_color_enabled_.to_rgba_string();
(*txt_ptr)["font_family"] = font_family_;
(*txt_ptr)["font_size"] = font_size_;
(*txt_ptr)["font_style"] = font_style_;
(*txt_ptr)["text_alignment"] = encode_text_alignment(get_text_alignment());
(*txt_ptr)["x"] = "(pos_x)";
(*txt_ptr)["y"] = "(pos_y)";
@ -915,8 +918,12 @@ rich_label_definition::rich_label_definition(const config& cfg)
rich_label_definition::resolution::resolution(const config& cfg)
: resolution_definition(cfg)
, text_color_enabled(color_t::from_rgba_string(cfg["text_font_color_enabled"].str()))
, text_color_disabled(color_t::from_rgba_string(cfg["text_font_color_disabled"].str()))
, link_color(cfg["link_color"].empty() ? font::YELLOW_COLOR : color_t::from_rgba_string(cfg["link_color"].str()))
, font_family(cfg["text_font_family"].str())
, font_size(cfg["text_font_size"].to_int(font::SIZE_NORMAL))
, font_style(cfg["text_font_style"].str("normal"))
{
// Note the order should be the same as the enum state_t is rich_label.hpp.
state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_enabled", missing_mandatory_wml_tag("rich_label_definition][resolution", "state_enabled")));
@ -944,8 +951,12 @@ std::unique_ptr<widget> builder_rich_label::build() const
assert(conf);
lbl->set_text_alignment(text_alignment);
lbl->set_text_color(conf->text_color_enabled, true);
lbl->set_text_color(conf->text_color_enabled, false);
lbl->set_link_color(conf->link_color);
lbl->set_font_family(conf->font_family);
lbl->set_font_size(conf->font_size);
lbl->set_font_style(conf->font_style);
lbl->set_label(lbl->get_label());
DBG_GUI_G << "Window builder: placed rich_label '" << id << "' with definition '"

View file

@ -15,6 +15,7 @@
#pragma once
#include "color.hpp"
#include "gui/widgets/styled_widget.hpp"
#include "font/standard_colors.hpp"
@ -108,13 +109,32 @@ public:
can_shrink_ = can_shrink;
}
void set_font_family(const std::string& font_family)
{
font_family_ = font_family;
}
void set_font_size(int font_size)
{
font_size_ = font_size;
}
void set_font_style(const std::string& font_style)
{
font_style_ = font_style;
}
void set_text_alpha(unsigned short alpha);
void set_text_color(const color_t& color, bool enabled)
{
if (enabled) {
text_color_enabled_ = color;
} else {
text_color_disabled_ = color;
}
}
const t_string& get_label() const
{
return unparsed_text_.empty() ? styled_widget::get_label() : unparsed_text_;
@ -174,16 +194,36 @@ private:
*/
bool link_aware_;
/**
* Base text color, enabled state
*/
color_t text_color_enabled_;
/**
* Base text color, disabled state
*/
color_t text_color_disabled_;
/**
* What color links will be rendered in.
*/
color_t link_color_;
/**
* Base font family
*/
std::string font_family_;
/**
* Base font size
*/
int font_size_;
/**
* Base font style
*/
std::string font_style_;
bool can_shrink_;
unsigned short text_alpha_;
@ -292,8 +332,11 @@ struct rich_label_definition : public styled_widget_definition
{
explicit resolution(const config& cfg);
color_t text_color_enabled, text_color_disabled;
color_t link_color;
std::string font_family;
int font_size;
std::string font_style;
};
};