editor_spin_slider.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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. String EditorSpinSlider::get_tooltip(const Point2 &p_pos) const {
  36. if (grabber->is_visible()) {
  37. #ifdef MACOS_ENABLED
  38. Key key = Key::META;
  39. #else
  40. Key key = Key::CTRL;
  41. #endif
  42. 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));
  43. }
  44. return TS->format_number(rtos(get_value()));
  45. }
  46. String EditorSpinSlider::get_text_value() const {
  47. return TS->format_number(String::num(get_value(), Math::range_step_decimals(get_step())));
  48. }
  49. void EditorSpinSlider::gui_input(const Ref<InputEvent> &p_event) {
  50. ERR_FAIL_COND(p_event.is_null());
  51. if (read_only) {
  52. return;
  53. }
  54. Ref<InputEventMouseButton> mb = p_event;
  55. if (mb.is_valid()) {
  56. if (mb->get_button_index() == MouseButton::LEFT) {
  57. if (mb->is_pressed()) {
  58. if (updown_offset != -1 && mb->get_position().x > updown_offset) {
  59. //there is an updown, so use it.
  60. if (mb->get_position().y < get_size().height / 2) {
  61. set_value(get_value() + get_step());
  62. } else {
  63. set_value(get_value() - get_step());
  64. }
  65. return;
  66. } else {
  67. grabbing_spinner_attempt = true;
  68. grabbing_spinner_dist_cache = 0;
  69. pre_grab_value = get_value();
  70. grabbing_spinner = false;
  71. grabbing_spinner_mouse_pos = get_global_mouse_position();
  72. emit_signal("grabbed");
  73. }
  74. } else {
  75. if (grabbing_spinner_attempt) {
  76. if (grabbing_spinner) {
  77. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  78. Input::get_singleton()->warp_mouse(grabbing_spinner_mouse_pos);
  79. queue_redraw();
  80. emit_signal("ungrabbed");
  81. } else {
  82. _focus_entered();
  83. }
  84. grabbing_spinner = false;
  85. grabbing_spinner_attempt = false;
  86. }
  87. }
  88. } else if (mb->get_button_index() == MouseButton::WHEEL_UP || mb->get_button_index() == MouseButton::WHEEL_DOWN) {
  89. if (grabber->is_visible()) {
  90. call_deferred(SNAME("queue_redraw"));
  91. }
  92. }
  93. }
  94. Ref<InputEventMouseMotion> mm = p_event;
  95. if (mm.is_valid()) {
  96. if (grabbing_spinner_attempt) {
  97. double diff_x = mm->get_relative().x;
  98. if (mm->is_shift_pressed() && grabbing_spinner) {
  99. diff_x *= 0.1;
  100. }
  101. grabbing_spinner_dist_cache += diff_x;
  102. if (!grabbing_spinner && ABS(grabbing_spinner_dist_cache) > 4 * EDSCALE) {
  103. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
  104. grabbing_spinner = true;
  105. }
  106. if (grabbing_spinner) {
  107. // Don't make the user scroll all the way back to 'in range' if they went off the end.
  108. if (pre_grab_value < get_min() && !is_lesser_allowed()) {
  109. pre_grab_value = get_min();
  110. }
  111. if (pre_grab_value > get_max() && !is_greater_allowed()) {
  112. pre_grab_value = get_max();
  113. }
  114. if (mm->is_command_or_control_pressed()) {
  115. // If control was just pressed, don't make the value do a huge jump in magnitude.
  116. if (grabbing_spinner_dist_cache != 0) {
  117. pre_grab_value += grabbing_spinner_dist_cache * get_step();
  118. grabbing_spinner_dist_cache = 0;
  119. }
  120. set_value(Math::round(pre_grab_value + get_step() * grabbing_spinner_dist_cache * 10));
  121. } else {
  122. set_value(pre_grab_value + get_step() * grabbing_spinner_dist_cache);
  123. }
  124. }
  125. } else if (updown_offset != -1) {
  126. bool new_hover = (mm->get_position().x > updown_offset);
  127. if (new_hover != hover_updown) {
  128. hover_updown = new_hover;
  129. queue_redraw();
  130. }
  131. }
  132. }
  133. Ref<InputEventKey> k = p_event;
  134. if (k.is_valid() && k->is_pressed() && k->is_action("ui_accept", true)) {
  135. _focus_entered();
  136. }
  137. }
  138. void EditorSpinSlider::_grabber_gui_input(const Ref<InputEvent> &p_event) {
  139. if (read_only) {
  140. return;
  141. }
  142. Ref<InputEventMouseButton> mb = p_event;
  143. if (is_read_only()) {
  144. return;
  145. }
  146. if (grabbing_grabber) {
  147. if (mb.is_valid()) {
  148. if (mb->get_button_index() == MouseButton::WHEEL_UP) {
  149. set_value(get_value() + get_step());
  150. mousewheel_over_grabber = true;
  151. } else if (mb->get_button_index() == MouseButton::WHEEL_DOWN) {
  152. set_value(get_value() - get_step());
  153. mousewheel_over_grabber = true;
  154. }
  155. }
  156. }
  157. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {
  158. if (mb->is_pressed()) {
  159. grabbing_grabber = true;
  160. if (!mousewheel_over_grabber) {
  161. grabbing_ratio = get_as_ratio();
  162. grabbing_from = grabber->get_transform().xform(mb->get_position()).x;
  163. }
  164. emit_signal("grabbed");
  165. } else {
  166. grabbing_grabber = false;
  167. mousewheel_over_grabber = false;
  168. emit_signal("ungrabbed");
  169. }
  170. }
  171. Ref<InputEventMouseMotion> mm = p_event;
  172. if (mm.is_valid() && grabbing_grabber) {
  173. if (mousewheel_over_grabber) {
  174. return;
  175. }
  176. float scale_x = get_global_transform_with_canvas().get_scale().x;
  177. ERR_FAIL_COND(Math::is_zero_approx(scale_x));
  178. float grabbing_ofs = (grabber->get_transform().xform(mm->get_position()).x - grabbing_from) / float(grabber_range) / scale_x;
  179. set_as_ratio(grabbing_ratio + grabbing_ofs);
  180. queue_redraw();
  181. }
  182. }
  183. void EditorSpinSlider::_value_input_gui_input(const Ref<InputEvent> &p_event) {
  184. Ref<InputEventKey> k = p_event;
  185. if (k.is_valid() && k->is_pressed() && !is_read_only()) {
  186. double step = get_step();
  187. double real_step = step;
  188. if (step < 1) {
  189. double divisor = 1.0 / get_step();
  190. if (trunc(divisor) == divisor) {
  191. step = 1.0;
  192. }
  193. }
  194. if (k->is_ctrl_pressed()) {
  195. step *= 100.0;
  196. } else if (k->is_shift_pressed()) {
  197. step *= 10.0;
  198. } else if (k->is_alt_pressed()) {
  199. step *= 0.1;
  200. }
  201. Key code = k->get_keycode();
  202. switch (code) {
  203. case Key::UP: {
  204. _evaluate_input_text();
  205. double last_value = get_value();
  206. set_value(last_value + step);
  207. double new_value = get_value();
  208. if (new_value < CLAMP(last_value + step, get_min(), get_max())) {
  209. set_value(last_value + real_step);
  210. }
  211. value_input_dirty = true;
  212. set_process_internal(true);
  213. } break;
  214. case Key::DOWN: {
  215. _evaluate_input_text();
  216. double last_value = get_value();
  217. set_value(last_value - step);
  218. double new_value = get_value();
  219. if (new_value > CLAMP(last_value - step, get_min(), get_max())) {
  220. set_value(last_value - real_step);
  221. }
  222. value_input_dirty = true;
  223. set_process_internal(true);
  224. } break;
  225. case Key::ESCAPE: {
  226. value_input_closed_frame = Engine::get_singleton()->get_frames_drawn();
  227. if (value_input_popup) {
  228. value_input_popup->hide();
  229. }
  230. } break;
  231. default:
  232. break;
  233. }
  234. }
  235. }
  236. void EditorSpinSlider::_update_value_input_stylebox() {
  237. if (!value_input) {
  238. return;
  239. }
  240. // Add a left margin to the stylebox to make the number align with the Label
  241. // when it's edited. The LineEdit "focus" stylebox uses the "normal" stylebox's
  242. // default margins.
  243. Ref<StyleBox> stylebox = get_theme_stylebox(SNAME("normal"), SNAME("LineEdit"))->duplicate();
  244. // EditorSpinSliders with a label have more space on the left, so add an
  245. // higher margin to match the location where the text begins.
  246. // The margin values below were determined by empirical testing.
  247. if (is_layout_rtl()) {
  248. stylebox->set_content_margin(SIDE_LEFT, 0);
  249. stylebox->set_content_margin(SIDE_RIGHT, (!get_label().is_empty() ? 23 : 16) * EDSCALE);
  250. } else {
  251. stylebox->set_content_margin(SIDE_LEFT, (!get_label().is_empty() ? 23 : 16) * EDSCALE);
  252. stylebox->set_content_margin(SIDE_RIGHT, 0);
  253. }
  254. value_input->add_theme_style_override("normal", stylebox);
  255. }
  256. void EditorSpinSlider::_draw_spin_slider() {
  257. updown_offset = -1;
  258. RID ci = get_canvas_item();
  259. bool rtl = is_layout_rtl();
  260. Vector2 size = get_size();
  261. Ref<StyleBox> sb = get_theme_stylebox(is_read_only() ? SNAME("read_only") : SNAME("normal"), SNAME("LineEdit"));
  262. if (!flat) {
  263. draw_style_box(sb, Rect2(Vector2(), size));
  264. }
  265. Ref<Font> font = get_theme_font(SNAME("font"), SNAME("LineEdit"));
  266. int font_size = get_theme_font_size(SNAME("font_size"), SNAME("LineEdit"));
  267. int sep_base = 4 * EDSCALE;
  268. int sep = sep_base + sb->get_offset().x; //make it have the same margin on both sides, looks better
  269. int label_width = font->get_string_size(label, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).width;
  270. int number_width = size.width - sb->get_minimum_size().width - label_width - sep;
  271. Ref<Texture2D> updown = get_theme_icon(is_read_only() ? SNAME("updown_disabled") : SNAME("updown"), SNAME("SpinBox"));
  272. String numstr = get_text_value();
  273. int vofs = (size.height - font->get_height(font_size)) / 2 + font->get_ascent(font_size);
  274. Color fc = get_theme_color(is_read_only() ? SNAME("font_uneditable_color") : SNAME("font_color"), SNAME("LineEdit"));
  275. Color lc = get_theme_color(is_read_only() ? SNAME("read_only_label_color") : SNAME("label_color"));
  276. if (flat && !label.is_empty()) {
  277. Ref<StyleBox> label_bg = get_theme_stylebox(SNAME("label_bg"), SNAME("EditorSpinSlider"));
  278. if (rtl) {
  279. 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)));
  280. } else {
  281. draw_style_box(label_bg, Rect2(Vector2(), Vector2(sb->get_offset().x * 2 + label_width, size.height)));
  282. }
  283. }
  284. if (has_focus()) {
  285. Ref<StyleBox> focus = get_theme_stylebox(SNAME("focus"), SNAME("LineEdit"));
  286. draw_style_box(focus, Rect2(Vector2(), size));
  287. }
  288. if (rtl) {
  289. 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));
  290. } else {
  291. 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));
  292. }
  293. int suffix_start = numstr.length();
  294. RID num_rid = TS->create_shaped_text();
  295. TS->shaped_text_add_string(num_rid, numstr + U"\u2009" + suffix, font->get_rids(), font_size, font->get_opentype_features());
  296. for (int i = 0; i < TextServer::SPACING_MAX; i++) {
  297. TS->shaped_text_set_spacing(num_rid, TextServer::SpacingType(i), font->get_spacing(TextServer::SpacingType(i)));
  298. }
  299. float text_start = rtl ? Math::round(sb->get_offset().x) : Math::round(sb->get_offset().x + label_width + sep);
  300. Vector2 text_ofs = rtl ? Vector2(text_start + (number_width - TS->shaped_text_get_width(num_rid)), vofs) : Vector2(text_start, vofs);
  301. int v_size = TS->shaped_text_get_glyph_count(num_rid);
  302. const Glyph *glyphs = TS->shaped_text_get_glyphs(num_rid);
  303. for (int i = 0; i < v_size; i++) {
  304. for (int j = 0; j < glyphs[i].repeat; j++) {
  305. if (text_ofs.x >= text_start && (text_ofs.x + glyphs[i].advance) <= (text_start + number_width)) {
  306. Color color = fc;
  307. if (glyphs[i].start >= suffix_start) {
  308. color.a *= 0.4;
  309. }
  310. if (glyphs[i].font_rid != RID()) {
  311. 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);
  312. } else if ((glyphs[i].flags & TextServer::GRAPHEME_IS_VIRTUAL) != TextServer::GRAPHEME_IS_VIRTUAL) {
  313. 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);
  314. }
  315. }
  316. text_ofs.x += glyphs[i].advance;
  317. }
  318. }
  319. TS->free_rid(num_rid);
  320. if (!hide_slider) {
  321. if (get_step() == 1) {
  322. Ref<Texture2D> updown2 = get_theme_icon(is_read_only() ? SNAME("updown_disabled") : SNAME("updown"), SNAME("SpinBox"));
  323. int updown_vofs = (size.height - updown2->get_height()) / 2;
  324. if (rtl) {
  325. updown_offset = sb->get_margin(SIDE_LEFT);
  326. } else {
  327. updown_offset = size.width - sb->get_margin(SIDE_RIGHT) - updown2->get_width();
  328. }
  329. Color c(1, 1, 1);
  330. if (hover_updown) {
  331. c *= Color(1.2, 1.2, 1.2);
  332. }
  333. draw_texture(updown2, Vector2(updown_offset, updown_vofs), c);
  334. if (grabber->is_visible()) {
  335. grabber->hide();
  336. }
  337. } else {
  338. const int grabber_w = 4 * EDSCALE;
  339. const int width = size.width - sb->get_minimum_size().width - grabber_w;
  340. const int ofs = sb->get_offset().x;
  341. const int svofs = (size.height + vofs) / 2 - 1;
  342. Color c = fc;
  343. // Draw the horizontal slider's background.
  344. c.a = 0.2;
  345. draw_rect(Rect2(ofs, svofs + 1, width, 2 * EDSCALE), c);
  346. // Draw the horizontal slider's filled part on the left.
  347. const int gofs = get_as_ratio() * width;
  348. c.a = 0.45;
  349. draw_rect(Rect2(ofs, svofs + 1, gofs, 2 * EDSCALE), c);
  350. // Draw the horizontal slider's grabber.
  351. c.a = 0.9;
  352. const Rect2 grabber_rect = Rect2(ofs + gofs, svofs, grabber_w, 4 * EDSCALE);
  353. draw_rect(grabber_rect, c);
  354. grabbing_spinner_mouse_pos = get_global_position() + grabber_rect.get_center();
  355. bool display_grabber = (grabbing_grabber || mouse_over_spin || mouse_over_grabber) && !grabbing_spinner && !(value_input_popup && value_input_popup->is_visible());
  356. if (grabber->is_visible() != display_grabber) {
  357. if (display_grabber) {
  358. grabber->show();
  359. } else {
  360. grabber->hide();
  361. }
  362. }
  363. if (display_grabber) {
  364. Ref<Texture2D> grabber_tex;
  365. if (mouse_over_grabber) {
  366. grabber_tex = get_theme_icon(SNAME("grabber_highlight"), SNAME("HSlider"));
  367. } else {
  368. grabber_tex = get_theme_icon(SNAME("grabber"), SNAME("HSlider"));
  369. }
  370. if (grabber->get_texture() != grabber_tex) {
  371. grabber->set_texture(grabber_tex);
  372. }
  373. Vector2 scale = get_global_transform_with_canvas().get_scale();
  374. grabber->set_scale(scale);
  375. grabber->reset_size();
  376. grabber->set_position(get_global_position() + (grabber_rect.get_center() - grabber->get_size() * 0.5) * scale);
  377. if (mousewheel_over_grabber) {
  378. Input::get_singleton()->warp_mouse(grabber->get_position() + grabber_rect.size);
  379. }
  380. grabber_range = width;
  381. }
  382. }
  383. }
  384. }
  385. void EditorSpinSlider::_notification(int p_what) {
  386. switch (p_what) {
  387. case NOTIFICATION_ENTER_TREE:
  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. }