summaryrefslogtreecommitdiff
path: root/editor
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <remi@verschelde.fr>2021-06-18 12:28:35 +0200
committerGitHub <noreply@github.com>2021-06-18 12:28:35 +0200
commit43755eada4a9fbc5e7aabb03a6b1aa3ab3213826 (patch)
tree170f3138c5e91fe11e040d789458d48594793a67 /editor
parentf6756e2bf57d517f423618f46dcaf0d8b1d2797b (diff)
parent5e8eb20ad60e35380eef5e3a7fed1e7955c7d6c4 (diff)
Merge pull request #47802 from Calinou/editor-sprite-frames-select-hold-mouse
Allow selecting frames by holding down the mouse in SpriteFrames editor
Diffstat (limited to 'editor')
-rw-r--r--editor/plugins/sprite_frames_editor_plugin.cpp54
-rw-r--r--editor/plugins/sprite_frames_editor_plugin.h1
2 files changed, 47 insertions, 8 deletions
diff --git a/editor/plugins/sprite_frames_editor_plugin.cpp b/editor/plugins/sprite_frames_editor_plugin.cpp
index a5a3d624ec..af72f59c1c 100644
--- a/editor/plugins/sprite_frames_editor_plugin.cpp
+++ b/editor/plugins/sprite_frames_editor_plugin.cpp
@@ -103,17 +103,16 @@ void SpriteFramesEditor::_sheet_preview_draw() {
}
void SpriteFramesEditor::_sheet_preview_input(const Ref<InputEvent> &p_event) {
- Ref<InputEventMouseButton> mb = p_event;
-
+ const Ref<InputEventMouseButton> mb = p_event;
if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MOUSE_BUTTON_LEFT) {
- Size2i size = split_sheet_preview->get_size();
- int h = split_sheet_h->get_value();
- int v = split_sheet_v->get_value();
+ const Size2i size = split_sheet_preview->get_size();
+ const int h = split_sheet_h->get_value();
+ const int v = split_sheet_v->get_value();
- int x = CLAMP(int(mb->get_position().x) * h / size.width, 0, h - 1);
- int y = CLAMP(int(mb->get_position().y) * v / size.height, 0, v - 1);
+ const int x = CLAMP(int(mb->get_position().x) * h / size.width, 0, h - 1);
+ const int y = CLAMP(int(mb->get_position().y) * v / size.height, 0, v - 1);
- int idx = h * y + x;
+ const int idx = h * y + x;
if (mb->is_shift_pressed() && last_frame_selected >= 0) {
//select multiple
@@ -124,6 +123,9 @@ void SpriteFramesEditor::_sheet_preview_input(const Ref<InputEvent> &p_event) {
}
for (int i = from; i <= to; i++) {
+ // Prevent double-toggling the same frame when moving the mouse when the mouse button is still held.
+ frames_toggled_by_mouse_hover.insert(idx);
+
if (mb->is_ctrl_pressed()) {
frames_selected.erase(i);
} else {
@@ -131,6 +133,9 @@ void SpriteFramesEditor::_sheet_preview_input(const Ref<InputEvent> &p_event) {
}
}
} else {
+ // Prevent double-toggling the same frame when moving the mouse when the mouse button is still held.
+ frames_toggled_by_mouse_hover.insert(idx);
+
if (frames_selected.has(idx)) {
frames_selected.erase(idx);
} else {
@@ -141,6 +146,39 @@ void SpriteFramesEditor::_sheet_preview_input(const Ref<InputEvent> &p_event) {
last_frame_selected = idx;
split_sheet_preview->update();
}
+
+ if (mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == MOUSE_BUTTON_LEFT) {
+ frames_toggled_by_mouse_hover.clear();
+ }
+
+ const Ref<InputEventMouseMotion> mm = p_event;
+ if (mm.is_valid() && mm->get_button_mask() & MOUSE_BUTTON_MASK_LEFT) {
+ // Select by holding down the mouse button on frames.
+ const Size2i size = split_sheet_preview->get_size();
+ const int h = split_sheet_h->get_value();
+ const int v = split_sheet_v->get_value();
+
+ const int x = CLAMP(int(mm->get_position().x) * h / size.width, 0, h - 1);
+ const int y = CLAMP(int(mm->get_position().y) * v / size.height, 0, v - 1);
+
+ const int idx = h * y + x;
+
+ if (!frames_toggled_by_mouse_hover.has(idx)) {
+ // Only allow toggling each tile once per mouse hold.
+ // Otherwise, the selection would constantly "flicker" in and out when moving the mouse cursor.
+ // The mouse button must be released before it can be toggled again.
+ frames_toggled_by_mouse_hover.insert(idx);
+
+ if (frames_selected.has(idx)) {
+ frames_selected.erase(idx);
+ } else {
+ frames_selected.insert(idx);
+ }
+
+ last_frame_selected = idx;
+ split_sheet_preview->update();
+ }
+ }
}
void SpriteFramesEditor::_sheet_scroll_input(const Ref<InputEvent> &p_event) {
diff --git a/editor/plugins/sprite_frames_editor_plugin.h b/editor/plugins/sprite_frames_editor_plugin.h
index 77cdbb4af6..e6c59e3533 100644
--- a/editor/plugins/sprite_frames_editor_plugin.h
+++ b/editor/plugins/sprite_frames_editor_plugin.h
@@ -87,6 +87,7 @@ class SpriteFramesEditor : public HSplitContainer {
Button *split_sheet_zoom_in;
EditorFileDialog *file_split_sheet;
Set<int> frames_selected;
+ Set<int> frames_toggled_by_mouse_hover;
int last_frame_selected;
float scale_ratio;