spin_box.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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/math/expression.h"
  32. #include "core/os/input.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 = 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_entered(const String &p_string) {
  49. Ref<Expression> expr;
  50. expr.instance();
  51. // Ignore the prefix and suffix in the expression
  52. Error err = expr->parse(p_string.trim_prefix(prefix + " ").trim_suffix(" " + suffix));
  53. if (err != OK) {
  54. return;
  55. }
  56. Variant value = expr->execute(Array(), nullptr, false);
  57. if (value.get_type() != Variant::NIL) {
  58. set_value(value);
  59. }
  60. _value_changed(0);
  61. }
  62. LineEdit *SpinBox::get_line_edit() {
  63. return line_edit;
  64. }
  65. void SpinBox::_line_edit_input(const Ref<InputEvent> &p_event) {
  66. }
  67. void SpinBox::_range_click_timeout() {
  68. if (!drag.enabled && Input::get_singleton()->is_mouse_button_pressed(BUTTON_LEFT)) {
  69. bool up = get_local_mouse_position().y < (get_size().height / 2);
  70. set_value(get_value() + (up ? get_step() : -get_step()));
  71. if (range_click_timer->is_one_shot()) {
  72. range_click_timer->set_wait_time(0.075);
  73. range_click_timer->set_one_shot(false);
  74. range_click_timer->start();
  75. }
  76. } else {
  77. range_click_timer->stop();
  78. }
  79. }
  80. void SpinBox::_release_mouse() {
  81. if (drag.enabled) {
  82. drag.enabled = false;
  83. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  84. warp_mouse(drag.capture_pos);
  85. }
  86. }
  87. void SpinBox::_gui_input(const Ref<InputEvent> &p_event) {
  88. if (!is_editable()) {
  89. return;
  90. }
  91. Ref<InputEventMouseButton> mb = p_event;
  92. if (mb.is_valid() && mb->is_pressed()) {
  93. bool up = mb->get_position().y < (get_size().height / 2);
  94. switch (mb->get_button_index()) {
  95. case BUTTON_LEFT: {
  96. line_edit->grab_focus();
  97. set_value(get_value() + (up ? get_step() : -get_step()));
  98. range_click_timer->set_wait_time(0.6);
  99. range_click_timer->set_one_shot(true);
  100. range_click_timer->start();
  101. drag.allowed = true;
  102. drag.capture_pos = mb->get_position();
  103. } break;
  104. case BUTTON_RIGHT: {
  105. line_edit->grab_focus();
  106. set_value((up ? get_max() : get_min()));
  107. } break;
  108. case BUTTON_WHEEL_UP: {
  109. if (line_edit->has_focus()) {
  110. set_value(get_value() + get_step() * mb->get_factor());
  111. accept_event();
  112. }
  113. } break;
  114. case BUTTON_WHEEL_DOWN: {
  115. if (line_edit->has_focus()) {
  116. set_value(get_value() - get_step() * mb->get_factor());
  117. accept_event();
  118. }
  119. } break;
  120. }
  121. }
  122. if (mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  123. //set_default_cursor_shape(CURSOR_ARROW);
  124. range_click_timer->stop();
  125. _release_mouse();
  126. drag.allowed = false;
  127. }
  128. Ref<InputEventMouseMotion> mm = p_event;
  129. if (mm.is_valid() && mm->get_button_mask() & BUTTON_MASK_LEFT) {
  130. if (drag.enabled) {
  131. drag.diff_y += mm->get_relative().y;
  132. float diff_y = -0.01 * Math::pow(ABS(drag.diff_y), 1.8f) * SGN(drag.diff_y);
  133. set_value(CLAMP(drag.base_val + get_step() * diff_y, get_min(), get_max()));
  134. } else if (drag.allowed && drag.capture_pos.distance_to(mm->get_position()) > 2) {
  135. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
  136. drag.enabled = true;
  137. drag.base_val = get_value();
  138. drag.diff_y = 0;
  139. }
  140. }
  141. }
  142. void SpinBox::_line_edit_focus_exit() {
  143. // discontinue because the focus_exit was caused by right-click context menu
  144. if (line_edit->get_menu()->is_visible()) {
  145. return;
  146. }
  147. _text_entered(line_edit->get_text());
  148. }
  149. inline void SpinBox::_adjust_width_for_icon(const Ref<Texture> &icon) {
  150. int w = icon->get_width();
  151. if (w != last_w) {
  152. line_edit->set_margin(MARGIN_RIGHT, -w);
  153. last_w = w;
  154. }
  155. }
  156. void SpinBox::_notification(int p_what) {
  157. if (p_what == NOTIFICATION_DRAW) {
  158. Ref<Texture> updown = get_icon("updown");
  159. _adjust_width_for_icon(updown);
  160. RID ci = get_canvas_item();
  161. Size2i size = get_size();
  162. updown->draw(ci, Point2i(size.width - updown->get_width(), (size.height - updown->get_height()) / 2));
  163. } else if (p_what == NOTIFICATION_FOCUS_EXIT) {
  164. //_value_changed(0);
  165. } else if (p_what == NOTIFICATION_ENTER_TREE) {
  166. _adjust_width_for_icon(get_icon("updown"));
  167. _value_changed(0);
  168. } else if (p_what == NOTIFICATION_EXIT_TREE) {
  169. _release_mouse();
  170. } else if (p_what == NOTIFICATION_THEME_CHANGED) {
  171. call_deferred("minimum_size_changed");
  172. get_line_edit()->call_deferred("minimum_size_changed");
  173. }
  174. }
  175. void SpinBox::set_align(LineEdit::Align p_align) {
  176. line_edit->set_align(p_align);
  177. }
  178. LineEdit::Align SpinBox::get_align() const {
  179. return line_edit->get_align();
  180. }
  181. void SpinBox::set_suffix(const String &p_suffix) {
  182. suffix = p_suffix;
  183. _value_changed(0);
  184. }
  185. String SpinBox::get_suffix() const {
  186. return suffix;
  187. }
  188. void SpinBox::set_prefix(const String &p_prefix) {
  189. prefix = p_prefix;
  190. _value_changed(0);
  191. }
  192. String SpinBox::get_prefix() const {
  193. return prefix;
  194. }
  195. void SpinBox::set_editable(bool p_editable) {
  196. line_edit->set_editable(p_editable);
  197. }
  198. bool SpinBox::is_editable() const {
  199. return line_edit->is_editable();
  200. }
  201. void SpinBox::apply() {
  202. _text_entered(line_edit->get_text());
  203. }
  204. void SpinBox::_bind_methods() {
  205. //ClassDB::bind_method(D_METHOD("_value_changed"),&SpinBox::_value_changed);
  206. ClassDB::bind_method(D_METHOD("_gui_input"), &SpinBox::_gui_input);
  207. ClassDB::bind_method(D_METHOD("_text_entered"), &SpinBox::_text_entered);
  208. ClassDB::bind_method(D_METHOD("set_align", "align"), &SpinBox::set_align);
  209. ClassDB::bind_method(D_METHOD("get_align"), &SpinBox::get_align);
  210. ClassDB::bind_method(D_METHOD("set_suffix", "suffix"), &SpinBox::set_suffix);
  211. ClassDB::bind_method(D_METHOD("get_suffix"), &SpinBox::get_suffix);
  212. ClassDB::bind_method(D_METHOD("set_prefix", "prefix"), &SpinBox::set_prefix);
  213. ClassDB::bind_method(D_METHOD("get_prefix"), &SpinBox::get_prefix);
  214. ClassDB::bind_method(D_METHOD("set_editable", "editable"), &SpinBox::set_editable);
  215. ClassDB::bind_method(D_METHOD("is_editable"), &SpinBox::is_editable);
  216. ClassDB::bind_method(D_METHOD("apply"), &SpinBox::apply);
  217. ClassDB::bind_method(D_METHOD("_line_edit_focus_exit"), &SpinBox::_line_edit_focus_exit);
  218. ClassDB::bind_method(D_METHOD("get_line_edit"), &SpinBox::get_line_edit);
  219. ClassDB::bind_method(D_METHOD("_line_edit_input"), &SpinBox::_line_edit_input);
  220. ClassDB::bind_method(D_METHOD("_range_click_timeout"), &SpinBox::_range_click_timeout);
  221. ADD_PROPERTY(PropertyInfo(Variant::INT, "align", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_align", "get_align");
  222. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable");
  223. ADD_PROPERTY(PropertyInfo(Variant::STRING, "prefix"), "set_prefix", "get_prefix");
  224. ADD_PROPERTY(PropertyInfo(Variant::STRING, "suffix"), "set_suffix", "get_suffix");
  225. }
  226. SpinBox::SpinBox() {
  227. last_w = 0;
  228. line_edit = memnew(LineEdit);
  229. add_child(line_edit);
  230. line_edit->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  231. line_edit->set_mouse_filter(MOUSE_FILTER_PASS);
  232. //connect("value_changed",this,"_value_changed");
  233. line_edit->connect("text_entered", this, "_text_entered", Vector<Variant>(), CONNECT_DEFERRED);
  234. line_edit->connect("focus_exited", this, "_line_edit_focus_exit", Vector<Variant>(), CONNECT_DEFERRED);
  235. line_edit->connect("gui_input", this, "_line_edit_input");
  236. range_click_timer = memnew(Timer);
  237. range_click_timer->connect("timeout", this, "_range_click_timeout");
  238. add_child(range_click_timer);
  239. }