editor_spin_slider.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /*************************************************************************/
  2. /* editor_spin_slider.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "editor_spin_slider.h"
  31. #include "core/input/input.h"
  32. #include "core/math/expression.h"
  33. #include "editor_node.h"
  34. #include "editor_scale.h"
  35. String EditorSpinSlider::get_tooltip(const Point2 &p_pos) const {
  36. if (grabber->is_visible()) {
  37. return rtos(get_value()) + "\n\n" + TTR("Hold Ctrl to round to integers. Hold Shift for more precise changes.");
  38. }
  39. return rtos(get_value());
  40. }
  41. String EditorSpinSlider::get_text_value() const {
  42. return String::num(get_value(), Math::range_step_decimals(get_step()));
  43. }
  44. void EditorSpinSlider::_gui_input(const Ref<InputEvent> &p_event) {
  45. if (read_only)
  46. return;
  47. Ref<InputEventMouseButton> mb = p_event;
  48. if (mb.is_valid()) {
  49. if (mb->get_button_index() == BUTTON_LEFT) {
  50. if (mb->is_pressed()) {
  51. if (updown_offset != -1 && mb->get_position().x > updown_offset) {
  52. //there is an updown, so use it.
  53. if (mb->get_position().y < get_size().height / 2) {
  54. set_value(get_value() + get_step());
  55. } else {
  56. set_value(get_value() - get_step());
  57. }
  58. return;
  59. } else {
  60. grabbing_spinner_attempt = true;
  61. grabbing_spinner_dist_cache = 0;
  62. pre_grab_value = get_value();
  63. grabbing_spinner = false;
  64. grabbing_spinner_mouse_pos = Input::get_singleton()->get_mouse_position();
  65. }
  66. } else {
  67. if (grabbing_spinner_attempt) {
  68. if (grabbing_spinner) {
  69. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  70. Input::get_singleton()->warp_mouse_position(grabbing_spinner_mouse_pos);
  71. update();
  72. } else {
  73. _focus_entered();
  74. }
  75. grabbing_spinner = false;
  76. grabbing_spinner_attempt = false;
  77. }
  78. }
  79. } else if (mb->get_button_index() == BUTTON_WHEEL_UP || mb->get_button_index() == BUTTON_WHEEL_DOWN) {
  80. if (grabber->is_visible())
  81. call_deferred("update");
  82. }
  83. }
  84. Ref<InputEventMouseMotion> mm = p_event;
  85. if (mm.is_valid()) {
  86. if (grabbing_spinner_attempt) {
  87. double diff_x = mm->get_relative().x;
  88. if (mm->get_shift() && grabbing_spinner) {
  89. diff_x *= 0.1;
  90. }
  91. grabbing_spinner_dist_cache += diff_x;
  92. if (!grabbing_spinner && ABS(grabbing_spinner_dist_cache) > 4 * EDSCALE) {
  93. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
  94. grabbing_spinner = true;
  95. }
  96. if (grabbing_spinner) {
  97. // Don't make the user scroll all the way back to 'in range' if they went off the end.
  98. if (pre_grab_value < get_min() && !is_lesser_allowed()) {
  99. pre_grab_value = get_min();
  100. }
  101. if (pre_grab_value > get_max() && !is_greater_allowed()) {
  102. pre_grab_value = get_max();
  103. }
  104. if (mm->get_control()) {
  105. // If control was just pressed, don't make the value do a huge jump in magnitude.
  106. if (grabbing_spinner_dist_cache != 0) {
  107. pre_grab_value += grabbing_spinner_dist_cache * get_step();
  108. grabbing_spinner_dist_cache = 0;
  109. }
  110. set_value(Math::round(pre_grab_value + get_step() * grabbing_spinner_dist_cache * 10));
  111. } else {
  112. set_value(pre_grab_value + get_step() * grabbing_spinner_dist_cache);
  113. }
  114. }
  115. } else if (updown_offset != -1) {
  116. bool new_hover = (mm->get_position().x > updown_offset);
  117. if (new_hover != hover_updown) {
  118. hover_updown = new_hover;
  119. update();
  120. }
  121. }
  122. }
  123. Ref<InputEventKey> k = p_event;
  124. if (k.is_valid() && k->is_pressed() && k->is_action("ui_accept")) {
  125. _focus_entered();
  126. }
  127. }
  128. void EditorSpinSlider::_grabber_gui_input(const Ref<InputEvent> &p_event) {
  129. Ref<InputEventMouseButton> mb = p_event;
  130. if (grabbing_grabber) {
  131. if (mb.is_valid()) {
  132. if (mb->get_button_index() == BUTTON_WHEEL_UP) {
  133. set_value(get_value() + get_step());
  134. mousewheel_over_grabber = true;
  135. } else if (mb->get_button_index() == BUTTON_WHEEL_DOWN) {
  136. set_value(get_value() - get_step());
  137. mousewheel_over_grabber = true;
  138. }
  139. }
  140. }
  141. if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT) {
  142. if (mb->is_pressed()) {
  143. grabbing_grabber = true;
  144. if (!mousewheel_over_grabber) {
  145. grabbing_ratio = get_as_ratio();
  146. grabbing_from = grabber->get_transform().xform(mb->get_position()).x;
  147. }
  148. } else {
  149. grabbing_grabber = false;
  150. mousewheel_over_grabber = false;
  151. }
  152. }
  153. Ref<InputEventMouseMotion> mm = p_event;
  154. if (mm.is_valid() && grabbing_grabber) {
  155. if (mousewheel_over_grabber)
  156. return;
  157. float grabbing_ofs = (grabber->get_transform().xform(mm->get_position()).x - grabbing_from) / float(grabber_range);
  158. set_as_ratio(grabbing_ratio + grabbing_ofs);
  159. update();
  160. }
  161. }
  162. void EditorSpinSlider::_notification(int p_what) {
  163. if (p_what == NOTIFICATION_WM_FOCUS_OUT ||
  164. p_what == NOTIFICATION_WM_FOCUS_IN ||
  165. p_what == NOTIFICATION_EXIT_TREE) {
  166. if (grabbing_spinner) {
  167. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  168. grabbing_spinner = false;
  169. grabbing_spinner_attempt = false;
  170. }
  171. }
  172. if (p_what == NOTIFICATION_READY) {
  173. // Add a left margin to the stylebox to make the number align with the Label
  174. // when it's edited. The LineEdit "focus" stylebox uses the "normal" stylebox's
  175. // default margins.
  176. Ref<StyleBoxFlat> stylebox =
  177. EditorNode::get_singleton()->get_theme_base()->get_theme_stylebox("normal", "LineEdit")->duplicate();
  178. // EditorSpinSliders with a label have more space on the left, so add an
  179. // higher margin to match the location where the text begins.
  180. // The margin values below were determined by empirical testing.
  181. stylebox->set_default_margin(MARGIN_LEFT, (get_label() != String() ? 23 : 16) * EDSCALE);
  182. value_input->add_theme_style_override("normal", stylebox);
  183. }
  184. if (p_what == NOTIFICATION_DRAW) {
  185. updown_offset = -1;
  186. Ref<StyleBox> sb = get_theme_stylebox("normal", "LineEdit");
  187. if (!flat) {
  188. draw_style_box(sb, Rect2(Vector2(), get_size()));
  189. }
  190. Ref<Font> font = get_theme_font("font", "LineEdit");
  191. int sep_base = 4 * EDSCALE;
  192. int sep = sep_base + sb->get_offset().x; //make it have the same margin on both sides, looks better
  193. int string_width = font->get_string_size(label).width;
  194. int number_width = get_size().width - sb->get_minimum_size().width - string_width - sep;
  195. Ref<Texture2D> updown = get_theme_icon("updown", "SpinBox");
  196. if (get_step() == 1) {
  197. number_width -= updown->get_width();
  198. }
  199. String numstr = get_text_value();
  200. int vofs = (get_size().height - font->get_height()) / 2 + font->get_ascent();
  201. Color fc = get_theme_color("font_color", "LineEdit");
  202. Color lc;
  203. if (use_custom_label_color) {
  204. lc = custom_label_color;
  205. } else {
  206. lc = fc;
  207. }
  208. if (flat && label != String()) {
  209. Color label_bg_color = get_theme_color("dark_color_3", "Editor");
  210. draw_rect(Rect2(Vector2(), Vector2(sb->get_offset().x * 2 + string_width, get_size().height)), label_bg_color);
  211. }
  212. if (has_focus()) {
  213. Ref<StyleBox> focus = get_theme_stylebox("focus", "LineEdit");
  214. draw_style_box(focus, Rect2(Vector2(), get_size()));
  215. }
  216. draw_string(font, Vector2(Math::round(sb->get_offset().x), vofs), label, lc * Color(1, 1, 1, 0.5));
  217. draw_string(font, Vector2(Math::round(sb->get_offset().x + string_width + sep), vofs), numstr, fc, number_width);
  218. if (get_step() == 1) {
  219. Ref<Texture2D> updown2 = get_theme_icon("updown", "SpinBox");
  220. int updown_vofs = (get_size().height - updown2->get_height()) / 2;
  221. updown_offset = get_size().width - sb->get_margin(MARGIN_RIGHT) - updown2->get_width();
  222. Color c(1, 1, 1);
  223. if (hover_updown) {
  224. c *= Color(1.2, 1.2, 1.2);
  225. }
  226. draw_texture(updown2, Vector2(updown_offset, updown_vofs), c);
  227. if (grabber->is_visible()) {
  228. grabber->hide();
  229. }
  230. } else if (!hide_slider) {
  231. int grabber_w = 4 * EDSCALE;
  232. int width = get_size().width - sb->get_minimum_size().width - grabber_w;
  233. int ofs = sb->get_offset().x;
  234. int svofs = (get_size().height + vofs) / 2 - 1;
  235. Color c = fc;
  236. c.a = 0.2;
  237. draw_rect(Rect2(ofs, svofs + 1, width, 2 * EDSCALE), c);
  238. int gofs = get_as_ratio() * width;
  239. c.a = 0.9;
  240. Rect2 grabber_rect = Rect2(ofs + gofs, svofs + 1, grabber_w, 2 * EDSCALE);
  241. draw_rect(grabber_rect, c);
  242. bool display_grabber = (mouse_over_spin || mouse_over_grabber) && !grabbing_spinner && !value_input_popup->is_visible();
  243. if (grabber->is_visible() != display_grabber) {
  244. if (display_grabber) {
  245. grabber->show();
  246. } else {
  247. grabber->hide();
  248. }
  249. }
  250. if (display_grabber) {
  251. Ref<Texture2D> grabber_tex;
  252. if (mouse_over_grabber) {
  253. grabber_tex = get_theme_icon("grabber_highlight", "HSlider");
  254. } else {
  255. grabber_tex = get_theme_icon("grabber", "HSlider");
  256. }
  257. if (grabber->get_texture() != grabber_tex) {
  258. grabber->set_texture(grabber_tex);
  259. }
  260. grabber->set_size(Size2(0, 0));
  261. grabber->set_position(get_global_position() + grabber_rect.position + grabber_rect.size * 0.5 - grabber->get_size() * 0.5);
  262. if (mousewheel_over_grabber) {
  263. Input::get_singleton()->warp_mouse_position(grabber->get_position() + grabber_rect.size);
  264. }
  265. grabber_range = width;
  266. }
  267. }
  268. }
  269. if (p_what == NOTIFICATION_MOUSE_ENTER) {
  270. mouse_over_spin = true;
  271. update();
  272. }
  273. if (p_what == NOTIFICATION_MOUSE_EXIT) {
  274. mouse_over_spin = false;
  275. update();
  276. }
  277. if (p_what == NOTIFICATION_FOCUS_ENTER) {
  278. if ((Input::get_singleton()->is_action_pressed("ui_focus_next") || Input::get_singleton()->is_action_pressed("ui_focus_prev")) && !value_input_just_closed) {
  279. _focus_entered();
  280. }
  281. value_input_just_closed = false;
  282. }
  283. }
  284. Size2 EditorSpinSlider::get_minimum_size() const {
  285. Ref<StyleBox> sb = get_theme_stylebox("normal", "LineEdit");
  286. Ref<Font> font = get_theme_font("font", "LineEdit");
  287. Size2 ms = sb->get_minimum_size();
  288. ms.height += font->get_height();
  289. return ms;
  290. }
  291. void EditorSpinSlider::set_hide_slider(bool p_hide) {
  292. hide_slider = p_hide;
  293. update();
  294. }
  295. bool EditorSpinSlider::is_hiding_slider() const {
  296. return hide_slider;
  297. }
  298. void EditorSpinSlider::set_label(const String &p_label) {
  299. label = p_label;
  300. update();
  301. }
  302. String EditorSpinSlider::get_label() const {
  303. return label;
  304. }
  305. void EditorSpinSlider::_evaluate_input_text() {
  306. String text = value_input->get_text();
  307. Ref<Expression> expr;
  308. expr.instance();
  309. Error err = expr->parse(text);
  310. if (err != OK) {
  311. return;
  312. }
  313. Variant v = expr->execute(Array(), nullptr, false);
  314. if (v.get_type() == Variant::NIL)
  315. return;
  316. set_value(v);
  317. }
  318. //text_entered signal
  319. void EditorSpinSlider::_value_input_entered(const String &p_text) {
  320. value_input_just_closed = true;
  321. value_input_popup->hide();
  322. }
  323. //modal_closed signal
  324. void EditorSpinSlider::_value_input_closed() {
  325. _evaluate_input_text();
  326. value_input_just_closed = true;
  327. }
  328. //focus_exited signal
  329. void EditorSpinSlider::_value_focus_exited() {
  330. // discontinue because the focus_exit was caused by right-click context menu
  331. if (value_input->get_menu()->is_visible())
  332. return;
  333. _evaluate_input_text();
  334. // focus is not on the same element after the vlalue_input was exited
  335. // -> focus is on next element
  336. // -> TAB was pressed
  337. // -> modal_close was not called
  338. // -> need to close/hide manually
  339. if (!value_input_just_closed) { //value_input_just_closed should do the same
  340. value_input_popup->hide();
  341. //tab was pressed
  342. } else {
  343. //enter, click, esc
  344. }
  345. }
  346. void EditorSpinSlider::_grabber_mouse_entered() {
  347. mouse_over_grabber = true;
  348. update();
  349. }
  350. void EditorSpinSlider::_grabber_mouse_exited() {
  351. mouse_over_grabber = false;
  352. update();
  353. }
  354. void EditorSpinSlider::set_read_only(bool p_enable) {
  355. read_only = p_enable;
  356. update();
  357. }
  358. bool EditorSpinSlider::is_read_only() const {
  359. return read_only;
  360. }
  361. void EditorSpinSlider::set_flat(bool p_enable) {
  362. flat = p_enable;
  363. update();
  364. }
  365. bool EditorSpinSlider::is_flat() const {
  366. return flat;
  367. }
  368. void EditorSpinSlider::set_custom_label_color(bool p_use_custom_label_color, Color p_custom_label_color) {
  369. use_custom_label_color = p_use_custom_label_color;
  370. custom_label_color = p_custom_label_color;
  371. }
  372. void EditorSpinSlider::_focus_entered() {
  373. Rect2 gr = get_screen_rect();
  374. value_input->set_text(get_text_value());
  375. value_input_popup->set_position(gr.position);
  376. value_input_popup->set_size(gr.size);
  377. value_input_popup->call_deferred("popup");
  378. value_input->call_deferred("grab_focus");
  379. value_input->call_deferred("select_all");
  380. value_input->set_focus_next(find_next_valid_focus()->get_path());
  381. value_input->set_focus_previous(find_prev_valid_focus()->get_path());
  382. }
  383. void EditorSpinSlider::_bind_methods() {
  384. ClassDB::bind_method(D_METHOD("set_label", "label"), &EditorSpinSlider::set_label);
  385. ClassDB::bind_method(D_METHOD("get_label"), &EditorSpinSlider::get_label);
  386. ClassDB::bind_method(D_METHOD("set_read_only", "read_only"), &EditorSpinSlider::set_read_only);
  387. ClassDB::bind_method(D_METHOD("is_read_only"), &EditorSpinSlider::is_read_only);
  388. ClassDB::bind_method(D_METHOD("set_flat", "flat"), &EditorSpinSlider::set_flat);
  389. ClassDB::bind_method(D_METHOD("is_flat"), &EditorSpinSlider::is_flat);
  390. ClassDB::bind_method(D_METHOD("_gui_input"), &EditorSpinSlider::_gui_input);
  391. ADD_PROPERTY(PropertyInfo(Variant::STRING, "label"), "set_label", "get_label");
  392. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "read_only"), "set_read_only", "is_read_only");
  393. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flat"), "set_flat", "is_flat");
  394. }
  395. EditorSpinSlider::EditorSpinSlider() {
  396. flat = false;
  397. grabbing_spinner_attempt = false;
  398. grabbing_spinner = false;
  399. grabbing_spinner_dist_cache = 0;
  400. pre_grab_value = 0;
  401. set_focus_mode(FOCUS_ALL);
  402. updown_offset = -1;
  403. hover_updown = false;
  404. grabber = memnew(TextureRect);
  405. add_child(grabber);
  406. grabber->hide();
  407. grabber->set_as_toplevel(true);
  408. grabber->set_mouse_filter(MOUSE_FILTER_STOP);
  409. grabber->connect("mouse_entered", callable_mp(this, &EditorSpinSlider::_grabber_mouse_entered));
  410. grabber->connect("mouse_exited", callable_mp(this, &EditorSpinSlider::_grabber_mouse_exited));
  411. grabber->connect("gui_input", callable_mp(this, &EditorSpinSlider::_grabber_gui_input));
  412. mouse_over_spin = false;
  413. mouse_over_grabber = false;
  414. mousewheel_over_grabber = false;
  415. grabbing_grabber = false;
  416. grabber_range = 1;
  417. value_input_popup = memnew(Popup);
  418. add_child(value_input_popup);
  419. value_input = memnew(LineEdit);
  420. value_input_popup->add_child(value_input);
  421. value_input_popup->set_wrap_controls(true);
  422. value_input->set_anchors_and_margins_preset(PRESET_WIDE);
  423. value_input_popup->connect("popup_hide", callable_mp(this, &EditorSpinSlider::_value_input_closed));
  424. value_input->connect("text_entered", callable_mp(this, &EditorSpinSlider::_value_input_entered));
  425. value_input->connect("focus_exited", callable_mp(this, &EditorSpinSlider::_value_focus_exited));
  426. value_input_just_closed = false;
  427. hide_slider = false;
  428. read_only = false;
  429. use_custom_label_color = false;
  430. }