Added an helper function for creating bound status labels
This commit is contained in:
parent
c03bd5411f
commit
7c663bc0a9
6 changed files with 78 additions and 48 deletions
|
@ -720,6 +720,7 @@
|
|||
<Unit filename="../../src/gui/widgets/spacer.hpp" />
|
||||
<Unit filename="../../src/gui/widgets/stacked_widget.cpp" />
|
||||
<Unit filename="../../src/gui/widgets/stacked_widget.hpp" />
|
||||
<Unit filename="../../src/gui/widgets/status_label_helper.hpp" />
|
||||
<Unit filename="../../src/gui/widgets/text.cpp" />
|
||||
<Unit filename="../../src/gui/widgets/text.hpp" />
|
||||
<Unit filename="../../src/gui/widgets/text_box.cpp" />
|
||||
|
|
|
@ -547,6 +547,7 @@ set(wesnoth-gui_widget_SRC
|
|||
gui/widgets/settings.cpp
|
||||
gui/widgets/slider.cpp
|
||||
gui/widgets/spacer.cpp
|
||||
gui/widgets/status_label_helper.cpp
|
||||
gui/widgets/stacked_widget.cpp
|
||||
gui/widgets/text.cpp
|
||||
gui/widgets/text_box.cpp
|
||||
|
|
|
@ -460,6 +460,7 @@ wesnoth_sources = Split("""
|
|||
gui/widgets/settings.cpp
|
||||
gui/widgets/slider.cpp
|
||||
gui/widgets/spacer.cpp
|
||||
gui/widgets/status_label_helper.cpp
|
||||
gui/widgets/stacked_widget.cpp
|
||||
gui/widgets/text_box.cpp
|
||||
gui/widgets/text.cpp
|
||||
|
|
|
@ -15,10 +15,11 @@
|
|||
|
||||
#include "gui/dialogs/editor/generator_settings.hpp"
|
||||
|
||||
#include "formatter.hpp"
|
||||
#include "gui/auxiliary/field.hpp"
|
||||
#include "gui/widgets/label.hpp"
|
||||
#include "gui/widgets/settings.hpp"
|
||||
#include "gui/widgets/slider.hpp"
|
||||
#include "gui/widgets/status_label_helper.hpp"
|
||||
#include "gettext.hpp"
|
||||
|
||||
#include "utils/functional.hpp"
|
||||
|
@ -32,10 +33,16 @@ static int min_size = 20;
|
|||
|
||||
REGISTER_DIALOG(generator_settings)
|
||||
|
||||
static const auto landform_label = [](tslider& s)->std::string {
|
||||
return s.get_value() == 0 ? _("Inland") : (s.get_value() < max_coastal ? _("Coastal") : _("Island"));
|
||||
};
|
||||
|
||||
tgenerator_settings::tgenerator_settings(generator_data& data)
|
||||
: players_(register_integer("players", true, data.nplayers))
|
||||
, width_(register_integer("width", true, data.width))
|
||||
, height_(register_integer("height", true, data.height))
|
||||
, update_width_label_()
|
||||
, update_height_label_()
|
||||
{
|
||||
register_integer("hills_num", true, data.iterations);
|
||||
register_integer("hills_size", true, data.hill_size);
|
||||
|
@ -54,46 +61,14 @@ void tgenerator_settings::pre_show(twindow& window)
|
|||
connect_signal_notify_modified(*players_->widget(), std::bind(
|
||||
&tgenerator_settings::adjust_minimum_size_by_players, this, std::ref(window)));
|
||||
|
||||
bind_status_label(window, "players");
|
||||
bind_status_label(window, "width");
|
||||
bind_status_label(window, "height");
|
||||
bind_status_label(window, "villages", _("/1000 tiles"));
|
||||
bind_status_label(window, "castle_size");
|
||||
bind_landform_status_label(window);
|
||||
}
|
||||
gui2::bind_status_label<tslider>(window, "players");
|
||||
|
||||
void tgenerator_settings::bind_status_label(twindow& window, const std::string& id, const std::string& suffix)
|
||||
{
|
||||
tslider& slider = find_widget<tslider>(&window, id, false);
|
||||
tlabel& label = find_widget<tlabel>(&window, id + "_label", false);
|
||||
update_width_label_ = gui2::bind_status_label<tslider>(window, "width");
|
||||
update_height_label_ = gui2::bind_status_label<tslider>(window, "height");
|
||||
|
||||
label.set_label(std::to_string(slider.get_value()) + suffix);
|
||||
|
||||
connect_signal_notify_modified(slider, std::bind(
|
||||
&tgenerator_settings::status_label_callback, this, std::ref(slider), std::ref(label), suffix));
|
||||
}
|
||||
|
||||
void tgenerator_settings::status_label_callback(tslider& slider, tlabel& label, const std::string& suffix)
|
||||
{
|
||||
label.set_label(std::to_string(slider.get_value()) + suffix);
|
||||
}
|
||||
|
||||
// TODO: remove
|
||||
void tgenerator_settings::bind_landform_status_label(twindow& window)
|
||||
{
|
||||
tslider& slider = find_widget<tslider>(&window, "landform", false);
|
||||
tlabel& label = find_widget<tlabel>(&window, "landform_label", false);
|
||||
|
||||
landform_status_label_callback(slider, label);
|
||||
|
||||
connect_signal_notify_modified(slider, std::bind(
|
||||
&tgenerator_settings::landform_status_label_callback, this, std::ref(slider), std::ref(label)));
|
||||
}
|
||||
|
||||
// TODO: remove
|
||||
void tgenerator_settings::landform_status_label_callback(tslider& slider, tlabel& label)
|
||||
{
|
||||
label.set_label(slider.get_value() == 0 ? _("Inland") : (slider.get_value() < max_coastal ? _("Coastal") : _("Island")));
|
||||
gui2::bind_status_label<tslider>(window, "villages", [](tslider& s)->std::string { return formatter() << s.get_value() << _("/1000 tiles"); });
|
||||
gui2::bind_status_label<tslider>(window, "castle_size");
|
||||
gui2::bind_status_label<tslider>(window, "landform", [](tslider& s)->std::string { return landform_label(s); });
|
||||
}
|
||||
|
||||
void tgenerator_settings::adjust_minimum_size_by_players(twindow& window)
|
||||
|
@ -103,12 +78,13 @@ void tgenerator_settings::adjust_minimum_size_by_players(twindow& window)
|
|||
const auto update_dimension_slider = [&](tfield_integer* field) {
|
||||
tslider& w = dynamic_cast<tslider&>(*field->widget());
|
||||
w.set_minimum_value(min_size + extra_size);
|
||||
|
||||
status_label_callback(w, find_widget<tlabel>(&window, w.id() + "_label", false));
|
||||
};
|
||||
|
||||
update_dimension_slider(width_);
|
||||
update_dimension_slider(height_);
|
||||
|
||||
update_width_label_();
|
||||
update_height_label_();
|
||||
}
|
||||
|
||||
} // end namespace gui2
|
||||
|
|
|
@ -37,13 +37,6 @@ public:
|
|||
private:
|
||||
void pre_show(twindow& window);
|
||||
|
||||
void bind_status_label(twindow& window, const std::string& id, const std::string& suffix = "");
|
||||
void status_label_callback(tslider& slider, tlabel& label, const std::string& suffix = "");
|
||||
|
||||
// TODO: find a more generic way to do this
|
||||
void bind_landform_status_label(twindow& window);
|
||||
void landform_status_label_callback(tslider& slider, tlabel& label);
|
||||
|
||||
void adjust_minimum_size_by_players(twindow& window);
|
||||
|
||||
/** Inherited from tdialog, implemented by REGISTER_DIALOG. */
|
||||
|
@ -53,6 +46,8 @@ private:
|
|||
tfield_integer* players_;
|
||||
tfield_integer* width_;
|
||||
tfield_integer* height_;
|
||||
|
||||
std::function<void()> update_width_label_, update_height_label_;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
56
src/gui/widgets/status_label_helper.hpp
Normal file
56
src/gui/widgets/status_label_helper.hpp
Normal file
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
Copyright (C) 2010 - 2016 by the Battle for Wesnoth Project http://www.wesnoth.org/
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY.
|
||||
|
||||
See the COPYING file for more details.
|
||||
*/
|
||||
|
||||
#ifndef GUI_WIDGETS_STATUS_LABEL_HELPER_HPP_INCLUDED
|
||||
#define GUI_WIDGETS_STATUS_LABEL_HELPER_HPP_INCLUDED
|
||||
|
||||
#include "gui/core/event/dispatcher.hpp"
|
||||
#include "gui/widgets/control.hpp"
|
||||
|
||||
#include "utils/functional.hpp"
|
||||
|
||||
namespace gui2 {
|
||||
|
||||
/**
|
||||
* Creates a bound status label that will reflect the state of a widget. The initial label value
|
||||
* is set here, and then again any time the widget is modified. A function is also returned that
|
||||
* can be called to update the field manually.
|
||||
*
|
||||
* Currently, it only works on types that have a get_value() member and fire a MODIFIED event,
|
||||
* but could be changed in the future.
|
||||
*/
|
||||
template<typename W>
|
||||
std::function<void()> bind_status_label(twindow& window, const std::string& id,
|
||||
std::function<std::string(W&)> value_getter = [](W& w)->std::string { return std::to_string(w.get_value()); })
|
||||
{
|
||||
W& source = find_widget<W>(&window, id, false);
|
||||
tcontrol& label = find_widget<tcontrol>(&window, id + "_label", false);
|
||||
|
||||
const auto update_label = [&, value_getter]() {
|
||||
const std::string value = value_getter(source);
|
||||
|
||||
label.set_label(value);
|
||||
};
|
||||
|
||||
// Bind the callback
|
||||
connect_signal_notify_modified(source, std::bind(update_label));
|
||||
|
||||
// Set initial value
|
||||
update_label();
|
||||
|
||||
return update_label;
|
||||
}
|
||||
|
||||
} // namespace gui2
|
||||
|
||||
#endif
|
Loading…
Add table
Reference in a new issue