spin_box.cpp 28 KB

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