spin_box.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*************************************************************************/
  2. /* spin_box.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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/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. drag.allowed = true;
  89. drag.capture_pos = mb->get_position();
  90. } break;
  91. case BUTTON_RIGHT: {
  92. set_value((up ? get_max() : get_min()));
  93. line_edit->grab_focus();
  94. } break;
  95. case BUTTON_WHEEL_UP: {
  96. if (line_edit->has_focus()) {
  97. set_value(get_value() + get_step() * mb->get_factor());
  98. accept_event();
  99. }
  100. } break;
  101. case BUTTON_WHEEL_DOWN: {
  102. if (line_edit->has_focus()) {
  103. set_value(get_value() - get_step() * mb->get_factor());
  104. accept_event();
  105. }
  106. } break;
  107. }
  108. }
  109. if (mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  110. //set_default_cursor_shape(CURSOR_ARROW);
  111. range_click_timer->stop();
  112. if (drag.enabled) {
  113. drag.enabled = false;
  114. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  115. warp_mouse(drag.capture_pos);
  116. }
  117. drag.allowed = false;
  118. }
  119. Ref<InputEventMouseMotion> mm = p_event;
  120. if (mm.is_valid() && mm->get_button_mask() & BUTTON_MASK_LEFT) {
  121. if (drag.enabled) {
  122. drag.diff_y += mm->get_relative().y;
  123. float diff_y = -0.01 * Math::pow(ABS(drag.diff_y), 1.8f) * SGN(drag.diff_y);
  124. set_value(CLAMP(drag.base_val + get_step() * diff_y, get_min(), get_max()));
  125. } else if (drag.allowed && drag.capture_pos.distance_to(mm->get_position()) > 2) {
  126. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
  127. drag.enabled = true;
  128. drag.base_val = get_value();
  129. drag.diff_y = 0;
  130. }
  131. }
  132. }
  133. void SpinBox::_line_edit_focus_exit() {
  134. _text_entered(line_edit->get_text());
  135. }
  136. inline void SpinBox::_adjust_width_for_icon(const Ref<Texture> icon) {
  137. int w = icon->get_width();
  138. if (w != last_w) {
  139. line_edit->set_margin(MARGIN_RIGHT, -w);
  140. last_w = w;
  141. }
  142. }
  143. void SpinBox::_notification(int p_what) {
  144. if (p_what == NOTIFICATION_DRAW) {
  145. Ref<Texture> updown = get_icon("updown");
  146. _adjust_width_for_icon(updown);
  147. RID ci = get_canvas_item();
  148. Size2i size = get_size();
  149. updown->draw(ci, Point2i(size.width - updown->get_width(), (size.height - updown->get_height()) / 2));
  150. } else if (p_what == NOTIFICATION_FOCUS_EXIT) {
  151. //_value_changed(0);
  152. } else if (p_what == NOTIFICATION_ENTER_TREE) {
  153. _adjust_width_for_icon(get_icon("updown"));
  154. _value_changed(0);
  155. }
  156. }
  157. void SpinBox::set_align(LineEdit::Align p_align) {
  158. line_edit->set_align(p_align);
  159. }
  160. LineEdit::Align SpinBox::get_align() const {
  161. return line_edit->get_align();
  162. }
  163. void SpinBox::set_suffix(const String &p_suffix) {
  164. suffix = p_suffix;
  165. _value_changed(0);
  166. }
  167. String SpinBox::get_suffix() const {
  168. return suffix;
  169. }
  170. void SpinBox::set_prefix(const String &p_prefix) {
  171. prefix = p_prefix;
  172. _value_changed(0);
  173. }
  174. String SpinBox::get_prefix() const {
  175. return prefix;
  176. }
  177. void SpinBox::set_editable(bool p_editable) {
  178. line_edit->set_editable(p_editable);
  179. }
  180. bool SpinBox::is_editable() const {
  181. return line_edit->is_editable();
  182. }
  183. void SpinBox::_bind_methods() {
  184. //ClassDB::bind_method(D_METHOD("_value_changed"),&SpinBox::_value_changed);
  185. ClassDB::bind_method(D_METHOD("_gui_input"), &SpinBox::_gui_input);
  186. ClassDB::bind_method(D_METHOD("_text_entered"), &SpinBox::_text_entered);
  187. ClassDB::bind_method(D_METHOD("set_align", "align"), &SpinBox::set_align);
  188. ClassDB::bind_method(D_METHOD("get_align"), &SpinBox::get_align);
  189. ClassDB::bind_method(D_METHOD("set_suffix", "suffix"), &SpinBox::set_suffix);
  190. ClassDB::bind_method(D_METHOD("get_suffix"), &SpinBox::get_suffix);
  191. ClassDB::bind_method(D_METHOD("set_prefix", "prefix"), &SpinBox::set_prefix);
  192. ClassDB::bind_method(D_METHOD("get_prefix"), &SpinBox::get_prefix);
  193. ClassDB::bind_method(D_METHOD("set_editable", "editable"), &SpinBox::set_editable);
  194. ClassDB::bind_method(D_METHOD("is_editable"), &SpinBox::is_editable);
  195. ClassDB::bind_method(D_METHOD("_line_edit_focus_exit"), &SpinBox::_line_edit_focus_exit);
  196. ClassDB::bind_method(D_METHOD("get_line_edit"), &SpinBox::get_line_edit);
  197. ClassDB::bind_method(D_METHOD("_line_edit_input"), &SpinBox::_line_edit_input);
  198. ClassDB::bind_method(D_METHOD("_range_click_timeout"), &SpinBox::_range_click_timeout);
  199. ADD_PROPERTY(PropertyInfo(Variant::INT, "align", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_align", "get_align");
  200. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable");
  201. ADD_PROPERTY(PropertyInfo(Variant::STRING, "prefix"), "set_prefix", "get_prefix");
  202. ADD_PROPERTY(PropertyInfo(Variant::STRING, "suffix"), "set_suffix", "get_suffix");
  203. }
  204. SpinBox::SpinBox() {
  205. last_w = 0;
  206. line_edit = memnew(LineEdit);
  207. add_child(line_edit);
  208. line_edit->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  209. //connect("value_changed",this,"_value_changed");
  210. line_edit->connect("text_entered", this, "_text_entered", Vector<Variant>(), CONNECT_DEFERRED);
  211. line_edit->connect("focus_exited", this, "_line_edit_focus_exit", Vector<Variant>(), CONNECT_DEFERRED);
  212. line_edit->connect("gui_input", this, "_line_edit_input");
  213. drag.enabled = false;
  214. range_click_timer = memnew(Timer);
  215. range_click_timer->connect("timeout", this, "_range_click_timeout");
  216. add_child(range_click_timer);
  217. }