spin_box.cpp 11 KB

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