diff options
Diffstat (limited to 'editor/plugins/canvas_item_editor_plugin.cpp')
-rw-r--r-- | editor/plugins/canvas_item_editor_plugin.cpp | 144 |
1 files changed, 93 insertions, 51 deletions
diff --git a/editor/plugins/canvas_item_editor_plugin.cpp b/editor/plugins/canvas_item_editor_plugin.cpp index bac2a4909f..1f9f23b5d0 100644 --- a/editor/plugins/canvas_item_editor_plugin.cpp +++ b/editor/plugins/canvas_item_editor_plugin.cpp @@ -35,7 +35,6 @@ #include "core/os/keyboard.h" #include "editor/debugger/editor_debugger_node.h" #include "editor/editor_node.h" -#include "editor/editor_scale.h" #include "editor/editor_settings.h" #include "editor/editor_string_names.h" #include "editor/editor_undo_redo_manager.h" @@ -45,6 +44,8 @@ #include "editor/plugins/animation_player_editor_plugin.h" #include "editor/plugins/script_editor_plugin.h" #include "editor/scene_tree_dock.h" +#include "editor/themes/editor_scale.h" +#include "editor/themes/editor_theme_manager.h" #include "scene/2d/polygon_2d.h" #include "scene/2d/skeleton_2d.h" #include "scene/2d/sprite_2d.h" @@ -345,7 +346,7 @@ void CanvasItemEditor::_snap_other_nodes( } } -Point2 CanvasItemEditor::snap_point(Point2 p_target, unsigned int p_modes, unsigned int p_forced_modes, const CanvasItem *p_self_canvas_item, List<CanvasItem *> p_other_nodes_exceptions) { +Point2 CanvasItemEditor::snap_point(Point2 p_target, unsigned int p_modes, unsigned int p_forced_modes, const CanvasItem *p_self_canvas_item, const List<CanvasItem *> &p_other_nodes_exceptions) { snap_target[0] = SNAP_TARGET_NONE; snap_target[1] = SNAP_TARGET_NONE; @@ -535,7 +536,7 @@ void CanvasItemEditor::_keying_changed() { } } -Rect2 CanvasItemEditor::_get_encompassing_rect_from_list(List<CanvasItem *> p_list) { +Rect2 CanvasItemEditor::_get_encompassing_rect_from_list(const List<CanvasItem *> &p_list) { ERR_FAIL_COND_V(p_list.is_empty(), Rect2()); // Handles the first element @@ -830,7 +831,7 @@ Vector2 CanvasItemEditor::_position_to_anchor(const Control *p_control, Vector2 return output; } -void CanvasItemEditor::_save_canvas_item_state(List<CanvasItem *> p_canvas_items, bool save_bones) { +void CanvasItemEditor::_save_canvas_item_state(const List<CanvasItem *> &p_canvas_items, bool save_bones) { original_transform = Transform2D(); bool transform_stored = false; @@ -853,14 +854,14 @@ void CanvasItemEditor::_save_canvas_item_state(List<CanvasItem *> p_canvas_items } } -void CanvasItemEditor::_restore_canvas_item_state(List<CanvasItem *> p_canvas_items, bool restore_bones) { +void CanvasItemEditor::_restore_canvas_item_state(const List<CanvasItem *> &p_canvas_items, bool restore_bones) { for (CanvasItem *ci : drag_selection) { CanvasItemEditorSelectedItem *se = editor_selection->get_node_editor_data<CanvasItemEditorSelectedItem>(ci); ci->_edit_set_state(se->undo_state); } } -void CanvasItemEditor::_commit_canvas_item_state(List<CanvasItem *> p_canvas_items, String action_name, bool commit_bones) { +void CanvasItemEditor::_commit_canvas_item_state(const List<CanvasItem *> &p_canvas_items, String action_name, bool commit_bones) { List<CanvasItem *> modified_canvas_items; for (CanvasItem *ci : p_canvas_items) { Dictionary old_state = editor_selection->get_node_editor_data<CanvasItemEditorSelectedItem>(ci)->undo_state; @@ -976,7 +977,7 @@ void CanvasItemEditor::_node_created(Node *p_node) { c->_edit_set_position(xform.xform(node_create_position)); } - call_deferred(SNAME("_reset_create_position")); // Defer the call in case more than one node is added. + callable_mp(this, &CanvasItemEditor::_reset_create_position).call_deferred(); // Defer the call in case more than one node is added. } void CanvasItemEditor::_reset_create_position() { @@ -1408,7 +1409,7 @@ bool CanvasItemEditor::_gui_input_pivot(const Ref<InputEvent> &p_event) { } // Cancel a drag - if (b.is_valid() && b->get_button_index() == MouseButton::RIGHT && b->is_pressed()) { + if (ED_IS_SHORTCUT("canvas_item_editor/cancel_transform", p_event) || (b.is_valid() && b->get_button_index() == MouseButton::RIGHT && b->is_pressed())) { _restore_canvas_item_state(drag_selection); _reset_drag(); viewport->queue_redraw(); @@ -1491,7 +1492,7 @@ bool CanvasItemEditor::_gui_input_rotate(const Ref<InputEvent> &p_event) { } // Cancel a drag - if (b.is_valid() && b->get_button_index() == MouseButton::RIGHT && b->is_pressed()) { + if (ED_IS_SHORTCUT("canvas_item_editor/cancel_transform", p_event) || (b.is_valid() && b->get_button_index() == MouseButton::RIGHT && b->is_pressed())) { _restore_canvas_item_state(drag_selection); _reset_drag(); viewport->queue_redraw(); @@ -1653,7 +1654,7 @@ bool CanvasItemEditor::_gui_input_anchors(const Ref<InputEvent> &p_event) { } // Cancel a drag - if (b.is_valid() && b->get_button_index() == MouseButton::RIGHT && b->is_pressed()) { + if (ED_IS_SHORTCUT("canvas_item_editor/cancel_transform", p_event) || (b.is_valid() && b->get_button_index() == MouseButton::RIGHT && b->is_pressed())) { _restore_canvas_item_state(drag_selection); _reset_drag(); viewport->queue_redraw(); @@ -1854,7 +1855,7 @@ bool CanvasItemEditor::_gui_input_resize(const Ref<InputEvent> &p_event) { } // Cancel a drag - if (b.is_valid() && b->get_button_index() == MouseButton::RIGHT && b->is_pressed()) { + if (ED_IS_SHORTCUT("canvas_item_editor/cancel_transform", p_event) || (b.is_valid() && b->get_button_index() == MouseButton::RIGHT && b->is_pressed())) { _restore_canvas_item_state(drag_selection); snap_target[0] = SNAP_TARGET_NONE; snap_target[1] = SNAP_TARGET_NONE; @@ -1993,7 +1994,7 @@ bool CanvasItemEditor::_gui_input_scale(const Ref<InputEvent> &p_event) { } // Cancel a drag - if (b.is_valid() && b->get_button_index() == MouseButton::RIGHT && b->is_pressed()) { + if (ED_IS_SHORTCUT("canvas_item_editor/cancel_transform", p_event) || (b.is_valid() && b->get_button_index() == MouseButton::RIGHT && b->is_pressed())) { _restore_canvas_item_state(drag_selection); _reset_drag(); viewport->queue_redraw(); @@ -2014,14 +2015,14 @@ bool CanvasItemEditor::_gui_input_move(const Ref<InputEvent> &p_event) { if ((b->is_alt_pressed() && !b->is_command_or_control_pressed()) || tool == TOOL_MOVE) { List<CanvasItem *> selection = _get_edited_canvas_items(); - drag_selection.clear(); - for (int i = 0; i < selection.size(); i++) { - if (_is_node_movable(selection[i], true)) { - drag_selection.push_back(selection[i]); + if (selection.size() > 0) { + drag_selection.clear(); + for (int i = 0; i < selection.size(); i++) { + if (_is_node_movable(selection[i], true)) { + drag_selection.push_back(selection[i]); + } } - } - if (selection.size() > 0) { drag_type = DRAG_MOVE; CanvasItem *ci = selection[0]; @@ -2043,8 +2044,9 @@ bool CanvasItemEditor::_gui_input_move(const Ref<InputEvent> &p_event) { drag_from = transform.affine_inverse().xform(b->get_position()); _save_canvas_item_state(drag_selection); + + return true; } - return true; } } } @@ -2130,7 +2132,7 @@ bool CanvasItemEditor::_gui_input_move(const Ref<InputEvent> &p_event) { } // Cancel a drag - if (b.is_valid() && b->get_button_index() == MouseButton::RIGHT && b->is_pressed()) { + if (ED_IS_SHORTCUT("canvas_item_editor/cancel_transform", p_event) || (b.is_valid() && b->get_button_index() == MouseButton::RIGHT && b->is_pressed())) { _restore_canvas_item_state(drag_selection, true); snap_target[0] = SNAP_TARGET_NONE; snap_target[1] = SNAP_TARGET_NONE; @@ -2344,7 +2346,7 @@ bool CanvasItemEditor::_gui_input_select(const Ref<InputEvent> &p_event) { return true; } - if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && b->is_pressed() && tool == TOOL_SELECT && !panner->is_panning()) { + if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && b->is_pressed() && !panner->is_panning() && (tool == TOOL_SELECT || tool == TOOL_MOVE || tool == TOOL_SCALE || tool == TOOL_ROTATE)) { // Single item selection Point2 click = transform.affine_inverse().xform(b->get_position()); @@ -2379,7 +2381,7 @@ bool CanvasItemEditor::_gui_input_select(const Ref<InputEvent> &p_event) { } else { bool still_selected = _select_click_on_item(ci, click, b->is_shift_pressed()); // Start dragging - if (still_selected) { + if (still_selected && (tool == TOOL_SELECT || tool == TOOL_MOVE)) { // Drag the node(s) if requested drag_start_origin = click; drag_type = DRAG_QUEUED; @@ -2463,7 +2465,7 @@ bool CanvasItemEditor::_gui_input_select(const Ref<InputEvent> &p_event) { } } - if (k.is_valid() && k->is_action_pressed(SNAME("ui_cancel"), false, true) && drag_type == DRAG_NONE && tool == TOOL_SELECT) { + if (k.is_valid() && k->is_action_pressed(SNAME("ui_cancel"), false, true) && drag_type == DRAG_NONE) { // Unselect everything editor_selection->clear(); viewport->queue_redraw(); @@ -2609,7 +2611,7 @@ void CanvasItemEditor::_gui_input_viewport(const Ref<InputEvent> &p_event) { // Grab focus if (!viewport->has_focus() && (!get_viewport()->gui_get_focus_owner() || !get_viewport()->gui_get_focus_owner()->is_text_field())) { - viewport->call_deferred(SNAME("grab_focus")); + callable_mp((Control *)viewport, &Control::grab_focus).call_deferred(); } } @@ -3638,7 +3640,7 @@ void CanvasItemEditor::_draw_invisible_nodes_positions(Node *p_node, const Trans _draw_invisible_nodes_positions(p_node->get_child(i), parent_xform, canvas_xform); } - if (ci && !ci->_edit_use_rect() && (!editor_selection->is_selected(ci) || _is_node_locked(ci))) { + if (show_position_gizmos && ci && !ci->_edit_use_rect() && (!editor_selection->is_selected(ci) || _is_node_locked(ci))) { Transform2D xform = transform * canvas_xform * parent_xform; // Draw the node's position @@ -3775,13 +3777,13 @@ void CanvasItemEditor::_draw_locks_and_groups(Node *p_node, const Transform2D &p real_t offset = 0; Ref<Texture2D> lock = get_editor_theme_icon(SNAME("LockViewport")); - if (p_node->has_meta("_edit_lock_") && show_edit_locks) { + if (show_lock_gizmos && p_node->has_meta("_edit_lock_")) { lock->draw(viewport_ci, (transform * canvas_xform * parent_xform).xform(Point2(0, 0)) + Point2(offset, 0)); offset += lock->get_size().x; } Ref<Texture2D> group = get_editor_theme_icon(SNAME("GroupViewport")); - if (ci->has_meta("_edit_group_") && show_edit_locks) { + if (show_group_gizmos && ci->has_meta("_edit_group_")) { group->draw(viewport_ci, (transform * canvas_xform * parent_xform).xform(Point2(0, 0)) + Point2(offset, 0)); //offset += group->get_size().x; } @@ -3897,7 +3899,7 @@ void CanvasItemEditor::_update_editor_settings() { // to distinguish from the other key icons at the top. On a light theme, // the icon will be dark, so we need to lighten it before blending it // with the red color. - const Color key_auto_color = EditorSettings::get_singleton()->is_dark_theme() ? Color(1, 1, 1) : Color(4.25, 4.25, 4.25); + const Color key_auto_color = EditorThemeManager::is_dark_theme() ? Color(1, 1, 1) : Color(4.25, 4.25, 4.25); key_auto_insert_button->add_theme_color_override("icon_pressed_color", key_auto_color.lerp(Color(1, 0, 0), 0.55)); animation_menu->set_icon(get_editor_theme_icon(SNAME("GuiTabMenuHl"))); @@ -3910,6 +3912,11 @@ void CanvasItemEditor::_update_editor_settings() { void CanvasItemEditor::_notification(int p_what) { switch (p_what) { + case NOTIFICATION_READY: { + EditorRunBar::get_singleton()->connect("play_pressed", callable_mp(this, &CanvasItemEditor::_update_override_camera_button).bind(true)); + EditorRunBar::get_singleton()->connect("stop_pressed", callable_mp(this, &CanvasItemEditor::_update_override_camera_button).bind(false)); + } break; + case NOTIFICATION_PHYSICS_PROCESS: { EditorNode::get_singleton()->get_scene_root()->set_snap_controls_to_pixels(GLOBAL_GET("gui/common/snap_controls_to_pixels")); @@ -3926,7 +3933,7 @@ void CanvasItemEditor::_notification(int p_what) { } else { rect = Rect2(); } - Transform2D xform = ci->get_transform(); + Transform2D xform = ci->get_global_transform(); if (rect != se->prev_rect || xform != se->prev_xform) { viewport->queue_redraw(); @@ -4289,16 +4296,28 @@ void CanvasItemEditor::_popup_callback(int p_op) { view_menu->get_popup()->set_item_checked(idx, show_viewport); viewport->queue_redraw(); } break; - case SHOW_EDIT_LOCKS: { - show_edit_locks = !show_edit_locks; - int idx = view_menu->get_popup()->get_item_index(SHOW_EDIT_LOCKS); - view_menu->get_popup()->set_item_checked(idx, show_edit_locks); + case SHOW_POSITION_GIZMOS: { + show_position_gizmos = !show_position_gizmos; + int idx = gizmos_menu->get_item_index(SHOW_POSITION_GIZMOS); + gizmos_menu->set_item_checked(idx, show_position_gizmos); + viewport->queue_redraw(); + } break; + case SHOW_LOCK_GIZMOS: { + show_lock_gizmos = !show_lock_gizmos; + int idx = gizmos_menu->get_item_index(SHOW_LOCK_GIZMOS); + gizmos_menu->set_item_checked(idx, show_lock_gizmos); + viewport->queue_redraw(); + } break; + case SHOW_GROUP_GIZMOS: { + show_group_gizmos = !show_group_gizmos; + int idx = gizmos_menu->get_item_index(SHOW_GROUP_GIZMOS); + gizmos_menu->set_item_checked(idx, show_group_gizmos); viewport->queue_redraw(); } break; case SHOW_TRANSFORMATION_GIZMOS: { show_transformation_gizmos = !show_transformation_gizmos; - int idx = view_menu->get_popup()->get_item_index(SHOW_TRANSFORMATION_GIZMOS); - view_menu->get_popup()->set_item_checked(idx, show_transformation_gizmos); + int idx = gizmos_menu->get_item_index(SHOW_TRANSFORMATION_GIZMOS); + gizmos_menu->set_item_checked(idx, show_transformation_gizmos); viewport->queue_redraw(); } break; case SNAP_USE_NODE_PARENT: { @@ -4710,7 +4729,7 @@ void CanvasItemEditor::_focus_selection(int p_op) { zoom *= 0.90; zoom_widget->set_zoom(zoom); viewport->queue_redraw(); // Redraw to update the global canvas transform after zoom changes. - call_deferred(SNAME("center_at"), rect.get_center()); // Defer because the updated transform is needed. + callable_mp(this, &CanvasItemEditor::center_at).call_deferred(rect.get_center()); // Defer because the updated transform is needed. } else { center_at(rect.get_center()); } @@ -4759,7 +4778,9 @@ Dictionary CanvasItemEditor::get_state() const { state["show_guides"] = show_guides; state["show_helpers"] = show_helpers; state["show_zoom_control"] = zoom_widget->is_visible(); - state["show_edit_locks"] = show_edit_locks; + state["show_position_gizmos"] = show_position_gizmos; + state["show_lock_gizmos"] = show_lock_gizmos; + state["show_group_gizmos"] = show_group_gizmos; state["show_transformation_gizmos"] = show_transformation_gizmos; state["snap_rotation"] = snap_rotation; state["snap_scale"] = snap_scale; @@ -4896,16 +4917,28 @@ void CanvasItemEditor::set_state(const Dictionary &p_state) { view_menu->get_popup()->set_item_checked(idx, show_helpers); } - if (state.has("show_edit_locks")) { - show_edit_locks = state["show_edit_locks"]; - int idx = view_menu->get_popup()->get_item_index(SHOW_EDIT_LOCKS); - view_menu->get_popup()->set_item_checked(idx, show_edit_locks); + if (state.has("show_position_gizmos")) { + show_position_gizmos = state["show_position_gizmos"]; + int idx = gizmos_menu->get_item_index(SHOW_POSITION_GIZMOS); + gizmos_menu->set_item_checked(idx, show_position_gizmos); + } + + if (state.has("show_lock_gizmos")) { + show_lock_gizmos = state["show_lock_gizmos"]; + int idx = gizmos_menu->get_item_index(SHOW_LOCK_GIZMOS); + gizmos_menu->set_item_checked(idx, show_lock_gizmos); + } + + if (state.has("show_group_gizmos")) { + show_group_gizmos = state["show_group_gizmos"]; + int idx = gizmos_menu->get_item_index(SHOW_GROUP_GIZMOS); + gizmos_menu->set_item_checked(idx, show_group_gizmos); } if (state.has("show_transformation_gizmos")) { show_transformation_gizmos = state["show_transformation_gizmos"]; - int idx = view_menu->get_popup()->get_item_index(SHOW_TRANSFORMATION_GIZMOS); - view_menu->get_popup()->set_item_checked(idx, show_transformation_gizmos); + int idx = gizmos_menu->get_item_index(SHOW_TRANSFORMATION_GIZMOS); + gizmos_menu->set_item_checked(idx, show_transformation_gizmos); } if (state.has("show_zoom_control")) { @@ -5053,9 +5086,6 @@ CanvasItemEditor::CanvasItemEditor() { SceneTreeDock::get_singleton()->connect("node_created", callable_mp(this, &CanvasItemEditor::_node_created)); SceneTreeDock::get_singleton()->connect("add_node_used", callable_mp(this, &CanvasItemEditor::_reset_create_position)); - EditorRunBar::get_singleton()->call_deferred(SNAME("connect"), "play_pressed", callable_mp(this, &CanvasItemEditor::_update_override_camera_button).bind(true)); - EditorRunBar::get_singleton()->call_deferred(SNAME("connect"), "stop_pressed", callable_mp(this, &CanvasItemEditor::_update_override_camera_button).bind(false)); - // Add some margin to the sides for better esthetics. // This prevents the first button's hover/pressed effect from "touching" the panel's border, // which looks ugly. @@ -5102,6 +5132,8 @@ CanvasItemEditor::CanvasItemEditor() { controls_vb = memnew(VBoxContainer); controls_vb->set_begin(Point2(5, 5)); + ED_SHORTCUT("canvas_item_editor/cancel_transform", TTR("Cancel Transformation"), Key::ESCAPE); + // To ensure that scripts can parse the list of shortcuts correctly, we have to define // those shortcuts one by one. Define shortcut before using it (by EditorZoomWidget). ED_SHORTCUT_ARRAY("canvas_item_editor/zoom_3.125_percent", TTR("Zoom to 3.125%"), @@ -5323,7 +5355,7 @@ CanvasItemEditor::CanvasItemEditor() { group_button->set_theme_type_variation("FlatButton"); main_menu_hbox->add_child(group_button); group_button->connect("pressed", callable_mp(this, &CanvasItemEditor::_popup_callback).bind(GROUP_SELECTED)); - group_button->set_tooltip_text(TTR("Make selected node's children not selectable.")); + group_button->set_tooltip_text(TTR("Groups the selected node with its children. This causes the parent to be selected when any child node is clicked in 2D and 3D view.")); // Define the shortcut globally (without a context) so that it works if the Scene tree dock is currently focused. group_button->set_shortcut(ED_SHORTCUT("editor/group_selected_nodes", TTR("Group Selected Node(s)"), KeyModifierMask::CMD_OR_CTRL | Key::G)); @@ -5331,7 +5363,7 @@ CanvasItemEditor::CanvasItemEditor() { ungroup_button->set_theme_type_variation("FlatButton"); main_menu_hbox->add_child(ungroup_button); ungroup_button->connect("pressed", callable_mp(this, &CanvasItemEditor::_popup_callback).bind(UNGROUP_SELECTED)); - ungroup_button->set_tooltip_text(TTR("Make selected node's children selectable.")); + ungroup_button->set_tooltip_text(TTR("Ungroups the selected node from its children. Child nodes will be individual items in 2D and 3D view.")); // Define the shortcut globally (without a context) so that it works if the Scene tree dock is currently focused. ungroup_button->set_shortcut(ED_SHORTCUT("editor/ungroup_selected_nodes", TTR("Ungroup Selected Node(s)"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::G)); @@ -5394,8 +5426,18 @@ CanvasItemEditor::CanvasItemEditor() { p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/show_guides", TTR("Show Guides"), Key::Y), SHOW_GUIDES); p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/show_origin", TTR("Show Origin")), SHOW_ORIGIN); p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/show_viewport", TTR("Show Viewport")), SHOW_VIEWPORT); - p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/show_edit_locks", TTR("Show Group And Lock Icons")), SHOW_EDIT_LOCKS); - p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/show_transformation_gizmos", TTR("Show Transformation Gizmos")), SHOW_TRANSFORMATION_GIZMOS); + p->add_separator(); + + gizmos_menu = memnew(PopupMenu); + gizmos_menu->set_name("GizmosMenu"); + gizmos_menu->connect("id_pressed", callable_mp(this, &CanvasItemEditor::_popup_callback)); + gizmos_menu->set_hide_on_checkable_item_selection(false); + gizmos_menu->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/show_position_gizmos", TTR("Position")), SHOW_POSITION_GIZMOS); + gizmos_menu->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/show_lock_gizmos", TTR("Lock")), SHOW_LOCK_GIZMOS); + gizmos_menu->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/show_group_gizmos", TTR("Group")), SHOW_GROUP_GIZMOS); + gizmos_menu->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/show_transformation_gizmos", TTR("Transformation")), SHOW_TRANSFORMATION_GIZMOS); + p->add_child(gizmos_menu); + p->add_submenu_item(TTR("Gizmos"), "GizmosMenu"); p->add_separator(); p->add_shortcut(ED_SHORTCUT("canvas_item_editor/center_selection", TTR("Center Selection"), Key::F), VIEW_CENTER_TO_SELECTION); @@ -5806,7 +5848,7 @@ void CanvasItemEditorViewport::_perform_drop_data() { Vector<String> error_files; EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); - undo_redo->create_action(TTR("Create Node")); + undo_redo->create_action_for_history(TTR("Create Node"), EditorNode::get_editor_data().get_current_edited_scene_history_id()); for (int i = 0; i < selected_files.size(); i++) { String path = selected_files[i]; |