mirror of
https://github.com/wesnoth/wesnoth
synced 2025-05-05 19:21:17 +00:00
GUI2: cleaned up and improved up bind_status_label implementation
- Used constexpr if instead of SFINAE for the default getter functions - Added support for menu buttons in the default getter function - Fixes potentially calling get_value_label on classes derived from integer_selector other than slider (this function is a member of the slider class) - Made value getters take a const reference (we don't want to be able to modify widgets here) - Made value getters return a t_string (avoids conversion to std::string and set_label takes a t_string anyway)
This commit is contained in:
parent
1d0b245504
commit
fb35023814
@ -66,9 +66,9 @@ void generator_settings::pre_show(window& window)
|
||||
// Do this *after* assigning the 'update_*_label_` functions or the game will crash!
|
||||
adjust_minimum_size_by_players();
|
||||
|
||||
gui2::bind_status_label<slider>(&window, "villages", [](slider& s)->std::string { return formatter() << s.get_value() << _("/1000 tiles"); });
|
||||
gui2::bind_status_label<slider>(&window, "villages", [](const slider& s) { return t_string(formatter() << s.get_value() << _("/1000 tiles")); });
|
||||
gui2::bind_status_label<slider>(&window, "castle_size");
|
||||
gui2::bind_status_label<slider>(&window, "landform", [](slider& s)->std::string {
|
||||
gui2::bind_status_label<slider>(&window, "landform", [](const slider& s) {
|
||||
return s.get_value() == 0 ? _("Inland") : (s.get_value() < max_coastal ? _("Coastal") : _("Island")); });
|
||||
}
|
||||
|
||||
|
@ -670,9 +670,7 @@ void preferences_dialog::post_build(window& window)
|
||||
connect_signal_notify_modified(menu,
|
||||
std::bind([=](widget& w) { set(pref_name, option_ids[dynamic_cast<menu_button&>(w).get_value()]); }, std::placeholders::_1));
|
||||
|
||||
gui2::bind_status_label<menu_button>(main_grid, "setter", [](menu_button& m)->std::string {
|
||||
return m.get_value_string();
|
||||
}, "value");
|
||||
gui2::bind_status_label<menu_button>(main_grid, "setter", default_status_value_getter<menu_button>, "value");
|
||||
|
||||
break;
|
||||
}
|
||||
|
@ -15,7 +15,7 @@
|
||||
|
||||
#include "gettext.hpp"
|
||||
#include "gui/core/event/dispatcher.hpp"
|
||||
#include "gui/widgets/integer_selector.hpp"
|
||||
#include "gui/widgets/menu_button.hpp"
|
||||
#include "gui/widgets/selectable_item.hpp"
|
||||
#include "gui/widgets/styled_widget.hpp"
|
||||
|
||||
@ -23,24 +23,25 @@
|
||||
|
||||
namespace gui2
|
||||
{
|
||||
/**
|
||||
* Default value getter for selectable widgets (like toggle buttons)
|
||||
*/
|
||||
template<typename T>
|
||||
static inline std::enable_if_t<std::is_base_of_v<selectable_item, T>, std::string>
|
||||
default_status_value_getter(T& w)
|
||||
t_string default_status_value_getter(const T& w)
|
||||
{
|
||||
return w.get_value_bool() ? _("yes") : _("no");
|
||||
}
|
||||
// Menu Buttons
|
||||
if constexpr(std::is_same_v<menu_button, T>) {
|
||||
return w.get_value_string();
|
||||
}
|
||||
|
||||
/**
|
||||
* Default value getter for integer-based widgets (like sliders)
|
||||
*/
|
||||
template<typename T>
|
||||
static inline std::enable_if_t<std::is_base_of_v<integer_selector, T>, std::string>
|
||||
default_status_value_getter(T& w)
|
||||
{
|
||||
return w.get_value_label();
|
||||
// Selectable widgets (like toggle buttons)
|
||||
if constexpr(std::is_base_of_v<selectable_item, T>) {
|
||||
return w.get_value_bool() ? _("yes") : _("no");
|
||||
}
|
||||
|
||||
// Sliders
|
||||
if constexpr(std::is_same_v<slider, T>) {
|
||||
return w.get_value_label();
|
||||
}
|
||||
|
||||
return w.get_label();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -63,7 +64,7 @@ template<typename W>
|
||||
std::function<void()> bind_status_label(
|
||||
widget* find_in,
|
||||
const std::string& source_id,
|
||||
const std::function<std::string(W&)> value_getter = default_status_value_getter<W>,
|
||||
const std::function<t_string(const W&)> value_getter = default_status_value_getter<W>,
|
||||
const std::string& label_id = "")
|
||||
{
|
||||
// If no label ID is provided, use the format source ID + '_label'.
|
||||
@ -76,9 +77,7 @@ std::function<void()> bind_status_label(
|
||||
styled_widget& label = find_widget<styled_widget>(find_in, label_id_, false);
|
||||
|
||||
const auto update_label = [&, value_getter]() {
|
||||
const std::string value = value_getter(source);
|
||||
|
||||
label.set_label(value);
|
||||
label.set_label(value_getter(source));
|
||||
};
|
||||
|
||||
// Bind the callback.
|
||||
|
Loading…
x
Reference in New Issue
Block a user