Allow the label of the slider to be customized...
...so it can display special values it needed.
This commit is contained in:
parent
b55c497c4a
commit
c4c9ca4060
4 changed files with 119 additions and 26 deletions
|
@ -14,7 +14,7 @@ full_redraw = "true"
|
|||
[line]
|
||||
x1 = 0
|
||||
y1 = "(height / 2)"
|
||||
x2 = "(width - 36)"
|
||||
x2 = "(width - 136)"
|
||||
y2 = "(height / 2)"
|
||||
|
||||
colour = {GROOVE_COLOUR}
|
||||
|
@ -26,7 +26,7 @@ full_redraw = "true"
|
|||
#
|
||||
|
||||
[text]
|
||||
x = "(width - 30)"
|
||||
x = "(width - 130)"
|
||||
y = {TEXT_V_CENTRE}
|
||||
w = 30
|
||||
h = "(height)"
|
||||
|
@ -54,10 +54,10 @@ full_redraw = "true"
|
|||
description = "A slider with it's value on the right hand side."
|
||||
|
||||
[resolution]
|
||||
min_width = 50
|
||||
min_width = 150
|
||||
min_height = 22
|
||||
|
||||
default_width = 150
|
||||
default_width = 250
|
||||
default_height = 22
|
||||
|
||||
max_width = 0
|
||||
|
@ -69,7 +69,7 @@ full_redraw = "true"
|
|||
maximum_positioner_length = 16
|
||||
|
||||
left_offset = 0
|
||||
right_offset = 35
|
||||
right_offset = 135
|
||||
|
||||
[state_enabled]
|
||||
{STATE "" "255, 255, 255, 255" "255, 255, 255, 255"}
|
||||
|
|
|
@ -114,6 +114,22 @@ void tslider::set_maximum_value(const int maximum_value)
|
|||
}
|
||||
}
|
||||
|
||||
t_string tslider::get_value_label() const
|
||||
{
|
||||
if(!value_labels_.empty()) {
|
||||
assert(value_labels_.size() == get_item_count());
|
||||
return value_labels_[get_item_position()];
|
||||
} else if(!minimum_value_label_.empty()
|
||||
&& get_value() == get_minimum_value()) {
|
||||
return minimum_value_label_;
|
||||
} else if(!maximum_value_label_.empty()
|
||||
&& get_value() == get_maximum_value()) {
|
||||
return maximum_value_label_;
|
||||
} else {
|
||||
return t_string((formatter() << get_value()).str());
|
||||
}
|
||||
}
|
||||
|
||||
unsigned tslider::minimum_positioner_length() const
|
||||
{
|
||||
const tslider_definition::tresolution* conf =
|
||||
|
@ -180,7 +196,7 @@ void tslider::update_canvas()
|
|||
tscrollbar_::update_canvas();
|
||||
|
||||
foreach(tcanvas& tmp, canvas()) {
|
||||
tmp.set_variable("text", variant((formatter() << get_value()).str()));
|
||||
tmp.set_variable("text", variant(get_value_label()));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -52,6 +52,26 @@ public:
|
|||
// The number of items needs to include the begin and end so count - 1.
|
||||
{ return minimum_value_ + get_item_count() - 1; }
|
||||
|
||||
/***** ***** ***** setters / getters for members ***** ****** *****/
|
||||
|
||||
void set_minimum_value_label(const t_string& minimum_value_label)
|
||||
{ minimum_value_label_ = minimum_value_label; }
|
||||
|
||||
void set_maximum_value_label(const t_string& maximum_value_label)
|
||||
{ maximum_value_label_ = maximum_value_label; }
|
||||
|
||||
void set_value_labels(const std::vector<t_string>& value_labels)
|
||||
{ value_labels_ = value_labels; }
|
||||
|
||||
/**
|
||||
* Returns the label shown for the current value.
|
||||
*
|
||||
* @returns The label for the current value, if no label
|
||||
* for the current label is defined, it returns
|
||||
* the result of get_value().
|
||||
*/
|
||||
t_string get_value_label() const;
|
||||
|
||||
private:
|
||||
|
||||
/**
|
||||
|
@ -90,6 +110,11 @@ private:
|
|||
/** Inherited from tscrollbar. */
|
||||
void update_canvas();
|
||||
|
||||
t_string minimum_value_label_;
|
||||
t_string maximum_value_label_;
|
||||
|
||||
std::vector<t_string> value_labels_;
|
||||
|
||||
/** Inherited from tcontrol. */
|
||||
const std::string& get_control_type() const
|
||||
{ static const std::string type = "slider"; return type; }
|
||||
|
|
|
@ -161,26 +161,7 @@ struct tbuilder_slider : public tbuilder_control
|
|||
private:
|
||||
tbuilder_slider();
|
||||
public:
|
||||
/*WIKI
|
||||
* @page = GUIToolkitWML
|
||||
* @order = 3_widget_slider
|
||||
*
|
||||
* == Slider ==
|
||||
*
|
||||
* @start_table = config
|
||||
* minimum_value (unsigned = 0) The width of the slider.
|
||||
* maximum_value (unsigned = 0) The height of the slider.
|
||||
* step_size (unsigned = 0) The height of the slider.
|
||||
* value (unsigned = 0) The height of the slider.
|
||||
* @end_table
|
||||
*/
|
||||
tbuilder_slider(const config& cfg) :
|
||||
tbuilder_control(cfg),
|
||||
minimum_value_(lexical_cast_default<unsigned>(cfg["minimum_value"])),
|
||||
maximum_value_(lexical_cast_default<unsigned>(cfg["maximum_value"])),
|
||||
step_size_(lexical_cast_default<unsigned>(cfg["step_size"])),
|
||||
value_(lexical_cast_default<unsigned>(cfg["value"]))
|
||||
{}
|
||||
tbuilder_slider(const config& cfg);
|
||||
|
||||
twidget* build () const;
|
||||
|
||||
|
@ -189,6 +170,11 @@ private:
|
|||
int maximum_value_;
|
||||
int step_size_;
|
||||
int value_;
|
||||
|
||||
t_string minimum_value_label_;
|
||||
t_string maximum_value_label_;
|
||||
|
||||
std::vector<t_string> value_labels_;
|
||||
};
|
||||
|
||||
struct tbuilder_spacer : public tbuilder_control
|
||||
|
@ -1050,6 +1036,61 @@ twidget* tbuilder_panel::build() const
|
|||
return panel;
|
||||
}
|
||||
|
||||
tbuilder_slider::tbuilder_slider(const config& cfg) :
|
||||
tbuilder_control(cfg),
|
||||
minimum_value_(lexical_cast_default<unsigned>(cfg["minimum_value"])),
|
||||
maximum_value_(lexical_cast_default<unsigned>(cfg["maximum_value"])),
|
||||
step_size_(lexical_cast_default<unsigned>(cfg["step_size"])),
|
||||
value_(lexical_cast_default<unsigned>(cfg["value"])),
|
||||
minimum_value_label_(cfg["minimum_value_label"]),
|
||||
maximum_value_label_(cfg["maximum_value_label"]),
|
||||
value_labels_()
|
||||
{
|
||||
/*WIKI
|
||||
* @page = GUIToolkitWML
|
||||
* @order = 3_widget_slider
|
||||
*
|
||||
* == Slider ==
|
||||
*
|
||||
* @start_table = config
|
||||
* minimum_value (unsigned = 0) The width of the slider.
|
||||
* maximum_value (unsigned = 0) The height of the slider.
|
||||
*
|
||||
* step_size (unsigned = 0) The height of the slider.
|
||||
* value (unsigned = 0) The height of the slider.
|
||||
*
|
||||
* minimum_value_label (t_string = "")
|
||||
* If the minimum value is choosen there
|
||||
* might be the need for a special value (eg
|
||||
* off). When this key has a value that value
|
||||
* will be shown if the minimum is selected.
|
||||
* maximum_value_label (t_string = "")
|
||||
* If the maximum value is choosen there
|
||||
* might be the need for a special value (eg
|
||||
* unlimited)). When this key has a value
|
||||
* that value will be shown if the maximum is
|
||||
* selected.
|
||||
* value_labels ([]) It might be the labels need to be shown
|
||||
* are not a lineair number sequence eg (0.5,
|
||||
* 1, 2, 4) in that case for all items this
|
||||
* section can be filled with the values,
|
||||
* which should be the same number of items
|
||||
* as the items in the slider. NOTE if this
|
||||
* option is used, 'minimum_value_label' and
|
||||
* 'maximum_value_label' are ignored.
|
||||
* @end_table
|
||||
*/
|
||||
const config* labels = cfg.child("value_labels");
|
||||
if(labels) {
|
||||
|
||||
const config::child_list& value = labels->get_children("value");
|
||||
foreach(const config* label, value) {
|
||||
|
||||
value_labels_.push_back((*label)["label"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
twidget* tbuilder_slider::build() const
|
||||
{
|
||||
tslider* slider = new tslider();
|
||||
|
@ -1061,6 +1102,17 @@ twidget* tbuilder_slider::build() const
|
|||
slider->set_step_size(step_size_);
|
||||
slider->set_value(value_);
|
||||
|
||||
if(!value_labels_.empty()) {
|
||||
VALIDATE(value_labels_.size() == slider->get_item_count(),
|
||||
_("The number of value_labels and values don't match."));
|
||||
|
||||
slider->set_value_labels(value_labels_);
|
||||
|
||||
} else {
|
||||
slider->set_minimum_value_label(minimum_value_label_);
|
||||
slider->set_maximum_value_label(maximum_value_label_);
|
||||
}
|
||||
|
||||
DBG_G << "Window builder: placed slider '" << id << "' with defintion '"
|
||||
<< definition << "'.\n";
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue