button.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**************************************************************************/
  2. /* button.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 BUTTON_H
  31. #define BUTTON_H
  32. #include "scene/gui/base_button.h"
  33. #include "scene/resources/text_paragraph.h"
  34. class Button : public BaseButton {
  35. GDCLASS(Button, BaseButton);
  36. private:
  37. bool flat = false;
  38. String text;
  39. String xl_text;
  40. Ref<TextParagraph> text_buf;
  41. String language;
  42. TextDirection text_direction = TEXT_DIRECTION_AUTO;
  43. TextServer::AutowrapMode autowrap_mode = TextServer::AUTOWRAP_OFF;
  44. TextServer::OverrunBehavior overrun_behavior = TextServer::OVERRUN_NO_TRIMMING;
  45. Ref<Texture2D> icon;
  46. bool expand_icon = false;
  47. bool clip_text = false;
  48. HorizontalAlignment alignment = HORIZONTAL_ALIGNMENT_CENTER;
  49. HorizontalAlignment horizontal_icon_alignment = HORIZONTAL_ALIGNMENT_LEFT;
  50. VerticalAlignment vertical_icon_alignment = VERTICAL_ALIGNMENT_CENTER;
  51. float _internal_margin[4] = {};
  52. struct ThemeCache {
  53. Ref<StyleBox> normal;
  54. Ref<StyleBox> normal_mirrored;
  55. Ref<StyleBox> pressed;
  56. Ref<StyleBox> pressed_mirrored;
  57. Ref<StyleBox> hover;
  58. Ref<StyleBox> hover_mirrored;
  59. Ref<StyleBox> hover_pressed;
  60. Ref<StyleBox> hover_pressed_mirrored;
  61. Ref<StyleBox> disabled;
  62. Ref<StyleBox> disabled_mirrored;
  63. Ref<StyleBox> focus;
  64. Size2 max_style_size;
  65. float style_margin_left = 0;
  66. float style_margin_right = 0;
  67. float style_margin_top = 0;
  68. float style_margin_bottom = 0;
  69. bool align_to_largest_stylebox = false;
  70. Color font_color;
  71. Color font_focus_color;
  72. Color font_pressed_color;
  73. Color font_hover_color;
  74. Color font_hover_pressed_color;
  75. Color font_disabled_color;
  76. Ref<Font> font;
  77. int font_size = 0;
  78. int outline_size = 0;
  79. Color font_outline_color;
  80. Color icon_normal_color;
  81. Color icon_focus_color;
  82. Color icon_pressed_color;
  83. Color icon_hover_color;
  84. Color icon_hover_pressed_color;
  85. Color icon_disabled_color;
  86. Ref<Texture2D> icon;
  87. int h_separation = 0;
  88. int icon_max_width = 0;
  89. int line_spacing = 0;
  90. } theme_cache;
  91. void _shape(Ref<TextParagraph> p_paragraph = Ref<TextParagraph>(), String p_text = "");
  92. void _texture_changed();
  93. protected:
  94. virtual void _update_theme_item_cache() override;
  95. void _set_internal_margin(Side p_side, float p_value);
  96. virtual void _queue_update_size_cache();
  97. Size2 _fit_icon_size(const Size2 &p_size) const;
  98. Ref<StyleBox> _get_current_stylebox() const;
  99. Size2 _get_largest_stylebox_size() const;
  100. void _notification(int p_what);
  101. static void _bind_methods();
  102. public:
  103. virtual Size2 get_minimum_size() const override;
  104. Size2 get_minimum_size_for_text_and_icon(const String &p_text, Ref<Texture2D> p_icon) const;
  105. void set_text(const String &p_text);
  106. String get_text() const;
  107. void set_text_overrun_behavior(TextServer::OverrunBehavior p_behavior);
  108. TextServer::OverrunBehavior get_text_overrun_behavior() const;
  109. void set_autowrap_mode(TextServer::AutowrapMode p_mode);
  110. TextServer::AutowrapMode get_autowrap_mode() const;
  111. void set_text_direction(TextDirection p_text_direction);
  112. TextDirection get_text_direction() const;
  113. void set_language(const String &p_language);
  114. String get_language() const;
  115. void set_button_icon(const Ref<Texture2D> &p_icon);
  116. Ref<Texture2D> get_button_icon() const;
  117. void set_expand_icon(bool p_enabled);
  118. bool is_expand_icon() const;
  119. void set_flat(bool p_enabled);
  120. bool is_flat() const;
  121. void set_clip_text(bool p_enabled);
  122. bool get_clip_text() const;
  123. void set_text_alignment(HorizontalAlignment p_alignment);
  124. HorizontalAlignment get_text_alignment() const;
  125. void set_icon_alignment(HorizontalAlignment p_alignment);
  126. void set_vertical_icon_alignment(VerticalAlignment p_alignment);
  127. HorizontalAlignment get_icon_alignment() const;
  128. VerticalAlignment get_vertical_icon_alignment() const;
  129. Button(const String &p_text = String());
  130. ~Button();
  131. };
  132. #endif // BUTTON_H