Skip to content

Commit

Permalink
Allow to abort SpinSlider value changes
Browse files Browse the repository at this point in the history
  • Loading branch information
passivestar committed Feb 16, 2024
1 parent a9bb850 commit 5689dbc
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 21 deletions.
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) {
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

0 comments on commit 5689dbc

Please sign in to comment.