The Battle for Wesnoth  1.19.5+dev
spinner.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2023 - 2024
3  by babaissarkar(Subhraman Sarkar) <suvrax@gmail.com>
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 
19 
22 
23 #include "gui/widgets/text_box.hpp"
24 
25 
26 namespace gui2
27 {
28 
29 // ------------ WIDGET -----------{
30 
31 
32 namespace implementation
33 {
34 struct builder_spinner;
35 }
36 
37 class spinner : public container_base
38 {
40 
41 public:
42  explicit spinner(const implementation::builder_spinner& builder);
43 
44  /** See @ref container_base::set_self_active. */
45  virtual void set_self_active(const bool active) override;
46 
47  /***** ***** ***** setters / getters for members ***** ****** *****/
48 
49  /** See @ref styled_widget::get_active. */
50  virtual bool get_active() const override;
51 
52  /** See @ref styled_widget::get_state. */
53  virtual unsigned get_state() const override;
54 
55  bool can_wrap() const override;
56 
57  void set_value(const int val);
58 
59  int get_value();
60 
61  void prev()
62  {
63  // Allow negatives?
64  if (get_value() > 0) {
66  } else {
67  if (invalid_) {
68  set_value(0);
69  }
70  }
71  }
72 
73  void next()
74  {
75  int val = get_value();
76  if (!invalid_) {
77  // No max value
78  set_value(val + step_size_);
79  } else {
80  set_value(0);
81  }
82  }
83 
84 private:
85  /**
86  * Possible states of the widget.
87  *
88  * Note the order of the states must be the same as defined in settings.hpp.
89  */
90  enum state_t {
93  };
94 
95  // It's not needed for now so keep it disabled, no definition exists yet.
96  // void set_state(const state_t state);
97 
98  /**
99  * Current state of the widget.
100  *
101  * The state of the widget determines what to render and how the widget
102  * reacts to certain 'events'.
103  */
105 
106  /** The grid that holds the content. */
107  std::unique_ptr<grid> content_grid_;
108 
110 
111  /** If the entered data is invalid. */
112  bool invalid_;
113 
115 
116  void finalize_setup();
117 
118 public:
119  /** Static type getter that does not rely on the widget being constructed. */
120  static const std::string& type();
121 
122 private:
123  /***** ***** ***** inherited ****** *****/
124 
125  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
126  virtual const std::string& get_control_type() const override;
127 
128  /***** ***** ***** signal handlers ***** ****** *****/
129 
131 };
132 
133 // }---------- DEFINITION ---------{
134 
136 {
137  explicit spinner_definition(const config& cfg);
138 
140  {
141  explicit resolution(const config& cfg);
142 
144  };
145 };
146 
147 // }---------- BUILDER -----------{
148 
149 namespace implementation
150 {
151 
153 {
154  explicit builder_spinner(const config& cfg);
155 
157 
158  virtual std::unique_ptr<widget> build() const override;
159 };
160 
161 } // namespace implementation
162 
163 // }------------ END --------------
164 
165 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
A generic container base class.
void prev()
Definition: spinner.hpp:61
bool invalid_
If the entered data is invalid.
Definition: spinner.hpp:112
void next()
Definition: spinner.hpp:73
std::unique_ptr< grid > content_grid_
The grid that holds the content.
Definition: spinner.hpp:107
int get_value()
Definition: spinner.cpp:64
state_t state_
Current state of the widget.
Definition: spinner.hpp:104
void finalize_setup()
Definition: spinner.cpp:90
virtual const std::string & get_control_type() const override
Inherited from styled_widget, implemented by REGISTER_WIDGET.
void set_value(const int val)
Definition: spinner.cpp:56
void signal_handler_left_button_down(const event::ui_event event)
Definition: spinner.cpp:119
virtual bool get_active() const override
See styled_widget::get_active.
Definition: spinner.cpp:104
text_box * get_internal_text_box()
Definition: spinner.cpp:51
bool can_wrap() const override
See widget::can_wrap.
Definition: spinner.cpp:114
state_t
Possible states of the widget.
Definition: spinner.hpp:90
virtual void set_self_active(const bool active) override
See container_base::set_self_active.
Definition: spinner.cpp:99
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: spinner.cpp:109
static const std::string & type()
Static type getter that does not rely on the widget being constructed.
spinner(const implementation::builder_spinner &builder)
Definition: spinner.cpp:40
A widget that allows the user to input text in single line.
Definition: text_box.hpp:125
ui_event
The event sent to the dispatcher.
Definition: handler.hpp:115
Generic file dialog.
std::shared_ptr< builder_grid > builder_grid_ptr
Contains the implementation details for lexical_cast and shouldn't be used directly.
virtual std::unique_ptr< widget > build() const override
Definition: spinner.cpp:157
builder_spinner(const config &cfg)
Definition: spinner.cpp:152
virtual std::unique_ptr< widget > build() const=0
spinner_definition(const config &cfg)
Definition: spinner.cpp:128