From 74bfe88267263297b43fef35fd9f32a91750d4d2 Mon Sep 17 00:00:00 2001 From: kobewi Date: Wed, 19 Jan 2022 19:59:12 +0100 Subject: [PATCH] Add ViewPanner to 2D editor --- editor/animation_bezier_editor.cpp | 42 ++--- editor/animation_bezier_editor.h | 2 +- editor/animation_track_editor.cpp | 14 +- editor/animation_track_editor.h | 4 +- editor/editor_node.cpp | 7 +- editor/editor_settings.cpp | 13 +- .../animation_blend_tree_editor_plugin.cpp | 4 +- editor/plugins/canvas_item_editor_plugin.cpp | 178 ++++++------------ editor/plugins/canvas_item_editor_plugin.h | 10 +- editor/plugins/polygon_2d_editor_plugin.cpp | 13 +- editor/plugins/polygon_2d_editor_plugin.h | 2 +- .../plugins/texture_region_editor_plugin.cpp | 46 +++-- editor/plugins/texture_region_editor_plugin.h | 7 + editor/plugins/tiles/tile_atlas_view.cpp | 13 +- editor/plugins/tiles/tile_atlas_view.h | 2 +- .../plugins/visual_shader_editor_plugin.cpp | 4 +- .../editor/visual_script_editor.cpp | 4 +- scene/gui/graph_edit.cpp | 18 +- scene/gui/graph_edit.h | 5 +- scene/gui/view_panner.cpp | 99 ++++++---- scene/gui/view_panner.h | 20 +- 21 files changed, 263 insertions(+), 244 deletions(-) diff --git a/editor/animation_bezier_editor.cpp b/editor/animation_bezier_editor.cpp index 36786425218..2cf3b1c2192 100644 --- a/editor/animation_bezier_editor.cpp +++ b/editor/animation_bezier_editor.cpp @@ -218,7 +218,7 @@ void AnimationBezierTrackEdit::_draw_line_clipped(const Vector2 &p_from, const V void AnimationBezierTrackEdit::_notification(int p_what) { if (p_what == NOTIFICATION_ENTER_TREE || p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) { - panner->set_control_scheme((ViewPanner::ControlScheme)EDITOR_GET("interface/editors/animation_editors_panning_scheme").operator int()); + panner->setup((ViewPanner::ControlScheme)EDITOR_GET("interface/editors/animation_editors_panning_scheme").operator int(), ED_GET_SHORTCUT("canvas_item_editor/pan_view"), bool(EditorSettings::get_singleton()->get("editors/panning/simple_panning"))); } if (p_what == NOTIFICATION_THEME_CHANGED || p_what == NOTIFICATION_ENTER_TREE) { close_button->set_icon(get_theme_icon(SNAME("Close"), SNAME("EditorIcons"))); @@ -632,27 +632,6 @@ void AnimationBezierTrackEdit::gui_input(const Ref &p_event) { } Ref mb = p_event; - if (mb.is_valid() && mb->is_pressed() && mb->is_alt_pressed()) { - // Alternate zoom (doesn't affect timeline). - if (mb->get_button_index() == MouseButton::WHEEL_DOWN) { - const float v_zoom_orig = v_zoom; - if (v_zoom < 100000) { - v_zoom *= 1.2; - } - v_scroll = v_scroll + (mb->get_position().y - get_size().y / 2) * (v_zoom - v_zoom_orig); - update(); - } - - if (mb->get_button_index() == MouseButton::WHEEL_UP) { - const float v_zoom_orig = v_zoom; - if (v_zoom > 0.000001) { - v_zoom /= 1.2; - } - v_scroll = v_scroll + (mb->get_position().y - get_size().y / 2) * (v_zoom - v_zoom_orig); - update(); - } - } - if (mb.is_valid() && mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) { menu_insert_key = mb->get_position(); if (menu_insert_key.x >= timeline->get_name_limit() && menu_insert_key.x <= get_size().width - timeline->get_buttons_width()) { @@ -1026,12 +1005,21 @@ void AnimationBezierTrackEdit::_pan_callback(Vector2 p_scroll_vec) { update(); } -void AnimationBezierTrackEdit::_zoom_callback(Vector2 p_scroll_vec, Vector2 p_origin) { +void AnimationBezierTrackEdit::_zoom_callback(Vector2 p_scroll_vec, Vector2 p_origin, bool p_alt) { const float v_zoom_orig = v_zoom; - if (p_scroll_vec.y > 0) { - timeline->get_zoom()->set_value(timeline->get_zoom()->get_value() / 1.05); + if (p_alt) { + // Alternate zoom (doesn't affect timeline). + if (p_scroll_vec.y > 0) { + v_zoom = MIN(v_zoom * 1.2, 100000); + } else { + v_zoom = MAX(v_zoom / 1.2, 0.000001); + } } else { - timeline->get_zoom()->set_value(timeline->get_zoom()->get_value() * 1.05); + if (p_scroll_vec.y > 0) { + timeline->get_zoom()->set_value(timeline->get_zoom()->get_value() / 1.05); + } else { + timeline->get_zoom()->set_value(timeline->get_zoom()->get_value() * 1.05); + } } v_scroll = v_scroll + (p_origin.y - get_size().y / 2) * (v_zoom - v_zoom_orig); update(); @@ -1172,8 +1160,6 @@ void AnimationBezierTrackEdit::_bind_methods() { AnimationBezierTrackEdit::AnimationBezierTrackEdit() { panner.instantiate(); panner->set_callbacks(callable_mp(this, &AnimationBezierTrackEdit::_scroll_callback), callable_mp(this, &AnimationBezierTrackEdit::_pan_callback), callable_mp(this, &AnimationBezierTrackEdit::_zoom_callback)); - panner->set_disable_rmb(true); - panner->set_control_scheme(ViewPanner::SCROLL_PANS); play_position = memnew(Control); play_position->set_mouse_filter(MOUSE_FILTER_PASS); diff --git a/editor/animation_bezier_editor.h b/editor/animation_bezier_editor.h index 6a5b97a7da7..8af493adcbd 100644 --- a/editor/animation_bezier_editor.h +++ b/editor/animation_bezier_editor.h @@ -128,7 +128,7 @@ class AnimationBezierTrackEdit : public Control { Ref panner; void _scroll_callback(Vector2 p_scroll_vec); void _pan_callback(Vector2 p_scroll_vec); - void _zoom_callback(Vector2 p_scroll_vec, Vector2 p_origin); + void _zoom_callback(Vector2 p_scroll_vec, Vector2 p_origin, bool p_alt); void _draw_line_clipped(const Vector2 &p_from, const Vector2 &p_to, const Color &p_color, int p_clip_left, int p_clip_right); void _draw_track(int p_track, const Color &p_color); diff --git a/editor/animation_track_editor.cpp b/editor/animation_track_editor.cpp index 973f74d6cc0..4aa98c0a6c1 100644 --- a/editor/animation_track_editor.cpp +++ b/editor/animation_track_editor.cpp @@ -1459,7 +1459,7 @@ int AnimationTimelineEdit::get_name_limit() const { void AnimationTimelineEdit::_notification(int p_what) { if (p_what == NOTIFICATION_ENTER_TREE || p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) { - panner->set_control_scheme((ViewPanner::ControlScheme)EDITOR_GET("interface/editors/animation_editors_panning_scheme").operator int()); + panner->setup((ViewPanner::ControlScheme)EDITOR_GET("interface/editors/animation_editors_panning_scheme").operator int(), ED_GET_SHORTCUT("canvas_item_editor/pan_view"), bool(EditorSettings::get_singleton()->get("editors/panning/simple_panning"))); } if (p_what == NOTIFICATION_ENTER_TREE) { @@ -1843,7 +1843,7 @@ void AnimationTimelineEdit::_pan_callback(Vector2 p_scroll_vec) { set_value(get_value() - p_scroll_vec.x / get_zoom_scale()); } -void AnimationTimelineEdit::_zoom_callback(Vector2 p_scroll_vec, Vector2 p_origin) { +void AnimationTimelineEdit::_zoom_callback(Vector2 p_scroll_vec, Vector2 p_origin, bool p_alt) { if (p_scroll_vec.y < 0) { get_zoom()->set_value(get_zoom()->get_value() * 1.05); } else { @@ -1932,8 +1932,6 @@ AnimationTimelineEdit::AnimationTimelineEdit() { panner.instantiate(); panner->set_callbacks(callable_mp(this, &AnimationTimelineEdit::_scroll_callback), callable_mp(this, &AnimationTimelineEdit::_pan_callback), callable_mp(this, &AnimationTimelineEdit::_zoom_callback)); - panner->set_disable_rmb(true); - panner->set_control_scheme(ViewPanner::SCROLL_PANS); set_layout_direction(Control::LAYOUT_DIRECTION_LTR); } @@ -4512,7 +4510,7 @@ MenuButton *AnimationTrackEditor::get_edit_menu() { void AnimationTrackEditor::_notification(int p_what) { if (p_what == NOTIFICATION_ENTER_TREE || p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) { - panner->set_control_scheme((ViewPanner::ControlScheme)EDITOR_GET("interface/editors/animation_editors_panning_scheme").operator int()); + panner->setup((ViewPanner::ControlScheme)EDITOR_GET("interface/editors/animation_editors_panning_scheme").operator int(), ED_GET_SHORTCUT("canvas_item_editor/pan_view"), bool(EditorSettings::get_singleton()->get("editors/panning/simple_panning"))); } if (p_what == NOTIFICATION_THEME_CHANGED || p_what == NOTIFICATION_ENTER_TREE) { @@ -5315,7 +5313,7 @@ void AnimationTrackEditor::_pan_callback(Vector2 p_scroll_vec) { scroll->set_v_scroll(scroll->get_v_scroll() - p_scroll_vec.y); } -void AnimationTrackEditor::_zoom_callback(Vector2 p_scroll_vec, Vector2 p_origin) { +void AnimationTrackEditor::_zoom_callback(Vector2 p_scroll_vec, Vector2 p_origin, bool p_alt) { if (p_scroll_vec.y < 0) { timeline->get_zoom()->set_value(timeline->get_zoom()->get_value() * 1.05); } else { @@ -6111,8 +6109,6 @@ AnimationTrackEditor::AnimationTrackEditor() { panner.instantiate(); panner->set_callbacks(callable_mp(this, &AnimationTrackEditor::_scroll_callback), callable_mp(this, &AnimationTrackEditor::_pan_callback), callable_mp(this, &AnimationTrackEditor::_zoom_callback)); - panner->set_disable_rmb(true); - panner->set_control_scheme(ViewPanner::SCROLL_PANS); scroll = memnew(ScrollContainer); timeline_vbox->add_child(scroll); @@ -6120,7 +6116,9 @@ AnimationTrackEditor::AnimationTrackEditor() { VScrollBar *sb = scroll->get_v_scroll_bar(); scroll->remove_child(sb); timeline_scroll->add_child(sb); // Move here so timeline and tracks are always aligned. + scroll->set_focus_mode(FOCUS_CLICK); scroll->connect("gui_input", callable_mp(this, &AnimationTrackEditor::_scroll_input)); + scroll->connect("focus_exited", callable_mp(panner.ptr(), &ViewPanner::release_pan_key)); bezier_edit = memnew(AnimationBezierTrackEdit); timeline_vbox->add_child(bezier_edit); diff --git a/editor/animation_track_editor.h b/editor/animation_track_editor.h index 2a2b20ada99..c33cabb9c0f 100644 --- a/editor/animation_track_editor.h +++ b/editor/animation_track_editor.h @@ -84,7 +84,7 @@ class AnimationTimelineEdit : public Range { Ref panner; void _scroll_callback(Vector2 p_scroll_vec); void _pan_callback(Vector2 p_scroll_vec); - void _zoom_callback(Vector2 p_scroll_vec, Vector2 p_origin); + void _zoom_callback(Vector2 p_scroll_vec, Vector2 p_origin, bool p_alt); bool dragging_timeline; bool dragging_hsize; @@ -379,7 +379,7 @@ class AnimationTrackEditor : public VBoxContainer { Ref panner; void _scroll_callback(Vector2 p_scroll_vec); void _pan_callback(Vector2 p_scroll_vec); - void _zoom_callback(Vector2 p_scroll_vec, Vector2 p_origin); + void _zoom_callback(Vector2 p_scroll_vec, Vector2 p_origin, bool p_alt); void _timeline_value_changed(double); diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index e64f60c58d2..eb2b639780f 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -6043,11 +6043,8 @@ EditorNode::EditorNode() { EDITOR_DEF("interface/inspector/default_color_picker_shape", (int32_t)ColorPicker::SHAPE_VHS_CIRCLE); EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "interface/inspector/default_color_picker_shape", PROPERTY_HINT_ENUM, "HSV Rectangle,HSV Rectangle Wheel,VHS Circle", PROPERTY_USAGE_DEFAULT)); EDITOR_DEF("run/auto_save/save_before_running", true); - EDITOR_DEF("interface/editors/sub_editor_panning_scheme", 0); - EDITOR_DEF("interface/editors/animation_editors_panning_scheme", 1); - // Should be in sync with ControlScheme in ViewPanner. - EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "interface/editors/sub_editor_panning_scheme", PROPERTY_HINT_ENUM, "Scroll Zooms,Scroll Pans", PROPERTY_USAGE_DEFAULT)); - EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "interface/editors/animation_editors_panning_scheme", PROPERTY_HINT_ENUM, "Scroll Zooms,Scroll Pans", PROPERTY_USAGE_DEFAULT)); + + ED_SHORTCUT("canvas_item_editor/pan_view", TTR("Pan View"), Key::SPACE); const Vector textfile_ext = ((String)(EditorSettings::get_singleton()->get("docks/filesystem/textfile_extensions"))).split(",", false); for (const String &E : textfile_ext) { diff --git a/editor/editor_settings.cpp b/editor/editor_settings.cpp index 2b98f46c176..0a0ebec5e09 100644 --- a/editor/editor_settings.cpp +++ b/editor/editor_settings.cpp @@ -643,10 +643,15 @@ void EditorSettings::_load_defaults(Ref p_extra_config) { _initial_set("editors/2d/bone_outline_size", 2); _initial_set("editors/2d/viewport_border_color", Color(0.4, 0.4, 1.0, 0.4)); _initial_set("editors/2d/constrain_editor_view", true); - _initial_set("editors/2d/warped_mouse_panning", true); - _initial_set("editors/2d/simple_panning", false); - _initial_set("editors/2d/scroll_to_pan", false); - _initial_set("editors/2d/pan_speed", 20); + + // Panning + // Enum should be in sync with ControlScheme in ViewPanner. + EDITOR_SETTING(Variant::INT, PROPERTY_HINT_ENUM, "editors/panning/2d_editor_panning_scheme", 0, "Scroll Zooms,Scroll Pans"); + EDITOR_SETTING(Variant::INT, PROPERTY_HINT_ENUM, "editors/panning/sub_editor_panning_scheme", 0, "Scroll Zooms,Scroll Pans"); + EDITOR_SETTING(Variant::INT, PROPERTY_HINT_ENUM, "editors/panning/animation_editors_panning_scheme", 1, "Scroll Zooms,Scroll Pans"); + _initial_set("editors/panning/simple_panning", false); + _initial_set("editors/panning/warped_mouse_panning", true); + _initial_set("editors/panning/2d_editor_pan_speed", 20); // Tiles editor _initial_set("editors/tiles_editor/display_grid", true); diff --git a/editor/plugins/animation_blend_tree_editor_plugin.cpp b/editor/plugins/animation_blend_tree_editor_plugin.cpp index 71db40a829e..8df1729ae10 100644 --- a/editor/plugins/animation_blend_tree_editor_plugin.cpp +++ b/editor/plugins/animation_blend_tree_editor_plugin.cpp @@ -40,6 +40,7 @@ #include "scene/gui/menu_button.h" #include "scene/gui/panel.h" #include "scene/gui/progress_bar.h" +#include "scene/gui/view_panner.h" #include "scene/main/window.h" void AnimationNodeBlendTreeEditor::add_custom_type(const String &p_name, const Ref