Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add up/down keys to inc/dec val in editor spin slider [3.x] #53090

Merged
merged 1 commit into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment on lines +212 to +216
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised by your findings on get_alt() vs get_metakey(), that doesn't seem to match other APPLE_STYLE_KEYS checks in the codebase.

But let's merge and see if any macOS user reports unexpected behavior.

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