2
0

editor_spin_slider.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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-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 "editor_spin_slider.h"
  31. #include "core/math/expression.h"
  32. #include "core/os/input.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 scale_x = get_global_transform_with_canvas().get_scale().x;
  158. ERR_FAIL_COND(Math::is_zero_approx(scale_x));
  159. float grabbing_ofs = (grabber->get_transform().xform(mm->get_position()).x - grabbing_from) / float(grabber_range) / scale_x;
  160. set_as_ratio(grabbing_ratio + grabbing_ofs);
  161. update();
  162. }
  163. }
  164. void EditorSpinSlider::_notification(int p_what) {
  165. if (p_what == MainLoop::NOTIFICATION_WM_FOCUS_OUT ||
  166. p_what == MainLoop::NOTIFICATION_WM_FOCUS_IN ||
  167. p_what == NOTIFICATION_EXIT_TREE) {
  168. if (grabbing_spinner) {
  169. grabber->hide();
  170. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  171. grabbing_spinner = false;
  172. grabbing_spinner_attempt = false;
  173. }
  174. }
  175. if (p_what == NOTIFICATION_READY) {
  176. // Add a left margin to the stylebox to make the number align with the Label
  177. // when it's edited. The LineEdit "focus" stylebox uses the "normal" stylebox's
  178. // default margins.
  179. Ref<StyleBoxFlat> stylebox =
  180. EditorNode::get_singleton()->get_theme_base()->get_stylebox("normal", "LineEdit")->duplicate();
  181. // EditorSpinSliders with a label have more space on the left, so add an
  182. // higher margin to match the location where the text begins.
  183. // The margin values below were determined by empirical testing.
  184. stylebox->set_default_margin(MARGIN_LEFT, (get_label() != String() ? 23 : 16) * EDSCALE);
  185. value_input->add_style_override("normal", stylebox);
  186. }
  187. if (p_what == NOTIFICATION_DRAW) {
  188. updown_offset = -1;
  189. Ref<StyleBox> sb = get_stylebox("normal", "LineEdit");
  190. if (!flat) {
  191. draw_style_box(sb, Rect2(Vector2(), get_size()));
  192. }
  193. Ref<Font> font = get_font("font", "LineEdit");
  194. int sep_base = 4 * EDSCALE;
  195. int sep = sep_base + sb->get_offset().x; //make it have the same margin on both sides, looks better
  196. int string_width = font->get_string_size(label).width;
  197. int number_width = get_size().width - sb->get_minimum_size().width - string_width - sep;
  198. Ref<Texture> updown = get_icon("updown", "SpinBox");
  199. if (get_step() == 1) {
  200. number_width -= updown->get_width();
  201. }
  202. String numstr = get_text_value();
  203. int vofs = (get_size().height - font->get_height()) / 2 + font->get_ascent();
  204. Color fc = get_color("font_color", "LineEdit");
  205. Color lc;
  206. if (use_custom_label_color) {
  207. lc = custom_label_color;
  208. } else {
  209. lc = fc;
  210. }
  211. if (flat && label != String()) {
  212. Color label_bg_color = get_color("dark_color_3", "Editor");
  213. draw_rect(Rect2(Vector2(), Vector2(sb->get_offset().x * 2 + string_width, get_size().height)), label_bg_color);
  214. }
  215. if (has_focus()) {
  216. Ref<StyleBox> focus = get_stylebox("focus", "LineEdit");
  217. draw_style_box(focus, Rect2(Vector2(), get_size()));
  218. }
  219. draw_string(font, Vector2(Math::round(sb->get_offset().x), vofs), label, lc * Color(1, 1, 1, 0.5));
  220. draw_string(font, Vector2(Math::round(sb->get_offset().x + string_width + sep), vofs), numstr, fc, number_width);
  221. if (get_step() == 1) {
  222. Ref<Texture> updown2 = get_icon("updown", "SpinBox");
  223. int updown_vofs = (get_size().height - updown2->get_height()) / 2;
  224. updown_offset = get_size().width - sb->get_margin(MARGIN_RIGHT) - updown2->get_width();
  225. Color c(1, 1, 1);
  226. if (hover_updown) {
  227. c *= Color(1.2, 1.2, 1.2);
  228. }
  229. draw_texture(updown2, Vector2(updown_offset, updown_vofs), c);
  230. if (grabber->is_visible()) {
  231. grabber->hide();
  232. }
  233. } else if (!hide_slider) {
  234. int grabber_w = 4 * EDSCALE;
  235. int width = get_size().width - sb->get_minimum_size().width - grabber_w;
  236. int ofs = sb->get_offset().x;
  237. int svofs = (get_size().height + vofs) / 2 - 1;
  238. Color c = fc;
  239. c.a = 0.2;
  240. draw_rect(Rect2(ofs, svofs + 1, width, 2 * EDSCALE), c);
  241. int gofs = get_as_ratio() * width;
  242. c.a = 0.9;
  243. Rect2 grabber_rect = Rect2(ofs + gofs, svofs + 1, grabber_w, 2 * EDSCALE);
  244. draw_rect(grabber_rect, c);
  245. bool display_grabber = (mouse_over_spin || mouse_over_grabber) && !grabbing_spinner && !value_input->is_visible();
  246. if (grabber->is_visible() != display_grabber) {
  247. if (display_grabber) {
  248. grabber->show();
  249. } else {
  250. grabber->hide();
  251. }
  252. }
  253. if (display_grabber) {
  254. Ref<Texture> grabber_tex;
  255. if (mouse_over_grabber) {
  256. grabber_tex = get_icon("grabber_highlight", "HSlider");
  257. } else {
  258. grabber_tex = get_icon("grabber", "HSlider");
  259. }
  260. if (grabber->get_texture() != grabber_tex) {
  261. grabber->set_texture(grabber_tex);
  262. }
  263. Vector2 scale = get_global_transform_with_canvas().get_scale();
  264. grabber->set_scale(scale);
  265. grabber->set_size(Size2(0, 0));
  266. grabber->set_position(get_global_position() + (grabber_rect.position + grabber_rect.size * 0.5 - grabber->get_size() * 0.5) * scale);
  267. if (mousewheel_over_grabber) {
  268. Input::get_singleton()->warp_mouse_position(grabber->get_position() + grabber_rect.size);
  269. }
  270. grabber_range = width;
  271. }
  272. }
  273. }
  274. if (p_what == NOTIFICATION_MOUSE_ENTER) {
  275. mouse_over_spin = true;
  276. update();
  277. }
  278. if (p_what == NOTIFICATION_MOUSE_EXIT) {
  279. mouse_over_spin = false;
  280. update();
  281. }
  282. if (p_what == NOTIFICATION_FOCUS_ENTER) {
  283. /* Sorry, I don't like this, it makes navigating the different fields with arrows more difficult.
  284. * Just press enter to edit.
  285. * if (Input::get_singleton()->is_mouse_button_pressed(BUTTON_LEFT) && !value_input_just_closed) {
  286. _focus_entered();
  287. }*/
  288. if ((Input::get_singleton()->is_action_pressed("ui_focus_next") || Input::get_singleton()->is_action_pressed("ui_focus_prev")) && !value_input_just_closed) {
  289. _focus_entered();
  290. }
  291. value_input_just_closed = false;
  292. }
  293. }
  294. Size2 EditorSpinSlider::get_minimum_size() const {
  295. Ref<StyleBox> sb = get_stylebox("normal", "LineEdit");
  296. Ref<Font> font = get_font("font", "LineEdit");
  297. Size2 ms = sb->get_minimum_size();
  298. ms.height += font->get_height();
  299. return ms;
  300. }
  301. void EditorSpinSlider::set_hide_slider(bool p_hide) {
  302. hide_slider = p_hide;
  303. update();
  304. }
  305. bool EditorSpinSlider::is_hiding_slider() const {
  306. return hide_slider;
  307. }
  308. void EditorSpinSlider::set_label(const String &p_label) {
  309. label = p_label;
  310. update();
  311. }
  312. String EditorSpinSlider::get_label() const {
  313. return label;
  314. }
  315. void EditorSpinSlider::_evaluate_input_text() {
  316. // Replace comma with dot to support it as decimal separator (GH-6028).
  317. // This prevents using functions like `pow()`, but using functions
  318. // in EditorSpinSlider is a barely known (and barely used) feature.
  319. // Instead, we'd rather support German/French keyboard layouts out of the box.
  320. const String text = value_input->get_text().replace(",", ".");
  321. Ref<Expression> expr;
  322. expr.instance();
  323. Error err = expr->parse(text);
  324. if (err != OK) {
  325. return;
  326. }
  327. Variant v = expr->execute(Array(), NULL, false);
  328. if (v.get_type() == Variant::NIL)
  329. return;
  330. set_value(v);
  331. }
  332. //text_entered signal
  333. void EditorSpinSlider::_value_input_entered(const String &p_text) {
  334. value_input_just_closed = true;
  335. value_input->hide();
  336. }
  337. //modal_closed signal
  338. void EditorSpinSlider::_value_input_closed() {
  339. _evaluate_input_text();
  340. value_input_just_closed = true;
  341. }
  342. //focus_exited signal
  343. void EditorSpinSlider::_value_focus_exited() {
  344. // discontinue because the focus_exit was caused by right-click context menu
  345. if (value_input->get_menu()->is_visible())
  346. return;
  347. _evaluate_input_text();
  348. // focus is not on the same element after the vlalue_input was exited
  349. // -> focus is on next element
  350. // -> TAB was pressed
  351. // -> modal_close was not called
  352. // -> need to close/hide manually
  353. if (!value_input_just_closed) { //value_input_just_closed should do the same
  354. value_input->hide();
  355. //tab was pressed
  356. } else {
  357. //enter, click, esc
  358. }
  359. }
  360. void EditorSpinSlider::_grabber_mouse_entered() {
  361. mouse_over_grabber = true;
  362. update();
  363. }
  364. void EditorSpinSlider::_grabber_mouse_exited() {
  365. mouse_over_grabber = false;
  366. update();
  367. }
  368. void EditorSpinSlider::set_read_only(bool p_enable) {
  369. read_only = p_enable;
  370. update();
  371. }
  372. bool EditorSpinSlider::is_read_only() const {
  373. return read_only;
  374. }
  375. void EditorSpinSlider::set_flat(bool p_enable) {
  376. flat = p_enable;
  377. update();
  378. }
  379. bool EditorSpinSlider::is_flat() const {
  380. return flat;
  381. }
  382. void EditorSpinSlider::set_custom_label_color(bool p_use_custom_label_color, Color p_custom_label_color) {
  383. use_custom_label_color = p_use_custom_label_color;
  384. custom_label_color = p_custom_label_color;
  385. }
  386. void EditorSpinSlider::_focus_entered() {
  387. Rect2 gr = get_global_rect();
  388. value_input->set_text(get_text_value());
  389. value_input->set_position(gr.position);
  390. value_input->set_size(gr.size);
  391. value_input->call_deferred("show_modal");
  392. value_input->call_deferred("grab_focus");
  393. value_input->call_deferred("select_all");
  394. value_input->set_focus_next(find_next_valid_focus()->get_path());
  395. value_input->set_focus_previous(find_prev_valid_focus()->get_path());
  396. }
  397. void EditorSpinSlider::_bind_methods() {
  398. ClassDB::bind_method(D_METHOD("set_label", "label"), &EditorSpinSlider::set_label);
  399. ClassDB::bind_method(D_METHOD("get_label"), &EditorSpinSlider::get_label);
  400. ClassDB::bind_method(D_METHOD("set_read_only", "read_only"), &EditorSpinSlider::set_read_only);
  401. ClassDB::bind_method(D_METHOD("is_read_only"), &EditorSpinSlider::is_read_only);
  402. ClassDB::bind_method(D_METHOD("set_flat", "flat"), &EditorSpinSlider::set_flat);
  403. ClassDB::bind_method(D_METHOD("is_flat"), &EditorSpinSlider::is_flat);
  404. ClassDB::bind_method(D_METHOD("_gui_input"), &EditorSpinSlider::_gui_input);
  405. ClassDB::bind_method(D_METHOD("_grabber_mouse_entered"), &EditorSpinSlider::_grabber_mouse_entered);
  406. ClassDB::bind_method(D_METHOD("_grabber_mouse_exited"), &EditorSpinSlider::_grabber_mouse_exited);
  407. ClassDB::bind_method(D_METHOD("_grabber_gui_input"), &EditorSpinSlider::_grabber_gui_input);
  408. ClassDB::bind_method(D_METHOD("_value_input_closed"), &EditorSpinSlider::_value_input_closed);
  409. ClassDB::bind_method(D_METHOD("_value_input_entered"), &EditorSpinSlider::_value_input_entered);
  410. ClassDB::bind_method(D_METHOD("_value_focus_exited"), &EditorSpinSlider::_value_focus_exited);
  411. ADD_PROPERTY(PropertyInfo(Variant::STRING, "label"), "set_label", "get_label");
  412. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "read_only"), "set_read_only", "is_read_only");
  413. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flat"), "set_flat", "is_flat");
  414. }
  415. EditorSpinSlider::EditorSpinSlider() {
  416. flat = false;
  417. grabbing_spinner_attempt = false;
  418. grabbing_spinner = false;
  419. grabbing_spinner_dist_cache = 0;
  420. pre_grab_value = 0;
  421. set_focus_mode(FOCUS_ALL);
  422. updown_offset = -1;
  423. hover_updown = false;
  424. grabber = memnew(TextureRect);
  425. add_child(grabber);
  426. grabber->hide();
  427. grabber->set_as_toplevel(true);
  428. grabber->set_mouse_filter(MOUSE_FILTER_STOP);
  429. grabber->connect("mouse_entered", this, "_grabber_mouse_entered");
  430. grabber->connect("mouse_exited", this, "_grabber_mouse_exited");
  431. grabber->connect("gui_input", this, "_grabber_gui_input");
  432. mouse_over_spin = false;
  433. mouse_over_grabber = false;
  434. mousewheel_over_grabber = false;
  435. grabbing_grabber = false;
  436. grabber_range = 1;
  437. value_input = memnew(LineEdit);
  438. add_child(value_input);
  439. value_input->set_as_toplevel(true);
  440. value_input->hide();
  441. value_input->connect("modal_closed", this, "_value_input_closed");
  442. value_input->connect("text_entered", this, "_value_input_entered");
  443. value_input->connect("focus_exited", this, "_value_focus_exited");
  444. value_input_just_closed = false;
  445. hide_slider = false;
  446. read_only = false;
  447. use_custom_label_color = false;
  448. }