spin_box.cpp 11 KB

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