Godot Engine – Multi-platform 2D and 3D game engine
Fix regressions with the new `CanvasItemManipulator`
Michael Alexsandere132d9c49e5c
diff --git a/editor/run/game_view_plugin.cpp b/editor/run/game_view_plugin.cppindex 9b0ca9217eb..ae8716ed312 100644--- a/editor/run/game_view_plugin.cpp+++ b/editor/run/game_view_plugin.cpp@@ -1193,6 +1193,7 @@ void GameView::_notification(int p_what) {ci_button[CanvasItemManipulator::TOOL_SELECT]->set_tooltip_text(vformat(TTR("%s+Drag: Rotate selected node around pivot."), keycode_get_string((Key)KeyModifierMask::CMD_OR_CTRL)) + "\n" + vformat(TTR("%s+Drag: Move selected node."), keycode_get_string((Key)KeyModifierMask::ALT)) + "\n" + vformat(TTR("%s+%s+Drag: Scale selected node."), keycode_get_string((Key)KeyModifierMask::CMD_OR_CTRL), keycode_get_string((Key)KeyModifierMask::ALT)) + "\n" + TTR("V: Set selected node's pivot position.") + "\n" + show_list_tooltip);ci_button[CanvasItemManipulator::TOOL_MOVE]->set_tooltip_text(show_list_tooltip);ci_button[CanvasItemManipulator::TOOL_ROTATE]->set_tooltip_text(show_list_tooltip);+ ci_button[CanvasItemManipulator::TOOL_SCALE]->set_tooltip_text(TTR("Shift: Scale proportionally.") + "\n" + show_list_tooltip);ci_button[CanvasItemManipulator::TOOL_LIST_SELECT]->set_tooltip_text(TTR("Show list of selectable nodes at position clicked.") + "\n" + show_list_tooltip);ci_button[CanvasItemManipulator::TOOL_EDIT_PIVOT]->set_tooltip_text(TTR("Click to change object's pivot.") + "\n" + TTR("Shift: Set temporary pivot.") + "\n" + TTR("Click this button while holding Shift to put the temporary pivot in the center of the selected nodes.") + "\n" + show_list_tooltip);ci_button[CanvasItemManipulator::TOOL_PAN]->set_tooltip_text(TTR("You can also use Pan View shortcut (Space by default) to pan in any mode.") + "\n" + show_list_tooltip);@@ -1698,6 +1699,7 @@ GameView::GameView(Ref<GameViewDebugger> p_debugger, EmbeddedProcessBase *p_embe}{Button *button = _setup_button(SNAME("EditPivot"), ci_group);+ button->set_disabled(true);ci_bar->get_hbox()->add_child(button);button->connect(SceneStringName(pressed), callable_mp(*debugger, &GameViewDebugger::set_ci_tool).bind(CanvasItemManipulator::TOOL_EDIT_PIVOT));ci_button[CanvasItemManipulator::TOOL_EDIT_PIVOT] = button;diff --git a/editor/scene/canvas_item_editor_plugin.cpp b/editor/scene/canvas_item_editor_plugin.cppindex 336543bb4c6..82f474b6784 100644--- a/editor/scene/canvas_item_editor_plugin.cpp+++ b/editor/scene/canvas_item_editor_plugin.cpp@@ -4028,6 +4028,7 @@ CanvasItemEditor::CanvasItemEditor() {viewport->add_child(frame_time_panel_container);ci_manipulator.instantiate();+ ci_manipulator->set_editor_mode_enabled(true);ci_manipulator->set_scale(EDSCALE);ci_manipulator->set_drag_threshold(8);// Enable pixel snapping even if pixel snap rendering is disabled in the Project Settings.diff --git a/scene/debugger/canvas_item_manipulator.cpp b/scene/debugger/canvas_item_manipulator.cppindex 8259b6f5135..161fb81b734 100644--- a/scene/debugger/canvas_item_manipulator.cpp+++ b/scene/debugger/canvas_item_manipulator.cpp@@ -2011,12 +2011,11 @@ void CanvasItemManipulator::get_canvas_items_at_pos(const Point2 &p_pos, Vector<find_canvas_items_at_pos(p_pos, start, r_items);// Remove invalid results.- bool is_editor = Engine::get_singleton()->is_editor_hint();for (int i = 0; i < r_items.size(); i++) {Node *node = r_items[i].item;// Make sure the selected node is in the current scene, or editable.- if (is_editor && node && node != SceneTree::get_singleton()->get_edited_scene_root()) {+ if (editor_mode && node && node != SceneTree::get_singleton()->get_edited_scene_root()) {node = start->get_deepest_editable_node(node);}@@ -2048,7 +2047,7 @@ void CanvasItemManipulator::get_canvas_items_at_pos(const Point2 &p_pos, Vector<}// Remove the item if invalid.- bool in_editor_scene = is_editor && ci != start && ci->get_owner() != start && !start->is_editable_instance(ci->get_owner());+ bool in_editor_scene = editor_mode && ci != start && ci->get_owner() != start && !start->is_editable_instance(ci->get_owner());if (duplicate || in_editor_scene || (!p_allow_locked && is_node_locked(ci))) {r_items.remove_at(i);i--;@@ -2059,26 +2058,26 @@ void CanvasItemManipulator::get_canvas_items_at_pos(const Point2 &p_pos, Vector<}void CanvasItemManipulator::find_canvas_items_at_pos(const Point2 &p_pos, Node *p_node, Vector<DebuggerHelpers::SelectResult> &r_items, const Transform2D &p_parent_xform, const Transform2D &p_canvas_xform) {- bool is_editor = Engine::get_singleton()->is_editor_hint();- SubViewport *vp = Object::cast_to<SubViewport>(p_node);-- if (!is_editor && vp) {- return; // FIXME: Make subviewport selection work at runtime.- }-Transform2D xform = p_canvas_xform;+ Viewport *vp = Object::cast_to<Viewport>(p_node);- if (is_editor) {- if (CanvasLayer *cl = Object::cast_to<CanvasLayer>(p_node)) {+ if (CanvasLayer *cl = Object::cast_to<CanvasLayer>(p_node)) {+ if (editor_mode) {xform = cl->get_transform();- } else if (vp) {- if (!vp->is_visible_subviewport()) {- return;- }- xform = vp->get_popup_base_transform();- if (!vp->get_visible_rect().has_point(xform.affine_inverse().xform(p_pos))) {- return;- }+ }+ } else if (vp) {+ if (!editor_mode && vp != SceneTree::get_singleton()->get_root()) {+ return; // FIXME: Make subviewport selection work at runtime.+ }++ if (editor_mode && !vp->is_visible_subviewport()) {+ return;+ }++ xform = vp->get_popup_base_transform();++ if (editor_mode && !vp->get_visible_rect().has_point(xform.affine_inverse().xform(p_pos))) {+ return;}}@@ -2108,7 +2107,7 @@ void CanvasItemManipulator::find_canvas_items_at_pos(const Point2 &p_pos, Node *// Cameras don't affect `CanvasLayer`s.// Only check at runtime, as cameras aren't active in the editor.- if (!is_editor && (!ci->get_canvas_layer_node() || ci->get_canvas_layer_node()->is_following_viewport())) {+ if (!editor_mode && (!ci->get_canvas_layer_node() || ci->get_canvas_layer_node()->is_following_viewport())) {Window *root = SceneTree::get_singleton()->get_root();pos = root->get_canvas_transform().affine_inverse().xform(p_pos);}@@ -2128,37 +2127,40 @@ void CanvasItemManipulator::find_canvas_items_in_rect(const Rect2 &p_rect, Nodereturn;}- bool is_editor = Engine::get_singleton()->is_editor_hint();+ Transform2D xform = p_canvas_xform;Viewport *vp = Object::cast_to<Viewport>(p_node);- if (!is_editor && vp && vp != SceneTree::get_singleton()->get_root()) {- return; // FIXME: Make subviewport selection work at runtime.- }+ if (CanvasLayer *cl = Object::cast_to<CanvasLayer>(p_node)) {+ if (editor_mode) {+ xform = cl->get_transform();+ }+ } else if (vp) {+ if (!editor_mode && vp != SceneTree::get_singleton()->get_root()) {+ return; // FIXME: Make subviewport selection work at runtime.+ }- Transform2D xform = p_canvas_xform;+ if (editor_mode && !vp->is_visible_subviewport()) {+ return;+ }- if (is_editor) {- if (CanvasLayer *cl = Object::cast_to<CanvasLayer>(p_node)) {- xform = cl->get_transform();- } else if (vp) {- if (!vp->is_visible_subviewport()) {- return;- }- xform = vp->get_popup_base_transform();- if (!vp->get_visible_rect().intersects(xform.affine_inverse().xform(p_rect))) {- return;- }+ xform = vp->get_popup_base_transform();++ if (editor_mode && !vp->get_visible_rect().intersects(xform.affine_inverse().xform(p_rect))) {+ return;}}- CanvasItem *ci = Object::cast_to<CanvasItem>(p_node);-bool editable = true;- if (is_editor) {+ if (editor_mode) {Node *start = Object::cast_to<Node>(find_items_start_callback.call());- editable = !is_editor || p_node == start || p_node->get_owner() == start || p_node == start->get_deepest_editable_node(p_node);+ if (p_node != start && !p_node->get_owner()) {+ return;+ }++ editable = p_node == start || p_node->get_owner() == start || p_node == start->get_deepest_editable_node(p_node);}+ CanvasItem *ci = Object::cast_to<CanvasItem>(p_node);bool lock_children = p_node->get_meta("_edit_group_", false);bool locked = is_node_locked(p_node);@@ -2188,7 +2190,7 @@ void CanvasItemManipulator::find_canvas_items_in_rect(const Rect2 &p_rect, NodeRect2 rect = p_rect;// Cameras don't affect `CanvasLayer`s.// Only check at runtime, as cameras aren't active in the editor.- if (!is_editor && (!ci->get_canvas_layer_node() || ci->get_canvas_layer_node()->is_following_viewport())) {+ if (!editor_mode && (!ci->get_canvas_layer_node() || ci->get_canvas_layer_node()->is_following_viewport())) {Window *root = SceneTree::get_singleton()->get_root();rect = root->get_canvas_transform().affine_inverse().xform(p_rect);}diff --git a/scene/debugger/canvas_item_manipulator.h b/scene/debugger/canvas_item_manipulator.hindex 09e893f5682..a8ad6cfd920 100644--- a/scene/debugger/canvas_item_manipulator.h+++ b/scene/debugger/canvas_item_manipulator.h@@ -134,6 +134,7 @@ private:Tool tool = TOOL_SELECT;DragType drag_type = DRAG_NONE;Input::CursorShape cursor_shape = Input::CursorShape::CURSOR_ARROW;+ bool editor_mode = false; // Set it manually instead of using the hint, to allow debugging the editor itself.bool local_space = false;bool show_transformation_gizmos = false;@@ -250,6 +251,8 @@ public:bool reset_temp_pivot();+ void set_editor_mode_enabled(bool p_enabled) { editor_mode = p_enabled; }+void set_tool(Tool p_tool);void set_drag_type(DragType p_type) { drag_type = p_type; }void set_local_space_enabled(bool p_enabled) { local_space = p_enabled; }diff --git a/scene/debugger/runtime_node_select.cpp b/scene/debugger/runtime_node_select.cppindex bcce5e52c69..802e981ed41 100644--- a/scene/debugger/runtime_node_select.cpp+++ b/scene/debugger/runtime_node_select.cpp@@ -512,6 +512,12 @@ void RuntimeNodeSelect::_root_window_input(const Ref<InputEvent> &p_event) {bool was_input_disabled = Input::get_singleton()->is_input_disabled();Input::get_singleton()->set_disable_input(false);+ Ref<InputEventMouse> me = p_event;+ if (me.is_valid()) {+ // Offset the mouse position by the embedding's transform.+ me->set_position(root->get_popup_base_transform().xform(me->get_position()));+ }+if (!ci_manipulator->gui_input(p_event)) {// Remind the user to enable the camera override when panning.if (!camera_override && ci_manipulator->get_tool() == CanvasItemManipulator::TOOL_PAN && b.is_valid() && b->is_pressed()) {@@ -1081,10 +1087,10 @@ void RuntimeNodeSelect::_update_selection() {}}- if (node_select_type == NODE_TYPE_2D) {+ if (node_select_type == NODE_TYPE_2D && !selected_ci_nodes.is_empty()) {Point2 temp_pivot = ci_manipulator->get_temp_pivot();- if (!selected_ci_nodes.is_empty() && ci_manipulator->is_showing_transformation_gizmos()) {+ if (ci_manipulator->is_showing_transformation_gizmos()) {CanvasItem *ci = nullptr;// Find the first movable node.@@ -1122,6 +1128,7 @@ void RuntimeNodeSelect::_update_selection() {RS::get_singleton()->canvas_item_add_set_transform(srect_ci, simple_xform);// Move Handles+if (is_moving) {Vector<Point2> points = {Point2(CanvasItemManipulator::GIZMO_HANDLE_X_RECT.position.x, CanvasItemManipulator::GIZMO_HANDLE_X_RECT.size.height / 2.0),@@ -1145,6 +1152,7 @@ void RuntimeNodeSelect::_update_selection() {}// Scale Handles+if (tool == CanvasItemManipulator::TOOL_SCALE || drag == CanvasItemManipulator::DRAG_SCALE_X || drag == CanvasItemManipulator::DRAG_SCALE_Y || (tool == CanvasItemManipulator::TOOL_SELECT && is_alt && is_ctrl)) {Size2 scale_factor(CanvasItemManipulator::GIZMO_HANDLE_DISTANCE, CanvasItemManipulator::GIZMO_HANDLE_DISTANCE);bool uniform = Input::get_singleton()->is_key_pressed(Key::SHIFT);@@ -1171,22 +1179,28 @@ void RuntimeNodeSelect::_update_selection() {RS::get_singleton()->canvas_item_add_line(srect_ci, Point2(), Point2(0, scale_factor.y), axis_y_color, scale);}- RS::get_singleton()->canvas_item_add_set_transform(srect_ci, Transform2D());-Input::get_singleton()->set_disable_input(was_input_disabled);-- // Rotation Line- if (drag == CanvasItemManipulator::DRAG_ROTATE) {- RS::get_singleton()->canvas_item_add_line(srect_ci, ci_manipulator->get_drag_rotation_center(), ci_manipulator->get_drag_to(), accent_color * Color(1, 1, 1, 0.6), 2 * scale);- }}}+ // Compensate the mouse position offset from the embedding.+ RS::get_singleton()->canvas_item_add_set_transform(srect_ci, SceneTree::get_singleton()->get_root()->get_popup_base_transform().affine_inverse());++ // Rotation Line++ if (drag == CanvasItemManipulator::DRAG_ROTATE) {+ RS::get_singleton()->canvas_item_add_line(srect_ci, ci_manipulator->get_drag_rotation_center(), ci_manipulator->get_drag_to(), accent_color * Color(1, 1, 1, 0.6), 2 * scale);+ }++ // Temporary Pivot+if (!Math::is_inf(temp_pivot.x) && !Math::is_inf(temp_pivot.y)) {Size2 pivot_size = pivot_icon->get_size() * scale;Rect2 rect(((temp_pivot - view_2d_offset) * view_2d_zoom - (pivot_size / 2.0)).floor(), pivot_size);RS::get_singleton()->canvas_item_add_texture_rect(srect_ci, rect, pivot_icon->get_rid(), false, accent_color);}++ RS::get_singleton()->canvas_item_add_set_transform(srect_ci, Transform2D());}#ifndef _3D_DISABLED