spin_box.cpp 25 KB

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