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