lxgui
gui_button.hpp
1 #ifndef LXGUI_GUI_BUTTON_HPP
2 #define LXGUI_GUI_BUTTON_HPP
3 
4 #include "lxgui/gui_frame.hpp"
5 #include "lxgui/lxgui.hpp"
6 #include "lxgui/utils.hpp"
7 #include "lxgui/utils_string.hpp"
8 
9 #include <string>
10 
11 namespace lxgui::gui {
12 
13 class texture;
14 class font_string;
15 
52 class button : public frame {
53 public:
54  using base = frame;
55 
56  enum class state { up, down, disabled };
57 
59  explicit button(utils::control_block& block, manager& mgr, const frame_core_attributes& attr);
60 
66  std::string serialize(const std::string& tab) const override;
67 
73  bool can_use_script(const std::string& script_name) const override;
74 
83  void
84  fire_script(const std::string& script_name, const event_data& data = event_data{}) override;
85 
90  void copy_from(const region& obj) override;
91 
96  void set_text(const utils::ustring& content);
97 
102  const utils::ustring& get_text() const;
103 
108  const utils::observer_ptr<texture>& get_normal_texture() {
109  return normal_texture_;
110  }
111 
116  utils::observer_ptr<const texture> get_normal_texture() const {
117  return normal_texture_;
118  }
119 
124  const utils::observer_ptr<texture>& get_pushed_texture() {
125  return pushed_texture_;
126  }
127 
132  utils::observer_ptr<const texture> get_pushed_texture() const {
133  return pushed_texture_;
134  }
135 
140  const utils::observer_ptr<texture>& get_disabled_texture() {
141  return disabled_texture_;
142  }
143 
148  utils::observer_ptr<const texture> get_disabled_texture() const {
149  return disabled_texture_;
150  }
151 
156  const utils::observer_ptr<texture>& get_highlight_texture() {
157  return highlight_texture_;
158  }
159 
164  utils::observer_ptr<const texture> get_highlight_texture() const {
165  return highlight_texture_;
166  }
167 
172  const utils::observer_ptr<font_string>& get_normal_text() {
173  return normal_text_;
174  }
175 
180  utils::observer_ptr<const font_string> get_normal_text() const {
181  return normal_text_;
182  }
183 
188  const utils::observer_ptr<font_string>& get_highlight_text() {
189  return highlight_text_;
190  }
191 
196  utils::observer_ptr<const font_string> get_highlight_text() const {
197  return highlight_text_;
198  }
199 
204  const utils::observer_ptr<font_string>& get_disabled_text() {
205  return disabled_text_;
206  }
207 
212  utils::observer_ptr<const font_string> get_disabled_text() const {
213  return disabled_text_;
214  }
215 
220  const utils::observer_ptr<font_string>& get_current_font_string() {
221  return current_font_string_;
222  }
223 
228  utils::observer_ptr<const font_string> get_current_font_string() const {
229  return current_font_string_;
230  }
231 
236  void set_normal_texture(utils::observer_ptr<texture> tex);
237 
242  void set_pushed_texture(utils::observer_ptr<texture> tex);
243 
248  void set_disabled_texture(utils::observer_ptr<texture> tex);
249 
254  void set_highlight_texture(utils::observer_ptr<texture> tex);
255 
260  void set_normal_text(utils::observer_ptr<font_string> fstr);
261 
266  void set_highlight_text(utils::observer_ptr<font_string> fstr);
267 
272  void set_disabled_text(utils::observer_ptr<font_string> fstr);
273 
278  void set_enabled(bool enabled) {
279  if (enabled) {
280  enable();
281  } else {
282  disable();
283  }
284  }
285 
290  virtual void disable();
291 
293  virtual void enable();
294 
299  bool is_enabled() const;
300 
305  virtual void push();
306 
311  virtual void release();
312 
318  void click(const std::string& mouse_event);
319 
326  void click(input::mouse_button button_id, input::mouse_button_event button_event);
327 
334  virtual void highlight();
335 
342  virtual void unlight();
343 
348  state get_button_state() const;
349 
355  void lock_highlight();
356 
358  void unlock_highlight();
359 
364  void set_pushed_text_offset(const vector2f& offset);
365 
370  const vector2f& get_pushed_text_offset() const;
371 
379  void set_button_clicks_enabled(const std::string& mouse_event, bool enable) {
380  if (enable) {
381  enable_button_clicks(mouse_event);
382  } else {
383  disable_button_clicks(mouse_event);
384  }
385  }
386 
396  input::mouse_button button_id, input::mouse_button_event button_event, bool enable) {
397  if (enable) {
398  enable_button_clicks(button_id, button_event);
399  } else {
400  disable_button_clicks(button_id, button_event);
401  }
402  }
403 
411  void enable_button_clicks(const std::string& mouse_event);
412 
420  void
422 
428  void disable_button_clicks(const std::string& mouse_event);
429 
437  void
439 
445  void disable_button_clicks();
446 
453  bool is_button_clicks_enabled(const std::string& mouse_event) const;
454 
463  input::mouse_button button_id, input::mouse_button_event button_event) const;
464 
466  static void register_on_lua(sol::state& lua);
467 
468  static constexpr const char* class_name = "Button";
469 
470 protected:
471  void parse_attributes_(const layout_node& node) override;
472  void parse_all_nodes_before_children_(const layout_node& node) override;
473 
474  bool is_button_clicks_enabled_(input::mouse_button button_id) const;
475 
476  virtual void click_(
477  input::mouse_button button_id, input::mouse_button_event button_event, float mx, float my);
478 
479  const std::vector<std::string>& get_type_list_() const override;
480 
482  bool is_highlighted_ = false;
483  bool is_highlight_locked_ = false;
484 
485  utils::ustring content_;
486 
487  utils::observer_ptr<texture> normal_texture_ = nullptr;
488  utils::observer_ptr<texture> pushed_texture_ = nullptr;
489  utils::observer_ptr<texture> disabled_texture_ = nullptr;
490  utils::observer_ptr<texture> highlight_texture_ = nullptr;
491 
492  utils::observer_ptr<font_string> normal_text_ = nullptr;
493  utils::observer_ptr<font_string> highlight_text_ = nullptr;
494  utils::observer_ptr<font_string> disabled_text_ = nullptr;
495  utils::observer_ptr<font_string> current_font_string_ = nullptr;
496 
498 
499  std::set<std::string> reg_click_list_;
500 };
501 
502 } // namespace lxgui::gui
503 
504 #endif
A frame with a button that can be clicked.
Definition: gui_button.hpp:52
utils::observer_ptr< const font_string > get_normal_text() const
Returns this button's normal text.
Definition: gui_button.hpp:180
const utils::observer_ptr< font_string > & get_normal_text()
Returns this button's normal text.
Definition: gui_button.hpp:172
void set_button_clicks_enabled(input::mouse_button button_id, input::mouse_button_event button_event, bool enable)
Make this button generate OnClick events when a specific mouse event occurs over the button.
Definition: gui_button.hpp:395
bool is_button_clicks_enabled(const std::string &mouse_event) const
Checks if this button can generate OnClick events when a specific mouse event occurs over the button.
Definition: gui_button.cpp:523
void enable_button_clicks(const std::string &mouse_event)
Make this button generate OnClick events when a specific mouse event occurs over the button.
Definition: gui_button.cpp:499
const utils::observer_ptr< texture > & get_pushed_texture()
Returns this button's pushed texture.
Definition: gui_button.hpp:124
utils::observer_ptr< const texture > get_highlight_texture() const
Returns this button's highlight texture.
Definition: gui_button.hpp:164
void set_highlight_text(utils::observer_ptr< font_string > fstr)
Sets this button's highlight text.
Definition: gui_button.cpp:267
const utils::observer_ptr< font_string > & get_current_font_string()
Returns the currently displayed text object.
Definition: gui_button.hpp:220
void set_normal_texture(utils::observer_ptr< texture > tex)
Sets this button's normal texture.
Definition: gui_button.cpp:223
static constexpr const char * class_name
Definition: gui_button.hpp:468
utils::observer_ptr< texture > highlight_texture_
Definition: gui_button.hpp:490
void click(const std::string &mouse_event)
Handle a mouse click over this button.
Definition: gui_button.cpp:398
utils::observer_ptr< const font_string > get_disabled_text() const
Returns this button's disabled text.
Definition: gui_button.hpp:212
void set_pushed_texture(utils::observer_ptr< texture > tex)
Sets this button's pushed texture.
Definition: gui_button.cpp:231
utils::observer_ptr< texture > disabled_texture_
Definition: gui_button.hpp:489
void set_disabled_texture(utils::observer_ptr< texture > tex)
Sets this button's disabled texture.
Definition: gui_button.cpp:239
state get_button_state() const
Returns this button's state.
Definition: gui_button.cpp:474
std::string serialize(const std::string &tab) const override
Prints all relevant information about this region in a string.
Definition: gui_button.cpp:22
void parse_attributes_(const layout_node &node) override
bool is_button_clicks_enabled_(input::mouse_button button_id) const
Definition: gui_button.cpp:533
utils::observer_ptr< font_string > current_font_string_
Definition: gui_button.hpp:495
std::set< std::string > reg_click_list_
Definition: gui_button.hpp:499
virtual void disable()
Disables this button.
Definition: gui_button.cpp:291
void set_highlight_texture(utils::observer_ptr< texture > tex)
Sets this button's highlight texture.
Definition: gui_button.cpp:247
utils::observer_ptr< const texture > get_disabled_texture() const
Returns this button's disabled texture.
Definition: gui_button.hpp:148
utils::observer_ptr< font_string > normal_text_
Definition: gui_button.hpp:492
virtual void click_(input::mouse_button button_id, input::mouse_button_event button_event, float mx, float my)
Definition: gui_button.cpp:412
void set_button_clicks_enabled(const std::string &mouse_event, bool enable)
Make this button generate OnClick events when a specific mouse event occurs over the button.
Definition: gui_button.hpp:379
utils::observer_ptr< const font_string > get_highlight_text() const
Returns this button's highlight text.
Definition: gui_button.hpp:196
bool is_enabled() const
Checks if this button is enabled.
Definition: gui_button.cpp:358
void set_enabled(bool enabled)
Enables or disables this button.
Definition: gui_button.hpp:278
virtual void highlight()
Highlights this button.
Definition: gui_button.cpp:431
const utils::ustring & get_text() const
Returns this button's text.
Definition: gui_button.cpp:219
utils::observer_ptr< const font_string > get_current_font_string() const
Returns the currently displayed text object.
Definition: gui_button.hpp:228
utils::observer_ptr< font_string > disabled_text_
Definition: gui_button.hpp:494
virtual void release()
Releases this button.
Definition: gui_button.cpp:380
utils::observer_ptr< const texture > get_normal_texture() const
Returns this button's normal texture.
Definition: gui_button.hpp:116
virtual void enable()
Enables this button.
Definition: gui_button.cpp:328
void lock_highlight()
Locks this button's highlighting.
Definition: gui_button.cpp:478
button(utils::control_block &block, manager &mgr, const frame_core_attributes &attr)
Constructor.
Definition: gui_button.cpp:13
void disable_button_clicks()
Stop this button from generating OnClick events.
Definition: gui_button.cpp:519
void set_text(const utils::ustring &content)
Sets this button's text.
Definition: gui_button.cpp:206
void copy_from(const region &obj) override
Copies a region's parameters into this button (inheritance).
Definition: gui_button.cpp:86
const utils::observer_ptr< font_string > & get_highlight_text()
Returns this button's highlight text.
Definition: gui_button.hpp:188
const std::vector< std::string > & get_type_list_() const override
Definition: gui_button.cpp:544
void set_pushed_text_offset(const vector2f &offset)
Sets this button's pushed text offset.
Definition: gui_button.cpp:490
static void register_on_lua(sol::state &lua)
Registers this region class to the provided Lua state.
void set_disabled_text(utils::observer_ptr< font_string > fstr)
Sets this button's disabled text.
Definition: gui_button.cpp:279
utils::ustring content_
Definition: gui_button.hpp:485
utils::observer_ptr< font_string > highlight_text_
Definition: gui_button.hpp:493
utils::observer_ptr< texture > pushed_texture_
Definition: gui_button.hpp:488
utils::observer_ptr< const texture > get_pushed_texture() const
Returns this button's pushed texture.
Definition: gui_button.hpp:132
vector2f pushed_text_offset_
Definition: gui_button.hpp:497
const utils::observer_ptr< texture > & get_highlight_texture()
Returns this button's highlight texture.
Definition: gui_button.hpp:156
const utils::observer_ptr< texture > & get_normal_texture()
Returns this button's normal texture.
Definition: gui_button.hpp:108
void set_normal_text(utils::observer_ptr< font_string > fstr)
Sets this button's normal text.
Definition: gui_button.cpp:255
virtual void push()
Pushed this button.
Definition: gui_button.cpp:362
const utils::observer_ptr< texture > & get_disabled_texture()
Returns this button's disabled texture.
Definition: gui_button.hpp:140
bool can_use_script(const std::string &script_name) const override
Returns 'true' if this button can use a script.
Definition: gui_button.cpp:26
const vector2f & get_pushed_text_offset() const
Returns this button's pushed text offset.
Definition: gui_button.cpp:495
void unlock_highlight()
Unlocks this button's highlighting.
Definition: gui_button.cpp:483
virtual void unlight()
Unlights this button.
Definition: gui_button.cpp:449
void fire_script(const std::string &script_name, const event_data &data=event_data{}) override
Calls a script.
Definition: gui_button.cpp:31
const utils::observer_ptr< font_string > & get_disabled_text()
Returns this button's disabled text.
Definition: gui_button.hpp:204
void parse_all_nodes_before_children_(const layout_node &node) override
utils::observer_ptr< texture > normal_texture_
Definition: gui_button.hpp:487
Stores a variable number of arguments for an event.
A region that can contain other regions and react to events.
Definition: gui_frame.hpp:255
An node in a layout file.
Manages the user interface.
Definition: gui_manager.hpp:44
region(utils::control_block &block, manager &mgr, const region_core_attributes &attr)
Contructor.
Definition: gui_region.cpp:21
Struct holding all the core information about a frame necessary for its creation.
static const vector2 zero