spin_box.h 6.1 KB

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