The Battle for Wesnoth  1.19.5+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  void set_value_range(int min_value, int max_value);
91 
92  void set_minimum_value_label(const t_string& minimum_value_label)
93  {
94  minimum_value_label_ = minimum_value_label;
95  }
96 
97  void set_maximum_value_label(const t_string& maximum_value_label)
98  {
99  maximum_value_label_ = maximum_value_label;
100  }
101 
102  void set_value_labels(const std::vector<t_string>& value_labels);
103 
104  using label_generator = std::function<t_string(int /*current position*/, int /*num positions*/)>;
105 
107  {
109  }
110 
111  /**
112  * Returns the label shown for the current value.
113  *
114  * @returns The label for the current value, if no label
115  * for the current label is defined, it returns
116  * the result of get_value().
117  */
118  t_string get_value_label() const;
119 
120 protected:
121  /** Inherited from scrollbar_base. */
122  virtual void child_callback_positioner_moved() override;
123 
124 private:
125  /** The best size for the slider part itself, if 0 ignored. */
127 
128  /**
129  * The minimum value the slider holds.
130  *
131  * The maximum value is minimum + item_last_.
132  * The current value is minimum + item_position_.
133  */
136 
137  /** Inherited from scrollbar_base. */
138  virtual unsigned get_length() const override
139  {
140  return get_width();
141  }
142 
143  /** Inherited from scrollbar_base. */
144  int positioner_length() const override;
145 
146  /** Inherited from scrollbar_base. */
147  unsigned offset_before() const override;
148 
149  /** Inherited from scrollbar_base. */
150  unsigned offset_after() const override;
151 
152  /** Inherited from scrollbar_base. */
153  bool on_positioner(const point& coordinate) const override;
154 
155  /** Inherited from scrollbar_base. */
156  int on_bar(const point& coordinate) const override;
157 
158  /** Inherited from scrollbar_base. */
159  int get_length_difference(const point& original, const point& current) const override
160  {
161  return current.x - original.x;
162  }
163 
164  /** Inherited from scrollbar_base. */
165  // void move_positioner(const int distance) override;
166 
167  /** See @ref styled_widget::update_canvas. */
168  virtual void update_canvas() override;
169 
170  /**
171  * When the slider shows the minimum value can show a special text.
172  * If this text is not empty this text is shown else the minimum value.
173  */
175 
176  /**
177  * When the slider shows the maximum value can show a special text.
178  * If this text is not empty this text is shown else the maximum value.
179  */
181 
182  /**
183  * Function to output custom value labels for the slider. When set
184  * its output is shown instead of the numeric values. It also overrides
185  * minimum_value_label_ and maximum_value_label_.
186  */
188 
189  /**
190  * When initially pressing the positioner and every time a new value is chosen through dragging,
191  * this value is updated with the mouse position at the time. This allows the widget to track
192  * how far the mouse has moved since setting the last value.
193  */
195 
196  // void update_current_item_mouse_position();
197 
198 public:
199  /** Static type getter that does not rely on the widget being constructed. */
200  static const std::string& type();
201 
202 private:
203  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
204  virtual const std::string& get_control_type() const override;
205 
206  /**
207  * Handlers for keyboard input
208  */
209  void handle_key_decrease(bool& handled);
210  void handle_key_increase(bool& handled);
211 
212  /**
213  * Signal handlers:
214  */
215  void signal_handler_sdl_key_down(const event::ui_event event, bool& handled, const SDL_Keycode key);
216 
217  // void signal_handler_left_button_down(const event::ui_event event, bool& handled);
218 
219  // In this subclass, only used to grab keyboard focus -
220  // see scrollbar_base class for more handling of this event.
221  void signal_handler_left_button_up(const event::ui_event event, bool& handled);
222 };
223 
224 // }---------- DEFINITION ---------{
225 
227 {
228  explicit slider_definition(const config& cfg);
229 
231  {
232  explicit resolution(const config& cfg);
233 
235 
236  unsigned left_offset;
237  unsigned right_offset;
238  };
239 };
240 
241 // }---------- BUILDER -----------{
242 
243 namespace implementation
244 {
246 {
247  explicit builder_slider(const config& cfg);
248 
250 
251  virtual std::unique_ptr<widget> build() const override;
252 
253 private:
257  unsigned step_size_;
258  int value_;
259 
262 
263  /* This vector should have the same number of items as the slider's values. */
264  std::vector<t_string> value_labels_;
265 };
266 
267 } // namespace implementation
268 
269 // }------------ END --------------
270 
271 } // 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:180
static const std::string & type()
Static type getter that does not rely on the widget being constructed.
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:126
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:97
int minimum_value_
The minimum value the slider holds.
Definition: slider.hpp:134
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:104
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:135
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:187
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:194
virtual unsigned get_length() const override
Inherited from scrollbar_base.
Definition: slider.hpp:138
int get_length_difference(const point &original, const point &current) const override
Inherited from scrollbar_base.
Definition: slider.hpp:159
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:174
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
void set_minimum_value_label(const t_string &minimum_value_label)
Definition: slider.hpp:92
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:106
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:264
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