spin_box.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**************************************************************************/
  2. /* spin_box.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 SPIN_BOX_H
  31. #define SPIN_BOX_H
  32. #include "scene/gui/line_edit.h"
  33. #include "scene/gui/range.h"
  34. #include "scene/main/timer.h"
  35. class SpinBox : public Range {
  36. GDCLASS(SpinBox, Range);
  37. LineEdit *line_edit = nullptr;
  38. bool update_on_text_changed = false;
  39. struct SizingCache {
  40. int buttons_block_width = 0;
  41. int buttons_width = 0;
  42. int buttons_vertical_separation = 0;
  43. int buttons_left = 0;
  44. int button_up_height = 0;
  45. int button_down_height = 0;
  46. int second_button_top = 0;
  47. int buttons_separator_top = 0;
  48. int field_and_buttons_separator_left = 0;
  49. int field_and_buttons_separator_width = 0;
  50. } sizing_cache;
  51. Timer *range_click_timer = nullptr;
  52. void _range_click_timeout();
  53. void _release_mouse_from_drag_mode();
  54. void _update_text(bool p_keep_line_edit = false);
  55. void _text_submitted(const String &p_string);
  56. void _text_changed(const String &p_string);
  57. String prefix;
  58. String suffix;
  59. String last_updated_text;
  60. double custom_arrow_step = 0.0;
  61. bool use_custom_arrow_step = false;
  62. void _line_edit_input(const Ref<InputEvent> &p_event);
  63. struct Drag {
  64. double base_val = 0.0;
  65. bool allowed = false;
  66. bool enabled = false;
  67. Vector2 capture_pos;
  68. double diff_y = 0.0;
  69. } drag;
  70. struct StateCache {
  71. bool up_button_hovered = false;
  72. bool up_button_pressed = false;
  73. bool up_button_disabled = false;
  74. bool down_button_hovered = false;
  75. bool down_button_pressed = false;
  76. bool down_button_disabled = false;
  77. } state_cache;
  78. void _line_edit_editing_toggled(bool p_toggled_on);
  79. inline void _compute_sizes();
  80. inline int _get_widest_button_icon_width();
  81. struct ThemeCache {
  82. Ref<Texture2D> updown_icon;
  83. Ref<Texture2D> up_icon;
  84. Ref<Texture2D> up_hover_icon;
  85. Ref<Texture2D> up_pressed_icon;
  86. Ref<Texture2D> up_disabled_icon;
  87. Ref<Texture2D> down_icon;
  88. Ref<Texture2D> down_hover_icon;
  89. Ref<Texture2D> down_pressed_icon;
  90. Ref<Texture2D> down_disabled_icon;
  91. Ref<StyleBox> up_base_stylebox;
  92. Ref<StyleBox> up_hover_stylebox;
  93. Ref<StyleBox> up_pressed_stylebox;
  94. Ref<StyleBox> up_disabled_stylebox;
  95. Ref<StyleBox> down_base_stylebox;
  96. Ref<StyleBox> down_hover_stylebox;
  97. Ref<StyleBox> down_pressed_stylebox;
  98. Ref<StyleBox> down_disabled_stylebox;
  99. Color up_icon_modulate;
  100. Color up_hover_icon_modulate;
  101. Color up_pressed_icon_modulate;
  102. Color up_disabled_icon_modulate;
  103. Color down_icon_modulate;
  104. Color down_hover_icon_modulate;
  105. Color down_pressed_icon_modulate;
  106. Color down_disabled_icon_modulate;
  107. Ref<StyleBox> field_and_buttons_separator;
  108. Ref<StyleBox> up_down_buttons_separator;
  109. int buttons_vertical_separation = 0;
  110. int field_and_buttons_separation = 0;
  111. int buttons_width = 0;
  112. #ifndef DISABLE_DEPRECATED
  113. bool set_min_buttons_width_from_icons = false;
  114. #endif
  115. } theme_cache;
  116. void _mouse_exited();
  117. void _update_buttons_state_for_current_value();
  118. void _set_step_no_signal(double p_step);
  119. protected:
  120. virtual void gui_input(const Ref<InputEvent> &p_event) override;
  121. void _value_changed(double p_value) override;
  122. void _validate_property(PropertyInfo &p_property) const;
  123. void _notification(int p_what);
  124. static void _bind_methods();
  125. public:
  126. LineEdit *get_line_edit();
  127. virtual Size2 get_minimum_size() const override;
  128. void set_horizontal_alignment(HorizontalAlignment p_alignment);
  129. HorizontalAlignment get_horizontal_alignment() const;
  130. void set_editable(bool p_enabled);
  131. bool is_editable() const;
  132. void set_suffix(const String &p_suffix);
  133. String get_suffix() const;
  134. void set_prefix(const String &p_prefix);
  135. String get_prefix() const;
  136. void set_update_on_text_changed(bool p_enabled);
  137. bool get_update_on_text_changed() const;
  138. void set_select_all_on_focus(bool p_enabled);
  139. bool is_select_all_on_focus() const;
  140. void apply();
  141. void set_custom_arrow_step(const double p_custom_arrow_step);
  142. double get_custom_arrow_step() const;
  143. SpinBox();
  144. };
  145. #endif // SPIN_BOX_H