spin_box.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 += last_w;
  37. return ms;
  38. }
  39. void SpinBox::_update_text(bool p_keep_line_edit) {
  40. String value = String::num(get_value(), Math::range_step_decimals(get_step()));
  41. if (is_localizing_numeral_system()) {
  42. value = TS->format_number(value);
  43. }
  44. if (!line_edit->has_focus()) {
  45. if (!prefix.is_empty()) {
  46. value = prefix + " " + value;
  47. }
  48. if (!suffix.is_empty()) {
  49. value += " " + suffix;
  50. }
  51. }
  52. if (p_keep_line_edit && value == last_updated_text && value != line_edit->get_text()) {
  53. return;
  54. }
  55. line_edit->set_text_with_selection(value);
  56. last_updated_text = value;
  57. }
  58. void SpinBox::_text_submitted(const String &p_string) {
  59. Ref<Expression> expr;
  60. expr.instantiate();
  61. // Convert commas ',' to dots '.' for French/German etc. keyboard layouts.
  62. String text = p_string.replace(",", ".");
  63. text = text.replace(";", ",");
  64. text = TS->parse_number(text);
  65. // Ignore the prefix and suffix in the expression.
  66. text = text.trim_prefix(prefix + " ").trim_suffix(" " + suffix);
  67. Error err = expr->parse(text);
  68. if (err != OK) {
  69. // If the expression failed try without converting commas to dots - they might have been for parameter separation.
  70. text = p_string;
  71. text = TS->parse_number(text);
  72. text = text.trim_prefix(prefix + " ").trim_suffix(" " + suffix);
  73. err = expr->parse(text);
  74. if (err != OK) {
  75. return;
  76. }
  77. }
  78. Variant value = expr->execute(Array(), nullptr, false, true);
  79. if (value.get_type() != Variant::NIL) {
  80. set_value(value);
  81. }
  82. _update_text();
  83. }
  84. void SpinBox::_text_changed(const String &p_string) {
  85. int cursor_pos = line_edit->get_caret_column();
  86. _text_submitted(p_string);
  87. // Line edit 'set_text' method resets the cursor position so we need to undo that.
  88. line_edit->set_caret_column(cursor_pos);
  89. }
  90. LineEdit *SpinBox::get_line_edit() {
  91. return line_edit;
  92. }
  93. void SpinBox::_line_edit_input(const Ref<InputEvent> &p_event) {
  94. }
  95. void SpinBox::_range_click_timeout() {
  96. if (!drag.enabled && Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT)) {
  97. bool up = get_local_mouse_position().y < (get_size().height / 2);
  98. double step = get_custom_arrow_step() != 0.0 ? get_custom_arrow_step() : get_step();
  99. set_value(get_value() + (up ? step : -step));
  100. if (range_click_timer->is_one_shot()) {
  101. range_click_timer->set_wait_time(0.075);
  102. range_click_timer->set_one_shot(false);
  103. range_click_timer->start();
  104. }
  105. } else {
  106. range_click_timer->stop();
  107. }
  108. }
  109. void SpinBox::_release_mouse() {
  110. if (drag.enabled) {
  111. drag.enabled = false;
  112. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_HIDDEN);
  113. warp_mouse(drag.capture_pos);
  114. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  115. }
  116. }
  117. void SpinBox::gui_input(const Ref<InputEvent> &p_event) {
  118. ERR_FAIL_COND(p_event.is_null());
  119. if (!is_editable()) {
  120. return;
  121. }
  122. Ref<InputEventMouseButton> mb = p_event;
  123. double step = get_custom_arrow_step() != 0.0 ? get_custom_arrow_step() : get_step();
  124. if (mb.is_valid() && mb->is_pressed()) {
  125. bool up = mb->get_position().y < (get_size().height / 2);
  126. switch (mb->get_button_index()) {
  127. case MouseButton::LEFT: {
  128. line_edit->grab_focus();
  129. set_value(get_value() + (up ? step : -step));
  130. range_click_timer->set_wait_time(0.6);
  131. range_click_timer->set_one_shot(true);
  132. range_click_timer->start();
  133. drag.allowed = true;
  134. drag.capture_pos = mb->get_position();
  135. } break;
  136. case MouseButton::RIGHT: {
  137. line_edit->grab_focus();
  138. set_value((up ? get_max() : get_min()));
  139. } break;
  140. case MouseButton::WHEEL_UP: {
  141. if (line_edit->has_focus()) {
  142. set_value(get_value() + step * mb->get_factor());
  143. accept_event();
  144. }
  145. } break;
  146. case MouseButton::WHEEL_DOWN: {
  147. if (line_edit->has_focus()) {
  148. set_value(get_value() - step * mb->get_factor());
  149. accept_event();
  150. }
  151. } break;
  152. default:
  153. break;
  154. }
  155. }
  156. if (mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  157. //set_default_cursor_shape(CURSOR_ARROW);
  158. range_click_timer->stop();
  159. _release_mouse();
  160. drag.allowed = false;
  161. line_edit->clear_pending_select_all_on_focus();
  162. }
  163. Ref<InputEventMouseMotion> mm = p_event;
  164. if (mm.is_valid() && (mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {
  165. if (drag.enabled) {
  166. drag.diff_y += mm->get_relative().y;
  167. double diff_y = -0.01 * Math::pow(ABS(drag.diff_y), 1.8) * SIGN(drag.diff_y);
  168. set_value(CLAMP(drag.base_val + step * diff_y, get_min(), get_max()));
  169. } else if (drag.allowed && drag.capture_pos.distance_to(mm->get_position()) > 2) {
  170. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
  171. drag.enabled = true;
  172. drag.base_val = get_value();
  173. drag.diff_y = 0;
  174. }
  175. }
  176. }
  177. void SpinBox::_line_edit_focus_enter() {
  178. int col = line_edit->get_caret_column();
  179. _update_text();
  180. line_edit->set_caret_column(col);
  181. // LineEdit text might change and it clears any selection. Have to re-select here.
  182. if (line_edit->is_select_all_on_focus() && !Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT)) {
  183. line_edit->select_all();
  184. }
  185. }
  186. void SpinBox::_line_edit_focus_exit() {
  187. // Discontinue because the focus_exit was caused by left-clicking the arrows.
  188. const Viewport *viewport = get_viewport();
  189. if (!viewport || viewport->gui_get_focus_owner() == get_line_edit()) {
  190. return;
  191. }
  192. // Discontinue because the focus_exit was caused by right-click context menu.
  193. if (line_edit->is_menu_visible()) {
  194. return;
  195. }
  196. // Discontinue because the focus_exit was caused by canceling.
  197. if (Input::get_singleton()->is_action_pressed("ui_cancel")) {
  198. _update_text();
  199. return;
  200. }
  201. _text_submitted(line_edit->get_text());
  202. }
  203. inline void SpinBox::_adjust_width_for_icon(const Ref<Texture2D> &icon) {
  204. int w = icon->get_width();
  205. if ((w != last_w)) {
  206. line_edit->set_offset(SIDE_LEFT, 0);
  207. line_edit->set_offset(SIDE_RIGHT, -w);
  208. last_w = w;
  209. }
  210. }
  211. void SpinBox::_notification(int p_what) {
  212. switch (p_what) {
  213. case NOTIFICATION_DRAW: {
  214. _update_text(true);
  215. _adjust_width_for_icon(theme_cache.updown_icon);
  216. RID ci = get_canvas_item();
  217. Size2i size = get_size();
  218. if (is_layout_rtl()) {
  219. theme_cache.updown_icon->draw(ci, Point2i(0, (size.height - theme_cache.updown_icon->get_height()) / 2));
  220. } else {
  221. theme_cache.updown_icon->draw(ci, Point2i(size.width - theme_cache.updown_icon->get_width(), (size.height - theme_cache.updown_icon->get_height()) / 2));
  222. }
  223. } break;
  224. case NOTIFICATION_ENTER_TREE: {
  225. _adjust_width_for_icon(theme_cache.updown_icon);
  226. _update_text();
  227. } break;
  228. case NOTIFICATION_VISIBILITY_CHANGED:
  229. drag.allowed = false;
  230. [[fallthrough]];
  231. case NOTIFICATION_EXIT_TREE: {
  232. _release_mouse();
  233. } break;
  234. case NOTIFICATION_TRANSLATION_CHANGED: {
  235. queue_redraw();
  236. } break;
  237. case NOTIFICATION_THEME_CHANGED: {
  238. call_deferred(SNAME("update_minimum_size"));
  239. get_line_edit()->call_deferred(SNAME("update_minimum_size"));
  240. } break;
  241. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  242. queue_redraw();
  243. } break;
  244. }
  245. }
  246. void SpinBox::set_horizontal_alignment(HorizontalAlignment p_alignment) {
  247. line_edit->set_horizontal_alignment(p_alignment);
  248. }
  249. HorizontalAlignment SpinBox::get_horizontal_alignment() const {
  250. return line_edit->get_horizontal_alignment();
  251. }
  252. void SpinBox::set_suffix(const String &p_suffix) {
  253. if (suffix == p_suffix) {
  254. return;
  255. }
  256. suffix = p_suffix;
  257. _update_text();
  258. }
  259. String SpinBox::get_suffix() const {
  260. return suffix;
  261. }
  262. void SpinBox::set_prefix(const String &p_prefix) {
  263. if (prefix == p_prefix) {
  264. return;
  265. }
  266. prefix = p_prefix;
  267. _update_text();
  268. }
  269. String SpinBox::get_prefix() const {
  270. return prefix;
  271. }
  272. void SpinBox::set_update_on_text_changed(bool p_enabled) {
  273. if (update_on_text_changed == p_enabled) {
  274. return;
  275. }
  276. update_on_text_changed = p_enabled;
  277. if (p_enabled) {
  278. line_edit->connect("text_changed", callable_mp(this, &SpinBox::_text_changed), CONNECT_DEFERRED);
  279. } else {
  280. line_edit->disconnect("text_changed", callable_mp(this, &SpinBox::_text_changed));
  281. }
  282. }
  283. bool SpinBox::get_update_on_text_changed() const {
  284. return update_on_text_changed;
  285. }
  286. void SpinBox::set_select_all_on_focus(bool p_enabled) {
  287. line_edit->set_select_all_on_focus(p_enabled);
  288. }
  289. bool SpinBox::is_select_all_on_focus() const {
  290. return line_edit->is_select_all_on_focus();
  291. }
  292. void SpinBox::set_editable(bool p_enabled) {
  293. line_edit->set_editable(p_enabled);
  294. }
  295. bool SpinBox::is_editable() const {
  296. return line_edit->is_editable();
  297. }
  298. void SpinBox::apply() {
  299. _text_submitted(line_edit->get_text());
  300. }
  301. void SpinBox::set_custom_arrow_step(double p_custom_arrow_step) {
  302. custom_arrow_step = p_custom_arrow_step;
  303. }
  304. double SpinBox::get_custom_arrow_step() const {
  305. return custom_arrow_step;
  306. }
  307. void SpinBox::_bind_methods() {
  308. ClassDB::bind_method(D_METHOD("set_horizontal_alignment", "alignment"), &SpinBox::set_horizontal_alignment);
  309. ClassDB::bind_method(D_METHOD("get_horizontal_alignment"), &SpinBox::get_horizontal_alignment);
  310. ClassDB::bind_method(D_METHOD("set_suffix", "suffix"), &SpinBox::set_suffix);
  311. ClassDB::bind_method(D_METHOD("get_suffix"), &SpinBox::get_suffix);
  312. ClassDB::bind_method(D_METHOD("set_prefix", "prefix"), &SpinBox::set_prefix);
  313. ClassDB::bind_method(D_METHOD("get_prefix"), &SpinBox::get_prefix);
  314. ClassDB::bind_method(D_METHOD("set_editable", "enabled"), &SpinBox::set_editable);
  315. ClassDB::bind_method(D_METHOD("set_custom_arrow_step", "arrow_step"), &SpinBox::set_custom_arrow_step);
  316. ClassDB::bind_method(D_METHOD("get_custom_arrow_step"), &SpinBox::get_custom_arrow_step);
  317. ClassDB::bind_method(D_METHOD("is_editable"), &SpinBox::is_editable);
  318. ClassDB::bind_method(D_METHOD("set_update_on_text_changed", "enabled"), &SpinBox::set_update_on_text_changed);
  319. ClassDB::bind_method(D_METHOD("get_update_on_text_changed"), &SpinBox::get_update_on_text_changed);
  320. ClassDB::bind_method(D_METHOD("set_select_all_on_focus", "enabled"), &SpinBox::set_select_all_on_focus);
  321. ClassDB::bind_method(D_METHOD("is_select_all_on_focus"), &SpinBox::is_select_all_on_focus);
  322. ClassDB::bind_method(D_METHOD("apply"), &SpinBox::apply);
  323. ClassDB::bind_method(D_METHOD("get_line_edit"), &SpinBox::get_line_edit);
  324. ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_horizontal_alignment", "get_horizontal_alignment");
  325. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable");
  326. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_on_text_changed"), "set_update_on_text_changed", "get_update_on_text_changed");
  327. ADD_PROPERTY(PropertyInfo(Variant::STRING, "prefix"), "set_prefix", "get_prefix");
  328. ADD_PROPERTY(PropertyInfo(Variant::STRING, "suffix"), "set_suffix", "get_suffix");
  329. 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");
  330. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "select_all_on_focus"), "set_select_all_on_focus", "is_select_all_on_focus");
  331. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, SpinBox, updown_icon, "updown");
  332. }
  333. SpinBox::SpinBox() {
  334. line_edit = memnew(LineEdit);
  335. add_child(line_edit, false, INTERNAL_MODE_FRONT);
  336. line_edit->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  337. line_edit->set_mouse_filter(MOUSE_FILTER_PASS);
  338. line_edit->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_LEFT);
  339. line_edit->connect("text_submitted", callable_mp(this, &SpinBox::_text_submitted), CONNECT_DEFERRED);
  340. line_edit->connect("focus_entered", callable_mp(this, &SpinBox::_line_edit_focus_enter), CONNECT_DEFERRED);
  341. line_edit->connect("focus_exited", callable_mp(this, &SpinBox::_line_edit_focus_exit), CONNECT_DEFERRED);
  342. line_edit->connect("gui_input", callable_mp(this, &SpinBox::_line_edit_input));
  343. range_click_timer = memnew(Timer);
  344. range_click_timer->connect("timeout", callable_mp(this, &SpinBox::_range_click_timeout));
  345. add_child(range_click_timer, false, INTERNAL_MODE_FRONT);
  346. }