editor_spin_slider.cpp 23 KB

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