The Battle for Wesnoth  1.15.1+dev
slider.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2018 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
19 
22 
23 namespace gui2
24 {
25 namespace implementation
26 {
27 struct builder_slider;
28 }
29 
30 // ------------ WIDGET -----------{
31 
32 /** A slider. */
33 class slider : public slider_base, public integer_selector
34 {
36 
37 public:
38  explicit slider(const implementation::builder_slider& builder);
39 
40  /***** ***** ***** ***** layout functions ***** ***** ***** *****/
41 
42 private:
43  /** See @ref widget::calculate_best_size. */
44  virtual point calculate_best_size() const override;
45 
46 public:
47  /***** ***** ***** ***** Inherited ***** ***** ***** *****/
48 
49  /** Inherited from integer_selector. */
50  virtual void set_value(int value) override;
51 
52  /** Inherited from integer_selector. */
53  virtual int get_value() const override
54  {
55  return minimum_value_ + get_slider_position() * get_step_size();
56  }
57 
58  /** Inherited from integer_selector. */
59  virtual int get_minimum_value() const override
60  {
61  return minimum_value_;
62  }
63 
64  /** Inherited from integer_selector. */
65  virtual int get_maximum_value() const override
66  {
67  // The number of items needs to include the begin and end so count - 1.
68  return minimum_value_ + slider_get_item_last() * step_size_;
69  }
70 
71  int get_item_count() const
72  {
73  assert(step_size_ != 0);
74  return slider_get_item_last() * step_size_ + 1;
75  }
76 
77  unsigned get_step_size() const
78  {
79  return step_size_;
80  }
81 
82  void set_step_size(int step_size);
83 
84  /***** ***** ***** setters / getters for members ***** ****** *****/
85  void set_best_slider_length(const unsigned length)
86  {
87  best_slider_length_ = length;
88  set_is_dirty(true);
89  }
90 
91  void set_value_range(int min_value, int max_value);
92 
93  void set_minimum_value_label(const t_string& minimum_value_label)
94  {
95  minimum_value_label_ = minimum_value_label;
96  }
97 
98  void set_maximum_value_label(const t_string& maximum_value_label)
99  {
100  maximum_value_label_ = maximum_value_label;
101  }
102 
103  void set_value_labels(const std::vector<t_string>& value_labels);
104 
105  using label_generator = std::function<t_string(int /*current position*/, int /*num positions*/)>;
106 
108  {
109  value_label_generator_ = generator;
110  }
111 
112  /**
113  * Returns the label shown for the current value.
114  *
115  * @returns The label for the current value, if no label
116  * for the current label is defined, it returns
117  * the result of get_value().
118  */
119  t_string get_value_label() const;
120 
121 protected:
122  /** Inherited from scrollbar_base. */
123  virtual void child_callback_positioner_moved() override;
124 
125 private:
126  /** The best size for the slider part itself, if 0 ignored. */
128 
129  /**
130  * The minimum value the slider holds.
131  *
132  * The maximum value is minimum + item_last_.
133  * The current value is minimum + item_position_.
134  */
137 
138  /** Inherited from scrollbar_base. */
139  virtual unsigned get_length() const override
140  {
141  return get_width();
142  }
143 
144  /** Inherited from scrollbar_base. */
145  int positioner_length() const override;
146 
147  /** Inherited from scrollbar_base. */
148  unsigned offset_before() const override;
149 
150  /** Inherited from scrollbar_base. */
151  unsigned offset_after() const override;
152 
153  /** Inherited from scrollbar_base. */
154  bool on_positioner(const point& coordinate) const override;
155 
156  /** Inherited from scrollbar_base. */
157  int on_bar(const point& coordinate) const override;
158 
159  /** Inherited from scrollbar_base. */
160  int get_length_difference(const point& original, const point& current) const override
161  {
162  return current.x - original.x;
163  }
164 
165  /** Inherited from scrollbar_base. */
166  // void move_positioner(const int distance) override;
167 
168  /** See @ref styled_widget::update_canvas. */
169  virtual void update_canvas() override;
170 
171  /**
172  * When the slider shows the minimum value can show a special text.
173  * If this text is not empty this text is shown else the minimum value.
174  */
176 
177  /**
178  * When the slider shows the maximum value can show a special text.
179  * If this text is not empty this text is shown else the maximum value.
180  */
182 
183  /**
184  * Function to output custom value labels for the slider. When set
185  * its output is shown instead of the numeric values. It also overrides
186  * minimum_value_label_ and maximum_value_label_.
187  */
189 
190  /**
191  * When initially pressing the positioner and every time a new value is chosen through dragging,
192  * this value is updated with the mouse position at the time. This allows the widget to track
193  * how far the mouse has moved since setting the last value.
194  */
196 
197  // void update_current_item_mouse_position();
198 
199 public:
200  /** Static type getter that does not rely on the widget being constructed. */
201  static const std::string& type();
202 
203 private:
204  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
205  virtual const std::string& get_control_type() const override;
206 
207  /**
208  * Handlers for keyboard input
209  */
210  void handle_key_decrease(bool& handled);
211  void handle_key_increase(bool& handled);
212 
213  /**
214  * Signal handlers:
215  */
216  void signal_handler_sdl_key_down(const event::ui_event event, bool& handled, const SDL_Keycode key);
217 
218  // void signal_handler_left_button_down(const event::ui_event event, bool& handled);
219 
220  // In this subclass, only used to grab keyboard focus -
221  // see scrollbar_base class for more handling of this event.
222  void signal_handler_left_button_up(const event::ui_event event, bool& handled);
223 };
224 
225 // }---------- DEFINITION ---------{
226 
228 {
229  explicit slider_definition(const config& cfg);
230 
232  {
233  explicit resolution(const config& cfg);
234 
236 
237  unsigned left_offset;
238  unsigned right_offset;
239  };
240 };
241 
242 // }---------- BUILDER -----------{
243 
244 namespace implementation
245 {
247 {
248  explicit builder_slider(const config& cfg);
249 
251 
252  widget* build() const;
253 
254 private:
258  unsigned step_size_;
259  int value_;
260 
263 
264  /* This vector should have the same number of items as the slider's values. */
265  std::vector<t_string> value_labels_;
266 };
267 
268 } // namespace implementation
269 
270 // }------------ END --------------
271 
272 } // namespace gui2
Base class of a resolution, contains the common keys for a resolution.
point current_item_mouse_position_
When initially pressing the positioner and every time a new value is chosen through dragging...
Definition: slider.hpp:195
virtual widget * build() const =0
std::function< t_string(int, int)> label_generator
Definition: slider.hpp:105
void set_value_labels(const label_generator &generator)
Definition: slider.hpp:107
Base class for all widgets.
Definition: widget.hpp:47
t_string maximum_value_label_
When the slider shows the maximum value can show a special text.
Definition: slider.hpp:181
Small abstract helper class.
int x
x coordinate.
Definition: point.hpp:44
Generic file dialog.
Definition: field-fwd.hpp:22
void set_minimum_value_label(const t_string &minimum_value_label)
Definition: slider.hpp:93
t_string minimum_value_label_
When the slider shows the minimum value can show a special text.
Definition: slider.hpp:175
virtual int get_maximum_value() const override
Inherited from integer_selector.
Definition: slider.hpp:65
int get_item_count() const
Definition: slider.hpp:71
void set_maximum_value_label(const t_string &maximum_value_label)
Definition: slider.hpp:98
Basic template class to generate new items.
unsigned best_slider_length_
The best size for the slider part itself, if 0 ignored.
Definition: slider.hpp:127
int minimum_value_
The minimum value the slider holds.
Definition: slider.hpp:135
window * build(const builder_window::window_resolution *definition)
Builds a window.
Holds a 2D point.
Definition: point.hpp:23
virtual int get_value() const override
Inherited from integer_selector.
Definition: slider.hpp:53
std::vector< t_string > value_labels_
Definition: slider.hpp:265
rng * generator
This generator is automatically synced during synced context.
Definition: random.cpp:60
unsigned get_step_size() const
Definition: slider.hpp:77
label_generator value_label_generator_
Function to output custom value labels for the slider.
Definition: slider.hpp:188
A slider.
Definition: slider.hpp:33
int step_size_
Definition: slider.hpp:136
int get_length_difference(const point &original, const point &current) const override
Inherited from scrollbar_base.
Definition: slider.hpp:160
point resolution()
Definition: general.cpp:373
Base class for a scroll bar.
Definition: slider_base.hpp:39
void set_best_slider_length(const unsigned length)
Definition: slider.hpp:85
map_location coordinate
Contains an x and y coordinate used for starting positions in maps.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
virtual unsigned get_length() const override
Inherited from scrollbar_base.
Definition: slider.hpp:139
virtual int get_minimum_value() const override
Inherited from integer_selector.
Definition: slider.hpp:59
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.
ui_event
The event send to the dispatcher.
Definition: handler.hpp:55