editor_spin_slider.cpp 25 KB

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