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