spin_box.cpp 29 KB

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