Debian Codegraph @

scene/gui/spin_box.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* spin_box.cpp */
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
31 #include "spin_box.h"
32
33 #include "core/input/input.h"
34 #include "core/math/expression.h"
35 #include "core/string/translation_server.h"
36 #include "scene/theme/theme_db.h"
37
38 void SpinBoxLineEdit::_accessibility_action_inc(const Variant &p_data) {
39 SpinBox *parent_sb = Object::cast_to<SpinBox>(get_parent());
40 if (parent_sb) {
41 double step = ((parent_sb->get_step() > 0) ? parent_sb->get_step() : 1);
42 parent_sb->set_value(parent_sb->get_value() + step);
43 }
44 }
45
46 void SpinBoxLineEdit::_accessibility_action_dec(const Variant &p_data) {
47 SpinBox *parent_sb = Object::cast_to<SpinBox>(get_parent());
48 if (parent_sb) {
49 double step = ((parent_sb->get_step() > 0) ? parent_sb->get_step() : 1);
50 parent_sb->set_value(parent_sb->get_value() - step);
51 }
52 }
53
54 void SpinBoxLineEdit::_notification(int p_what) {
55 ERR_MAIN_THREAD_GUARD;
56 switch (p_what) {
57 case NOTIFICATION_ACCESSIBILITY_UPDATE: {
58 RID ae = get_accessibility_element();
59 ERR_FAIL_COND(ae.is_null());
60
61 SpinBox *parent_sb = Object::cast_to<SpinBox>(get_parent());
62 if (parent_sb) {
63 DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_SPIN_BUTTON);
64 DisplayServer::get_singleton()->accessibility_update_set_name(ae, parent_sb->get_accessibility_name());
65 DisplayServer::get_singleton()->accessibility_update_set_description(ae, parent_sb->get_accessibility_description());
66 DisplayServer::get_singleton()->accessibility_update_set_live(ae, parent_sb->get_accessibility_live());
67 DisplayServer::get_singleton()->accessibility_update_set_num_value(ae, parent_sb->get_value());
68 DisplayServer::get_singleton()->accessibility_update_set_num_range(ae, parent_sb->get_min(), parent_sb->get_max());
69 if (parent_sb->get_step() > 0) {
70 DisplayServer::get_singleton()->accessibility_update_set_num_step(ae, parent_sb->get_step());
71 } else {
72 DisplayServer::get_singleton()->accessibility_update_set_num_step(ae, 1);
73 }
74 //DisplayServer::get_singleton()->accessibility_update_set_num_jump(ae, ???);
75 DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_DECREMENT, callable_mp(this, &SpinBoxLineEdit::_accessibility_action_dec));
76 DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_INCREMENT, callable_mp(this, &SpinBoxLineEdit::_accessibility_action_inc));
77 }
78 } break;
79 }
80 }
81
82 Size2 SpinBox::get_minimum_size() const {
83 Size2 ms = line_edit->get_combined_minimum_size();
84 ms.width += sizing_cache.buttons_block_width;
85 return ms;
86 }
87
88 void SpinBox::_update_text(bool p_only_update_if_value_changed) {
89 double step = get_step();
90 String value = String::num(get_value(), Math::range_step_decimals(step));
91 if (is_localizing_numeral_system()) {
92 value = TranslationServer::get_singleton()->format_number(value, _get_locale());
93 }
94
95 if (p_only_update_if_value_changed && value == last_text_value) {
96 return;
97 }
98 last_text_value = value;
99
100 if (!line_edit->is_editing()) {
101 if (!prefix.is_empty()) {
102 value = prefix + " " + value;
103 }
104 if (!suffix.is_empty()) {
105 value += " " + suffix;
106 }
107 }
108
109 if (!accepted && update_on_text_changed && !line_edit->get_text().replace_char(',', '.').contains_char('.')) {
110 value = String::num(get_value(), 0);
111 }
112
113 line_edit->set_text_with_selection(value);
114 }
115
116 void SpinBox::_text_submitted(const String &p_string) {
117 if (p_string.is_empty()) {
118 return;
119 }
120
121 String text = p_string;
122
123 if (update_on_text_changed) {
124 // Convert commas ',' to dots '.' for French/German etc. keyboard layouts.
125 text = p_string.replace_char(',', '.');
126
127 if (!text.begins_with(".") && p_string.ends_with(".")) {
128 return;
129 }
130
131 if (text.begins_with(".")) {
132 line_edit->set_text("0.");
133 line_edit->set_caret_column(line_edit->get_text().length());
134 return;
135 }
136 }
137
138 Ref<Expression> expr;
139 expr.instantiate();
140
141 const String &lang = _get_locale();
142 text = text.replace_char(';', ',');
143 text = TranslationServer::get_singleton()->parse_number(text, lang);
144 // Ignore the prefix and suffix in the expression.
145 text = text.trim_prefix(prefix + " ").trim_suffix(" " + suffix);
146
147 Error err = expr->parse(text);
148
149 if (err != OK) {
150 // If the expression failed try without converting commas to dots - they might have been for parameter separation.
151 text = p_string;
152 text = TranslationServer::get_singleton()->parse_number(text, lang);
153 text = text.trim_prefix(prefix + " ").trim_suffix(" " + suffix);
154
155 err = expr->parse(text);
156 if (err != OK) {
157 _update_text();
158 return;
159 }
160 }
161
162 Variant value = expr->execute(Array(), nullptr, false, true);
163 if (value.get_type() != Variant::NIL) {
164 set_value(value);
165 }
166 _update_text();
167 }
168
169 void SpinBox::_text_changed(const String &p_string) {
170 accepted = false;
171 int cursor_pos = line_edit->get_caret_column();
172
173 _text_submitted(p_string);
174
175 String text = p_string.replace_char(',', '.');
176
177 // Line edit 'set_text' method resets the cursor position so we need to undo that.
178 if (update_on_text_changed && !text.begins_with(".")) {
179 line_edit->set_caret_column(cursor_pos);
180 }
181 }
182
183 LineEdit *SpinBox::get_line_edit() {
184 return line_edit;
185 }
186
187 void SpinBox::_line_edit_input(const Ref<InputEvent> &p_event) {
188 if (drag.enabled) {
189 line_edit->accept_event();
190 }
191 }
192
193 void SpinBox::_range_click_timeout() {
194 if (!drag.enabled && Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT)) {
195 Rect2 up_button_rc = Rect2(sizing_cache.buttons_left, 0, sizing_cache.buttons_width, sizing_cache.button_up_height);
196 Rect2 down_button_rc = Rect2(sizing_cache.buttons_left, sizing_cache.second_button_top, sizing_cache.buttons_width, sizing_cache.button_down_height);
197
198 Vector2 mpos = get_local_mouse_position();
199
200 bool mouse_on_up_button = up_button_rc.has_point(mpos);
201 bool mouse_on_down_button = down_button_rc.has_point(mpos);
202
203 if (mouse_on_up_button || mouse_on_down_button) {
204 _arrow_clicked(mouse_on_up_button);
205 }
206
207 if (range_click_timer->is_one_shot()) {
208 range_click_timer->set_wait_time(0.075);
209 range_click_timer->set_one_shot(false);
210 range_click_timer->start();
211 }
212
213 } else {
214 range_click_timer->stop();
215 }
216 }
217
218 void SpinBox::_release_mouse_from_drag_mode() {
219 if (drag.enabled) {
220 drag.enabled = false;
221 Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_HIDDEN);
222 warp_mouse(drag.capture_pos);
223 Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
224 }
225 }
226
227 void SpinBox::_arrow_clicked(bool p_up) {
228 double arrow_step = get_custom_arrow_step() != 0.0 ? get_custom_arrow_step() : get_step();
229 if (custom_arrow_round) {
230 // Arrow button is being pressed, snap the value to next `arrow_step`.
231 // `arrow_step` should be a multiple of `step`, otherwise it may not be able to increase/decrease the value.
232 arrow_step = Math::snapped(arrow_step, get_step());
233 double new_value = _calc_value(get_value(), arrow_step);
234 if ((p_up && new_value <= get_value()) || (!p_up && new_value >= get_value())) {
235 new_value = _calc_value(get_value() + (p_up ? arrow_step : -arrow_step), arrow_step);
236 }
237 set_value(new_value);
238 } else {
239 set_value(get_value() + (p_up ? arrow_step : -arrow_step));
240 }
241 }
242
243 void SpinBox::_mouse_exited() {
244 if (state_cache.up_button_hovered || state_cache.down_button_hovered) {
245 state_cache.up_button_hovered = false;
246 state_cache.down_button_hovered = false;
247 queue_redraw();
248 }
249 }
250
251 void SpinBox::gui_input(const Ref<InputEvent> &p_event) {
252 ERR_FAIL_COND(p_event.is_null());
253
254 if (!is_editable()) {
255 return;
256 }
257
258 Ref<InputEventMouse> me = p_event;
259 Ref<InputEventMouseButton> mb = p_event;
260 Ref<InputEventMouseMotion> mm = p_event;
261
262 double step = get_step();
263 Vector2 mpos;
264 bool mouse_on_up_button = false;
265 bool mouse_on_down_button = false;
266 if (mb.is_valid() || mm.is_valid()) {
267 Rect2 up_button_rc = Rect2(sizing_cache.buttons_left, 0, sizing_cache.buttons_width, sizing_cache.button_up_height);
268 Rect2 down_button_rc = Rect2(sizing_cache.buttons_left, sizing_cache.second_button_top, sizing_cache.buttons_width, sizing_cache.button_down_height);
269
270 mpos = me->get_position();
271
272 mouse_on_up_button = up_button_rc.has_point(mpos);
273 mouse_on_down_button = down_button_rc.has_point(mpos);
274 }
275
276 if (mb.is_valid() && mb->is_pressed()) {
277 switch (mb->get_button_index()) {
278 case MouseButton::LEFT: {
279 accepted = true;
280 line_edit->grab_focus(true);
281
282 if (mouse_on_up_button || mouse_on_down_button) {
283 _arrow_clicked(mouse_on_up_button);
284 }
285 state_cache.up_button_pressed = mouse_on_up_button;
286 state_cache.down_button_pressed = mouse_on_down_button;
287 queue_redraw();
288
289 range_click_timer->set_wait_time(0.6);
290 range_click_timer->set_one_shot(true);
291 range_click_timer->start();
292
293 drag.allowed = true;
294 drag.capture_pos = mb->get_position();
295 } break;
296 case MouseButton::RIGHT: {
297 line_edit->grab_focus(true);
298 if (mouse_on_up_button || mouse_on_down_button) {
299 set_value(mouse_on_up_button ? get_max() : get_min());
300 }
301 } break;
302 case MouseButton::WHEEL_UP: {
303 if (line_edit->is_editing()) {
304 set_value(get_value() + step * mb->get_factor());
305 accept_event();
306 }
307 } break;
308 case MouseButton::WHEEL_DOWN: {
309 if (line_edit->is_editing()) {
310 set_value(get_value() - step * mb->get_factor());
311 accept_event();
312 }
313 } break;
314 default:
315 break;
316 }
317 }
318
319 if (mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
320 if (state_cache.up_button_pressed || state_cache.down_button_pressed) {
321 state_cache.up_button_pressed = false;
322 state_cache.down_button_pressed = false;
323 queue_redraw();
324 }
325
326 //set_default_cursor_shape(CURSOR_ARROW);
327 range_click_timer->stop();
328 _release_mouse_from_drag_mode();
329 drag.allowed = false;
330 line_edit->clear_pending_select_all_on_focus();
331 }
332
333 if (mm.is_valid()) {
334 bool old_up_hovered = state_cache.up_button_hovered;
335 bool old_down_hovered = state_cache.down_button_hovered;
336
337 state_cache.up_button_hovered = mouse_on_up_button;
338 state_cache.down_button_hovered = mouse_on_down_button;
339
340 if (old_up_hovered != state_cache.up_button_hovered || old_down_hovered != state_cache.down_button_hovered) {
341 queue_redraw();
342 }
343 }
344
345 if (mm.is_valid() && (mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {
346 if (drag.enabled) {
347 drag.diff_y += mm->get_relative().y;
348 double diff_y = -0.01 * Math::pow(Math::abs(drag.diff_y), 1.8) * SIGN(drag.diff_y);
349 set_value(CLAMP(drag.base_val + step * diff_y, get_min(), get_max()));
350 } else if (drag.allowed && drag.capture_pos.distance_to(mm->get_position()) > 2) {
351 Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
352 drag.enabled = true;
353 drag.base_val = get_value();
354 drag.diff_y = 0;
355 }
356 }
357 }
358
359 void SpinBox::_line_edit_editing_toggled(bool p_toggled_on) {
360 if (p_toggled_on) {
361 int col = line_edit->get_caret_column();
362 _update_text();
363 line_edit->set_caret_column(col);
364
365 // LineEdit text might change and it clears any selection. Have to re-select here.
366 if (line_edit->is_select_all_on_focus() && !Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT)) {
367 line_edit->select_all();
368 }
369 } else {
370 accepted = true;
371
372 if (Input::get_singleton()->is_action_pressed("ui_cancel") || line_edit->get_text().is_empty()) {
373 _update_text(); // Revert text if editing was canceled.
374 } else {
375 line_edit->set_text(line_edit->get_text().trim_suffix(".").trim_suffix(","));
376 _update_text(true); // Update text in case value was changed this frame (e.g. on `focus_exited`).
377 _text_submitted(line_edit->get_text());
378 }
379 }
380 }
381
382 inline void SpinBox::_compute_sizes() {
383 int buttons_block_wanted_width = theme_cache.buttons_width + theme_cache.field_and_buttons_separation;
384 int buttons_block_icon_enforced_width = _get_widest_button_icon_width() + theme_cache.field_and_buttons_separation;
385
386 #ifndef DISABLE_DEPRECATED
387 const bool min_width_from_icons = theme_cache.set_min_buttons_width_from_icons || (theme_cache.buttons_width < 0);
388 #else
389 const bool min_width_from_icons = theme_cache.buttons_width < 0;
390 #endif
391 int w = min_width_from_icons != 0 ? MAX(buttons_block_icon_enforced_width, buttons_block_wanted_width) : buttons_block_wanted_width;
392
393 if (w != sizing_cache.buttons_block_width) {
394 line_edit->set_offset(SIDE_LEFT, 0);
395 line_edit->set_offset(SIDE_RIGHT, -w);
396 sizing_cache.buttons_block_width = w;
397 }
398
399 Size2i size = get_size();
400
401 sizing_cache.buttons_width = w - theme_cache.field_and_buttons_separation;
402 sizing_cache.buttons_vertical_separation = CLAMP(theme_cache.buttons_vertical_separation, 0, size.height);
403 sizing_cache.buttons_left = is_layout_rtl() ? 0 : size.width - sizing_cache.buttons_width;
404 sizing_cache.button_up_height = (size.height - sizing_cache.buttons_vertical_separation) / 2;
405 sizing_cache.button_down_height = size.height - sizing_cache.button_up_height - sizing_cache.buttons_vertical_separation;
406 sizing_cache.second_button_top = size.height - sizing_cache.button_down_height;
407
408 sizing_cache.buttons_separator_top = sizing_cache.button_up_height;
409 sizing_cache.field_and_buttons_separator_left = is_layout_rtl() ? sizing_cache.buttons_width : size.width - sizing_cache.buttons_block_width;
410 sizing_cache.field_and_buttons_separator_width = theme_cache.field_and_buttons_separation;
411 }
412
413 inline int SpinBox::_get_widest_button_icon_width() {
414 int max = 0;
415 #ifndef DISABLE_DEPRECATED
416 max = MAX(max, theme_cache.updown_icon->get_width());
417 #endif
418 max = MAX(max, theme_cache.up_icon->get_width());
419 max = MAX(max, theme_cache.up_hover_icon->get_width());
420 max = MAX(max, theme_cache.up_pressed_icon->get_width());
421 max = MAX(max, theme_cache.up_disabled_icon->get_width());
422 max = MAX(max, theme_cache.down_icon->get_width());
423 max = MAX(max, theme_cache.down_hover_icon->get_width());
424 max = MAX(max, theme_cache.down_pressed_icon->get_width());
425 max = MAX(max, theme_cache.down_disabled_icon->get_width());
426 return max;
427 }
428
429 void SpinBox::_notification(int p_what) {
430 switch (p_what) {
431 case NOTIFICATION_DRAW: {
432 _update_text(true);
433 _compute_sizes();
434
435 Size2i size = get_size();
436
437 Ref<StyleBox> up_stylebox = theme_cache.up_base_stylebox;
438 Ref<StyleBox> down_stylebox = theme_cache.down_base_stylebox;
439 Ref<Texture2D> up_icon = theme_cache.up_icon;
440 Ref<Texture2D> down_icon = theme_cache.down_icon;
441 Color up_icon_modulate = theme_cache.up_icon_modulate;
442 Color down_icon_modulate = theme_cache.down_icon_modulate;
443
444 bool is_fully_disabled = !is_editable();
445
446 if (state_cache.up_button_disabled || is_fully_disabled) {
447 up_stylebox = theme_cache.up_disabled_stylebox;
448 up_icon = theme_cache.up_disabled_icon;
449 up_icon_modulate = theme_cache.up_disabled_icon_modulate;
450 } else if (state_cache.up_button_pressed && !drag.enabled) {
451 up_stylebox = theme_cache.up_pressed_stylebox;
452 up_icon = theme_cache.up_pressed_icon;
453 up_icon_modulate = theme_cache.up_pressed_icon_modulate;
454 } else if (state_cache.up_button_hovered && !drag.enabled) {
455 up_stylebox = theme_cache.up_hover_stylebox;
456 up_icon = theme_cache.up_hover_icon;
457 up_icon_modulate = theme_cache.up_hover_icon_modulate;
458 }
459
460 if (state_cache.down_button_disabled || is_fully_disabled) {
461 down_stylebox = theme_cache.down_disabled_stylebox;
462 down_icon = theme_cache.down_disabled_icon;
463 down_icon_modulate = theme_cache.down_disabled_icon_modulate;
464 } else if (state_cache.down_button_pressed && !drag.enabled) {
465 down_stylebox = theme_cache.down_pressed_stylebox;
466 down_icon = theme_cache.down_pressed_icon;
467 down_icon_modulate = theme_cache.down_pressed_icon_modulate;
468 } else if (state_cache.down_button_hovered && !drag.enabled) {
469 down_stylebox = theme_cache.down_hover_stylebox;
470 down_icon = theme_cache.down_hover_icon;
471 down_icon_modulate = theme_cache.down_hover_icon_modulate;
472 }
473
474 // Compute center icon positions once we know which one is used.
475 int up_icon_left = sizing_cache.buttons_left + (sizing_cache.buttons_width - up_icon->get_width()) / 2;
476 int up_icon_top = (sizing_cache.button_up_height - up_icon->get_height()) / 2;
477 int down_icon_left = sizing_cache.buttons_left + (sizing_cache.buttons_width - down_icon->get_width()) / 2;
478 int down_icon_top = sizing_cache.second_button_top + (sizing_cache.button_down_height - down_icon->get_height()) / 2;
479
480 // Draw separators.
481 draw_style_box(theme_cache.up_down_buttons_separator, Rect2(sizing_cache.buttons_left, sizing_cache.buttons_separator_top, sizing_cache.buttons_width, sizing_cache.buttons_vertical_separation));
482 draw_style_box(theme_cache.field_and_buttons_separator, Rect2(sizing_cache.field_and_buttons_separator_left, 0, sizing_cache.field_and_buttons_separator_width, size.height));
483
484 // Draw buttons.
485 draw_style_box(up_stylebox, Rect2(sizing_cache.buttons_left, 0, sizing_cache.buttons_width, sizing_cache.button_up_height));
486 draw_style_box(down_stylebox, Rect2(sizing_cache.buttons_left, sizing_cache.second_button_top, sizing_cache.buttons_width, sizing_cache.button_down_height));
487
488 #ifndef DISABLE_DEPRECATED
489 if (theme_cache.is_updown_assigned) {
490 int updown_icon_left = sizing_cache.buttons_left + (sizing_cache.buttons_width - theme_cache.updown_icon->get_width()) / 2;
491 int updown_icon_top = (size.height - theme_cache.updown_icon->get_height()) / 2;
492
493 theme_cache.updown_icon->draw(get_canvas_item(), Point2i(updown_icon_left, updown_icon_top));
494 break; // If updown is a valid texture, skip other arrows (for compatibility).
495 }
496 #endif
497 // Draw arrows.
498 draw_texture(up_icon, Point2i(up_icon_left, up_icon_top), up_icon_modulate);
499 draw_texture(down_icon, Point2i(down_icon_left, down_icon_top), down_icon_modulate);
500
501 } break;
502
503 case NOTIFICATION_MOUSE_EXIT: {
504 _mouse_exited();
505 } break;
506
507 case NOTIFICATION_ENTER_TREE: {
508 _compute_sizes();
509 _update_text();
510 _update_buttons_state_for_current_value();
511 } break;
512
513 case NOTIFICATION_READY: {
514 connect(CoreStringName(changed), callable_mp(this, &SpinBox::_update_buttons_state_for_current_value));
515 } break;
516
517 case NOTIFICATION_VISIBILITY_CHANGED:
518 drag.allowed = false;
519 [[fallthrough]];
520 case NOTIFICATION_EXIT_TREE: {
521 _release_mouse_from_drag_mode();
522 } break;
523
524 case NOTIFICATION_TRANSLATION_CHANGED: {
525 queue_redraw();
526 } break;
527
528 case NOTIFICATION_THEME_CHANGED: {
529 #ifndef DISABLE_DEPRECATED
530 theme_cache.is_updown_assigned = !theme_cache.updown_icon->get_size().is_zero_approx();
531 #endif
532 callable_mp((Control *)this, &Control::update_minimum_size).call_deferred();
533 callable_mp((Control *)get_line_edit(), &Control::update_minimum_size).call_deferred();
534 } break;
535
536 case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
537 queue_redraw();
538 } break;
539 }
540 }
541
542 void SpinBox::set_horizontal_alignment(HorizontalAlignment p_alignment) {
543 line_edit->set_horizontal_alignment(p_alignment);
544 }
545
546 HorizontalAlignment SpinBox::get_horizontal_alignment() const {
547 return line_edit->get_horizontal_alignment();
548 }
549
550 void SpinBox::set_suffix(const String &p_suffix) {
551 if (suffix == p_suffix) {
552 return;
553 }
554
555 suffix = p_suffix;
556 _update_text();
557 }
558
559 String SpinBox::get_suffix() const {
560 return suffix;
561 }
562
563 void SpinBox::set_prefix(const String &p_prefix) {
564 if (prefix == p_prefix) {
565 return;
566 }
567
568 prefix = p_prefix;
569 _update_text();
570 }
571
572 String SpinBox::get_prefix() const {
573 return prefix;
574 }
575
576 void SpinBox::set_update_on_text_changed(bool p_enabled) {
577 if (update_on_text_changed == p_enabled) {
578 return;
579 }
580
581 update_on_text_changed = p_enabled;
582
583 if (p_enabled) {
584 line_edit->connect(SceneStringName(text_changed), callable_mp(this, &SpinBox::_text_changed), CONNECT_DEFERRED);
585 } else {
586 line_edit->disconnect(SceneStringName(text_changed), callable_mp(this, &SpinBox::_text_changed));
587 }
588 }
589
590 bool SpinBox::get_update_on_text_changed() const {
591 return update_on_text_changed;
592 }
593
594 void SpinBox::set_select_all_on_focus(bool p_enabled) {
595 line_edit->set_select_all_on_focus(p_enabled);
596 }
597
598 bool SpinBox::is_select_all_on_focus() const {
599 return line_edit->is_select_all_on_focus();
600 }
601
602 void SpinBox::set_editable(bool p_enabled) {
603 line_edit->set_editable(p_enabled);
604 queue_redraw();
605 }
606
607 bool SpinBox::is_editable() const {
608 return line_edit->is_editable();
609 }
610
611 void SpinBox::apply() {
612 _text_submitted(line_edit->get_text());
613 }
614
615 void SpinBox::set_custom_arrow_step(double p_custom_arrow_step) {
616 custom_arrow_step = p_custom_arrow_step;
617 }
618
619 double SpinBox::get_custom_arrow_step() const {
620 return custom_arrow_step;
621 }
622
623 void SpinBox::set_custom_arrow_round(bool p_round) {
624 custom_arrow_round = p_round;
625 }
626
627 bool SpinBox::is_custom_arrow_rounding() const {
628 return custom_arrow_round;
629 }
630
631 void SpinBox::_value_changed(double p_value) {
632 _update_buttons_state_for_current_value();
633 }
634
635 void SpinBox::_update_buttons_state_for_current_value() {
636 double value = get_value();
637 bool should_disable_up = value == get_max() && !is_greater_allowed();
638 bool should_disable_down = value == get_min() && !is_lesser_allowed();
639
640 if (state_cache.up_button_disabled != should_disable_up || state_cache.down_button_disabled != should_disable_down) {
641 state_cache.up_button_disabled = should_disable_up;
642 state_cache.down_button_disabled = should_disable_down;
643 queue_redraw();
644 }
645 }
646
647 void SpinBox::_validate_property(PropertyInfo &p_property) const {
648 if (p_property.name == "exp_edit") {
649 p_property.usage = PROPERTY_USAGE_NONE;
650 }
651 }
652
653 void SpinBox::_bind_methods() {
654 ClassDB::bind_method(D_METHOD("set_horizontal_alignment", "alignment"), &SpinBox::set_horizontal_alignment);
655 ClassDB::bind_method(D_METHOD("get_horizontal_alignment"), &SpinBox::get_horizontal_alignment);
656 ClassDB::bind_method(D_METHOD("set_suffix", "suffix"), &SpinBox::set_suffix);
657 ClassDB::bind_method(D_METHOD("get_suffix"), &SpinBox::get_suffix);
658 ClassDB::bind_method(D_METHOD("set_prefix", "prefix"), &SpinBox::set_prefix);
659 ClassDB::bind_method(D_METHOD("get_prefix"), &SpinBox::get_prefix);
660 ClassDB::bind_method(D_METHOD("set_editable", "enabled"), &SpinBox::set_editable);
661 ClassDB::bind_method(D_METHOD("set_custom_arrow_step", "arrow_step"), &SpinBox::set_custom_arrow_step);
662 ClassDB::bind_method(D_METHOD("get_custom_arrow_step"), &SpinBox::get_custom_arrow_step);
663 ClassDB::bind_method(D_METHOD("set_custom_arrow_round", "round"), &SpinBox::set_custom_arrow_round);
664 ClassDB::bind_method(D_METHOD("is_custom_arrow_rounding"), &SpinBox::is_custom_arrow_rounding);
665 ClassDB::bind_method(D_METHOD("is_editable"), &SpinBox::is_editable);
666 ClassDB::bind_method(D_METHOD("set_update_on_text_changed", "enabled"), &SpinBox::set_update_on_text_changed);
667 ClassDB::bind_method(D_METHOD("get_update_on_text_changed"), &SpinBox::get_update_on_text_changed);
668 ClassDB::bind_method(D_METHOD("set_select_all_on_focus", "enabled"), &SpinBox::set_select_all_on_focus);
669 ClassDB::bind_method(D_METHOD("is_select_all_on_focus"), &SpinBox::is_select_all_on_focus);
670 ClassDB::bind_method(D_METHOD("apply"), &SpinBox::apply);
671 ClassDB::bind_method(D_METHOD("get_line_edit"), &SpinBox::get_line_edit);
672
673 ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_horizontal_alignment", "get_horizontal_alignment");
674 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable");
675 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_on_text_changed"), "set_update_on_text_changed", "get_update_on_text_changed");
676 ADD_PROPERTY(PropertyInfo(Variant::STRING, "prefix"), "set_prefix", "get_prefix");
677 ADD_PROPERTY(PropertyInfo(Variant::STRING, "suffix"), "set_suffix", "get_suffix");
678 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "custom_arrow_step", PROPERTY_HINT_RANGE, "0,10000,0.0001,or_greater"), "set_custom_arrow_step", "get_custom_arrow_step");
679 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "custom_arrow_round"), "set_custom_arrow_round", "is_custom_arrow_rounding");
680 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "select_all_on_focus"), "set_select_all_on_focus", "is_select_all_on_focus");
681
682 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, SpinBox, buttons_vertical_separation);
683 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, SpinBox, field_and_buttons_separation);
684 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, SpinBox, buttons_width);
685 #ifndef DISABLE_DEPRECATED
686 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, SpinBox, set_min_buttons_width_from_icons);
687
688 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, SpinBox, updown_icon, "updown");
689 #endif
690 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, SpinBox, up_icon, "up");
691 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, SpinBox, up_hover_icon, "up_hover");
692 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, SpinBox, up_pressed_icon, "up_pressed");
693 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, SpinBox, up_disabled_icon, "up_disabled");
694 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, SpinBox, down_icon, "down");
695 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, SpinBox, down_hover_icon, "down_hover");
696 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, SpinBox, down_pressed_icon, "down_pressed");
697 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, SpinBox, down_disabled_icon, "down_disabled");
698
699 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, SpinBox, up_base_stylebox, "up_background");
700 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, SpinBox, up_hover_stylebox, "up_background_hovered");
701 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, SpinBox, up_pressed_stylebox, "up_background_pressed");
702 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, SpinBox, up_disabled_stylebox, "up_background_disabled");
703 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, SpinBox, down_base_stylebox, "down_background");
704 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, SpinBox, down_hover_stylebox, "down_background_hovered");
705 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, SpinBox, down_pressed_stylebox, "down_background_pressed");
706 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, SpinBox, down_disabled_stylebox, "down_background_disabled");
707
708 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, SpinBox, up_icon_modulate, "up_icon_modulate");
709 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, SpinBox, up_hover_icon_modulate, "up_hover_icon_modulate");
710 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, SpinBox, up_pressed_icon_modulate, "up_pressed_icon_modulate");
711 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, SpinBox, up_disabled_icon_modulate, "up_disabled_icon_modulate");
712 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, SpinBox, down_icon_modulate, "down_icon_modulate");
713 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, SpinBox, down_hover_icon_modulate, "down_hover_icon_modulate");
714 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, SpinBox, down_pressed_icon_modulate, "down_pressed_icon_modulate");
715 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, SpinBox, down_disabled_icon_modulate, "down_disabled_icon_modulate");
716
717 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, SpinBox, field_and_buttons_separator, "field_and_buttons_separator");
718 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, SpinBox, up_down_buttons_separator, "up_down_buttons_separator");
719
720 ADD_CLASS_DEPENDENCY("LineEdit");
721 }
722
723 SpinBox::SpinBox() {
724 line_edit = memnew(SpinBoxLineEdit);
725 line_edit->set_emoji_menu_enabled(false);
726 add_child(line_edit, false, INTERNAL_MODE_FRONT);
727
728 line_edit->set_theme_type_variation("SpinBoxInnerLineEdit");
729 line_edit->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
730 line_edit->set_mouse_filter(MOUSE_FILTER_PASS);
731 line_edit->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_LEFT);
732
733 line_edit->connect(SceneStringName(text_submitted), callable_mp(this, &SpinBox::_text_submitted), CONNECT_DEFERRED);
734 line_edit->connect("editing_toggled", callable_mp(this, &SpinBox::_line_edit_editing_toggled), CONNECT_DEFERRED);
735 line_edit->connect(SceneStringName(gui_input), callable_mp(this, &SpinBox::_line_edit_input));
736
737 range_click_timer = memnew(Timer);
738 range_click_timer->connect("timeout", callable_mp(this, &SpinBox::_range_click_timeout));
739 add_child(range_click_timer, false, INTERNAL_MODE_FRONT);
740 }