editor_spin_slider.cpp 17 KB

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