| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "scene/gui/line_edit.h" |
| 34 |
#include "scene/gui/range.h" |
| 35 |
#include "scene/main/timer.h" |
| 36 |
|
| 37 |
class SpinBoxLineEdit : public LineEdit { |
| 38 |
GDCLASS(SpinBoxLineEdit, LineEdit); |
| 39 |
|
| 40 |
protected: |
| 41 |
void _notification(int p_what); |
| 42 |
|
| 43 |
void _accessibility_action_inc(const Variant &p_data); |
| 44 |
void _accessibility_action_dec(const Variant &p_data); |
| 45 |
}; |
| 46 |
|
| 47 |
class SpinBox : public Range { |
| 48 |
GDCLASS(SpinBox, Range); |
| 49 |
|
| 50 |
SpinBoxLineEdit *line_edit = nullptr; |
| 51 |
bool update_on_text_changed = false; |
| 52 |
bool accepted = true; |
| 53 |
|
| 54 |
struct SizingCache { |
| 55 |
int buttons_block_width = 0; |
| 56 |
int buttons_width = 0; |
| 57 |
int buttons_vertical_separation = 0; |
| 58 |
int buttons_left = 0; |
| 59 |
int button_up_height = 0; |
| 60 |
int button_down_height = 0; |
| 61 |
int second_button_top = 0; |
| 62 |
int buttons_separator_top = 0; |
| 63 |
int field_and_buttons_separator_left = 0; |
| 64 |
int field_and_buttons_separator_width = 0; |
| 65 |
} sizing_cache; |
| 66 |
|
| 67 |
Timer *range_click_timer = nullptr; |
| 68 |
void _range_click_timeout(); |
| 69 |
void _release_mouse_from_drag_mode(); |
| 70 |
void _arrow_clicked(bool p_up); |
| 71 |
|
| 72 |
void _update_text(bool p_only_update_if_value_changed = false); |
| 73 |
void _text_submitted(const String &p_string); |
| 74 |
void _text_changed(const String &p_string); |
| 75 |
|
| 76 |
String prefix; |
| 77 |
String suffix; |
| 78 |
String last_text_value; |
| 79 |
double custom_arrow_step = 0.0; |
| 80 |
bool custom_arrow_round = false; |
| 81 |
|
| 82 |
void _line_edit_input(const Ref<InputEvent> &p_event); |
| 83 |
|
| 84 |
struct Drag { |
| 85 |
double base_val = 0.0; |
| 86 |
bool allowed = false; |
| 87 |
bool enabled = false; |
| 88 |
Vector2 capture_pos; |
| 89 |
double diff_y = 0.0; |
| 90 |
} drag; |
| 91 |
|
| 92 |
struct StateCache { |
| 93 |
bool up_button_hovered = false; |
| 94 |
bool up_button_pressed = false; |
| 95 |
bool up_button_disabled = false; |
| 96 |
bool down_button_hovered = false; |
| 97 |
bool down_button_pressed = false; |
| 98 |
bool down_button_disabled = false; |
| 99 |
} state_cache; |
| 100 |
|
| 101 |
void _line_edit_editing_toggled(bool p_toggled_on); |
| 102 |
|
| 103 |
inline void _compute_sizes(); |
| 104 |
inline int _get_widest_button_icon_width(); |
| 105 |
|
| 106 |
struct ThemeCache { |
| 107 |
Ref<Texture2D> up_icon; |
| 108 |
Ref<Texture2D> up_hover_icon; |
| 109 |
Ref<Texture2D> up_pressed_icon; |
| 110 |
Ref<Texture2D> up_disabled_icon; |
| 111 |
Ref<Texture2D> down_icon; |
| 112 |
Ref<Texture2D> down_hover_icon; |
| 113 |
Ref<Texture2D> down_pressed_icon; |
| 114 |
Ref<Texture2D> down_disabled_icon; |
| 115 |
|
| 116 |
Ref<StyleBox> up_base_stylebox; |
| 117 |
Ref<StyleBox> up_hover_stylebox; |
| 118 |
Ref<StyleBox> up_pressed_stylebox; |
| 119 |
Ref<StyleBox> up_disabled_stylebox; |
| 120 |
Ref<StyleBox> down_base_stylebox; |
| 121 |
Ref<StyleBox> down_hover_stylebox; |
| 122 |
Ref<StyleBox> down_pressed_stylebox; |
| 123 |
Ref<StyleBox> down_disabled_stylebox; |
| 124 |
|
| 125 |
Color up_icon_modulate; |
| 126 |
Color up_hover_icon_modulate; |
| 127 |
Color up_pressed_icon_modulate; |
| 128 |
Color up_disabled_icon_modulate; |
| 129 |
Color down_icon_modulate; |
| 130 |
Color down_hover_icon_modulate; |
| 131 |
Color down_pressed_icon_modulate; |
| 132 |
Color down_disabled_icon_modulate; |
| 133 |
|
| 134 |
Ref<StyleBox> field_and_buttons_separator; |
| 135 |
Ref<StyleBox> up_down_buttons_separator; |
| 136 |
|
| 137 |
int buttons_vertical_separation = 0; |
| 138 |
int field_and_buttons_separation = 0; |
| 139 |
int buttons_width = 0; |
| 140 |
#ifndef DISABLE_DEPRECATED |
| 141 |
Ref<Texture2D> updown_icon; |
| 142 |
bool is_updown_assigned = false; |
| 143 |
bool set_min_buttons_width_from_icons = false; |
| 144 |
#endif |
| 145 |
} theme_cache; |
| 146 |
|
| 147 |
void _mouse_exited(); |
| 148 |
void _update_buttons_state_for_current_value(); |
| 149 |
|
| 150 |
protected: |
| 151 |
virtual void gui_input(const Ref<InputEvent> &p_event) override; |
| 152 |
void _value_changed(double p_value) override; |
| 153 |
void _validate_property(PropertyInfo &p_property) const; |
| 154 |
|
| 155 |
void _notification(int p_what); |
| 156 |
static void _bind_methods(); |
| 157 |
|
| 158 |
public: |
| 159 |
LineEdit *get_line_edit(); |
| 160 |
|
| 161 |
virtual Size2 get_minimum_size() const override; |
| 162 |
|
| 163 |
void set_horizontal_alignment(HorizontalAlignment p_alignment); |
| 164 |
HorizontalAlignment get_horizontal_alignment() const; |
| 165 |
|
| 166 |
void set_editable(bool p_enabled); |
| 167 |
bool is_editable() const; |
| 168 |
|
| 169 |
void set_suffix(const String &p_suffix); |
| 170 |
String get_suffix() const; |
| 171 |
|
| 172 |
void set_prefix(const String &p_prefix); |
| 173 |
String get_prefix() const; |
| 174 |
|
| 175 |
void set_update_on_text_changed(bool p_enabled); |
| 176 |
bool get_update_on_text_changed() const; |
| 177 |
|
| 178 |
void set_select_all_on_focus(bool p_enabled); |
| 179 |
bool is_select_all_on_focus() const; |
| 180 |
|
| 181 |
void apply(); |
| 182 |
void set_custom_arrow_step(const double p_custom_arrow_step); |
| 183 |
double get_custom_arrow_step() const; |
| 184 |
|
| 185 |
void set_custom_arrow_round(bool p_round); |
| 186 |
bool is_custom_arrow_rounding() const; |
| 187 |
|
| 188 |
SpinBox(); |
| 189 |
}; |