editor_spin_slider.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. /**************************************************************************/
  2. /* editor_spin_slider.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "core/os/keyboard.h"
  34. #include "editor/editor_scale.h"
  35. #include "editor/editor_settings.h"
  36. String EditorSpinSlider::get_tooltip(const Point2 &p_pos) const {
  37. if (grabber->is_visible()) {
  38. Key key = (OS::get_singleton()->has_feature("macos") || OS::get_singleton()->has_feature("web_macos") || OS::get_singleton()->has_feature("web_ios")) ? Key::META : Key::CTRL;
  39. return TS->format_number(rtos(get_value())) + "\n\n" + vformat(TTR("Hold %s to round to integers.\nHold Shift for more precise changes."), find_keycode_name(key));
  40. }
  41. return TS->format_number(rtos(get_value()));
  42. }
  43. String EditorSpinSlider::get_text_value() const {
  44. return TS->format_number(String::num(get_value(), Math::range_step_decimals(get_step())));
  45. }
  46. void EditorSpinSlider::gui_input(const Ref<InputEvent> &p_event) {
  47. ERR_FAIL_COND(p_event.is_null());
  48. if (read_only) {
  49. return;
  50. }
  51. Ref<InputEventMouseButton> mb = p_event;
  52. if (mb.is_valid()) {
  53. if (mb->get_button_index() == MouseButton::LEFT) {
  54. if (mb->is_pressed()) {
  55. if (updown_offset != -1 && mb->get_position().x > updown_offset) {
  56. //there is an updown, so use it.
  57. if (mb->get_position().y < get_size().height / 2) {
  58. set_value(get_value() + get_step());
  59. } else {
  60. set_value(get_value() - get_step());
  61. }
  62. return;
  63. } else {
  64. grabbing_spinner_attempt = true;
  65. grabbing_spinner_dist_cache = 0;
  66. pre_grab_value = get_value();
  67. grabbing_spinner = false;
  68. grabbing_spinner_mouse_pos = get_global_mouse_position();
  69. emit_signal("grabbed");
  70. }
  71. } else {
  72. if (grabbing_spinner_attempt) {
  73. if (grabbing_spinner) {
  74. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  75. Input::get_singleton()->warp_mouse(grabbing_spinner_mouse_pos);
  76. queue_redraw();
  77. emit_signal("ungrabbed");
  78. } else {
  79. _focus_entered();
  80. }
  81. grabbing_spinner = false;
  82. grabbing_spinner_attempt = false;
  83. }
  84. }
  85. } else if (mb->get_button_index() == MouseButton::WHEEL_UP || mb->get_button_index() == MouseButton::WHEEL_DOWN) {
  86. if (grabber->is_visible()) {
  87. call_deferred(SNAME("queue_redraw"));
  88. }
  89. }
  90. }
  91. Ref<InputEventMouseMotion> mm = p_event;
  92. if (mm.is_valid()) {
  93. if (grabbing_spinner_attempt) {
  94. double diff_x = mm->get_relative().x;
  95. if (mm->is_shift_pressed() && grabbing_spinner) {
  96. diff_x *= 0.1;
  97. }
  98. grabbing_spinner_dist_cache += diff_x * grabbing_spinner_speed;
  99. if (!grabbing_spinner && ABS(grabbing_spinner_dist_cache) > 4 * grabbing_spinner_speed * EDSCALE) {
  100. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
  101. grabbing_spinner = true;
  102. }
  103. if (grabbing_spinner) {
  104. // Don't make the user scroll all the way back to 'in range' if they went off the end.
  105. if (pre_grab_value < get_min() && !is_lesser_allowed()) {
  106. pre_grab_value = get_min();
  107. }
  108. if (pre_grab_value > get_max() && !is_greater_allowed()) {
  109. pre_grab_value = get_max();
  110. }
  111. if (mm->is_command_or_control_pressed()) {
  112. // If control was just pressed, don't make the value do a huge jump in magnitude.
  113. if (grabbing_spinner_dist_cache != 0) {
  114. pre_grab_value += grabbing_spinner_dist_cache * get_step();
  115. grabbing_spinner_dist_cache = 0;
  116. }
  117. set_value(Math::round(pre_grab_value + get_step() * grabbing_spinner_dist_cache * 10));
  118. } else {
  119. set_value(pre_grab_value + get_step() * grabbing_spinner_dist_cache);
  120. }
  121. }
  122. } else if (updown_offset != -1) {
  123. bool new_hover = (mm->get_position().x > updown_offset);
  124. if (new_hover != hover_updown) {
  125. hover_updown = new_hover;
  126. queue_redraw();
  127. }
  128. }
  129. }
  130. Ref<InputEventKey> k = p_event;
  131. if (k.is_valid() && k->is_pressed() && k->is_action("ui_accept", true)) {
  132. _focus_entered();
  133. }
  134. }
  135. void EditorSpinSlider::_grabber_gui_input(const Ref<InputEvent> &p_event) {
  136. if (read_only) {
  137. return;
  138. }
  139. Ref<InputEventMouseButton> mb = p_event;
  140. if (is_read_only()) {
  141. return;
  142. }
  143. if (grabbing_grabber) {
  144. if (mb.is_valid()) {
  145. if (mb->get_button_index() == MouseButton::WHEEL_UP) {
  146. set_value(get_value() + get_step());
  147. mousewheel_over_grabber = true;
  148. } else if (mb->get_button_index() == MouseButton::WHEEL_DOWN) {
  149. set_value(get_value() - get_step());
  150. mousewheel_over_grabber = true;
  151. }
  152. }
  153. }
  154. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {
  155. if (mb->is_pressed()) {
  156. grabbing_grabber = true;
  157. if (!mousewheel_over_grabber) {
  158. grabbing_ratio = get_as_ratio();
  159. grabbing_from = grabber->get_transform().xform(mb->get_position()).x;
  160. }
  161. emit_signal("grabbed");
  162. } else {
  163. grabbing_grabber = false;
  164. mousewheel_over_grabber = false;
  165. emit_signal("ungrabbed");
  166. }
  167. }
  168. Ref<InputEventMouseMotion> mm = p_event;
  169. if (mm.is_valid() && grabbing_grabber) {
  170. if (mousewheel_over_grabber) {
  171. return;
  172. }
  173. float scale_x = get_global_transform_with_canvas().get_scale().x;
  174. ERR_FAIL_COND(Math::is_zero_approx(scale_x));
  175. float grabbing_ofs = (grabber->get_transform().xform(mm->get_position()).x - grabbing_from) / float(grabber_range) / scale_x;
  176. set_as_ratio(grabbing_ratio + grabbing_ofs);
  177. queue_redraw();
  178. }
  179. }
  180. void EditorSpinSlider::_value_input_gui_input(const Ref<InputEvent> &p_event) {
  181. Ref<InputEventKey> k = p_event;
  182. if (k.is_valid() && k->is_pressed() && !is_read_only()) {
  183. double step = get_step();
  184. if (step < 1) {
  185. double divisor = 1.0 / get_step();
  186. if (trunc(divisor) == divisor) {
  187. step = 1.0;
  188. }
  189. }
  190. if (k->is_command_or_control_pressed()) {
  191. step *= 100.0;
  192. } else if (k->is_shift_pressed()) {
  193. step *= 10.0;
  194. } else if (k->is_alt_pressed()) {
  195. step *= 0.1;
  196. }
  197. Key code = k->get_keycode();
  198. switch (code) {
  199. case Key::UP:
  200. case Key::DOWN: {
  201. _evaluate_input_text();
  202. double last_value = get_value();
  203. if (code == Key::DOWN) {
  204. step *= -1;
  205. }
  206. set_value(last_value + step);
  207. double new_value = get_value();
  208. double clamp_value = CLAMP(new_value, get_min(), get_max());
  209. if (new_value != clamp_value) {
  210. set_value(clamp_value);
  211. }
  212. value_input_dirty = true;
  213. set_process_internal(true);
  214. } break;
  215. case Key::ESCAPE: {
  216. value_input_closed_frame = Engine::get_singleton()->get_frames_drawn();
  217. if (value_input_popup) {
  218. value_input_popup->hide();
  219. }
  220. } break;
  221. default:
  222. break;
  223. }
  224. }
  225. }
  226. void EditorSpinSlider::_update_value_input_stylebox() {
  227. if (!value_input) {
  228. return;
  229. }
  230. // Add a left margin to the stylebox to make the number align with the Label
  231. // when it's edited. The LineEdit "focus" stylebox uses the "normal" stylebox's
  232. // default margins.
  233. Ref<StyleBox> stylebox = get_theme_stylebox(SNAME("normal"), SNAME("LineEdit"))->duplicate();
  234. // EditorSpinSliders with a label have more space on the left, so add an
  235. // higher margin to match the location where the text begins.
  236. // The margin values below were determined by empirical testing.
  237. if (is_layout_rtl()) {
  238. stylebox->set_content_margin(SIDE_LEFT, 0);
  239. stylebox->set_content_margin(SIDE_RIGHT, (!get_label().is_empty() ? 23 : 16) * EDSCALE);
  240. } else {
  241. stylebox->set_content_margin(SIDE_LEFT, (!get_label().is_empty() ? 23 : 16) * EDSCALE);
  242. stylebox->set_content_margin(SIDE_RIGHT, 0);
  243. }
  244. value_input->add_theme_style_override("normal", stylebox);
  245. }
  246. void EditorSpinSlider::_draw_spin_slider() {
  247. updown_offset = -1;
  248. RID ci = get_canvas_item();
  249. bool rtl = is_layout_rtl();
  250. Vector2 size = get_size();
  251. Ref<StyleBox> sb = get_theme_stylebox(is_read_only() ? SNAME("read_only") : SNAME("normal"), SNAME("LineEdit"));
  252. if (!flat) {
  253. draw_style_box(sb, Rect2(Vector2(), size));
  254. }
  255. Ref<Font> font = get_theme_font(SNAME("font"), SNAME("LineEdit"));
  256. int font_size = get_theme_font_size(SNAME("font_size"), SNAME("LineEdit"));
  257. int sep_base = 4 * EDSCALE;
  258. int sep = sep_base + sb->get_offset().x; //make it have the same margin on both sides, looks better
  259. int label_width = font->get_string_size(label, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).width;
  260. int number_width = size.width - sb->get_minimum_size().width - label_width - sep;
  261. Ref<Texture2D> updown = get_theme_icon(is_read_only() ? SNAME("updown_disabled") : SNAME("updown"), SNAME("SpinBox"));
  262. String numstr = get_text_value();
  263. int vofs = (size.height - font->get_height(font_size)) / 2 + font->get_ascent(font_size);
  264. Color fc = get_theme_color(is_read_only() ? SNAME("font_uneditable_color") : SNAME("font_color"), SNAME("LineEdit"));
  265. Color lc = get_theme_color(is_read_only() ? SNAME("read_only_label_color") : SNAME("label_color"));
  266. if (flat && !label.is_empty()) {
  267. Ref<StyleBox> label_bg = get_theme_stylebox(SNAME("label_bg"), SNAME("EditorSpinSlider"));
  268. if (rtl) {
  269. draw_style_box(label_bg, Rect2(Vector2(size.width - (sb->get_offset().x * 2 + label_width), 0), Vector2(sb->get_offset().x * 2 + label_width, size.height)));
  270. } else {
  271. draw_style_box(label_bg, Rect2(Vector2(), Vector2(sb->get_offset().x * 2 + label_width, size.height)));
  272. }
  273. }
  274. if (has_focus()) {
  275. Ref<StyleBox> focus = get_theme_stylebox(SNAME("focus"), SNAME("LineEdit"));
  276. draw_style_box(focus, Rect2(Vector2(), size));
  277. }
  278. if (rtl) {
  279. draw_string(font, Vector2(Math::round(size.width - sb->get_offset().x - label_width), vofs), label, HORIZONTAL_ALIGNMENT_RIGHT, -1, font_size, lc * Color(1, 1, 1, 0.5));
  280. } else {
  281. draw_string(font, Vector2(Math::round(sb->get_offset().x), vofs), label, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, lc * Color(1, 1, 1, 0.5));
  282. }
  283. int suffix_start = numstr.length();
  284. RID num_rid = TS->create_shaped_text();
  285. TS->shaped_text_add_string(num_rid, numstr + U"\u2009" + suffix, font->get_rids(), font_size, font->get_opentype_features());
  286. float text_start = rtl ? Math::round(sb->get_offset().x) : Math::round(sb->get_offset().x + label_width + sep);
  287. Vector2 text_ofs = rtl ? Vector2(text_start + (number_width - TS->shaped_text_get_width(num_rid)), vofs) : Vector2(text_start, vofs);
  288. int v_size = TS->shaped_text_get_glyph_count(num_rid);
  289. const Glyph *glyphs = TS->shaped_text_get_glyphs(num_rid);
  290. for (int i = 0; i < v_size; i++) {
  291. for (int j = 0; j < glyphs[i].repeat; j++) {
  292. if (text_ofs.x >= text_start && (text_ofs.x + glyphs[i].advance) <= (text_start + number_width)) {
  293. Color color = fc;
  294. if (glyphs[i].start >= suffix_start) {
  295. color.a *= 0.4;
  296. }
  297. if (glyphs[i].font_rid != RID()) {
  298. TS->font_draw_glyph(glyphs[i].font_rid, ci, glyphs[i].font_size, text_ofs + Vector2(glyphs[i].x_off, glyphs[i].y_off), glyphs[i].index, color);
  299. } else if ((glyphs[i].flags & TextServer::GRAPHEME_IS_VIRTUAL) != TextServer::GRAPHEME_IS_VIRTUAL) {
  300. TS->draw_hex_code_box(ci, glyphs[i].font_size, text_ofs + Vector2(glyphs[i].x_off, glyphs[i].y_off), glyphs[i].index, color);
  301. }
  302. }
  303. text_ofs.x += glyphs[i].advance;
  304. }
  305. }
  306. TS->free_rid(num_rid);
  307. if (!hide_slider) {
  308. if (get_step() == 1) {
  309. Ref<Texture2D> updown2 = get_theme_icon(is_read_only() ? SNAME("updown_disabled") : SNAME("updown"), SNAME("SpinBox"));
  310. int updown_vofs = (size.height - updown2->get_height()) / 2;
  311. if (rtl) {
  312. updown_offset = sb->get_margin(SIDE_LEFT);
  313. } else {
  314. updown_offset = size.width - sb->get_margin(SIDE_RIGHT) - updown2->get_width();
  315. }
  316. Color c(1, 1, 1);
  317. if (hover_updown) {
  318. c *= Color(1.2, 1.2, 1.2);
  319. }
  320. draw_texture(updown2, Vector2(updown_offset, updown_vofs), c);
  321. if (grabber->is_visible()) {
  322. grabber->hide();
  323. }
  324. } else {
  325. const int grabber_w = 4 * EDSCALE;
  326. const int width = size.width - sb->get_minimum_size().width - grabber_w;
  327. const int ofs = sb->get_offset().x;
  328. const int svofs = (size.height + vofs) / 2 - 1;
  329. Color c = fc;
  330. // Draw the horizontal slider's background.
  331. c.a = 0.2;
  332. draw_rect(Rect2(ofs, svofs + 1, width, 2 * EDSCALE), c);
  333. // Draw the horizontal slider's filled part on the left.
  334. const int gofs = get_as_ratio() * width;
  335. c.a = 0.45;
  336. draw_rect(Rect2(ofs, svofs + 1, gofs, 2 * EDSCALE), c);
  337. // Draw the horizontal slider's grabber.
  338. c.a = 0.9;
  339. const Rect2 grabber_rect = Rect2(ofs + gofs, svofs, grabber_w, 4 * EDSCALE);
  340. draw_rect(grabber_rect, c);
  341. grabbing_spinner_mouse_pos = get_global_position() + grabber_rect.get_center();
  342. bool display_grabber = (grabbing_grabber || mouse_over_spin || mouse_over_grabber) && !grabbing_spinner && !(value_input_popup && value_input_popup->is_visible());
  343. if (grabber->is_visible() != display_grabber) {
  344. if (display_grabber) {
  345. grabber->show();
  346. } else {
  347. grabber->hide();
  348. }
  349. }
  350. if (display_grabber) {
  351. Ref<Texture2D> grabber_tex;
  352. if (mouse_over_grabber) {
  353. grabber_tex = get_theme_icon(SNAME("grabber_highlight"), SNAME("HSlider"));
  354. } else {
  355. grabber_tex = get_theme_icon(SNAME("grabber"), SNAME("HSlider"));
  356. }
  357. if (grabber->get_texture() != grabber_tex) {
  358. grabber->set_texture(grabber_tex);
  359. }
  360. Vector2 scale = get_global_transform_with_canvas().get_scale();
  361. grabber->set_scale(scale);
  362. grabber->reset_size();
  363. grabber->set_position(get_global_position() + (grabber_rect.get_center() - grabber->get_size() * 0.5) * scale);
  364. if (mousewheel_over_grabber) {
  365. Input::get_singleton()->warp_mouse(grabber->get_position() + grabber_rect.size);
  366. }
  367. grabber_range = width;
  368. }
  369. }
  370. }
  371. }
  372. void EditorSpinSlider::_notification(int p_what) {
  373. switch (p_what) {
  374. case NOTIFICATION_ENTER_TREE: {
  375. grabbing_spinner_speed = EditorSettings::get_singleton()->get("interface/inspector/float_drag_speed");
  376. _update_value_input_stylebox();
  377. } break;
  378. case NOTIFICATION_THEME_CHANGED: {
  379. _update_value_input_stylebox();
  380. } break;
  381. case NOTIFICATION_INTERNAL_PROCESS: {
  382. if (value_input_dirty) {
  383. value_input_dirty = false;
  384. value_input->set_text(get_text_value());
  385. }
  386. set_process_internal(false);
  387. } break;
  388. case NOTIFICATION_DRAW: {
  389. _draw_spin_slider();
  390. } break;
  391. case NOTIFICATION_WM_WINDOW_FOCUS_IN:
  392. case NOTIFICATION_WM_WINDOW_FOCUS_OUT:
  393. case NOTIFICATION_WM_CLOSE_REQUEST:
  394. case NOTIFICATION_EXIT_TREE: {
  395. if (grabbing_spinner) {
  396. grabber->hide();
  397. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  398. Input::get_singleton()->warp_mouse(grabbing_spinner_mouse_pos);
  399. grabbing_spinner = false;
  400. grabbing_spinner_attempt = false;
  401. }
  402. } break;
  403. case NOTIFICATION_MOUSE_ENTER: {
  404. mouse_over_spin = true;
  405. queue_redraw();
  406. } break;
  407. case NOTIFICATION_MOUSE_EXIT: {
  408. mouse_over_spin = false;
  409. queue_redraw();
  410. } break;
  411. case NOTIFICATION_FOCUS_ENTER: {
  412. if ((Input::get_singleton()->is_action_pressed("ui_focus_next") || Input::get_singleton()->is_action_pressed("ui_focus_prev")) && value_input_closed_frame != Engine::get_singleton()->get_frames_drawn()) {
  413. _focus_entered();
  414. }
  415. value_input_closed_frame = 0;
  416. } break;
  417. }
  418. }
  419. LineEdit *EditorSpinSlider::get_line_edit() {
  420. _ensure_input_popup();
  421. return value_input;
  422. }
  423. Size2 EditorSpinSlider::get_minimum_size() const {
  424. Ref<StyleBox> sb = get_theme_stylebox(SNAME("normal"), SNAME("LineEdit"));
  425. Ref<Font> font = get_theme_font(SNAME("font"), SNAME("LineEdit"));
  426. int font_size = get_theme_font_size(SNAME("font_size"), SNAME("LineEdit"));
  427. Size2 ms = sb->get_minimum_size();
  428. ms.height += font->get_height(font_size);
  429. return ms;
  430. }
  431. void EditorSpinSlider::set_hide_slider(bool p_hide) {
  432. hide_slider = p_hide;
  433. queue_redraw();
  434. }
  435. bool EditorSpinSlider::is_hiding_slider() const {
  436. return hide_slider;
  437. }
  438. void EditorSpinSlider::set_label(const String &p_label) {
  439. label = p_label;
  440. queue_redraw();
  441. }
  442. String EditorSpinSlider::get_label() const {
  443. return label;
  444. }
  445. void EditorSpinSlider::set_suffix(const String &p_suffix) {
  446. suffix = p_suffix;
  447. queue_redraw();
  448. }
  449. String EditorSpinSlider::get_suffix() const {
  450. return suffix;
  451. }
  452. void EditorSpinSlider::_evaluate_input_text() {
  453. Ref<Expression> expr;
  454. expr.instantiate();
  455. // Convert commas ',' to dots '.' for French/German etc. keyboard layouts.
  456. String text = value_input->get_text().replace(",", ".");
  457. text = text.replace(";", ",");
  458. text = TS->parse_number(text);
  459. Error err = expr->parse(text);
  460. if (err != OK) {
  461. // If the expression failed try without converting commas to dots - they might have been for parameter separation.
  462. text = value_input->get_text();
  463. text = TS->parse_number(text);
  464. err = expr->parse(text);
  465. if (err != OK) {
  466. return;
  467. }
  468. }
  469. Variant v = expr->execute(Array(), nullptr, false, true);
  470. if (v.get_type() == Variant::NIL) {
  471. return;
  472. }
  473. set_value(v);
  474. }
  475. //text_submitted signal
  476. void EditorSpinSlider::_value_input_submitted(const String &p_text) {
  477. value_input_closed_frame = Engine::get_singleton()->get_frames_drawn();
  478. if (value_input_popup) {
  479. value_input_popup->hide();
  480. }
  481. }
  482. //modal_closed signal
  483. void EditorSpinSlider::_value_input_closed() {
  484. _evaluate_input_text();
  485. value_input_closed_frame = Engine::get_singleton()->get_frames_drawn();
  486. }
  487. //focus_exited signal
  488. void EditorSpinSlider::_value_focus_exited() {
  489. // discontinue because the focus_exit was caused by right-click context menu
  490. if (value_input->is_menu_visible()) {
  491. return;
  492. }
  493. if (is_read_only()) {
  494. // Spin slider has become read only while it was being edited.
  495. return;
  496. }
  497. _evaluate_input_text();
  498. // focus is not on the same element after the value_input was exited
  499. // -> focus is on next element
  500. // -> TAB was pressed
  501. // -> modal_close was not called
  502. // -> need to close/hide manually
  503. if (value_input_closed_frame != Engine::get_singleton()->get_frames_drawn()) {
  504. if (value_input_popup) {
  505. value_input_popup->hide();
  506. }
  507. //tab was pressed
  508. } else {
  509. //enter, click, esc
  510. grab_focus();
  511. }
  512. emit_signal("value_focus_exited");
  513. }
  514. void EditorSpinSlider::_grabber_mouse_entered() {
  515. mouse_over_grabber = true;
  516. queue_redraw();
  517. }
  518. void EditorSpinSlider::_grabber_mouse_exited() {
  519. mouse_over_grabber = false;
  520. queue_redraw();
  521. }
  522. void EditorSpinSlider::set_read_only(bool p_enable) {
  523. read_only = p_enable;
  524. if (read_only && value_input) {
  525. value_input->release_focus();
  526. }
  527. queue_redraw();
  528. }
  529. bool EditorSpinSlider::is_read_only() const {
  530. return read_only;
  531. }
  532. void EditorSpinSlider::set_flat(bool p_enable) {
  533. flat = p_enable;
  534. queue_redraw();
  535. }
  536. bool EditorSpinSlider::is_flat() const {
  537. return flat;
  538. }
  539. bool EditorSpinSlider::is_grabbing() const {
  540. return grabbing_grabber || grabbing_spinner;
  541. }
  542. void EditorSpinSlider::_focus_entered() {
  543. _ensure_input_popup();
  544. value_input->set_text(get_text_value());
  545. value_input_popup->set_size(get_size());
  546. value_input_popup->call_deferred(SNAME("show"));
  547. value_input->call_deferred(SNAME("grab_focus"));
  548. value_input->call_deferred(SNAME("select_all"));
  549. value_input->set_focus_next(find_next_valid_focus()->get_path());
  550. value_input->set_focus_previous(find_prev_valid_focus()->get_path());
  551. emit_signal("value_focus_entered");
  552. }
  553. void EditorSpinSlider::_bind_methods() {
  554. ClassDB::bind_method(D_METHOD("set_label", "label"), &EditorSpinSlider::set_label);
  555. ClassDB::bind_method(D_METHOD("get_label"), &EditorSpinSlider::get_label);
  556. ClassDB::bind_method(D_METHOD("set_suffix", "suffix"), &EditorSpinSlider::set_suffix);
  557. ClassDB::bind_method(D_METHOD("get_suffix"), &EditorSpinSlider::get_suffix);
  558. ClassDB::bind_method(D_METHOD("set_read_only", "read_only"), &EditorSpinSlider::set_read_only);
  559. ClassDB::bind_method(D_METHOD("is_read_only"), &EditorSpinSlider::is_read_only);
  560. ClassDB::bind_method(D_METHOD("set_flat", "flat"), &EditorSpinSlider::set_flat);
  561. ClassDB::bind_method(D_METHOD("is_flat"), &EditorSpinSlider::is_flat);
  562. ClassDB::bind_method(D_METHOD("set_hide_slider", "hide_slider"), &EditorSpinSlider::set_hide_slider);
  563. ClassDB::bind_method(D_METHOD("is_hiding_slider"), &EditorSpinSlider::is_hiding_slider);
  564. ADD_PROPERTY(PropertyInfo(Variant::STRING, "label"), "set_label", "get_label");
  565. ADD_PROPERTY(PropertyInfo(Variant::STRING, "suffix"), "set_suffix", "get_suffix");
  566. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "read_only"), "set_read_only", "is_read_only");
  567. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flat"), "set_flat", "is_flat");
  568. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_slider"), "set_hide_slider", "is_hiding_slider");
  569. ADD_SIGNAL(MethodInfo("grabbed"));
  570. ADD_SIGNAL(MethodInfo("ungrabbed"));
  571. ADD_SIGNAL(MethodInfo("value_focus_entered"));
  572. ADD_SIGNAL(MethodInfo("value_focus_exited"));
  573. }
  574. void EditorSpinSlider::_ensure_input_popup() {
  575. if (value_input_popup) {
  576. return;
  577. }
  578. value_input_popup = memnew(Control);
  579. add_child(value_input_popup);
  580. value_input = memnew(LineEdit);
  581. value_input->set_focus_mode(FOCUS_CLICK);
  582. value_input_popup->add_child(value_input);
  583. value_input->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  584. value_input_popup->connect("hidden", callable_mp(this, &EditorSpinSlider::_value_input_closed));
  585. value_input->connect("text_submitted", callable_mp(this, &EditorSpinSlider::_value_input_submitted));
  586. value_input->connect("focus_exited", callable_mp(this, &EditorSpinSlider::_value_focus_exited));
  587. value_input->connect("gui_input", callable_mp(this, &EditorSpinSlider::_value_input_gui_input));
  588. if (is_inside_tree()) {
  589. _update_value_input_stylebox();
  590. }
  591. }
  592. EditorSpinSlider::EditorSpinSlider() {
  593. set_focus_mode(FOCUS_ALL);
  594. grabber = memnew(TextureRect);
  595. add_child(grabber);
  596. grabber->hide();
  597. grabber->set_as_top_level(true);
  598. grabber->set_mouse_filter(MOUSE_FILTER_STOP);
  599. grabber->connect("mouse_entered", callable_mp(this, &EditorSpinSlider::_grabber_mouse_entered));
  600. grabber->connect("mouse_exited", callable_mp(this, &EditorSpinSlider::_grabber_mouse_exited));
  601. grabber->connect("gui_input", callable_mp(this, &EditorSpinSlider::_grabber_gui_input));
  602. }