2
0

editor_spin_slider.cpp 22 KB

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