Skip to content

Commit

Permalink
Merge pull request #53090 from Razoric480/key_up_down_spin_slider_32
Browse files Browse the repository at this point in the history
  • Loading branch information
akien-mga authored Oct 5, 2021
2 parents d82c75a + d43a485 commit 86ee82c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
63 changes: 63 additions & 0 deletions editor/editor_spin_slider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,67 @@ void EditorSpinSlider::_grabber_gui_input(const Ref<InputEvent> &p_event) {
}
}

void EditorSpinSlider::_value_input_gui_input(const Ref<InputEvent> &p_event) {
Ref<InputEventKey> 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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions editor/editor_spin_slider.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<InputEvent> &p_event);
bool hide_slider;
bool flat;

Expand Down

0 comments on commit 86ee82c

Please sign in to comment.