editor_spin_slider.cpp 24 KB

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