spin_box.cpp 30 KB

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