lxgui
Loading...
Searching...
No Matches
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
11namespace lxgui::gui {
12
13class texture;
14class font_string;
15
52class button : public frame {
53public:
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() {
222 }
223
228 utils::observer_ptr<const font_string> get_current_font_string() const {
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
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
470protected:
471 void parse_attributes_(const layout_node& node) override;
472 void parse_all_nodes_before_children_(const layout_node& node) override;
473
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;
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.
utils::observer_ptr< const texture > get_highlight_texture() const
Returns this button's highlight texture.
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.
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.
const utils::observer_ptr< texture > & get_highlight_texture()
Returns this button's highlight texture.
void enable_button_clicks(const std::string &mouse_event)
Make this button generate OnClick events when a specific mouse event occurs over the button.
void set_highlight_text(utils::observer_ptr< font_string > fstr)
Sets this button's highlight text.
void set_normal_texture(utils::observer_ptr< texture > tex)
Sets this button's normal texture.
utils::observer_ptr< const font_string > get_disabled_text() const
Returns this button's disabled text.
static constexpr const char * class_name
utils::observer_ptr< texture > highlight_texture_
void click(const std::string &mouse_event)
Handle a mouse click over this button.
void set_pushed_texture(utils::observer_ptr< texture > tex)
Sets this button's pushed texture.
utils::observer_ptr< texture > disabled_texture_
const utils::observer_ptr< texture > & get_disabled_texture()
Returns this button's disabled texture.
void set_disabled_texture(utils::observer_ptr< texture > tex)
Sets this button's disabled texture.
state get_button_state() const
Returns this button's state.
std::string serialize(const std::string &tab) const override
Prints all relevant information about this region in a string.
void parse_attributes_(const layout_node &node) override
bool is_button_clicks_enabled_(input::mouse_button button_id) const
utils::observer_ptr< font_string > current_font_string_
std::set< std::string > reg_click_list_
virtual void disable()
Disables this button.
void set_highlight_texture(utils::observer_ptr< texture > tex)
Sets this button's highlight texture.
utils::observer_ptr< font_string > normal_text_
virtual void click_(input::mouse_button button_id, input::mouse_button_event button_event, float mx, float my)
const utils::observer_ptr< font_string > & get_normal_text()
Returns this button's normal text.
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.
bool is_enabled() const
Checks if this button is enabled.
const utils::observer_ptr< texture > & get_pushed_texture()
Returns this button's pushed texture.
utils::observer_ptr< const font_string > get_highlight_text() const
Returns this button's highlight text.
void set_enabled(bool enabled)
Enables or disables this button.
virtual void highlight()
Highlights this button.
const utils::ustring & get_text() const
Returns this button's text.
utils::observer_ptr< font_string > disabled_text_
utils::observer_ptr< const texture > get_normal_texture() const
Returns this button's normal texture.
virtual void release()
Releases this button.
virtual void enable()
Enables this button.
void lock_highlight()
Locks this button's highlighting.
void disable_button_clicks()
Stop this button from generating OnClick events.
void set_text(const utils::ustring &content)
Sets this button's text.
void copy_from(const region &obj) override
Copies a region's parameters into this button (inheritance).
const std::vector< std::string > & get_type_list_() const override
void set_pushed_text_offset(const vector2f &offset)
Sets this button's pushed text offset.
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.
utils::ustring content_
utils::observer_ptr< font_string > highlight_text_
utils::observer_ptr< texture > pushed_texture_
const utils::observer_ptr< font_string > & get_disabled_text()
Returns this button's disabled text.
utils::observer_ptr< const texture > get_pushed_texture() const
Returns this button's pushed texture.
vector2f pushed_text_offset_
void set_normal_text(utils::observer_ptr< font_string > fstr)
Sets this button's normal text.
virtual void push()
Pushed this button.
bool can_use_script(const std::string &script_name) const override
Returns 'true' if this button can use a script.
const vector2f & get_pushed_text_offset() const
Returns this button's pushed text offset.
void unlock_highlight()
Unlocks this button's highlighting.
utils::observer_ptr< const font_string > get_normal_text() const
Returns this button's normal text.
utils::observer_ptr< const font_string > get_current_font_string() const
Returns the currently displayed text object.
const utils::observer_ptr< font_string > & get_current_font_string()
Returns the currently displayed text object.
virtual void unlight()
Unlights this button.
const utils::observer_ptr< texture > & get_normal_texture()
Returns this button's normal texture.
void fire_script(const std::string &script_name, const event_data &data=event_data{}) override
Calls a script.
utils::observer_ptr< const texture > get_disabled_texture() const
Returns this button's disabled texture.
void parse_all_nodes_before_children_(const layout_node &node) override
utils::observer_ptr< texture > normal_texture_
const utils::observer_ptr< font_string > & get_highlight_text()
Returns this button's highlight text.
Stores a variable number of arguments for an event.
A region that can contain other regions and react to events.
An node in a layout file.
Manages the user interface.
Struct holding all the core information about a frame necessary for its creation.
static const vector2 zero