editor_spin_slider.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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. double real_step = step;
  185. if (step < 1) {
  186. double divisor = 1.0 / get_step();
  187. if (trunc(divisor) == divisor) {
  188. step = 1.0;
  189. }
  190. }
  191. if (k->is_ctrl_pressed()) {
  192. step *= 100.0;
  193. } else if (k->is_shift_pressed()) {
  194. step *= 10.0;
  195. } else if (k->is_alt_pressed()) {
  196. step *= 0.1;
  197. }
  198. Key code = k->get_keycode();
  199. switch (code) {
  200. case Key::UP: {
  201. _evaluate_input_text();
  202. double last_value = get_value();
  203. set_value(last_value + step);
  204. double new_value = get_value();
  205. if (new_value < CLAMP(last_value + step, get_min(), get_max())) {
  206. set_value(last_value + real_step);
  207. }
  208. value_input_dirty = true;
  209. set_process_internal(true);
  210. } break;
  211. case Key::DOWN: {
  212. _evaluate_input_text();
  213. double last_value = get_value();
  214. set_value(last_value - step);
  215. double new_value = get_value();
  216. if (new_value > CLAMP(last_value - step, get_min(), get_max())) {
  217. set_value(last_value - real_step);
  218. }
  219. value_input_dirty = true;
  220. set_process_internal(true);
  221. } break;
  222. case Key::ESCAPE: {
  223. value_input_closed_frame = Engine::get_singleton()->get_frames_drawn();
  224. if (value_input_popup) {
  225. value_input_popup->hide();
  226. }
  227. } break;
  228. default:
  229. break;
  230. }
  231. }
  232. }
  233. void EditorSpinSlider::_update_value_input_stylebox() {
  234. if (!value_input) {
  235. return;
  236. }
  237. // Add a left margin to the stylebox to make the number align with the Label
  238. // when it's edited. The LineEdit "focus" stylebox uses the "normal" stylebox's
  239. // default margins.
  240. Ref<StyleBox> stylebox = get_theme_stylebox(SNAME("normal"), SNAME("LineEdit"))->duplicate();
  241. // EditorSpinSliders with a label have more space on the left, so add an
  242. // higher margin to match the location where the text begins.
  243. // The margin values below were determined by empirical testing.
  244. if (is_layout_rtl()) {
  245. stylebox->set_content_margin(SIDE_LEFT, 0);
  246. stylebox->set_content_margin(SIDE_RIGHT, (!get_label().is_empty() ? 23 : 16) * EDSCALE);
  247. } else {
  248. stylebox->set_content_margin(SIDE_LEFT, (!get_label().is_empty() ? 23 : 16) * EDSCALE);
  249. stylebox->set_content_margin(SIDE_RIGHT, 0);
  250. }
  251. value_input->add_theme_style_override("normal", stylebox);
  252. }
  253. void EditorSpinSlider::_draw_spin_slider() {
  254. updown_offset = -1;
  255. RID ci = get_canvas_item();
  256. bool rtl = is_layout_rtl();
  257. Vector2 size = get_size();
  258. Ref<StyleBox> sb = get_theme_stylebox(is_read_only() ? SNAME("read_only") : SNAME("normal"), SNAME("LineEdit"));
  259. if (!flat) {
  260. draw_style_box(sb, Rect2(Vector2(), size));
  261. }
  262. Ref<Font> font = get_theme_font(SNAME("font"), SNAME("LineEdit"));
  263. int font_size = get_theme_font_size(SNAME("font_size"), SNAME("LineEdit"));
  264. int sep_base = 4 * EDSCALE;
  265. int sep = sep_base + sb->get_offset().x; //make it have the same margin on both sides, looks better
  266. int label_width = font->get_string_size(label, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).width;
  267. int number_width = size.width - sb->get_minimum_size().width - label_width - sep;
  268. Ref<Texture2D> updown = get_theme_icon(is_read_only() ? SNAME("updown_disabled") : SNAME("updown"), SNAME("SpinBox"));
  269. String numstr = get_text_value();
  270. int vofs = (size.height - font->get_height(font_size)) / 2 + font->get_ascent(font_size);
  271. Color fc = get_theme_color(is_read_only() ? SNAME("font_uneditable_color") : SNAME("font_color"), SNAME("LineEdit"));
  272. Color lc = get_theme_color(is_read_only() ? SNAME("read_only_label_color") : SNAME("label_color"));
  273. if (flat && !label.is_empty()) {
  274. Ref<StyleBox> label_bg = get_theme_stylebox(SNAME("label_bg"), SNAME("EditorSpinSlider"));
  275. if (rtl) {
  276. 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)));
  277. } else {
  278. draw_style_box(label_bg, Rect2(Vector2(), Vector2(sb->get_offset().x * 2 + label_width, size.height)));
  279. }
  280. }
  281. if (has_focus()) {
  282. Ref<StyleBox> focus = get_theme_stylebox(SNAME("focus"), SNAME("LineEdit"));
  283. draw_style_box(focus, Rect2(Vector2(), size));
  284. }
  285. if (rtl) {
  286. 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));
  287. } else {
  288. 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));
  289. }
  290. int suffix_start = numstr.length();
  291. RID num_rid = TS->create_shaped_text();
  292. TS->shaped_text_add_string(num_rid, numstr + U"\u2009" + suffix, font->get_rids(), font_size, font->get_opentype_features());
  293. for (int i = 0; i < TextServer::SPACING_MAX; i++) {
  294. TS->shaped_text_set_spacing(num_rid, TextServer::SpacingType(i), font->get_spacing(TextServer::SpacingType(i)));
  295. }
  296. float text_start = rtl ? Math::round(sb->get_offset().x) : Math::round(sb->get_offset().x + label_width + sep);
  297. Vector2 text_ofs = rtl ? Vector2(text_start + (number_width - TS->shaped_text_get_width(num_rid)), vofs) : Vector2(text_start, vofs);
  298. int v_size = TS->shaped_text_get_glyph_count(num_rid);
  299. const Glyph *glyphs = TS->shaped_text_get_glyphs(num_rid);
  300. for (int i = 0; i < v_size; i++) {
  301. for (int j = 0; j < glyphs[i].repeat; j++) {
  302. if (text_ofs.x >= text_start && (text_ofs.x + glyphs[i].advance) <= (text_start + number_width)) {
  303. Color color = fc;
  304. if (glyphs[i].start >= suffix_start) {
  305. color.a *= 0.4;
  306. }
  307. if (glyphs[i].font_rid != RID()) {
  308. 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);
  309. } else if ((glyphs[i].flags & TextServer::GRAPHEME_IS_VIRTUAL) != TextServer::GRAPHEME_IS_VIRTUAL) {
  310. 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);
  311. }
  312. }
  313. text_ofs.x += glyphs[i].advance;
  314. }
  315. }
  316. TS->free_rid(num_rid);
  317. if (!hide_slider) {
  318. if (get_step() == 1) {
  319. Ref<Texture2D> updown2 = get_theme_icon(is_read_only() ? SNAME("updown_disabled") : SNAME("updown"), SNAME("SpinBox"));
  320. int updown_vofs = (size.height - updown2->get_height()) / 2;
  321. if (rtl) {
  322. updown_offset = sb->get_margin(SIDE_LEFT);
  323. } else {
  324. updown_offset = size.width - sb->get_margin(SIDE_RIGHT) - updown2->get_width();
  325. }
  326. Color c(1, 1, 1);
  327. if (hover_updown) {
  328. c *= Color(1.2, 1.2, 1.2);
  329. }
  330. draw_texture(updown2, Vector2(updown_offset, updown_vofs), c);
  331. if (grabber->is_visible()) {
  332. grabber->hide();
  333. }
  334. } else {
  335. const int grabber_w = 4 * EDSCALE;
  336. const int width = size.width - sb->get_minimum_size().width - grabber_w;
  337. const int ofs = sb->get_offset().x;
  338. const int svofs = (size.height + vofs) / 2 - 1;
  339. Color c = fc;
  340. // Draw the horizontal slider's background.
  341. c.a = 0.2;
  342. draw_rect(Rect2(ofs, svofs + 1, width, 2 * EDSCALE), c);
  343. // Draw the horizontal slider's filled part on the left.
  344. const int gofs = get_as_ratio() * width;
  345. c.a = 0.45;
  346. draw_rect(Rect2(ofs, svofs + 1, gofs, 2 * EDSCALE), c);
  347. // Draw the horizontal slider's grabber.
  348. c.a = 0.9;
  349. const Rect2 grabber_rect = Rect2(ofs + gofs, svofs, grabber_w, 4 * EDSCALE);
  350. draw_rect(grabber_rect, c);
  351. grabbing_spinner_mouse_pos = get_global_position() + grabber_rect.get_center();
  352. bool display_grabber = (grabbing_grabber || mouse_over_spin || mouse_over_grabber) && !grabbing_spinner && !(value_input_popup && value_input_popup->is_visible());
  353. if (grabber->is_visible() != display_grabber) {
  354. if (display_grabber) {
  355. grabber->show();
  356. } else {
  357. grabber->hide();
  358. }
  359. }
  360. if (display_grabber) {
  361. Ref<Texture2D> grabber_tex;
  362. if (mouse_over_grabber) {
  363. grabber_tex = get_theme_icon(SNAME("grabber_highlight"), SNAME("HSlider"));
  364. } else {
  365. grabber_tex = get_theme_icon(SNAME("grabber"), SNAME("HSlider"));
  366. }
  367. if (grabber->get_texture() != grabber_tex) {
  368. grabber->set_texture(grabber_tex);
  369. }
  370. Vector2 scale = get_global_transform_with_canvas().get_scale();
  371. grabber->set_scale(scale);
  372. grabber->reset_size();
  373. grabber->set_position(get_global_position() + (grabber_rect.get_center() - grabber->get_size() * 0.5) * scale);
  374. if (mousewheel_over_grabber) {
  375. Input::get_singleton()->warp_mouse(grabber->get_position() + grabber_rect.size);
  376. }
  377. grabber_range = width;
  378. }
  379. }
  380. }
  381. }
  382. void EditorSpinSlider::_notification(int p_what) {
  383. switch (p_what) {
  384. case NOTIFICATION_ENTER_TREE: {
  385. grabbing_spinner_speed = EditorSettings::get_singleton()->get("interface/inspector/float_drag_speed");
  386. _update_value_input_stylebox();
  387. } break;
  388. case NOTIFICATION_THEME_CHANGED: {
  389. _update_value_input_stylebox();
  390. } break;
  391. case NOTIFICATION_INTERNAL_PROCESS: {
  392. if (value_input_dirty) {
  393. value_input_dirty = false;
  394. value_input->set_text(get_text_value());
  395. }
  396. set_process_internal(false);
  397. } break;
  398. case NOTIFICATION_DRAW: {
  399. _draw_spin_slider();
  400. } break;
  401. case NOTIFICATION_WM_WINDOW_FOCUS_IN:
  402. case NOTIFICATION_WM_WINDOW_FOCUS_OUT:
  403. case NOTIFICATION_WM_CLOSE_REQUEST:
  404. case NOTIFICATION_EXIT_TREE: {
  405. if (grabbing_spinner) {
  406. grabber->hide();
  407. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  408. Input::get_singleton()->warp_mouse(grabbing_spinner_mouse_pos);
  409. grabbing_spinner = false;
  410. grabbing_spinner_attempt = false;
  411. }
  412. } break;
  413. case NOTIFICATION_MOUSE_ENTER: {
  414. mouse_over_spin = true;
  415. queue_redraw();
  416. } break;
  417. case NOTIFICATION_MOUSE_EXIT: {
  418. mouse_over_spin = false;
  419. queue_redraw();
  420. } break;
  421. case NOTIFICATION_FOCUS_ENTER: {
  422. 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()) {
  423. _focus_entered();
  424. }
  425. value_input_closed_frame = 0;
  426. } break;
  427. }
  428. }
  429. LineEdit *EditorSpinSlider::get_line_edit() {
  430. _ensure_input_popup();
  431. return value_input;
  432. }
  433. Size2 EditorSpinSlider::get_minimum_size() const {
  434. Ref<StyleBox> sb = get_theme_stylebox(SNAME("normal"), SNAME("LineEdit"));
  435. Ref<Font> font = get_theme_font(SNAME("font"), SNAME("LineEdit"));
  436. int font_size = get_theme_font_size(SNAME("font_size"), SNAME("LineEdit"));
  437. Size2 ms = sb->get_minimum_size();
  438. ms.height += font->get_height(font_size);
  439. return ms;
  440. }
  441. void EditorSpinSlider::set_hide_slider(bool p_hide) {
  442. hide_slider = p_hide;
  443. queue_redraw();
  444. }
  445. bool EditorSpinSlider::is_hiding_slider() const {
  446. return hide_slider;
  447. }
  448. void EditorSpinSlider::set_label(const String &p_label) {
  449. label = p_label;
  450. queue_redraw();
  451. }
  452. String EditorSpinSlider::get_label() const {
  453. return label;
  454. }
  455. void EditorSpinSlider::set_suffix(const String &p_suffix) {
  456. suffix = p_suffix;
  457. queue_redraw();
  458. }
  459. String EditorSpinSlider::get_suffix() const {
  460. return suffix;
  461. }
  462. void EditorSpinSlider::_evaluate_input_text() {
  463. // Replace comma with dot to support it as decimal separator (GH-6028).
  464. // This prevents using functions like `pow()`, but using functions
  465. // in EditorSpinSlider is a barely known (and barely used) feature.
  466. // Instead, we'd rather support German/French keyboard layouts out of the box.
  467. const String text = TS->parse_number(value_input->get_text().replace(",", "."));
  468. Ref<Expression> expr;
  469. expr.instantiate();
  470. Error err = expr->parse(text);
  471. if (err != OK) {
  472. return;
  473. }
  474. Variant v = expr->execute(Array(), nullptr, false, true);
  475. if (v.get_type() == Variant::NIL) {
  476. return;
  477. }
  478. set_value(v);
  479. }
  480. //text_submitted signal
  481. void EditorSpinSlider::_value_input_submitted(const String &p_text) {
  482. value_input_closed_frame = Engine::get_singleton()->get_frames_drawn();
  483. if (value_input_popup) {
  484. value_input_popup->hide();
  485. }
  486. }
  487. //modal_closed signal
  488. void EditorSpinSlider::_value_input_closed() {
  489. _evaluate_input_text();
  490. value_input_closed_frame = Engine::get_singleton()->get_frames_drawn();
  491. }
  492. //focus_exited signal
  493. void EditorSpinSlider::_value_focus_exited() {
  494. // discontinue because the focus_exit was caused by right-click context menu
  495. if (value_input->is_menu_visible()) {
  496. return;
  497. }
  498. if (is_read_only()) {
  499. // Spin slider has become read only while it was being edited.
  500. return;
  501. }
  502. _evaluate_input_text();
  503. // focus is not on the same element after the value_input was exited
  504. // -> focus is on next element
  505. // -> TAB was pressed
  506. // -> modal_close was not called
  507. // -> need to close/hide manually
  508. if (value_input_closed_frame != Engine::get_singleton()->get_frames_drawn()) {
  509. if (value_input_popup) {
  510. value_input_popup->hide();
  511. }
  512. //tab was pressed
  513. } else {
  514. //enter, click, esc
  515. grab_focus();
  516. }
  517. emit_signal("value_focus_exited");
  518. }
  519. void EditorSpinSlider::_grabber_mouse_entered() {
  520. mouse_over_grabber = true;
  521. queue_redraw();
  522. }
  523. void EditorSpinSlider::_grabber_mouse_exited() {
  524. mouse_over_grabber = false;
  525. queue_redraw();
  526. }
  527. void EditorSpinSlider::set_read_only(bool p_enable) {
  528. read_only = p_enable;
  529. if (read_only && value_input) {
  530. value_input->release_focus();
  531. }
  532. queue_redraw();
  533. }
  534. bool EditorSpinSlider::is_read_only() const {
  535. return read_only;
  536. }
  537. void EditorSpinSlider::set_flat(bool p_enable) {
  538. flat = p_enable;
  539. queue_redraw();
  540. }
  541. bool EditorSpinSlider::is_flat() const {
  542. return flat;
  543. }
  544. bool EditorSpinSlider::is_grabbing() const {
  545. return grabbing_grabber || grabbing_spinner;
  546. }
  547. void EditorSpinSlider::_focus_entered() {
  548. _ensure_input_popup();
  549. value_input->set_text(get_text_value());
  550. value_input_popup->set_size(get_size());
  551. value_input_popup->call_deferred(SNAME("show"));
  552. value_input->call_deferred(SNAME("grab_focus"));
  553. value_input->call_deferred(SNAME("select_all"));
  554. value_input->set_focus_next(find_next_valid_focus()->get_path());
  555. value_input->set_focus_previous(find_prev_valid_focus()->get_path());
  556. emit_signal("value_focus_entered");
  557. }
  558. void EditorSpinSlider::_bind_methods() {
  559. ClassDB::bind_method(D_METHOD("set_label", "label"), &EditorSpinSlider::set_label);
  560. ClassDB::bind_method(D_METHOD("get_label"), &EditorSpinSlider::get_label);
  561. ClassDB::bind_method(D_METHOD("set_suffix", "suffix"), &EditorSpinSlider::set_suffix);
  562. ClassDB::bind_method(D_METHOD("get_suffix"), &EditorSpinSlider::get_suffix);
  563. ClassDB::bind_method(D_METHOD("set_read_only", "read_only"), &EditorSpinSlider::set_read_only);
  564. ClassDB::bind_method(D_METHOD("is_read_only"), &EditorSpinSlider::is_read_only);
  565. ClassDB::bind_method(D_METHOD("set_flat", "flat"), &EditorSpinSlider::set_flat);
  566. ClassDB::bind_method(D_METHOD("is_flat"), &EditorSpinSlider::is_flat);
  567. ClassDB::bind_method(D_METHOD("set_hide_slider", "hide_slider"), &EditorSpinSlider::set_hide_slider);
  568. ClassDB::bind_method(D_METHOD("is_hiding_slider"), &EditorSpinSlider::is_hiding_slider);
  569. ADD_PROPERTY(PropertyInfo(Variant::STRING, "label"), "set_label", "get_label");
  570. ADD_PROPERTY(PropertyInfo(Variant::STRING, "suffix"), "set_suffix", "get_suffix");
  571. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "read_only"), "set_read_only", "is_read_only");
  572. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flat"), "set_flat", "is_flat");
  573. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_slider"), "set_hide_slider", "is_hiding_slider");
  574. ADD_SIGNAL(MethodInfo("grabbed"));
  575. ADD_SIGNAL(MethodInfo("ungrabbed"));
  576. ADD_SIGNAL(MethodInfo("value_focus_entered"));
  577. ADD_SIGNAL(MethodInfo("value_focus_exited"));
  578. }
  579. void EditorSpinSlider::_ensure_input_popup() {
  580. if (value_input_popup) {
  581. return;
  582. }
  583. value_input_popup = memnew(Control);
  584. add_child(value_input_popup);
  585. value_input = memnew(LineEdit);
  586. value_input->set_focus_mode(FOCUS_CLICK);
  587. value_input_popup->add_child(value_input);
  588. value_input->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  589. value_input_popup->connect("hidden", callable_mp(this, &EditorSpinSlider::_value_input_closed));
  590. value_input->connect("text_submitted", callable_mp(this, &EditorSpinSlider::_value_input_submitted));
  591. value_input->connect("focus_exited", callable_mp(this, &EditorSpinSlider::_value_focus_exited));
  592. value_input->connect("gui_input", callable_mp(this, &EditorSpinSlider::_value_input_gui_input));
  593. if (is_inside_tree()) {
  594. _update_value_input_stylebox();
  595. }
  596. }
  597. EditorSpinSlider::EditorSpinSlider() {
  598. set_focus_mode(FOCUS_ALL);
  599. grabber = memnew(TextureRect);
  600. add_child(grabber);
  601. grabber->hide();
  602. grabber->set_as_top_level(true);
  603. grabber->set_mouse_filter(MOUSE_FILTER_STOP);
  604. grabber->connect("mouse_entered", callable_mp(this, &EditorSpinSlider::_grabber_mouse_entered));
  605. grabber->connect("mouse_exited", callable_mp(this, &EditorSpinSlider::_grabber_mouse_exited));
  606. grabber->connect("gui_input", callable_mp(this, &EditorSpinSlider::_grabber_gui_input));
  607. }