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

Allow aborting SpinSlider value changes #88275

Merged
merged 1 commit into from
Feb 16, 2024
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
78 changes: 57 additions & 21 deletions editor/gui/editor_spin_slider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,27 +68,15 @@ void EditorSpinSlider::gui_input(const Ref<InputEvent> &p_event) {
}
return;
} else {
grabbing_spinner_attempt = true;
grabbing_spinner_dist_cache = 0;
pre_grab_value = get_value();
grabbing_spinner = false;
grabbing_spinner_mouse_pos = get_global_mouse_position();
emit_signal("grabbed");
_grab_start();
}
} else {
if (grabbing_spinner_attempt) {
if (grabbing_spinner) {
Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
Input::get_singleton()->warp_mouse(grabbing_spinner_mouse_pos);
queue_redraw();
emit_signal("ungrabbed");
} else {
_focus_entered();
}

grabbing_spinner = false;
grabbing_spinner_attempt = false;
}
_grab_end();
}
} else if (mb->get_button_index() == MouseButton::RIGHT) {
if (mb->is_pressed() && is_grabbing()) {
_grab_end();
set_value(pre_grab_value);
}
} else if (mb->get_button_index() == MouseButton::WHEEL_UP || mb->get_button_index() == MouseButton::WHEEL_DOWN) {
if (grabber->is_visible()) {
Expand Down Expand Up @@ -142,8 +130,47 @@ void EditorSpinSlider::gui_input(const Ref<InputEvent> &p_event) {
}

Ref<InputEventKey> k = p_event;
if (k.is_valid() && k->is_pressed() && k->is_action("ui_accept", true)) {
_focus_entered();
if (k.is_valid() && k->is_pressed()) {
if (k->is_action("ui_accept", true)) {
_focus_entered();
} else if (is_grabbing()) {
if (k->is_action("ui_cancel", true)) {
_grab_end();
set_value(pre_grab_value);
}
accept_event();
}
}
}

void EditorSpinSlider::_grab_start() {
grabbing_spinner_attempt = true;
grabbing_spinner_dist_cache = 0;
pre_grab_value = get_value();
grabbing_spinner = false;
grabbing_spinner_mouse_pos = get_global_mouse_position();
emit_signal("grabbed");
}

void EditorSpinSlider::_grab_end() {
if (grabbing_spinner_attempt) {
if (grabbing_spinner) {
Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
Input::get_singleton()->warp_mouse(grabbing_spinner_mouse_pos);
queue_redraw();
grabbing_spinner = false;
emit_signal("ungrabbed");
} else {
_focus_entered();
}

grabbing_spinner_attempt = false;
}

if (grabbing_grabber) {
grabbing_grabber = false;
mousewheel_over_grabber = false;
emit_signal("ungrabbed");
}
}

Expand Down Expand Up @@ -173,16 +200,25 @@ void EditorSpinSlider::_grabber_gui_input(const Ref<InputEvent> &p_event) {
if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {
if (mb->is_pressed()) {
grabbing_grabber = true;
pre_grab_value = get_value();
if (!mousewheel_over_grabber) {
grabbing_ratio = get_as_ratio();
grabbing_from = grabber->get_transform().xform(mb->get_position()).x;
}
grab_focus();
emit_signal("grabbed");
} else {
grabbing_grabber = false;
mousewheel_over_grabber = false;
emit_signal("ungrabbed");
}
} else if (mb.is_valid() && mb->get_button_index() == MouseButton::RIGHT) {
if (mb->is_pressed() && grabbing_grabber) {
grabbing_grabber = false;
mousewheel_over_grabber = false;
set_value(pre_grab_value);
emit_signal("ungrabbed");
}
}

Ref<InputEventMouseMotion> mm = p_event;
Expand Down
3 changes: 3 additions & 0 deletions editor/gui/editor_spin_slider.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ class EditorSpinSlider : public Range {
bool hide_slider = false;
bool flat = false;

void _grab_start();
void _grab_end();

void _grabber_gui_input(const Ref<InputEvent> &p_event);
void _value_input_closed();
void _value_input_submitted(const String &);
Expand Down
4 changes: 4 additions & 0 deletions editor/plugins/node_3d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1488,6 +1488,10 @@ Transform3D Node3DEditorViewport::_compute_transform(TransformMode p_mode, const
}

void Node3DEditorViewport::_surface_mouse_enter() {
if (Input::get_singleton()->get_mouse_mode() == Input::MOUSE_MODE_CAPTURED) {
Copy link
Contributor Author

@passivestar passivestar Feb 13, 2024

Choose a reason for hiding this comment

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

When the mouse cursor is captured by another element (i.e a SpinSlider), 3d viewport grabs focus and starts reacting to input. It happens only if it's in the middle of the screen at that time, so must be unintentional

Copy link
Contributor

Choose a reason for hiding this comment

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

I've had this rarely occur even outside of this PR. This sounds good to me on paper.

Copy link
Contributor

Choose a reason for hiding this comment

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

This section is a duplicate of #87642, I can confirm that it works. All good if this PR supersedes it since it does more than mine.

return;
}

if (!surface->has_focus() && (!get_viewport()->gui_get_focus_owner() || !get_viewport()->gui_get_focus_owner()->is_text_field())) {
surface->grab_focus();
}
Expand Down
Loading