From d43a485e51ba2230bffde5448e9d7f868fdc46d2 Mon Sep 17 00:00:00 2001 From: Francois Belair Date: Mon, 7 Sep 2020 11:07:02 -0400 Subject: [PATCH] Add up/down keys to inc/dec val in spin slider Back ported to 3.2. --- editor/editor_spin_slider.cpp | 63 +++++++++++++++++++++++++++++++++++ editor/editor_spin_slider.h | 1 + 2 files changed, 64 insertions(+) diff --git a/editor/editor_spin_slider.cpp b/editor/editor_spin_slider.cpp index 8e23de6f7c28..aca5dd4c3f03 100644 --- a/editor/editor_spin_slider.cpp +++ b/editor/editor_spin_slider.cpp @@ -188,6 +188,67 @@ void EditorSpinSlider::_grabber_gui_input(const Ref &p_event) { } } +void EditorSpinSlider::_value_input_gui_input(const Ref &p_event) { + Ref k = p_event; + if (k.is_valid() && k->is_pressed()) { + double step = get_step(); + double real_step = step; + if (step < 1) { + double divisor = 1.0 / get_step(); + + if (trunc(divisor) == divisor) { + step = 1.0; + } + } + +#ifdef APPLE_STYLE_KEYS + if (k->get_command()) { +#else + if (k->get_control()) { +#endif + step *= 100.0; + } else if (k->get_shift()) { + step *= 10.0; +#ifdef APPLE_STYLE_KEYS + } else if (k->get_metakey()) { +#else + } else if (k->get_alt()) { +#endif + step *= 0.1; + } + + uint32_t code = k->get_scancode(); + switch (code) { + case KEY_UP: { + _evaluate_input_text(); + + double last_value = get_value(); + set_value(last_value + step); + double new_value = get_value(); + + if (new_value < CLAMP(last_value + step, get_min(), get_max())) { + set_value(last_value + real_step); + } + + value_input->set_text(get_text_value()); + } break; + case KEY_DOWN: { + _evaluate_input_text(); + + double last_value = get_value(); + set_value(last_value - step); + double new_value = get_value(); + + if (new_value > CLAMP(last_value - step, get_min(), get_max())) { + set_value(last_value - real_step); + } + + value_input->set_text(get_text_value()); + } break; + } + } +} + void EditorSpinSlider::_draw_spin_slider() { updown_offset = -1; @@ -485,6 +546,7 @@ void EditorSpinSlider::_bind_methods() { ClassDB::bind_method(D_METHOD("is_flat"), &EditorSpinSlider::is_flat); ClassDB::bind_method(D_METHOD("_gui_input"), &EditorSpinSlider::_gui_input); + ClassDB::bind_method(D_METHOD("_value_input_gui_input"), &EditorSpinSlider::_value_input_gui_input); ClassDB::bind_method(D_METHOD("_grabber_mouse_entered"), &EditorSpinSlider::_grabber_mouse_entered); ClassDB::bind_method(D_METHOD("_grabber_mouse_exited"), &EditorSpinSlider::_grabber_mouse_exited); ClassDB::bind_method(D_METHOD("_grabber_gui_input"), &EditorSpinSlider::_grabber_gui_input); @@ -526,6 +588,7 @@ EditorSpinSlider::EditorSpinSlider() { value_input->connect("modal_closed", this, "_value_input_closed"); value_input->connect("text_entered", this, "_value_input_entered"); value_input->connect("focus_exited", this, "_value_focus_exited"); + value_input->connect("gui_input", this, "_value_input_gui_input"); value_input_just_closed = false; hide_slider = false; read_only = false; diff --git a/editor/editor_spin_slider.h b/editor/editor_spin_slider.h index d67daf398e37..fae52ea661ec 100644 --- a/editor/editor_spin_slider.h +++ b/editor/editor_spin_slider.h @@ -69,6 +69,7 @@ class EditorSpinSlider : public Range { void _value_input_closed(); void _value_input_entered(const String &); void _value_focus_exited(); + void _value_input_gui_input(const Ref &p_event); bool hide_slider; bool flat;