spin_box.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*************************************************************************/
  2. /* spin_box.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "os/input.h"
  32. Size2 SpinBox::get_minimum_size() const {
  33. Size2 ms = line_edit->get_combined_minimum_size();
  34. ms.width += last_w;
  35. return ms;
  36. }
  37. void SpinBox::_value_changed(double) {
  38. String value = String::num(get_value(), Math::step_decimals(get_step()));
  39. if (prefix != "")
  40. value = prefix + " " + value;
  41. if (suffix != "")
  42. value += " " + suffix;
  43. line_edit->set_text(value);
  44. }
  45. void SpinBox::_text_entered(const String &p_string) {
  46. /*
  47. if (!p_string.is_numeric())
  48. return;
  49. */
  50. String value = p_string;
  51. if (prefix != "" && p_string.begins_with(prefix))
  52. value = p_string.substr(prefix.length(), p_string.length() - prefix.length());
  53. set_value(value.to_double());
  54. _value_changed(0);
  55. }
  56. LineEdit *SpinBox::get_line_edit() {
  57. return line_edit;
  58. }
  59. void SpinBox::_line_edit_input(const Ref<InputEvent> &p_event) {
  60. }
  61. void SpinBox::_range_click_timeout() {
  62. if (!drag.enabled && Input::get_singleton()->is_mouse_button_pressed(BUTTON_LEFT)) {
  63. bool up = get_local_mouse_position().y < (get_size().height / 2);
  64. set_value(get_value() + (up ? get_step() : -get_step()));
  65. if (range_click_timer->is_one_shot()) {
  66. range_click_timer->set_wait_time(0.075);
  67. range_click_timer->set_one_shot(false);
  68. range_click_timer->start();
  69. }
  70. } else {
  71. range_click_timer->stop();
  72. }
  73. }
  74. void SpinBox::_gui_input(const Ref<InputEvent> &p_event) {
  75. if (!is_editable()) {
  76. return;
  77. }
  78. Ref<InputEventMouseButton> mb = p_event;
  79. if (mb.is_valid() && mb->is_pressed()) {
  80. bool up = mb->get_position().y < (get_size().height / 2);
  81. switch (mb->get_button_index()) {
  82. case BUTTON_LEFT: {
  83. set_value(get_value() + (up ? get_step() : -get_step()));
  84. range_click_timer->set_wait_time(0.6);
  85. range_click_timer->set_one_shot(true);
  86. range_click_timer->start();
  87. line_edit->grab_focus();
  88. } break;
  89. case BUTTON_RIGHT: {
  90. set_value((up ? get_max() : get_min()));
  91. line_edit->grab_focus();
  92. } break;
  93. case BUTTON_WHEEL_UP: {
  94. if (line_edit->has_focus()) {
  95. set_value(get_value() + get_step() * mb->get_factor());
  96. accept_event();
  97. }
  98. } break;
  99. case BUTTON_WHEEL_DOWN: {
  100. if (line_edit->has_focus()) {
  101. set_value(get_value() - get_step() * mb->get_factor());
  102. accept_event();
  103. }
  104. } break;
  105. }
  106. }
  107. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == 1) {
  108. //set_default_cursor_shape(CURSOR_VSIZE);
  109. Vector2 cpos = Vector2(mb->get_position().x, mb->get_position().y);
  110. drag.mouse_pos = cpos;
  111. }
  112. if (mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == 1) {
  113. //set_default_cursor_shape(CURSOR_ARROW);
  114. range_click_timer->stop();
  115. if (drag.enabled) {
  116. drag.enabled = false;
  117. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  118. warp_mouse(drag.capture_pos);
  119. }
  120. }
  121. Ref<InputEventMouseMotion> mm = p_event;
  122. if (mm.is_valid() && mm->get_button_mask() & 1) {
  123. Vector2 cpos = mm->get_position();
  124. if (drag.enabled) {
  125. float diff_y = drag.mouse_pos.y - cpos.y;
  126. diff_y = Math::pow(ABS(diff_y), 1.8f) * SGN(diff_y);
  127. diff_y *= 0.1;
  128. drag.mouse_pos = cpos;
  129. drag.base_val = CLAMP(drag.base_val + get_step() * diff_y, get_min(), get_max());
  130. set_value(drag.base_val);
  131. } else if (drag.mouse_pos.distance_to(cpos) > 2) {
  132. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
  133. drag.enabled = true;
  134. drag.base_val = get_value();
  135. drag.mouse_pos = cpos;
  136. drag.capture_pos = cpos;
  137. }
  138. }
  139. }
  140. void SpinBox::_line_edit_focus_exit() {
  141. _text_entered(line_edit->get_text());
  142. }
  143. void SpinBox::_notification(int p_what) {
  144. if (p_what == NOTIFICATION_DRAW) {
  145. Ref<Texture> updown = get_icon("updown");
  146. int w = updown->get_width();
  147. if (w != last_w) {
  148. line_edit->set_margin(MARGIN_RIGHT, -w);
  149. last_w = w;
  150. }
  151. RID ci = get_canvas_item();
  152. Size2i size = get_size();
  153. updown->draw(ci, Point2i(size.width - updown->get_width(), (size.height - updown->get_height()) / 2));
  154. } else if (p_what == NOTIFICATION_FOCUS_EXIT) {
  155. //_value_changed(0);
  156. } else if (p_what == NOTIFICATION_ENTER_TREE) {
  157. _value_changed(0);
  158. }
  159. }
  160. void SpinBox::set_suffix(const String &p_suffix) {
  161. suffix = p_suffix;
  162. _value_changed(0);
  163. }
  164. String SpinBox::get_suffix() const {
  165. return suffix;
  166. }
  167. void SpinBox::set_prefix(const String &p_prefix) {
  168. prefix = p_prefix;
  169. _value_changed(0);
  170. }
  171. String SpinBox::get_prefix() const {
  172. return prefix;
  173. }
  174. void SpinBox::set_editable(bool p_editable) {
  175. line_edit->set_editable(p_editable);
  176. }
  177. bool SpinBox::is_editable() const {
  178. return line_edit->is_editable();
  179. }
  180. void SpinBox::_bind_methods() {
  181. //ClassDB::bind_method(D_METHOD("_value_changed"),&SpinBox::_value_changed);
  182. ClassDB::bind_method(D_METHOD("_gui_input"), &SpinBox::_gui_input);
  183. ClassDB::bind_method(D_METHOD("_text_entered"), &SpinBox::_text_entered);
  184. ClassDB::bind_method(D_METHOD("set_suffix", "suffix"), &SpinBox::set_suffix);
  185. ClassDB::bind_method(D_METHOD("get_suffix"), &SpinBox::get_suffix);
  186. ClassDB::bind_method(D_METHOD("set_prefix", "prefix"), &SpinBox::set_prefix);
  187. ClassDB::bind_method(D_METHOD("get_prefix"), &SpinBox::get_prefix);
  188. ClassDB::bind_method(D_METHOD("set_editable", "editable"), &SpinBox::set_editable);
  189. ClassDB::bind_method(D_METHOD("is_editable"), &SpinBox::is_editable);
  190. ClassDB::bind_method(D_METHOD("_line_edit_focus_exit"), &SpinBox::_line_edit_focus_exit);
  191. ClassDB::bind_method(D_METHOD("get_line_edit"), &SpinBox::get_line_edit);
  192. ClassDB::bind_method(D_METHOD("_line_edit_input"), &SpinBox::_line_edit_input);
  193. ClassDB::bind_method(D_METHOD("_range_click_timeout"), &SpinBox::_range_click_timeout);
  194. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable");
  195. ADD_PROPERTY(PropertyInfo(Variant::STRING, "prefix"), "set_prefix", "get_prefix");
  196. ADD_PROPERTY(PropertyInfo(Variant::STRING, "suffix"), "set_suffix", "get_suffix");
  197. }
  198. SpinBox::SpinBox() {
  199. last_w = 0;
  200. line_edit = memnew(LineEdit);
  201. add_child(line_edit);
  202. line_edit->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  203. //connect("value_changed",this,"_value_changed");
  204. line_edit->connect("text_entered", this, "_text_entered", Vector<Variant>(), CONNECT_DEFERRED);
  205. line_edit->connect("focus_exited", this, "_line_edit_focus_exit", Vector<Variant>(), CONNECT_DEFERRED);
  206. line_edit->connect("gui_input", this, "_line_edit_input");
  207. drag.enabled = false;
  208. range_click_timer = memnew(Timer);
  209. range_click_timer->connect("timeout", this, "_range_click_timeout");
  210. add_child(range_click_timer);
  211. }