spin_box.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*************************************************************************/
  2. /* spin_box.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. Size2 SpinBox::get_minimum_size() const {
  34. Size2 ms = line_edit->get_combined_minimum_size();
  35. ms.width += last_w;
  36. return ms;
  37. }
  38. void SpinBox::_value_changed(double p_value) {
  39. String value = TS->format_number(String::num(get_value(), Math::range_step_decimals(get_step())));
  40. if (!line_edit->has_focus()) {
  41. if (!prefix.is_empty()) {
  42. value = prefix + " " + value;
  43. }
  44. if (!suffix.is_empty()) {
  45. value += " " + suffix;
  46. }
  47. }
  48. line_edit->set_text(value);
  49. Range::_value_changed(p_value);
  50. }
  51. void SpinBox::_text_submitted(const String &p_string) {
  52. Ref<Expression> expr;
  53. expr.instantiate();
  54. String num = TS->parse_number(p_string);
  55. // Ignore the prefix and suffix in the expression
  56. Error err = expr->parse(num.trim_prefix(prefix + " ").trim_suffix(" " + suffix));
  57. if (err != OK) {
  58. return;
  59. }
  60. Variant value = expr->execute(Array(), nullptr, false, true);
  61. if (value.get_type() != Variant::NIL) {
  62. set_value(value);
  63. }
  64. _value_changed(0);
  65. }
  66. void SpinBox::_text_changed(const String &p_string) {
  67. int cursor_pos = line_edit->get_caret_column();
  68. _text_submitted(p_string);
  69. // Line edit 'set_text' method resets the cursor position so we need to undo that.
  70. line_edit->set_caret_column(cursor_pos);
  71. }
  72. LineEdit *SpinBox::get_line_edit() {
  73. return line_edit;
  74. }
  75. void SpinBox::_line_edit_input(const Ref<InputEvent> &p_event) {
  76. }
  77. void SpinBox::_range_click_timeout() {
  78. if (!drag.enabled && Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT)) {
  79. bool up = get_local_mouse_position().y < (get_size().height / 2);
  80. double step = get_custom_arrow_step() != 0.0 ? get_custom_arrow_step() : get_step();
  81. set_value(get_value() + (up ? step : -step));
  82. if (range_click_timer->is_one_shot()) {
  83. range_click_timer->set_wait_time(0.075);
  84. range_click_timer->set_one_shot(false);
  85. range_click_timer->start();
  86. }
  87. } else {
  88. range_click_timer->stop();
  89. }
  90. }
  91. void SpinBox::_release_mouse() {
  92. if (drag.enabled) {
  93. drag.enabled = false;
  94. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_HIDDEN);
  95. warp_mouse(drag.capture_pos);
  96. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  97. }
  98. }
  99. void SpinBox::gui_input(const Ref<InputEvent> &p_event) {
  100. ERR_FAIL_COND(p_event.is_null());
  101. if (!is_editable()) {
  102. return;
  103. }
  104. Ref<InputEventMouseButton> mb = p_event;
  105. double step = get_custom_arrow_step() != 0.0 ? get_custom_arrow_step() : get_step();
  106. if (mb.is_valid() && mb->is_pressed()) {
  107. bool up = mb->get_position().y < (get_size().height / 2);
  108. switch (mb->get_button_index()) {
  109. case MouseButton::LEFT: {
  110. line_edit->grab_focus();
  111. set_value(get_value() + (up ? step : -step));
  112. range_click_timer->set_wait_time(0.6);
  113. range_click_timer->set_one_shot(true);
  114. range_click_timer->start();
  115. drag.allowed = true;
  116. drag.capture_pos = mb->get_position();
  117. } break;
  118. case MouseButton::RIGHT: {
  119. line_edit->grab_focus();
  120. set_value((up ? get_max() : get_min()));
  121. } break;
  122. case MouseButton::WHEEL_UP: {
  123. if (line_edit->has_focus()) {
  124. set_value(get_value() + step * mb->get_factor());
  125. accept_event();
  126. }
  127. } break;
  128. case MouseButton::WHEEL_DOWN: {
  129. if (line_edit->has_focus()) {
  130. set_value(get_value() - step * mb->get_factor());
  131. accept_event();
  132. }
  133. } break;
  134. default:
  135. break;
  136. }
  137. }
  138. if (mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  139. //set_default_cursor_shape(CURSOR_ARROW);
  140. range_click_timer->stop();
  141. _release_mouse();
  142. drag.allowed = false;
  143. }
  144. Ref<InputEventMouseMotion> mm = p_event;
  145. if (mm.is_valid() && (mm->get_button_mask() & MouseButton::MASK_LEFT) != MouseButton::NONE) {
  146. if (drag.enabled) {
  147. drag.diff_y += mm->get_relative().y;
  148. double diff_y = -0.01 * Math::pow(ABS(drag.diff_y), 1.8) * SIGN(drag.diff_y);
  149. set_value(CLAMP(drag.base_val + step * diff_y, get_min(), get_max()));
  150. } else if (drag.allowed && drag.capture_pos.distance_to(mm->get_position()) > 2) {
  151. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
  152. drag.enabled = true;
  153. drag.base_val = get_value();
  154. drag.diff_y = 0;
  155. }
  156. }
  157. }
  158. void SpinBox::_line_edit_focus_enter() {
  159. int col = line_edit->get_caret_column();
  160. _value_changed(0); // Update the LineEdit's text.
  161. line_edit->set_caret_column(col);
  162. }
  163. void SpinBox::_line_edit_focus_exit() {
  164. // Discontinue because the focus_exit was caused by right-click context menu.
  165. if (line_edit->is_menu_visible()) {
  166. return;
  167. }
  168. _text_submitted(line_edit->get_text());
  169. }
  170. inline void SpinBox::_adjust_width_for_icon(const Ref<Texture2D> &icon) {
  171. int w = icon->get_width();
  172. if ((w != last_w)) {
  173. line_edit->set_offset(SIDE_LEFT, 0);
  174. line_edit->set_offset(SIDE_RIGHT, -w);
  175. last_w = w;
  176. }
  177. }
  178. void SpinBox::_notification(int p_what) {
  179. switch (p_what) {
  180. case NOTIFICATION_DRAW: {
  181. Ref<Texture2D> updown = get_theme_icon(SNAME("updown"));
  182. _adjust_width_for_icon(updown);
  183. RID ci = get_canvas_item();
  184. Size2i size = get_size();
  185. if (is_layout_rtl()) {
  186. updown->draw(ci, Point2i(0, (size.height - updown->get_height()) / 2));
  187. } else {
  188. updown->draw(ci, Point2i(size.width - updown->get_width(), (size.height - updown->get_height()) / 2));
  189. }
  190. } break;
  191. case NOTIFICATION_ENTER_TREE: {
  192. _adjust_width_for_icon(get_theme_icon(SNAME("updown")));
  193. _value_changed(0);
  194. } break;
  195. case NOTIFICATION_EXIT_TREE: {
  196. _release_mouse();
  197. } break;
  198. case NOTIFICATION_TRANSLATION_CHANGED: {
  199. _value_changed(0);
  200. queue_redraw();
  201. } break;
  202. case NOTIFICATION_THEME_CHANGED: {
  203. call_deferred(SNAME("update_minimum_size"));
  204. get_line_edit()->call_deferred(SNAME("update_minimum_size"));
  205. } break;
  206. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  207. queue_redraw();
  208. } break;
  209. }
  210. }
  211. void SpinBox::set_horizontal_alignment(HorizontalAlignment p_alignment) {
  212. line_edit->set_horizontal_alignment(p_alignment);
  213. }
  214. HorizontalAlignment SpinBox::get_horizontal_alignment() const {
  215. return line_edit->get_horizontal_alignment();
  216. }
  217. void SpinBox::set_suffix(const String &p_suffix) {
  218. if (suffix == p_suffix) {
  219. return;
  220. }
  221. suffix = p_suffix;
  222. _value_changed(0);
  223. }
  224. String SpinBox::get_suffix() const {
  225. return suffix;
  226. }
  227. void SpinBox::set_prefix(const String &p_prefix) {
  228. if (prefix == p_prefix) {
  229. return;
  230. }
  231. prefix = p_prefix;
  232. _value_changed(0);
  233. }
  234. String SpinBox::get_prefix() const {
  235. return prefix;
  236. }
  237. void SpinBox::set_update_on_text_changed(bool p_enabled) {
  238. if (update_on_text_changed == p_enabled) {
  239. return;
  240. }
  241. update_on_text_changed = p_enabled;
  242. if (p_enabled) {
  243. line_edit->connect("text_changed", callable_mp(this, &SpinBox::_text_changed), CONNECT_DEFERRED);
  244. } else {
  245. line_edit->disconnect("text_changed", callable_mp(this, &SpinBox::_text_changed));
  246. }
  247. }
  248. bool SpinBox::get_update_on_text_changed() const {
  249. return update_on_text_changed;
  250. }
  251. void SpinBox::set_editable(bool p_enabled) {
  252. line_edit->set_editable(p_enabled);
  253. }
  254. bool SpinBox::is_editable() const {
  255. return line_edit->is_editable();
  256. }
  257. void SpinBox::apply() {
  258. _text_submitted(line_edit->get_text());
  259. }
  260. void SpinBox::set_custom_arrow_step(double p_custom_arrow_step) {
  261. custom_arrow_step = p_custom_arrow_step;
  262. }
  263. double SpinBox::get_custom_arrow_step() const {
  264. return custom_arrow_step;
  265. }
  266. void SpinBox::_bind_methods() {
  267. ClassDB::bind_method(D_METHOD("set_horizontal_alignment", "alignment"), &SpinBox::set_horizontal_alignment);
  268. ClassDB::bind_method(D_METHOD("get_horizontal_alignment"), &SpinBox::get_horizontal_alignment);
  269. ClassDB::bind_method(D_METHOD("set_suffix", "suffix"), &SpinBox::set_suffix);
  270. ClassDB::bind_method(D_METHOD("get_suffix"), &SpinBox::get_suffix);
  271. ClassDB::bind_method(D_METHOD("set_prefix", "prefix"), &SpinBox::set_prefix);
  272. ClassDB::bind_method(D_METHOD("get_prefix"), &SpinBox::get_prefix);
  273. ClassDB::bind_method(D_METHOD("set_editable", "enabled"), &SpinBox::set_editable);
  274. ClassDB::bind_method(D_METHOD("set_custom_arrow_step", "arrow_step"), &SpinBox::set_custom_arrow_step);
  275. ClassDB::bind_method(D_METHOD("get_custom_arrow_step"), &SpinBox::get_custom_arrow_step);
  276. ClassDB::bind_method(D_METHOD("is_editable"), &SpinBox::is_editable);
  277. ClassDB::bind_method(D_METHOD("set_update_on_text_changed", "enabled"), &SpinBox::set_update_on_text_changed);
  278. ClassDB::bind_method(D_METHOD("get_update_on_text_changed"), &SpinBox::get_update_on_text_changed);
  279. ClassDB::bind_method(D_METHOD("apply"), &SpinBox::apply);
  280. ClassDB::bind_method(D_METHOD("get_line_edit"), &SpinBox::get_line_edit);
  281. ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_horizontal_alignment", "get_horizontal_alignment");
  282. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable");
  283. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_on_text_changed"), "set_update_on_text_changed", "get_update_on_text_changed");
  284. ADD_PROPERTY(PropertyInfo(Variant::STRING, "prefix"), "set_prefix", "get_prefix");
  285. ADD_PROPERTY(PropertyInfo(Variant::STRING, "suffix"), "set_suffix", "get_suffix");
  286. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "custom_arrow_step"), "set_custom_arrow_step", "get_custom_arrow_step");
  287. }
  288. SpinBox::SpinBox() {
  289. line_edit = memnew(LineEdit);
  290. add_child(line_edit, false, INTERNAL_MODE_FRONT);
  291. line_edit->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  292. line_edit->set_mouse_filter(MOUSE_FILTER_PASS);
  293. line_edit->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_LEFT);
  294. line_edit->connect("text_submitted", callable_mp(this, &SpinBox::_text_submitted), CONNECT_DEFERRED);
  295. line_edit->connect("focus_entered", callable_mp(this, &SpinBox::_line_edit_focus_enter), CONNECT_DEFERRED);
  296. line_edit->connect("focus_exited", callable_mp(this, &SpinBox::_line_edit_focus_exit), CONNECT_DEFERRED);
  297. line_edit->connect("gui_input", callable_mp(this, &SpinBox::_line_edit_input));
  298. range_click_timer = memnew(Timer);
  299. range_click_timer->connect("timeout", callable_mp(this, &SpinBox::_range_click_timeout));
  300. add_child(range_click_timer, false, INTERNAL_MODE_FRONT);
  301. }