base_button.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*************************************************************************/
  2. /* base_button.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef BASE_BUTTON_H
  31. #define BASE_BUTTON_H
  32. #include "scene/gui/control.h"
  33. class ButtonGroup;
  34. class BaseButton : public Control {
  35. GDCLASS(BaseButton, Control);
  36. public:
  37. enum ActionMode {
  38. ACTION_MODE_BUTTON_PRESS,
  39. ACTION_MODE_BUTTON_RELEASE,
  40. };
  41. private:
  42. int button_mask;
  43. bool toggle_mode;
  44. bool shortcut_in_tooltip;
  45. bool keep_pressed_outside;
  46. FocusMode enabled_focus_mode;
  47. Ref<ShortCut> shortcut;
  48. ActionMode action_mode;
  49. struct Status {
  50. bool pressed;
  51. bool hovering;
  52. bool press_attempt;
  53. bool pressing_inside;
  54. bool disabled;
  55. } status;
  56. Ref<ButtonGroup> button_group;
  57. void _unpress_group();
  58. void _pressed();
  59. void _toggled(bool p_pressed);
  60. void on_action_event(Ref<InputEvent> p_event);
  61. protected:
  62. virtual void pressed();
  63. virtual void toggled(bool p_pressed);
  64. static void _bind_methods();
  65. virtual void _gui_input(Ref<InputEvent> p_event);
  66. virtual void _unhandled_input(Ref<InputEvent> p_event);
  67. void _notification(int p_what);
  68. public:
  69. enum DrawMode {
  70. DRAW_NORMAL,
  71. DRAW_PRESSED,
  72. DRAW_HOVER,
  73. DRAW_DISABLED,
  74. DRAW_HOVER_PRESSED,
  75. };
  76. DrawMode get_draw_mode() const;
  77. /* Signals */
  78. bool is_pressed() const; ///< return whether button is pressed (toggled in)
  79. bool is_pressing() const; ///< return whether button is pressed (toggled in)
  80. bool is_hovered() const;
  81. void set_pressed(bool p_pressed); ///only works in toggle mode
  82. void set_toggle_mode(bool p_on);
  83. bool is_toggle_mode() const;
  84. void set_shortcut_in_tooltip(bool p_on);
  85. bool is_shortcut_in_tooltip_enabled() const;
  86. void set_disabled(bool p_disabled);
  87. bool is_disabled() const;
  88. void set_action_mode(ActionMode p_mode);
  89. ActionMode get_action_mode() const;
  90. void set_keep_pressed_outside(bool p_on);
  91. bool is_keep_pressed_outside() const;
  92. void set_button_mask(int p_mask);
  93. int get_button_mask() const;
  94. void set_enabled_focus_mode(FocusMode p_mode);
  95. FocusMode get_enabled_focus_mode() const;
  96. void set_shortcut(const Ref<ShortCut> &p_shortcut);
  97. Ref<ShortCut> get_shortcut() const;
  98. virtual String get_tooltip(const Point2 &p_pos) const;
  99. void set_button_group(const Ref<ButtonGroup> &p_group);
  100. Ref<ButtonGroup> get_button_group() const;
  101. BaseButton();
  102. ~BaseButton();
  103. };
  104. VARIANT_ENUM_CAST(BaseButton::DrawMode)
  105. VARIANT_ENUM_CAST(BaseButton::ActionMode)
  106. class ButtonGroup : public Resource {
  107. GDCLASS(ButtonGroup, Resource);
  108. friend class BaseButton;
  109. Set<BaseButton *> buttons;
  110. protected:
  111. static void _bind_methods();
  112. public:
  113. BaseButton *get_pressed_button();
  114. void get_buttons(List<BaseButton *> *r_buttons);
  115. Array _get_buttons();
  116. ButtonGroup();
  117. };
  118. #endif