base_button.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*************************************************************************/
  2. /* base_button.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. /**
  34. @author Juan Linietsky <reduzio@gmail.com>
  35. */
  36. class ButtonGroup;
  37. class BaseButton : public Control {
  38. GDCLASS(BaseButton, Control);
  39. public:
  40. enum ActionMode {
  41. ACTION_MODE_BUTTON_PRESS,
  42. ACTION_MODE_BUTTON_RELEASE,
  43. };
  44. private:
  45. int button_mask;
  46. bool toggle_mode;
  47. bool shortcut_in_tooltip;
  48. FocusMode enabled_focus_mode;
  49. Ref<ShortCut> shortcut;
  50. ActionMode action_mode;
  51. struct Status {
  52. bool pressed;
  53. bool hovering;
  54. bool press_attempt;
  55. bool pressing_inside;
  56. bool disabled;
  57. int pressing_button;
  58. } status;
  59. Ref<ButtonGroup> button_group;
  60. void _unpress_group();
  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_button_mask(int p_mask);
  91. int get_button_mask() const;
  92. void set_enabled_focus_mode(FocusMode p_mode);
  93. FocusMode get_enabled_focus_mode() const;
  94. void set_shortcut(const Ref<ShortCut> &p_shortcut);
  95. Ref<ShortCut> get_shortcut() const;
  96. virtual String get_tooltip(const Point2 &p_pos) const;
  97. void set_button_group(const Ref<ButtonGroup> &p_group);
  98. Ref<ButtonGroup> get_button_group() const;
  99. BaseButton();
  100. ~BaseButton();
  101. };
  102. VARIANT_ENUM_CAST(BaseButton::DrawMode)
  103. VARIANT_ENUM_CAST(BaseButton::ActionMode)
  104. class ButtonGroup : public Resource {
  105. GDCLASS(ButtonGroup, Resource)
  106. friend class BaseButton;
  107. Set<BaseButton *> buttons;
  108. protected:
  109. static void _bind_methods();
  110. public:
  111. BaseButton *get_pressed_button();
  112. void get_buttons(List<BaseButton *> *r_buttons);
  113. Array _get_buttons();
  114. ButtonGroup();
  115. };
  116. #endif