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