Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Make possible to manipulate `CanvasItem`s in the Game view

Michael Alexsanderfe27ded36f76
diff --git a/core/debugger/remote_debugger.h b/core/debugger/remote_debugger.h
index 9c64342a022..efaed7c8250 100644
--- a/core/debugger/remote_debugger.h
+++ b/core/debugger/remote_debugger.h
@@ -42,6 +42,7 @@ public:
MESSAGE_TYPE_LOG,
MESSAGE_TYPE_ERROR,
MESSAGE_TYPE_LOG_RICH,
+ MESSAGE_TYPE_EDITOR,
};
private:
diff --git a/editor/debugger/editor_debugger_inspector.cpp b/editor/debugger/editor_debugger_inspector.cpp
index ff8ea59c6f8..486739a3e31 100644
--- a/editor/debugger/editor_debugger_inspector.cpp
+++ b/editor/debugger/editor_debugger_inspector.cpp
@@ -144,6 +144,7 @@ void EditorDebuggerInspector::_bind_methods() {
ADD_SIGNAL(MethodInfo("object_selected", PropertyInfo(Variant::INT, "id")));
ADD_SIGNAL(MethodInfo("objects_edited", PropertyInfo(Variant::STRING, "property"), PropertyInfo(Variant::DICTIONARY, "values", PROPERTY_HINT_DICTIONARY_TYPE, "int;Variant"), PropertyInfo(Variant::STRING, "field")));
ADD_SIGNAL(MethodInfo("object_property_updated", PropertyInfo(Variant::INT, "id"), PropertyInfo(Variant::STRING, "property")));
+ ADD_SIGNAL(MethodInfo("canvas_item_objects_changed", PropertyInfo(Variant::DICTIONARY, "states")));
}
void EditorDebuggerInspector::_notification(int p_what) {
@@ -167,12 +168,12 @@ void EditorDebuggerInspector::_object_selected(ObjectID p_object) {
emit_signal(SNAME("object_selected"), p_object);
}
-EditorDebuggerRemoteObjects *EditorDebuggerInspector::set_objects(const Array &p_arr, int p_debugger_id) {
- ERR_FAIL_COND_V(p_arr.is_empty(), nullptr);
+EditorDebuggerRemoteObjects *EditorDebuggerInspector::set_objects(const Array &p_array, int p_debugger_id) {
+ ERR_FAIL_COND_V(p_array.is_empty(), nullptr);
TypedArray<uint64_t> ids;
LocalVector<SceneDebuggerObject> objects;
- for (const Array arr : p_arr) {
+ for (const Array arr : p_array) {
SceneDebuggerObject obj;
obj.deserialize(arr);
if (obj.id.is_valid()) {
@@ -185,10 +186,13 @@ EditorDebuggerRemoteObjects *EditorDebuggerInspector::set_objects(const Array &p
// Sorting is necessary, as selected nodes in the remote tree are ordered by index.
ids.sort();
+ bool pre_existing = false;
+
EditorDebuggerRemoteObjects *remote_objects = nullptr;
for (EditorDebuggerRemoteObjects *robjs : remote_objects_list) {
if (robjs->remote_object_ids == ids) {
remote_objects = robjs;
+ pre_existing = true;
break;
}
}
@@ -254,8 +258,13 @@ EditorDebuggerRemoteObjects *EditorDebuggerInspector::set_objects(const Array &p
}
}
+ if (!pre_existing && ClassDB::is_parent_class(obj.class_name, CanvasItem::get_class_static())) {
+ remote_objects->has_canvas_items = true;
+ }
+
nc++;
}
+
for (HashMap<String, UsageData>::Iterator E = usage.begin(); E;) {
HashMap<String, UsageData>::Iterator next = E;
++next;
@@ -392,6 +401,38 @@ EditorDebuggerRemoteObjects *EditorDebuggerInspector::set_objects(const Array &p
return remote_objects;
}
+void EditorDebuggerInspector::add_undo_redo_action(const Array &p_array) {
+ Dictionary states = p_array[1];
+ ERR_FAIL_COND(states.is_empty());
+
+ Object *obj = InspectorDock::get_inspector_singleton()->get_edited_object();
+ EditorDebuggerRemoteObjects *remote_objects = Object::cast_to<EditorDebuggerRemoteObjects>(obj);
+ if (!remote_objects) {
+ return;
+ }
+
+ for (const Variant &var : states.get_key_list()) {
+ if (!remote_objects->remote_object_ids.has(var)) {
+ return;
+ }
+ }
+
+ Dictionary undo_state;
+ for (const KeyValue<Variant, Variant> &kv : states) {
+ undo_state[kv.key] = Dictionary(kv.value)["undo"];
+ }
+ Dictionary redo_state;
+ for (const KeyValue<Variant, Variant> &kv : states) {
+ redo_state[kv.key] = Dictionary(kv.value)["redo"];
+ }
+
+ EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
+ ur->create_action(p_array[0], UndoRedo::MERGE_DISABLE, remote_objects);
+ ur->add_do_method(this, SNAME("emit_signal"), SNAME("canvas_item_objects_changed"), redo_state);
+ ur->add_undo_method(this, SNAME("emit_signal"), SNAME("canvas_item_objects_changed"), undo_state);
+ ur->commit_action(false);
+}
+
void EditorDebuggerInspector::clear_remote_inspector() {
if (remote_objects_list.is_empty()) {
return;
diff --git a/editor/debugger/editor_debugger_inspector.h b/editor/debugger/editor_debugger_inspector.h
index f43af57a252..a0348846e8d 100644
--- a/editor/debugger/editor_debugger_inspector.h
+++ b/editor/debugger/editor_debugger_inspector.h
@@ -56,6 +56,7 @@ public:
List<PropertyInfo> prop_list;
HashMap<StringName, TypedDictionary<uint64_t, Variant>> prop_values;
int debugger_id = 0;
+ bool has_canvas_items = false;
bool _hide_script_from_inspector() { return true; }
bool _hide_metadata_from_inspector() { return true; }
@@ -90,6 +91,7 @@ public:
// Remote Object cache
EditorDebuggerRemoteObjects *set_objects(const Array &p_array, int p_debugger_id);
+ void add_undo_redo_action(const Array &p_array);
void clear_remote_inspector();
void clear_cache();
void invalidate_selection_from_cache(const TypedArray<uint64_t> &p_ids);
diff --git a/editor/debugger/editor_debugger_node.cpp b/editor/debugger/editor_debugger_node.cpp
index d2275ad8d50..63550a3138b 100644
--- a/editor/debugger/editor_debugger_node.cpp
+++ b/editor/debugger/editor_debugger_node.cpp
@@ -770,7 +770,7 @@ void EditorDebuggerNode::set_remote_selection(const TypedArray<int64_t> &p_ids,
void EditorDebuggerNode::clear_remote_tree_selection() {
remote_scene_tree->clear_selection();
- get_debugger(remote_scene_tree->get_current_debugger())->clear_inspector();
+ get_debugger(remote_scene_tree->get_current_debugger())->clear_inspector(clear_remote_selection);
}
void EditorDebuggerNode::stop_waiting_inspection() {
@@ -824,7 +824,11 @@ void EditorDebuggerNode::_remote_tree_button_pressed(Object *p_item, int p_colum
void EditorDebuggerNode::_remote_objects_updated(EditorDebuggerRemoteObjects *p_remote_objects) {
if (p_remote_objects->debugger_id == tabs->get_current_tab() && p_remote_objects != InspectorDock::get_inspector_singleton()->get_edited_object()) {
+ // Pushing an item to the inspector also clears it, which sends a message to clear the remote selection.
+ // We workaround it by temporarily not sending messages when this happens.
+ clear_remote_selection = false;
EditorNode::get_singleton()->push_item(p_remote_objects);
+ clear_remote_selection = true;
}
}
diff --git a/editor/debugger/editor_debugger_node.h b/editor/debugger/editor_debugger_node.h
index 35187693cf6..23254769299 100644
--- a/editor/debugger/editor_debugger_node.h
+++ b/editor/debugger/editor_debugger_node.h
@@ -103,6 +103,7 @@ private:
bool inspect_edited_object_wait = false;
float inspect_edited_object_timeout = 0;
+ bool clear_remote_selection = true;
EditorDebuggerTree *remote_scene_tree = nullptr;
bool remote_scene_tree_wait = false;
bool remote_scene_tree_queue_update = false;
diff --git a/editor/debugger/editor_debugger_tree.cpp b/editor/debugger/editor_debugger_tree.cpp
index cb1a285a547..424d2ca53ca 100644
--- a/editor/debugger/editor_debugger_tree.cpp
+++ b/editor/debugger/editor_debugger_tree.cpp
@@ -404,6 +404,7 @@ void EditorDebuggerTree::update_scene_tree(const SceneDebuggerTree *p_tree, int
void EditorDebuggerTree::select_nodes(const TypedArray<int64_t> &p_ids) {
// Manually select, as the tree control may be out-of-date for some reason (e.g. not shown yet).
+ // IDs that weren't found will be excluded in the next update (either removed or not nodes).
inspected_object_ids = p_ids;
scrolling_to_item = true;
diff --git a/editor/debugger/script_editor_debugger.cpp b/editor/debugger/script_editor_debugger.cpp
index 2937242261a..9afba9ef9ce 100644
--- a/editor/debugger/script_editor_debugger.cpp
+++ b/editor/debugger/script_editor_debugger.cpp
@@ -33,6 +33,7 @@
#include "core/config/project_settings.h"
#include "core/debugger/debugger_marshalls.h"
#include "core/debugger/remote_debugger.h"
+#include "core/input/input.h"
#include "core/io/resource_loader.h"
#include "core/object/callable_mp.h"
#include "core/object/class_db.h"
@@ -52,7 +53,6 @@
#include "editor/editor_string_names.h"
#include "editor/file_system/editor_file_system.h"
#include "editor/gui/editor_file_dialog.h"
-#include "editor/gui/editor_toaster.h"
#include "editor/inspector/editor_property_name_processor.h"
#include "editor/scene/3d/node_3d_editor_plugin.h"
#include "editor/scene/3d/node_3d_editor_viewport.h"
@@ -300,6 +300,12 @@ void ScriptEditorDebugger::update_remote_object(ObjectID p_obj_id, const String
}
}
+void ScriptEditorDebugger::change_canvas_item_objects(const Dictionary &p_states) {
+ ERR_FAIL_COND(p_states.is_empty());
+ Array msg = { p_states.duplicate() };
+ _put_msg("scene:change_canvas_item_states", msg);
+}
+
void ScriptEditorDebugger::request_remote_objects(const TypedArray<uint64_t> &p_obj_ids, bool p_update_selection) {
ERR_FAIL_COND(p_obj_ids.is_empty());
Array msg = { p_obj_ids.duplicate(), p_update_selection };
@@ -586,6 +592,9 @@ void ScriptEditorDebugger::_msg_output(uint64_t p_thread_id, const Array &p_data
case RemoteDebugger::MESSAGE_TYPE_ERROR: {
msg_type = EditorLog::MSG_TYPE_ERROR;
} break;
+ case RemoteDebugger::MESSAGE_TYPE_EDITOR: {
+ msg_type = EditorLog::MSG_TYPE_EDITOR;
+ } break;
default: {
WARN_PRINT("Unhandled script debugger message type: " + itos(type));
msg_type = EditorLog::MSG_TYPE_STD;
@@ -938,8 +947,9 @@ void ScriptEditorDebugger::_msg_remote_selection_invalidated(uint64_t p_thread_i
inspector->invalidate_selection_from_cache(p_data[0]);
}
-void ScriptEditorDebugger::_msg_show_selection_limit_warning(uint64_t p_thread_id, const Array &p_data) {
- EditorToaster::get_singleton()->popup_str(vformat(TTR("Some remote nodes were not selected, as the configured maximum selection is %d. This can be changed at \"debugger/max_node_selection\" in the Editor Settings."), EDITOR_GET("debugger/max_node_selection")), EditorToaster::SEVERITY_WARNING);
+void ScriptEditorDebugger::_msg_remote_undo_redo_action(uint64_t p_thread_id, const Array &p_data) {
+ ERR_FAIL_COND(p_data.size() < 2);
+ inspector->add_undo_redo_action(p_data);
}
void ScriptEditorDebugger::_msg_performance_profile_names(uint64_t p_thread_id, const Array &p_data) {
@@ -980,12 +990,22 @@ void ScriptEditorDebugger::_msg_window_title(uint64_t p_thread_id, const Array &
emit_signal(SNAME("remote_window_title_changed"), p_data[0]);
}
-void ScriptEditorDebugger::_msg_embed_suspend_toggle(uint64_t p_thread_id, const Array &p_data) {
- emit_signal(SNAME("embed_shortcut_requested"), EMBED_SUSPEND_TOGGLE);
-}
+void ScriptEditorDebugger::_msg_editor_shortcut_pressed(uint64_t p_thread_id, const Array &p_data) {
+ ERR_FAIL_COND(p_data.size() != 1);
-void ScriptEditorDebugger::_msg_embed_next_frame(uint64_t p_thread_id, const Array &p_data) {
- emit_signal(SNAME("embed_shortcut_requested"), EMBED_NEXT_FRAME);
+ Ref<Shortcut> shortcut = EditorSettings::get_singleton()->get_shortcut(p_data[0]);
+ if (shortcut.is_null()) {
+ return;
+ }
+
+ Array events = shortcut->get_events();
+ if (!events.is_empty()) {
+ Ref<InputEventKey> k = events[0].duplicate();
+ if (k.is_valid()) {
+ k->set_pressed(true);
+ Input::get_singleton()->parse_input_event(k);
+ }
+ }
}
void ScriptEditorDebugger::_parse_message(const String &p_msg, uint64_t p_thread_id, const Array &p_data) {
@@ -1035,13 +1055,12 @@ void ScriptEditorDebugger::_init_parse_message_handlers() {
parse_message_handlers["remote_objects_selected"] = &ScriptEditorDebugger::_msg_remote_objects_selected;
parse_message_handlers["remote_nothing_selected"] = &ScriptEditorDebugger::_msg_remote_nothing_selected;
parse_message_handlers["remote_selection_invalidated"] = &ScriptEditorDebugger::_msg_remote_selection_invalidated;
- parse_message_handlers["show_selection_limit_warning"] = &ScriptEditorDebugger::_msg_show_selection_limit_warning;
+ parse_message_handlers["remote_undo_redo_action"] = &ScriptEditorDebugger::_msg_remote_undo_redo_action;
parse_message_handlers["performance:profile_names"] = &ScriptEditorDebugger::_msg_performance_profile_names;
parse_message_handlers["filesystem:update_file"] = &ScriptEditorDebugger::_msg_filesystem_update_file;
parse_message_handlers["evaluation_return"] = &ScriptEditorDebugger::_msg_evaluation_return;
parse_message_handlers["window:title"] = &ScriptEditorDebugger::_msg_window_title;
- parse_message_handlers["request_embed_suspend_toggle"] = &ScriptEditorDebugger::_msg_embed_suspend_toggle;
- parse_message_handlers["request_embed_next_frame"] = &ScriptEditorDebugger::_msg_embed_next_frame;
+ parse_message_handlers["editor_shortcut_pressed"] = &ScriptEditorDebugger::_msg_editor_shortcut_pressed;
}
void ScriptEditorDebugger::_set_reason_text(const String &p_reason, MessageType p_type) {
@@ -2087,7 +2106,6 @@ void ScriptEditorDebugger::_bind_methods() {
ADD_SIGNAL(MethodInfo("set_breakpoint", PropertyInfo("script"), PropertyInfo(Variant::INT, "line"), PropertyInfo(Variant::BOOL, "enabled")));
ADD_SIGNAL(MethodInfo("clear_breakpoints"));
ADD_SIGNAL(MethodInfo("errors_cleared"));
- ADD_SIGNAL(MethodInfo("embed_shortcut_requested", PropertyInfo(Variant::INT, "embed_shortcut_action")));
}
void ScriptEditorDebugger::add_debugger_tab(Control *p_control) {
@@ -2272,6 +2290,7 @@ ScriptEditorDebugger::ScriptEditorDebugger() {
inspector->connect("object_selected", callable_mp(this, &ScriptEditorDebugger::_remote_object_selected));
inspector->connect("objects_edited", callable_mp(this, &ScriptEditorDebugger::_remote_objects_edited));
inspector->connect("object_property_updated", callable_mp(this, &ScriptEditorDebugger::_remote_object_property_updated));
+ inspector->connect("canvas_item_objects_changed", callable_mp(this, &ScriptEditorDebugger::change_canvas_item_objects));
inspector->register_text_enter(search);
inspector->set_use_filter(true);
inspector_vbox->add_child(inspector);
diff --git a/editor/debugger/script_editor_debugger.h b/editor/debugger/script_editor_debugger.h
index 1a0dbfc736d..5cd91570af5 100644
--- a/editor/debugger/script_editor_debugger.h
+++ b/editor/debugger/script_editor_debugger.h
@@ -235,13 +235,12 @@ private:
void _msg_remote_objects_selected(uint64_t p_thread_id, const Array &p_data);
void _msg_remote_nothing_selected(uint64_t p_thread_id, const Array &p_data);
void _msg_remote_selection_invalidated(uint64_t p_thread_id, const Array &p_data);
- void _msg_show_selection_limit_warning(uint64_t p_thread_id, const Array &p_data);
+ void _msg_remote_undo_redo_action(uint64_t p_thread_id, const Array &p_data);
void _msg_performance_profile_names(uint64_t p_thread_id, const Array &p_data);
void _msg_filesystem_update_file(uint64_t p_thread_id, const Array &p_data);
void _msg_evaluation_return(uint64_t p_thread_id, const Array &p_data);
void _msg_window_title(uint64_t p_thread_id, const Array &p_data);
- void _msg_embed_suspend_toggle(uint64_t p_thread_id, const Array &p_data);
- void _msg_embed_next_frame(uint64_t p_thread_id, const Array &p_data);
+ void _msg_editor_shortcut_pressed(uint64_t p_thread_id, const Array &p_data);
void _parse_message(const String &p_msg, uint64_t p_thread_id, const Array &p_data);
void _set_reason_text(const String &p_reason, MessageType p_type);
@@ -306,13 +305,9 @@ protected:
static void _bind_methods();
public:
- enum EmbedShortcutAction {
- EMBED_SUSPEND_TOGGLE,
- EMBED_NEXT_FRAME,
- };
-
void request_remote_objects(const TypedArray<uint64_t> &p_obj_ids, bool p_update_selection = true);
void update_remote_object(ObjectID p_obj_id, const String &p_prop, const Variant &p_value, const String &p_field = "");
+ void change_canvas_item_objects(const Dictionary &p_states);
void clear_inspector(bool p_send_msg = true);
diff --git a/editor/plugins/editor_plugin_list.h b/editor/plugins/editor_plugin_list.h
index 13112a8a676..f33b17a7a10 100644
--- a/editor/plugins/editor_plugin_list.h
+++ b/editor/plugins/editor_plugin_list.h
@@ -35,7 +35,9 @@
class Control;
class InputEvent;
-class EditorPluginList {
+class EditorPluginList : public Object {
+ GDCLASS(EditorPluginList, Object);
+
LocalVector<EditorPlugin *> plugins_list;
public:
diff --git a/editor/run/game_view_plugin.cpp b/editor/run/game_view_plugin.cpp
index de5dc9289c2..c10e63a61b5 100644
--- a/editor/run/game_view_plugin.cpp
+++ b/editor/run/game_view_plugin.cpp
@@ -39,11 +39,14 @@
#include "editor/debugger/script_editor_debugger.h"
#include "editor/docks/editor_dock.h"
#include "editor/docks/editor_dock_manager.h"
+#include "editor/docks/inspector_dock.h"
#include "editor/editor_interface.h"
#include "editor/editor_main_screen.h"
#include "editor/editor_node.h"
#include "editor/editor_string_names.h"
#include "editor/gui/editor_bottom_panel.h"
+#include "editor/gui/editor_icon_manager.h"
+#include "editor/gui/editor_toaster.h"
#include "editor/gui/editor_toolbar_group.h"
#include "editor/gui/window_wrapper.h"
#include "editor/run/editor_run_bar.h"
@@ -54,8 +57,9 @@
#include "editor/settings/editor_feature_profile.h"
#include "editor/settings/editor_settings.h"
#include "editor/themes/editor_scale.h"
+#include "scene/debugger/canvas_item_manipulator.h"
#include "scene/gui/box_container.h"
-#include "scene/gui/button.h"
+#include "scene/gui/flow_container.h"
#include "scene/gui/label.h"
#include "scene/gui/menu_button.h"
#include "scene/gui/panel.h"
@@ -71,14 +75,19 @@ void GameViewDebugger::_session_started(Ref<EditorDebuggerSession> p_session) {
Dictionary settings;
settings["debugger/max_node_selection"] = EDITOR_GET("debugger/max_node_selection");
settings["editors/2d/selection_rectangle_color"] = EDITOR_GET("editors/2d/selection_rectangle_color");
+ settings["editors/2d/locked_selection_rectangle_color"] = EDITOR_GET("editors/2d/locked_selection_rectangle_color");
settings["editors/panning/2d_editor_panning_scheme"] = EDITOR_GET("editors/panning/2d_editor_panning_scheme");
settings["editors/panning/simple_panning"] = EDITOR_GET("editors/panning/simple_panning");
settings["editors/panning/warped_mouse_panning"] = EDITOR_GET("editors/panning/warped_mouse_panning");
settings["editors/panning/2d_editor_pan_speed"] = EDITOR_GET("editors/panning/2d_editor_pan_speed");
settings["editors/polygon_editor/point_grab_radius"] = EDITOR_GET("editors/polygon_editor/point_grab_radius");
settings["canvas_item_editor/pan_view"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("canvas_item_editor/pan_view"));
+ settings["canvas_item_editor/cancel_transform"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("canvas_item_editor/cancel_transform"));
+ settings["accent_color"] = EditorNode::get_singleton()->get_editor_theme()->get_color(SNAME("accent_color"), EditorStringName(Editor));
settings["box_selection_fill_color"] = EditorNode::get_singleton()->get_editor_theme()->get_color(SNAME("box_selection_fill_color"), EditorStringName(Editor));
settings["box_selection_stroke_color"] = EditorNode::get_singleton()->get_editor_theme()->get_color(SNAME("box_selection_stroke_color"), EditorStringName(Editor));
+ settings["axis_x_color"] = EditorNode::get_singleton()->get_editor_theme()->get_color(SNAME("axis_x_color"), EditorStringName(Editor));
+ settings["axis_y_color"] = EditorNode::get_singleton()->get_editor_theme()->get_color(SNAME("axis_y_color"), EditorStringName(Editor));
#ifndef _3D_DISABLED
settings["editors/3d/default_fov"] = EDITOR_GET("editors/3d/default_fov");
settings["editors/3d/default_z_near"] = EDITOR_GET("editors/3d/default_z_near");
@@ -128,32 +137,26 @@ void GameViewDebugger::_session_started(Ref<EditorDebuggerSession> p_session) {
settings["spatial_editor/freelook_slow_modifier"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("spatial_editor/freelook_slow_modifier"));
#endif // _3D_DISABLED
- Array setup_data;
- setup_data.append(settings);
- p_session->send_message("scene:runtime_node_select_setup", setup_data);
-
- Array type;
- type.append(node_type);
- p_session->send_message("scene:runtime_node_select_set_type", type);
- Array visible;
- visible.append(selection_visible);
- p_session->send_message("scene:runtime_node_select_set_visible", visible);
- Array mode;
- mode.append(select_mode);
- p_session->send_message("scene:runtime_node_select_set_mode", mode);
- Array avoid_locked;
- avoid_locked.append(selection_avoid_locked);
- p_session->send_message("scene:runtime_node_select_set_avoid_locked", avoid_locked);
- Array prefer_group;
- prefer_group.append(selection_prefer_group);
- p_session->send_message("scene:runtime_node_select_set_prefer_group", prefer_group);
- Array mute_audio_data;
- mute_audio_data.append(mute_audio);
- p_session->send_message("scene:debug_mute_audio", mute_audio_data);
+ p_session->send_message("scene:runtime_node_select_setup", { settings });
+ p_session->send_message("scene:runtime_node_select_set_node_type", { node_type });
+ p_session->send_message("scene:runtime_node_select_set_selection_visible", { selection_visible });
+ p_session->send_message("scene:runtime_node_select_set_ci_tool", { ci_tool });
+ p_session->send_message("scene:runtime_node_select_set_ci_local_space", { ci_local_space });
+ p_session->send_message("scene:runtime_node_select_set_n3d_tool", { n3d_tool });
+ p_session->send_message("scene:runtime_node_select_set_avoid_locked", { selection_avoid_locked });
+ p_session->send_message("scene:runtime_node_select_set_prefer_group", { selection_prefer_group });
+ p_session->send_message("scene:debug_mute_audio", { mute_audio });
Dictionary shortcut_settings;
shortcut_settings["editor/suspend_resume_embedded_project"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("editor/suspend_resume_embedded_project"));
shortcut_settings["editor/next_frame_embedded_project"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("editor/next_frame_embedded_project"));
+ shortcut_settings["canvas_item_editor/select_mode"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("canvas_item_editor/select_mode"));
+ shortcut_settings["canvas_item_editor/move_mode"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("canvas_item_editor/move_mode"));
+ shortcut_settings["canvas_item_editor/rotate_mode"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("canvas_item_editor/rotate_mode"));
+ shortcut_settings["canvas_item_editor/scale_mode"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("canvas_item_editor/scale_mode"));
+ shortcut_settings["canvas_item_editor/pan_mode"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("canvas_item_editor/pan_mode"));
+ shortcut_settings["ui_undo"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("ui_undo"));
+ shortcut_settings["ui_redo"] = DebuggerMarshalls::serialize_key_shortcut(ED_GET_SHORTCUT("ui_redo"));
p_session->send_message("scene:setup_embedded_shortcuts", { shortcut_settings });
@@ -235,7 +238,7 @@ void GameViewDebugger::set_node_type(int p_type) {
for (Ref<EditorDebuggerSession> &I : sessions) {
if (I->is_active()) {
- I->send_message("scene:runtime_node_select_set_type", message);
+ I->send_message("scene:runtime_node_select_set_node_type", message);
}
}
}
@@ -248,7 +251,7 @@ void GameViewDebugger::set_selection_visible(bool p_visible) {
for (Ref<EditorDebuggerSession> &I : sessions) {
if (I->is_active()) {
- I->send_message("scene:runtime_node_select_set_visible", message);
+ I->send_message("scene:runtime_node_select_set_selection_visible", message);
}
}
}
@@ -279,15 +282,28 @@ void GameViewDebugger::set_selection_prefer_group(bool p_enabled) {
}
}
-void GameViewDebugger::set_select_mode(int p_mode) {
- select_mode = p_mode;
+void GameViewDebugger::set_ci_tool(int p_tool) {
+ ci_tool = p_tool;
Array message;
- message.append(p_mode);
+ message.append(p_tool);
for (Ref<EditorDebuggerSession> &I : sessions) {
if (I->is_active()) {
- I->send_message("scene:runtime_node_select_set_mode", message);
+ I->send_message("scene:runtime_node_select_set_ci_tool", message);
+ }
+ }
+}
+
+void GameViewDebugger::set_n3d_tool(int p_tool) {
+ n3d_tool = p_tool;
+
+ Array message;
+ message.append(p_tool);
+
+ for (Ref<EditorDebuggerSession> &I : sessions) {
+ if (I->is_active()) {
+ I->send_message("scene:runtime_node_select_set_n3d_tool", message);
}
}
}
@@ -318,6 +334,19 @@ void GameViewDebugger::set_camera_manipulate_mode(EditorDebuggerNode::CameraOver
}
}
+void GameViewDebugger::set_ci_local_space(bool p_enabled) {
+ ci_local_space = p_enabled;
+
+ Array message;
+ message.append(p_enabled);
+
+ for (Ref<EditorDebuggerSession> &I : sessions) {
+ if (I->is_active()) {
+ I->send_message("scene:runtime_node_select_set_ci_local_space", message);
+ }
+ }
+}
+
void GameViewDebugger::report_window_focused(bool p_focused) {
Array message;
message.append(p_focused);
@@ -404,12 +433,28 @@ bool GameViewDebugger::_msg_get_screenshot(const Array &p_args) {
return true;
}
+bool GameViewDebugger::_msg_show_toaster(const Array &p_args) {
+ ERR_FAIL_COND_V_MSG(p_args.size() != 2, false, "show_toaster: invalid number of arguments");
+ EditorToaster::get_singleton()->popup_str(p_args[0], p_args[1]);
+ return true;
+}
+
+bool GameViewDebugger::_msg_open_scene(const Array &p_args) {
+ ERR_FAIL_COND_V_MSG(p_args.size() != 1, false, "open_scene: invalid number of arguments");
+ EditorNode::get_singleton()->open_scene(p_args[0]);
+ return true;
+}
+
bool GameViewDebugger::capture(const String &p_message, const Array &p_data, int p_session) {
Ref<EditorDebuggerSession> session = get_session(p_session);
ERR_FAIL_COND_V(session.is_null(), true);
if (p_message == "game_view:get_screenshot") {
return _msg_get_screenshot(p_data);
+ } else if (p_message == "game_view:show_toaster") {
+ return _msg_show_toaster(p_data);
+ } else if (p_message == "game_view:open_scene") {
+ return _msg_open_scene(p_data);
} else if (p_message == "game_view:setup_complete") {
emit_signal(SNAME("setup_complete"));
return true;
@@ -583,6 +628,8 @@ void GameView::_play_pressed() {
embedded_process->embed_process(current_process_id);
_update_ui();
}
+
+ InspectorDock::get_inspector_singleton()->connect(SNAME("edited_object_changed"), callable_mp(this, &GameView::_inspector_object_changed));
}
void GameView::_stop_pressed() {
@@ -607,6 +654,8 @@ void GameView::_stop_pressed() {
}
screen_index_before_start = -1;
+
+ InspectorDock::get_inspector_singleton()->disconnect(SNAME("edited_object_changed"), callable_mp(this, &GameView::_inspector_object_changed));
}
void GameView::_embedding_completed() {
@@ -647,6 +696,13 @@ void GameView::_editor_or_project_settings_changed() {
_update_ui();
}
+void GameView::_inspector_object_changed() {
+ EditorDebuggerRemoteObjects *robjs = nullptr;
+ Object *obj = InspectorDock::get_inspector_singleton()->get_edited_object();
+ robjs = Object::cast_to<EditorDebuggerRemoteObjects>(obj);
+ ci_button[CanvasItemManipulator::TOOL_EDIT_PIVOT]->set_disabled(!robjs || !robjs->has_canvas_items);
+}
+
void GameView::_update_debugger_buttons() {
bool empty = active_sessions == 0;
@@ -703,23 +759,6 @@ void GameView::_update_debugger_buttons() {
}
}
-void GameView::_handle_shortcut_requested(int p_embed_action) {
- switch (p_embed_action) {
- case ScriptEditorDebugger::EMBED_SUSPEND_TOGGLE: {
- _toggle_suspend_button();
- } break;
- case ScriptEditorDebugger::EMBED_NEXT_FRAME: {
- debugger->next_frame();
- } break;
- }
-}
-
-void GameView::_toggle_suspend_button() {
- const bool new_pressed = !suspend_button->is_pressed();
- suspend_button->set_pressed(new_pressed);
- _suspend_button_toggled(new_pressed);
-}
-
void GameView::_suspend_button_toggled(bool p_pressed) {
_update_debugger_buttons();
@@ -728,37 +767,16 @@ void GameView::_suspend_button_toggled(bool p_pressed) {
void GameView::_node_type_pressed(int p_option) {
RuntimeNodeSelect::NodeType type = (RuntimeNodeSelect::NodeType)p_option;
- for (int i = 0; i < RuntimeNodeSelect::NODE_TYPE_MAX; i++) {
- node_type_button[i]->set_pressed_no_signal(i == type);
- }
+ ci_bar->set_visible(type == RuntimeNodeSelect::NODE_TYPE_2D);
+ n3d_bar->set_visible(type == RuntimeNodeSelect::NODE_TYPE_3D);
_update_debugger_buttons();
debugger->set_node_type(type);
}
-void GameView::_select_mode_pressed(int p_option) {
- RuntimeNodeSelect::SelectMode mode = (RuntimeNodeSelect::SelectMode)p_option;
- if (!select_mode_button[mode]->is_visible()) {
- return;
- }
-
- for (int i = 0; i < RuntimeNodeSelect::SELECT_MODE_MAX; i++) {
- select_mode_button[i]->set_pressed_no_signal(i == mode);
- }
-
- debugger->set_select_mode(mode);
-
- EditorSettings::get_singleton()->set_project_metadata("game_view", "select_mode", mode);
-}
-
void GameView::_selection_options_menu_id_pressed(int p_id) {
switch (p_id) {
- case SELECTION_HIDE: {
- selection_hide = !selection_hide;
- debugger->set_selection_visible(selection_hide);
- EditorSettings::get_singleton()->set_project_metadata("game_view", "hide_selection", selection_hide);
- } break;
case SELECTION_AVOID_LOCKED: {
selection_avoid_locked = !selection_avoid_locked;
debugger->set_selection_avoid_locked(selection_avoid_locked);
@@ -771,18 +789,13 @@ void GameView::_selection_options_menu_id_pressed(int p_id) {
} break;
}
- PopupMenu *menu = selection_options_menu->get_popup();
- menu->set_item_checked(menu->get_item_index(SELECTION_HIDE), selection_hide);
+ PopupMenu *menu = n3d_options_menu->get_popup();
menu->set_item_checked(menu->get_item_index(SELECTION_AVOID_LOCKED), selection_avoid_locked);
menu->set_item_checked(menu->get_item_index(SELECTION_PREFER_GROUP), selection_prefer_group);
}
void GameView::_game_embed_mode_pressed(int p_option) {
EmbedMode mode = (EmbedMode)p_option;
- for (int i = 0; i < EmbedMode::EMBED_TYPE_MAX; i++) {
- game_embed_mode_button[i]->set_pressed_no_signal(i == mode);
- }
-
switch (mode) {
case EMBED_TYPE_DISABLED: {
embed_on_play = false;
@@ -799,6 +812,7 @@ void GameView::_game_embed_mode_pressed(int p_option) {
case EMBED_TYPE_MAX:
break;
}
+
EditorSettings::get_singleton()->set_project_metadata("game_view", "embed_on_play", embed_on_play);
EditorSettings::get_singleton()->set_project_metadata("game_view", "make_floating_on_play", make_floating_on_play);
@@ -1050,9 +1064,17 @@ void GameView::_update_embed_window_size() {
}
}
+void GameView::_debug_hide_selection_button_pressed() {
+ debug_hide_selection = !debug_hide_selection;
+ debug_hide_selection_button->set_button_icon(EditorIconManager::get_icon(debug_hide_selection ? SNAME("GuiVisibilityHidden") : SNAME("GuiVisibilityVisible")));
+ debug_hide_selection_button->set_tooltip_text(debug_hide_selection ? TTRC("Show selection indicators.") : TTRC("Hide selection indicators."));
+ debugger->set_selection_visible(!debug_hide_selection);
+ EditorSettings::get_singleton()->set_project_metadata("game_view", "show_selection", !debug_hide_selection);
+}
+
void GameView::_debug_mute_audio_button_pressed() {
debug_mute_audio = !debug_mute_audio;
- debug_mute_audio_button->set_button_icon(get_editor_theme_icon(debug_mute_audio ? SNAME("AudioMute") : SNAME("AudioStreamPlayer")));
+ debug_mute_audio_button->set_button_icon(EditorIconManager::get_icon(debug_mute_audio ? SNAME("AudioMute") : SNAME("AudioStreamPlayer")));
debug_mute_audio_button->set_tooltip_text(debug_mute_audio ? TTRC("Unmute game audio.") : TTRC("Mute game audio."));
debugger->set_debug_mute_audio(debug_mute_audio);
}
@@ -1166,7 +1188,17 @@ void GameView::_camera_override_menu_id_pressed(int p_id) {
void GameView::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_TRANSLATION_CHANGED: {
- select_mode_button[RuntimeNodeSelect::SELECT_MODE_SINGLE]->set_tooltip_text(vformat(TTR("%s+%s+RMB: Show list of all nodes at position clicked."), keycode_get_string((Key)KeyModifierMask::CMD_OR_CTRL), keycode_get_string((Key)KeyModifierMask::ALT)));
+ const String show_list_tooltip = vformat(TTR("%s+RMB: Show list of all nodes at position clicked, including locked."), keycode_get_string((Key)KeyModifierMask::ALT));
+
+ 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_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);
+
+ n3d_button[RuntimeNodeSelect::SELECT_MODE_SINGLE]->set_tooltip_text(vformat(TTR("%s+%s+RMB: Show list of all nodes at position clicked."), keycode_get_string((Key)KeyModifierMask::CMD_OR_CTRL), keycode_get_string((Key)KeyModifierMask::ALT)));
+
_update_ui();
} break;
@@ -1175,28 +1207,6 @@ void GameView::_notification(int p_what) {
} break;
case NOTIFICATION_THEME_CHANGED: {
- suspend_button->set_button_icon(get_editor_theme_icon(SNAME("Suspend")));
- next_frame_button->set_button_icon(get_editor_theme_icon(SNAME("NextFrame")));
-
- node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->set_button_icon(get_editor_theme_icon(SNAME("InputEventJoypadMotion")));
- node_type_button[RuntimeNodeSelect::NODE_TYPE_2D]->set_button_icon(get_editor_theme_icon(SNAME("2DNodes")));
- node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->set_button_icon(get_editor_theme_icon(SNAME("Node3D")));
-
- select_mode_button[RuntimeNodeSelect::SELECT_MODE_SINGLE]->set_button_icon(get_editor_theme_icon(SNAME("ToolSelect")));
- select_mode_button[RuntimeNodeSelect::SELECT_MODE_LIST]->set_button_icon(get_editor_theme_icon(SNAME("ListSelect")));
-
- selection_options_menu->set_button_icon(get_editor_theme_icon(SNAME("GuiDropdown")));
-
- debug_mute_audio_button->set_button_icon(get_editor_theme_icon(debug_mute_audio ? SNAME("AudioMute") : SNAME("AudioStreamPlayer")));
-
- camera_override_button->set_button_icon(get_editor_theme_icon(SNAME("Camera")));
- camera_override_menu->set_button_icon(get_editor_theme_icon(SNAME("GuiDropdown")));
-
- game_embed_mode_button[EmbedMode::EMBED_TYPE_EDITOR]->set_button_icon(get_editor_theme_icon(SNAME("EmbedFused")));
- game_embed_mode_button[EmbedMode::EMBED_TYPE_FLOATING]->set_button_icon(get_editor_theme_icon(SNAME("EmbedFloating")));
- game_embed_mode_button[EmbedMode::EMBED_TYPE_DISABLED]->set_button_icon(get_editor_theme_icon(SNAME("EmbedDisabled")));
- game_window_options_menu->set_button_icon(get_editor_theme_icon(SNAME("GuiDropdown")));
-
_update_speed_state_icon(time_scale_index);
_update_speed_state_size();
_update_speed_state_color();
@@ -1298,14 +1308,12 @@ void GameView::_attach_script_debugger() {
if (embedded_script_debugger) {
embedded_script_debugger->connect("remote_window_title_changed", callable_mp(this, &GameView::_remote_window_title_changed));
- embedded_script_debugger->connect("embed_shortcut_requested", callable_mp(this, &GameView::_handle_shortcut_requested));
}
}
void GameView::_detach_script_debugger() {
if (embedded_script_debugger) {
embedded_script_debugger->disconnect("remote_window_title_changed", callable_mp(this, &GameView::_remote_window_title_changed));
- embedded_script_debugger->disconnect("embed_shortcut_requested", callable_mp(this, &GameView::_handle_shortcut_requested));
embedded_script_debugger = nullptr;
}
embedded_process->set_script_debugger(nullptr);
@@ -1472,11 +1480,25 @@ void GameView::_feature_profile_changed() {
bool is_3d_enabled = is_profile_null || !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_3D);
if (!is_3d_enabled && node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->is_pressed()) {
+ node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->set_pressed(true);
_node_type_pressed(RuntimeNodeSelect::NODE_TYPE_NONE);
}
node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->set_visible(is_3d_enabled);
}
+Button *GameView::_setup_button(const StringName &p_icon, const Ref<ButtonGroup> &p_group, const Ref<Shortcut> &p_shortcut) {
+ Button *button = memnew(Button);
+ button->set_button_icon(EditorIconManager::get_icon(p_icon));
+ button->set_toggle_mode(true);
+ button->set_button_group(p_group);
+ button->set_theme_type_variation(SceneStringName(FlatButton));
+ if (p_shortcut.is_valid()) {
+ button->set_shortcut(p_shortcut);
+ button->set_shortcut_context(this);
+ }
+ return button;
+}
+
GameView::GameView(Ref<GameViewDebugger> p_debugger, EmbeddedProcessBase *p_embedded_process, WindowWrapper *p_wrapper) {
singleton = this;
@@ -1491,12 +1513,11 @@ GameView::GameView(Ref<GameViewDebugger> p_debugger, EmbeddedProcessBase *p_embe
window_wrapper = p_wrapper;
embedded_process = p_embedded_process;
- MarginContainer *toolbar_margin = memnew(MarginContainer);
+ toolbar_margin = memnew(MarginContainer);
toolbar_margin->set_theme_type_variation("MainToolBarMargin");
add_child(toolbar_margin);
- // FIXME: Turn this back into a FlowContainer once GH-115523 is fixed.
- HBoxContainer *main_menu_fc = memnew(HBoxContainer);
+ FlowContainer *main_menu_fc = memnew(FlowContainer);
toolbar_margin->add_child(main_menu_fc);
HBoxContainer *process_hb = memnew(HBoxContainer);
@@ -1504,6 +1525,7 @@ GameView::GameView(Ref<GameViewDebugger> p_debugger, EmbeddedProcessBase *p_embe
main_menu_fc->add_child(process_hb);
suspend_button = memnew(Button);
process_hb->add_child(suspend_button);
+ suspend_button->set_button_icon(EditorIconManager::get_icon(SNAME("Suspend")));
suspend_button->set_toggle_mode(true);
suspend_button->set_theme_type_variation(SceneStringName(FlatButton));
suspend_button->connect(SceneStringName(toggled), callable_mp(this, &GameView::_suspend_button_toggled));
@@ -1513,6 +1535,7 @@ GameView::GameView(Ref<GameViewDebugger> p_debugger, EmbeddedProcessBase *p_embe
next_frame_button = memnew(Button);
process_hb->add_child(next_frame_button);
+ next_frame_button->set_button_icon(EditorIconManager::get_icon(SNAME("NextFrame")));
next_frame_button->set_theme_type_variation(SceneStringName(FlatButton));
next_frame_button->connect(SceneStringName(pressed), callable_mp(*debugger, &GameViewDebugger::next_frame));
next_frame_button->set_accessibility_name(TTRC("Next Frame"));
@@ -1538,11 +1561,15 @@ GameView::GameView(Ref<GameViewDebugger> p_debugger, EmbeddedProcessBase *p_embe
HBoxContainer *input_hb = EditorToolbarGroup::create(main_menu_fc);
+ node_group.instantiate();
+
node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE] = memnew(Button);
input_hb->add_child(node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]);
node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->set_text(TTRC("Input"));
+ node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->set_button_icon(EditorIconManager::get_icon(SNAME("InputEventJoypadMotion")));
node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->set_toggle_mode(true);
node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->set_pressed(true);
+ node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->set_button_group(node_group);
node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->set_theme_type_variation("FlatButtonNoIconTint");
node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->connect(SceneStringName(pressed), callable_mp(this, &GameView::_node_type_pressed).bind(RuntimeNodeSelect::NODE_TYPE_NONE));
node_type_button[RuntimeNodeSelect::NODE_TYPE_NONE]->set_tooltip_text(TTRC("Allow game input as usual."));
@@ -1550,7 +1577,9 @@ GameView::GameView(Ref<GameViewDebugger> p_debugger, EmbeddedProcessBase *p_embe
node_type_button[RuntimeNodeSelect::NODE_TYPE_2D] = memnew(Button);
input_hb->add_child(node_type_button[RuntimeNodeSelect::NODE_TYPE_2D]);
node_type_button[RuntimeNodeSelect::NODE_TYPE_2D]->set_text(TTRC("2D"));
+ node_type_button[RuntimeNodeSelect::NODE_TYPE_2D]->set_button_icon(EditorIconManager::get_icon(SNAME("2DNodes")));
node_type_button[RuntimeNodeSelect::NODE_TYPE_2D]->set_toggle_mode(true);
+ node_type_button[RuntimeNodeSelect::NODE_TYPE_2D]->set_button_group(node_group);
node_type_button[RuntimeNodeSelect::NODE_TYPE_2D]->set_theme_type_variation("FlatButtonNoIconTint");
node_type_button[RuntimeNodeSelect::NODE_TYPE_2D]->connect(SceneStringName(pressed), callable_mp(this, &GameView::_node_type_pressed).bind(RuntimeNodeSelect::NODE_TYPE_2D));
node_type_button[RuntimeNodeSelect::NODE_TYPE_2D]->set_tooltip_text(TTRC("Disable game input to allow selecting Node2Ds and Controls, as well as manipulate the 2D camera."));
@@ -1558,63 +1587,20 @@ GameView::GameView(Ref<GameViewDebugger> p_debugger, EmbeddedProcessBase *p_embe
node_type_button[RuntimeNodeSelect::NODE_TYPE_3D] = memnew(Button);
input_hb->add_child(node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]);
node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->set_text(TTRC("3D"));
+ node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->set_button_icon(EditorIconManager::get_icon(SNAME("Node3D")));
node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->set_toggle_mode(true);
+ node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->set_button_group(node_group);
node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->set_theme_type_variation("FlatButtonNoIconTint");
node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->connect(SceneStringName(pressed), callable_mp(this, &GameView::_node_type_pressed).bind(RuntimeNodeSelect::NODE_TYPE_3D));
node_type_button[RuntimeNodeSelect::NODE_TYPE_3D]->set_tooltip_text(TTRC("Disable game input to allow selecting Node3Ds and manipulating the 3D camera."));
main_menu_fc->add_child(memnew(VSeparator));
- HBoxContainer *selection_hb = EditorToolbarGroup::create(main_menu_fc);
-
- select_mode_button[RuntimeNodeSelect::SELECT_MODE_SINGLE] = memnew(Button);
- selection_hb->add_child(select_mode_button[RuntimeNodeSelect::SELECT_MODE_SINGLE]);
- select_mode_button[RuntimeNodeSelect::SELECT_MODE_SINGLE]->set_toggle_mode(true);
- select_mode_button[RuntimeNodeSelect::SELECT_MODE_SINGLE]->set_pressed(true);
- select_mode_button[RuntimeNodeSelect::SELECT_MODE_SINGLE]->set_theme_type_variation(SceneStringName(FlatButton));
- select_mode_button[RuntimeNodeSelect::SELECT_MODE_SINGLE]->connect(SceneStringName(pressed), callable_mp(this, &GameView::_select_mode_pressed).bind(RuntimeNodeSelect::SELECT_MODE_SINGLE));
- select_mode_button[RuntimeNodeSelect::SELECT_MODE_SINGLE]->set_shortcut(ED_GET_SHORTCUT("spatial_editor/tool_select"));
- select_mode_button[RuntimeNodeSelect::SELECT_MODE_SINGLE]->set_shortcut_context(this);
-
- select_mode_button[RuntimeNodeSelect::SELECT_MODE_LIST] = memnew(Button);
- selection_hb->add_child(select_mode_button[RuntimeNodeSelect::SELECT_MODE_LIST]);
- select_mode_button[RuntimeNodeSelect::SELECT_MODE_LIST]->set_toggle_mode(true);
- select_mode_button[RuntimeNodeSelect::SELECT_MODE_LIST]->set_theme_type_variation(SceneStringName(FlatButton));
- select_mode_button[RuntimeNodeSelect::SELECT_MODE_LIST]->connect(SceneStringName(pressed), callable_mp(this, &GameView::_select_mode_pressed).bind(RuntimeNodeSelect::SELECT_MODE_LIST));
- select_mode_button[RuntimeNodeSelect::SELECT_MODE_LIST]->set_tooltip_text(TTRC("Show list of selectable nodes at position clicked."));
-
- _select_mode_pressed(EditorSettings::get_singleton()->get_project_metadata("game_view", "select_mode", 0));
-
- selection_options_menu = memnew(MenuButton);
- selection_hb->add_child(selection_options_menu);
- selection_options_menu->set_flat(false);
- selection_options_menu->set_theme_type_variation("FlatMenuButtonNoIconTint");
- selection_options_menu->set_h_size_flags(SIZE_SHRINK_END);
- selection_options_menu->set_tooltip_text(TTRC("Selection options."));
-
- PopupMenu *selection_menu = selection_options_menu->get_popup();
- selection_menu->connect(SceneStringName(id_pressed), callable_mp(this, &GameView::_selection_options_menu_id_pressed));
- selection_menu->add_check_item(TTRC("Show Selection Visibility"), SELECTION_HIDE);
- selection_menu->add_check_item(TTRC("Don't Select Locked Nodes"), SELECTION_AVOID_LOCKED);
- selection_menu->add_check_item(TTRC("Select Group Over Children"), SELECTION_PREFER_GROUP);
-
- selection_hide = EditorSettings::get_singleton()->get_project_metadata("game_view", "hide_selection", true);
- selection_avoid_locked = EditorSettings::get_singleton()->get_project_metadata("game_view", "selection_avoid_locked", false);
- selection_prefer_group = EditorSettings::get_singleton()->get_project_metadata("game_view", "selection_prefer_group", false);
- selection_menu->set_item_checked(selection_menu->get_item_index(SELECTION_HIDE), selection_hide);
- selection_menu->set_item_checked(selection_menu->get_item_index(SELECTION_AVOID_LOCKED), selection_avoid_locked);
- selection_menu->set_item_checked(selection_menu->get_item_index(SELECTION_PREFER_GROUP), selection_prefer_group);
-
- debugger->set_selection_visible(selection_hide);
- debugger->set_selection_avoid_locked(selection_avoid_locked);
- debugger->set_selection_prefer_group(selection_prefer_group);
-
- main_menu_fc->add_child(memnew(VSeparator));
-
HBoxContainer *camera_hb = EditorToolbarGroup::create(main_menu_fc);
camera_override_button = memnew(Button);
camera_hb->add_child(camera_override_button);
+ camera_override_button->set_button_icon(EditorIconManager::get_icon(SNAME("Camera")));
camera_override_button->set_toggle_mode(true);
camera_override_button->set_theme_type_variation(SceneStringName(FlatButton));
camera_override_button->set_tooltip_text(TTRC("Override the in-game camera."));
@@ -1622,6 +1608,7 @@ GameView::GameView(Ref<GameViewDebugger> p_debugger, EmbeddedProcessBase *p_embe
camera_override_menu = memnew(MenuButton);
camera_hb->add_child(camera_override_menu);
+ camera_override_menu->set_button_icon(EditorIconManager::get_icon(SNAME("GuiDropdown")));
camera_override_menu->set_flat(false);
camera_override_menu->set_theme_type_variation("FlatMenuButtonNoIconTint");
camera_override_menu->set_h_size_flags(SIZE_SHRINK_END);
@@ -1641,12 +1628,145 @@ GameView::GameView(Ref<GameViewDebugger> p_debugger, EmbeddedProcessBase *p_embe
HBoxContainer *audio_hb = memnew(HBoxContainer);
main_menu_fc->add_child(audio_hb);
+ debug_hide_selection = !EditorSettings::get_singleton()->get_project_metadata("game_view", "show_selection", true);
+
+ debug_hide_selection_button = memnew(Button);
+ audio_hb->add_child(debug_hide_selection_button);
+ debug_hide_selection_button->set_button_icon(EditorIconManager::get_icon(debug_hide_selection ? "GuiVisibilityHidden" : "GuiVisibilityVisible"));
+ debug_hide_selection_button->set_theme_type_variation(SceneStringName(FlatButton));
+ debug_hide_selection_button->connect(SceneStringName(pressed), callable_mp(this, &GameView::_debug_hide_selection_button_pressed));
+ debug_hide_selection_button->set_tooltip_text(debug_hide_selection ? TTRC("Show selection indicators.") : TTRC("Hide selection indicators."));
+ debugger->set_selection_visible(!debug_hide_selection);
+
debug_mute_audio_button = memnew(Button);
audio_hb->add_child(debug_mute_audio_button);
debug_mute_audio_button->set_theme_type_variation(SceneStringName(FlatButton));
+ debug_mute_audio_button->set_button_icon(EditorIconManager::get_icon(debug_mute_audio ? SNAME("AudioMute") : SNAME("AudioStreamPlayer")));
debug_mute_audio_button->connect(SceneStringName(pressed), callable_mp(this, &GameView::_debug_mute_audio_button_pressed));
debug_mute_audio_button->set_tooltip_text(debug_mute_audio ? TTRC("Unmute game audio.") : TTRC("Mute game audio."));
+ main_menu_fc->add_child(memnew(VSeparator));
+
+ // 2D Section
+
+ ci_bar = memnew(EditorToolbarGroup);
+ main_menu_fc->add_child(ci_bar);
+ ci_bar->hide();
+
+ ci_group.instantiate();
+
+ {
+ Button *button = _setup_button(SNAME("ToolSelect"), ci_group, ED_GET_SHORTCUT("canvas_item_editor/select_mode"));
+ ci_bar->get_hbox()->add_child(button);
+ button->set_pressed(true);
+ button->connect(SceneStringName(pressed), callable_mp(*debugger, &GameViewDebugger::set_ci_tool).bind(CanvasItemManipulator::TOOL_SELECT));
+ ci_button[CanvasItemManipulator::TOOL_SELECT] = button;
+ }
+
+ VSeparator *vsep = memnew(VSeparator);
+ vsep->set_theme_type_variation("VSeparatorButtonGroup");
+ ci_bar->get_hbox()->add_child(vsep);
+
+ {
+ Button *button = _setup_button(SNAME("ToolMove"), ci_group, ED_GET_SHORTCUT("canvas_item_editor/move_mode"));
+ ci_bar->get_hbox()->add_child(button);
+ button->connect(SceneStringName(pressed), callable_mp(*debugger, &GameViewDebugger::set_ci_tool).bind(CanvasItemManipulator::TOOL_MOVE));
+ ci_button[CanvasItemManipulator::TOOL_MOVE] = button;
+ }
+ {
+ Button *button = _setup_button(SNAME("ToolRotate"), ci_group, ED_GET_SHORTCUT("canvas_item_editor/rotate_mode"));
+ ci_bar->get_hbox()->add_child(button);
+ button->connect(SceneStringName(pressed), callable_mp(*debugger, &GameViewDebugger::set_ci_tool).bind(CanvasItemManipulator::TOOL_ROTATE));
+ ci_button[CanvasItemManipulator::TOOL_ROTATE] = button;
+ }
+ {
+ Button *button = _setup_button(SNAME("ToolScale"), ci_group, ED_GET_SHORTCUT("canvas_item_editor/scale_mode"));
+ ci_bar->get_hbox()->add_child(button);
+ button->connect(SceneStringName(pressed), callable_mp(*debugger, &GameViewDebugger::set_ci_tool).bind(CanvasItemManipulator::TOOL_SCALE));
+ ci_button[CanvasItemManipulator::TOOL_SCALE] = button;
+ }
+
+ vsep = memnew(VSeparator);
+ vsep->set_theme_type_variation("VSeparatorButtonGroup");
+ ci_bar->get_hbox()->add_child(vsep);
+
+ {
+ Button *button = _setup_button(SNAME("ListSelect"), ci_group);
+ ci_bar->get_hbox()->add_child(button);
+ button->connect(SceneStringName(pressed), callable_mp(*debugger, &GameViewDebugger::set_ci_tool).bind(CanvasItemManipulator::TOOL_LIST_SELECT));
+ ci_button[CanvasItemManipulator::TOOL_LIST_SELECT] = button;
+ }
+ {
+ Button *button = _setup_button(SNAME("EditPivot"), ci_group);
+ 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;
+ }
+ {
+ Button *button = _setup_button(SNAME("ToolPan"), ci_group, ED_GET_SHORTCUT("canvas_item_editor/pan_mode"));
+ ci_bar->get_hbox()->add_child(button);
+ button->connect(SceneStringName(pressed), callable_mp(*debugger, &GameViewDebugger::set_ci_tool).bind(CanvasItemManipulator::TOOL_PAN));
+ ci_button[CanvasItemManipulator::TOOL_PAN] = button;
+ }
+
+ vsep = memnew(VSeparator);
+ vsep->set_theme_type_variation("VSeparatorButtonGroup");
+ ci_bar->get_hbox()->add_child(vsep);
+
+ Button *ci_local_space = memnew(Button);
+ ci_bar->get_hbox()->add_child(ci_local_space);
+ ci_local_space->set_button_icon(EditorIconManager::get_icon(SNAME("Object")));
+ ci_local_space->set_toggle_mode(true);
+ ci_local_space->set_pressed(true);
+ ci_local_space->set_theme_type_variation(SceneStringName(FlatButton));
+ ci_local_space->connect(SceneStringName(toggled), callable_mp(*debugger, &GameViewDebugger::set_ci_local_space));
+ ci_local_space->set_shortcut(ED_GET_SHORTCUT("canvas_item_editor/use_local_space"));
+ ci_local_space->set_shortcut_context(this);
+
+ // 3D Section
+
+ n3d_bar = memnew(EditorToolbarGroup);
+ main_menu_fc->add_child(n3d_bar);
+ n3d_bar->hide();
+
+ n3d_group.instantiate();
+
+ {
+ Button *button = _setup_button(SNAME("ToolSelect"), n3d_group, ED_GET_SHORTCUT("spatial_editor/tool_select"));
+ n3d_bar->get_hbox()->add_child(button);
+ button->set_pressed(true);
+ button->connect(SceneStringName(pressed), callable_mp(*debugger, &GameViewDebugger::set_n3d_tool).bind(RuntimeNodeSelect::SELECT_MODE_SINGLE));
+ n3d_button[RuntimeNodeSelect::SELECT_MODE_SINGLE] = button;
+ }
+ {
+ Button *button = _setup_button(SNAME("ListSelect"), n3d_group);
+ n3d_bar->get_hbox()->add_child(button);
+ button->set_tooltip_text(TTRC("Show list of selectable nodes at position clicked."));
+ button->connect(SceneStringName(pressed), callable_mp(*debugger, &GameViewDebugger::set_n3d_tool).bind(RuntimeNodeSelect::SELECT_MODE_LIST));
+ n3d_button[RuntimeNodeSelect::SELECT_MODE_LIST] = button;
+ }
+
+ n3d_options_menu = memnew(MenuButton);
+ n3d_bar->get_hbox()->add_child(n3d_options_menu);
+ n3d_options_menu->set_button_icon(EditorIconManager::get_icon(SNAME("GuiDropdown")));
+ n3d_options_menu->set_flat(false);
+ n3d_options_menu->set_theme_type_variation("FlatMenuButtonNoIconTint");
+ n3d_options_menu->set_h_size_flags(SIZE_SHRINK_END);
+ n3d_options_menu->set_tooltip_text(TTRC("Selection options."));
+
+ PopupMenu *selection_menu = n3d_options_menu->get_popup();
+ selection_menu->connect(SceneStringName(id_pressed), callable_mp(this, &GameView::_selection_options_menu_id_pressed));
+ selection_menu->add_check_item(TTRC("Don't Select Locked Nodes"), SELECTION_AVOID_LOCKED);
+ selection_menu->add_check_item(TTRC("Select Group Over Children"), SELECTION_PREFER_GROUP);
+
+ selection_avoid_locked = EditorSettings::get_singleton()->get_project_metadata("game_view", "selection_avoid_locked", false);
+ selection_prefer_group = EditorSettings::get_singleton()->get_project_metadata("game_view", "selection_prefer_group", false);
+ selection_menu->set_item_checked(selection_menu->get_item_index(SELECTION_AVOID_LOCKED), selection_avoid_locked);
+ selection_menu->set_item_checked(selection_menu->get_item_index(SELECTION_PREFER_GROUP), selection_prefer_group);
+
+ debugger->set_selection_avoid_locked(selection_avoid_locked);
+ debugger->set_selection_prefer_group(selection_prefer_group);
+
embedding_hb = memnew(HBoxContainer);
embedding_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
embedding_hb->set_alignment(ALIGNMENT_END);
@@ -1664,29 +1784,38 @@ GameView::GameView(Ref<GameViewDebugger> p_debugger, EmbeddedProcessBase *p_embe
game_size_label->set_horizontal_alignment(HorizontalAlignment::HORIZONTAL_ALIGNMENT_RIGHT);
game_size_label->set_mouse_filter(MouseFilter::MOUSE_FILTER_PASS);
+ game_embed_group.instantiate();
+
game_embed_mode_button[EmbedMode::EMBED_TYPE_EDITOR] = memnew(Button);
game_hb->add_child(game_embed_mode_button[EmbedMode::EMBED_TYPE_EDITOR]);
+ game_embed_mode_button[EmbedMode::EMBED_TYPE_EDITOR]->set_button_icon(EditorIconManager::get_icon(SNAME("EmbedFused")));
game_embed_mode_button[EmbedMode::EMBED_TYPE_EDITOR]->set_toggle_mode(true);
+ game_embed_mode_button[EmbedMode::EMBED_TYPE_EDITOR]->set_button_group(game_embed_group);
game_embed_mode_button[EmbedMode::EMBED_TYPE_EDITOR]->set_theme_type_variation("FlatMenuButtonNoIconTint");
game_embed_mode_button[EmbedMode::EMBED_TYPE_EDITOR]->connect(SceneStringName(pressed), callable_mp(this, &GameView::_game_embed_mode_pressed).bind(EmbedMode::EMBED_TYPE_EDITOR));
game_embed_mode_button[EmbedMode::EMBED_TYPE_EDITOR]->set_tooltip_text(TTRC("Run the game embedded in the Game workspace on the next run."));
game_embed_mode_button[EmbedMode::EMBED_TYPE_FLOATING] = memnew(Button);
game_hb->add_child(game_embed_mode_button[EmbedMode::EMBED_TYPE_FLOATING]);
+ game_embed_mode_button[EmbedMode::EMBED_TYPE_FLOATING]->set_button_icon(EditorIconManager::get_icon(SNAME("EmbedFloating")));
game_embed_mode_button[EmbedMode::EMBED_TYPE_FLOATING]->set_toggle_mode(true);
+ game_embed_mode_button[EmbedMode::EMBED_TYPE_FLOATING]->set_button_group(game_embed_group);
game_embed_mode_button[EmbedMode::EMBED_TYPE_FLOATING]->set_theme_type_variation("FlatMenuButtonNoIconTint");
game_embed_mode_button[EmbedMode::EMBED_TYPE_FLOATING]->connect(SceneStringName(pressed), callable_mp(this, &GameView::_game_embed_mode_pressed).bind(EmbedMode::EMBED_TYPE_FLOATING));
game_embed_mode_button[EmbedMode::EMBED_TYPE_FLOATING]->set_tooltip_text(TTRC("Run the game in a floating window with the toolbar on the next run."));
game_embed_mode_button[EmbedMode::EMBED_TYPE_DISABLED] = memnew(Button);
game_hb->add_child(game_embed_mode_button[EmbedMode::EMBED_TYPE_DISABLED]);
+ game_embed_mode_button[EmbedMode::EMBED_TYPE_DISABLED]->set_button_icon(EditorIconManager::get_icon(SNAME("EmbedDisabled")));
game_embed_mode_button[EmbedMode::EMBED_TYPE_DISABLED]->set_toggle_mode(true);
+ game_embed_mode_button[EmbedMode::EMBED_TYPE_DISABLED]->set_button_group(game_embed_group);
game_embed_mode_button[EmbedMode::EMBED_TYPE_DISABLED]->set_theme_type_variation("FlatMenuButtonNoIconTint");
game_embed_mode_button[EmbedMode::EMBED_TYPE_DISABLED]->connect(SceneStringName(pressed), callable_mp(this, &GameView::_game_embed_mode_pressed).bind(EmbedMode::EMBED_TYPE_DISABLED));
game_embed_mode_button[EmbedMode::EMBED_TYPE_DISABLED]->set_tooltip_text(TTRC("Run the game in a floating window without the toolbar on the next run."));
game_window_options_menu = memnew(MenuButton);
game_hb->add_child(game_window_options_menu);
+ game_window_options_menu->set_button_icon(EditorIconManager::get_icon(SNAME("GuiDropdown")));
game_window_options_menu->set_flat(false);
game_window_options_menu->set_theme_type_variation("FlatMenuButtonNoIconTint");
game_window_options_menu->set_h_size_flags(SIZE_SHRINK_END);
diff --git a/editor/run/game_view_plugin.h b/editor/run/game_view_plugin.h
index c2a3fa4e4d8..cef9b2a2fd9 100644
--- a/editor/run/game_view_plugin.h
+++ b/editor/run/game_view_plugin.h
@@ -34,9 +34,12 @@
#include "editor/debugger/editor_debugger_plugin.h"
#include "editor/editor_main_screen.h"
#include "editor/plugins/editor_plugin.h"
+#include "scene/debugger/canvas_item_manipulator.h"
#include "scene/debugger/runtime_node_select.h"
#include "scene/gui/box_container.h"
+class ButtonGroup;
+class EditorToolbarGroup;
class EmbeddedProcessBase;
class VSeparator;
class WindowWrapper;
@@ -52,7 +55,9 @@ private:
bool is_feature_enabled = true;
int node_type = RuntimeNodeSelect::NODE_TYPE_NONE;
bool selection_visible = true;
- int select_mode = RuntimeNodeSelect::SELECT_MODE_SINGLE;
+ int ci_tool = CanvasItemManipulator::TOOL_SELECT;
+ int ci_local_space = true;
+ int n3d_tool = RuntimeNodeSelect::SELECT_MODE_SINGLE;
bool mute_audio = false;
EditorDebuggerNode::CameraOverride camera_override_mode = EditorDebuggerNode::OVERRIDE_INGAME;
@@ -73,6 +78,8 @@ private:
HashMap<uint64_t, ScreenshotCB> screenshot_callbacks;
bool _msg_get_screenshot(const Array &p_args);
+ bool _msg_show_toaster(const Array &p_args);
+ bool _msg_open_scene(const Array &p_args);
protected:
static void _bind_methods();
@@ -93,7 +100,10 @@ public:
void reset_time_scale();
void set_node_type(int p_type);
- void set_select_mode(int p_mode);
+ void set_ci_tool(int p_tool);
+ void set_n3d_tool(int p_tool);
+ // FIXME: This is present only for Android, and the code that uses it needs to be updated.
+ void set_select_mode(int p_mode) {}
void set_selection_visible(bool p_visible);
@@ -107,6 +117,8 @@ public:
void set_camera_override(bool p_enabled);
void set_camera_manipulate_mode(EditorDebuggerNode::CameraOverride p_mode);
+ void set_ci_local_space(bool p_enabled);
+
void report_window_focused(bool p_focused);
void reset_camera_2d_position();
@@ -125,7 +137,6 @@ class GameView : public VBoxContainer {
CAMERA_RESET_3D,
CAMERA_MODE_INGAME,
CAMERA_MODE_EDITORS,
- SELECTION_HIDE,
SELECTION_AVOID_LOCKED,
SELECTION_PREFER_GROUP,
WINDOW_SIZE_MODE_FIXED,
@@ -180,29 +191,39 @@ class GameView : public VBoxContainer {
Rect2i floating_window_rect;
int floating_window_screen = -1;
+ bool debug_hide_selection = false;
bool debug_mute_audio = false;
- bool selection_hide = true;
+ EditorToolbarGroup *ci_bar = nullptr;
+ EditorToolbarGroup *n3d_bar = nullptr;
+
bool selection_avoid_locked = false;
bool selection_prefer_group = false;
Button *suspend_button = nullptr;
Button *next_frame_button = nullptr;
+ Ref<ButtonGroup> node_group;
+ Ref<ButtonGroup> ci_group;
+ Ref<ButtonGroup> n3d_group;
+
Button *node_type_button[RuntimeNodeSelect::NODE_TYPE_MAX];
- Button *select_mode_button[RuntimeNodeSelect::SELECT_MODE_MAX];
+ Button *ci_button[CanvasItemManipulator::TOOL_MAX];
+ Button *n3d_button[RuntimeNodeSelect::SELECT_MODE_MAX];
- MenuButton *selection_options_menu = nullptr;
+ MenuButton *n3d_options_menu = nullptr;
Button *camera_override_button = nullptr;
MenuButton *camera_override_menu = nullptr;
+ Button *debug_hide_selection_button = nullptr;
Button *debug_mute_audio_button = nullptr;
HBoxContainer *embedding_hb = nullptr;
MenuButton *game_window_options_menu = nullptr;
Label *game_size_label = nullptr;
HBoxContainer *game_hb = nullptr;
+ Ref<ButtonGroup> game_embed_group;
Button *game_embed_mode_button[EmbedMode::EMBED_TYPE_MAX];
Panel *panel = nullptr;
EmbeddedProcessBase *embedded_process = nullptr;
@@ -227,12 +248,9 @@ class GameView : public VBoxContainer {
void _update_debugger_buttons();
- void _handle_shortcut_requested(int p_embed_action);
- void _toggle_suspend_button();
void _suspend_button_toggled(bool p_pressed);
void _node_type_pressed(int p_option);
- void _select_mode_pressed(int p_option);
void _game_embed_mode_pressed(int p_option);
void _selection_options_menu_id_pressed(int p_id);
void _game_window_options_menu_menu_id_pressed(int p_id);
@@ -254,6 +272,7 @@ class GameView : public VBoxContainer {
void _embedding_failed();
void _embedded_process_focused();
void _editor_or_project_settings_changed();
+ void _inspector_object_changed();
EmbedAvailability _get_embed_available();
void _update_ui();
@@ -264,7 +283,9 @@ class GameView : public VBoxContainer {
void _update_arguments_for_instance(int p_idx, List<String> &r_arguments);
void _show_update_window_wrapper();
+ void _debug_hide_selection_button_pressed();
void _debug_mute_audio_button_pressed();
+
void _setup_complete();
void _game_window_size_received(const Array &p_state);
void _hdr_state_received(const Array &p_state);
@@ -282,6 +303,8 @@ class GameView : public VBoxContainer {
void _feature_profile_changed();
+ Button *_setup_button(const StringName &p_icon, const Ref<ButtonGroup> &p_group, const Ref<Shortcut> &p_shortcut = nullptr);
+
protected:
void _notification(int p_what);
@@ -296,6 +319,8 @@ public:
void set_window_layout(Ref<ConfigFile> p_layout);
void get_window_layout(Ref<ConfigFile> p_layout);
+ MarginContainer *toolbar_margin = nullptr;
+
GameView(Ref<GameViewDebugger> p_debugger, EmbeddedProcessBase *p_embedded_process, WindowWrapper *p_wrapper);
};
diff --git a/editor/scene/2d/abstract_polygon_2d_editor.cpp b/editor/scene/2d/abstract_polygon_2d_editor.cpp
index f40e58b9667..ce54396a2ed 100644
--- a/editor/scene/2d/abstract_polygon_2d_editor.cpp
+++ b/editor/scene/2d/abstract_polygon_2d_editor.cpp
@@ -390,8 +390,8 @@ bool AbstractPolygon2DEditor::forward_gui_input(const Ref<InputEvent> &p_event)
return (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT);
}
- CanvasItemEditor::Tool tool = CanvasItemEditor::get_singleton()->get_current_tool();
- if (tool != CanvasItemEditor::TOOL_SELECT) {
+ CanvasItemManipulator::Tool tool = CanvasItemEditor::get_singleton()->get_current_tool();
+ if (tool != CanvasItemManipulator::TOOL_SELECT) {
return false;
}
@@ -399,7 +399,7 @@ bool AbstractPolygon2DEditor::forward_gui_input(const Ref<InputEvent> &p_event)
Transform2D xform = canvas_item_editor->get_canvas_transform() * _get_node()->get_screen_transform();
Vector2 gpoint = mb->get_position();
- Vector2 cpoint = canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint));
+ Vector2 cpoint = canvas_item_editor->get_canvas_item_manipulator()->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint));
cpoint = _get_node()->get_screen_transform().affine_inverse().xform(cpoint);
if (mode == MODE_EDIT || (_is_line() && mode == MODE_CREATE)) {
@@ -565,7 +565,7 @@ bool AbstractPolygon2DEditor::forward_gui_input(const Ref<InputEvent> &p_event)
Vector2 gpoint = mm->get_position();
if (center_drag) {
- Vector2 cpoint = canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint));
+ Vector2 cpoint = canvas_item_editor->get_canvas_item_manipulator()->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint));
cpoint = _get_node()->get_screen_transform().affine_inverse().xform(cpoint);
Vector2 delta = center_drag_origin - cpoint;
@@ -583,7 +583,7 @@ bool AbstractPolygon2DEditor::forward_gui_input(const Ref<InputEvent> &p_event)
}
center_drag_origin = cpoint;
} else if (edited_point.valid() && (wip_active || mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {
- Vector2 cpoint = canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint));
+ Vector2 cpoint = canvas_item_editor->get_canvas_item_manipulator()->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint));
cpoint = _get_node()->get_screen_transform().affine_inverse().xform(cpoint);
//Move the point in a single axis. Should only work when editing a polygon and while holding shift.
diff --git a/editor/scene/2d/camera_2d_editor_plugin.cpp b/editor/scene/2d/camera_2d_editor_plugin.cpp
index 8c41c35cdab..2af46cb80d4 100644
--- a/editor/scene/2d/camera_2d_editor_plugin.cpp
+++ b/editor/scene/2d/camera_2d_editor_plugin.cpp
@@ -111,7 +111,7 @@ bool Camera2DEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
}
pos = CanvasItemEditor::get_singleton()->get_canvas_transform().affine_inverse().xform(pos);
- pos = CanvasItemEditor::get_singleton()->snap_point(pos);
+ pos = CanvasItemEditor::get_singleton()->get_canvas_item_manipulator()->snap_point(pos);
switch (drag_type) {
case Drag::LEFT: {
@@ -211,7 +211,7 @@ void Camera2DEditor::_update_overlays_if_needed(Camera2D *p_camera) {
}
void Camera2DEditor::_update_hover(const Vector2 &p_mouse_pos) {
- if (CanvasItemEditor::get_singleton()->get_current_tool() != CanvasItemEditor::TOOL_SELECT) {
+ if (CanvasItemEditor::get_singleton()->get_current_tool() != CanvasItemManipulator::TOOL_SELECT) {
hover_type = Drag::NONE;
CanvasItemEditor::get_singleton()->set_cursor_shape_override();
return;
diff --git a/editor/scene/2d/path_2d_editor_plugin.cpp b/editor/scene/2d/path_2d_editor_plugin.cpp
index b35a8e9304d..f9ca13167a9 100644
--- a/editor/scene/2d/path_2d_editor_plugin.cpp
+++ b/editor/scene/2d/path_2d_editor_plugin.cpp
@@ -89,7 +89,7 @@ bool Path2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) {
Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_screen_transform();
Vector2 gpoint = mb->get_position();
- Vector2 cpoint = canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint));
+ Vector2 cpoint = canvas_item_editor->get_canvas_item_manipulator()->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint));
cpoint = node->to_local(node->get_viewport()->get_popup_base_transform().affine_inverse().xform(cpoint));
if (mb->is_pressed() && action == ACTION_NONE) {
@@ -378,7 +378,7 @@ bool Path2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) {
// Handle point/control movement.
Transform2D xform = canvas_item_editor->get_canvas_transform() * node->get_screen_transform();
Vector2 gpoint = mm->get_position();
- Vector2 cpoint = canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint));
+ Vector2 cpoint = canvas_item_editor->get_canvas_item_manipulator()->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(gpoint));
cpoint = node->to_local(node->get_viewport()->get_popup_base_transform().affine_inverse().xform(cpoint));
Ref<Curve2D> curve = node->get_curve();
diff --git a/editor/scene/2d/physics/cast_2d_editor_plugin.cpp b/editor/scene/2d/physics/cast_2d_editor_plugin.cpp
index 724042f14d2..604e24d38b6 100644
--- a/editor/scene/2d/physics/cast_2d_editor_plugin.cpp
+++ b/editor/scene/2d/physics/cast_2d_editor_plugin.cpp
@@ -105,7 +105,7 @@ bool Cast2DEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_event) {
Ref<InputEventMouseMotion> mm = p_event;
if (mm.is_valid() && pressed) {
- Vector2 point = canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(mm->get_position()));
+ Vector2 point = canvas_item_editor->get_canvas_item_manipulator()->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(mm->get_position()));
point = node->get_screen_transform().affine_inverse().xform(point);
node->set("target_position", point);
diff --git a/editor/scene/2d/physics/collision_shape_2d_editor_plugin.cpp b/editor/scene/2d/physics/collision_shape_2d_editor_plugin.cpp
index 997f379b5d8..7065e9ce125 100644
--- a/editor/scene/2d/physics/collision_shape_2d_editor_plugin.cpp
+++ b/editor/scene/2d/physics/collision_shape_2d_editor_plugin.cpp
@@ -427,7 +427,7 @@ bool CollisionShape2DEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_e
return false;
}
- Vector2 cpoint = canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(mm->get_position()));
+ Vector2 cpoint = canvas_item_editor->get_canvas_item_manipulator()->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(mm->get_position()));
cpoint = (node->get_viewport()->get_popup_base_transform() * node->get_canvas_transform()).affine_inverse().xform(cpoint);
cpoint = original_transform.affine_inverse().xform(cpoint);
last_point = cpoint;
diff --git a/editor/scene/2d/scene_paint_2d_editor_plugin.cpp b/editor/scene/2d/scene_paint_2d_editor_plugin.cpp
index b6f82b7c0c3..7a4712a4b04 100644
--- a/editor/scene/2d/scene_paint_2d_editor_plugin.cpp
+++ b/editor/scene/2d/scene_paint_2d_editor_plugin.cpp
@@ -79,8 +79,8 @@ void ScenePaint2DEditor::_edit(Object *p_object) {
{
#endif
grid = CanvasItemEditor::get_singleton()->is_grid_visible();
- grid_step = CanvasItemEditor::get_singleton()->get_grid_step();
- grid_offset = CanvasItemEditor::get_singleton()->get_grid_offset();
+ grid_step = CanvasItemEditor::get_singleton()->get_canvas_item_manipulator()->get_grid_step();
+ grid_offset = CanvasItemEditor::get_singleton()->get_canvas_item_manipulator()->get_grid_offset();
}
}
@@ -368,9 +368,9 @@ void ScenePaint2DEditor::_add_node_at_pos() {
pos = paint_mode == PAINT_MODE_SNAP_GRID ? cell_pos : (cell_pos + offset);
if (!allow_overlapping) {
- Vector<CanvasItemEditor::SelectResult> results;
- canvas_item_editor->find_canvas_items_at_pos(pos, node, results);
- for (const CanvasItemEditor::SelectResult &result : results) {
+ Vector<DebuggerHelpers::SelectResult> results;
+ canvas_item_editor->get_canvas_item_manipulator()->find_canvas_items_at_pos(pos, node, results);
+ for (const DebuggerHelpers::SelectResult &result : results) {
Node2D *root = _get_node_root(result.item);
if (_is_scene_painted(root)) {
Vector2 root_pos = node->get_global_transform().xform(root->get_position());
@@ -424,10 +424,10 @@ void ScenePaint2DEditor::_remove_node_at_pos() {
}
Node *scene = EditorNode::get_singleton()->get_edited_scene();
- Vector<CanvasItemEditor::SelectResult> results;
- canvas_item_editor->find_canvas_items_at_pos(pos, node, results);
+ Vector<DebuggerHelpers::SelectResult> results;
+ canvas_item_editor->get_canvas_item_manipulator()->find_canvas_items_at_pos(pos, node, results);
- for (const CanvasItemEditor::SelectResult &result : results) {
+ for (const DebuggerHelpers::SelectResult &result : results) {
Node2D *root = _get_node_root(result.item);
if (!_is_scene_painted(root)) {
continue;
@@ -639,10 +639,10 @@ void ScenePaint2DEditor::_update_scene_picker(int p_mode, Control *p_control) {
case PICK_CANVAS_ITEM: {
CanvasItemEditor *canvas_item_editor = CanvasItemEditor::get_singleton();
Vector2 pos = canvas_item_editor->get_canvas_transform().affine_inverse().xform(viewport->get_local_mouse_position());
- Vector<CanvasItemEditor::SelectResult> results;
+ Vector<DebuggerHelpers::SelectResult> results;
Node *scene = EditorNode::get_singleton()->get_edited_scene();
- canvas_item_editor->find_canvas_items_at_pos(pos, scene, results);
- for (const CanvasItemEditor::SelectResult &result : results) {
+ canvas_item_editor->get_canvas_item_manipulator()->find_canvas_items_at_pos(pos, scene, results);
+ for (const DebuggerHelpers::SelectResult &result : results) {
Node2D *root = _get_node_root(result.item);
if (_is_selected_scene_valid(root)) {
node_2d = root;
@@ -755,8 +755,8 @@ ScenePaint2DEditor::PaintMode ScenePaint2DEditor::_reload_paint_mode() {
}
void ScenePaint2DEditor::_grid_step_changed() {
- grid_step = CanvasItemEditor::get_singleton()->get_grid_step();
- grid_offset = CanvasItemEditor::get_singleton()->get_grid_offset();
+ grid_step = CanvasItemEditor::get_singleton()->get_canvas_item_manipulator()->get_grid_step();
+ grid_offset = CanvasItemEditor::get_singleton()->get_canvas_item_manipulator()->get_grid_offset();
}
void ScenePaint2DEditor::_bind_methods() {
@@ -982,7 +982,7 @@ ScenePaint2DEditor::ScenePaint2DEditor() {
void ScenePaint2DEditorPlugin::_canvas_item_tool_changed(int p_tool) {
bool prev_selected = scene_paint_2d_editor->is_tool_selected;
- scene_paint_2d_editor->is_tool_selected = (CanvasItemEditor::Tool)p_tool == CanvasItemEditor::TOOL_SCENE_PAINT;
+ scene_paint_2d_editor->is_tool_selected = (CanvasItemManipulator::Tool)p_tool == CanvasItemManipulator::TOOL_SCENE_PAINT;
if (!scene_paint_2d_editor->is_tool_selected && prev_selected) {
make_visible(false);
scene_paint_2d_editor->_update_hint_label();
diff --git a/editor/scene/canvas_item_editor_plugin.cpp b/editor/scene/canvas_item_editor_plugin.cpp
index 55d0f94dc08..336543bb4c6 100644
--- a/editor/scene/canvas_item_editor_plugin.cpp
+++ b/editor/scene/canvas_item_editor_plugin.cpp
@@ -67,6 +67,8 @@
#include "scene/2d/polygon_2d.h"
#include "scene/2d/skeleton_2d.h"
#include "scene/2d/sprite_2d.h"
+#include "scene/debugger/canvas_item_manipulator.h"
+#include "scene/debugger/debugger_helpers.h"
#include "scene/gui/base_button.h"
#include "scene/gui/flow_container.h"
#include "scene/gui/grid_container.h"
@@ -86,10 +88,6 @@
#include "scene/resources/style_box_texture.h"
#include "servers/rendering/rendering_server.h"
-#define DRAG_THRESHOLD (8 * EDSCALE)
-constexpr real_t SCALE_HANDLE_DISTANCE = 25;
-constexpr real_t MOVE_HANDLE_DISTANCE = 25;
-
class SnapDialog : public ConfirmationDialog {
GDCLASS(SnapDialog, ConfirmationDialog);
@@ -285,244 +283,6 @@ public:
}
};
-bool CanvasItemEditor::_is_node_locked(const Node *p_node) const {
- return p_node->get_meta("_edit_lock_", false);
-}
-
-bool CanvasItemEditor::_is_node_movable(const Node *p_node, bool p_popup_warning) {
- if (_is_node_locked(p_node)) {
- return false;
- }
- if (Object::cast_to<Control>(p_node) && Object::cast_to<Container>(p_node->get_parent())) {
- if (p_popup_warning) {
- EditorToaster::get_singleton()->popup_str(TTR("Children of a container get their position and size determined only by their parent."), EditorToaster::SEVERITY_WARNING);
- }
- return false;
- }
- return true;
-}
-
-void CanvasItemEditor::_snap_if_closer_float(
- const real_t p_value,
- real_t &r_current_snap, SnapTarget &r_current_snap_target,
- const real_t p_target_value, const SnapTarget p_snap_target,
- const real_t p_radius) {
- const real_t radius = p_radius / zoom;
- const real_t dist = Math::abs(p_value - p_target_value);
- if ((p_radius < 0 || dist < radius) && (r_current_snap_target == SNAP_TARGET_NONE || dist < Math::abs(r_current_snap - p_value))) {
- r_current_snap = p_target_value;
- r_current_snap_target = p_snap_target;
- }
-}
-
-void CanvasItemEditor::_snap_if_closer_point(
- Point2 p_value,
- Point2 &r_current_snap, SnapTarget (&r_current_snap_target)[2],
- Point2 p_target_value, const SnapTarget p_snap_target,
- const real_t rotation,
- const real_t p_radius) {
- Transform2D rot_trans = Transform2D(rotation, Point2());
- p_value = rot_trans.inverse().xform(p_value);
- p_target_value = rot_trans.inverse().xform(p_target_value);
- r_current_snap = rot_trans.inverse().xform(r_current_snap);
-
- _snap_if_closer_float(
- p_value.x,
- r_current_snap.x,
- r_current_snap_target[0],
- p_target_value.x,
- p_snap_target,
- p_radius);
-
- _snap_if_closer_float(
- p_value.y,
- r_current_snap.y,
- r_current_snap_target[1],
- p_target_value.y,
- p_snap_target,
- p_radius);
-
- r_current_snap = rot_trans.xform(r_current_snap);
-}
-
-void CanvasItemEditor::_snap_other_nodes(
- const Point2 p_value,
- const Transform2D p_transform_to_snap,
- Point2 &r_current_snap, SnapTarget (&r_current_snap_target)[2],
- const SnapTarget p_snap_target, List<const CanvasItem *> p_exceptions,
- const Node *p_current) {
- const CanvasItem *ci = Object::cast_to<CanvasItem>(p_current);
-
- // Check if the element is in the exception
- bool exception = false;
- for (const CanvasItem *&E : p_exceptions) {
- if (E == p_current) {
- exception = true;
- break;
- }
- };
-
- if (ci && !exception) {
- Transform2D ci_transform = ci->get_screen_transform();
- if (std::fmod(ci_transform.get_rotation() - p_transform_to_snap.get_rotation(), (real_t)360.0) == 0.0) {
- if (ci->_edit_use_rect()) {
- Point2 begin = ci_transform.xform(ci->_edit_get_rect().get_position());
- Point2 end = ci_transform.xform(ci->_edit_get_rect().get_position() + ci->_edit_get_rect().get_size());
-
- _snap_if_closer_point(p_value, r_current_snap, r_current_snap_target, begin, p_snap_target, ci_transform.get_rotation());
- _snap_if_closer_point(p_value, r_current_snap, r_current_snap_target, end, p_snap_target, ci_transform.get_rotation());
- } else {
- Point2 position = ci_transform.xform(Point2());
- _snap_if_closer_point(p_value, r_current_snap, r_current_snap_target, position, p_snap_target, ci_transform.get_rotation());
- }
- }
- }
- for (int i = 0; i < p_current->get_child_count(); i++) {
- _snap_other_nodes(p_value, p_transform_to_snap, r_current_snap, r_current_snap_target, p_snap_target, List<const CanvasItem *>(p_exceptions), p_current->get_child(i));
- }
-}
-
-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;
-
- bool is_snap_active = smart_snap_active ^ Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL);
-
- // Smart snap using the canvas position
- Vector2 output = p_target;
- real_t rotation = 0.0;
-
- if (p_self_canvas_item) {
- rotation = p_self_canvas_item->get_screen_transform().get_rotation();
-
- // Parent sides and center
- if ((is_snap_active && snap_node_parent && (p_modes & SNAP_NODE_PARENT)) || (p_forced_modes & SNAP_NODE_PARENT)) {
- if (const Control *c = Object::cast_to<Control>(p_self_canvas_item)) {
- Point2 begin = p_self_canvas_item->get_screen_transform().xform(_anchor_to_position(c, Point2(0, 0)));
- Point2 end = p_self_canvas_item->get_screen_transform().xform(_anchor_to_position(c, Point2(1, 1)));
- _snap_if_closer_point(p_target, output, snap_target, begin, SNAP_TARGET_PARENT, rotation);
- _snap_if_closer_point(p_target, output, snap_target, (begin + end) / 2.0, SNAP_TARGET_PARENT, rotation);
- _snap_if_closer_point(p_target, output, snap_target, end, SNAP_TARGET_PARENT, rotation);
- } else if (const CanvasItem *parent_ci = Object::cast_to<CanvasItem>(p_self_canvas_item->get_parent())) {
- if (parent_ci->_edit_use_rect()) {
- Point2 begin = p_self_canvas_item->get_transform().affine_inverse().xform(parent_ci->_edit_get_rect().get_position());
- Point2 end = p_self_canvas_item->get_transform().affine_inverse().xform(parent_ci->_edit_get_rect().get_position() + parent_ci->_edit_get_rect().get_size());
- _snap_if_closer_point(p_target, output, snap_target, begin, SNAP_TARGET_PARENT, rotation);
- _snap_if_closer_point(p_target, output, snap_target, (begin + end) / 2.0, SNAP_TARGET_PARENT, rotation);
- _snap_if_closer_point(p_target, output, snap_target, end, SNAP_TARGET_PARENT, rotation);
- } else {
- Point2 position = p_self_canvas_item->get_transform().affine_inverse().xform(Point2());
- _snap_if_closer_point(p_target, output, snap_target, position, SNAP_TARGET_PARENT, rotation);
- }
- }
- }
-
- // Self anchors
- if ((is_snap_active && snap_node_anchors && (p_modes & SNAP_NODE_ANCHORS)) || (p_forced_modes & SNAP_NODE_ANCHORS)) {
- if (const Control *c = Object::cast_to<Control>(p_self_canvas_item)) {
- Point2 begin = p_self_canvas_item->get_screen_transform().xform(_anchor_to_position(c, Point2(c->get_anchor(SIDE_LEFT), c->get_anchor(SIDE_TOP))));
- Point2 end = p_self_canvas_item->get_screen_transform().xform(_anchor_to_position(c, Point2(c->get_anchor(SIDE_RIGHT), c->get_anchor(SIDE_BOTTOM))));
- _snap_if_closer_point(p_target, output, snap_target, begin, SNAP_TARGET_SELF_ANCHORS, rotation);
- _snap_if_closer_point(p_target, output, snap_target, end, SNAP_TARGET_SELF_ANCHORS, rotation);
- }
- }
-
- // Self sides
- if ((is_snap_active && snap_node_sides && (p_modes & SNAP_NODE_SIDES)) || (p_forced_modes & SNAP_NODE_SIDES)) {
- if (p_self_canvas_item->_edit_use_rect()) {
- Point2 begin = p_self_canvas_item->get_screen_transform().xform(p_self_canvas_item->_edit_get_rect().get_position());
- Point2 end = p_self_canvas_item->get_screen_transform().xform(p_self_canvas_item->_edit_get_rect().get_position() + p_self_canvas_item->_edit_get_rect().get_size());
- _snap_if_closer_point(p_target, output, snap_target, begin, SNAP_TARGET_SELF, rotation);
- _snap_if_closer_point(p_target, output, snap_target, end, SNAP_TARGET_SELF, rotation);
- }
- }
-
- // Self center
- if ((is_snap_active && snap_node_center && (p_modes & SNAP_NODE_CENTER)) || (p_forced_modes & SNAP_NODE_CENTER)) {
- if (p_self_canvas_item->_edit_use_rect()) {
- Point2 center = p_self_canvas_item->get_screen_transform().xform(p_self_canvas_item->_edit_get_rect().get_center());
- _snap_if_closer_point(p_target, output, snap_target, center, SNAP_TARGET_SELF, rotation);
- } else {
- Point2 position = p_self_canvas_item->get_screen_transform().xform(Point2());
- _snap_if_closer_point(p_target, output, snap_target, position, SNAP_TARGET_SELF, rotation);
- }
- }
- }
-
- // Other nodes sides
- if ((is_snap_active && snap_other_nodes && (p_modes & SNAP_OTHER_NODES)) || (p_forced_modes & SNAP_OTHER_NODES)) {
- Transform2D to_snap_transform;
- List<const CanvasItem *> exceptions = List<const CanvasItem *>();
- for (const CanvasItem *E : p_other_nodes_exceptions) {
- exceptions.push_back(E);
- }
- if (p_self_canvas_item) {
- exceptions.push_back(p_self_canvas_item);
- to_snap_transform = p_self_canvas_item->get_screen_transform();
- }
-
- _snap_other_nodes(
- p_target, to_snap_transform,
- output, snap_target,
- SNAP_TARGET_OTHER_NODE,
- List<const CanvasItem *>(exceptions),
- get_tree()->get_edited_scene_root());
- }
-
- if (((is_snap_active && snap_guides && (p_modes & SNAP_GUIDES)) || (p_forced_modes & SNAP_GUIDES)) && std::fmod(rotation, (real_t)360.0) == 0.0) {
- // Guides.
- if (Node *scene = EditorNode::get_singleton()->get_edited_scene()) {
- Array vguides = scene->get_meta("_edit_vertical_guides_", Array());
- for (int i = 0; i < vguides.size(); i++) {
- _snap_if_closer_float(p_target.x, output.x, snap_target[0], vguides[i], SNAP_TARGET_GUIDE);
- }
-
- Array hguides = scene->get_meta("_edit_horizontal_guides_", Array());
- for (int i = 0; i < hguides.size(); i++) {
- _snap_if_closer_float(p_target.y, output.y, snap_target[1], hguides[i], SNAP_TARGET_GUIDE);
- }
- }
- }
-
- if (((grid_snap_active && (p_modes & SNAP_GRID)) || (p_forced_modes & SNAP_GRID)) && std::fmod(rotation, (real_t)360.0) == 0.0) {
- // Grid
- Point2 offset = grid_offset;
- if (snap_relative) {
- List<CanvasItem *> selection = _get_edited_canvas_items();
- if (selection.size() == 1 && Object::cast_to<Node2D>(selection.front()->get())) {
- offset = Object::cast_to<Node2D>(selection.front()->get())->get_global_position();
- } else if (selection.size() > 0) {
- offset = _get_encompassing_rect_from_list(selection).position;
- }
- }
- Point2 grid_output;
- grid_output.x = Math::snapped(p_target.x - offset.x, grid_step.x * Math::pow(2.0, grid_step_multiplier)) + offset.x;
- grid_output.y = Math::snapped(p_target.y - offset.y, grid_step.y * Math::pow(2.0, grid_step_multiplier)) + offset.y;
- _snap_if_closer_point(p_target, output, snap_target, grid_output, SNAP_TARGET_GRID, 0.0, -1.0);
- }
-
- if (((snap_pixel && (p_modes & SNAP_PIXEL)) || (p_forced_modes & SNAP_PIXEL)) && rotation == 0.0) {
- // Pixel
- output = output.snappedf(1);
- }
-
- snap_transform = Transform2D(rotation, output);
-
- return output;
-}
-
-real_t CanvasItemEditor::snap_angle(real_t p_target, real_t p_start) const {
- if (((smart_snap_active || snap_rotation) ^ Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) && snap_rotation_step != 0) {
- if (snap_relative) {
- return Math::snapped(p_target - snap_rotation_offset, snap_rotation_step) + snap_rotation_offset + (p_start - (int)(p_start / snap_rotation_step) * snap_rotation_step);
- } else {
- return Math::snapped(p_target - snap_rotation_offset, snap_rotation_step) + snap_rotation_offset;
- }
- } else {
- return p_target;
- }
-}
-
void CanvasItemEditor::shortcut_input(const Ref<InputEvent> &p_ev) {
ERR_FAIL_COND(p_ev.is_null());
@@ -537,16 +297,16 @@ void CanvasItemEditor::shortcut_input(const Ref<InputEvent> &p_ev) {
viewport->queue_redraw();
}
- if (k->is_pressed() && !k->is_command_or_control_pressed() && !k->is_echo() && (grid_snap_active || is_grid_visible())) {
+ if (k->is_pressed() && !k->is_command_or_control_pressed() && !k->is_echo() && (ci_manipulator->is_grid_snap_enabled() || is_grid_visible())) {
if (multiply_grid_step_shortcut.is_valid() && multiply_grid_step_shortcut->matches_event(p_ev)) {
// Multiply the grid size
- grid_step_multiplier = MIN(grid_step_multiplier + 1, 12);
+ ci_manipulator->set_grid_step_multiplier(MIN(ci_manipulator->get_grid_step_multiplier() + 1, 12));
viewport->queue_redraw();
} else if (divide_grid_step_shortcut.is_valid() && divide_grid_step_shortcut->matches_event(p_ev)) {
// Divide the grid size
- Point2 new_grid_step = grid_step * Math::pow(2.0, grid_step_multiplier - 1);
+ Point2 new_grid_step = ci_manipulator->get_grid_step() * Math::pow(2.0, ci_manipulator->get_grid_step_multiplier() - 1);
if (new_grid_step.x >= 1.0 && new_grid_step.y >= 1.0) {
- grid_step_multiplier--;
+ ci_manipulator->set_grid_step_multiplier(ci_manipulator->get_grid_step_multiplier() - 1);
}
viewport->queue_redraw();
}
@@ -634,7 +394,7 @@ void CanvasItemEditor::_expand_encompassing_rect_using_children(Rect2 &r_rect, c
}
}
- if (ci && ci->is_visible_in_tree() && (include_locked_nodes || !_is_node_locked(ci))) {
+ if (ci && ci->is_visible_in_tree() && (include_locked_nodes || !CanvasItemManipulator::is_node_locked(ci))) {
Transform2D xform = p_canvas_xform;
if (!ci->is_set_as_top_level()) {
xform *= p_parent_xform;
@@ -660,175 +420,15 @@ Rect2 CanvasItemEditor::_get_encompassing_rect(const Node *p_node) {
return rect;
}
-void CanvasItemEditor::find_canvas_items_at_pos(const Point2 &p_pos, Node *p_node, Vector<SelectResult> &r_items, const Transform2D &p_parent_xform, const Transform2D &p_canvas_xform) {
- if (!p_node) {
- return;
- }
-
- CanvasItem *ci = Object::cast_to<CanvasItem>(p_node);
-
- Transform2D xform = p_canvas_xform;
- if (CanvasLayer *cl = Object::cast_to<CanvasLayer>(p_node)) {
- xform = cl->get_transform();
- } else if (Viewport *vp = Object::cast_to<Viewport>(p_node)) {
- 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;
- }
- }
-
- for (int i = p_node->get_child_count() - 1; i >= 0; i--) {
- if (ci) {
- if (!ci->is_set_as_top_level()) {
- find_canvas_items_at_pos(p_pos, p_node->get_child(i), r_items, p_parent_xform * ci->get_transform(), xform);
- } else {
- find_canvas_items_at_pos(p_pos, p_node->get_child(i), r_items, ci->get_transform(), xform);
- }
- } else {
- find_canvas_items_at_pos(p_pos, p_node->get_child(i), r_items, Transform2D(), xform);
- }
- }
-
- if (ci && ci->is_visible_in_tree()) {
- if (!ci->is_set_as_top_level()) {
- xform *= p_parent_xform;
- }
- xform = (xform * ci->get_transform()).affine_inverse();
- const real_t local_grab_distance = xform.basis_xform(Vector2(grab_distance, 0)).length() / zoom;
- if (ci->_edit_is_selected_on_click(xform.xform(p_pos), local_grab_distance)) {
- Node2D *node = Object::cast_to<Node2D>(ci);
-
- SelectResult res;
- res.item = ci;
- res.z_index = node ? node->get_z_index() : 0;
- res.has_z = node;
- r_items.push_back(res);
- }
- }
-}
-
-void CanvasItemEditor::_get_canvas_items_at_pos(const Point2 &p_pos, Vector<SelectResult> &r_items, bool p_allow_locked) {
- Node *scene = EditorNode::get_singleton()->get_edited_scene();
-
- find_canvas_items_at_pos(p_pos, scene, r_items);
-
- //Remove invalid results
- 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 (node && node != get_tree()->get_edited_scene_root()) {
- node = scene->get_deepest_editable_node(node);
- }
-
- CanvasItem *ci = Object::cast_to<CanvasItem>(node);
- if (!p_allow_locked) {
- // Replace the node by the group if grouped
- while (node && node != scene->get_parent()) {
- CanvasItem *ci_tmp = Object::cast_to<CanvasItem>(node);
- if (ci_tmp && node->has_meta("_edit_group_")) {
- ci = ci_tmp;
- }
- node = node->get_parent();
- }
- }
-
- // Check if the canvas item is already in the list (for groups or scenes)
- bool duplicate = false;
- for (int j = 0; j < i; j++) {
- if (r_items[j].item == ci) {
- duplicate = true;
- break;
- }
- }
-
- //Remove the item if invalid
- if (!ci || duplicate || (ci != scene && ci->get_owner() != scene && !scene->is_editable_instance(ci->get_owner())) || (!p_allow_locked && _is_node_locked(ci))) {
- r_items.remove_at(i);
- i--;
- } else {
- r_items.write[i].item = ci;
- }
- }
-}
-
-void CanvasItemEditor::_find_canvas_items_in_rect(const Rect2 &p_rect, Node *p_node, List<CanvasItem *> *r_items, const Transform2D &p_parent_xform, const Transform2D &p_canvas_xform) {
- if (!p_node) {
- return;
- }
- CanvasItem *ci = Object::cast_to<CanvasItem>(p_node);
- Node *scene = EditorNode::get_singleton()->get_edited_scene();
-
- if (p_node != scene && !p_node->get_owner()) {
- return;
- }
-
- bool editable = p_node == scene || p_node->get_owner() == scene || p_node == scene->get_deepest_editable_node(p_node);
- bool lock_children = p_node->get_meta("_edit_group_", false);
- bool locked = _is_node_locked(p_node);
-
- Transform2D xform = p_canvas_xform;
- if (CanvasLayer *cl = Object::cast_to<CanvasLayer>(p_node)) {
- xform = cl->get_transform();
- } else if (Viewport *vp = Object::cast_to<Viewport>(p_node)) {
- 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;
- }
- }
-
- if (!lock_children || !editable) {
- for (int i = p_node->get_child_count() - 1; i >= 0; i--) {
- if (ci) {
- if (!ci->is_set_as_top_level()) {
- _find_canvas_items_in_rect(p_rect, p_node->get_child(i), r_items, p_parent_xform * ci->get_transform(), xform);
- } else {
- _find_canvas_items_in_rect(p_rect, p_node->get_child(i), r_items, ci->get_transform(), xform);
- }
- } else {
- CanvasLayer *cl = Object::cast_to<CanvasLayer>(p_node);
- _find_canvas_items_in_rect(p_rect, p_node->get_child(i), r_items, Transform2D(), cl ? cl->get_transform() : xform);
- }
- }
- }
-
- if (ci && ci->is_visible_in_tree() && !locked && editable) {
- if (!ci->is_set_as_top_level()) {
- xform *= p_parent_xform;
- }
- xform *= ci->get_transform();
-
- if (ci->_edit_use_rect()) {
- Rect2 rect = ci->_edit_get_rect();
- if (p_rect.has_point(xform.xform(rect.position)) &&
- p_rect.has_point(xform.xform(rect.position + Vector2(rect.size.x, 0))) &&
- p_rect.has_point(xform.xform(rect.position + Vector2(rect.size.x, rect.size.y))) &&
- p_rect.has_point(xform.xform(rect.position + Vector2(0, rect.size.y)))) {
- r_items->push_back(ci);
- }
- } else {
- if (p_rect.has_point(xform.xform(Point2()))) {
- r_items->push_back(ci);
- }
- }
- }
-}
-
Dictionary CanvasItemEditor::_get_context_data() {
const Vector2 mouse_pos = transform.affine_inverse().xform(viewport->get_local_mouse_position());
- Vector<SelectResult> currently_hovered;
- _get_canvas_items_at_pos(mouse_pos, currently_hovered, true);
+ Vector<DebuggerHelpers::SelectResult> currently_hovered;
+ ci_manipulator->get_canvas_items_at_pos(mouse_pos, currently_hovered, true);
TypedArray<CanvasItem *> items;
items.reserve(currently_hovered.size());
- for (const SelectResult &result : currently_hovered) {
+ for (const DebuggerHelpers::SelectResult &result : currently_hovered) {
items.push_back(result.item);
}
@@ -838,13 +438,15 @@ Dictionary CanvasItemEditor::_get_context_data() {
return context_data;
}
-bool CanvasItemEditor::_select_click_on_item(CanvasItem *item, Point2 p_click_pos, bool p_append) {
+bool CanvasItemEditor::_select_click_on_item(const Variant &p_node, bool p_append) {
+ Node *node = Object::cast_to<Node>(p_node);
bool still_selected = true;
const List<Node *> &top_node_list = editor_selection->get_top_selected_node_list();
+
if (p_append && !top_node_list.is_empty()) {
- if (editor_selection->is_selected(item)) {
+ if (editor_selection->is_selected(node)) {
// Already in the selection, remove it from the selected nodes
- editor_selection->remove_node(item);
+ editor_selection->remove_node(node);
still_selected = false;
if (top_node_list.size() == 1) {
@@ -852,19 +454,18 @@ bool CanvasItemEditor::_select_click_on_item(CanvasItem *item, Point2 p_click_po
}
} else {
// Add the item to the selection
- editor_selection->add_node(item);
+ editor_selection->add_node(node);
}
} else {
- if (!editor_selection->is_selected(item)) {
+ if (!editor_selection->is_selected(node)) {
// Select a new one and clear previous selection
editor_selection->clear();
- editor_selection->add_node(item);
+ editor_selection->add_node(node);
// Reselect
- if (Engine::get_singleton()->is_editor_hint()) {
- selected_from_canvas = true;
- }
+ selected_from_canvas = true;
}
}
+
viewport->queue_redraw();
return still_selected;
}
@@ -874,7 +475,7 @@ List<CanvasItem *> CanvasItemEditor::_get_edited_canvas_items(bool p_retrieve_lo
for (const KeyValue<ObjectID, Object *> &E : editor_selection->get_selection()) {
CanvasItem *ci = ObjectDB::get_instance<CanvasItem>(E.key);
if (ci) {
- if (ci->is_visible_in_tree() && (p_retrieve_locked || !_is_node_locked(ci))) {
+ if (ci->is_visible_in_tree() && (p_retrieve_locked || !CanvasItemManipulator::is_node_locked(ci))) {
Viewport *vp = ci->get_viewport();
if (vp && !vp->is_visible_subviewport()) {
continue;
@@ -935,11 +536,153 @@ Vector2 CanvasItemEditor::_position_to_anchor(const Control *p_control, Vector2
return output;
}
-void CanvasItemEditor::_save_canvas_item_state(const List<CanvasItem *> &p_canvas_items, bool save_bones) {
+bool CanvasItemEditor::_get_selection_callback(Array r_selection) {
+ bool has_locked_items = false;
+ List<CanvasItem *> selection = _get_edited_canvas_items(false, true, &has_locked_items);
+ for (const CanvasItem *ci : selection) {
+ r_selection.append(ci);
+ }
+ return has_locked_items;
+}
+
+void CanvasItemEditor::_box_selection_updated(const Rect2 &p_rect) {
+ box_selection_rect = transform.xform(p_rect);
+ viewport->queue_redraw();
+}
+
+void CanvasItemEditor::_box_selected(const Array &p_selection) {
+ if (p_selection.size() == 1 && editor_selection->get_selection().is_empty()) {
+ EditorNode::get_singleton()->push_item(p_selection.front());
+ }
+
+ for (const Variant &var : p_selection) {
+ CanvasItem *ci = Object::cast_to<CanvasItem>(var);
+ editor_selection->add_node(ci);
+ }
+}
+
+void CanvasItemEditor::_clear_selection_requested(bool p_selected_from_canvas) {
+ editor_selection->clear();
+ viewport->queue_redraw();
+ selected_from_canvas = p_selected_from_canvas;
+}
+
+void CanvasItemEditor::_selection_menu_requested(const Array &p_selection, const Point2 &p_pos, bool p_append) {
+ NodePath root_path = get_tree()->get_edited_scene_root()->get_path();
+ StringName root_name = root_path.get_name(root_path.get_name_count() - 1);
+ int icon_max_width = EditorNode::get_singleton()->get_editor_theme()->get_constant(SNAME("class_icon_size"), EditorStringName(Editor));
+
+ for (const Variant &var : p_selection) {
+ Node *node = Object::cast_to<Node>(var);
+ Ref<Texture2D> icon = EditorNode::get_singleton()->get_object_icon(node);
+ String node_path = "/" + root_name + "/" + String(root_path.rel_path_to(node->get_path()));
+
+ int locked = 0;
+ if (CanvasItemManipulator::is_node_locked(node)) {
+ locked = 1;
+ } else {
+ Node *scene = EditorNode::get_singleton()->get_edited_scene();
+ Node *node_tmp = node;
+
+ while (node_tmp && node_tmp != scene->get_parent()) {
+ if (node_tmp->has_meta("_edit_group_")) {
+ locked = 2;
+ }
+ node_tmp = node_tmp->get_parent();
+ }
+ }
+
+ String suffix;
+ if (locked == 1) {
+ suffix = " (" + TTR("Locked") + ")";
+ } else if (locked == 2) {
+ suffix = " (" + TTR("Grouped") + ")";
+ }
+
+ int idx = selection_menu->get_item_count();
+ selection_menu->add_item((String)node->get_name() + suffix);
+ selection_menu->set_item_icon(idx, icon);
+ selection_menu->set_item_icon_max_width(idx, icon_max_width);
+ selection_menu->set_item_metadata(idx, node_path);
+ selection_menu->set_item_tooltip(idx, String(node->get_name()) + "\nType: " + node->get_class() + "\nPath: " + node_path);
+ }
+
+ selection_results_menu = p_selection;
+ selection_menu_additive_selection = p_append;
+ selection_menu->set_position(viewport->get_screen_transform().xform(p_pos));
+ selection_menu->reset_size();
+ selection_menu->popup();
+}
+
+void CanvasItemEditor::_tool_menu_requested(const Point2 &p_pos) {
+ add_node_menu->clear();
+ add_node_menu->add_icon_item(get_editor_theme_icon(SNAME("Add")), TTRC("Add 2D Node Here..."), ADD_NODE);
+ add_node_menu->add_icon_item(get_editor_theme_icon(SNAME("Instance")), TTRC("Instantiate Scene Here..."), ADD_INSTANCE);
+ for (Node *node : SceneTreeDock::get_singleton()->get_node_clipboard()) {
+ if (Object::cast_to<CanvasItem>(node)) {
+ add_node_menu->add_icon_item(get_editor_theme_icon(SNAME("ActionPaste")), TTRC("Paste Node(s) Here"), ADD_PASTE);
+ break;
+ }
+ }
+ for (Node *node : EditorNode::get_singleton()->get_editor_selection()->get_top_selected_node_list()) {
+ if (Object::cast_to<CanvasItem>(node) && ci_manipulator->is_node_movable(node, false)) {
+ add_node_menu->add_icon_item(get_editor_theme_icon(SNAME("ToolMove")), TTRC("Move Node(s) Here"), ADD_MOVE);
+ break;
+ }
+ }
+
+ if (EditorContextMenuPluginManager::get_singleton()->has_plugins_for_slot(EditorContextMenuPlugin::CONTEXT_SLOT_2D_EDITOR)) {
+ EditorContextMenuPlugin::OptionsData context_data = _get_context_data();
+ EditorContextMenuPluginManager::get_singleton()->add_options_from_plugins(add_node_menu, EditorContextMenuPlugin::CONTEXT_SLOT_2D_EDITOR, context_data);
+
+#ifndef DISABLE_DEPRECATED
+ Array items = context_data["hovered_nodes"];
+ PackedStringArray paths;
+ paths.reserve_exact(items.size());
+ for (const Variant &var : items) {
+ CanvasItem *ci = Object::cast_to<CanvasItem>(var);
+ paths.append((String)ci->get_path());
+ }
+ EditorContextMenuPluginManager::get_singleton()->add_options_from_plugins(add_node_menu, EditorContextMenuPlugin::CONTEXT_SLOT_2D_EDITOR, paths, items, 500);
+#endif
+ }
+
+ add_node_menu->reset_size();
+ add_node_menu->set_position(viewport->get_screen_transform().xform(p_pos));
+ add_node_menu->popup();
+ node_create_position = transform.affine_inverse().xform(p_pos);
+}
+
+void CanvasItemEditor::_commit_guide_meta_requested(const String &p_msg, const Dictionary &p_meta) {
+ EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
+ undo_redo->create_action(p_msg);
+
+ for (const KeyValue<Variant, Variant> &kv : p_meta) {
+ Array guides = kv.value;
+ if (guides.is_empty()) {
+ undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "remove_meta", kv.key);
+ } else {
+ undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", kv.key, guides);
+ }
+
+ Array prev_guides = EditorNode::get_singleton()->get_edited_scene()->get_meta(kv.key, Array());
+ if (prev_guides.is_empty()) {
+ undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "remove_meta", kv.key);
+ } else {
+ undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", kv.key, prev_guides);
+ }
+ }
+
+ undo_redo->add_undo_method(viewport, "queue_redraw");
+ undo_redo->commit_action();
+}
+
+void CanvasItemEditor::_save_canvas_state_requested(const Array &p_selection, bool p_save_bones) {
original_transform = Transform2D();
bool transform_stored = false;
- for (CanvasItem *ci : p_canvas_items) {
+ for (const Variant &var : p_selection) {
+ CanvasItem *ci = Object::cast_to<CanvasItem>(var);
CanvasItemEditorSelectedItem *se = editor_selection->get_node_editor_data<CanvasItemEditorSelectedItem>(ci);
if (se) {
if (!transform_stored) {
@@ -958,16 +701,22 @@ void CanvasItemEditor::_save_canvas_item_state(const List<CanvasItem *> &p_canva
}
}
-void CanvasItemEditor::_restore_canvas_item_state(const List<CanvasItem *> &p_canvas_items, bool restore_bones) {
- for (CanvasItem *ci : drag_selection) {
+void CanvasItemEditor::_restore_canvas_state_requested(const Array &p_selection, bool p_restore_bones) {
+ message = "";
+
+ for (const Variant &var : p_selection) {
+ CanvasItem *ci = Object::cast_to<CanvasItem>(var);
CanvasItemEditorSelectedItem *se = editor_selection->get_node_editor_data<CanvasItemEditorSelectedItem>(ci);
ci->_edit_set_state(se->undo_state);
}
}
-void CanvasItemEditor::_commit_canvas_item_state(const List<CanvasItem *> &p_canvas_items, const String &action_name, bool commit_bones) {
+void CanvasItemEditor::_commit_canvas_state_requested(const Array &p_selection, const String &p_message, bool p_commit_bones) {
+ message = "";
+
List<CanvasItem *> modified_canvas_items;
- for (CanvasItem *ci : p_canvas_items) {
+ for (const Variant &var : p_selection) {
+ CanvasItem *ci = Object::cast_to<CanvasItem>(var);
Dictionary old_state = editor_selection->get_node_editor_data<CanvasItemEditorSelectedItem>(ci)->undo_state;
Dictionary new_state = ci->_edit_get_state();
@@ -981,13 +730,13 @@ void CanvasItemEditor::_commit_canvas_item_state(const List<CanvasItem *> &p_can
}
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
- undo_redo->create_action(action_name);
+ undo_redo->create_action(p_message);
for (CanvasItem *ci : modified_canvas_items) {
CanvasItemEditorSelectedItem *se = editor_selection->get_node_editor_data<CanvasItemEditorSelectedItem>(ci);
if (se) {
undo_redo->add_do_method(ci, "_edit_set_state", ci->_edit_get_state());
undo_redo->add_undo_method(ci, "_edit_set_state", se->undo_state);
- if (commit_bones) {
+ if (p_commit_bones) {
for (const Dictionary &F : se->pre_drag_bones_undo_state) {
ci = Object::cast_to<CanvasItem>(ci->get_parent());
undo_redo->add_do_method(ci, "_edit_set_state", ci->_edit_get_state());
@@ -999,9 +748,47 @@ void CanvasItemEditor::_commit_canvas_item_state(const List<CanvasItem *> &p_can
undo_redo->add_do_method(viewport, "queue_redraw");
undo_redo->add_undo_method(viewport, "queue_redraw");
undo_redo->commit_action();
+
+ if (!key_auto_insert_button->is_pressed()) {
+ return;
+ }
+
+ switch (ci_manipulator->get_drag_type()) {
+ case CanvasItemManipulator::DRAG_ROTATE: {
+ _insert_animation_keys(false, true, false, true);
+ } break;
+
+ case CanvasItemManipulator::DRAG_LEFT:
+ case CanvasItemManipulator::DRAG_RIGHT:
+ case CanvasItemManipulator::DRAG_TOP:
+ case CanvasItemManipulator::DRAG_BOTTOM:
+ case CanvasItemManipulator::DRAG_TOP_LEFT:
+ case CanvasItemManipulator::DRAG_TOP_RIGHT:
+ case CanvasItemManipulator::DRAG_BOTTOM_LEFT:
+ case CanvasItemManipulator::DRAG_BOTTOM_RIGHT:
+ case CanvasItemManipulator::DRAG_SCALE_BOTH:
+ case CanvasItemManipulator::DRAG_SCALE_X:
+ case CanvasItemManipulator::DRAG_SCALE_Y: {
+ _insert_animation_keys(false, false, true, true);
+ } break;
+
+ case CanvasItemManipulator::DRAG_MOVE:
+ case CanvasItemManipulator::DRAG_MOVE_X:
+ case CanvasItemManipulator::DRAG_MOVE_Y: {
+ _insert_animation_keys(true, false, false, true);
+ } break;
+
+ default:
+ break;
+ }
}
void CanvasItemEditor::_snap_changed() {
+ Point2 grid_offset;
+ Point2 grid_step;
+ real_t snap_rotation_offset = 0;
+ real_t snap_rotation_step = 0;
+ real_t snap_scale_step = 0;
static_cast<SnapDialog *>(snap_dialog)->get_fields(grid_offset, grid_step, primary_grid_step, snap_rotation_offset, snap_rotation_step, snap_scale_step);
EditorSettings::get_singleton()->set_project_metadata("2d_editor", "grid_offset", grid_offset);
@@ -1011,7 +798,13 @@ void CanvasItemEditor::_snap_changed() {
EditorSettings::get_singleton()->set_project_metadata("2d_editor", "snap_rotation_step", snap_rotation_step);
EditorSettings::get_singleton()->set_project_metadata("2d_editor", "snap_scale_step", snap_scale_step);
- grid_step_multiplier = 0;
+ ci_manipulator->set_grid_offset(grid_offset);
+ ci_manipulator->set_grid_step(grid_step);
+ ci_manipulator->set_grid_step_multiplier(0);
+ ci_manipulator->set_snap_rotation_offset(snap_rotation_offset);
+ ci_manipulator->set_snap_rotation_step(snap_rotation_step);
+ ci_manipulator->set_snap_scale_step(snap_scale_step);
+
viewport->queue_redraw();
emit_signal("snap_changed");
}
@@ -1021,16 +814,16 @@ void CanvasItemEditor::_selection_result_pressed(int p_result) {
return;
}
- CanvasItem *item = selection_results_menu[p_result].item;
-
- if (item) {
- _select_click_on_item(item, Point2(), selection_menu_additive_selection);
+ Variant var = selection_results_menu[p_result];
+ if (var) {
+ _select_click_on_item(var, selection_menu_additive_selection);
}
+
selection_results_menu.clear();
}
void CanvasItemEditor::_selection_menu_hide() {
- selection_results.clear();
+ selection_results_menu.clear();
selection_menu->clear();
selection_menu->reset_size();
}
@@ -1073,7 +866,7 @@ void CanvasItemEditor::_add_node_pressed(int p_result) {
for (Node *node : nodes_to_move) {
CanvasItem *ci = Object::cast_to<CanvasItem>(node);
if (ci) {
- if (!_is_node_movable(ci, true)) {
+ if (!ci_manipulator->is_node_movable(ci, true)) {
continue;
}
Transform2D xform = ci->get_transform() * ci->get_global_transform_with_canvas().affine_inverse();
@@ -1143,7 +936,7 @@ bool CanvasItemEditor::is_grid_visible() const {
case GRID_VISIBILITY_SHOW:
return true;
case GRID_VISIBILITY_SHOW_WHEN_SNAPPING:
- return grid_snap_active;
+ return ci_manipulator->is_grid_snap_enabled();
case GRID_VISIBILITY_HIDE:
return false;
}
@@ -1169,7 +962,7 @@ void CanvasItemEditor::_on_grid_menu_id_pressed(int p_id) {
}
// Toggle grid: go to the least restrictive option possible.
- if (grid_snap_active) {
+ if (ci_manipulator->is_grid_snap_enabled()) {
switch (grid_visibility) {
case GRID_VISIBILITY_SHOW:
case GRID_VISIBILITY_SHOW_WHEN_SNAPPING:
@@ -1215,1572 +1008,114 @@ void CanvasItemEditor::_reset_transform(TransformType p_type) {
break;
case TransformType::SCALE:
undo_redo->add_undo_method(res_node, "set_scale", res_node->get_scale());
- undo_redo->add_do_method(res_node, "set_scale", Size2(1, 1));
- break;
- }
- continue;
- }
- Control *res_control = Object::cast_to<Control>(node);
- if (res_control) {
- switch (p_type) {
- case TransformType::POSITION:
- undo_redo->add_undo_method(res_control, "set_position", res_control->get_position());
- undo_redo->add_do_method(res_control, "set_position", Vector2());
- break;
- case TransformType::ROTATION:
- undo_redo->add_undo_method(res_control, "set_rotation", res_control->get_rotation());
- undo_redo->add_do_method(res_control, "set_rotation", 0);
- break;
- case TransformType::SCALE:
- undo_redo->add_undo_method(res_control, "set_scale", res_control->get_scale());
- undo_redo->add_do_method(res_control, "set_scale", Size2(1, 1));
- break;
- }
- }
- }
- undo_redo->commit_action();
-}
-
-void CanvasItemEditor::_switch_theme_preview(int p_mode) {
- view_menu->get_popup()->hide();
-
- if (theme_preview == p_mode) {
- return;
- }
- theme_preview = (ThemePreviewMode)p_mode;
- EditorSettings::get_singleton()->set_project_metadata("2d_editor", "theme_preview", theme_preview);
-
- for (int i = 0; i < THEME_PREVIEW_MAX; i++) {
- theme_menu->set_item_checked(i, i == theme_preview);
- }
-
- EditorNode::get_singleton()->update_preview_themes(theme_preview);
-}
-
-bool CanvasItemEditor::_gui_input_rulers_and_guides(const Ref<InputEvent> &p_event) {
- EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
- Ref<InputEventMouseButton> b = p_event;
- Ref<InputEventMouseMotion> m = p_event;
-
- if (drag_type == DRAG_NONE) {
- if (show_guides && show_rulers && EditorNode::get_singleton()->get_edited_scene()) {
- Transform2D xform = viewport_scrollable->get_transform() * transform;
- // Retrieve the guide lists
- Array vguides = EditorNode::get_singleton()->get_edited_scene()->get_meta("_edit_vertical_guides_", Array());
- Array hguides = EditorNode::get_singleton()->get_edited_scene()->get_meta("_edit_horizontal_guides_", Array());
-
- // Hover over guides
- real_t minimum = 1e20;
- is_hovering_h_guide = false;
- is_hovering_v_guide = false;
-
- if (m.is_valid() && m->get_position().x < ruler_width_scaled) {
- // Check if we are hovering an existing horizontal guide
- for (int i = 0; i < hguides.size(); i++) {
- if (Math::abs(xform.xform(Point2(0, hguides[i])).y - m->get_position().y) < MIN(minimum, 8)) {
- is_hovering_h_guide = true;
- is_hovering_v_guide = false;
- break;
- }
- }
-
- } else if (m.is_valid() && m->get_position().y < ruler_width_scaled) {
- // Check if we are hovering an existing vertical guide
- for (int i = 0; i < vguides.size(); i++) {
- if (Math::abs(xform.xform(Point2(vguides[i], 0)).x - m->get_position().x) < MIN(minimum, 8)) {
- is_hovering_v_guide = true;
- is_hovering_h_guide = false;
- break;
- }
- }
- }
-
- // Start dragging a guide
- if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && b->is_pressed()) {
- // Press button
- if (b->get_position().x < ruler_width_scaled && b->get_position().y < ruler_width_scaled) {
- // Drag a new double guide
- drag_type = DRAG_DOUBLE_GUIDE;
- dragged_guide_index = -1;
- return true;
- } else if (b->get_position().x < ruler_width_scaled) {
- // Check if we drag an existing horizontal guide
- dragged_guide_index = -1;
- for (int i = 0; i < hguides.size(); i++) {
- if (Math::abs(xform.xform(Point2(0, hguides[i])).y - b->get_position().y) < MIN(minimum, 8)) {
- dragged_guide_index = i;
- }
- }
-
- if (dragged_guide_index >= 0) {
- // Drag an existing horizontal guide
- drag_type = DRAG_H_GUIDE;
- } else {
- // Drag a new vertical guide
- drag_type = DRAG_V_GUIDE;
- }
- return true;
- } else if (b->get_position().y < ruler_width_scaled) {
- // Check if we drag an existing vertical guide
- dragged_guide_index = -1;
- for (int i = 0; i < vguides.size(); i++) {
- if (Math::abs(xform.xform(Point2(vguides[i], 0)).x - b->get_position().x) < MIN(minimum, 8)) {
- dragged_guide_index = i;
- }
- }
-
- if (dragged_guide_index >= 0) {
- // Drag an existing vertical guide
- drag_type = DRAG_V_GUIDE;
- } else {
- // Drag a new vertical guide
- drag_type = DRAG_H_GUIDE;
- }
- drag_from = xform.affine_inverse().xform(b->get_position());
- return true;
- }
- }
- }
- }
-
- if (drag_type == DRAG_DOUBLE_GUIDE || drag_type == DRAG_V_GUIDE || drag_type == DRAG_H_GUIDE) {
- // Move the guide
- if (m.is_valid()) {
- Transform2D xform = viewport_scrollable->get_transform() * transform;
- drag_to = xform.affine_inverse().xform(m->get_position());
-
- dragged_guide_pos = xform.xform(snap_point(drag_to, SNAP_GRID | SNAP_PIXEL | SNAP_OTHER_NODES));
- viewport->queue_redraw();
- return true;
- }
-
- // Release confirms the guide move
- if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && !b->is_pressed()) {
- if (show_guides && EditorNode::get_singleton()->get_edited_scene()) {
- Transform2D xform = viewport_scrollable->get_transform() * transform;
-
- // Retrieve the guide lists
- Array vguides = EditorNode::get_singleton()->get_edited_scene()->get_meta("_edit_vertical_guides_", Array());
- Array hguides = EditorNode::get_singleton()->get_edited_scene()->get_meta("_edit_horizontal_guides_", Array());
-
- Point2 edited = snap_point(xform.affine_inverse().xform(b->get_position()), SNAP_GRID | SNAP_PIXEL | SNAP_OTHER_NODES);
- if (drag_type == DRAG_V_GUIDE) {
- Array prev_vguides = vguides.duplicate();
- if (b->get_position().x > ruler_width_scaled) {
- // Adds a new vertical guide
- if (dragged_guide_index >= 0) {
- vguides[dragged_guide_index] = edited.x;
- undo_redo->create_action(TTR("Move Vertical Guide"));
- undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_vertical_guides_", vguides);
- undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_vertical_guides_", prev_vguides);
- undo_redo->add_undo_method(viewport, "queue_redraw");
- undo_redo->commit_action();
- } else {
- vguides.push_back(edited.x);
- undo_redo->create_action(TTR("Create Vertical Guide"));
- undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_vertical_guides_", vguides);
- if (prev_vguides.is_empty()) {
- undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "remove_meta", "_edit_vertical_guides_");
- } else {
- undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_vertical_guides_", prev_vguides);
- }
- undo_redo->add_undo_method(viewport, "queue_redraw");
- undo_redo->commit_action();
- }
- } else {
- if (dragged_guide_index >= 0) {
- vguides.remove_at(dragged_guide_index);
- undo_redo->create_action(TTR("Remove Vertical Guide"));
- if (vguides.is_empty()) {
- undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "remove_meta", "_edit_vertical_guides_");
- } else {
- undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_vertical_guides_", vguides);
- }
- undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_vertical_guides_", prev_vguides);
- undo_redo->add_undo_method(viewport, "queue_redraw");
- undo_redo->commit_action();
- }
- }
- } else if (drag_type == DRAG_H_GUIDE) {
- Array prev_hguides = hguides.duplicate();
- if (b->get_position().y > ruler_width_scaled) {
- // Adds a new horizontal guide
- if (dragged_guide_index >= 0) {
- hguides[dragged_guide_index] = edited.y;
- undo_redo->create_action(TTR("Move Horizontal Guide"));
- undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_horizontal_guides_", hguides);
- undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_horizontal_guides_", prev_hguides);
- undo_redo->add_undo_method(viewport, "queue_redraw");
- undo_redo->commit_action();
- } else {
- hguides.push_back(edited.y);
- undo_redo->create_action(TTR("Create Horizontal Guide"));
- undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_horizontal_guides_", hguides);
- if (prev_hguides.is_empty()) {
- undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "remove_meta", "_edit_horizontal_guides_");
- } else {
- undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_horizontal_guides_", prev_hguides);
- }
- undo_redo->add_undo_method(viewport, "queue_redraw");
- undo_redo->commit_action();
- }
- } else {
- if (dragged_guide_index >= 0) {
- hguides.remove_at(dragged_guide_index);
- undo_redo->create_action(TTR("Remove Horizontal Guide"));
- if (hguides.is_empty()) {
- undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "remove_meta", "_edit_horizontal_guides_");
- } else {
- undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_horizontal_guides_", hguides);
- }
- undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_horizontal_guides_", prev_hguides);
- undo_redo->add_undo_method(viewport, "queue_redraw");
- undo_redo->commit_action();
- }
- }
- } else if (drag_type == DRAG_DOUBLE_GUIDE) {
- Array prev_hguides = hguides.duplicate();
- Array prev_vguides = vguides.duplicate();
- if (b->get_position().x > ruler_width_scaled && b->get_position().y > ruler_width_scaled) {
- // Adds a new horizontal guide a new vertical guide
- vguides.push_back(edited.x);
- hguides.push_back(edited.y);
- undo_redo->create_action(TTR("Create Horizontal and Vertical Guides"));
- undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_vertical_guides_", vguides);
- undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_horizontal_guides_", hguides);
- if (prev_vguides.is_empty()) {
- undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "remove_meta", "_edit_vertical_guides_");
- } else {
- undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_vertical_guides_", prev_vguides);
- }
- if (prev_hguides.is_empty()) {
- undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "remove_meta", "_edit_horizontal_guides_");
- } else {
- undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_horizontal_guides_", prev_hguides);
- }
- undo_redo->add_undo_method(viewport, "queue_redraw");
- undo_redo->commit_action();
- }
- }
- }
- snap_target[0] = SNAP_TARGET_NONE;
- snap_target[1] = SNAP_TARGET_NONE;
- _reset_drag();
- viewport->queue_redraw();
- return true;
- }
- }
- return false;
-}
-
-bool CanvasItemEditor::_gui_input_zoom_or_pan(const Ref<InputEvent> &p_event, bool p_already_accepted) {
- panner->set_force_drag(tool == TOOL_PAN);
- bool panner_active = panner->gui_input(p_event, viewport->get_global_rect());
- if (panner->is_panning() != pan_pressed) {
- pan_pressed = panner->is_panning();
- _update_cursor();
- }
-
- if (panner_active) {
- return true;
- }
-
- Ref<InputEventKey> k = p_event;
- if (k.is_valid()) {
- if (k->is_pressed()) {
- if (ED_IS_SHORTCUT("canvas_item_editor/zoom_3.125_percent", p_event)) {
- _shortcut_zoom_set(1.0 / 32.0);
- } else if (ED_IS_SHORTCUT("canvas_item_editor/zoom_6.25_percent", p_event)) {
- _shortcut_zoom_set(1.0 / 16.0);
- } else if (ED_IS_SHORTCUT("canvas_item_editor/zoom_12.5_percent", p_event)) {
- _shortcut_zoom_set(1.0 / 8.0);
- } else if (ED_IS_SHORTCUT("canvas_item_editor/zoom_25_percent", p_event)) {
- _shortcut_zoom_set(1.0 / 4.0);
- } else if (ED_IS_SHORTCUT("canvas_item_editor/zoom_50_percent", p_event)) {
- _shortcut_zoom_set(1.0 / 2.0);
- } else if (ED_IS_SHORTCUT("canvas_item_editor/zoom_100_percent", p_event)) {
- _shortcut_zoom_set(1.0);
- } else if (ED_IS_SHORTCUT("canvas_item_editor/zoom_200_percent", p_event)) {
- _shortcut_zoom_set(2.0);
- } else if (ED_IS_SHORTCUT("canvas_item_editor/zoom_400_percent", p_event)) {
- _shortcut_zoom_set(4.0);
- } else if (ED_IS_SHORTCUT("canvas_item_editor/zoom_800_percent", p_event)) {
- _shortcut_zoom_set(8.0);
- } else if (ED_IS_SHORTCUT("canvas_item_editor/zoom_1600_percent", p_event)) {
- _shortcut_zoom_set(16.0);
- }
- }
- }
-
- return false;
-}
-
-void CanvasItemEditor::_pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event) {
- view_offset.x -= p_scroll_vec.x / zoom;
- view_offset.y -= p_scroll_vec.y / zoom;
- update_viewport();
-}
-
-void CanvasItemEditor::_zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event) {
- Ref<InputEventMouseButton> mb = p_event;
- if (mb.is_valid()) {
- // Special behavior for scroll events, as the zoom_by_increment method can smartly end up on powers of two.
- int increment = p_zoom_factor > 1.0 ? 1 : -1;
- bool by_integer = mb->is_alt_pressed();
-
- if (EDITOR_GET("editors/2d/use_integer_zoom_by_default")) {
- by_integer = !by_integer;
- }
-
- zoom_widget->set_zoom_by_increments(increment, by_integer);
- } else {
- zoom_widget->set_zoom(zoom_widget->get_zoom() * p_zoom_factor);
- }
-
- _zoom_on_position(zoom_widget->get_zoom(), p_origin);
-}
-
-bool CanvasItemEditor::_gui_input_pivot(const Ref<InputEvent> &p_event) {
- Ref<InputEventMouseMotion> m = p_event;
- Ref<InputEventMouseButton> b = p_event;
- Ref<InputEventKey> k = p_event;
-
- // Drag the pivot (in pivot mode / with V key)
- if (drag_type == DRAG_NONE) {
- bool move_temp_pivot = ((b.is_valid() && b->is_shift_pressed()) || (k.is_valid() && k->is_shift_pressed()));
-
- if ((b.is_valid() && b->is_pressed() && b->get_button_index() == MouseButton::LEFT && tool == TOOL_EDIT_PIVOT) ||
- (k.is_valid() && k->is_pressed() && !k->is_echo() && k->get_keycode() == Key::V && tool == TOOL_SELECT && (k->get_modifiers_mask().is_empty() || move_temp_pivot))) {
- List<CanvasItem *> selection = _get_edited_canvas_items();
-
- // Filters the selection with nodes that allow setting the pivot
- drag_selection = List<CanvasItem *>();
- for (CanvasItem *ci : selection) {
- if (ci->_edit_use_pivot() || move_temp_pivot) {
- drag_selection.push_back(ci);
- }
- }
-
- // Start dragging if we still have nodes
- if (drag_selection.size() > 0) {
- Vector2 event_pos = (b.is_valid()) ? b->get_position() : viewport->get_local_mouse_position();
-
- if (move_temp_pivot) {
- drag_type = DRAG_TEMP_PIVOT;
- temp_pivot = transform.affine_inverse().xform(event_pos);
- viewport->queue_redraw();
- return true;
- }
-
- _save_canvas_item_state(drag_selection);
- drag_from = transform.affine_inverse().xform(event_pos);
- Vector2 new_pos;
- if (drag_selection.size() == 1) {
- new_pos = snap_point(drag_from, SNAP_NODE_SIDES | SNAP_NODE_CENTER | SNAP_NODE_ANCHORS | SNAP_OTHER_NODES | SNAP_GRID | SNAP_PIXEL, 0, drag_selection.front()->get());
- } else {
- new_pos = snap_point(drag_from, SNAP_OTHER_NODES | SNAP_GRID | SNAP_PIXEL, 0, nullptr, drag_selection);
- }
- for (CanvasItem *ci : drag_selection) {
- ci->_edit_set_pivot(ci->get_screen_transform().affine_inverse().xform(new_pos));
- }
-
- drag_type = DRAG_PIVOT;
- }
- return true;
- }
- }
-
- if (drag_type == DRAG_PIVOT) {
- // Move the pivot
- if (m.is_valid()) {
- drag_to = transform.affine_inverse().xform(m->get_position());
- _restore_canvas_item_state(drag_selection);
- Vector2 new_pos;
- if (drag_selection.size() == 1) {
- new_pos = snap_point(drag_to, SNAP_NODE_SIDES | SNAP_NODE_CENTER | SNAP_NODE_ANCHORS | SNAP_OTHER_NODES | SNAP_GRID | SNAP_PIXEL, 0, drag_selection.front()->get());
- } else {
- new_pos = snap_point(drag_to, SNAP_OTHER_NODES | SNAP_GRID | SNAP_PIXEL);
- }
- for (CanvasItem *ci : drag_selection) {
- ci->_edit_set_pivot(ci->get_screen_transform().affine_inverse().xform(new_pos));
- }
- return true;
- }
-
- // Confirm the pivot move
- if (drag_selection.size() >= 1 &&
- ((b.is_valid() && !b->is_pressed() && b->get_button_index() == MouseButton::LEFT && tool == TOOL_EDIT_PIVOT) ||
- (k.is_valid() && !k->is_pressed() && k->get_keycode() == Key::V))) {
- _commit_drag();
- snap_target[0] = SNAP_TARGET_NONE;
- snap_target[1] = SNAP_TARGET_NONE;
- return true;
- }
-
- // Cancel a drag
- 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;
- _reset_drag();
- viewport->queue_redraw();
- return true;
- }
- }
-
- if (drag_type == DRAG_TEMP_PIVOT) {
- if (m.is_valid()) {
- temp_pivot = transform.affine_inverse().xform(m->get_position());
- viewport->queue_redraw();
- return true;
- }
-
- if ((b.is_valid() && !b->is_pressed() && b->get_button_index() == MouseButton::LEFT && tool == TOOL_EDIT_PIVOT) ||
- (k.is_valid() && !k->is_pressed() && k->get_keycode() == Key::V)) {
- drag_type = DRAG_NONE;
- return true;
- }
- }
- return false;
-}
-
-bool CanvasItemEditor::_gui_input_rotate(const Ref<InputEvent> &p_event) {
- Ref<InputEventMouseButton> b = p_event;
- Ref<InputEventMouseMotion> m = p_event;
-
- // Start rotation
- if (drag_type == DRAG_NONE) {
- if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && b->is_pressed()) {
- if ((b->is_command_or_control_pressed() && !b->is_alt_pressed() && tool == TOOL_SELECT) || tool == TOOL_ROTATE) {
- bool has_locked_items = false;
- List<CanvasItem *> selection = _get_edited_canvas_items(false, true, &has_locked_items);
-
- // Remove not movable nodes
- for (List<CanvasItem *>::Element *E = selection.front(); E;) {
- List<CanvasItem *>::Element *N = E->next();
- if (!_is_node_movable(E->get(), true)) {
- selection.erase(E);
- }
- E = N;
- }
-
- drag_selection = selection;
- if (drag_selection.size() > 0) {
- drag_type = DRAG_ROTATE;
- drag_from = transform.affine_inverse().xform(b->get_position());
- drag_to = drag_from;
- CanvasItem *ci = drag_selection.front()->get();
- if (!Math::is_inf(temp_pivot.x) || !Math::is_inf(temp_pivot.y)) {
- drag_rotation_center = temp_pivot;
- } else if (ci->_edit_use_pivot()) {
- drag_rotation_center = ci->get_screen_transform().xform(ci->_edit_get_pivot());
- } else {
- drag_rotation_center = ci->get_screen_transform().get_origin();
- }
- _save_canvas_item_state(drag_selection);
- viewport->queue_redraw();
- return true;
- } else {
- if (has_locked_items) {
- EditorToaster::get_singleton()->popup_str(TTR(locked_transform_warning), EditorToaster::SEVERITY_WARNING);
- }
- return has_locked_items;
- }
- }
- }
- }
-
- if (drag_type == DRAG_ROTATE) {
- // Rotate the node
- if (m.is_valid()) {
- _restore_canvas_item_state(drag_selection);
- for (CanvasItem *ci : drag_selection) {
- drag_to = transform.affine_inverse().xform(m->get_position());
- //Rotate the opposite way if the canvas item's compounded scale has an uneven number of negative elements
- bool opposite = (ci->get_global_transform().get_scale().sign().dot(ci->get_transform().get_scale().sign()) == 0);
- real_t prev_rotation = ci->_edit_get_rotation();
- real_t new_rotation = snap_angle(ci->_edit_get_rotation() + (opposite ? -1 : 1) * (drag_from - drag_rotation_center).angle_to(drag_to - drag_rotation_center), prev_rotation);
-
- ci->_edit_set_rotation(new_rotation);
- if (!Math::is_inf(temp_pivot.x) || !Math::is_inf(temp_pivot.y)) {
- Transform2D xform = ci->get_screen_transform() * ci->get_transform().affine_inverse();
- Vector2 radius = xform.xform(ci->_edit_get_position()) - temp_pivot;
- radius = radius.rotated(new_rotation - prev_rotation);
- ci->_edit_set_position(xform.affine_inverse().xform(temp_pivot + radius));
- }
- viewport->queue_redraw();
- }
- return true;
- }
-
- // Confirms the node rotation
- if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && !b->is_pressed()) {
- _commit_drag();
- return true;
- }
-
- // Cancel a drag
- 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();
- return true;
- }
- }
- return false;
-}
-
-bool CanvasItemEditor::_gui_input_open_scene_on_double_click(const Ref<InputEvent> &p_event) {
- Ref<InputEventMouseButton> b = p_event;
-
- // Open a sub-scene on double-click
- if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && b->is_pressed() && b->is_double_click() && tool == TOOL_SELECT) {
- List<CanvasItem *> selection = _get_edited_canvas_items();
- if (selection.size() == 1) {
- CanvasItem *ci = selection.front()->get();
- if (ci->is_instance() && ci != EditorNode::get_singleton()->get_edited_scene()) {
- EditorNode::get_singleton()->open_scene(ci->get_scene_file_path());
- return true;
- }
- }
- }
- return false;
-}
-
-bool CanvasItemEditor::_gui_input_anchors(const Ref<InputEvent> &p_event) {
- Ref<InputEventMouseButton> b = p_event;
- Ref<InputEventMouseMotion> m = p_event;
-
- // Starts anchor dragging if needed
- if (drag_type == DRAG_NONE) {
- if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && b->is_pressed() && tool == TOOL_SELECT) {
- List<CanvasItem *> selection = _get_edited_canvas_items();
- if (selection.size() == 1) {
- Control *control = Object::cast_to<Control>(selection.front()->get());
- if (control && _is_node_movable(control)) {
- Vector2 anchor_pos[4];
- anchor_pos[0] = Vector2(control->get_anchor(SIDE_LEFT), control->get_anchor(SIDE_TOP));
- anchor_pos[1] = Vector2(control->get_anchor(SIDE_RIGHT), control->get_anchor(SIDE_TOP));
- anchor_pos[2] = Vector2(control->get_anchor(SIDE_RIGHT), control->get_anchor(SIDE_BOTTOM));
- anchor_pos[3] = Vector2(control->get_anchor(SIDE_LEFT), control->get_anchor(SIDE_BOTTOM));
-
- Rect2 anchor_rects[4];
- for (int i = 0; i < 4; i++) {
- anchor_pos[i] = (transform * control->get_screen_transform()).xform(_anchor_to_position(control, anchor_pos[i]));
- anchor_rects[i] = Rect2(anchor_pos[i], anchor_handle->get_size());
- if (control->is_layout_rtl()) {
- anchor_rects[i].position -= anchor_handle->get_size() * Vector2(real_t(i == 1 || i == 2), real_t(i <= 1));
- } else {
- anchor_rects[i].position -= anchor_handle->get_size() * Vector2(real_t(i == 0 || i == 3), real_t(i <= 1));
- }
- }
-
- const DragType dragger[] = {
- DRAG_ANCHOR_TOP_LEFT,
- DRAG_ANCHOR_TOP_RIGHT,
- DRAG_ANCHOR_BOTTOM_RIGHT,
- DRAG_ANCHOR_BOTTOM_LEFT,
- };
-
- for (int i = 0; i < 4; i++) {
- if (anchor_rects[i].has_point(b->get_position())) {
- if ((anchor_pos[0] == anchor_pos[2]) && (anchor_pos[0].distance_to(b->get_position()) < anchor_handle->get_size().length() / 3.0)) {
- drag_type = DRAG_ANCHOR_ALL;
- } else {
- drag_type = dragger[i];
- }
- drag_from = transform.affine_inverse().xform(b->get_position());
- drag_selection = List<CanvasItem *>();
- drag_selection.push_back(control);
- _save_canvas_item_state(drag_selection);
- return true;
- }
- }
- }
- }
- }
- }
-
- if (drag_type == DRAG_ANCHOR_TOP_LEFT || drag_type == DRAG_ANCHOR_TOP_RIGHT || drag_type == DRAG_ANCHOR_BOTTOM_RIGHT || drag_type == DRAG_ANCHOR_BOTTOM_LEFT || drag_type == DRAG_ANCHOR_ALL) {
- // Drag the anchor
- if (m.is_valid()) {
- _restore_canvas_item_state(drag_selection);
- Control *control = Object::cast_to<Control>(drag_selection.front()->get());
-
- drag_to = transform.affine_inverse().xform(m->get_position());
-
- Transform2D xform = control->get_screen_transform().affine_inverse();
-
- Point2 previous_anchor;
- previous_anchor.x = (drag_type == DRAG_ANCHOR_TOP_LEFT || drag_type == DRAG_ANCHOR_BOTTOM_LEFT) ? control->get_anchor(SIDE_LEFT) : control->get_anchor(SIDE_RIGHT);
- previous_anchor.y = (drag_type == DRAG_ANCHOR_TOP_LEFT || drag_type == DRAG_ANCHOR_TOP_RIGHT) ? control->get_anchor(SIDE_TOP) : control->get_anchor(SIDE_BOTTOM);
- previous_anchor = xform.affine_inverse().xform(_anchor_to_position(control, previous_anchor));
-
- Vector2 new_anchor = xform.xform(snap_point(previous_anchor + (drag_to - drag_from), SNAP_GRID | SNAP_OTHER_NODES, SNAP_NODE_PARENT | SNAP_NODE_SIDES | SNAP_NODE_CENTER, control));
- new_anchor = _position_to_anchor(control, new_anchor).snappedf(0.001);
-
- bool use_single_axis = m->is_shift_pressed();
- Vector2 drag_vector = xform.xform(drag_to) - xform.xform(drag_from);
- bool use_y = Math::abs(drag_vector.y) > Math::abs(drag_vector.x);
-
- switch (drag_type) {
- case DRAG_ANCHOR_TOP_LEFT:
- if (!use_single_axis || !use_y) {
- control->set_anchor(SIDE_LEFT, new_anchor.x, false, false);
- }
- if (!use_single_axis || use_y) {
- control->set_anchor(SIDE_TOP, new_anchor.y, false, false);
- }
- break;
- case DRAG_ANCHOR_TOP_RIGHT:
- if (!use_single_axis || !use_y) {
- control->set_anchor(SIDE_RIGHT, new_anchor.x, false, false);
- }
- if (!use_single_axis || use_y) {
- control->set_anchor(SIDE_TOP, new_anchor.y, false, false);
- }
- break;
- case DRAG_ANCHOR_BOTTOM_RIGHT:
- if (!use_single_axis || !use_y) {
- control->set_anchor(SIDE_RIGHT, new_anchor.x, false, false);
- }
- if (!use_single_axis || use_y) {
- control->set_anchor(SIDE_BOTTOM, new_anchor.y, false, false);
- }
- break;
- case DRAG_ANCHOR_BOTTOM_LEFT:
- if (!use_single_axis || !use_y) {
- control->set_anchor(SIDE_LEFT, new_anchor.x, false, false);
- }
- if (!use_single_axis || use_y) {
- control->set_anchor(SIDE_BOTTOM, new_anchor.y, false, false);
- }
- break;
- case DRAG_ANCHOR_ALL:
- if (!use_single_axis || !use_y) {
- control->set_anchor(SIDE_LEFT, new_anchor.x, false, true);
- control->set_anchor(SIDE_RIGHT, new_anchor.x, false, true);
- }
- if (!use_single_axis || use_y) {
- control->set_anchor(SIDE_TOP, new_anchor.y, false, true);
- control->set_anchor(SIDE_BOTTOM, new_anchor.y, false, true);
- }
- break;
- default:
- break;
- }
- return true;
- }
-
- // Confirms new anchor position
- if (drag_selection.size() >= 1 && b.is_valid() && b->get_button_index() == MouseButton::LEFT && !b->is_pressed()) {
- _commit_drag();
- return true;
- }
-
- // Cancel a drag
- 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;
- _reset_drag();
- viewport->queue_redraw();
- return true;
- }
- }
- return false;
-}
-
-bool CanvasItemEditor::_gui_input_resize(const Ref<InputEvent> &p_event) {
- Ref<InputEventMouseButton> b = p_event;
- Ref<InputEventMouseMotion> m = p_event;
-
- // Drag resize handles
- if (drag_type == DRAG_NONE) {
- if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && b->is_pressed() && tool == TOOL_SELECT) {
- List<CanvasItem *> selection = _get_edited_canvas_items();
- if (selection.size() == 1) {
- CanvasItem *ci = selection.front()->get();
- if (ci->_edit_use_rect() && _is_node_movable(ci)) {
- Rect2 rect = ci->_edit_get_rect();
- Transform2D xform = transform * ci->get_screen_transform();
-
- const Vector2 endpoints[4] = {
- xform.xform(rect.position),
- xform.xform(rect.position + Vector2(rect.size.x, 0)),
- xform.xform(rect.position + rect.size),
- xform.xform(rect.position + Vector2(0, rect.size.y))
- };
-
- const DragType dragger[] = {
- DRAG_TOP_LEFT,
- DRAG_TOP,
- DRAG_TOP_RIGHT,
- DRAG_RIGHT,
- DRAG_BOTTOM_RIGHT,
- DRAG_BOTTOM,
- DRAG_BOTTOM_LEFT,
- DRAG_LEFT
- };
-
- DragType resize_drag = DRAG_NONE;
- real_t radius = select_handle->get_size().width * (1.5f / 2.0f);
-
- for (int i = 0; i < 4; i++) {
- int prev = (i + 3) % 4;
- int next = (i + 1) % 4;
-
- Vector2 ofs = ((endpoints[i] - endpoints[prev]).normalized() + ((endpoints[i] - endpoints[next]).normalized())).normalized();
- ofs *= (select_handle->get_size().width / 2);
- ofs += endpoints[i];
- if (ofs.distance_to(b->get_position()) < radius) {
- resize_drag = dragger[i * 2];
- }
-
- ofs = (endpoints[i] + endpoints[next]) / 2;
- ofs += (endpoints[next] - endpoints[i]).orthogonal().normalized() * (select_handle->get_size().width / 2);
- if (ofs.distance_to(b->get_position()) < radius) {
- resize_drag = dragger[i * 2 + 1];
- }
- }
-
- if (resize_drag != DRAG_NONE) {
- drag_type = resize_drag;
- drag_from = transform.affine_inverse().xform(b->get_position());
- drag_selection = List<CanvasItem *>();
- drag_selection.push_back(ci);
- _save_canvas_item_state(drag_selection);
- return true;
- }
- }
- }
- }
- }
-
- if (drag_type == DRAG_LEFT || drag_type == DRAG_RIGHT || drag_type == DRAG_TOP || drag_type == DRAG_BOTTOM ||
- drag_type == DRAG_TOP_LEFT || drag_type == DRAG_TOP_RIGHT || drag_type == DRAG_BOTTOM_LEFT || drag_type == DRAG_BOTTOM_RIGHT) {
- // Resize the node
- if (m.is_valid()) {
- CanvasItem *ci = drag_selection.front()->get();
- CanvasItemEditorSelectedItem *se = editor_selection->get_node_editor_data<CanvasItemEditorSelectedItem>(ci);
- //Reset state
- ci->_edit_set_state(se->undo_state);
-
- bool uniform = m->is_shift_pressed();
- bool symmetric = m->is_alt_pressed();
-
- Rect2 local_rect = ci->_edit_get_rect();
- real_t aspect = local_rect.has_area() ? (local_rect.get_size().y / local_rect.get_size().x) : (local_rect.get_size().y + 1.0) / (local_rect.get_size().x + 1.0);
- Point2 current_begin = local_rect.get_position();
- Point2 current_end = local_rect.get_position() + local_rect.get_size();
- Point2 max_begin = (symmetric) ? (current_begin + current_end - ci->_edit_get_minimum_size()) / 2.0 : current_end - ci->_edit_get_minimum_size();
- Point2 min_end = (symmetric) ? (current_begin + current_end + ci->_edit_get_minimum_size()) / 2.0 : current_begin + ci->_edit_get_minimum_size();
- Point2 center = (current_begin + current_end) / 2.0;
-
- drag_to = transform.affine_inverse().xform(m->get_position());
-
- Transform2D xform = ci->get_screen_transform();
-
- Point2 drag_to_snapped_begin;
- Point2 drag_to_snapped_end;
-
- drag_to_snapped_end = snap_point(xform.xform(current_end) + (drag_to - drag_from), SNAP_NODE_ANCHORS | SNAP_NODE_PARENT | SNAP_OTHER_NODES | SNAP_GRID | SNAP_PIXEL, 0, ci);
- drag_to_snapped_begin = snap_point(xform.xform(current_begin) + (drag_to - drag_from), SNAP_NODE_ANCHORS | SNAP_NODE_PARENT | SNAP_OTHER_NODES | SNAP_GRID | SNAP_PIXEL, 0, ci);
-
- Point2 drag_begin = xform.affine_inverse().xform(drag_to_snapped_begin);
- Point2 drag_end = xform.affine_inverse().xform(drag_to_snapped_end);
-
- // Horizontal resize
- if (drag_type == DRAG_LEFT || drag_type == DRAG_TOP_LEFT || drag_type == DRAG_BOTTOM_LEFT) {
- current_begin.x = MIN(drag_begin.x, max_begin.x);
- } else if (drag_type == DRAG_RIGHT || drag_type == DRAG_TOP_RIGHT || drag_type == DRAG_BOTTOM_RIGHT) {
- current_end.x = MAX(drag_end.x, min_end.x);
- }
-
- // Vertical resize
- if (drag_type == DRAG_TOP || drag_type == DRAG_TOP_LEFT || drag_type == DRAG_TOP_RIGHT) {
- current_begin.y = MIN(drag_begin.y, max_begin.y);
- } else if (drag_type == DRAG_BOTTOM || drag_type == DRAG_BOTTOM_LEFT || drag_type == DRAG_BOTTOM_RIGHT) {
- current_end.y = MAX(drag_end.y, min_end.y);
- }
-
- // Uniform resize
- if (uniform) {
- if (drag_type == DRAG_LEFT || drag_type == DRAG_RIGHT) {
- current_end.y = current_begin.y + aspect * (current_end.x - current_begin.x);
- } else if (drag_type == DRAG_TOP || drag_type == DRAG_BOTTOM) {
- current_end.x = current_begin.x + (current_end.y - current_begin.y) / aspect;
- } else {
- if (aspect >= 1.0) {
- if (drag_type == DRAG_TOP_LEFT || drag_type == DRAG_TOP_RIGHT) {
- current_begin.y = current_end.y - aspect * (current_end.x - current_begin.x);
- } else {
- current_end.y = current_begin.y + aspect * (current_end.x - current_begin.x);
- }
- } else {
- if (drag_type == DRAG_TOP_LEFT || drag_type == DRAG_BOTTOM_LEFT) {
- current_begin.x = current_end.x - (current_end.y - current_begin.y) / aspect;
- } else {
- current_end.x = current_begin.x + (current_end.y - current_begin.y) / aspect;
- }
- }
- }
- }
-
- // Symmetric resize
- if (symmetric) {
- if (drag_type == DRAG_LEFT || drag_type == DRAG_TOP_LEFT || drag_type == DRAG_BOTTOM_LEFT) {
- current_end.x = 2.0 * center.x - current_begin.x;
- } else if (drag_type == DRAG_RIGHT || drag_type == DRAG_TOP_RIGHT || drag_type == DRAG_BOTTOM_RIGHT) {
- current_begin.x = 2.0 * center.x - current_end.x;
- }
- if (drag_type == DRAG_TOP || drag_type == DRAG_TOP_LEFT || drag_type == DRAG_TOP_RIGHT) {
- current_end.y = 2.0 * center.y - current_begin.y;
- } else if (drag_type == DRAG_BOTTOM || drag_type == DRAG_BOTTOM_LEFT || drag_type == DRAG_BOTTOM_RIGHT) {
- current_begin.y = 2.0 * center.y - current_end.y;
- }
- }
-
- bool anchors_mode = ControlEditorToolbar::get_singleton()->is_anchors_mode_enabled();
- if (anchors_mode) {
- Control *control = Object::cast_to<Control>(ci);
- if (control) {
- Size2 parent_rect_size = control->get_parent_anchorable_rect().size;
- if (parent_rect_size.x == 0.0 || parent_rect_size.y == 0.0) {
- EditorToaster::get_singleton()->popup_str(TTR("Can't modify anchor offsets when the parent has a size of 0. Disable the anchors mode in the toolbar."), EditorToaster::SEVERITY_WARNING);
- return true;
- }
- }
- }
-
- ci->_edit_set_rect(Rect2(current_begin, current_end - current_begin));
- return true;
- }
-
- // Confirm resize
- if (drag_selection.size() >= 1 && b.is_valid() && b->get_button_index() == MouseButton::LEFT && !b->is_pressed()) {
- _commit_drag();
- return true;
- }
-
- // Cancel a drag
- 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;
- _reset_drag();
- viewport->queue_redraw();
- return true;
- }
- }
- return false;
-}
-
-bool CanvasItemEditor::_gui_input_scale(const Ref<InputEvent> &p_event) {
- Ref<InputEventMouseButton> b = p_event;
- Ref<InputEventMouseMotion> m = p_event;
-
- // Drag resize handles
- if (drag_type == DRAG_NONE) {
- if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && b->is_pressed() &&
- ((tool == TOOL_SELECT && b->is_alt_pressed() && b->is_command_or_control_pressed()) || tool == TOOL_SCALE)) {
- bool has_locked_items = false;
- List<CanvasItem *> selection = _get_edited_canvas_items(false, true, &has_locked_items);
-
- // Remove non-movable nodes.
- for (CanvasItem *ci : selection) {
- if (!_is_node_movable(ci, true)) {
- selection.erase(ci);
- }
- }
-
- if (!selection.is_empty()) {
- CanvasItem *ci = selection.front()->get();
-
- Transform2D edit_transform;
- if (!Math::is_inf(temp_pivot.x) || !Math::is_inf(temp_pivot.y)) {
- edit_transform = Transform2D(ci->_edit_get_rotation(), temp_pivot);
- } else {
- edit_transform = ci->_edit_get_transform();
- }
-
- Transform2D xform = transform * ci->get_screen_transform();
- Transform2D unscaled_transform = (xform * ci->get_transform().affine_inverse() * edit_transform).orthonormalized();
- Transform2D simple_xform;
- if (use_local_space) {
- simple_xform = viewport->get_transform() * unscaled_transform;
- } else {
- Transform2D translation = Transform2D(0.0f, unscaled_transform.get_origin());
- simple_xform = viewport->get_transform() * translation;
- }
-
- drag_type = DRAG_SCALE_BOTH;
-
- if (show_transformation_gizmos) {
- Size2 scale_factor = Size2(SCALE_HANDLE_DISTANCE, SCALE_HANDLE_DISTANCE);
- Rect2 x_handle_rect = Rect2(scale_factor.x * EDSCALE, -5 * EDSCALE, 10 * EDSCALE, 10 * EDSCALE);
- if (x_handle_rect.has_point(simple_xform.affine_inverse().xform(b->get_position()))) {
- drag_type = DRAG_SCALE_X;
- }
- Rect2 y_handle_rect = Rect2(-5 * EDSCALE, scale_factor.y * EDSCALE, 10 * EDSCALE, 10 * EDSCALE);
- if (y_handle_rect.has_point(simple_xform.affine_inverse().xform(b->get_position()))) {
- drag_type = DRAG_SCALE_Y;
- }
- }
-
- drag_from = transform.affine_inverse().xform(b->get_position());
- drag_to = drag_from;
- drag_selection = selection;
- _save_canvas_item_state(drag_selection);
- return true;
- } else {
- if (has_locked_items) {
- EditorToaster::get_singleton()->popup_str(TTR(locked_transform_warning), EditorToaster::SEVERITY_WARNING);
- }
- return has_locked_items;
- }
- }
- } else if (drag_type == DRAG_SCALE_BOTH || drag_type == DRAG_SCALE_X || drag_type == DRAG_SCALE_Y) {
- // Resize the node
- if (m.is_valid()) {
- _restore_canvas_item_state(drag_selection);
-
- drag_to = transform.affine_inverse().xform(m->get_position());
-
- Size2 scale_max;
- if (drag_type != DRAG_SCALE_BOTH) {
- for (CanvasItem *ci : drag_selection) {
- Size2 scale = ci->_edit_get_scale();
-
- if (Math::abs(scale.x) > Math::abs(scale_max.x)) {
- scale_max.x = scale.x;
- }
- if (Math::abs(scale.y) > Math::abs(scale_max.y)) {
- scale_max.y = scale.y;
- }
- }
- }
-
- Transform2D edit_transform;
- bool using_temp_pivot = !Math::is_inf(temp_pivot.x) || !Math::is_inf(temp_pivot.y);
- if (using_temp_pivot) {
- edit_transform = Transform2D(drag_selection.front()->get()->_edit_get_rotation(), temp_pivot);
- } else {
- edit_transform = drag_selection.front()->get()->_edit_get_transform();
- }
- for (CanvasItem *ci : drag_selection) {
- Transform2D parent_xform = ci->get_screen_transform() * ci->get_transform().affine_inverse();
- Transform2D unscaled_transform = (transform * parent_xform * edit_transform).orthonormalized();
- Transform2D simple_xform;
-
- if (use_local_space || drag_type == DRAG_SCALE_BOTH) {
- simple_xform = (viewport->get_transform() * unscaled_transform).affine_inverse() * transform;
- } else {
- Transform2D translation = Transform2D(0.0f, unscaled_transform.get_origin());
- simple_xform = (viewport->get_transform() * translation).affine_inverse() * transform;
- }
-
- bool uniform = m->is_shift_pressed();
- bool is_ctrl = m->is_command_or_control_pressed();
-
- Point2 drag_from_local = simple_xform.xform(drag_from);
- Point2 drag_to_local = simple_xform.xform(drag_to);
- Point2 offset = drag_to_local - drag_from_local;
-
- Transform2D object_transform = ci->_edit_get_transform();
- if (ci->is_class("Node2D")) {
- object_transform.set_skew(ci->get("skew"));
- }
-
- Size2 scale = ci->_edit_get_scale();
- Size2 original_scale = scale;
- real_t ratio = scale.y / scale.x;
- if (drag_type == DRAG_SCALE_BOTH) {
- Size2 scale_factor = drag_to_local / drag_from_local;
- if (uniform) {
- scale *= (scale_factor.x + scale_factor.y) / 2.0;
- } else {
- scale *= scale_factor;
- }
- } else {
- Size2 scale_factor = Vector2(offset.x, -offset.y) / SCALE_HANDLE_DISTANCE;
- Size2 parent_scale = parent_xform.get_scale();
- // Take into account the biggest scale, so all nodes are scaled uniformly.
- scale_factor *= Vector2(1.0 / parent_scale.x, 1.0 / parent_scale.y) / (scale_max / original_scale);
-
- if (drag_type == DRAG_SCALE_X) {
- if (!use_local_space && !uniform) {
- object_transform.set_origin(Vector2(0.0, 0.0));
- object_transform.scale(Size2(scale_factor.x + 1.0, 1.0));
- scale *= object_transform.get_scale();
- } else {
- scale.x += scale_factor.x;
- }
- if (uniform) {
- scale.y = scale.x * ratio;
- }
- } else if (drag_type == DRAG_SCALE_Y) {
- if (!use_local_space && !uniform) {
- object_transform.set_origin(Vector2(0.0, 0.0));
- object_transform.scale(Size2(1.0, -scale_factor.y + 1.0));
- scale *= object_transform.get_scale();
- } else {
- scale.y -= scale_factor.y;
- }
- if (uniform) {
- scale.x = scale.y / ratio;
- }
- }
- }
-
- if (snap_scale && !is_ctrl) {
- if (snap_relative) {
- scale.x = original_scale.x * (Math::round((scale.x / original_scale.x) / snap_scale_step) * snap_scale_step);
- scale.y = original_scale.y * (Math::round((scale.y / original_scale.y) / snap_scale_step) * snap_scale_step);
- } else {
- scale.x = Math::round(scale.x / snap_scale_step) * snap_scale_step;
- scale.y = Math::round(scale.y / snap_scale_step) * snap_scale_step;
- }
- }
-
- ci->_edit_set_scale(scale);
- if (!use_local_space && !uniform) {
- Node2D *n2d = Object::cast_to<Node2D>(ci);
- if (n2d) {
- n2d->_edit_set_rotation(object_transform.get_rotation());
- n2d->set_skew(object_transform.get_skew());
- }
- }
-
- if (using_temp_pivot) {
- Point2 ci_origin = ci->_edit_get_transform().get_origin();
- ci->_edit_set_position(ci_origin + (ci_origin - temp_pivot) * ((scale - original_scale) / original_scale));
- }
- }
-
- return true;
- }
-
- // Confirm resize
- if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && !b->is_pressed()) {
- _commit_drag();
- return true;
- }
-
- // Cancel a drag
- 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();
- return true;
- }
- }
- return false;
-}
-
-bool CanvasItemEditor::_gui_input_move(const Ref<InputEvent> &p_event) {
- Ref<InputEventMouseButton> b = p_event;
- Ref<InputEventMouseMotion> m = p_event;
- Ref<InputEventKey> k = p_event;
-
- if (drag_type == DRAG_NONE) {
- //Start moving the nodes
- if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && b->is_pressed()) {
- if ((tool == TOOL_SELECT && b->is_alt_pressed() && !b->is_command_or_control_pressed()) || tool == TOOL_MOVE) {
- bool has_locked_items = false;
- List<CanvasItem *> selection = _get_edited_canvas_items(false, true, &has_locked_items);
-
- if (selection.size() > 0) {
- drag_selection.clear();
- for (CanvasItem *E : selection) {
- if (_is_node_movable(E, true)) {
- drag_selection.push_back(E);
- }
- }
-
- drag_type = DRAG_MOVE;
-
- CanvasItem *ci = selection.front()->get();
- Transform2D parent_xform = ci->get_screen_transform() * ci->get_transform().affine_inverse();
- Transform2D unscaled_transform = (transform * parent_xform * ci->_edit_get_transform()).orthonormalized();
- Transform2D simple_xform;
- if (use_local_space) {
- simple_xform = viewport->get_transform() * unscaled_transform;
- } else {
- Transform2D translation = Transform2D(0.0f, unscaled_transform.get_origin());
- simple_xform = viewport->get_transform() * translation;
- }
-
- if (show_transformation_gizmos) {
- Size2 move_factor = Size2(MOVE_HANDLE_DISTANCE, MOVE_HANDLE_DISTANCE);
- Rect2 x_handle_rect = Rect2(move_factor.x * EDSCALE, -5 * EDSCALE, 10 * EDSCALE, 10 * EDSCALE);
- if (x_handle_rect.has_point(simple_xform.affine_inverse().xform(b->get_position()))) {
- drag_type = DRAG_MOVE_X;
- }
- Rect2 y_handle_rect = Rect2(-5 * EDSCALE, move_factor.y * EDSCALE, 10 * EDSCALE, 10 * EDSCALE);
- if (y_handle_rect.has_point(simple_xform.affine_inverse().xform(b->get_position()))) {
- drag_type = DRAG_MOVE_Y;
- }
- }
-
- drag_from = transform.affine_inverse().xform(b->get_position());
- _save_canvas_item_state(drag_selection);
-
- return true;
- } else {
- if (has_locked_items) {
- EditorToaster::get_singleton()->popup_str(TTR(locked_transform_warning), EditorToaster::SEVERITY_WARNING);
- }
- return has_locked_items;
- }
- }
- }
- }
-
- if (drag_type == DRAG_MOVE || drag_type == DRAG_MOVE_X || drag_type == DRAG_MOVE_Y) {
- // Move the nodes
- if (m.is_valid() && !drag_selection.is_empty()) {
- _restore_canvas_item_state(drag_selection, true);
-
- drag_to = transform.affine_inverse().xform(m->get_position());
- Point2 previous_pos;
- if (drag_selection.size() == 1) {
- Transform2D parent_xform = drag_selection.front()->get()->get_screen_transform() * drag_selection.front()->get()->get_transform().affine_inverse();
- previous_pos = parent_xform.xform(drag_selection.front()->get()->_edit_get_position());
- } else {
- previous_pos = _get_encompassing_rect_from_list(drag_selection).position;
- }
-
- Point2 drag_delta = drag_to - drag_from;
- if (drag_type == DRAG_MOVE_X || drag_type == DRAG_MOVE_Y) {
- const CanvasItem *selected = drag_selection.front()->get();
- Transform2D parent_xform = selected->get_screen_transform() * selected->get_transform().affine_inverse();
- Transform2D unscaled_transform = (transform * parent_xform * selected->_edit_get_transform()).orthonormalized();
- Transform2D simple_xform;
- if (use_local_space) {
- simple_xform = viewport->get_transform() * unscaled_transform;
- } else {
- simple_xform = viewport->get_transform();
- }
-
- drag_delta = simple_xform.affine_inverse().basis_xform(drag_delta);
- if (drag_type == DRAG_MOVE_X) {
- drag_delta.y = 0;
- } else {
- drag_delta.x = 0;
- }
- drag_delta = simple_xform.basis_xform(drag_delta);
- }
- Point2 new_pos = snap_point(previous_pos + drag_delta, SNAP_GRID | SNAP_GUIDES | SNAP_PIXEL | SNAP_NODE_PARENT | SNAP_NODE_ANCHORS | SNAP_OTHER_NODES, 0, nullptr, drag_selection);
-
- bool single_axis = m->is_shift_pressed();
- if (single_axis) {
- if (Math::abs(new_pos.x - previous_pos.x) > Math::abs(new_pos.y - previous_pos.y)) {
- new_pos.y = previous_pos.y;
- } else {
- new_pos.x = previous_pos.x;
- }
- }
-
- for (CanvasItem *ci : drag_selection) {
- Transform2D parent_xform_inv = ci->get_transform() * ci->get_screen_transform().affine_inverse();
- ci->_edit_set_position(ci->_edit_get_position() + parent_xform_inv.basis_xform(new_pos - previous_pos));
- }
- return true;
- }
-
- // Confirm the move (only if it was moved)
- if (b.is_valid() && !b->is_pressed() && b->get_button_index() == MouseButton::LEFT) {
- _commit_drag();
- return true;
- }
-
- // Cancel a drag
- 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;
- _reset_drag();
- viewport->queue_redraw();
- return true;
- }
- }
-
- // Move the canvas items with the arrow keys
- if (k.is_valid() && k->is_pressed() && (tool == TOOL_SELECT || tool == TOOL_MOVE) &&
- (k->get_keycode() == Key::UP || k->get_keycode() == Key::DOWN || k->get_keycode() == Key::LEFT || k->get_keycode() == Key::RIGHT)) {
- if (!k->is_echo()) {
- // Start moving the canvas items with the keyboard, if they are movable
- List<CanvasItem *> selection = _get_edited_canvas_items();
-
- drag_selection.clear();
- for (CanvasItem *item : selection) {
- if (_is_node_movable(item, true)) {
- drag_selection.push_back(item);
- }
- }
-
- drag_type = DRAG_KEY_MOVE;
- drag_from = Vector2();
- drag_to = Vector2();
- _save_canvas_item_state(drag_selection, true);
- }
-
- if (drag_selection.size() > 0) {
- _restore_canvas_item_state(drag_selection, true);
-
- bool move_local_base = k->is_alt_pressed();
- bool move_local_base_rotated = k->is_ctrl_pressed() || k->is_meta_pressed();
-
- Vector2 dir;
- if (k->get_keycode() == Key::UP) {
- dir += Vector2(0, -1);
- } else if (k->get_keycode() == Key::DOWN) {
- dir += Vector2(0, 1);
- } else if (k->get_keycode() == Key::LEFT) {
- dir += Vector2(-1, 0);
- } else if (k->get_keycode() == Key::RIGHT) {
- dir += Vector2(1, 0);
- }
- if (k->is_shift_pressed()) {
- dir *= grid_step * Math::pow(2.0, grid_step_multiplier);
- }
-
- drag_to += dir;
- if (k->is_shift_pressed()) {
- drag_to = drag_to.snapped(grid_step * Math::pow(2.0, grid_step_multiplier));
- }
-
- Point2 previous_pos;
- if (drag_selection.size() == 1) {
- Transform2D xform = drag_selection.front()->get()->get_global_transform_with_canvas() * drag_selection.front()->get()->get_transform().affine_inverse();
- previous_pos = xform.xform(drag_selection.front()->get()->_edit_get_position());
- } else {
- previous_pos = _get_encompassing_rect_from_list(drag_selection).position;
- }
-
- Point2 new_pos;
- if (drag_selection.size() == 1) {
- Node2D *node_2d = Object::cast_to<Node2D>(drag_selection.front()->get());
- if (node_2d && move_local_base_rotated) {
- Transform2D m2;
- m2.rotate(node_2d->get_rotation());
- new_pos += m2.xform(drag_to);
- } else if (move_local_base) {
- new_pos += drag_to;
- } else {
- new_pos = previous_pos + (drag_to - drag_from);
- }
- } else {
- new_pos = previous_pos + (drag_to - drag_from);
- }
-
- for (CanvasItem *ci : drag_selection) {
- Transform2D xform = ci->get_global_transform_with_canvas().affine_inverse() * ci->get_transform();
- ci->_edit_set_position(ci->_edit_get_position() + xform.xform(new_pos) - xform.xform(previous_pos));
- }
- }
- return true;
- }
-
- // Confirm canvas items move by arrow keys.
- if (k.is_valid() && !k->is_pressed() && drag_type == DRAG_KEY_MOVE && (tool == TOOL_SELECT || tool == TOOL_MOVE) &&
- (k->get_keycode() == Key::UP || k->get_keycode() == Key::DOWN || k->get_keycode() == Key::LEFT || k->get_keycode() == Key::RIGHT)) {
- _commit_drag();
- return true;
- }
-
- return (k.is_valid() && (k->get_keycode() == Key::UP || k->get_keycode() == Key::DOWN || k->get_keycode() == Key::LEFT || k->get_keycode() == Key::RIGHT)); // Accept the key event in any case
-}
-
-bool CanvasItemEditor::_gui_input_select(const Ref<InputEvent> &p_event) {
- Ref<InputEventMouseButton> b = p_event;
- Ref<InputEventMouseMotion> m = p_event;
- Ref<InputEventKey> k = p_event;
-
- if (drag_type == DRAG_NONE || (drag_type == DRAG_BOX_SELECTION && b.is_valid() && !b->is_pressed())) {
- if (b.is_valid() && b->is_pressed() &&
- ((b->get_button_index() == MouseButton::RIGHT && b->is_alt_pressed()) ||
- (b->get_button_index() == MouseButton::LEFT && tool == TOOL_LIST_SELECT))) {
- // Popup the selection menu list
- Point2 click = transform.affine_inverse().xform(b->get_position());
-
- _get_canvas_items_at_pos(click, selection_results, b->is_alt_pressed());
-
- if (selection_results.size() == 1) {
- CanvasItem *item = selection_results[0].item;
- selection_results.clear();
-
- _select_click_on_item(item, click, b->is_shift_pressed());
-
- return true;
- } else if (!selection_results.is_empty()) {
- // Sorts items according the their z-index
- selection_results.sort();
-
- NodePath root_path = get_tree()->get_edited_scene_root()->get_path();
- StringName root_name = root_path.get_name(root_path.get_name_count() - 1);
- int icon_max_width = EditorNode::get_singleton()->get_editor_theme()->get_constant(SNAME("class_icon_size"), EditorStringName(Editor));
-
- for (int i = 0; i < selection_results.size(); i++) {
- CanvasItem *item = selection_results[i].item;
-
- Ref<Texture2D> icon = EditorNode::get_singleton()->get_object_icon(item);
- String node_path = "/" + root_name + "/" + String(root_path.rel_path_to(item->get_path()));
-
- int locked = 0;
- if (_is_node_locked(item)) {
- locked = 1;
- } else {
- Node *scene = EditorNode::get_singleton()->get_edited_scene();
- Node *node = item;
-
- while (node && node != scene->get_parent()) {
- CanvasItem *ci_tmp = Object::cast_to<CanvasItem>(node);
- if (ci_tmp && node->has_meta("_edit_group_")) {
- locked = 2;
- }
- node = node->get_parent();
- }
- }
-
- String suffix;
- if (locked == 1) {
- suffix = " (" + TTR("Locked") + ")";
- } else if (locked == 2) {
- suffix = " (" + TTR("Grouped") + ")";
- }
- selection_menu->add_item((String)item->get_name() + suffix);
- selection_menu->set_item_icon(i, icon);
- selection_menu->set_item_icon_max_width(i, icon_max_width);
- selection_menu->set_item_metadata(i, node_path);
- selection_menu->set_item_tooltip(i, String(item->get_name()) + "\nType: " + item->get_class() + "\nPath: " + node_path);
- }
-
- selection_results_menu = selection_results;
- selection_menu_additive_selection = b->is_shift_pressed();
- selection_menu->set_position(viewport->get_screen_transform().xform(b->get_position()));
- selection_menu->reset_size();
- selection_menu->popup();
- return true;
+ undo_redo->add_do_method(res_node, "set_scale", Size2(1, 1));
+ break;
}
+ continue;
}
-
- if (b.is_valid() && b->is_pressed() && b->get_button_index() == MouseButton::RIGHT && tool != TOOL_SCENE_PAINT) {
- add_node_menu->clear();
- add_node_menu->add_icon_item(get_editor_theme_icon(SNAME("Add")), TTRC("Add 2D Node Here..."), ADD_NODE);
- add_node_menu->add_icon_item(get_editor_theme_icon(SNAME("Instance")), TTRC("Instantiate Scene Here..."), ADD_INSTANCE);
- for (Node *node : SceneTreeDock::get_singleton()->get_node_clipboard()) {
- if (Object::cast_to<CanvasItem>(node)) {
- add_node_menu->add_icon_item(get_editor_theme_icon(SNAME("ActionPaste")), TTRC("Paste Node(s) Here"), ADD_PASTE);
+ Control *res_control = Object::cast_to<Control>(node);
+ if (res_control) {
+ switch (p_type) {
+ case TransformType::POSITION:
+ undo_redo->add_undo_method(res_control, "set_position", res_control->get_position());
+ undo_redo->add_do_method(res_control, "set_position", Vector2());
break;
- }
- }
- for (Node *node : EditorNode::get_singleton()->get_editor_selection()->get_top_selected_node_list()) {
- if (Object::cast_to<CanvasItem>(node) && _is_node_movable(node, false)) {
- add_node_menu->add_icon_item(get_editor_theme_icon(SNAME("ToolMove")), TTRC("Move Node(s) Here"), ADD_MOVE);
+ case TransformType::ROTATION:
+ undo_redo->add_undo_method(res_control, "set_rotation", res_control->get_rotation());
+ undo_redo->add_do_method(res_control, "set_rotation", 0);
+ break;
+ case TransformType::SCALE:
+ undo_redo->add_undo_method(res_control, "set_scale", res_control->get_scale());
+ undo_redo->add_do_method(res_control, "set_scale", Size2(1, 1));
break;
- }
- }
-
- if (EditorContextMenuPluginManager::get_singleton()->has_plugins_for_slot(EditorContextMenuPlugin::CONTEXT_SLOT_2D_EDITOR)) {
- EditorContextMenuPlugin::OptionsData context_data = _get_context_data();
- EditorContextMenuPluginManager::get_singleton()->add_options_from_plugins(add_node_menu, EditorContextMenuPlugin::CONTEXT_SLOT_2D_EDITOR, context_data);
-
-#ifndef DISABLE_DEPRECATED
- TypedArray<CanvasItem> items = context_data["hovered_nodes"];
- PackedStringArray paths;
- paths.reserve_exact(items.size());
- for (const Variant &v : items) {
- CanvasItem *ci = Object::cast_to<CanvasItem>(v);
- paths.append((String)ci->get_path());
- }
- EditorContextMenuPluginManager::get_singleton()->add_options_from_plugins(add_node_menu, EditorContextMenuPlugin::CONTEXT_SLOT_2D_EDITOR, paths, items, 500);
-#endif
}
-
- add_node_menu->reset_size();
- add_node_menu->set_position(viewport->get_screen_transform().xform(b->get_position()));
- add_node_menu->popup();
- node_create_position = transform.affine_inverse().xform(b->get_position());
- return true;
- }
-
- Point2 click;
- bool can_select = b.is_valid() && b->get_button_index() == MouseButton::LEFT && !panner->is_panning() && (tool == TOOL_SELECT || tool == TOOL_MOVE || tool == TOOL_SCALE || tool == TOOL_ROTATE);
- if (can_select) {
- click = transform.affine_inverse().xform(b->get_position());
- // Allow selecting on release when performed very small box selection (necessary when Shift is pressed, see below).
- can_select = b->is_pressed() || (drag_type == DRAG_BOX_SELECTION && click.distance_to(drag_from) <= DRAG_THRESHOLD);
}
+ }
+ undo_redo->commit_action();
+}
- if (can_select) {
- // Single item selection.
- Node *scene = EditorNode::get_singleton()->get_edited_scene();
- if (!scene) {
- return true;
- }
-
- // Find the item to select.
- CanvasItem *ci = nullptr;
-
- Vector<SelectResult> selection = Vector<SelectResult>();
- // Retrieve the canvas items.
- _get_canvas_items_at_pos(click, selection);
- if (!selection.is_empty()) {
- ci = selection[0].item;
- }
+void CanvasItemEditor::_switch_theme_preview(int p_mode) {
+ view_menu->get_popup()->hide();
- // Shift also allows forcing box selection when item was clicked.
- if (!ci || (b->is_shift_pressed() && b->is_pressed())) {
- // Start a box selection.
- if (!b->is_shift_pressed()) {
- // Clear the selection if not additive.
- editor_selection->clear();
- viewport->queue_redraw();
- selected_from_canvas = true;
- };
+ if (theme_preview == p_mode) {
+ return;
+ }
+ theme_preview = (ThemePreviewMode)p_mode;
+ EditorSettings::get_singleton()->set_project_metadata("2d_editor", "theme_preview", theme_preview);
- if (b->is_pressed()) {
- drag_from = click;
- drag_type = DRAG_BOX_SELECTION;
- box_selecting_to = drag_from;
- return true;
- }
- } else {
- bool still_selected = _select_click_on_item(ci, click, b->is_shift_pressed());
- // Start dragging.
- if (still_selected && (tool == TOOL_SELECT || tool == TOOL_MOVE) && b->is_pressed()) {
- // Drag the node(s) if requested.
- drag_start_origin = click;
- drag_type = DRAG_QUEUED;
- } else if (!b->is_pressed()) {
- _reset_drag();
- }
- // Select the item.
- return true;
- }
- }
+ for (int i = 0; i < THEME_PREVIEW_MAX; i++) {
+ theme_menu->set_item_checked(i, i == theme_preview);
}
- if (drag_type == DRAG_QUEUED) {
- if (b.is_valid() && !b->is_pressed()) {
- _reset_drag();
- return true;
- }
- if (m.is_valid()) {
- Point2 click = transform.affine_inverse().xform(m->get_position());
- bool movement_threshold_passed = drag_start_origin.distance_to(click) > (8 * MAX(1, EDSCALE)) / zoom;
- if (m.is_valid() && movement_threshold_passed) {
- List<CanvasItem *> selection2 = _get_edited_canvas_items();
-
- drag_selection.clear();
- for (CanvasItem *E : selection2) {
- if (_is_node_movable(E, true)) {
- drag_selection.push_back(E);
- }
- }
+ EditorNode::get_singleton()->update_preview_themes(theme_preview);
+}
- if (selection2.size() > 0) {
- drag_type = DRAG_MOVE;
- drag_from = drag_start_origin;
- _save_canvas_item_state(drag_selection);
- }
- return true;
- }
- }
+bool CanvasItemEditor::_gui_input_zoom_or_pan(const Ref<InputEvent> &p_event, bool p_already_accepted) {
+ panner->set_force_drag(ci_manipulator->get_tool() == CanvasItemManipulator::TOOL_PAN);
+ bool panner_active = panner->gui_input(p_event, viewport->get_global_rect());
+ if (panner->is_panning() != pan_pressed) {
+ pan_pressed = panner->is_panning();
+ _update_cursor();
}
- if (drag_type == DRAG_BOX_SELECTION) {
- if (b.is_valid() && !b->is_pressed() && b->get_button_index() == MouseButton::LEFT) {
- // Confirms box selection.
- Node *scene = EditorNode::get_singleton()->get_edited_scene();
- if (scene) {
- List<CanvasItem *> selitems;
-
- Point2 bsfrom = drag_from;
- Point2 bsto = box_selecting_to;
- if (bsfrom.x > bsto.x) {
- SWAP(bsfrom.x, bsto.x);
- }
- if (bsfrom.y > bsto.y) {
- SWAP(bsfrom.y, bsto.y);
- }
+ if (panner_active) {
+ return true;
+ }
- _find_canvas_items_in_rect(Rect2(bsfrom, bsto - bsfrom), scene, &selitems);
- if (selitems.size() == 1 && editor_selection->get_selection().is_empty()) {
- EditorNode::get_singleton()->push_item(selitems.front()->get());
- }
- for (CanvasItem *E : selitems) {
- editor_selection->add_node(E);
- }
+ Ref<InputEventKey> k = p_event;
+ if (k.is_valid()) {
+ if (k->is_pressed()) {
+ if (ED_IS_SHORTCUT("canvas_item_editor/zoom_3.125_percent", p_event)) {
+ _shortcut_zoom_set(1.0 / 32.0);
+ } else if (ED_IS_SHORTCUT("canvas_item_editor/zoom_6.25_percent", p_event)) {
+ _shortcut_zoom_set(1.0 / 16.0);
+ } else if (ED_IS_SHORTCUT("canvas_item_editor/zoom_12.5_percent", p_event)) {
+ _shortcut_zoom_set(1.0 / 8.0);
+ } else if (ED_IS_SHORTCUT("canvas_item_editor/zoom_25_percent", p_event)) {
+ _shortcut_zoom_set(1.0 / 4.0);
+ } else if (ED_IS_SHORTCUT("canvas_item_editor/zoom_50_percent", p_event)) {
+ _shortcut_zoom_set(1.0 / 2.0);
+ } else if (ED_IS_SHORTCUT("canvas_item_editor/zoom_100_percent", p_event)) {
+ _shortcut_zoom_set(1.0);
+ } else if (ED_IS_SHORTCUT("canvas_item_editor/zoom_200_percent", p_event)) {
+ _shortcut_zoom_set(2.0);
+ } else if (ED_IS_SHORTCUT("canvas_item_editor/zoom_400_percent", p_event)) {
+ _shortcut_zoom_set(4.0);
+ } else if (ED_IS_SHORTCUT("canvas_item_editor/zoom_800_percent", p_event)) {
+ _shortcut_zoom_set(8.0);
+ } else if (ED_IS_SHORTCUT("canvas_item_editor/zoom_1600_percent", p_event)) {
+ _shortcut_zoom_set(16.0);
}
-
- _reset_drag();
- viewport->queue_redraw();
- return true;
- }
-
- if (b.is_valid() && b->is_pressed() && b->get_button_index() == MouseButton::RIGHT) {
- // Cancel box selection.
- _reset_drag();
- viewport->queue_redraw();
- return true;
- }
-
- if (m.is_valid()) {
- // Update box selection.
- box_selecting_to = transform.affine_inverse().xform(m->get_position());
- viewport->queue_redraw();
- return true;
}
}
- 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();
- }
return false;
}
-bool CanvasItemEditor::_gui_input_ruler_tool(const Ref<InputEvent> &p_event) {
- if (tool != TOOL_RULER) {
- ruler_tool_active = false;
- return false;
- }
-
- Ref<InputEventMouseButton> b = p_event;
- Ref<InputEventMouseMotion> m = p_event;
-
- Point2 previous_origin = ruler_tool_origin;
- if (!ruler_tool_active) {
- ruler_tool_origin = snap_point(viewport->get_local_mouse_position() / zoom + view_offset);
- }
+void CanvasItemEditor::_pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event) {
+ view_offset.x -= p_scroll_vec.x / zoom;
+ view_offset.y -= p_scroll_vec.y / zoom;
+ ci_manipulator->set_view_offset(view_offset);
+ update_viewport();
+}
- if (ruler_tool_active && b.is_valid() && b->get_button_index() == MouseButton::RIGHT) {
- ruler_tool_active = false;
- viewport->queue_redraw();
- return true;
- }
+void CanvasItemEditor::_zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event) {
+ Ref<InputEventMouseButton> mb = p_event;
+ if (mb.is_valid()) {
+ // Special behavior for scroll events, as the zoom_by_increment method can smartly end up on powers of two.
+ int increment = p_zoom_factor > 1.0 ? 1 : -1;
+ bool by_integer = mb->is_alt_pressed();
- if (b.is_valid() && b->get_button_index() == MouseButton::LEFT) {
- if (b->is_pressed()) {
- ruler_tool_active = true;
- } else {
- ruler_tool_active = false;
+ if (EDITOR_GET("editors/2d/use_integer_zoom_by_default")) {
+ by_integer = !by_integer;
}
- viewport->queue_redraw();
- return true;
- }
-
- if (m.is_valid() && (ruler_tool_active || (grid_snap_active && previous_origin != ruler_tool_origin))) {
- viewport->queue_redraw();
- return true;
+ zoom_widget->set_zoom_by_increments(increment, by_integer);
+ } else {
+ zoom_widget->set_zoom(zoom_widget->get_zoom() * p_zoom_factor);
}
- return false;
+ _zoom_on_position(zoom_widget->get_zoom(), p_origin);
}
bool CanvasItemEditor::_gui_input_hover(const Ref<InputEvent> &p_event) {
@@ -2788,15 +1123,15 @@ bool CanvasItemEditor::_gui_input_hover(const Ref<InputEvent> &p_event) {
if (m.is_valid()) {
Point2 click = transform.affine_inverse().xform(m->get_position());
- // Checks if the hovered items changed, redraw the viewport if so
- Vector<SelectResult> hovering_results_items;
- _get_canvas_items_at_pos(click, hovering_results_items);
+ // Checks if the hovered items changed, redraw the viewport if so.
+ Vector<DebuggerHelpers::SelectResult> hovering_results_items;
+ ci_manipulator->get_canvas_items_at_pos(click, hovering_results_items);
hovering_results_items.sort();
- // Compute the nodes names and icon position
+ // Compute the nodes names and icon position.
Vector<_HoverResult> hovering_results_tmp;
for (int i = 0; i < hovering_results_items.size(); i++) {
- CanvasItem *ci = hovering_results_items[i].item;
+ CanvasItem *ci = Object::cast_to<CanvasItem>(hovering_results_items[i].item);
if (ci->_edit_use_rect()) {
continue;
@@ -2843,33 +1178,7 @@ void CanvasItemEditor::_gui_input_viewport(const Ref<InputEvent> &p_event) {
bool release_lmb = (mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT); // Required to properly release some stuff (e.g. selection box) while panning.
if (simple_panning || !pan_pressed || release_lmb) {
- accepted = true;
- if (_gui_input_rulers_and_guides(p_event)) {
- // print_line("Rulers and guides");
- } else if (EditorNode::get_singleton()->get_editor_plugins_over()->forward_gui_input(p_event)) {
- // print_line("Plugin");
- } else if (_gui_input_open_scene_on_double_click(p_event)) {
- // print_line("Open scene on double click");
- } else if (_gui_input_scale(p_event)) {
- // print_line("Set scale");
- } else if (_gui_input_pivot(p_event)) {
- // print_line("Set pivot");
- } else if (_gui_input_resize(p_event)) {
- // print_line("Resize");
- } else if (_gui_input_rotate(p_event)) {
- // print_line("Rotate");
- } else if (_gui_input_move(p_event)) {
- // print_line("Move");
- } else if (_gui_input_anchors(p_event)) {
- // print_line("Anchors");
- } else if (_gui_input_ruler_tool(p_event)) {
- // print_line("Measure");
- } else if (_gui_input_select(p_event)) {
- // print_line("Selection");
- } else {
- // print_line("Not accepted");
- accepted = false;
- }
+ accepted = ci_manipulator->gui_input(p_event);
}
accepted = (_gui_input_zoom_or_pan(p_event, accepted) || accepted);
@@ -2892,209 +1201,12 @@ void CanvasItemEditor::_gui_input_viewport(const Ref<InputEvent> &p_event) {
}
}
-void CanvasItemEditor::_commit_drag() {
- if (!drag_selection.is_empty()) {
- switch (drag_type) {
- // Confirm the pivot move.
- case DRAG_PIVOT: {
- _commit_canvas_item_state(
- drag_selection,
- vformat(
- TTR("Set CanvasItem \"%s\" Pivot Offset to (%d, %d)"),
- drag_selection.front()->get()->get_name(),
- drag_selection.front()->get()->_edit_get_pivot().x,
- drag_selection.front()->get()->_edit_get_pivot().y));
- } break;
-
- // Confirm the node rotation.
- case DRAG_ROTATE: {
- if (drag_selection.size() != 1) {
- _commit_canvas_item_state(
- drag_selection,
- vformat(TTR("Rotate %d CanvasItems"), drag_selection.size()),
- true);
- } else {
- _commit_canvas_item_state(
- drag_selection,
- vformat(TTR("Rotate CanvasItem \"%s\" to %d degrees"),
- drag_selection.front()->get()->get_name(),
- Math::rad_to_deg(drag_selection.front()->get()->_edit_get_rotation())),
- true);
- }
-
- if (key_auto_insert_button->is_pressed()) {
- _insert_animation_keys(false, true, false, true);
- }
- } break;
-
- // Confirm new anchor position.
- case DRAG_ANCHOR_TOP_LEFT:
- case DRAG_ANCHOR_TOP_RIGHT:
- case DRAG_ANCHOR_BOTTOM_RIGHT:
- case DRAG_ANCHOR_BOTTOM_LEFT:
- case DRAG_ANCHOR_ALL: {
- _commit_canvas_item_state(
- drag_selection,
- vformat(TTR("Move CanvasItem \"%s\" Anchor"), drag_selection.front()->get()->get_name()));
- snap_target[0] = SNAP_TARGET_NONE;
- snap_target[1] = SNAP_TARGET_NONE;
- } break;
-
- // Confirm resize.
- case DRAG_LEFT:
- case DRAG_RIGHT:
- case DRAG_TOP:
- case DRAG_BOTTOM:
- case DRAG_TOP_LEFT:
- case DRAG_TOP_RIGHT:
- case DRAG_BOTTOM_LEFT:
- case DRAG_BOTTOM_RIGHT: {
- const Node2D *node2d = Object::cast_to<Node2D>(drag_selection.front()->get());
- if (node2d) {
- // Extends from Node2D.
- // Node2D doesn't have an actual stored rect size, unlike Controls.
- _commit_canvas_item_state(
- drag_selection,
- vformat(
- TTR("Scale Node2D \"%s\" to (%s, %s)"),
- drag_selection.front()->get()->get_name(),
- Math::snapped(drag_selection.front()->get()->_edit_get_scale().x, 0.01),
- Math::snapped(drag_selection.front()->get()->_edit_get_scale().y, 0.01)),
- true);
- } else {
- // Extends from Control.
- _commit_canvas_item_state(
- drag_selection,
- vformat(
- TTR("Resize Control \"%s\" to (%d, %d)"),
- drag_selection.front()->get()->get_name(),
- drag_selection.front()->get()->_edit_get_rect().size.x,
- drag_selection.front()->get()->_edit_get_rect().size.y),
- true);
- }
-
- if (key_auto_insert_button->is_pressed()) {
- _insert_animation_keys(false, false, true, true);
- }
-
- snap_target[0] = SNAP_TARGET_NONE;
- snap_target[1] = SNAP_TARGET_NONE;
- } break;
-
- // Confirm resize.
- case DRAG_SCALE_BOTH:
- case DRAG_SCALE_X:
- case DRAG_SCALE_Y: {
- if (drag_selection.size() != 1) {
- _commit_canvas_item_state(
- drag_selection,
- vformat(TTR("Scale %d CanvasItems"), drag_selection.size()),
- true);
- } else {
- _commit_canvas_item_state(
- drag_selection,
- vformat(TTR("Scale CanvasItem \"%s\" to (%s, %s)"),
- drag_selection.front()->get()->get_name(),
- Math::snapped(drag_selection.front()->get()->_edit_get_scale().x, 0.01),
- Math::snapped(drag_selection.front()->get()->_edit_get_scale().y, 0.01)),
- true);
- }
- if (key_auto_insert_button->is_pressed()) {
- _insert_animation_keys(false, false, true, true);
- }
- } break;
-
- // Confirm the canvas items move.
- case DRAG_MOVE:
- case DRAG_MOVE_X:
- case DRAG_MOVE_Y: {
- if (transform.affine_inverse().xform(get_viewport()->get_mouse_position()) != drag_from) {
- if (drag_selection.size() != 1) {
- _commit_canvas_item_state(
- drag_selection,
- vformat(TTR("Move %d CanvasItems"), drag_selection.size()),
- true);
- } else {
- _commit_canvas_item_state(
- drag_selection,
- vformat(
- TTR("Move CanvasItem \"%s\" to (%d, %d)"),
- drag_selection.front()->get()->get_name(),
- drag_selection.front()->get()->_edit_get_position().x,
- drag_selection.front()->get()->_edit_get_position().y),
- true);
- }
- }
-
- if (key_auto_insert_button->is_pressed()) {
- _insert_animation_keys(true, false, false, true);
- }
-
- // Make sure smart snapping lines disappear.
- snap_target[0] = SNAP_TARGET_NONE;
- snap_target[1] = SNAP_TARGET_NONE;
- } break;
-
- // Confirm the canvas items move by arrow keys.
- case DRAG_KEY_MOVE: {
- if (tool != TOOL_SELECT && tool != TOOL_MOVE) {
- return;
- }
-
- if (drag_selection.size() > 1) {
- _commit_canvas_item_state(
- drag_selection,
- vformat(TTR("Move %d CanvasItems"), drag_selection.size()),
- true);
- } else if (drag_selection.size() == 1) {
- _commit_canvas_item_state(
- drag_selection,
- vformat(TTR("Move CanvasItem \"%s\" to (%d, %d)"),
- drag_selection.front()->get()->get_name(),
- drag_selection.front()->get()->_edit_get_position().x,
- drag_selection.front()->get()->_edit_get_position().y),
- true);
- }
- } break;
-
- default:
- break;
- }
- }
-
- _reset_drag();
- viewport->queue_redraw();
- _update_cursor();
-}
-
void CanvasItemEditor::_update_cursor() {
if (cursor_shape_override != CURSOR_ARROW) {
set_default_cursor_shape(cursor_shape_override);
return;
}
-
- // Choose the correct default cursor.
- CursorShape c = CURSOR_ARROW;
- switch (tool) {
- case TOOL_MOVE:
- c = CURSOR_MOVE;
- break;
- case TOOL_EDIT_PIVOT:
- c = CURSOR_CROSS;
- break;
- case TOOL_PAN:
- c = CURSOR_DRAG;
- break;
- case TOOL_RULER:
- c = CURSOR_CROSS;
- break;
- default:
- break;
- }
- if (pan_pressed) {
- c = CURSOR_DRAG;
- }
- set_default_cursor_shape(c);
+ set_default_cursor_shape((Control::CursorShape)ci_manipulator->get_cursor_shape());
}
void CanvasItemEditor::_update_lock_and_group_button() {
@@ -3137,79 +1249,14 @@ void CanvasItemEditor::_update_lock_and_group_button() {
}
void CanvasItemEditor::set_cursor_shape_override(CursorShape p_shape) {
- if (cursor_shape_override == p_shape) {
- return;
+ if (cursor_shape_override != p_shape) {
+ cursor_shape_override = p_shape;
+ _update_cursor();
}
- cursor_shape_override = p_shape;
- _update_cursor();
}
Control::CursorShape CanvasItemEditor::get_cursor_shape(const Point2 &p_pos) const {
- // Compute an eventual rotation of the cursor
- const CursorShape rotation_array[4] = { CURSOR_HSIZE, CURSOR_BDIAGSIZE, CURSOR_VSIZE, CURSOR_FDIAGSIZE };
- int rotation_array_index = 0;
-
- List<CanvasItem *> selection = _get_edited_canvas_items();
- if (selection.size() == 1) {
- const double angle = Math::fposmod((double)selection.front()->get()->get_global_transform_with_canvas().get_rotation(), Math::PI);
- if (angle > Math::PI * 7.0 / 8.0) {
- rotation_array_index = 0;
- } else if (angle > Math::PI * 5.0 / 8.0) {
- rotation_array_index = 1;
- } else if (angle > Math::PI * 3.0 / 8.0) {
- rotation_array_index = 2;
- } else if (angle > Math::PI * 1.0 / 8.0) {
- rotation_array_index = 3;
- } else {
- rotation_array_index = 0;
- }
- }
-
- // Choose the correct cursor
- CursorShape c = get_default_cursor_shape();
- switch (drag_type) {
- case DRAG_LEFT:
- case DRAG_RIGHT:
- c = rotation_array[rotation_array_index];
- break;
- case DRAG_V_GUIDE:
- c = CURSOR_HSIZE;
- break;
- case DRAG_TOP:
- case DRAG_BOTTOM:
- c = rotation_array[(rotation_array_index + 2) % 4];
- break;
- case DRAG_H_GUIDE:
- c = CURSOR_VSIZE;
- break;
- case DRAG_TOP_LEFT:
- case DRAG_BOTTOM_RIGHT:
- c = rotation_array[(rotation_array_index + 3) % 4];
- break;
- case DRAG_DOUBLE_GUIDE:
- c = CURSOR_FDIAGSIZE;
- break;
- case DRAG_TOP_RIGHT:
- case DRAG_BOTTOM_LEFT:
- c = rotation_array[(rotation_array_index + 1) % 4];
- break;
- case DRAG_MOVE:
- c = CURSOR_MOVE;
- break;
- default:
- break;
- }
-
- if (is_hovering_h_guide) {
- c = CURSOR_VSIZE;
- } else if (is_hovering_v_guide) {
- c = CURSOR_HSIZE;
- }
-
- if (pan_pressed) {
- c = CURSOR_DRAG;
- }
- return c;
+ return pan_pressed ? CURSOR_DRAG : get_default_cursor_shape();
}
bool CanvasItemEditor::cyclical_dependency_exists(const String &p_target_scene_path, Node *p_desired_node) const {
@@ -3250,7 +1297,7 @@ void CanvasItemEditor::add_node_to_scene(Node *p_parent, Node *p_child, const Ve
if (Object::cast_to<CanvasItem>(p_child)) {
// There's nothing to be used as source position, so snapping will work as absolute if enabled.
- Vector2 target_position = snap_point(p_target_position);
+ Vector2 target_position = ci_manipulator->snap_point(p_target_position);
CanvasItem *parent_ci = Object::cast_to<CanvasItem>(p_parent);
// Set position via undo_redo for proper live editing support.
undo_redo->add_do_method(p_child, "set_position", parent_ci ? parent_ci->get_global_transform().affine_inverse().xform(target_position) : target_position);
@@ -3307,12 +1354,15 @@ void CanvasItemEditor::_draw_focus() {
void CanvasItemEditor::_draw_guides() {
Color guide_color = EDITOR_GET("editors/2d/guides_color");
Transform2D xform = viewport_scrollable->get_transform() * transform;
+ CanvasItemManipulator::DragType drag = ci_manipulator->get_drag_type();
// Guides already there.
if (Node *scene = EditorNode::get_singleton()->get_edited_scene()) {
Array vguides = scene->get_meta("_edit_vertical_guides_", Array());
+ int dragged_guide_index = ci_manipulator->get_dragged_guide_index();
+
for (int i = 0; i < vguides.size(); i++) {
- if (drag_type == DRAG_V_GUIDE && i == dragged_guide_index) {
+ if (drag == CanvasItemManipulator::DRAG_V_GUIDE && i == dragged_guide_index) {
continue;
}
real_t x = xform.xform(Point2(vguides[i], 0)).x;
@@ -3321,7 +1371,7 @@ void CanvasItemEditor::_draw_guides() {
Array hguides = scene->get_meta("_edit_horizontal_guides_", Array());
for (int i = 0; i < hguides.size(); i++) {
- if (drag_type == DRAG_H_GUIDE && i == dragged_guide_index) {
+ if (drag == CanvasItemManipulator::DRAG_H_GUIDE && i == dragged_guide_index) {
continue;
}
real_t y = xform.xform(Point2(0, hguides[i])).y;
@@ -3334,34 +1384,40 @@ void CanvasItemEditor::_draw_guides() {
Color outline_color = text_color.inverted();
const float outline_size = 2;
const String &lang = _get_locale();
- if (drag_type == DRAG_DOUBLE_GUIDE || drag_type == DRAG_V_GUIDE) {
+ Point2 dragged_guide_pos = ci_manipulator->get_dragged_guide_position();
+ real_t ruler_width = ci_manipulator->get_ruler_width();
+
+ if (drag == CanvasItemManipulator::DRAG_DOUBLE_GUIDE || drag == CanvasItemManipulator::DRAG_V_GUIDE) {
String str = TranslationServer::get_singleton()->format_number(vformat("%d px", Math::round(xform.affine_inverse().xform(dragged_guide_pos).x)), lang);
Ref<Font> font = get_theme_font(SNAME("bold"), EditorStringName(EditorFonts));
int font_size = 1.3 * get_theme_font_size(SNAME("bold_size"), EditorStringName(EditorFonts));
Size2 text_size = font->get_string_size(str, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size);
- viewport->draw_string_outline(font, Point2(dragged_guide_pos.x + 10, ruler_width_scaled + text_size.y / 2 + 10), str, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, outline_size, outline_color);
- viewport->draw_string(font, Point2(dragged_guide_pos.x + 10, ruler_width_scaled + text_size.y / 2 + 10), str, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, text_color);
+ viewport->draw_string_outline(font, Point2(dragged_guide_pos.x + 10, ruler_width + text_size.y / 2 + 10), str, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, outline_size, outline_color);
+ viewport->draw_string(font, Point2(dragged_guide_pos.x + 10, ruler_width + text_size.y / 2 + 10), str, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, text_color);
viewport->draw_line(Point2(dragged_guide_pos.x, 0), Point2(dragged_guide_pos.x, viewport->get_size().y), guide_color, Math::round(EDSCALE));
}
- if (drag_type == DRAG_DOUBLE_GUIDE || drag_type == DRAG_H_GUIDE) {
+ if (drag == CanvasItemManipulator::DRAG_DOUBLE_GUIDE || drag == CanvasItemManipulator::DRAG_H_GUIDE) {
String str = TranslationServer::get_singleton()->format_number(vformat("%d px", Math::round(xform.affine_inverse().xform(dragged_guide_pos).y)), lang);
Ref<Font> font = get_theme_font(SNAME("bold"), EditorStringName(EditorFonts));
int font_size = 1.3 * get_theme_font_size(SNAME("bold_size"), EditorStringName(EditorFonts));
Size2 text_size = font->get_string_size(str, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size);
- viewport->draw_string_outline(font, Point2(ruler_width_scaled + 10, dragged_guide_pos.y + text_size.y / 2 + 10), str, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, outline_size, outline_color);
- viewport->draw_string(font, Point2(ruler_width_scaled + 10, dragged_guide_pos.y + text_size.y / 2 + 10), str, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, text_color);
+ viewport->draw_string_outline(font, Point2(ruler_width + 10, dragged_guide_pos.y + text_size.y / 2 + 10), str, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, outline_size, outline_color);
+ viewport->draw_string(font, Point2(ruler_width + 10, dragged_guide_pos.y + text_size.y / 2 + 10), str, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, text_color);
viewport->draw_line(Point2(0, dragged_guide_pos.y), Point2(viewport->get_size().x, dragged_guide_pos.y), guide_color, Math::round(EDSCALE));
}
}
void CanvasItemEditor::_draw_smart_snapping() {
Color line_color = EDITOR_GET("editors/2d/smart_snapping_line_color");
- if (snap_target[0] != SNAP_TARGET_NONE && snap_target[0] != SNAP_TARGET_GRID) {
+ Transform2D snap_transform = ci_manipulator->get_snap_transform();
+ Pair<CanvasItemManipulator::SnapTarget, CanvasItemManipulator::SnapTarget> snap_target = ci_manipulator->get_snap_target();
+
+ if (snap_target.first != CanvasItemManipulator::SNAP_TARGET_NONE && snap_target.first != CanvasItemManipulator::SNAP_TARGET_GRID) {
viewport->draw_set_transform_matrix(viewport->get_transform() * transform * snap_transform);
viewport->draw_line(Point2(0, -1.0e+10F), Point2(0, 1.0e+10F), line_color);
viewport->draw_set_transform_matrix(viewport->get_transform());
}
- if (snap_target[1] != SNAP_TARGET_NONE && snap_target[1] != SNAP_TARGET_GRID) {
+ if (snap_target.second != CanvasItemManipulator::SNAP_TARGET_NONE && snap_target.second != CanvasItemManipulator::SNAP_TARGET_GRID) {
viewport->draw_set_transform_matrix(viewport->get_transform() * transform * snap_transform);
viewport->draw_line(Point2(-1.0e+10F, 0), Point2(1.0e+10F, 0), line_color);
viewport->draw_set_transform_matrix(viewport->get_transform());
@@ -3374,19 +1430,20 @@ void CanvasItemEditor::_draw_rulers() {
Color font_color = get_theme_color(SceneStringName(font_color), EditorStringName(Editor));
font_color.a = 0.9;
Ref<Font> font = get_theme_font(SNAME("rulers"), EditorStringName(EditorFonts));
- real_t ruler_tick_scale = ruler_width_scaled / 15.0;
+ real_t ruler_width = ci_manipulator->get_ruler_width();
+ real_t ruler_tick_scale = ruler_width / 15.0;
const String lang = _get_locale();
- // The rule transform
+ // The rule transform.
Transform2D ruler_transform;
- if (grid_snap_active || is_grid_visible()) {
+ if (ci_manipulator->is_grid_snap_enabled() || is_grid_visible()) {
List<CanvasItem *> selection = _get_edited_canvas_items();
- if (snap_relative && selection.size() > 0) {
+ if (ci_manipulator->is_snap_relative_enabled() && selection.size() > 0) {
ruler_transform.translate_local(_get_encompassing_rect_from_list(selection).position);
- ruler_transform.scale_basis(grid_step * Math::pow(2.0, grid_step_multiplier));
+ ruler_transform.scale_basis(ci_manipulator->get_grid_step() * Math::pow(2.0, ci_manipulator->get_grid_step_multiplier()));
} else {
- ruler_transform.translate_local(grid_offset);
- ruler_transform.scale_basis(grid_step * Math::pow(2.0, grid_step_multiplier));
+ ruler_transform.translate_local(ci_manipulator->get_grid_offset());
+ ruler_transform.scale_basis(ci_manipulator->get_grid_step() * Math::pow(2.0, ci_manipulator->get_grid_step_multiplier()));
}
while ((transform * ruler_transform).get_scale().x < 50.0 * ruler_tick_scale || (transform * ruler_transform).get_scale().y < 50.0 * ruler_tick_scale) {
ruler_transform.scale_basis(Point2(2, 2));
@@ -3402,7 +1459,7 @@ void CanvasItemEditor::_draw_rulers() {
ruler_transform.scale(Size2(basic_rule, basic_rule));
}
- // Subdivisions
+ // Subdivisions.
int major_subdivision = 2;
Transform2D major_subdivide;
major_subdivide.scale(Size2(1.0 / major_subdivision, 1.0 / major_subdivision));
@@ -3411,34 +1468,34 @@ void CanvasItemEditor::_draw_rulers() {
Transform2D minor_subdivide;
minor_subdivide.scale(Size2(1.0 / minor_subdivision, 1.0 / minor_subdivision));
- // First and last graduations to draw (in the ruler space)
- Point2 first = (transform * ruler_transform * major_subdivide * minor_subdivide).affine_inverse().xform(Point2(ruler_width_scaled, ruler_width_scaled));
+ // First and last graduations to draw (in the ruler space).
+ Point2 first = (transform * ruler_transform * major_subdivide * minor_subdivide).affine_inverse().xform(Point2(ruler_width, ruler_width));
Point2 last = (transform * ruler_transform * major_subdivide * minor_subdivide).affine_inverse().xform(viewport->get_size());
- // Draw top ruler
- viewport->draw_rect(Rect2(Point2(ruler_width_scaled, 0), Size2(viewport->get_size().x, ruler_width_scaled)), bg_color);
+ // Draw top ruler.
+ viewport->draw_rect(Rect2(Point2(ruler_width, 0), Size2(viewport->get_size().x, ruler_width)), bg_color);
for (int i = Math::ceil(first.x); i < last.x; i++) {
Point2 position = (transform * ruler_transform * major_subdivide * minor_subdivide).xform(Point2(i, 0)).round();
if (i % (major_subdivision * minor_subdivision) == 0) {
- viewport->draw_line(Point2(position.x, 0), Point2(position.x, ruler_width_scaled), graduation_color, Math::round(EDSCALE));
+ viewport->draw_line(Point2(position.x, 0), Point2(position.x, ruler_width), graduation_color, Math::round(EDSCALE));
real_t val = (ruler_transform * major_subdivide * minor_subdivide).xform(Point2(i, 0)).x;
const String &formatted = TranslationServer::get_singleton()->format_number(vformat(((int)val == val) ? "%d" : "%.1f", val), lang);
viewport->draw_string(font, Point2(position.x + MAX(Math::round(ruler_font_size / 8.0), 2), font->get_ascent(ruler_font_size) + Math::round(EDSCALE)), formatted, HORIZONTAL_ALIGNMENT_LEFT, -1, ruler_font_size, font_color);
} else {
if (i % minor_subdivision == 0) {
- viewport->draw_line(Point2(position.x, ruler_width_scaled * 0.33), Point2(position.x, ruler_width_scaled), graduation_color, Math::round(EDSCALE));
+ viewport->draw_line(Point2(position.x, ruler_width * 0.33), Point2(position.x, ruler_width), graduation_color, Math::round(EDSCALE));
} else {
- viewport->draw_line(Point2(position.x, ruler_width_scaled * 0.75), Point2(position.x, ruler_width_scaled), graduation_color, Math::round(EDSCALE));
+ viewport->draw_line(Point2(position.x, ruler_width * 0.75), Point2(position.x, ruler_width), graduation_color, Math::round(EDSCALE));
}
}
}
- // Draw left ruler
- viewport->draw_rect(Rect2(Point2(0, ruler_width_scaled), Size2(ruler_width_scaled, viewport->get_size().y)), bg_color);
+ // Draw left ruler.
+ viewport->draw_rect(Rect2(Point2(0, ruler_width), Size2(ruler_width, viewport->get_size().y)), bg_color);
for (int i = Math::ceil(first.y); i < last.y; i++) {
Point2 position = (transform * ruler_transform * major_subdivide * minor_subdivide).xform(Point2(0, i)).round();
if (i % (major_subdivision * minor_subdivision) == 0) {
- viewport->draw_line(Point2(0, position.y), Point2(ruler_width_scaled, position.y), graduation_color, Math::round(EDSCALE));
+ viewport->draw_line(Point2(0, position.y), Point2(ruler_width, position.y), graduation_color, Math::round(EDSCALE));
real_t val = (ruler_transform * major_subdivide * minor_subdivide).xform(Point2(0, i)).y;
Transform2D text_xform = Transform2D(-Math::PI / 2.0, Point2(font->get_ascent(ruler_font_size) + Math::round(EDSCALE), position.y - 2));
@@ -3449,24 +1506,27 @@ void CanvasItemEditor::_draw_rulers() {
} else {
if (i % minor_subdivision == 0) {
- viewport->draw_line(Point2(ruler_width_scaled * 0.33, position.y), Point2(ruler_width_scaled, position.y), graduation_color, Math::round(EDSCALE));
+ viewport->draw_line(Point2(ruler_width * 0.33, position.y), Point2(ruler_width, position.y), graduation_color, Math::round(EDSCALE));
} else {
- viewport->draw_line(Point2(ruler_width_scaled * 0.75, position.y), Point2(ruler_width_scaled, position.y), graduation_color, Math::round(EDSCALE));
+ viewport->draw_line(Point2(ruler_width * 0.75, position.y), Point2(ruler_width, position.y), graduation_color, Math::round(EDSCALE));
}
}
}
- // Draw the top left corner
- viewport->draw_rect(Rect2(Point2(), Size2(ruler_width_scaled, ruler_width_scaled)), graduation_color);
+ // Draw the top left corner.
+ viewport->draw_rect(Rect2(Point2(), Size2(ruler_width, ruler_width)), graduation_color);
}
void CanvasItemEditor::_draw_grid() {
if (is_grid_visible()) {
- // Draw the grid
+ // Draw the grid.
Vector2 real_grid_offset;
const List<CanvasItem *> selection = _get_edited_canvas_items();
+ Point2 grid_step = ci_manipulator->get_grid_step();
+ Point2 grid_offset = ci_manipulator->get_grid_offset();
+ int grid_step_multiplier = ci_manipulator->get_grid_step_multiplier();
- if (snap_relative && selection.size() > 0) {
+ if (ci_manipulator->is_snap_relative_enabled() && selection.size() > 0) {
const Vector2 topleft = _get_encompassing_rect_from_list(selection).position;
real_grid_offset.x = std::fmod(topleft.x, grid_step.x * (real_t)Math::pow(2.0, grid_step_multiplier));
real_grid_offset.y = std::fmod(topleft.y, grid_step.y * (real_t)Math::pow(2.0, grid_step_multiplier));
@@ -3533,12 +1593,14 @@ void CanvasItemEditor::_draw_grid() {
}
void CanvasItemEditor::_draw_ruler_tool() {
- if (tool != TOOL_RULER) {
+ if (ci_manipulator->get_tool() != CanvasItemManipulator::TOOL_RULER) {
return;
}
+ Point2 ruler_tool_origin = ci_manipulator->get_ruler_tool_origin();
+ bool grid_snap_active = ci_manipulator->is_grid_snap_enabled();
const Ref<Texture2D> position_icon = get_editor_theme_icon(SNAME("EditorPosition"));
- if (ruler_tool_active) {
+ if (ci_manipulator->is_ruler_tool_active()) {
const String &lang = _get_locale();
const TranslationServer *ts = TranslationServer::get_singleton();
@@ -3547,7 +1609,7 @@ void CanvasItemEditor::_draw_ruler_tool() {
ruler_secondary_color.a = 0.5;
Point2 begin = (ruler_tool_origin - view_offset) * zoom;
- Point2 end = snap_point(viewport->get_local_mouse_position() / zoom + view_offset) * zoom - view_offset * zoom;
+ Point2 end = ci_manipulator->snap_point(viewport->get_local_mouse_position() / zoom + view_offset) * zoom - view_offset * zoom;
Point2 corner = Point2(begin.x, end.y);
Vector2 length_vector = (begin - end).abs() / zoom;
@@ -3660,6 +1722,7 @@ void CanvasItemEditor::_draw_ruler_tool() {
text_pos.x = CLAMP(text_pos.x, text_width / 2, viewport->get_rect().size.x - text_width * 1.5);
text_pos.y = CLAMP(text_pos.y, text_height * 2.5, viewport->get_rect().size.y - text_height / 2);
+ Point2 grid_step = ci_manipulator->get_grid_step();
if (draw_secondary_lines) {
viewport->draw_string_outline(font, text_pos, ts->format_number(vformat("%.2f " + TTR("units"), (length_vector / grid_step).length()), lang), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, outline_size, outline_color);
viewport->draw_string(font, text_pos, ts->format_number(vformat("%.2f " + TTR("units"), (length_vector / grid_step).length()), lang), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, font_color);
@@ -3678,18 +1741,16 @@ void CanvasItemEditor::_draw_ruler_tool() {
viewport->draw_string(font, text_pos, ts->format_number(vformat("%d " + TTR("units"), std::round((length_vector / grid_step).length())), lang), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, font_color);
}
}
- } else {
- if (grid_snap_active) {
- viewport->draw_texture(position_icon, (ruler_tool_origin - view_offset) * zoom - position_icon->get_size() / 2);
- }
+ } else if (grid_snap_active) {
+ viewport->draw_texture(position_icon, (ruler_tool_origin - view_offset) * zoom - position_icon->get_size() / 2);
}
}
void CanvasItemEditor::_draw_control_anchors(Control *control) {
Transform2D xform = transform * control->get_screen_transform();
RID ci = viewport->get_canvas_item();
- if (tool == TOOL_SELECT && !Object::cast_to<Container>(control->get_parent())) {
- // Compute the anchors
+ if (ci_manipulator->get_tool() == CanvasItemManipulator::TOOL_SELECT && !Object::cast_to<Container>(control->get_parent())) {
+ // Compute the anchors.
real_t anchors_values[4];
anchors_values[0] = control->get_anchor(SIDE_LEFT);
anchors_values[1] = control->get_anchor(SIDE_TOP);
@@ -3702,7 +1763,7 @@ void CanvasItemEditor::_draw_control_anchors(Control *control) {
anchors_pos[i] = xform.xform(_anchor_to_position(control, value));
}
- // Draw the anchors handles
+ // Draw the anchors handles.
Rect2 anchor_rects[4];
if (control->is_layout_rtl()) {
anchor_rects[0] = Rect2(anchors_pos[0] - Vector2(0.0, anchor_handle->get_size().y), Point2(-anchor_handle->get_size().x, anchor_handle->get_size().y));
@@ -3724,11 +1785,11 @@ void CanvasItemEditor::_draw_control_anchors(Control *control) {
void CanvasItemEditor::_draw_control_helpers(Control *control) {
Transform2D xform = transform * control->get_screen_transform();
- if (tool == TOOL_SELECT && show_helpers && !Object::cast_to<Container>(control->get_parent())) {
- // Draw the helpers
+ if (ci_manipulator->get_tool() == CanvasItemManipulator::TOOL_SELECT && show_helpers && !Object::cast_to<Container>(control->get_parent())) {
+ // Draw the helpers.
Color color_base = Color(0.8, 0.8, 0.8, 0.5);
- // Compute the anchors
+ // Compute the anchors.
real_t anchors_values[4];
anchors_values[0] = control->get_anchor(SIDE_LEFT);
anchors_values[1] = control->get_anchor(SIDE_TOP);
@@ -3742,20 +1803,22 @@ void CanvasItemEditor::_draw_control_helpers(Control *control) {
anchors_pos[i] = xform.xform(_anchor_to_position(control, anchors[i]));
}
- // Get which anchor is dragged
+ CanvasItemManipulator::DragType drag_type = ci_manipulator->get_drag_type();
+
+ // Get which anchor is dragged.
int dragged_anchor = -1;
switch (drag_type) {
- case DRAG_ANCHOR_ALL:
- case DRAG_ANCHOR_TOP_LEFT:
+ case CanvasItemManipulator::DRAG_ANCHOR_ALL:
+ case CanvasItemManipulator::DRAG_ANCHOR_TOP_LEFT:
dragged_anchor = 0;
break;
- case DRAG_ANCHOR_TOP_RIGHT:
+ case CanvasItemManipulator::DRAG_ANCHOR_TOP_RIGHT:
dragged_anchor = 1;
break;
- case DRAG_ANCHOR_BOTTOM_RIGHT:
+ case CanvasItemManipulator::DRAG_ANCHOR_BOTTOM_RIGHT:
dragged_anchor = 2;
break;
- case DRAG_ANCHOR_BOTTOM_LEFT:
+ case CanvasItemManipulator::DRAG_ANCHOR_BOTTOM_LEFT:
dragged_anchor = 3;
break;
default:
@@ -3814,12 +1877,12 @@ void CanvasItemEditor::_draw_control_helpers(Control *control) {
Point2 start, end;
switch (drag_type) {
- case DRAG_LEFT:
- case DRAG_TOP_LEFT:
- case DRAG_BOTTOM_LEFT:
+ case CanvasItemManipulator::DRAG_LEFT:
+ case CanvasItemManipulator::DRAG_TOP_LEFT:
+ case CanvasItemManipulator::DRAG_BOTTOM_LEFT:
_draw_margin_at_position(control->get_size().width, parent_transform.xform(Vector2((node_pos_in_parent[0] + node_pos_in_parent[2]) / 2, node_pos_in_parent[3])) + Vector2(0, 5), SIDE_BOTTOM);
[[fallthrough]];
- case DRAG_MOVE:
+ case CanvasItemManipulator::DRAG_MOVE:
start = Vector2(node_pos_in_parent[0], Math::lerp(node_pos_in_parent[1], node_pos_in_parent[3], ratio));
end = start - Vector2(control->get_offset(SIDE_LEFT), 0);
_draw_margin_at_position(control->get_offset(SIDE_LEFT), parent_transform.xform((start + end) / 2), SIDE_TOP);
@@ -3829,12 +1892,12 @@ void CanvasItemEditor::_draw_control_helpers(Control *control) {
break;
}
switch (drag_type) {
- case DRAG_RIGHT:
- case DRAG_TOP_RIGHT:
- case DRAG_BOTTOM_RIGHT:
+ case CanvasItemManipulator::DRAG_RIGHT:
+ case CanvasItemManipulator::DRAG_TOP_RIGHT:
+ case CanvasItemManipulator::DRAG_BOTTOM_RIGHT:
_draw_margin_at_position(control->get_size().width, parent_transform.xform(Vector2((node_pos_in_parent[0] + node_pos_in_parent[2]) / 2, node_pos_in_parent[3])) + Vector2(0, 5), SIDE_BOTTOM);
[[fallthrough]];
- case DRAG_MOVE:
+ case CanvasItemManipulator::DRAG_MOVE:
start = Vector2(node_pos_in_parent[2], Math::lerp(node_pos_in_parent[3], node_pos_in_parent[1], ratio));
end = start - Vector2(control->get_offset(SIDE_RIGHT), 0);
_draw_margin_at_position(control->get_offset(SIDE_RIGHT), parent_transform.xform((start + end) / 2), SIDE_BOTTOM);
@@ -3844,12 +1907,12 @@ void CanvasItemEditor::_draw_control_helpers(Control *control) {
break;
}
switch (drag_type) {
- case DRAG_TOP:
- case DRAG_TOP_LEFT:
- case DRAG_TOP_RIGHT:
+ case CanvasItemManipulator::DRAG_TOP:
+ case CanvasItemManipulator::DRAG_TOP_LEFT:
+ case CanvasItemManipulator::DRAG_TOP_RIGHT:
_draw_margin_at_position(control->get_size().height, parent_transform.xform(Vector2(node_pos_in_parent[2], (node_pos_in_parent[1] + node_pos_in_parent[3]) / 2)) + Vector2(5, 0), SIDE_RIGHT);
[[fallthrough]];
- case DRAG_MOVE:
+ case CanvasItemManipulator::DRAG_MOVE:
start = Vector2(Math::lerp(node_pos_in_parent[0], node_pos_in_parent[2], ratio), node_pos_in_parent[1]);
end = start - Vector2(0, control->get_offset(SIDE_TOP));
_draw_margin_at_position(control->get_offset(SIDE_TOP), parent_transform.xform((start + end) / 2), SIDE_LEFT);
@@ -3859,12 +1922,12 @@ void CanvasItemEditor::_draw_control_helpers(Control *control) {
break;
}
switch (drag_type) {
- case DRAG_BOTTOM:
- case DRAG_BOTTOM_LEFT:
- case DRAG_BOTTOM_RIGHT:
+ case CanvasItemManipulator::DRAG_BOTTOM:
+ case CanvasItemManipulator::DRAG_BOTTOM_LEFT:
+ case CanvasItemManipulator::DRAG_BOTTOM_RIGHT:
_draw_margin_at_position(control->get_size().height, parent_transform.xform(Vector2(node_pos_in_parent[2], (node_pos_in_parent[1] + node_pos_in_parent[3]) / 2) + Vector2(5, 0)), SIDE_RIGHT);
[[fallthrough]];
- case DRAG_MOVE:
+ case CanvasItemManipulator::DRAG_MOVE:
start = Vector2(Math::lerp(node_pos_in_parent[2], node_pos_in_parent[0], ratio), node_pos_in_parent[3]);
end = start - Vector2(0, control->get_offset(SIDE_BOTTOM));
_draw_margin_at_position(control->get_offset(SIDE_BOTTOM), parent_transform.xform((start + end) / 2), SIDE_RIGHT);
@@ -3876,15 +1939,15 @@ void CanvasItemEditor::_draw_control_helpers(Control *control) {
switch (drag_type) {
//Draw the ghost rect if the node if rotated/scaled
- case DRAG_LEFT:
- case DRAG_TOP_LEFT:
- case DRAG_TOP:
- case DRAG_TOP_RIGHT:
- case DRAG_RIGHT:
- case DRAG_BOTTOM_RIGHT:
- case DRAG_BOTTOM:
- case DRAG_BOTTOM_LEFT:
- case DRAG_MOVE:
+ case CanvasItemManipulator::DRAG_LEFT:
+ case CanvasItemManipulator::DRAG_TOP_LEFT:
+ case CanvasItemManipulator::DRAG_TOP:
+ case CanvasItemManipulator::DRAG_TOP_RIGHT:
+ case CanvasItemManipulator::DRAG_RIGHT:
+ case CanvasItemManipulator::DRAG_BOTTOM_RIGHT:
+ case CanvasItemManipulator::DRAG_BOTTOM:
+ case CanvasItemManipulator::DRAG_BOTTOM_LEFT:
+ case CanvasItemManipulator::DRAG_MOVE:
if (control->get_rotation() != 0.0 || control->get_scale() != Vector2(1, 1)) {
Rect2 rect = Rect2(Vector2(node_pos_in_parent[0], node_pos_in_parent[1]), control->get_size());
viewport->draw_rect(parent_transform.xform(rect), color_base, false, Math::round(EDSCALE));
@@ -3904,7 +1967,10 @@ void CanvasItemEditor::_draw_selection() {
RID vp_ci = viewport->get_canvas_item();
List<CanvasItem *> selection = _get_edited_canvas_items(true, false);
bool single = selection.size() == 1;
- bool transform_tool = tool == TOOL_SELECT || tool == TOOL_MOVE || tool == TOOL_SCALE || tool == TOOL_ROTATE || tool == TOOL_EDIT_PIVOT;
+ CanvasItemManipulator::Tool tool = ci_manipulator->get_tool();
+ bool transform_tool = tool == CanvasItemManipulator::TOOL_SELECT || tool == CanvasItemManipulator::TOOL_MOVE || tool == CanvasItemManipulator::TOOL_SCALE || tool == CanvasItemManipulator::TOOL_ROTATE || tool == CanvasItemManipulator::TOOL_EDIT_PIVOT;
+ CanvasItemManipulator::DragType drag_type = ci_manipulator->get_drag_type();
+ bool use_local_space = ci_manipulator->is_local_space_enabled();
for (CanvasItem *E : selection) {
CanvasItem *ci = Object::cast_to<CanvasItem>(E);
@@ -3912,9 +1978,9 @@ void CanvasItemEditor::_draw_selection() {
// Draw the previous position if we are dragging the node
if (show_helpers &&
- (drag_type == DRAG_MOVE || drag_type == DRAG_ROTATE ||
- drag_type == DRAG_LEFT || drag_type == DRAG_RIGHT || drag_type == DRAG_TOP || drag_type == DRAG_BOTTOM ||
- drag_type == DRAG_TOP_LEFT || drag_type == DRAG_TOP_RIGHT || drag_type == DRAG_BOTTOM_LEFT || drag_type == DRAG_BOTTOM_RIGHT)) {
+ (drag_type == CanvasItemManipulator::DRAG_MOVE || drag_type == CanvasItemManipulator::DRAG_ROTATE ||
+ drag_type == CanvasItemManipulator::DRAG_LEFT || drag_type == CanvasItemManipulator::DRAG_RIGHT || drag_type == CanvasItemManipulator::DRAG_TOP || drag_type == CanvasItemManipulator::DRAG_BOTTOM ||
+ drag_type == CanvasItemManipulator::DRAG_TOP_LEFT || drag_type == CanvasItemManipulator::DRAG_TOP_RIGHT || drag_type == CanvasItemManipulator::DRAG_BOTTOM_LEFT || drag_type == CanvasItemManipulator::DRAG_BOTTOM_RIGHT)) {
const Transform2D pre_drag_xform = transform * se->pre_drag_xform;
const Color pre_drag_color = Color(0.4, 0.6, 1, 0.7);
@@ -3985,13 +2051,13 @@ void CanvasItemEditor::_draw_selection() {
// Draw control-related helpers
Control *control = Object::cast_to<Control>(ci);
- if (control && _is_node_movable(control)) {
+ if (control && ci_manipulator->is_node_movable(control)) {
_draw_control_anchors(control);
_draw_control_helpers(control);
}
// Draw the resize handles
- if (tool == TOOL_SELECT && ci->_edit_use_rect() && _is_node_movable(ci)) {
+ if (tool == CanvasItemManipulator::TOOL_SELECT && ci->_edit_use_rect() && ci_manipulator->is_node_movable(ci)) {
Rect2 rect = ci->_edit_get_rect();
const Vector2 endpoints[4] = {
xform.xform(rect.position),
@@ -4020,13 +2086,13 @@ void CanvasItemEditor::_draw_selection() {
// Remove non-movable nodes.
for (List<CanvasItem *>::Element *E = selection.front(); E;) {
List<CanvasItem *>::Element *N = E->next();
- if (!_is_node_movable(E->get())) {
+ if (!ci_manipulator->is_node_movable(E->get())) {
selection.erase(E);
}
E = N;
}
- if (!selection.is_empty() && transform_tool && show_transformation_gizmos) {
+ if (!selection.is_empty() && transform_tool && ci_manipulator->is_showing_transformation_gizmos()) {
CanvasItem *ci = selection.front()->get();
Transform2D xform = transform * ci->get_screen_transform();
@@ -4034,7 +2100,7 @@ void CanvasItemEditor::_draw_selection() {
bool is_alt = Input::get_singleton()->is_key_pressed(Key::ALT);
// Draw the move handles.
- if ((tool == TOOL_SELECT && is_alt && !is_ctrl) || tool == TOOL_MOVE) {
+ if ((tool == CanvasItemManipulator::TOOL_SELECT && is_alt && !is_ctrl) || tool == CanvasItemManipulator::TOOL_MOVE) {
Transform2D unscaled_transform = (xform * ci->get_transform().affine_inverse() * ci->_edit_get_transform()).orthonormalized();
Transform2D simple_xform;
if (use_local_space) {
@@ -4044,7 +2110,7 @@ void CanvasItemEditor::_draw_selection() {
simple_xform = viewport->get_transform() * translation;
}
- Size2 move_factor = Size2(MOVE_HANDLE_DISTANCE, MOVE_HANDLE_DISTANCE);
+ Size2 move_factor = Size2(CanvasItemManipulator::GIZMO_HANDLE_DISTANCE, CanvasItemManipulator::GIZMO_HANDLE_DISTANCE);
viewport->draw_set_transform_matrix(simple_xform);
Vector<Point2> points = {
@@ -4068,8 +2134,9 @@ void CanvasItemEditor::_draw_selection() {
}
// Draw the rescale handles.
- if ((tool == TOOL_SELECT && is_alt && is_ctrl) || tool == TOOL_SCALE || drag_type == DRAG_SCALE_X || drag_type == DRAG_SCALE_Y) {
+ if ((tool == CanvasItemManipulator::TOOL_SELECT && is_alt && is_ctrl) || tool == CanvasItemManipulator::TOOL_SCALE || drag_type == CanvasItemManipulator::DRAG_SCALE_X || drag_type == CanvasItemManipulator::DRAG_SCALE_Y) {
Transform2D edit_transform;
+ Point2 temp_pivot = ci_manipulator->get_temp_pivot();
if (!Math::is_inf(temp_pivot.x) || !Math::is_inf(temp_pivot.y)) {
edit_transform = Transform2D(ci->_edit_get_rotation(), temp_pivot);
} else {
@@ -4084,16 +2151,16 @@ void CanvasItemEditor::_draw_selection() {
simple_xform = viewport->get_transform() * translation;
}
- Size2 scale_factor = Size2(SCALE_HANDLE_DISTANCE, SCALE_HANDLE_DISTANCE);
+ Size2 scale_factor = Size2(CanvasItemManipulator::GIZMO_HANDLE_DISTANCE, CanvasItemManipulator::GIZMO_HANDLE_DISTANCE);
bool uniform = Input::get_singleton()->is_key_pressed(Key::SHIFT);
- Point2 offset = (simple_xform.affine_inverse().xform(drag_to) - simple_xform.affine_inverse().xform(drag_from)) * zoom;
+ Point2 offset = (simple_xform.affine_inverse().xform(ci_manipulator->get_drag_to()) - simple_xform.affine_inverse().xform(ci_manipulator->get_drag_from())) * zoom;
- if (drag_type == DRAG_SCALE_X) {
+ if (drag_type == CanvasItemManipulator::DRAG_SCALE_X) {
scale_factor.x += offset.x;
if (uniform) {
scale_factor.y += offset.x;
}
- } else if (drag_type == DRAG_SCALE_Y) {
+ } else if (drag_type == CanvasItemManipulator::DRAG_SCALE_Y) {
scale_factor.y += offset.y;
if (uniform) {
scale_factor.x += offset.y;
@@ -4113,31 +2180,29 @@ void CanvasItemEditor::_draw_selection() {
}
}
- if (drag_type == DRAG_BOX_SELECTION) {
- // Draw the dragging box
- Point2 bsfrom = transform.xform(drag_from);
- Point2 bsto = transform.xform(box_selecting_to);
-
+ if (drag_type == CanvasItemManipulator::DRAG_BOX_SELECTION) {
+ // Draw the dragging box.
viewport->draw_rect(
- Rect2(bsfrom, bsto - bsfrom),
+ box_selection_rect,
get_theme_color(SNAME("box_selection_fill_color"), EditorStringName(Editor)));
viewport->draw_rect(
- Rect2(bsfrom, bsto - bsfrom),
+ box_selection_rect,
get_theme_color(SNAME("box_selection_stroke_color"), EditorStringName(Editor)),
false,
Math::round(EDSCALE));
}
- if (drag_type == DRAG_ROTATE) {
+ if (drag_type == CanvasItemManipulator::DRAG_ROTATE) {
// Draw the line when rotating a node
viewport->draw_line(
- transform.xform(drag_rotation_center),
- transform.xform(drag_to),
+ transform.xform(ci_manipulator->get_drag_rotation_center()),
+ transform.xform(ci_manipulator->get_drag_to()),
get_theme_color(SNAME("accent_color"), EditorStringName(Editor)) * Color(1, 1, 1, 0.6),
Math::round(2 * EDSCALE));
}
+ Point2 temp_pivot = ci_manipulator->get_temp_pivot();
if (!Math::is_inf(temp_pivot.x) || !Math::is_inf(temp_pivot.y)) {
viewport->draw_texture(pivot_icon, (temp_pivot - view_offset) * zoom - (pivot_icon->get_size() / 2).floor(), get_theme_color(SNAME("accent_color"), EditorStringName(Editor)));
}
@@ -4242,14 +2307,14 @@ 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 (show_position_gizmos && 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) || CanvasItemManipulator::is_node_locked(ci))) {
Transform2D xform = transform * canvas_xform * parent_xform;
// Draw the node's position
Ref<Texture2D> position_icon = get_editor_theme_icon(SNAME("EditorPositionUnselected"));
Transform2D unscaled_transform = (xform * ci->get_transform().affine_inverse() * ci->_edit_get_transform()).orthonormalized();
Transform2D simple_xform;
- if (use_local_space) {
+ if (ci_manipulator->is_local_space_enabled()) {
simple_xform = viewport->get_transform() * unscaled_transform;
} else {
Transform2D translation = Transform2D(0.0f, unscaled_transform.get_origin());
@@ -4294,49 +2359,56 @@ void CanvasItemEditor::_draw_hover() {
}
void CanvasItemEditor::_draw_message() {
- if (drag_type != DRAG_NONE && !drag_selection.is_empty() && drag_selection.front()->get()) {
- Transform2D current_transform = drag_selection.front()->get()->get_global_transform();
+ CanvasItemManipulator::DragType drag_type = ci_manipulator->get_drag_type();
- double snap = EDITOR_GET("interface/inspector/default_float_step");
- int snap_step_decimals = Math::range_step_decimals(snap);
- const String &lang = _get_locale();
+ if (drag_type != CanvasItemManipulator::DRAG_NONE) {
+ TypedArray<CanvasItem> drag_selection = ci_manipulator->get_drag_selection();
+
+ if (!drag_selection.is_empty() && drag_selection.front()) {
+ Transform2D current_transform = Object::cast_to<CanvasItem>(drag_selection.front())->get_global_transform();
+ bool use_local_space = ci_manipulator->is_local_space_enabled();
+
+ double snap = EDITOR_GET("interface/inspector/default_float_step");
+ int snap_step_decimals = Math::range_step_decimals(snap);
+ const String &lang = _get_locale();
#define FORMAT(value) (TranslationServer::get_singleton()->format_number(String::num(value, snap_step_decimals), lang))
- switch (drag_type) {
- case DRAG_MOVE:
- case DRAG_MOVE_X:
- case DRAG_MOVE_Y: {
- Vector2 delta = current_transform.get_origin() - original_transform.get_origin();
- if (drag_type == DRAG_MOVE || use_local_space) {
- message = TTR("Moving:") + " (" + FORMAT(delta.x) + ", " + FORMAT(delta.y) + ") px";
- } else if (drag_type == DRAG_MOVE_X) {
- message = TTR("Moving:") + " " + FORMAT(delta.x) + " px";
- } else if (drag_type == DRAG_MOVE_Y) {
- message = TTR("Moving:") + " " + FORMAT(delta.y) + " px";
- }
- } break;
-
- case DRAG_ROTATE: {
- real_t delta = Math::rad_to_deg(current_transform.get_rotation() - original_transform.get_rotation());
- message = TTR("Rotating:") + " " + FORMAT(delta) + String::utf8(" °");
- } break;
-
- case DRAG_SCALE_X:
- case DRAG_SCALE_Y:
- case DRAG_SCALE_BOTH: {
- Vector2 original_scale = (Math::is_zero_approx(original_transform.get_scale().x) || Math::is_zero_approx(original_transform.get_scale().y)) ? Vector2(CMP_EPSILON, CMP_EPSILON) : original_transform.get_scale();
- Vector2 delta = current_transform.get_scale() / original_scale;
- if (drag_type == DRAG_SCALE_BOTH || !use_local_space) {
- message = TTR("Scaling:") + String::utf8(" ×(") + FORMAT(delta.x) + ", " + FORMAT(delta.y) + ")";
- } else if (drag_type == DRAG_SCALE_X) {
- message = TTR("Scaling:") + String::utf8(" ×") + FORMAT(delta.x);
- } else if (drag_type == DRAG_SCALE_Y) {
- message = TTR("Scaling:") + String::utf8(" ×") + FORMAT(delta.y);
- }
- } break;
+ switch (drag_type) {
+ case CanvasItemManipulator::DRAG_MOVE:
+ case CanvasItemManipulator::DRAG_MOVE_X:
+ case CanvasItemManipulator::DRAG_MOVE_Y: {
+ Vector2 delta = current_transform.get_origin() - original_transform.get_origin();
+ if (drag_type == CanvasItemManipulator::DRAG_MOVE || use_local_space) {
+ message = TTR("Moving:") + " (" + FORMAT(delta.x) + ", " + FORMAT(delta.y) + ") px";
+ } else if (drag_type == CanvasItemManipulator::DRAG_MOVE_X) {
+ message = TTR("Moving:") + " " + FORMAT(delta.x) + " px";
+ } else if (drag_type == CanvasItemManipulator::DRAG_MOVE_Y) {
+ message = TTR("Moving:") + " " + FORMAT(delta.y) + " px";
+ }
+ } break;
+
+ case CanvasItemManipulator::DRAG_ROTATE: {
+ real_t delta = Math::rad_to_deg(current_transform.get_rotation() - original_transform.get_rotation());
+ message = TTR("Rotating:") + " " + FORMAT(delta) + String::utf8(" °");
+ } break;
+
+ case CanvasItemManipulator::DRAG_SCALE_X:
+ case CanvasItemManipulator::DRAG_SCALE_Y:
+ case CanvasItemManipulator::DRAG_SCALE_BOTH: {
+ Vector2 original_scale = (Math::is_zero_approx(original_transform.get_scale().x) || Math::is_zero_approx(original_transform.get_scale().y)) ? Vector2(CMP_EPSILON, CMP_EPSILON) : original_transform.get_scale();
+ Vector2 delta = current_transform.get_scale() / original_scale;
+ if (drag_type == CanvasItemManipulator::DRAG_SCALE_BOTH || !use_local_space) {
+ message = TTR("Scaling:") + String::utf8(" ×(") + FORMAT(delta.x) + ", " + FORMAT(delta.y) + ")";
+ } else if (drag_type == CanvasItemManipulator::DRAG_SCALE_X) {
+ message = TTR("Scaling:") + String::utf8(" ×") + FORMAT(delta.x);
+ } else if (drag_type == CanvasItemManipulator::DRAG_SCALE_Y) {
+ message = TTR("Scaling:") + String::utf8(" ×") + FORMAT(delta.y);
+ }
+ } break;
- default:
- break;
+ default:
+ break;
+ }
}
#undef FORMAT
}
@@ -4347,7 +2419,7 @@ void CanvasItemEditor::_draw_message() {
Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
- Point2 msgpos = Point2(ruler_width_scaled + 10 * EDSCALE, viewport->get_size().y - 14 * EDSCALE);
+ Point2 msgpos = Point2(ci_manipulator->get_ruler_width() + 10 * EDSCALE, viewport->get_size().y - 14 * EDSCALE);
viewport->draw_string(font, msgpos + Point2(1, 1), message, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, Color(0, 0, 0, 0.8));
viewport->draw_string(font, msgpos + Point2(-1, -1), message, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, Color(0, 0, 0, 0.8));
viewport->draw_string(font, msgpos, message, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, Color(1, 1, 1, 1));
@@ -4425,10 +2497,10 @@ void CanvasItemEditor::_draw_viewport() {
EditorNode::get_singleton()->get_editor_plugins_over()->forward_canvas_draw_over_viewport(viewport);
EditorNode::get_singleton()->get_editor_plugins_force_over()->forward_canvas_force_draw_over_viewport(viewport);
- if (show_rulers) {
+ if (ci_manipulator->is_showing_rulers()) {
_draw_rulers();
}
- if (show_guides) {
+ if (ci_manipulator->is_showing_guides()) {
_draw_guides();
}
_draw_smart_snapping();
@@ -4442,7 +2514,7 @@ void CanvasItemEditor::update_viewport() {
viewport->queue_redraw();
}
-void CanvasItemEditor::set_current_tool(Tool p_tool) {
+void CanvasItemEditor::set_current_tool(CanvasItemManipulator::Tool p_tool) {
_button_tool_select(p_tool);
}
@@ -4496,9 +2568,10 @@ void CanvasItemEditor::_update_editor_settings() {
// Compute the ruler width here so we can reuse the result throughout the various draw functions.
real_t ruler_width_unscaled = EDITOR_GET("editors/2d/ruler_width");
ruler_font_size = MAX(get_theme_font_size(SNAME("rulers_size"), EditorStringName(EditorFonts)) * ruler_width_unscaled / 15.0, 8);
- ruler_width_scaled = MAX(ruler_width_unscaled * EDSCALE, ruler_font_size * 2.0);
+ ci_manipulator->set_ruler_width(MAX(ruler_width_unscaled * EDSCALE, ruler_font_size * 2.0));
- grab_distance = EDITOR_GET("editors/polygon_editor/point_grab_radius");
+ ci_manipulator->set_grab_distance(EDITOR_GET("editors/polygon_editor/point_grab_radius"));
+ ci_manipulator->set_shortcut(CanvasItemManipulator::SHORTCUT_CANCEL_TRANSFORM, ED_GET_SHORTCUT("canvas_item_editor/cancel_transform"));
resample_delay = EDITOR_GET("editors/2d/auto_resample_delay");
resample_timer->set_wait_time(resample_delay);
@@ -4531,6 +2604,18 @@ void CanvasItemEditor::_notification(int p_what) {
AnimationPlayerEditor::get_singleton()->connect("animation_selected", callable_mp(this, &CanvasItemEditor::_keying_changed).unbind(1));
_update_editor_settings();
+ ci_manipulator->set_viewport(get_viewport());
+ ci_manipulator->set_callbacks(
+ callable_mp(EditorNode::get_singleton(), &EditorNode::get_edited_scene),
+ callable_mp(this, &CanvasItemEditor::_select_click_on_item),
+ callable_mp(this, &CanvasItemEditor::_get_selection_callback),
+ callable_mp(viewport_scrollable, &Control::get_transform),
+ callable_mp((Viewport *)EditorNode::get_singleton()->get_scene_root(), &Viewport::get_global_canvas_transform),
+ callable_mp((CanvasItem *)viewport, &CanvasItem::get_local_mouse_position),
+ callable_mp(EditorNode::get_singleton()->get_editor_plugins_over(), &EditorPluginList::forward_gui_input));
+
+ ControlEditorToolbar::get_singleton()->connect("anchors_mode_toggled", callable_mp(*ci_manipulator, &CanvasItemManipulator::set_anchors_mode_enabled));
+
connect("item_lock_status_changed", callable_mp(this, &CanvasItemEditor::_update_lock_and_group_button));
connect("item_group_status_changed", callable_mp(this, &CanvasItemEditor::_update_lock_and_group_button));
@@ -4625,8 +2710,8 @@ void CanvasItemEditor::_notification(int p_what) {
case NOTIFICATION_APPLICATION_FOCUS_OUT:
case NOTIFICATION_WM_WINDOW_FOCUS_OUT: {
- if (drag_type != DRAG_NONE) {
- _commit_drag();
+ if (ci_manipulator->get_drag_type() != CanvasItemManipulator::DRAG_NONE) {
+ ci_manipulator->commit_drag();
}
} break;
@@ -4647,12 +2732,11 @@ void CanvasItemEditor::_notification(int p_what) {
void CanvasItemEditor::_selection_changed() {
_update_lock_and_group_button();
if (!selected_from_canvas) {
- _reset_drag();
+ ci_manipulator->reset_drag();
}
selected_from_canvas = false;
- if (temp_pivot != Vector2(Math::INF, Math::INF)) {
- temp_pivot = Vector2(Math::INF, Math::INF);
+ if (ci_manipulator->reset_temp_pivot()) {
viewport->queue_redraw();
}
@@ -4690,7 +2774,7 @@ void CanvasItemEditor::edit(CanvasItem *p_canvas_item) {
Array selection = editor_selection->get_selected_nodes();
if (selection.size() != 1 || Object::cast_to<Node>(selection[0]) != p_canvas_item) {
- _reset_drag();
+ ci_manipulator->reset_drag();
}
}
@@ -4699,7 +2783,7 @@ void CanvasItemEditor::_update_scrollbars() {
// Move the zoom buttons (top-left corner).
Point2 controls_vb_begin = Point2(5, 5) * EDSCALE;
- controls_vb_begin += (show_rulers) ? Point2(ruler_width_scaled, ruler_width_scaled) : Point2();
+ controls_vb_begin += (ci_manipulator->is_showing_rulers()) ? Point2(ci_manipulator->get_ruler_width(), ci_manipulator->get_ruler_width()) : Point2();
controls_vb->set_begin(controls_vb_begin);
// Move the information panel (bottom-right corner).
@@ -4709,8 +2793,8 @@ void CanvasItemEditor::_update_scrollbars() {
// Move the frame time panel to account for rulers (top-right corner).
float frame_time_panel_end_y = 10 * EDSCALE;
- if (show_rulers) {
- frame_time_panel_end_y += ruler_width_scaled;
+ if (ci_manipulator->is_showing_rulers()) {
+ frame_time_panel_end_y += ci_manipulator->get_ruler_width();
}
frame_time_panel_container->set_begin(Point2(frame_time_panel_container->get_begin().x, frame_time_panel_end_y));
frame_time_panel_container->set_end(Point2(-10 * EDSCALE, frame_time_panel_container->get_end().y));
@@ -4755,15 +2839,18 @@ void CanvasItemEditor::_update_scrollbars() {
h_scroll->set_page(screen_rect.x);
}
+ bool show_rulers = ci_manipulator->is_showing_rulers();
+ float_t ruler_width = ci_manipulator->get_ruler_width();
+
// Move and resize the scrollbars, avoiding overlap.
if (is_layout_rtl()) {
- v_scroll->set_begin(Point2(0, (show_rulers) ? ruler_width_scaled : 0));
+ v_scroll->set_begin(Point2(0, (show_rulers) ? ruler_width : 0));
v_scroll->set_end(Point2(vmin.width, size.height - (h_scroll->is_visible() ? hmin.height : 0)));
} else {
- v_scroll->set_begin(Point2(size.width - vmin.width, (show_rulers) ? ruler_width_scaled : 0));
+ v_scroll->set_begin(Point2(size.width - vmin.width, (show_rulers) ? ruler_width : 0));
v_scroll->set_end(Point2(size.width, size.height - (h_scroll->is_visible() ? hmin.height : 0)));
}
- h_scroll->set_begin(Point2((show_rulers) ? ruler_width_scaled : 0, size.height - hmin.height));
+ h_scroll->set_begin(Point2((show_rulers) ? ruler_width : 0, size.height - hmin.height));
h_scroll->set_end(Point2(size.width - (v_scroll->is_visible() ? vmin.width : 0), size.height));
// Calculate scrollable area.
@@ -4781,6 +2868,7 @@ void CanvasItemEditor::_update_scroll(real_t) {
view_offset.x = h_scroll->get_value();
view_offset.y = v_scroll->get_value();
+ ci_manipulator->set_view_offset(view_offset);
viewport->queue_redraw();
}
@@ -4793,6 +2881,7 @@ void CanvasItemEditor::_zoom_on_position(real_t p_zoom, Point2 p_position) {
real_t prev_zoom = zoom;
zoom = p_zoom;
+ ci_manipulator->set_zoom(zoom);
view_offset += p_position / prev_zoom - p_position / zoom;
@@ -4808,6 +2897,7 @@ void CanvasItemEditor::_zoom_on_position(real_t p_zoom, Point2 p_position) {
view_offset = view_offset_int + (view_offset_frac * closest_zoom_factor).round() / closest_zoom_factor;
}
+ ci_manipulator->set_view_offset(view_offset);
zoom_widget->set_zoom(zoom);
update_viewport();
if (auto_resampling_enabled) {
@@ -4828,48 +2918,31 @@ void CanvasItemEditor::_shortcut_zoom_set(real_t p_zoom) {
}
void CanvasItemEditor::_button_toggle_local_space(bool p_status) {
- use_local_space = p_status;
+ ci_manipulator->set_local_space_enabled(p_status);
viewport->queue_redraw();
}
void CanvasItemEditor::_button_toggle_smart_snap(bool p_status) {
- smart_snap_active = p_status;
+ ci_manipulator->set_smart_snap_enabled(p_status);
viewport->queue_redraw();
}
void CanvasItemEditor::_button_toggle_grid_snap(bool p_status) {
- grid_snap_active = p_status;
+ ci_manipulator->set_grid_snap_enabled(p_status);
viewport->queue_redraw();
emit_signal("grid_visibility_changed", is_grid_visible());
}
void CanvasItemEditor::_button_tool_select(int p_index) {
- if (drag_type != DRAG_NONE) {
- _commit_drag();
- }
-
- Button *tb[TOOL_MAX] = { select_button, scene_paint_button, list_select_button, move_button, scale_button, rotate_button, pivot_button, pan_button, ruler_button };
- for (int i = 0; i < TOOL_MAX; i++) {
+ Button *tb[CanvasItemManipulator::TOOL_MAX] = { select_button, scene_paint_button, list_select_button, move_button, scale_button, rotate_button, pivot_button, pan_button, ruler_button };
+ for (int i = 0; i < CanvasItemManipulator::TOOL_MAX; i++) {
tb[i]->set_pressed(i == p_index);
}
- tool = (Tool)p_index;
-
- if (p_index == TOOL_EDIT_PIVOT && Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
- // Special action that places temporary rotation pivot in the middle of the selection.
- List<CanvasItem *> selection = _get_edited_canvas_items();
- if (!selection.is_empty()) {
- Vector2 center;
- for (const CanvasItem *ci : selection) {
- center += ci->get_viewport()->get_popup_base_transform().xform(ci->_edit_get_position());
- }
- temp_pivot = center / selection.size();
- }
- }
+ ci_manipulator->set_tool((CanvasItemManipulator::Tool)p_index);
viewport->queue_redraw();
- _update_cursor();
- emit_signal("canvas_item_tool_changed", tool);
+ emit_signal("canvas_item_tool_changed", ci_manipulator->get_tool());
}
void CanvasItemEditor::_insert_animation_keys(bool p_location, bool p_rotation, bool p_scale, bool p_on_existing) {
@@ -5010,64 +3083,66 @@ void CanvasItemEditor::_popup_callback(int p_op) {
viewport->queue_redraw();
} break;
case SHOW_TRANSFORMATION_GIZMOS: {
- show_transformation_gizmos = !show_transformation_gizmos;
+ bool show_transformation_gizmos = !ci_manipulator->is_showing_transformation_gizmos();
+ ci_manipulator->set_show_transformation_gizmos(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: {
- snap_node_parent = !snap_node_parent;
+ int snap_mode = ci_manipulator->get_snap_mode() ^ CanvasItemManipulator::SNAP_NODE_PARENT;
+ ci_manipulator->set_snap_mode(snap_mode);
int idx = smartsnap_config_popup->get_item_index(SNAP_USE_NODE_PARENT);
- smartsnap_config_popup->set_item_checked(idx, snap_node_parent);
+ smartsnap_config_popup->set_item_checked(idx, snap_mode & CanvasItemManipulator::SNAP_NODE_PARENT);
} break;
case SNAP_USE_NODE_ANCHORS: {
- snap_node_anchors = !snap_node_anchors;
+ int snap_mode = ci_manipulator->get_snap_mode() ^ CanvasItemManipulator::SNAP_NODE_ANCHORS;
int idx = smartsnap_config_popup->get_item_index(SNAP_USE_NODE_ANCHORS);
- smartsnap_config_popup->set_item_checked(idx, snap_node_anchors);
+ smartsnap_config_popup->set_item_checked(idx, snap_mode & CanvasItemManipulator::SNAP_NODE_ANCHORS);
} break;
case SNAP_USE_NODE_SIDES: {
- snap_node_sides = !snap_node_sides;
+ int snap_mode = ci_manipulator->get_snap_mode() ^ CanvasItemManipulator::SNAP_NODE_SIDES;
int idx = smartsnap_config_popup->get_item_index(SNAP_USE_NODE_SIDES);
- smartsnap_config_popup->set_item_checked(idx, snap_node_sides);
+ smartsnap_config_popup->set_item_checked(idx, snap_mode & CanvasItemManipulator::SNAP_NODE_SIDES);
} break;
case SNAP_USE_NODE_CENTER: {
- snap_node_center = !snap_node_center;
+ int snap_mode = ci_manipulator->get_snap_mode() ^ CanvasItemManipulator::SNAP_NODE_CENTER;
int idx = smartsnap_config_popup->get_item_index(SNAP_USE_NODE_CENTER);
- smartsnap_config_popup->set_item_checked(idx, snap_node_center);
+ smartsnap_config_popup->set_item_checked(idx, snap_mode & CanvasItemManipulator::SNAP_NODE_CENTER);
} break;
case SNAP_USE_OTHER_NODES: {
- snap_other_nodes = !snap_other_nodes;
+ int snap_mode = ci_manipulator->get_snap_mode() ^ CanvasItemManipulator::SNAP_OTHER_NODES;
int idx = smartsnap_config_popup->get_item_index(SNAP_USE_OTHER_NODES);
- smartsnap_config_popup->set_item_checked(idx, snap_other_nodes);
+ smartsnap_config_popup->set_item_checked(idx, snap_mode & CanvasItemManipulator::SNAP_OTHER_NODES);
} break;
case SNAP_USE_GUIDES: {
- snap_guides = !snap_guides;
+ int snap_mode = ci_manipulator->get_snap_mode() ^ CanvasItemManipulator::SNAP_GUIDES;
int idx = smartsnap_config_popup->get_item_index(SNAP_USE_GUIDES);
- smartsnap_config_popup->set_item_checked(idx, snap_guides);
+ smartsnap_config_popup->set_item_checked(idx, snap_mode & CanvasItemManipulator::SNAP_GUIDES);
} break;
case SNAP_USE_ROTATION: {
- snap_rotation = !snap_rotation;
+ bool snap_rotation = !ci_manipulator->is_snap_rotation_enabled();
int idx = snap_config_menu->get_popup()->get_item_index(SNAP_USE_ROTATION);
snap_config_menu->get_popup()->set_item_checked(idx, snap_rotation);
} break;
case SNAP_USE_SCALE: {
- snap_scale = !snap_scale;
+ bool snap_scale = !ci_manipulator->is_snap_scale_enabled();
int idx = snap_config_menu->get_popup()->get_item_index(SNAP_USE_SCALE);
snap_config_menu->get_popup()->set_item_checked(idx, snap_scale);
} break;
case SNAP_RELATIVE: {
- snap_relative = !snap_relative;
+ bool snap_relative = !ci_manipulator->is_snap_relative_enabled();
int idx = snap_config_menu->get_popup()->get_item_index(SNAP_RELATIVE);
snap_config_menu->get_popup()->set_item_checked(idx, snap_relative);
viewport->queue_redraw();
} break;
case SNAP_USE_PIXEL: {
- snap_pixel = !snap_pixel;
+ int snap_mode = ci_manipulator->get_snap_mode() ^ CanvasItemManipulator::SNAP_PIXEL;
int idx = snap_config_menu->get_popup()->get_item_index(SNAP_USE_PIXEL);
- snap_config_menu->get_popup()->set_item_checked(idx, snap_pixel);
+ snap_config_menu->get_popup()->set_item_checked(idx, snap_mode & CanvasItemManipulator::SNAP_PIXEL);
} break;
case SNAP_CONFIGURE: {
- static_cast<SnapDialog *>(snap_dialog)->set_fields(grid_offset, grid_step, primary_grid_step, snap_rotation_offset, snap_rotation_step, snap_scale_step);
+ static_cast<SnapDialog *>(snap_dialog)->set_fields(ci_manipulator->get_grid_offset(), ci_manipulator->get_grid_step(), primary_grid_step, ci_manipulator->get_snap_rotation_offset(), ci_manipulator->get_snap_rotation_step(), ci_manipulator->get_snap_scale_step());
snap_dialog->popup_centered(Size2(320, 160) * EDSCALE);
} break;
case SKELETON_SHOW_BONES: {
@@ -5092,13 +3167,15 @@ void CanvasItemEditor::_popup_callback(int p_op) {
viewport->queue_redraw();
} break;
case SHOW_RULERS: {
- show_rulers = !show_rulers;
+ bool show_rulers = !ci_manipulator->is_showing_rulers();
+ ci_manipulator->set_show_rulers(show_rulers);
int idx = view_menu->get_popup()->get_item_index(SHOW_RULERS);
view_menu->get_popup()->set_item_checked(idx, show_rulers);
update_viewport();
} break;
case SHOW_GUIDES: {
- show_guides = !show_guides;
+ bool show_guides = !ci_manipulator->is_showing_guides();
+ ci_manipulator->set_show_guides(show_guides);
int idx = view_menu->get_popup()->get_item_index(SHOW_GUIDES);
view_menu->get_popup()->set_item_checked(idx, show_guides);
viewport->queue_redraw();
@@ -5410,6 +3487,7 @@ void CanvasItemEditor::_focus_selection(int p_op) {
zoom = scale_x < scale_y ? scale_x : scale_y;
zoom *= 0.90;
zoom_widget->set_zoom(zoom);
+ ci_manipulator->set_zoom(zoom);
viewport->queue_redraw(); // Redraw to update the global canvas transform after zoom changes.
callable_mp(this, &CanvasItemEditor::center_at).call_deferred(rect.get_center()); // Defer because the updated transform is needed.
if (auto_resampling_enabled) {
@@ -5420,12 +3498,6 @@ void CanvasItemEditor::_focus_selection(int p_op) {
}
}
-void CanvasItemEditor::_reset_drag() {
- message = "";
- drag_type = DRAG_NONE;
- drag_selection.clear();
-}
-
void CanvasItemEditor::_bind_methods() {
ClassDB::bind_method("_get_editor_data", &CanvasItemEditor::_get_editor_data);
@@ -5446,37 +3518,38 @@ Dictionary CanvasItemEditor::get_state() const {
// Take the editor scale into account.
state["zoom"] = zoom / MAX(1, EDSCALE);
state["ofs"] = view_offset;
- state["grid_offset"] = grid_offset;
- state["grid_step"] = grid_step;
+ state["grid_offset"] = ci_manipulator->get_grid_offset();
+ state["grid_step"] = ci_manipulator->get_grid_step();
state["primary_grid_step"] = primary_grid_step;
- state["snap_rotation_offset"] = snap_rotation_offset;
- state["snap_rotation_step"] = snap_rotation_step;
- state["snap_scale_step"] = snap_scale_step;
- state["smart_snap_active"] = smart_snap_active;
- state["grid_snap_active"] = grid_snap_active;
- state["snap_node_parent"] = snap_node_parent;
- state["snap_node_anchors"] = snap_node_anchors;
- state["snap_node_sides"] = snap_node_sides;
- state["snap_node_center"] = snap_node_center;
- state["snap_other_nodes"] = snap_other_nodes;
- state["snap_guides"] = snap_guides;
+ state["snap_rotation_offset"] = ci_manipulator->get_snap_rotation_offset();
+ state["snap_rotation_step"] = ci_manipulator->get_snap_rotation_step();
+ state["snap_scale_step"] = ci_manipulator->get_snap_rotation_step();
+ state["smart_snap_active"] = ci_manipulator->is_smart_snap_enabled();
+ state["grid_snap_active"] = ci_manipulator->is_grid_snap_enabled();
+ int snap_mode = ci_manipulator->get_snap_mode();
+ state["snap_node_parent"] = snap_mode & CanvasItemManipulator::SNAP_NODE_PARENT;
+ state["snap_node_anchors"] = snap_mode & CanvasItemManipulator::SNAP_NODE_ANCHORS;
+ state["snap_node_sides"] = snap_mode & CanvasItemManipulator::SNAP_NODE_SIDES;
+ state["snap_node_center"] = snap_mode & CanvasItemManipulator::SNAP_NODE_CENTER;
+ state["snap_other_nodes"] = snap_mode & CanvasItemManipulator::SNAP_OTHER_NODES;
+ state["snap_guides"] = snap_mode & CanvasItemManipulator::SNAP_GUIDES;
state["grid_visibility"] = grid_visibility;
state["show_origin"] = show_origin;
state["show_viewport"] = show_viewport;
state["show_information"] = viewport->is_showing_information();
state["show_frame_time"] = viewport->is_showing_frame_time();
- state["show_rulers"] = show_rulers;
- state["show_guides"] = show_guides;
+ state["show_rulers"] = ci_manipulator->is_showing_rulers();
+ state["show_guides"] = ci_manipulator->is_showing_guides();
state["show_helpers"] = show_helpers;
state["show_zoom_control"] = zoom_widget->is_visible();
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;
- state["snap_relative"] = snap_relative;
- state["snap_pixel"] = snap_pixel;
+ state["show_transformation_gizmos"] = ci_manipulator->is_showing_transformation_gizmos();
+ state["snap_rotation"] = ci_manipulator->is_snap_rotation_enabled();
+ state["snap_scale"] = ci_manipulator->is_snap_scale_enabled();
+ state["snap_relative"] = ci_manipulator->is_snap_relative_enabled();
+ state["snap_pixel"] = snap_mode & CanvasItemManipulator::SNAP_PIXEL;
return state;
}
@@ -5488,6 +3561,7 @@ void CanvasItemEditor::set_state(const Dictionary &p_state) {
// and the zoom level will still be the same (relative to the editor scale).
zoom = real_t(p_state["zoom"]) * MAX(1, EDSCALE);
zoom_widget->set_zoom(zoom);
+ ci_manipulator->set_zoom(zoom);
if (auto_resampling_enabled) {
resample_timer->start();
}
@@ -5496,15 +3570,16 @@ void CanvasItemEditor::set_state(const Dictionary &p_state) {
if (state.has("ofs")) {
view_offset = p_state["ofs"];
previous_update_view_offset = view_offset;
+ ci_manipulator->set_view_offset(view_offset);
update_scrollbars = true;
}
if (state.has("grid_offset")) {
- grid_offset = state["grid_offset"];
+ ci_manipulator->set_grid_offset(state["grid_offset"]);
}
if (state.has("grid_step")) {
- grid_step = state["grid_step"];
+ ci_manipulator->set_grid_step(state["grid_step"]);
}
#ifndef DISABLE_DEPRECATED
@@ -5519,61 +3594,67 @@ void CanvasItemEditor::set_state(const Dictionary &p_state) {
}
if (state.has("snap_rotation_step")) {
- snap_rotation_step = state["snap_rotation_step"];
+ ci_manipulator->set_snap_rotation_step(state["snap_rotation_step"]);
}
if (state.has("snap_rotation_offset")) {
- snap_rotation_offset = state["snap_rotation_offset"];
+ ci_manipulator->set_snap_rotation_offset(state["snap_rotation_offset"]);
}
if (state.has("snap_scale_step")) {
- snap_scale_step = state["snap_scale_step"];
+ ci_manipulator->set_snap_scale_step(state["snap_scale_step"]);
}
if (state.has("smart_snap_active")) {
- smart_snap_active = state["smart_snap_active"];
- smart_snap_button->set_pressed(smart_snap_active);
+ ci_manipulator->set_smart_snap_enabled(state["smart_snap_active"]);
+ smart_snap_button->set_pressed(ci_manipulator->is_smart_snap_enabled());
}
if (state.has("grid_snap_active")) {
- grid_snap_active = state["grid_snap_active"];
- grid_snap_button->set_pressed(grid_snap_active);
+ ci_manipulator->set_grid_snap_enabled(state["grid_snap_active"]);
+ grid_snap_button->set_pressed(ci_manipulator->is_grid_snap_enabled());
}
if (state.has("snap_node_parent")) {
- snap_node_parent = state["snap_node_parent"];
+ bool snap_mode = ci_manipulator->get_snap_mode() | (int)state["snap_node_parent"];
+ ci_manipulator->set_snap_mode(snap_mode);
int idx = smartsnap_config_popup->get_item_index(SNAP_USE_NODE_PARENT);
- smartsnap_config_popup->set_item_checked(idx, snap_node_parent);
+ smartsnap_config_popup->set_item_checked(idx, snap_mode);
}
if (state.has("snap_node_anchors")) {
- snap_node_anchors = state["snap_node_anchors"];
+ bool snap_mode = ci_manipulator->get_snap_mode() | (int)state["snap_node_anchors"];
+ ci_manipulator->set_snap_mode(snap_mode);
int idx = smartsnap_config_popup->get_item_index(SNAP_USE_NODE_ANCHORS);
- smartsnap_config_popup->set_item_checked(idx, snap_node_anchors);
+ smartsnap_config_popup->set_item_checked(idx, snap_mode);
}
if (state.has("snap_node_sides")) {
- snap_node_sides = state["snap_node_sides"];
+ bool snap_mode = ci_manipulator->get_snap_mode() | (int)state["snap_node_sides"];
+ ci_manipulator->set_snap_mode(snap_mode);
int idx = smartsnap_config_popup->get_item_index(SNAP_USE_NODE_SIDES);
- smartsnap_config_popup->set_item_checked(idx, snap_node_sides);
+ smartsnap_config_popup->set_item_checked(idx, snap_mode);
}
if (state.has("snap_node_center")) {
- snap_node_center = state["snap_node_center"];
+ bool snap_mode = ci_manipulator->get_snap_mode() | (int)state["snap_node_center"];
+ ci_manipulator->set_snap_mode(snap_mode);
int idx = smartsnap_config_popup->get_item_index(SNAP_USE_NODE_CENTER);
- smartsnap_config_popup->set_item_checked(idx, snap_node_center);
+ smartsnap_config_popup->set_item_checked(idx, snap_mode);
}
if (state.has("snap_other_nodes")) {
- snap_other_nodes = state["snap_other_nodes"];
+ bool snap_mode = ci_manipulator->get_snap_mode() | (int)state["snap_other_nodes"];
+ ci_manipulator->set_snap_mode(snap_mode);
int idx = smartsnap_config_popup->get_item_index(SNAP_USE_OTHER_NODES);
- smartsnap_config_popup->set_item_checked(idx, snap_other_nodes);
+ smartsnap_config_popup->set_item_checked(idx, snap_mode);
}
if (state.has("snap_guides")) {
- snap_guides = state["snap_guides"];
+ bool snap_mode = ci_manipulator->get_snap_mode() | (int)state["snap_guides"];
+ ci_manipulator->set_snap_mode(snap_mode);
int idx = smartsnap_config_popup->get_item_index(SNAP_USE_GUIDES);
- smartsnap_config_popup->set_item_checked(idx, snap_guides);
+ smartsnap_config_popup->set_item_checked(idx, snap_mode);
}
if (state.has("grid_visibility")) {
@@ -5605,16 +3686,16 @@ void CanvasItemEditor::set_state(const Dictionary &p_state) {
}
if (state.has("show_rulers")) {
- show_rulers = state["show_rulers"];
+ ci_manipulator->set_show_rulers(state["show_rulers"]);
int idx = view_menu->get_popup()->get_item_index(SHOW_RULERS);
- view_menu->get_popup()->set_item_checked(idx, show_rulers);
+ view_menu->get_popup()->set_item_checked(idx, ci_manipulator->is_showing_rulers());
update_scrollbars = true;
}
if (state.has("show_guides")) {
- show_guides = state["show_guides"];
+ ci_manipulator->set_show_guides(state["show_guides"]);
int idx = view_menu->get_popup()->get_item_index(SHOW_GUIDES);
- view_menu->get_popup()->set_item_checked(idx, show_guides);
+ view_menu->get_popup()->set_item_checked(idx, ci_manipulator->is_showing_guides());
}
if (state.has("show_helpers")) {
@@ -5642,9 +3723,9 @@ void CanvasItemEditor::set_state(const Dictionary &p_state) {
}
if (state.has("show_transformation_gizmos")) {
- show_transformation_gizmos = state["show_transformation_gizmos"];
+ ci_manipulator->set_show_transformation_gizmos(state["show_transformation_gizmos"]);
int idx = gizmos_menu->get_item_index(SHOW_TRANSFORMATION_GIZMOS);
- gizmos_menu->set_item_checked(idx, show_transformation_gizmos);
+ gizmos_menu->set_item_checked(idx, ci_manipulator->is_showing_transformation_gizmos());
}
if (state.has("show_zoom_control")) {
@@ -5653,27 +3734,31 @@ void CanvasItemEditor::set_state(const Dictionary &p_state) {
}
if (state.has("snap_rotation")) {
- snap_rotation = state["snap_rotation"];
+ bool snap_rotation = state["snap_rotation"];
+ ci_manipulator->set_snap_rotation_enabled(snap_rotation);
int idx = snap_config_menu->get_popup()->get_item_index(SNAP_USE_ROTATION);
snap_config_menu->get_popup()->set_item_checked(idx, snap_rotation);
}
if (state.has("snap_scale")) {
- snap_scale = state["snap_scale"];
+ bool snap_scale = state["snap_scale"];
+ ci_manipulator->set_snap_scale_enabled(snap_scale);
int idx = snap_config_menu->get_popup()->get_item_index(SNAP_USE_SCALE);
snap_config_menu->get_popup()->set_item_checked(idx, snap_scale);
}
if (state.has("snap_relative")) {
- snap_relative = state["snap_relative"];
+ bool snap_relative = state["snap_relative"];
+ ci_manipulator->set_snap_relative_enabled(snap_relative);
int idx = snap_config_menu->get_popup()->get_item_index(SNAP_RELATIVE);
snap_config_menu->get_popup()->set_item_checked(idx, snap_relative);
}
if (state.has("snap_pixel")) {
- snap_pixel = state["snap_pixel"];
+ bool snap_mode = ci_manipulator->get_snap_mode() | (int)state["snap_pixel"];
+ ci_manipulator->set_snap_mode(snap_mode);
int idx = snap_config_menu->get_popup()->get_item_index(SNAP_USE_PIXEL);
- snap_config_menu->get_popup()->set_item_checked(idx, snap_pixel);
+ snap_config_menu->get_popup()->set_item_checked(idx, snap_mode);
}
if (update_scrollbars) {
@@ -5685,17 +3770,19 @@ void CanvasItemEditor::set_state(const Dictionary &p_state) {
void CanvasItemEditor::clear() {
zoom = 1.0 / MAX(1, EDSCALE);
zoom_widget->set_zoom(zoom);
+ ci_manipulator->set_zoom(zoom);
- view_offset = Point2(-150 - ruler_width_scaled, -95 - ruler_width_scaled);
+ view_offset = Point2(-150 - ci_manipulator->get_ruler_width(), -95 - ci_manipulator->get_ruler_width());
previous_update_view_offset = view_offset; // Moves the view a little bit to the left so that (0,0) is visible. The values a relative to a 16/10 screen.
+ ci_manipulator->set_view_offset(view_offset);
_update_scrollbars();
- grid_offset = EditorSettings::get_singleton()->get_project_metadata("2d_editor", "grid_offset", Vector2());
- grid_step = EditorSettings::get_singleton()->get_project_metadata("2d_editor", "grid_step", Vector2(8, 8));
primary_grid_step = EditorSettings::get_singleton()->get_project_metadata("2d_editor", "primary_grid_step", Vector2i(8, 8));
- snap_rotation_step = EditorSettings::get_singleton()->get_project_metadata("2d_editor", "snap_rotation_step", Math::deg_to_rad(15.0));
- snap_rotation_offset = EditorSettings::get_singleton()->get_project_metadata("2d_editor", "snap_rotation_offset", 0.0);
- snap_scale_step = EditorSettings::get_singleton()->get_project_metadata("2d_editor", "snap_scale_step", 0.1);
+ ci_manipulator->set_grid_offset(EditorSettings::get_singleton()->get_project_metadata("2d_editor", "grid_offset", Vector2()));
+ ci_manipulator->set_grid_step(EditorSettings::get_singleton()->get_project_metadata("2d_editor", "grid_step", Vector2(8, 8)));
+ ci_manipulator->set_snap_rotation_step(EditorSettings::get_singleton()->get_project_metadata("2d_editor", "snap_rotation_step", Math::deg_to_rad(15.0)));
+ ci_manipulator->set_snap_rotation_offset(EditorSettings::get_singleton()->get_project_metadata("2d_editor", "snap_rotation_offset", 0.0));
+ ci_manipulator->set_snap_scale_step(EditorSettings::get_singleton()->get_project_metadata("2d_editor", "snap_scale_step", 0.1));
if (auto_resampling_enabled) {
if (resample_timer->is_inside_tree()) {
@@ -5785,6 +3872,7 @@ void CanvasItemEditor::focus_selection() {
void CanvasItemEditor::center_at(const Point2 &p_pos) {
Vector2 offset = viewport->get_size() / 2 - EditorNode::get_singleton()->get_scene_root()->get_global_canvas_transform().xform(p_pos);
view_offset = (view_offset - offset / zoom).round();
+ ci_manipulator->set_view_offset(view_offset);
update_viewport();
}
@@ -5796,9 +3884,6 @@ CanvasItemEditor::CanvasItemEditor() {
set_dock_shortcut(ED_GET_SHORTCUT("editor/editor_2d"));
set_allow_switch_screen(true);
- snap_target[0] = SNAP_TARGET_NONE;
- snap_target[1] = SNAP_TARGET_NONE;
-
editor_selection = EditorNode::get_singleton()->get_editor_selection();
editor_selection->add_editor_plugin(this);
editor_selection->connect("selection_changed", callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw));
@@ -5942,12 +4027,32 @@ CanvasItemEditor::CanvasItemEditor() {
viewport->add_child(info_panel_container);
viewport->add_child(frame_time_panel_container);
+ ci_manipulator.instantiate();
+ 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.
+ // This results in crisper visuals by preventing 2D nodes from being placed at subpixel coordinates.
+ ci_manipulator->set_snap_mode(SNAP_NODE_PARENT | SNAP_NODE_ANCHORS | SNAP_NODE_SIDES | SNAP_NODE_CENTER | SNAP_OTHER_NODES | SNAP_GUIDES | SNAP_PIXEL);
+ ci_manipulator->connect("box_selected", callable_mp(this, &CanvasItemEditor::_box_selected));
+ ci_manipulator->connect("box_selection_updated", callable_mp(this, &CanvasItemEditor::_box_selection_updated));
+ ci_manipulator->connect("clear_selection_requested", callable_mp(this, &CanvasItemEditor::_clear_selection_requested));
+ ci_manipulator->connect("selection_menu_requested", callable_mp(this, &CanvasItemEditor::_selection_menu_requested));
+ ci_manipulator->connect("update_canvas_requested", callable_mp((CanvasItem *)viewport, &CanvasItem::queue_redraw));
+ ci_manipulator->connect("tool_menu_requested", callable_mp(this, &CanvasItemEditor::_tool_menu_requested));
+ ci_manipulator->connect("scene_double_clicked", callable_mp(EditorNode::get_singleton(), &EditorNode::open_scene).bind(false, false, false));
+ ci_manipulator->connect("save_canvas_state_requested", callable_mp(this, &CanvasItemEditor::_save_canvas_state_requested));
+ ci_manipulator->connect("restore_canvas_state_requested", callable_mp(this, &CanvasItemEditor::_restore_canvas_state_requested));
+ ci_manipulator->connect("commit_canvas_state_requested", callable_mp(this, &CanvasItemEditor::_commit_canvas_state_requested));
+ ci_manipulator->connect("commit_guide_meta_requested", callable_mp(this, &CanvasItemEditor::_commit_guide_meta_requested));
+ ci_manipulator->connect("cursor_shape_changed", callable_mp(this, &CanvasItemEditor::_update_cursor));
+ ci_manipulator->connect("warning_emitted", callable_mp(EditorToaster::get_singleton(), &EditorToaster::popup_str).bind(EditorToaster::SEVERITY_WARNING, ""));
+
select_button = memnew(Button);
select_button->set_tooltip_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
select_button->set_theme_type_variation(SceneStringName(FlatButton));
input_hbox->add_child(select_button);
select_button->set_toggle_mode(true);
- select_button->connect(SceneStringName(pressed), callable_mp(this, &CanvasItemEditor::_button_tool_select).bind(TOOL_SELECT));
+ select_button->connect(SceneStringName(pressed), callable_mp(this, &CanvasItemEditor::_button_tool_select).bind(CanvasItemManipulator::TOOL_SELECT));
select_button->set_pressed(true);
select_button->set_shortcut(ED_SHORTCUT("canvas_item_editor/select_mode", TTRC("Select Mode"), Key::Q, true));
select_button->set_shortcut_context(this);
@@ -5957,7 +4062,7 @@ CanvasItemEditor::CanvasItemEditor() {
scene_paint_button->set_theme_type_variation(SceneStringName(FlatButton));
input_hbox->add_child(scene_paint_button);
scene_paint_button->set_toggle_mode(true);
- scene_paint_button->connect(SceneStringName(pressed), callable_mp(this, &CanvasItemEditor::_button_tool_select).bind(TOOL_SCENE_PAINT));
+ scene_paint_button->connect(SceneStringName(pressed), callable_mp(this, &CanvasItemEditor::_button_tool_select).bind(CanvasItemManipulator::TOOL_SCENE_PAINT));
scene_paint_button->set_shortcut(ED_SHORTCUT("canvas_item_editor/scene_paint_mode", TTRC("Scene Paint Mode"), Key::B, true));
scene_paint_button->set_shortcut_context(this);
scene_paint_button->set_accessibility_name(TTRC("Scene Paint Mode"));
@@ -5970,7 +4075,7 @@ CanvasItemEditor::CanvasItemEditor() {
move_button->set_theme_type_variation(SceneStringName(FlatButton));
input_hbox->add_child(move_button);
move_button->set_toggle_mode(true);
- move_button->connect(SceneStringName(pressed), callable_mp(this, &CanvasItemEditor::_button_tool_select).bind(TOOL_MOVE));
+ move_button->connect(SceneStringName(pressed), callable_mp(this, &CanvasItemEditor::_button_tool_select).bind(CanvasItemManipulator::TOOL_MOVE));
move_button->set_shortcut(ED_SHORTCUT("canvas_item_editor/move_mode", TTRC("Move Mode"), Key::W, true));
move_button->set_shortcut_context(this);
move_button->set_accessibility_name(TTRC("Move Mode"));
@@ -5979,7 +4084,7 @@ CanvasItemEditor::CanvasItemEditor() {
rotate_button->set_theme_type_variation(SceneStringName(FlatButton));
input_hbox->add_child(rotate_button);
rotate_button->set_toggle_mode(true);
- rotate_button->connect(SceneStringName(pressed), callable_mp(this, &CanvasItemEditor::_button_tool_select).bind(TOOL_ROTATE));
+ rotate_button->connect(SceneStringName(pressed), callable_mp(this, &CanvasItemEditor::_button_tool_select).bind(CanvasItemManipulator::TOOL_ROTATE));
rotate_button->set_shortcut(ED_SHORTCUT("canvas_item_editor/rotate_mode", TTRC("Rotate Mode"), Key::E, true));
rotate_button->set_shortcut_context(this);
rotate_button->set_accessibility_name(TTRC("Rotate Mode"));
@@ -5988,7 +4093,7 @@ CanvasItemEditor::CanvasItemEditor() {
scale_button->set_theme_type_variation(SceneStringName(FlatButton));
input_hbox->add_child(scale_button);
scale_button->set_toggle_mode(true);
- scale_button->connect(SceneStringName(pressed), callable_mp(this, &CanvasItemEditor::_button_tool_select).bind(TOOL_SCALE));
+ scale_button->connect(SceneStringName(pressed), callable_mp(this, &CanvasItemEditor::_button_tool_select).bind(CanvasItemManipulator::TOOL_SCALE));
scale_button->set_shortcut(ED_SHORTCUT("canvas_item_editor/scale_mode", TTRC("Scale Mode"), Key::R, true));
scale_button->set_shortcut_context(this);
scale_button->set_accessibility_name(TTRC("Scale Mode"));
@@ -6001,7 +4106,7 @@ CanvasItemEditor::CanvasItemEditor() {
list_select_button->set_theme_type_variation(SceneStringName(FlatButton));
input_hbox->add_child(list_select_button);
list_select_button->set_toggle_mode(true);
- list_select_button->connect(SceneStringName(pressed), callable_mp(this, &CanvasItemEditor::_button_tool_select).bind(TOOL_LIST_SELECT));
+ list_select_button->connect(SceneStringName(pressed), callable_mp(this, &CanvasItemEditor::_button_tool_select).bind(CanvasItemManipulator::TOOL_LIST_SELECT));
list_select_button->set_accessibility_name(TTRC("List Selectable Nodes"));
pivot_button = memnew(Button);
@@ -6009,14 +4114,14 @@ CanvasItemEditor::CanvasItemEditor() {
pivot_button->set_theme_type_variation(SceneStringName(FlatButton));
input_hbox->add_child(pivot_button);
pivot_button->set_toggle_mode(true);
- pivot_button->connect(SceneStringName(pressed), callable_mp(this, &CanvasItemEditor::_button_tool_select).bind(TOOL_EDIT_PIVOT));
+ pivot_button->connect(SceneStringName(pressed), callable_mp(this, &CanvasItemEditor::_button_tool_select).bind(CanvasItemManipulator::TOOL_EDIT_PIVOT));
pivot_button->set_accessibility_name(TTRC("Change Pivot"));
pan_button = memnew(Button);
pan_button->set_theme_type_variation(SceneStringName(FlatButton));
input_hbox->add_child(pan_button);
pan_button->set_toggle_mode(true);
- pan_button->connect(SceneStringName(pressed), callable_mp(this, &CanvasItemEditor::_button_tool_select).bind(TOOL_PAN));
+ pan_button->connect(SceneStringName(pressed), callable_mp(this, &CanvasItemEditor::_button_tool_select).bind(CanvasItemManipulator::TOOL_PAN));
pan_button->set_shortcut(ED_SHORTCUT("canvas_item_editor/pan_mode", TTRC("Pan Mode"), Key::G));
pan_button->set_shortcut_context(this);
pan_button->set_accessibility_name(TTRC("Pan View"));
@@ -6025,7 +4130,7 @@ CanvasItemEditor::CanvasItemEditor() {
ruler_button->set_theme_type_variation(SceneStringName(FlatButton));
input_hbox->add_child(ruler_button);
ruler_button->set_toggle_mode(true);
- ruler_button->connect(SceneStringName(pressed), callable_mp(this, &CanvasItemEditor::_button_tool_select).bind(TOOL_RULER));
+ ruler_button->connect(SceneStringName(pressed), callable_mp(this, &CanvasItemEditor::_button_tool_select).bind(CanvasItemManipulator::TOOL_RULER));
ruler_button->set_shortcut(ED_SHORTCUT("canvas_item_editor/ruler_mode", TTRC("Ruler Mode"), Key::M));
ruler_button->set_shortcut_context(this);
ruler_button->set_accessibility_name(TTRC("Ruler Mode"));
@@ -6424,10 +4529,6 @@ void CanvasItemEditorViewport::_on_change_type_confirmed() {
texture_node_type_selector->hide();
}
-void CanvasItemEditorViewport::_on_change_type_closed() {
- _remove_preview();
-}
-
void CanvasItemEditorViewport::_create_preview(const Vector<String> &files) const {
bool add_preview = false;
@@ -6623,7 +4724,7 @@ bool CanvasItemEditorViewport::_create_instance(Node *p_parent, const String &p_
CanvasItem *instance_ci = Object::cast_to<CanvasItem>(instantiated_scene);
if (instance_ci) {
Vector2 target_pos = canvas_item_editor->get_canvas_transform().affine_inverse().xform(p_point);
- target_pos = canvas_item_editor->snap_point(target_pos);
+ target_pos = canvas_item_editor->get_canvas_item_manipulator()->snap_point(target_pos);
CanvasItem *parent_ci = Object::cast_to<CanvasItem>(p_parent);
if (parent_ci) {
@@ -6688,7 +4789,7 @@ void CanvasItemEditorViewport::_create_script_node(Node *p_parent, const String
Point2 target_position = xform.affine_inverse().xform(p_point);
// There's nothing to be used as source position, so snapping will work as absolute if enabled.
- target_position = canvas_item_editor->snap_point(target_position);
+ target_position = canvas_item_editor->get_canvas_item_manipulator()->snap_point(target_position);
CanvasItem *parent_ci = Object::cast_to<CanvasItem>(p_parent);
Point2 local_target_pos = parent_ci ? parent_ci->get_global_transform().affine_inverse().xform(target_position) : target_position;
@@ -7180,7 +5281,7 @@ CanvasItemEditorViewport::CanvasItemEditorViewport(CanvasItemEditor *p_canvas_it
texture_node_type_selector = memnew(AcceptDialog);
EditorNode::get_singleton()->get_gui_base()->add_child(texture_node_type_selector);
texture_node_type_selector->connect(SceneStringName(confirmed), callable_mp(this, &CanvasItemEditorViewport::_on_change_type_confirmed));
- texture_node_type_selector->connect("canceled", callable_mp(this, &CanvasItemEditorViewport::_on_change_type_closed));
+ texture_node_type_selector->connect("canceled", callable_mp(this, &CanvasItemEditorViewport::_remove_preview));
VBoxContainer *vbc = memnew(VBoxContainer);
texture_node_type_selector->add_child(vbc);
diff --git a/editor/scene/canvas_item_editor_plugin.h b/editor/scene/canvas_item_editor_plugin.h
index f38cafce2c9..394afdc04dc 100644
--- a/editor/scene/canvas_item_editor_plugin.h
+++ b/editor/scene/canvas_item_editor_plugin.h
@@ -32,6 +32,7 @@
#include "editor/docks/editor_dock.h"
#include "editor/plugins/editor_plugin.h"
+#include "scene/debugger/canvas_item_manipulator.h"
#include "scene/gui/box_container.h"
class AcceptDialog;
@@ -79,19 +80,6 @@ class CanvasItemEditor : public EditorDock {
GDCLASS(CanvasItemEditor, EditorDock);
public:
- enum Tool {
- TOOL_SELECT,
- TOOL_SCENE_PAINT,
- TOOL_LIST_SELECT,
- TOOL_MOVE,
- TOOL_SCALE,
- TOOL_ROTATE,
- TOOL_EDIT_PIVOT,
- TOOL_PAN,
- TOOL_RULER,
- TOOL_MAX
- };
-
enum AddNodeOption {
ADD_NODE,
ADD_INSTANCE,
@@ -100,17 +88,6 @@ public:
};
private:
- enum SnapTarget {
- SNAP_TARGET_NONE = 0,
- SNAP_TARGET_PARENT,
- SNAP_TARGET_SELF_ANCHORS,
- SNAP_TARGET_SELF,
- SNAP_TARGET_OTHER_NODE,
- SNAP_TARGET_GUIDE,
- SNAP_TARGET_GRID,
- SNAP_TARGET_PIXEL
- };
-
enum MenuOption {
SNAP_USE,
SNAP_USE_NODE_PARENT,
@@ -157,38 +134,6 @@ private:
AUTO_RESAMPLE_CANVAS_ITEMS,
};
- enum DragType {
- DRAG_NONE,
- DRAG_BOX_SELECTION,
- DRAG_LEFT,
- DRAG_TOP_LEFT,
- DRAG_TOP,
- DRAG_TOP_RIGHT,
- DRAG_RIGHT,
- DRAG_BOTTOM_RIGHT,
- DRAG_BOTTOM,
- DRAG_BOTTOM_LEFT,
- DRAG_ANCHOR_TOP_LEFT,
- DRAG_ANCHOR_TOP_RIGHT,
- DRAG_ANCHOR_BOTTOM_RIGHT,
- DRAG_ANCHOR_BOTTOM_LEFT,
- DRAG_ANCHOR_ALL,
- DRAG_QUEUED,
- DRAG_MOVE,
- DRAG_MOVE_X,
- DRAG_MOVE_Y,
- DRAG_SCALE_X,
- DRAG_SCALE_Y,
- DRAG_SCALE_BOTH,
- DRAG_ROTATE,
- DRAG_PIVOT,
- DRAG_TEMP_PIVOT,
- DRAG_V_GUIDE,
- DRAG_H_GUIDE,
- DRAG_DOUBLE_GUIDE,
- DRAG_KEY_MOVE
- };
-
enum GridVisibility {
GRID_VISIBILITY_SHOW,
GRID_VISIBILITY_SHOW_WHEN_SNAPPING,
@@ -201,11 +146,8 @@ private:
SCALE,
};
- const String locked_transform_warning = TTRC("All selected CanvasItems are either invisible or locked in some way and can't be transformed.");
-
bool selection_menu_additive_selection = false;
- Tool tool = TOOL_SELECT;
CanvasItemEditorViewport *viewport = nullptr;
Control *viewport_scrollable = nullptr;
@@ -222,15 +164,12 @@ private:
Transform2D transform;
GridVisibility grid_visibility = GRID_VISIBILITY_SHOW_WHEN_SNAPPING;
- bool show_rulers = true;
- bool show_guides = true;
bool show_origin = true;
bool show_viewport = true;
bool show_helpers = false;
bool show_position_gizmos = true;
bool show_lock_gizmos = true;
bool show_group_gizmos = true;
- bool show_transformation_gizmos = true;
real_t zoom = 1.0;
Point2 view_offset;
@@ -243,65 +182,28 @@ private:
bool selected_from_canvas = false;
bool had_visible_selection = false;
- // Defaults are defined in clear().
- Point2 grid_offset;
- Point2 grid_step;
+ // Default is defined in `clear()`.
Vector2i primary_grid_step;
- int grid_step_multiplier = 0;
Color selection_rectangle_color;
Color locked_selection_rectangle_color;
- real_t snap_rotation_step = 0.0;
- real_t snap_rotation_offset = 0.0;
- real_t snap_scale_step = 0.0;
- bool use_local_space = true;
- bool smart_snap_active = false;
- bool grid_snap_active = false;
-
- bool snap_node_parent = true;
- bool snap_node_anchors = true;
- bool snap_node_sides = true;
- bool snap_node_center = true;
- bool snap_other_nodes = true;
- bool snap_guides = true;
- bool snap_rotation = false;
- bool snap_scale = false;
- bool snap_relative = false;
- // Enable pixel snapping even if pixel snap rendering is disabled in the Project Settings.
- // This results in crisper visuals by preventing 2D nodes from being placed at subpixel coordinates.
- bool snap_pixel = true;
-
bool key_pos = true;
bool key_rot = true;
bool key_scale = false;
bool pan_pressed = false;
- Vector2 temp_pivot = Vector2(Math::INF, Math::INF);
- bool ruler_tool_active = false;
- Point2 ruler_tool_origin;
- real_t ruler_width_scaled = 16.0;
int ruler_font_size = 8;
Point2 node_create_position;
- real_t grab_distance = 0.0;
bool simple_panning = false;
MenuOption last_option = SNAP_USE;
-public:
- struct SelectResult {
- CanvasItem *item = nullptr;
- real_t z_index = 0;
- bool has_z = true;
- _FORCE_INLINE_ bool operator<(const SelectResult &p_rr) const {
- return has_z && p_rr.has_z ? p_rr.z_index < z_index : p_rr.has_z;
- }
- };
-
private:
- Vector<SelectResult> selection_results;
- Vector<SelectResult> selection_results_menu;
+ Ref<CanvasItemManipulator> ci_manipulator;
+
+ Array selection_results_menu;
struct _HoverResult {
Point2 position;
@@ -386,21 +288,10 @@ private:
Control *top_ruler = nullptr;
Control *left_ruler = nullptr;
- Point2 drag_start_origin;
- DragType drag_type = DRAG_NONE;
- Point2 drag_from;
- Point2 drag_to;
- Point2 drag_rotation_center;
- List<CanvasItem *> drag_selection;
- int dragged_guide_index = -1;
- Point2 dragged_guide_pos;
- bool is_hovering_h_guide = false;
- bool is_hovering_v_guide = false;
-
bool updating_value_dialog = false;
Transform2D original_transform;
- Point2 box_selecting_to;
+ Rect2 box_selection_rect;
CursorShape cursor_shape_override = CURSOR_ARROW;
Ref<StyleBoxTexture> select_sb;
@@ -419,21 +310,26 @@ private:
void _pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event);
void _zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event);
- bool _is_node_locked(const Node *p_node) const;
- bool _is_node_movable(const Node *p_node, bool p_popup_warning = false);
- void _get_canvas_items_at_pos(const Point2 &p_pos, Vector<SelectResult> &r_items, bool p_allow_locked = false);
- void _find_canvas_items_in_rect(const Rect2 &p_rect, Node *p_node, List<CanvasItem *> *r_items, const Transform2D &p_parent_xform = Transform2D(), const Transform2D &p_canvas_xform = Transform2D());
Dictionary _get_context_data();
- bool _select_click_on_item(CanvasItem *item, Point2 p_click_pos, bool p_append);
+ bool _select_click_on_item(const Variant &p_node, bool p_append);
ConfirmationDialog *snap_dialog = nullptr;
CanvasItem *ref_item = nullptr;
- void _save_canvas_item_state(const List<CanvasItem *> &p_canvas_items, bool save_bones = false);
- void _restore_canvas_item_state(const List<CanvasItem *> &p_canvas_items, bool restore_bones = false);
- void _commit_canvas_item_state(const List<CanvasItem *> &p_canvas_items, const String &action_name, bool commit_bones = false);
+ bool _get_selection_callback(Array r_selection);
+ void _box_selection_updated(const Rect2 &p_rect);
+
+ void _box_selected(const Array &p_selection);
+ void _clear_selection_requested(bool p_selected_from_canvas);
+ void _selection_menu_requested(const Array &p_selection, const Point2 &p_pos, bool p_append);
+ void _tool_menu_requested(const Point2 &p_pos);
+ void _commit_guide_meta_requested(const String &p_msg, const Dictionary &p_meta);
+
+ void _save_canvas_state_requested(const Array &p_selection, bool p_save_bones);
+ void _restore_canvas_state_requested(const Array &p_selection, bool p_restore_bones);
+ void _commit_canvas_state_requested(const Array &p_selection, const String &p_message, bool p_commit_bones);
Vector2 _anchor_to_position(const Control *p_control, Vector2 anchor);
Vector2 _position_to_anchor(const Control *p_control, Vector2 position);
@@ -503,51 +399,18 @@ private:
void _draw_viewport();
- bool _gui_input_anchors(const Ref<InputEvent> &p_event);
- bool _gui_input_move(const Ref<InputEvent> &p_event);
- bool _gui_input_open_scene_on_double_click(const Ref<InputEvent> &p_event);
- bool _gui_input_scale(const Ref<InputEvent> &p_event);
- bool _gui_input_pivot(const Ref<InputEvent> &p_event);
- bool _gui_input_resize(const Ref<InputEvent> &p_event);
- bool _gui_input_rotate(const Ref<InputEvent> &p_event);
- bool _gui_input_select(const Ref<InputEvent> &p_event);
- bool _gui_input_ruler_tool(const Ref<InputEvent> &p_event);
bool _gui_input_zoom_or_pan(const Ref<InputEvent> &p_event, bool p_already_accepted);
- bool _gui_input_rulers_and_guides(const Ref<InputEvent> &p_event);
bool _gui_input_hover(const Ref<InputEvent> &p_event);
- void _commit_drag();
-
void _gui_input_viewport(const Ref<InputEvent> &p_event);
void _update_cursor();
void _update_lock_and_group_button();
void _selection_changed();
void _focus_selection(int p_op);
- void _reset_drag();
void _project_settings_changed();
- SnapTarget snap_target[2];
- Transform2D snap_transform;
- void _snap_if_closer_float(
- const real_t p_value,
- real_t &r_current_snap, SnapTarget &r_current_snap_target,
- const real_t p_target_value, const SnapTarget p_snap_target,
- const real_t p_radius = 10.0);
- void _snap_if_closer_point(
- Point2 p_value,
- Point2 &r_current_snap, SnapTarget (&r_current_snap_target)[2],
- Point2 p_target_value, const SnapTarget p_snap_target,
- const real_t rotation = 0.0,
- const real_t p_radius = 10.0);
- void _snap_other_nodes(
- const Point2 p_value,
- const Transform2D p_transform_to_snap,
- Point2 &r_current_snap, SnapTarget (&r_current_snap_target)[2],
- const SnapTarget p_snap_target, List<const CanvasItem *> p_exceptions,
- const Node *p_current);
-
VBoxContainer *controls_vb = nullptr;
Control *info_panel_container = nullptr;
Control *frame_time_panel_container = nullptr;
@@ -592,9 +455,6 @@ public:
String message;
- Point2 snap_point(Point2 p_target, unsigned int p_modes = SNAP_DEFAULT, unsigned int p_forced_modes = 0, const CanvasItem *p_self_canvas_item = nullptr, const List<CanvasItem *> &p_other_nodes_exceptions = List<CanvasItem *>());
- real_t snap_angle(real_t p_target, real_t p_start = 0) const;
-
Transform2D get_canvas_transform() const { return transform; }
static CanvasItemEditor *get_singleton() { return singleton; }
@@ -619,16 +479,13 @@ public:
Control *get_info_panel_container() { return info_panel_container; }
Control *get_frame_time_panel_container() { return frame_time_panel_container; }
- void find_canvas_items_at_pos(const Point2 &p_pos, Node *p_node, Vector<SelectResult> &r_items, const Transform2D &p_parent_xform = Transform2D(), const Transform2D &p_canvas_xform = Transform2D());
-
void update_viewport();
- Tool get_current_tool() { return tool; }
- void set_current_tool(Tool p_tool);
+ Ref<CanvasItemManipulator> get_canvas_item_manipulator() { return ci_manipulator; }
+ CanvasItemManipulator::Tool get_current_tool() { return ci_manipulator->get_tool(); }
+ void set_current_tool(CanvasItemManipulator::Tool p_tool);
bool is_grid_visible() const;
- Vector2 get_grid_step() const { return grid_step; }
- Vector2 get_grid_offset() const { return grid_offset; }
double get_zoom() const { return zoom; }
Point2 get_view_offset() const { return view_offset; }
@@ -714,7 +571,6 @@ class CanvasItemEditorViewport : public Control {
void _on_mouse_exit();
void _on_select_texture_node_type(Object *selected);
void _on_change_type_confirmed();
- void _on_change_type_closed();
void _create_preview(const Vector<String> &files) const;
void _remove_preview();
diff --git a/editor/scene/gui/control_editor_plugin.cpp b/editor/scene/gui/control_editor_plugin.cpp
index 1df8ac38b8d..f16e09845ed 100644
--- a/editor/scene/gui/control_editor_plugin.cpp
+++ b/editor/scene/gui/control_editor_plugin.cpp
@@ -904,8 +904,8 @@ void ControlEditorToolbar::_anchors_to_current_ratio() {
for (Node *E : selection) {
Control *control = Object::cast_to<Control>(E);
if (control) {
- Point2 top_left_anchor = _position_to_anchor(control, Point2());
- Point2 bottom_right_anchor = _position_to_anchor(control, control->get_size());
+ Point2 top_left_anchor = CanvasItemManipulator::position_to_anchor(control, Point2());
+ Point2 bottom_right_anchor = CanvasItemManipulator::position_to_anchor(control, control->get_size());
undo_redo->add_do_method(control, "set_anchor", SIDE_LEFT, top_left_anchor.x, false, true);
undo_redo->add_do_method(control, "set_anchor", SIDE_RIGHT, bottom_right_anchor.x, false, true);
undo_redo->add_do_method(control, "set_anchor", SIDE_TOP, top_left_anchor.y, false, true);
@@ -944,6 +944,7 @@ void ControlEditorToolbar::_anchor_mode_toggled(bool p_status) {
anchors_mode = p_status;
CanvasItemEditor::get_singleton()->update_viewport();
+ emit_signal("anchors_mode_toggled", anchors_mode);
}
void ControlEditorToolbar::_container_flags_selected(int p_flags, bool p_vertical) {
@@ -1043,30 +1044,11 @@ void ControlEditorToolbar::_maximize_flag_toggled(bool p_maximize, bool p_vertic
undo_redo->commit_action();
}
-Vector2 ControlEditorToolbar::_position_to_anchor(const Control *p_control, Vector2 position) {
- ERR_FAIL_NULL_V(p_control, Vector2());
-
- Rect2 parent_rect = p_control->get_parent_anchorable_rect();
-
- Vector2 output;
- if (p_control->is_layout_rtl()) {
- output.x = (parent_rect.size.x == 0) ? 0.0 : (parent_rect.size.x - p_control->get_transform().xform(position).x - parent_rect.position.x) / parent_rect.size.x;
- } else {
- output.x = (parent_rect.size.x == 0) ? 0.0 : (p_control->get_transform().xform(position).x - parent_rect.position.x) / parent_rect.size.x;
- }
- output.y = (parent_rect.size.y == 0) ? 0.0 : (p_control->get_transform().xform(position).y - parent_rect.position.y) / parent_rect.size.y;
- return output;
-}
-
-bool ControlEditorToolbar::_is_node_locked(const Node *p_node) {
- return p_node->get_meta("_edit_lock_", false);
-}
-
List<Control *> ControlEditorToolbar::_get_edited_controls() {
List<Control *> selection;
for (const KeyValue<ObjectID, Object *> &E : editor_selection->get_selection()) {
Control *control = ObjectDB::get_instance<Control>(E.key);
- if (control && control->is_visible_in_tree() && control->get_viewport() == EditorNode::get_singleton()->get_scene_root() && !_is_node_locked(control)) {
+ if (control && control->is_visible_in_tree() && control->get_viewport() == EditorNode::get_singleton()->get_scene_root() && !CanvasItemManipulator::is_node_locked(control)) {
selection.push_back(control);
}
}
@@ -1310,6 +1292,10 @@ void ControlEditorToolbar::_notification(int p_what) {
}
}
+void ControlEditorToolbar::_bind_methods() {
+ ADD_SIGNAL(MethodInfo("anchors_mode_toggled", PropertyInfo(Variant::BOOL, "enabled")));
+}
+
ControlEditorToolbar::ControlEditorToolbar() {
// Anchor and offset tools.
anchors_button = memnew(ControlEditorPopupButton);
diff --git a/editor/scene/gui/control_editor_plugin.h b/editor/scene/gui/control_editor_plugin.h
index b49353a437d..a52b597e479 100644
--- a/editor/scene/gui/control_editor_plugin.h
+++ b/editor/scene/gui/control_editor_plugin.h
@@ -241,14 +241,14 @@ class ControlEditorToolbar : public HBoxContainer {
void _update_anchor_selection_ui(bool p_pressed);
void _update_container_sizing_selection_ui(bool p_pressed);
- Vector2 _position_to_anchor(const Control *p_control, Vector2 position);
- bool _is_node_locked(const Node *p_node);
List<Control *> _get_edited_controls();
void _selection_changed();
protected:
void _notification(int p_notification);
+ static void _bind_methods();
+
static ControlEditorToolbar *singleton;
public:
diff --git a/modules/navigation_2d/editor/navigation_link_2d_editor_plugin.cpp b/modules/navigation_2d/editor/navigation_link_2d_editor_plugin.cpp
index 63b79981db4..ebae480432e 100644
--- a/modules/navigation_2d/editor/navigation_link_2d_editor_plugin.cpp
+++ b/modules/navigation_2d/editor/navigation_link_2d_editor_plugin.cpp
@@ -123,7 +123,7 @@ bool NavigationLink2DEditor::forward_canvas_gui_input(const Ref<InputEvent> &p_e
Ref<InputEventMouseMotion> mm = p_event;
if (mm.is_valid()) {
- Vector2 point = canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(mm->get_position()));
+ Vector2 point = canvas_item_editor->get_canvas_item_manipulator()->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(mm->get_position()));
point = node->get_screen_transform().affine_inverse().xform(point);
if (start_grabbed) {
diff --git a/modules/tilemap/editor/tile_map_layer_editor.cpp b/modules/tilemap/editor/tile_map_layer_editor.cpp
index ab4b8baf95e..53ed75c8914 100644
--- a/modules/tilemap/editor/tile_map_layer_editor.cpp
+++ b/modules/tilemap/editor/tile_map_layer_editor.cpp
@@ -121,7 +121,7 @@ void TileMapLayerEditorTilesPlugin::_update_toolbar() {
random_tile_toggle->set_visible(!using_select);
bucket_contiguous_checkbox->set_visible(!using_select && pressed_tool == bucket_tool_button);
scatter_controls_container->set_visible(!using_select && random_tile_toggle->is_pressed());
- CanvasItemEditor::get_singleton()->set_current_tool(CanvasItemEditor::TOOL_SELECT);
+ CanvasItemEditor::get_singleton()->set_current_tool(CanvasItemManipulator::TOOL_SELECT);
}
void TileMapLayerEditorTilesPlugin::_update_transform_buttons() {
@@ -556,7 +556,7 @@ bool TileMapLayerEditorTilesPlugin::forward_canvas_gui_input(const Ref<InputEven
return false;
}
- if (CanvasItemEditor::get_singleton()->get_current_tool() != CanvasItemEditor::TOOL_SELECT) {
+ if (CanvasItemEditor::get_singleton()->get_current_tool() != CanvasItemManipulator::TOOL_SELECT) {
_stop_dragging();
return false;
}
@@ -840,7 +840,7 @@ void TileMapLayerEditorTilesPlugin::forward_canvas_draw_over_viewport(Control *p
}
// Handle the preview of the tiles to be placed.
- if ((tiles_bottom_panel->is_visible_in_tree() || patterns_mc->is_visible_in_tree()) && CanvasItemEditor::get_singleton()->get_current_tool() == CanvasItemEditor::TOOL_SELECT && has_mouse) { // Only if the tilemap editor is opened and the viewport is hovered.
+ if ((tiles_bottom_panel->is_visible_in_tree() || patterns_mc->is_visible_in_tree()) && CanvasItemEditor::get_singleton()->get_current_tool() == CanvasItemManipulator::TOOL_SELECT && has_mouse) { // Only if the tilemap editor is opened and the viewport is hovered.
HashMap<Vector2i, TileMapCell> preview;
Rect2i drawn_grid_rect;
@@ -3035,7 +3035,7 @@ bool TileMapLayerEditorTerrainsPlugin::forward_canvas_gui_input(const Ref<InputE
return false;
}
- if (CanvasItemEditor::get_singleton()->get_current_tool() != CanvasItemEditor::TOOL_SELECT) {
+ if (CanvasItemEditor::get_singleton()->get_current_tool() != CanvasItemManipulator::TOOL_SELECT) {
return false;
}
@@ -3745,8 +3745,8 @@ void TileMapLayerEditor::_notification(int p_what) {
}
if (is_visible()) {
// Fix: Don't change the tool if we are in scene paint mode.
- if (CanvasItemEditor::get_singleton()->get_current_tool() != CanvasItemEditor::TOOL_SCENE_PAINT) {
- CanvasItemEditor::get_singleton()->set_current_tool(CanvasItemEditor::TOOL_SELECT);
+ if (CanvasItemEditor::get_singleton()->get_current_tool() != CanvasItemManipulator::TOOL_SCENE_PAINT) {
+ CanvasItemEditor::get_singleton()->set_current_tool(CanvasItemManipulator::TOOL_SELECT);
}
}
} break;
diff --git a/scene/2d/animated_sprite_2d.cpp b/scene/2d/animated_sprite_2d.cpp
index 500c6756e62..971e1c50771 100644
--- a/scene/2d/animated_sprite_2d.cpp
+++ b/scene/2d/animated_sprite_2d.cpp
@@ -36,7 +36,7 @@
#include "scene/main/viewport.h"
#include "servers/display/accessibility_server.h"
-#ifdef TOOLS_ENABLED
+#ifdef DEBUG_ENABLED
Dictionary AnimatedSprite2D::_edit_get_state() const {
Dictionary state = Node2D::_edit_get_state();
state["offset"] = offset;
@@ -60,9 +60,7 @@ Point2 AnimatedSprite2D::_edit_get_pivot() const {
bool AnimatedSprite2D::_edit_use_pivot() const {
return true;
}
-#endif // TOOLS_ENABLED
-#ifdef DEBUG_ENABLED
Rect2 AnimatedSprite2D::_edit_get_rect() const {
return _get_rect();
}
diff --git a/scene/2d/animated_sprite_2d.h b/scene/2d/animated_sprite_2d.h
index c390ab35b82..56d33d58dd0 100644
--- a/scene/2d/animated_sprite_2d.h
+++ b/scene/2d/animated_sprite_2d.h
@@ -71,16 +71,14 @@ protected:
void _validate_property(PropertyInfo &p_property) const;
public:
-#ifdef TOOLS_ENABLED
+#ifdef DEBUG_ENABLED
virtual Dictionary _edit_get_state() const override;
virtual void _edit_set_state(const Dictionary &p_state) override;
virtual void _edit_set_pivot(const Point2 &p_pivot) override;
virtual Point2 _edit_get_pivot() const override;
virtual bool _edit_use_pivot() const override;
-#endif // TOOLS_ENABLED
-#ifdef DEBUG_ENABLED
virtual Rect2 _edit_get_rect() const override;
virtual bool _edit_use_rect() const override;
#endif // DEBUG_ENABLED
diff --git a/scene/2d/light_2d.cpp b/scene/2d/light_2d.cpp
index 156bced6aed..53efee49a5c 100644
--- a/scene/2d/light_2d.cpp
+++ b/scene/2d/light_2d.cpp
@@ -345,7 +345,7 @@ Light2D::~Light2D() {
//////////////////////////////
-#ifdef TOOLS_ENABLED
+#ifdef DEBUG_ENABLED
Dictionary PointLight2D::_edit_get_state() const {
Dictionary state = Node2D::_edit_get_state();
state["offset"] = get_texture_offset();
@@ -369,9 +369,7 @@ Point2 PointLight2D::_edit_get_pivot() const {
bool PointLight2D::_edit_use_pivot() const {
return true;
}
-#endif // TOOLS_ENABLED
-#ifdef DEBUG_ENABLED
Rect2 PointLight2D::_edit_get_rect() const {
if (texture.is_null()) {
return Rect2();
diff --git a/scene/2d/light_2d.h b/scene/2d/light_2d.h
index 32e772eb240..6fc354d3b4d 100644
--- a/scene/2d/light_2d.h
+++ b/scene/2d/light_2d.h
@@ -152,16 +152,14 @@ protected:
static void _bind_methods();
public:
-#ifdef TOOLS_ENABLED
+#ifdef DEBUG_ENABLED
virtual Dictionary _edit_get_state() const override;
virtual void _edit_set_state(const Dictionary &p_state) override;
virtual void _edit_set_pivot(const Point2 &p_pivot) override;
virtual Point2 _edit_get_pivot() const override;
virtual bool _edit_use_pivot() const override;
-#endif // TOOLS_ENABLED
-#ifdef DEBUG_ENABLED
virtual Rect2 _edit_get_rect() const override;
virtual bool _edit_use_rect() const override;
#endif // DEBUG_ENABLED
diff --git a/scene/2d/node_2d.cpp b/scene/2d/node_2d.cpp
index 5b4cca68ef0..f37e249640c 100644
--- a/scene/2d/node_2d.cpp
+++ b/scene/2d/node_2d.cpp
@@ -36,7 +36,7 @@
#include "servers/display/accessibility_server.h"
#include "servers/rendering/rendering_server.h"
-#ifdef TOOLS_ENABLED
+#ifdef DEBUG_ENABLED
Dictionary Node2D::_edit_get_state() const {
Dictionary state;
state["position"] = get_position();
@@ -114,7 +114,7 @@ void Node2D::_edit_set_rect(const Rect2 &p_edit_rect) {
_update_transform();
}
-#endif
+#endif // DEBUG_ENABLED
void Node2D::_set_xform_dirty(bool p_dirty) const {
if (is_group_processing()) {
diff --git a/scene/2d/node_2d.h b/scene/2d/node_2d.h
index 256c1ad899a..962ebac24a7 100644
--- a/scene/2d/node_2d.h
+++ b/scene/2d/node_2d.h
@@ -57,7 +57,7 @@ protected:
public:
static constexpr AncestralClass static_ancestral_class = AncestralClass::NODE_2D;
-#ifdef TOOLS_ENABLED
+#ifdef DEBUG_ENABLED
virtual Dictionary _edit_get_state() const override;
virtual void _edit_set_state(const Dictionary &p_state) override;
@@ -72,7 +72,8 @@ public:
virtual bool _edit_use_rotation() const override;
virtual void _edit_set_rect(const Rect2 &p_edit_rect) override;
-#endif
+#endif // DEBUG_ENABLED
+
virtual void reparent(RequiredParam<Node> p_parent, bool p_keep_global_transform = true) override;
void set_position(const Point2 &p_pos);
diff --git a/scene/2d/polygon_2d.cpp b/scene/2d/polygon_2d.cpp
index 73862c3a6dd..22019ebfae0 100644
--- a/scene/2d/polygon_2d.cpp
+++ b/scene/2d/polygon_2d.cpp
@@ -46,7 +46,7 @@ Callable Polygon2D::_navmesh_source_geometry_parsing_callback;
RID Polygon2D::_navmesh_source_geometry_parser;
#endif // NAVIGATION_2D_DISABLED
-#ifdef TOOLS_ENABLED
+#ifdef DEBUG_ENABLED
Dictionary Polygon2D::_edit_get_state() const {
Dictionary state = Node2D::_edit_get_state();
state["offset"] = offset;
@@ -70,9 +70,7 @@ Point2 Polygon2D::_edit_get_pivot() const {
bool Polygon2D::_edit_use_pivot() const {
return true;
}
-#endif // TOOLS_ENABLED
-#ifdef DEBUG_ENABLED
Rect2 Polygon2D::_edit_get_rect() const {
if (rect_cache_dirty) {
int l = polygon.size();
diff --git a/scene/2d/polygon_2d.h b/scene/2d/polygon_2d.h
index 84c7c9ffb4a..86422ac252c 100644
--- a/scene/2d/polygon_2d.h
+++ b/scene/2d/polygon_2d.h
@@ -85,16 +85,14 @@ protected:
static void _bind_methods();
public:
-#ifdef TOOLS_ENABLED
+#ifdef DEBUG_ENABLED
virtual Dictionary _edit_get_state() const override;
virtual void _edit_set_state(const Dictionary &p_state) override;
virtual void _edit_set_pivot(const Point2 &p_pivot) override;
virtual Point2 _edit_get_pivot() const override;
virtual bool _edit_use_pivot() const override;
-#endif // TOOLS_ENABLED
-#ifdef DEBUG_ENABLED
virtual Rect2 _edit_get_rect() const override;
virtual bool _edit_use_rect() const override;
diff --git a/scene/2d/sprite_2d.cpp b/scene/2d/sprite_2d.cpp
index 9c703fe4b45..638403a54cc 100644
--- a/scene/2d/sprite_2d.cpp
+++ b/scene/2d/sprite_2d.cpp
@@ -36,7 +36,7 @@
#include "scene/main/viewport.h"
#include "servers/display/accessibility_server.h"
-#ifdef TOOLS_ENABLED
+#ifdef DEBUG_ENABLED
Dictionary Sprite2D::_edit_get_state() const {
Dictionary state = Node2D::_edit_get_state();
state["offset"] = offset;
@@ -75,9 +75,7 @@ void Sprite2D::_edit_set_rect(const Rect2 &p_rect) {
set_region_rect(Rect2(region_rect.position, p_rect.size));
set_position(get_position() + get_transform().basis_xform(delta));
}
-#endif // TOOLS_ENABLED
-#ifdef DEBUG_ENABLED
bool Sprite2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
return is_pixel_opaque(p_point);
}
diff --git a/scene/2d/sprite_2d.h b/scene/2d/sprite_2d.h
index 20c67594fd0..1d82e44dfd4 100644
--- a/scene/2d/sprite_2d.h
+++ b/scene/2d/sprite_2d.h
@@ -38,9 +38,9 @@ class Sprite2D : public Node2D {
Ref<Texture2D> texture;
-#ifdef TOOLS_ENABLED
+#ifdef DEBUG_ENABLED
bool dragging_to_resize_rect = false;
-#endif // TOOLS_ENABLED
+#endif // DEBUG_ENABLED
bool centered = true;
Point2 offset;
@@ -70,7 +70,7 @@ protected:
void _validate_property(PropertyInfo &p_property) const;
public:
-#ifdef TOOLS_ENABLED
+#ifdef DEBUG_ENABLED
virtual Dictionary _edit_get_state() const override;
virtual void _edit_set_state(const Dictionary &p_state) override;
@@ -79,9 +79,7 @@ public:
virtual bool _edit_use_pivot() const override;
virtual void _edit_set_rect(const Rect2 &p_rect) override;
-#endif // TOOLS_ENABLED
-#ifdef DEBUG_ENABLED
virtual bool _edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const override;
virtual Rect2 _edit_get_rect() const override;
diff --git a/scene/2d/visible_on_screen_notifier_2d.cpp b/scene/2d/visible_on_screen_notifier_2d.cpp
index 685bb14da8a..306fdd92d82 100644
--- a/scene/2d/visible_on_screen_notifier_2d.cpp
+++ b/scene/2d/visible_on_screen_notifier_2d.cpp
@@ -35,7 +35,7 @@
#include "core/object/class_db.h"
#include "servers/rendering/rendering_server.h"
-#ifdef TOOLS_ENABLED
+#ifdef DEBUG_ENABLED
Dictionary VisibleOnScreenNotifier2D::_edit_get_state() const {
Dictionary state = Node2D::_edit_get_state();
state["rect"] = rect;
@@ -51,9 +51,7 @@ void VisibleOnScreenNotifier2D::_edit_set_state(const Dictionary &p_state) {
void VisibleOnScreenNotifier2D::_edit_set_rect(const Rect2 &p_edit_rect) {
set_rect(p_edit_rect);
}
-#endif // TOOLS_ENABLED
-#ifdef DEBUG_ENABLED
Rect2 VisibleOnScreenNotifier2D::_edit_get_rect() const {
return rect;
}
diff --git a/scene/2d/visible_on_screen_notifier_2d.h b/scene/2d/visible_on_screen_notifier_2d.h
index 39f0191b8e0..bf881bc0d71 100644
--- a/scene/2d/visible_on_screen_notifier_2d.h
+++ b/scene/2d/visible_on_screen_notifier_2d.h
@@ -52,16 +52,14 @@ protected:
static void _bind_methods();
public:
-#ifdef TOOLS_ENABLED
+#ifdef DEBUG_ENABLED
virtual Dictionary _edit_get_state() const override;
virtual void _edit_set_state(const Dictionary &p_state) override;
virtual Vector2 _edit_get_minimum_size() const override { return Vector2(); }
virtual void _edit_set_rect(const Rect2 &p_edit_rect) override;
-#endif // TOOLS_ENABLED
-#ifdef DEBUG_ENABLED
virtual Rect2 _edit_get_rect() const override;
virtual bool _edit_use_rect() const override;
diff --git a/scene/debugger/canvas_item_manipulator.cpp b/scene/debugger/canvas_item_manipulator.cpp
new file mode 100644
index 00000000000..f280689b841
--- /dev/null
+++ b/scene/debugger/canvas_item_manipulator.cpp
@@ -0,0 +1,2325 @@
+/**************************************************************************/
+/* canvas_item_manipulator.cpp */
+/**************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/**************************************************************************/
+
+#ifdef DEBUG_ENABLED
+#include "canvas_item_manipulator.h"
+
+#include "core/config/engine.h"
+#include "core/input/shortcut.h"
+#include "scene/2d/node_2d.h"
+#include "scene/gui/container.h"
+#include "scene/main/canvas_layer.h"
+#include "scene/main/scene_tree.h"
+#include "scene/main/window.h"
+
+bool CanvasItemManipulator::_gui_input_rulers_and_guides(const Ref<InputEvent> &p_event) {
+ Ref<InputEventMouseButton> b = p_event;
+ Ref<InputEventMouseMotion> m = p_event;
+
+ if (show_guides && show_rulers && drag_type == DRAG_NONE) {
+ Node *start = Object::cast_to<Node>(find_items_start_callback.call());
+ if (start) {
+ Transform2D xform = (Transform2D)local_transform_callback.call() * (Transform2D)global_transform_callback.call();
+ // Retrieve the guide lists.
+ Array vguides = start->get_meta("_edit_vertical_guides_", Array());
+ Array hguides = start->get_meta("_edit_horizontal_guides_", Array());
+
+ // Hover over guides.
+ real_t minimum = 1e20;
+ is_hovering_h_guide = false;
+ is_hovering_v_guide = false;
+
+ if (m.is_valid() && m->get_position().x < ruler_width) {
+ // Check if we are hovering an existing horizontal guide.
+ for (int i = 0; i < hguides.size(); i++) {
+ if (Math::abs(xform.xform(Point2(0, hguides[i])).y - m->get_position().y) < MIN(minimum, 8)) {
+ is_hovering_h_guide = true;
+ is_hovering_v_guide = false;
+ break;
+ }
+ }
+
+ } else if (m.is_valid() && m->get_position().y < ruler_width) {
+ // Check if we are hovering an existing vertical guide.
+ for (int i = 0; i < vguides.size(); i++) {
+ if (Math::abs(xform.xform(Point2(vguides[i], 0)).x - m->get_position().x) < MIN(minimum, 8)) {
+ is_hovering_v_guide = true;
+ is_hovering_h_guide = false;
+ break;
+ }
+ }
+ }
+
+ // Start dragging a guide
+ if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && b->is_pressed()) {
+ // Press button
+ if (b->get_position().x < ruler_width && b->get_position().y < ruler_width) {
+ // Drag a new double guide.
+ drag_type = DRAG_DOUBLE_GUIDE;
+ dragged_guide_index = -1;
+ return true;
+ } else if (b->get_position().x < ruler_width) {
+ // Check if we drag an existing horizontal guide.
+ dragged_guide_index = -1;
+ for (int i = 0; i < hguides.size(); i++) {
+ if (Math::abs(xform.xform(Point2(0, hguides[i])).y - b->get_position().y) < MIN(minimum, 8)) {
+ dragged_guide_index = i;
+ }
+ }
+
+ if (dragged_guide_index >= 0) {
+ // Drag an existing horizontal guide.
+ drag_type = DRAG_H_GUIDE;
+ } else {
+ // Drag a new vertical guide.
+ drag_type = DRAG_V_GUIDE;
+ }
+ return true;
+ } else if (b->get_position().y < ruler_width) {
+ // Check if we drag an existing vertical guide.
+ dragged_guide_index = -1;
+ for (int i = 0; i < vguides.size(); i++) {
+ if (Math::abs(xform.xform(Point2(vguides[i], 0)).x - b->get_position().x) < MIN(minimum, 8)) {
+ dragged_guide_index = i;
+ }
+ }
+
+ if (dragged_guide_index >= 0) {
+ // Drag an existing vertical guide.
+ drag_type = DRAG_V_GUIDE;
+ } else {
+ // Drag a new vertical guide.
+ drag_type = DRAG_H_GUIDE;
+ }
+ drag_from = xform.affine_inverse().xform(b->get_position());
+ return true;
+ }
+ }
+ }
+ }
+
+ if (drag_type == DRAG_DOUBLE_GUIDE || drag_type == DRAG_V_GUIDE || drag_type == DRAG_H_GUIDE) {
+ // Move the guide.
+ if (m.is_valid()) {
+ Transform2D xform = (Transform2D)local_transform_callback.call() * (Transform2D)global_transform_callback.call();
+ drag_to = xform.affine_inverse().xform(m->get_position());
+
+ dragged_guide_pos = xform.xform(snap_point(drag_to, SNAP_GRID | SNAP_PIXEL | SNAP_OTHER_NODES));
+ emit_signal(update_canvas_signal);
+ return true;
+ }
+
+ // Confirm the guide move.
+ if (show_guides && b.is_valid() && b->get_button_index() == MouseButton::LEFT && !b->is_pressed()) {
+ Node *start = Object::cast_to<Node>(find_items_start_callback.call());
+ if (start) {
+ Transform2D xform = (Transform2D)local_transform_callback.call() * (Transform2D)global_transform_callback.call();
+
+ // Retrieve the guide lists.
+ Array vguides = start->get_meta("_edit_vertical_guides_", Array());
+ Array hguides = start->get_meta("_edit_horizontal_guides_", Array());
+
+ Point2 edited = snap_point(xform.affine_inverse().xform(b->get_position()), SNAP_GRID | SNAP_PIXEL | SNAP_OTHER_NODES);
+ if (drag_type == DRAG_V_GUIDE) {
+ String msg;
+ if (b->get_position().x > ruler_width) { // Add/move a vertical guide.
+ if (dragged_guide_index >= 0) {
+ vguides[dragged_guide_index] = edited.x;
+ msg = "Move Vertical Guide";
+ } else {
+ vguides.push_back(edited.x);
+ msg = "Create Vertical Guide";
+ }
+ } else if (dragged_guide_index >= 0) {
+ vguides.remove_at(dragged_guide_index);
+ msg = "Remove Vertical Guide";
+ }
+
+ Dictionary dict;
+ dict["_edit_vertical_guides_"] = vguides;
+ emit_signal("commit_guide_meta_requested", RTR(msg), dict);
+
+ } else if (drag_type == DRAG_H_GUIDE) { // Add/move a horizontal guide.
+ String msg;
+ if (b->get_position().y > ruler_width) {
+ if (dragged_guide_index >= 0) {
+ hguides[dragged_guide_index] = edited.y;
+ msg = "Move Horizontal Guide";
+ } else {
+ hguides.push_back(edited.y);
+ msg = "Create Horizontal Guide";
+ }
+ } else if (dragged_guide_index >= 0) {
+ hguides.remove_at(dragged_guide_index);
+ msg = "Remove Horizontal Guide";
+ }
+
+ Dictionary dict;
+ dict["_edit_horizontal_guides_"] = hguides;
+ emit_signal("commit_guide_meta_requested", RTR(msg), dict);
+
+ } else if (drag_type == DRAG_DOUBLE_GUIDE) { // Add/move a double guide.
+ if (b->get_position().x > ruler_width && b->get_position().y > ruler_width) {
+ vguides.push_back(edited.x);
+ hguides.push_back(edited.y);
+
+ Dictionary dict;
+ dict["_edit_vertical_guides_"] = vguides;
+ dict["_edit_horizontal_guides_"] = hguides;
+ emit_signal("commit_guide_meta_requested", RTR("Create Horizontal and Vertical Guides"), dict);
+ }
+ }
+ }
+ }
+
+ snap_target[0] = SNAP_TARGET_NONE;
+ snap_target[1] = SNAP_TARGET_NONE;
+ reset_drag();
+ emit_signal(update_canvas_signal);
+ return true;
+ }
+
+ return false;
+}
+
+bool CanvasItemManipulator::_gui_input_open_scene_on_double_click(const Ref<InputEvent> &p_event) {
+ Ref<InputEventMouseButton> b = p_event;
+
+ // Open a sub-scene on double-click.
+ if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && b->is_pressed() && b->is_double_click() && tool == TOOL_SELECT) {
+ Array selection;
+ get_selection_callback.call(selection);
+ if (selection.size() == 1) {
+ CanvasItem *ci = Object::cast_to<CanvasItem>(selection.front());
+ if (ci->is_instance() && ci != find_items_start_callback.call()) {
+ emit_signal("scene_double_clicked", ci->get_scene_file_path());
+ return true;
+ }
+ }
+ }
+
+ return false;
+}
+
+bool CanvasItemManipulator::_gui_input_scale(const Ref<InputEvent> &p_event) {
+ Ref<InputEventMouseButton> b = p_event;
+ Ref<InputEventMouseMotion> m = p_event;
+
+ if (drag_type == DRAG_NONE) {
+ // Drag the resize handles.
+ if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && b->is_pressed() &&
+ ((tool == TOOL_SELECT && b->is_alt_pressed() && b->is_command_or_control_pressed()) || tool == TOOL_SCALE)) {
+ Array selection;
+ bool has_locked_items = get_selection_callback.call(selection);
+
+ drag_selection.clear();
+ // Remove unmovable nodes.
+ for (const Variant &var : selection) {
+ const Node *node = Object::cast_to<Node>(var);
+ if (is_node_movable(node, true)) {
+ drag_selection.push_back(var);
+ }
+ }
+
+ if (!drag_selection.is_empty()) {
+ CanvasItem *ci = Object::cast_to<CanvasItem>(drag_selection.front());
+
+ Transform2D edit_transform;
+ if (!Math::is_inf(temp_pivot.x) || !Math::is_inf(temp_pivot.y)) {
+ edit_transform = Transform2D(ci->_edit_get_rotation(), temp_pivot);
+ } else {
+ edit_transform = ci->_edit_get_transform();
+ }
+
+ Transform2D xform = (Transform2D)global_transform_callback.call() * ci->get_screen_transform();
+ Transform2D unscaled_transform = (xform * ci->get_transform().affine_inverse() * edit_transform).orthonormalized();
+ Transform2D simple_xform;
+ if (local_space) {
+ simple_xform = (Transform2D)local_transform_callback.call() * unscaled_transform;
+ } else {
+ Transform2D translation = Transform2D(0.0f, unscaled_transform.get_origin());
+ simple_xform = (Transform2D)local_transform_callback.call() * translation;
+ }
+
+ drag_type = DRAG_SCALE_BOTH;
+
+ if (show_transformation_gizmos) {
+ Size2 scale_factor = Size2(GIZMO_HANDLE_DISTANCE, GIZMO_HANDLE_DISTANCE);
+ Rect2 x_handle_rect = Rect2(scale_factor.x * scale, -5 * scale, 10 * scale, 10 * scale);
+ if (x_handle_rect.has_point(simple_xform.affine_inverse().xform(b->get_position()))) {
+ drag_type = DRAG_SCALE_X;
+ }
+ Rect2 y_handle_rect = Rect2(-5 * scale, scale_factor.y * scale, 10 * scale, 10 * scale);
+ if (y_handle_rect.has_point(simple_xform.affine_inverse().xform(b->get_position()))) {
+ drag_type = DRAG_SCALE_Y;
+ }
+ }
+
+ drag_from = Transform2D(global_transform_callback.call()).affine_inverse().xform(b->get_position());
+ drag_to = drag_from;
+ emit_signal("save_canvas_state_requested", drag_selection, false);
+
+ return true;
+ } else {
+ if (has_locked_items) {
+ _emit_locked_warn_signal();
+ }
+
+ return has_locked_items;
+ }
+ }
+ } else if (drag_type == DRAG_SCALE_BOTH || drag_type == DRAG_SCALE_X || drag_type == DRAG_SCALE_Y) {
+ // Resize the node.
+ if (m.is_valid()) {
+ if (!_validate_drag_selection()) {
+ drag_type = DRAG_NONE;
+ return true;
+ }
+
+ emit_signal("restore_canvas_state_requested", drag_selection, false);
+
+ drag_to = Transform2D(global_transform_callback.call()).affine_inverse().xform(m->get_position());
+
+ Size2 scale_max;
+ if (drag_type != DRAG_SCALE_BOTH) {
+ for (const Variant &var : drag_selection) {
+ CanvasItem *ci = Object::cast_to<CanvasItem>(var);
+ Size2 ci_scale = ci->_edit_get_scale();
+
+ if (Math::abs(ci_scale.x) > Math::abs(scale_max.x)) {
+ scale_max.x = ci_scale.x;
+ }
+ if (Math::abs(ci_scale.y) > Math::abs(scale_max.y)) {
+ scale_max.y = ci_scale.y;
+ }
+ }
+ }
+
+ Transform2D edit_transform;
+ bool using_temp_pivot = !Math::is_inf(temp_pivot.x) || !Math::is_inf(temp_pivot.y);
+ CanvasItem *ci_front = Object::cast_to<CanvasItem>(drag_selection.front());
+ if (using_temp_pivot) {
+ edit_transform = Transform2D(ci_front->_edit_get_rotation(), temp_pivot);
+ } else {
+ edit_transform = ci_front->_edit_get_transform();
+ }
+
+ for (const Variant &var : drag_selection) {
+ CanvasItem *ci = Object::cast_to<CanvasItem>(var);
+ Transform2D parent_xform = ci->get_screen_transform() * ci->get_transform().affine_inverse();
+ Transform2D unscaled_transform = ((Transform2D)global_transform_callback.call() * parent_xform * edit_transform).orthonormalized();
+ Transform2D simple_xform;
+
+ if (local_space || drag_type == DRAG_SCALE_BOTH) {
+ simple_xform = ((Transform2D)local_transform_callback.call() * unscaled_transform).affine_inverse() * (Transform2D)global_transform_callback.call();
+ } else {
+ Transform2D translation = Transform2D(0.0f, unscaled_transform.get_origin());
+ simple_xform = ((Transform2D)local_transform_callback.call() * translation).affine_inverse() * (Transform2D)global_transform_callback.call();
+ }
+
+ bool uniform = m->is_shift_pressed();
+ bool is_ctrl = m->is_command_or_control_pressed();
+
+ Point2 drag_from_local = simple_xform.xform(drag_from);
+ Point2 drag_to_local = simple_xform.xform(drag_to);
+ Point2 offset = drag_to_local - drag_from_local;
+
+ Transform2D object_transform = ci->_edit_get_transform();
+ if (ci->is_class("Node2D")) {
+ object_transform.set_skew(ci->get("skew"));
+ }
+
+ Size2 ci_scale = ci->_edit_get_scale();
+ Size2 original_scale = ci_scale;
+ real_t ratio = ci_scale.y / ci_scale.x;
+ if (drag_type == DRAG_SCALE_BOTH) {
+ Size2 scale_factor = drag_to_local / drag_from_local;
+ if (uniform) {
+ ci_scale *= (scale_factor.x + scale_factor.y) / 2.0;
+ } else {
+ ci_scale *= scale_factor;
+ }
+ } else {
+ Size2 scale_factor = Size2(offset.x, -offset.y) / GIZMO_HANDLE_DISTANCE;
+ Size2 parent_scale = parent_xform.get_scale();
+ // Take into account the biggest scale, so all nodes are scaled uniformly.
+ scale_factor *= Size2(1.0 / parent_scale.x, 1.0 / parent_scale.y) / (scale_max / original_scale);
+
+ if (drag_type == DRAG_SCALE_X) {
+ if (!local_space && !uniform) {
+ object_transform.set_origin(Point2());
+ object_transform.scale(Size2(scale_factor.x + 1.0, 1));
+ ci_scale *= object_transform.get_scale();
+ } else {
+ ci_scale.x += scale_factor.x;
+ }
+ if (uniform) {
+ ci_scale.y = ci_scale.x * ratio;
+ }
+ } else if (drag_type == DRAG_SCALE_Y) {
+ if (!local_space && !uniform) {
+ object_transform.set_origin(Point2());
+ object_transform.scale(Size2(1, -scale_factor.y + 1.0));
+ ci_scale *= object_transform.get_scale();
+ } else {
+ ci_scale.y -= scale_factor.y;
+ }
+ if (uniform) {
+ ci_scale.x = ci_scale.y / ratio;
+ }
+ }
+ }
+
+ if (snap_scale && !is_ctrl) {
+ if (snap_relative) {
+ ci_scale.x = original_scale.x * (Math::round((ci_scale.x / original_scale.x) / snap_scale_step) * snap_scale_step);
+ ci_scale.y = original_scale.y * (Math::round((ci_scale.y / original_scale.y) / snap_scale_step) * snap_scale_step);
+ } else {
+ ci_scale.x = Math::round(ci_scale.x / snap_scale_step) * snap_scale_step;
+ ci_scale.y = Math::round(ci_scale.y / snap_scale_step) * snap_scale_step;
+ }
+ }
+
+ ci->_edit_set_scale(ci_scale);
+ if (!local_space && !uniform) {
+ Node2D *n2d = Object::cast_to<Node2D>(ci);
+ if (n2d) {
+ n2d->_edit_set_rotation(object_transform.get_rotation());
+ n2d->set_skew(object_transform.get_skew());
+ }
+ }
+
+ if (using_temp_pivot) {
+ Point2 ci_origin = ci->_edit_get_transform().get_origin();
+ ci->_edit_set_position(ci_origin + (ci_origin - temp_pivot) * ((ci_scale - original_scale) / original_scale));
+ }
+ }
+
+ return true;
+ }
+
+ // Confirm resize.
+ if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && !b->is_pressed()) {
+ commit_drag();
+ return true;
+ }
+
+ if (_gui_input_cancel_drag(p_event)) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+bool CanvasItemManipulator::_gui_input_pivot(const Ref<InputEvent> &p_event) {
+ Ref<InputEventMouseMotion> m = p_event;
+ Ref<InputEventMouseButton> b = p_event;
+ Ref<InputEventKey> k = p_event;
+
+ // Drag the pivot (in pivot mode, or with the V key).
+ if (drag_type == DRAG_NONE) {
+ bool move_temp_pivot = ((b.is_valid() && b->is_shift_pressed()) || (k.is_valid() && k->is_shift_pressed()));
+
+ if ((b.is_valid() && b->is_pressed() && b->get_button_index() == MouseButton::LEFT && tool == TOOL_EDIT_PIVOT) ||
+ (k.is_valid() && k->is_pressed() && !k->is_echo() && k->get_keycode() == Key::V && tool == TOOL_SELECT && (k->get_modifiers_mask().is_empty() || move_temp_pivot))) {
+ Array selection;
+ get_selection_callback.call(selection);
+
+ drag_selection.clear();
+ // Filters the selection with nodes that allow setting the pivot.
+ for (const Variant &var : selection) {
+ const CanvasItem *ci = Object::cast_to<CanvasItem>(var);
+ if (ci->_edit_use_pivot() || move_temp_pivot) {
+ drag_selection.push_back(var);
+ }
+ }
+
+ // Start dragging if we still have nodes.
+ if (!drag_selection.is_empty()) {
+ Vector2 event_pos = b.is_valid() ? b->get_position() : (Point2)local_mouse_pos_callback.call();
+
+ if (move_temp_pivot) {
+ drag_type = DRAG_TEMP_PIVOT;
+ temp_pivot = Transform2D(global_transform_callback.call()).affine_inverse().xform(event_pos);
+ emit_signal(update_canvas_signal);
+ return true;
+ }
+
+ emit_signal("save_canvas_state_requested", drag_selection, false);
+ drag_from = Transform2D(global_transform_callback.call()).affine_inverse().xform(event_pos);
+ Vector2 new_pos;
+ if (drag_selection.size() == 1) {
+ const Node2D *n2d = Object::cast_to<Node2D>(drag_selection.front());
+ new_pos = snap_point(drag_from, SNAP_NODE_SIDES | SNAP_NODE_CENTER | SNAP_NODE_ANCHORS | SNAP_OTHER_NODES | SNAP_GRID | SNAP_PIXEL, 0, n2d);
+ } else {
+ new_pos = snap_point(drag_from, SNAP_OTHER_NODES | SNAP_GRID | SNAP_PIXEL, 0, nullptr, drag_selection);
+ }
+
+ for (const Variant &var : drag_selection) {
+ CanvasItem *ci = Object::cast_to<CanvasItem>(var);
+ ci->_edit_set_pivot(ci->get_screen_transform().affine_inverse().xform(new_pos));
+ }
+
+ drag_type = DRAG_PIVOT;
+ }
+
+ return true;
+ }
+ }
+
+ if (drag_type == DRAG_PIVOT) {
+ // Move the pivot.
+ if (m.is_valid()) {
+ if (!_validate_drag_selection()) {
+ drag_type = DRAG_NONE;
+ return true;
+ }
+
+ drag_to = Transform2D(global_transform_callback.call()).affine_inverse().xform(m->get_position());
+ emit_signal("restore_canvas_state_requested", drag_selection, false);
+
+ Vector2 new_pos;
+ if (drag_selection.size() == 1) {
+ const Node2D *n2d = Object::cast_to<Node2D>(drag_selection.front());
+ new_pos = snap_point(drag_to, SNAP_NODE_SIDES | SNAP_NODE_CENTER | SNAP_NODE_ANCHORS | SNAP_OTHER_NODES | SNAP_GRID | SNAP_PIXEL, 0, n2d);
+ } else {
+ new_pos = snap_point(drag_to, SNAP_OTHER_NODES | SNAP_GRID | SNAP_PIXEL);
+ }
+
+ for (const Variant &var : drag_selection) {
+ CanvasItem *ci = Object::cast_to<CanvasItem>(var);
+ ci->_edit_set_pivot(ci->get_screen_transform().affine_inverse().xform(new_pos));
+ }
+
+ return true;
+ }
+
+ // Confirm the pivot move.
+ if (drag_selection.size() >= 1 &&
+ ((b.is_valid() && !b->is_pressed() && b->get_button_index() == MouseButton::LEFT && tool == TOOL_EDIT_PIVOT) ||
+ (k.is_valid() && !k->is_pressed() && k->get_keycode() == Key::V))) {
+ commit_drag();
+ snap_target[0] = SNAP_TARGET_NONE;
+ snap_target[1] = SNAP_TARGET_NONE;
+ return true;
+ }
+
+ if (_gui_input_cancel_drag(p_event, true)) {
+ return true;
+ }
+ }
+
+ if (drag_type == DRAG_TEMP_PIVOT) {
+ if (m.is_valid()) {
+ temp_pivot = Transform2D(global_transform_callback.call()).affine_inverse().xform(m->get_position());
+ emit_signal(update_canvas_signal);
+ return true;
+ }
+
+ if ((b.is_valid() && !b->is_pressed() && b->get_button_index() == MouseButton::LEFT && tool == TOOL_EDIT_PIVOT) ||
+ (k.is_valid() && !k->is_pressed() && k->get_keycode() == Key::V)) {
+ drag_type = DRAG_NONE;
+ return true;
+ }
+ }
+
+ return false;
+}
+
+bool CanvasItemManipulator::_gui_input_resize(const Ref<InputEvent> &p_event) {
+ Ref<InputEventMouseButton> b = p_event;
+ Ref<InputEventMouseMotion> m = p_event;
+
+ // Drag resize handles.
+ if (drag_type == DRAG_NONE && b.is_valid() && b->get_button_index() == MouseButton::LEFT && b->is_pressed() && tool == TOOL_SELECT) {
+ Array selection;
+ get_selection_callback.call(selection);
+
+ if (selection.size() == 1) {
+ CanvasItem *ci = Object::cast_to<CanvasItem>(selection.front());
+ if (ci->_edit_use_rect() && is_node_movable(ci)) {
+ Rect2 rect = ci->_edit_get_rect();
+ Transform2D xform = (Transform2D)global_transform_callback.call() * ci->get_screen_transform();
+
+ const Point2 endpoints[4] = {
+ xform.xform(rect.position),
+ xform.xform(rect.position + Point2(rect.size.x, 0)),
+ xform.xform(rect.position + rect.size),
+ xform.xform(rect.position + Point2(0, rect.size.y))
+ };
+
+ const DragType dragger[] = {
+ DRAG_TOP_LEFT,
+ DRAG_TOP,
+ DRAG_TOP_RIGHT,
+ DRAG_RIGHT,
+ DRAG_BOTTOM_RIGHT,
+ DRAG_BOTTOM,
+ DRAG_BOTTOM_LEFT,
+ DRAG_LEFT,
+ };
+
+ DragType resize_drag = DRAG_NONE;
+ real_t radius = RESIZE_HANDLE_DIAMETER * (1.5f / 2.0f);
+
+ for (int i = 0; i < 4; i++) {
+ int prev = (i + 3) % 4;
+ int next = (i + 1) % 4;
+
+ Point2 ofs = ((endpoints[i] - endpoints[prev]).normalized() + ((endpoints[i] - endpoints[next]).normalized())).normalized();
+ ofs *= RESIZE_HANDLE_DIAMETER / 2;
+ ofs += endpoints[i];
+ if (ofs.distance_to(b->get_position()) < radius) {
+ resize_drag = dragger[i * 2];
+ }
+
+ ofs = (endpoints[i] + endpoints[next]) / 2;
+ ofs += (endpoints[next] - endpoints[i]).orthogonal().normalized() * (RESIZE_HANDLE_DIAMETER / 2);
+ if (ofs.distance_to(b->get_position()) < radius) {
+ resize_drag = dragger[i * 2 + 1];
+ }
+ }
+
+ if (resize_drag != DRAG_NONE) {
+ drag_type = resize_drag;
+ drag_from = Transform2D(global_transform_callback.call()).affine_inverse().xform(b->get_position());
+ drag_selection.clear();
+ drag_selection.push_back(ci);
+ emit_signal("save_canvas_state_requested", drag_selection, false);
+ return true;
+ }
+ }
+ }
+ }
+
+ if (drag_type == DRAG_LEFT || drag_type == DRAG_RIGHT || drag_type == DRAG_TOP || drag_type == DRAG_BOTTOM ||
+ drag_type == DRAG_TOP_LEFT || drag_type == DRAG_TOP_RIGHT || drag_type == DRAG_BOTTOM_LEFT || drag_type == DRAG_BOTTOM_RIGHT) {
+ // Resize the node.
+ if (m.is_valid()) {
+ if (!_validate_drag_selection()) {
+ drag_type = DRAG_NONE;
+ return true;
+ }
+
+ CanvasItem *ci = Object::cast_to<CanvasItem>(drag_selection.front());
+ Array single = { ci };
+ emit_signal("restore_canvas_state_requested", single, false);
+
+ bool uniform = m->is_shift_pressed();
+ bool symmetric = m->is_alt_pressed();
+
+ Rect2 local_rect = ci->_edit_get_rect();
+ real_t aspect = local_rect.has_area() ? (local_rect.get_size().y / local_rect.get_size().x) : (local_rect.get_size().y + 1.0) / (local_rect.get_size().x + 1.0);
+ Point2 current_begin = local_rect.get_position();
+ Point2 current_end = local_rect.get_position() + local_rect.get_size();
+ Point2 max_begin = (symmetric) ? (current_begin + current_end - ci->_edit_get_minimum_size()) / 2.0 : current_end - ci->_edit_get_minimum_size();
+ Point2 min_end = (symmetric) ? (current_begin + current_end + ci->_edit_get_minimum_size()) / 2.0 : current_begin + ci->_edit_get_minimum_size();
+ Point2 center = (current_begin + current_end) / 2.0;
+
+ drag_to = Transform2D(global_transform_callback.call()).affine_inverse().xform(m->get_position());
+
+ Transform2D xform = ci->get_screen_transform();
+
+ Point2 drag_to_snapped_begin;
+ Point2 drag_to_snapped_end;
+
+ drag_to_snapped_end = snap_point(xform.xform(current_end) + (drag_to - drag_from), SNAP_NODE_ANCHORS | SNAP_NODE_PARENT | SNAP_OTHER_NODES | SNAP_GRID | SNAP_PIXEL, 0, ci);
+ drag_to_snapped_begin = snap_point(xform.xform(current_begin) + (drag_to - drag_from), SNAP_NODE_ANCHORS | SNAP_NODE_PARENT | SNAP_OTHER_NODES | SNAP_GRID | SNAP_PIXEL, 0, ci);
+
+ Point2 drag_begin = xform.affine_inverse().xform(drag_to_snapped_begin);
+ Point2 drag_end = xform.affine_inverse().xform(drag_to_snapped_end);
+
+ // Horizontal resize.
+ if (drag_type == DRAG_LEFT || drag_type == DRAG_TOP_LEFT || drag_type == DRAG_BOTTOM_LEFT) {
+ current_begin.x = MIN(drag_begin.x, max_begin.x);
+ } else if (drag_type == DRAG_RIGHT || drag_type == DRAG_TOP_RIGHT || drag_type == DRAG_BOTTOM_RIGHT) {
+ current_end.x = MAX(drag_end.x, min_end.x);
+ }
+
+ // Vertical resize.
+ if (drag_type == DRAG_TOP || drag_type == DRAG_TOP_LEFT || drag_type == DRAG_TOP_RIGHT) {
+ current_begin.y = MIN(drag_begin.y, max_begin.y);
+ } else if (drag_type == DRAG_BOTTOM || drag_type == DRAG_BOTTOM_LEFT || drag_type == DRAG_BOTTOM_RIGHT) {
+ current_end.y = MAX(drag_end.y, min_end.y);
+ }
+
+ // Uniform resize.
+ if (uniform) {
+ if (drag_type == DRAG_LEFT || drag_type == DRAG_RIGHT) {
+ current_end.y = current_begin.y + aspect * (current_end.x - current_begin.x);
+ } else if (drag_type == DRAG_TOP || drag_type == DRAG_BOTTOM) {
+ current_end.x = current_begin.x + (current_end.y - current_begin.y) / aspect;
+ } else {
+ if (aspect >= 1.0) {
+ if (drag_type == DRAG_TOP_LEFT || drag_type == DRAG_TOP_RIGHT) {
+ current_begin.y = current_end.y - aspect * (current_end.x - current_begin.x);
+ } else {
+ current_end.y = current_begin.y + aspect * (current_end.x - current_begin.x);
+ }
+ } else {
+ if (drag_type == DRAG_TOP_LEFT || drag_type == DRAG_BOTTOM_LEFT) {
+ current_begin.x = current_end.x - (current_end.y - current_begin.y) / aspect;
+ } else {
+ current_end.x = current_begin.x + (current_end.y - current_begin.y) / aspect;
+ }
+ }
+ }
+ }
+
+ // Symmetric resize.
+ if (symmetric) {
+ if (drag_type == DRAG_LEFT || drag_type == DRAG_TOP_LEFT || drag_type == DRAG_BOTTOM_LEFT) {
+ current_end.x = 2.0 * center.x - current_begin.x;
+ } else if (drag_type == DRAG_RIGHT || drag_type == DRAG_TOP_RIGHT || drag_type == DRAG_BOTTOM_RIGHT) {
+ current_begin.x = 2.0 * center.x - current_end.x;
+ }
+ if (drag_type == DRAG_TOP || drag_type == DRAG_TOP_LEFT || drag_type == DRAG_TOP_RIGHT) {
+ current_end.y = 2.0 * center.y - current_begin.y;
+ } else if (drag_type == DRAG_BOTTOM || drag_type == DRAG_BOTTOM_LEFT || drag_type == DRAG_BOTTOM_RIGHT) {
+ current_begin.y = 2.0 * center.y - current_end.y;
+ }
+ }
+
+ if (anchors_mode) {
+ Control *control = Object::cast_to<Control>(ci);
+ if (control) {
+ Size2 parent_rect_size = control->get_parent_anchorable_rect().size;
+ if (parent_rect_size.x == 0.0 || parent_rect_size.y == 0.0) {
+ emit_signal("anchors_mode_warn_requested", TTRC("Can't modify anchor offsets when the parent has a size of 0. Disable the anchors mode in the toolbar."));
+ return true;
+ }
+ }
+ }
+
+ ci->_edit_set_rect(Rect2(current_begin, current_end - current_begin));
+ return true;
+ }
+
+ // Confirm resize.
+ if (drag_selection.size() >= 1 && b.is_valid() && b->get_button_index() == MouseButton::LEFT && !b->is_pressed()) {
+ commit_drag();
+ return true;
+ }
+
+ if (_gui_input_cancel_drag(p_event, true)) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+bool CanvasItemManipulator::_gui_input_rotate(const Ref<InputEvent> &p_event) {
+ Ref<InputEventMouseButton> b = p_event;
+ Ref<InputEventMouseMotion> m = p_event;
+
+ if (drag_type == DRAG_NONE) {
+ // Start rotation.
+ if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && b->is_pressed()) {
+ if ((b->is_command_or_control_pressed() && !b->is_alt_pressed() && tool == TOOL_SELECT) || tool == TOOL_ROTATE) {
+ Array selection;
+ bool has_locked_items = get_selection_callback.call(selection);
+
+ drag_selection.clear();
+ // Remove unmovable nodes.
+ for (const Variant &var : selection) {
+ const Node *node = Object::cast_to<Node>(var);
+ if (is_node_movable(node, true)) {
+ drag_selection.push_back(var);
+ }
+ }
+
+ if (!drag_selection.is_empty()) {
+ drag_type = DRAG_ROTATE;
+ drag_from = Transform2D(global_transform_callback.call()).affine_inverse().xform(b->get_position());
+ drag_to = drag_from;
+
+ CanvasItem *ci = Object::cast_to<CanvasItem>(drag_selection.front());
+ if (!Math::is_inf(temp_pivot.x) || !Math::is_inf(temp_pivot.y)) {
+ drag_rotation_center = temp_pivot;
+ } else if (ci->_edit_use_pivot()) {
+ drag_rotation_center = ci->get_screen_transform().xform(ci->_edit_get_pivot());
+ } else {
+ drag_rotation_center = ci->get_screen_transform().get_origin();
+ }
+
+ emit_signal("save_canvas_state_requested", drag_selection, false);
+
+ return true;
+ } else {
+ if (has_locked_items) {
+ _emit_locked_warn_signal();
+ }
+
+ return has_locked_items;
+ }
+ }
+ }
+ }
+
+ if (drag_type == DRAG_ROTATE) {
+ // Rotate the node.
+ if (m.is_valid()) {
+ if (!_validate_drag_selection()) {
+ drag_type = DRAG_NONE;
+ return true;
+ }
+
+ emit_signal("restore_canvas_state_requested", drag_selection, false);
+
+ for (const Variant &var : drag_selection) {
+ CanvasItem *ci = Object::cast_to<CanvasItem>(var);
+ drag_to = Transform2D(global_transform_callback.call()).affine_inverse().xform(m->get_position());
+
+ // Rotate the opposite way if the canvas item's compounded scale has an uneven number of negative elements.
+ bool opposite = (ci->get_global_transform().get_scale().sign().dot(ci->get_transform().get_scale().sign()) == 0);
+
+ real_t prev_rotation = ci->_edit_get_rotation();
+ real_t new_rotation = ci->_edit_get_rotation() + (opposite ? -1 : 1) * (drag_from - drag_rotation_center).angle_to(drag_to - drag_rotation_center);
+ // Snap angle.
+ if (((smart_snap || snap_rotation) != m->is_command_or_control_pressed()) && snap_rotation_step != 0) {
+ if (snap_relative) {
+ new_rotation = Math::snapped(new_rotation - snap_rotation_offset, snap_rotation_step) + snap_rotation_offset + (prev_rotation - (int)(prev_rotation / snap_rotation_step) * snap_rotation_step);
+ } else {
+ new_rotation = Math::snapped(new_rotation - snap_rotation_offset, snap_rotation_step) + snap_rotation_offset;
+ }
+ }
+
+ ci->_edit_set_rotation(new_rotation);
+ if (!Math::is_inf(temp_pivot.x) || !Math::is_inf(temp_pivot.y)) {
+ Transform2D xform = ci->get_screen_transform() * ci->get_transform().affine_inverse();
+ Vector2 radius = xform.xform(ci->_edit_get_position()) - temp_pivot;
+ radius = radius.rotated(new_rotation - prev_rotation);
+ ci->_edit_set_position(xform.affine_inverse().xform(temp_pivot + radius));
+ }
+
+ emit_signal(update_canvas_signal);
+ }
+
+ return true;
+ }
+
+ // Confirm the node rotation.
+ if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && !b->is_pressed()) {
+ commit_drag();
+ return true;
+ }
+
+ if (_gui_input_cancel_drag(p_event)) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+bool CanvasItemManipulator::_gui_input_move(const Ref<InputEvent> &p_event) {
+ Ref<InputEventMouseButton> b = p_event;
+ Ref<InputEventMouseMotion> m = p_event;
+ Ref<InputEventKey> k = p_event;
+
+ // Start moving the nodes.
+ if (drag_type == DRAG_NONE && b.is_valid() && b->get_button_index() == MouseButton::LEFT && b->is_pressed() &&
+ ((tool == TOOL_SELECT && b->is_alt_pressed() && !b->is_command_or_control_pressed()) || tool == TOOL_MOVE)) {
+ Array selection;
+ bool has_locked_items = get_selection_callback.call(selection);
+
+ drag_selection.clear();
+ // Remove unmovable nodes.
+ for (const Variant &var : selection) {
+ const Node *node = Object::cast_to<Node>(var);
+ if (is_node_movable(node, true)) {
+ drag_selection.push_back(var);
+ }
+ }
+
+ if (!drag_selection.is_empty()) {
+ drag_type = DRAG_MOVE;
+
+ CanvasItem *ci = Object::cast_to<CanvasItem>(drag_selection.front());
+ Transform2D parent_xform = ci->get_screen_transform() * ci->get_transform().affine_inverse();
+ Transform2D unscaled_transform = ((Transform2D)global_transform_callback.call() * parent_xform * ci->_edit_get_transform()).orthonormalized();
+ Transform2D simple_xform;
+ if (local_space) {
+ simple_xform = (Transform2D)local_transform_callback.call() * unscaled_transform;
+ } else {
+ Transform2D translation = Transform2D(0.0f, unscaled_transform.get_origin());
+ simple_xform = (Transform2D)local_transform_callback.call() * translation;
+ }
+
+ if (show_transformation_gizmos) {
+ const Rect2 x_handle_rect = Rect2(GIZMO_HANDLE_X_RECT.position * scale, GIZMO_HANDLE_X_RECT.size * scale);
+ if (x_handle_rect.has_point(simple_xform.affine_inverse().xform(b->get_position()))) {
+ drag_type = DRAG_MOVE_X;
+ }
+
+ const Rect2 y_handle_rect = Rect2(GIZMO_HANDLE_Y_RECT.position * scale, GIZMO_HANDLE_Y_RECT.size * scale);
+ if (y_handle_rect.has_point(simple_xform.affine_inverse().xform(b->get_position()))) {
+ drag_type = DRAG_MOVE_Y;
+ }
+ }
+
+ drag_from = Transform2D(global_transform_callback.call()).affine_inverse().xform(b->get_position());
+ emit_signal("save_canvas_state_requested", drag_selection, true);
+
+ return true;
+ } else {
+ if (has_locked_items) {
+ _emit_locked_warn_signal();
+ }
+
+ return has_locked_items;
+ }
+ }
+
+ // Move the nodes.
+ if (drag_type == DRAG_MOVE || drag_type == DRAG_MOVE_X || drag_type == DRAG_MOVE_Y) {
+ if (m.is_valid() && !drag_selection.is_empty()) {
+ if (!_validate_drag_selection()) {
+ drag_type = DRAG_NONE;
+ return true;
+ }
+
+ emit_signal("restore_canvas_state_requested", drag_selection, true);
+
+ drag_to = Transform2D(global_transform_callback.call()).affine_inverse().xform(m->get_position());
+ Point2 previous_pos;
+ if (drag_selection.size() == 1) {
+ const CanvasItem *ci = Object::cast_to<CanvasItem>(drag_selection.front());
+ Transform2D parent_xform = ci->get_screen_transform() * ci->get_transform().affine_inverse();
+ previous_pos = parent_xform.xform(ci->_edit_get_position());
+ } else {
+ previous_pos = _encompass_selection_rect(drag_selection).position;
+ }
+
+ Point2 drag_delta = drag_to - drag_from;
+ if (drag_type == DRAG_MOVE_X || drag_type == DRAG_MOVE_Y) {
+ const CanvasItem *selected = Object::cast_to<CanvasItem>(drag_selection.front());
+ Transform2D parent_xform = selected->get_screen_transform() * selected->get_transform().affine_inverse();
+ Transform2D unscaled_transform = ((Transform2D)global_transform_callback.call() * parent_xform * selected->_edit_get_transform()).orthonormalized();
+
+ Transform2D simple_xform = local_transform_callback.call();
+ if (local_space) {
+ simple_xform *= unscaled_transform;
+ }
+
+ drag_delta = simple_xform.affine_inverse().basis_xform(drag_delta);
+ if (drag_type == DRAG_MOVE_X) {
+ drag_delta.y = 0;
+ } else {
+ drag_delta.x = 0;
+ }
+ drag_delta = simple_xform.basis_xform(drag_delta);
+ }
+ Point2 new_pos = snap_point(previous_pos + drag_delta, SNAP_GRID | SNAP_GUIDES | SNAP_PIXEL | SNAP_NODE_PARENT | SNAP_NODE_ANCHORS | SNAP_OTHER_NODES, 0, nullptr, drag_selection);
+
+ bool single_axis = m->is_shift_pressed();
+ if (single_axis) {
+ if (Math::abs(new_pos.x - previous_pos.x) > Math::abs(new_pos.y - previous_pos.y)) {
+ new_pos.y = previous_pos.y;
+ } else {
+ new_pos.x = previous_pos.x;
+ }
+ }
+
+ for (const Variant &var : drag_selection) {
+ CanvasItem *ci = Object::cast_to<CanvasItem>(var);
+ Transform2D parent_xform_inv = ci->get_transform() * ci->get_screen_transform().affine_inverse();
+ ci->_edit_set_position(ci->_edit_get_position() + parent_xform_inv.basis_xform(new_pos - previous_pos));
+ }
+ return true;
+ }
+
+ // Confirm the move (only if it was moved).
+ if (b.is_valid() && !b->is_pressed() && b->get_button_index() == MouseButton::LEFT) {
+ commit_drag();
+ return true;
+ }
+
+ if (_gui_input_cancel_drag(p_event, true, true)) {
+ return true;
+ }
+ }
+
+ // Move the canvas items with the arrow keys.
+ if (k.is_valid() && k->is_pressed() && (tool == TOOL_SELECT || tool == TOOL_MOVE) &&
+ (k->get_keycode() == Key::UP || k->get_keycode() == Key::DOWN || k->get_keycode() == Key::LEFT || k->get_keycode() == Key::RIGHT)) {
+ if (!k->is_echo()) {
+ // Start moving the canvas items with the keyboard, if they are movable.
+ Array selection;
+ get_selection_callback.call(selection);
+
+ drag_selection.clear();
+ // Remove unmovable nodes.
+ for (const Variant &var : selection) {
+ const Node *node = Object::cast_to<Node>(var);
+ if (is_node_movable(node, true)) {
+ drag_selection.push_back(var);
+ }
+ }
+
+ drag_type = DRAG_KEY_MOVE;
+ drag_from = Vector2();
+ drag_to = Vector2();
+ emit_signal("save_canvas_state_requested", drag_selection, true);
+ }
+
+ if (!drag_selection.is_empty()) {
+ emit_signal("restore_canvas_state_requested", drag_selection, true);
+
+ bool move_local_base = k->is_alt_pressed();
+ bool move_local_base_rotated = k->is_ctrl_pressed() || k->is_meta_pressed();
+
+ Vector2 dir;
+ if (k->get_keycode() == Key::UP) {
+ dir += Vector2(0, -1);
+ } else if (k->get_keycode() == Key::DOWN) {
+ dir += Vector2(0, 1);
+ } else if (k->get_keycode() == Key::LEFT) {
+ dir += Vector2(-1, 0);
+ } else if (k->get_keycode() == Key::RIGHT) {
+ dir += Vector2(1, 0);
+ }
+ if (k->is_shift_pressed()) {
+ dir *= grid_step * Math::pow(2.0, grid_step_multiplier);
+ }
+
+ drag_to += dir;
+ if (k->is_shift_pressed()) {
+ drag_to = drag_to.snapped(grid_step * Math::pow(2.0, grid_step_multiplier));
+ }
+
+ Point2 previous_pos;
+ if (drag_selection.size() == 1) {
+ CanvasItem *ci = Object::cast_to<CanvasItem>(drag_selection.front());
+ Transform2D xform = ci->get_global_transform_with_canvas() * ci->get_transform().affine_inverse();
+ previous_pos = xform.xform(ci->_edit_get_position());
+ } else {
+ previous_pos = _encompass_selection_rect(drag_selection).position;
+ }
+
+ Point2 new_pos;
+ if (drag_selection.size() == 1) {
+ Node2D *node_2d = Object::cast_to<Node2D>(drag_selection.front());
+ if (node_2d && move_local_base_rotated) {
+ Transform2D m2;
+ m2.rotate(node_2d->get_rotation());
+ new_pos += m2.xform(drag_to);
+ } else if (move_local_base) {
+ new_pos += drag_to;
+ } else {
+ new_pos = previous_pos + (drag_to - drag_from);
+ }
+ } else {
+ new_pos = previous_pos + (drag_to - drag_from);
+ }
+
+ for (const Variant &var : drag_selection) {
+ CanvasItem *ci = Object::cast_to<CanvasItem>(var);
+ Transform2D xform = ci->get_global_transform_with_canvas().affine_inverse() * ci->get_transform();
+ ci->_edit_set_position(ci->_edit_get_position() + xform.xform(new_pos) - xform.xform(previous_pos));
+ }
+ }
+ return true;
+ }
+
+ // Confirm canvas items move by arrow keys.
+ if (k.is_valid() && !k->is_pressed() && drag_type == DRAG_KEY_MOVE && (tool == TOOL_SELECT || tool == TOOL_MOVE) &&
+ (k->get_keycode() == Key::UP || k->get_keycode() == Key::DOWN || k->get_keycode() == Key::LEFT || k->get_keycode() == Key::RIGHT)) {
+ commit_drag();
+ return true;
+ }
+
+ // Accept the key event in any case.
+ return (k.is_valid() && (k->get_keycode() == Key::UP || k->get_keycode() == Key::DOWN || k->get_keycode() == Key::LEFT || k->get_keycode() == Key::RIGHT));
+}
+
+bool CanvasItemManipulator::_gui_input_anchors(const Ref<InputEvent> &p_event) {
+ Ref<InputEventMouseButton> b = p_event;
+ Ref<InputEventMouseMotion> m = p_event;
+
+ // Start anchor dragging.
+ if (drag_type == DRAG_NONE) {
+ if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && b->is_pressed() && tool == TOOL_SELECT) {
+ Array selection;
+ get_selection_callback.call(selection);
+
+ if (selection.size() == 1) {
+ Control *control = Object::cast_to<Control>(selection.front());
+ if (control && is_node_movable(control)) {
+ Vector2 anchor_pos[4];
+ anchor_pos[0] = Vector2(control->get_anchor(SIDE_LEFT), control->get_anchor(SIDE_TOP));
+ anchor_pos[1] = Vector2(control->get_anchor(SIDE_RIGHT), control->get_anchor(SIDE_TOP));
+ anchor_pos[2] = Vector2(control->get_anchor(SIDE_RIGHT), control->get_anchor(SIDE_BOTTOM));
+ anchor_pos[3] = Vector2(control->get_anchor(SIDE_LEFT), control->get_anchor(SIDE_BOTTOM));
+
+ Rect2 anchor_rects[4];
+ for (int i = 0; i < 4; i++) {
+ anchor_pos[i] = ((Transform2D)global_transform_callback.call() * control->get_screen_transform()).xform(anchor_to_position(control, anchor_pos[i]));
+ anchor_rects[i] = Rect2(anchor_pos[i], ANCHOR_HANDLE_SIZE);
+ if (control->is_layout_rtl()) {
+ anchor_rects[i].position -= ANCHOR_HANDLE_SIZE * Vector2(real_t(i == 1 || i == 2), real_t(i <= 1));
+ } else {
+ anchor_rects[i].position -= ANCHOR_HANDLE_SIZE * Vector2(real_t(i == 0 || i == 3), real_t(i <= 1));
+ }
+ }
+
+ const DragType dragger[] = {
+ DRAG_ANCHOR_TOP_LEFT,
+ DRAG_ANCHOR_TOP_RIGHT,
+ DRAG_ANCHOR_BOTTOM_RIGHT,
+ DRAG_ANCHOR_BOTTOM_LEFT,
+ };
+
+ for (int i = 0; i < 4; i++) {
+ if (anchor_rects[i].has_point(b->get_position())) {
+ if ((anchor_pos[0] == anchor_pos[2]) && (anchor_pos[0].distance_to(b->get_position()) < ANCHOR_HANDLE_SIZE.length() / 3.0)) {
+ drag_type = DRAG_ANCHOR_ALL;
+ } else {
+ drag_type = dragger[i];
+ }
+
+ drag_from = Transform2D(global_transform_callback.call()).affine_inverse().xform(b->get_position());
+ drag_selection.clear();
+ drag_selection.push_back(control);
+ emit_signal("save_canvas_state_requested", drag_selection, false);
+
+ return true;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ if (drag_type == DRAG_ANCHOR_TOP_LEFT || drag_type == DRAG_ANCHOR_TOP_RIGHT || drag_type == DRAG_ANCHOR_BOTTOM_RIGHT || drag_type == DRAG_ANCHOR_BOTTOM_LEFT || drag_type == DRAG_ANCHOR_ALL) {
+ // Drag the anchor.
+ if (m.is_valid()) {
+ if (!_validate_drag_selection()) {
+ drag_type = DRAG_NONE;
+ return true;
+ }
+
+ emit_signal("restore_canvas_state_requested", drag_selection, false);
+ Control *control = Object::cast_to<Control>(drag_selection.front());
+
+ drag_to = Transform2D(global_transform_callback.call()).affine_inverse().xform(m->get_position());
+
+ Transform2D xform = control->get_screen_transform().affine_inverse();
+
+ Point2 previous_anchor;
+ previous_anchor.x = (drag_type == DRAG_ANCHOR_TOP_LEFT || drag_type == DRAG_ANCHOR_BOTTOM_LEFT) ? control->get_anchor(SIDE_LEFT) : control->get_anchor(SIDE_RIGHT);
+ previous_anchor.y = (drag_type == DRAG_ANCHOR_TOP_LEFT || drag_type == DRAG_ANCHOR_TOP_RIGHT) ? control->get_anchor(SIDE_TOP) : control->get_anchor(SIDE_BOTTOM);
+ previous_anchor = xform.affine_inverse().xform(anchor_to_position(control, previous_anchor));
+
+ Vector2 new_anchor = xform.xform(snap_point(previous_anchor + (drag_to - drag_from), SNAP_GRID | SNAP_OTHER_NODES, SNAP_NODE_PARENT | SNAP_NODE_SIDES | SNAP_NODE_CENTER, control));
+ new_anchor = position_to_anchor(control, new_anchor).snappedf(0.001);
+
+ bool use_single_axis = m->is_shift_pressed();
+ Vector2 drag_vector = xform.xform(drag_to) - xform.xform(drag_from);
+ bool use_y = Math::abs(drag_vector.y) > Math::abs(drag_vector.x);
+
+ switch (drag_type) {
+ case DRAG_ANCHOR_TOP_LEFT:
+ if (!use_single_axis || !use_y) {
+ control->set_anchor(SIDE_LEFT, new_anchor.x, false, false);
+ }
+ if (!use_single_axis || use_y) {
+ control->set_anchor(SIDE_TOP, new_anchor.y, false, false);
+ }
+ break;
+ case DRAG_ANCHOR_TOP_RIGHT:
+ if (!use_single_axis || !use_y) {
+ control->set_anchor(SIDE_RIGHT, new_anchor.x, false, false);
+ }
+ if (!use_single_axis || use_y) {
+ control->set_anchor(SIDE_TOP, new_anchor.y, false, false);
+ }
+ break;
+ case DRAG_ANCHOR_BOTTOM_RIGHT:
+ if (!use_single_axis || !use_y) {
+ control->set_anchor(SIDE_RIGHT, new_anchor.x, false, false);
+ }
+ if (!use_single_axis || use_y) {
+ control->set_anchor(SIDE_BOTTOM, new_anchor.y, false, false);
+ }
+ break;
+ case DRAG_ANCHOR_BOTTOM_LEFT:
+ if (!use_single_axis || !use_y) {
+ control->set_anchor(SIDE_LEFT, new_anchor.x, false, false);
+ }
+ if (!use_single_axis || use_y) {
+ control->set_anchor(SIDE_BOTTOM, new_anchor.y, false, false);
+ }
+ break;
+ case DRAG_ANCHOR_ALL:
+ if (!use_single_axis || !use_y) {
+ control->set_anchor(SIDE_LEFT, new_anchor.x, false, true);
+ control->set_anchor(SIDE_RIGHT, new_anchor.x, false, true);
+ }
+ if (!use_single_axis || use_y) {
+ control->set_anchor(SIDE_TOP, new_anchor.y, false, true);
+ control->set_anchor(SIDE_BOTTOM, new_anchor.y, false, true);
+ }
+ break;
+ default:
+ break;
+ }
+ return true;
+ }
+
+ // Confirm anchor position.
+ if (drag_selection.size() >= 1 && b.is_valid() && b->get_button_index() == MouseButton::LEFT && !b->is_pressed()) {
+ commit_drag();
+ return true;
+ }
+
+ if (_gui_input_cancel_drag(p_event, true)) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+bool CanvasItemManipulator::_gui_input_ruler_tool(const Ref<InputEvent> &p_event) {
+ if (tool != TOOL_RULER) {
+ ruler_tool_active = false;
+ return false;
+ }
+
+ Ref<InputEventMouseButton> b = p_event;
+ Ref<InputEventMouseMotion> m = p_event;
+
+ Point2 previous_origin = ruler_tool_origin;
+ if (!ruler_tool_active) {
+ ruler_tool_origin = snap_point((Point2)local_mouse_pos_callback.call() / zoom + view_offset);
+ }
+
+ if (ruler_tool_active && b.is_valid() && b->get_button_index() == MouseButton::RIGHT) {
+ ruler_tool_active = false;
+ emit_signal(update_canvas_signal);
+ return true;
+ }
+
+ if (b.is_valid() && b->get_button_index() == MouseButton::LEFT) {
+ if (b->is_pressed()) {
+ ruler_tool_active = true;
+ } else {
+ ruler_tool_active = false;
+ }
+
+ emit_signal(update_canvas_signal);
+ return true;
+ }
+
+ if (m.is_valid() && (ruler_tool_active || (grid_snap && previous_origin != ruler_tool_origin))) {
+ emit_signal(update_canvas_signal);
+ return true;
+ }
+
+ return false;
+}
+
+bool CanvasItemManipulator::_gui_input_select(const Ref<InputEvent> &p_event) {
+ Ref<InputEventMouseButton> b = p_event;
+ Ref<InputEventMouseMotion> m = p_event;
+ Ref<InputEventKey> k = p_event;
+
+ if (drag_type == DRAG_NONE || (drag_type == DRAG_BOX_SELECTION && b.is_valid() && !b->is_pressed())) {
+ // Popup the selection menu list.
+ if (b.is_valid() && b->is_pressed() &&
+ ((b->get_button_index() == MouseButton::RIGHT && b->is_alt_pressed()) ||
+ (b->get_button_index() == MouseButton::LEFT && tool == TOOL_LIST_SELECT))) {
+ Vector<DebuggerHelpers::SelectResult> items;
+ get_canvas_items_at_pos(Transform2D(global_transform_callback.call()).affine_inverse().xform(b->get_position()), items, b->is_alt_pressed());
+
+ if (items.size() == 1) {
+ // Only one node below the cursor, just select it.
+ point_selected_callback.call(items[0].item, b->is_shift_pressed());
+ return true;
+ }
+
+ if (!items.is_empty()) {
+ items.sort(); // Sorts items according the their z-index.
+
+ Array nodes;
+ for (const DebuggerHelpers::SelectResult &item : items) {
+ nodes.append(item.item);
+ }
+ emit_signal("selection_menu_requested", nodes, b->get_position(), b->is_shift_pressed());
+
+ return true;
+ }
+ }
+
+ if (b.is_valid() && b->is_pressed() && b->get_button_index() == MouseButton::RIGHT && tool != TOOL_SCENE_PAINT) {
+ emit_signal("tool_menu_requested", viewport->get_screen_transform().xform(b->get_position()));
+ return true;
+ }
+
+ // Single item selection.
+ if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && (tool == TOOL_SELECT || tool == TOOL_MOVE || tool == TOOL_SCALE || tool == TOOL_ROTATE)) {
+ Point2 pos = Transform2D(global_transform_callback.call()).affine_inverse().xform(b->get_position());
+ // Allow selecting on release when performed very small box selection (necessary when Shift is pressed, see below).
+ if (b->is_pressed() || (drag_type == DRAG_BOX_SELECTION && pos.distance_to(drag_from) <= drag_threshold * scale)) {
+ Vector<DebuggerHelpers::SelectResult> items;
+ get_canvas_items_at_pos(pos, items);
+
+ CanvasItem *ci = nullptr;
+
+ // Retrieve the canvas items.
+ if (!items.is_empty()) {
+ items.sort(); // Sorts items according the their z-index.
+ ci = Object::cast_to<CanvasItem>(items[0].item);
+ }
+
+ // Shift also allows forcing box selection when item was clicked.
+ if (!ci || (b->is_shift_pressed() && b->is_pressed())) {
+ // Start a box selection.
+ if (!b->is_shift_pressed()) {
+ // Clear the selection if not additive.
+ emit_signal("clear_selection_requested", true);
+ }
+
+ if (b->is_pressed()) {
+ drag_from = pos;
+ drag_type = DRAG_BOX_SELECTION;
+ box_selecting_to = drag_from;
+ return true;
+ }
+ } else {
+ bool selected = point_selected_callback.call(ci, b->is_shift_pressed());
+ // Start dragging.
+ if (selected && (tool == TOOL_SELECT || tool == TOOL_MOVE) && b->is_pressed()) {
+ // Drag the node(s) if requested.
+ drag_start_origin = pos;
+ drag_type = DRAG_QUEUED;
+ } else if (!b->is_pressed()) {
+ reset_drag();
+ }
+
+ return true; // Selected the item.
+ }
+ }
+ }
+ }
+
+ if (drag_type == DRAG_QUEUED) {
+ if (b.is_valid() && !b->is_pressed()) {
+ reset_drag();
+ return true;
+ }
+
+ if (m.is_valid()) {
+ Point2 pos = Transform2D(global_transform_callback.call()).affine_inverse().xform(m->get_position());
+ bool movement_threshold_passed = drag_start_origin.distance_to(pos) > (8 * MAX(1, scale)) / zoom;
+ if (m.is_valid() && movement_threshold_passed) {
+ Array selection;
+ get_selection_callback.call(selection);
+
+ drag_selection.clear();
+ for (const Variant &var : selection) {
+ const Node *node = Object::cast_to<Node>(var);
+ if (is_node_movable(node, true)) {
+ drag_selection.push_back(var);
+ }
+ }
+
+ if (!selection.is_empty()) {
+ drag_type = DRAG_MOVE;
+ drag_from = drag_start_origin;
+ emit_signal("save_canvas_state_requested", drag_selection, false);
+ }
+
+ return true;
+ }
+ }
+ }
+
+ if (drag_type == DRAG_BOX_SELECTION) {
+ // Update box area.
+ Point2 bsfrom = drag_from;
+ Point2 bsto = box_selecting_to;
+ if (bsfrom.x > bsto.x) {
+ SWAP(bsfrom.x, bsto.x);
+ }
+ if (bsfrom.y > bsto.y) {
+ SWAP(bsfrom.y, bsto.y);
+ }
+ Rect2 area(bsfrom, bsto - bsfrom);
+
+ // Confirm box selection.
+ if (b.is_valid() && !b->is_pressed() && b->get_button_index() == MouseButton::LEFT) {
+ Node *start = Object::cast_to<Node>(find_items_start_callback.call());
+ if (start) {
+ Vector<DebuggerHelpers::SelectResult> items;
+ find_canvas_items_in_rect(area, start, items);
+
+ if (!items.is_empty()) {
+ Array nodes;
+ for (const DebuggerHelpers::SelectResult &item : items) {
+ nodes.append(item.item);
+ }
+ emit_signal("box_selected", nodes);
+ }
+ }
+
+ reset_drag();
+ emit_signal("box_selection_updated", Rect2());
+ return true;
+ }
+
+ // Cancel box selection.
+ if (b.is_valid() && b->is_pressed() && b->get_button_index() == MouseButton::RIGHT) {
+ reset_drag();
+ emit_signal("box_selection_updated", Rect2());
+ return true;
+ }
+
+ // Update box selection.
+ if (m.is_valid()) {
+ box_selecting_to = Transform2D(global_transform_callback.call()).affine_inverse().xform(m->get_position());
+ emit_signal("box_selection_updated", area);
+ return true;
+ }
+ }
+
+ // Unselect everything.
+ if (k.is_valid() && k->is_action_pressed(SNAME("ui_cancel"), false, true) && drag_type == DRAG_NONE) {
+ emit_signal("clear_selection_requested", false);
+ }
+
+ return false;
+}
+
+bool CanvasItemManipulator::_gui_input_cancel_drag(const Ref<InputEvent> &p_event, bool p_reset_snap_targets, bool p_restore_bones) {
+ Ref<InputEventMouseButton> b = p_event;
+ if ((b.is_valid() && b->is_pressed() && b->get_button_index() == MouseButton::RIGHT) || DebuggerHelpers::is_shortcut_pressed(SHORTCUT_CANCEL_TRANSFORM, inputs)) {
+ if (p_reset_snap_targets) {
+ snap_target[0] = SNAP_TARGET_NONE;
+ snap_target[1] = SNAP_TARGET_NONE;
+ }
+
+ emit_signal("restore_canvas_state_requested", drag_selection, p_restore_bones);
+ reset_drag();
+ emit_signal(update_canvas_signal);
+
+ return true;
+ }
+
+ return false;
+}
+
+bool CanvasItemManipulator::_validate_drag_selection() {
+ // Check if all nodes are still available.
+ for (int i = 0; i < drag_selection.size(); i++) {
+ if (!drag_selection[i]) {
+ drag_selection.remove_at(i);
+ i--;
+ }
+ }
+
+ return !drag_selection.is_empty();
+}
+
+Rect2 CanvasItemManipulator::_encompass_selection_rect(const Array &p_selected) {
+ ERR_FAIL_COND_V(p_selected.is_empty(), Rect2());
+
+ // Handles the first element.
+ CanvasItem *ci = Object::cast_to<CanvasItem>(p_selected.front());
+ Rect2 rect = Rect2(ci->get_global_transform_with_canvas().xform(ci->_edit_get_rect().get_center()), Size2());
+
+ // Expand with the other ones.
+ for (const Variant &var : p_selected) {
+ CanvasItem *ci2 = Object::cast_to<CanvasItem>(var);
+ Transform2D xform = ci2->get_global_transform_with_canvas();
+ Rect2 current_rect = ci2->_edit_get_rect();
+
+ rect.expand_to(xform.xform(current_rect.position));
+ rect.expand_to(xform.xform(current_rect.position + Vector2(current_rect.size.x, 0)));
+ rect.expand_to(xform.xform(current_rect.position + current_rect.size));
+ rect.expand_to(xform.xform(current_rect.position + Vector2(0, current_rect.size.y)));
+ }
+
+ return rect;
+}
+
+Point2 CanvasItemManipulator::anchor_to_position(const Control *p_control, const Vector2 p_anchor) {
+ ERR_FAIL_NULL_V(p_control, Point2());
+
+ Transform2D parent_transform = p_control->get_transform().affine_inverse();
+ Rect2 parent_rect = p_control->get_parent_anchorable_rect();
+
+ if (p_control->is_layout_rtl()) {
+ return parent_transform.xform(parent_rect.position + Point2(parent_rect.size.x - parent_rect.size.x * p_anchor.x, parent_rect.size.y * p_anchor.y));
+ } else {
+ return parent_transform.xform(parent_rect.position + Point2(parent_rect.size.x * p_anchor.x, parent_rect.size.y * p_anchor.y));
+ }
+}
+
+Point2 CanvasItemManipulator::position_to_anchor(const Control *p_control, const Point2 &p_position) {
+ ERR_FAIL_NULL_V(p_control, Vector2());
+
+ Rect2 parent_rect = p_control->get_parent_anchorable_rect();
+
+ Vector2 output;
+ if (p_control->is_layout_rtl()) {
+ output.x = (parent_rect.size.x == 0) ? 0.0 : (parent_rect.size.x - p_control->get_transform().xform(p_position).x - parent_rect.position.x) / parent_rect.size.x;
+ } else {
+ output.x = (parent_rect.size.x == 0) ? 0.0 : (p_control->get_transform().xform(p_position).x - parent_rect.position.x) / parent_rect.size.x;
+ }
+ output.y = (parent_rect.size.y == 0) ? 0.0 : (p_control->get_transform().xform(p_position).y - parent_rect.position.y) / parent_rect.size.y;
+ return output;
+}
+
+void CanvasItemManipulator::_snap_if_closer_float(
+ const real_t p_value,
+ real_t &r_current_snap, SnapTarget &r_current_snap_target,
+ const real_t p_target_value, const SnapTarget p_snap_target,
+ const real_t p_radius) {
+ const real_t radius = p_radius / zoom;
+ const real_t dist = Math::abs(p_value - p_target_value);
+ if ((p_radius < 0 || dist < radius) && (r_current_snap_target == SNAP_TARGET_NONE || dist < Math::abs(r_current_snap - p_value))) {
+ r_current_snap = p_target_value;
+ r_current_snap_target = p_snap_target;
+ }
+}
+
+void CanvasItemManipulator::_snap_if_closer_point(
+ Point2 p_value,
+ Point2 &r_current_snap, SnapTarget (&r_current_snap_target)[2],
+ Point2 p_target_value, const SnapTarget p_snap_target,
+ const real_t rotation,
+ const real_t p_radius) {
+ Transform2D rot_trans = Transform2D(rotation, Point2());
+ p_value = rot_trans.inverse().xform(p_value);
+ p_target_value = rot_trans.inverse().xform(p_target_value);
+ r_current_snap = rot_trans.inverse().xform(r_current_snap);
+
+ _snap_if_closer_float(
+ p_value.x,
+ r_current_snap.x,
+ r_current_snap_target[0],
+ p_target_value.x,
+ p_snap_target,
+ p_radius);
+
+ _snap_if_closer_float(
+ p_value.y,
+ r_current_snap.y,
+ r_current_snap_target[1],
+ p_target_value.y,
+ p_snap_target,
+ p_radius);
+
+ r_current_snap = rot_trans.xform(r_current_snap);
+}
+
+void CanvasItemManipulator::_snap_other_nodes(
+ const Point2 p_value,
+ const Transform2D p_transform_to_snap,
+ Point2 &r_current_snap, SnapTarget (&r_current_snap_target)[2],
+ const SnapTarget p_snap_target, const Array &p_exceptions,
+ const Node *p_current) {
+ const CanvasItem *ci = Object::cast_to<CanvasItem>(p_current);
+
+ // Check if the element is in the exception
+ bool exception = false;
+ for (const Variant &var : p_exceptions) {
+ const CanvasItem *ex = Object::cast_to<CanvasItem>(var);
+ if (ex == p_current) {
+ exception = true;
+ break;
+ }
+ }
+
+ if (ci && !exception) {
+ Transform2D ci_transform = ci->get_screen_transform();
+ if (Math::fmod(ci_transform.get_rotation() - p_transform_to_snap.get_rotation(), (real_t)360.0) == 0.0) {
+ if (ci->_edit_use_rect()) {
+ Point2 begin = ci_transform.xform(ci->_edit_get_rect().get_position());
+ Point2 end = ci_transform.xform(ci->_edit_get_rect().get_position() + ci->_edit_get_rect().get_size());
+
+ _snap_if_closer_point(p_value, r_current_snap, r_current_snap_target, begin, p_snap_target, ci_transform.get_rotation());
+ _snap_if_closer_point(p_value, r_current_snap, r_current_snap_target, end, p_snap_target, ci_transform.get_rotation());
+ } else {
+ Point2 position = ci_transform.xform(Point2());
+ _snap_if_closer_point(p_value, r_current_snap, r_current_snap_target, position, p_snap_target, ci_transform.get_rotation());
+ }
+ }
+ }
+ for (const Node *node : p_current->iterate_children()) {
+ _snap_other_nodes(p_value, p_transform_to_snap, r_current_snap, r_current_snap_target, p_snap_target, p_exceptions, node);
+ }
+}
+
+Point2 CanvasItemManipulator::snap_point(Point2 p_target, unsigned int p_modes, unsigned int p_forced_modes, const CanvasItem *p_self_canvas_item, const Array &p_other_nodes_exceptions) {
+ snap_target[0] = SNAP_TARGET_NONE;
+ snap_target[1] = SNAP_TARGET_NONE;
+
+ bool is_snap_active = smart_snap != Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL);
+
+ // Smart snap using the canvas position.
+ Vector2 output = p_target;
+ real_t rotation = 0.0;
+
+ if (p_self_canvas_item) {
+ rotation = p_self_canvas_item->get_screen_transform().get_rotation();
+
+ // Parent sides and center.
+ if ((is_snap_active && (snap_mode & SNAP_NODE_PARENT) && (p_modes & SNAP_NODE_PARENT)) || (p_forced_modes & SNAP_NODE_PARENT)) {
+ if (const Control *c = Object::cast_to<Control>(p_self_canvas_item)) {
+ Point2 begin = p_self_canvas_item->get_screen_transform().xform(anchor_to_position(c, Point2(0, 0)));
+ Point2 end = p_self_canvas_item->get_screen_transform().xform(anchor_to_position(c, Point2(1, 1)));
+ _snap_if_closer_point(p_target, output, snap_target, begin, SNAP_TARGET_PARENT, rotation);
+ _snap_if_closer_point(p_target, output, snap_target, (begin + end) / 2.0, SNAP_TARGET_PARENT, rotation);
+ _snap_if_closer_point(p_target, output, snap_target, end, SNAP_TARGET_PARENT, rotation);
+ } else if (const CanvasItem *parent_ci = Object::cast_to<CanvasItem>(p_self_canvas_item->get_parent())) {
+ if (parent_ci->_edit_use_rect()) {
+ Point2 begin = p_self_canvas_item->get_transform().affine_inverse().xform(parent_ci->_edit_get_rect().get_position());
+ Point2 end = p_self_canvas_item->get_transform().affine_inverse().xform(parent_ci->_edit_get_rect().get_position() + parent_ci->_edit_get_rect().get_size());
+ _snap_if_closer_point(p_target, output, snap_target, begin, SNAP_TARGET_PARENT, rotation);
+ _snap_if_closer_point(p_target, output, snap_target, (begin + end) / 2.0, SNAP_TARGET_PARENT, rotation);
+ _snap_if_closer_point(p_target, output, snap_target, end, SNAP_TARGET_PARENT, rotation);
+ } else {
+ Point2 position = p_self_canvas_item->get_transform().affine_inverse().xform(Point2());
+ _snap_if_closer_point(p_target, output, snap_target, position, SNAP_TARGET_PARENT, rotation);
+ }
+ }
+ }
+
+ // Self anchors.
+ if ((is_snap_active && (snap_mode & SNAP_NODE_ANCHORS) && (p_modes & SNAP_NODE_ANCHORS)) || (p_forced_modes & SNAP_NODE_ANCHORS)) {
+ if (const Control *c = Object::cast_to<Control>(p_self_canvas_item)) {
+ Point2 begin = p_self_canvas_item->get_screen_transform().xform(anchor_to_position(c, Point2(c->get_anchor(SIDE_LEFT), c->get_anchor(SIDE_TOP))));
+ Point2 end = p_self_canvas_item->get_screen_transform().xform(anchor_to_position(c, Point2(c->get_anchor(SIDE_RIGHT), c->get_anchor(SIDE_BOTTOM))));
+ _snap_if_closer_point(p_target, output, snap_target, begin, SNAP_TARGET_SELF_ANCHORS, rotation);
+ _snap_if_closer_point(p_target, output, snap_target, end, SNAP_TARGET_SELF_ANCHORS, rotation);
+ }
+ }
+
+ // Self sides.
+ if ((is_snap_active && (snap_mode & SNAP_NODE_SIDES) && (p_modes & SNAP_NODE_SIDES)) || (p_forced_modes & SNAP_NODE_SIDES)) {
+ if (p_self_canvas_item->_edit_use_rect()) {
+ Point2 begin = p_self_canvas_item->get_screen_transform().xform(p_self_canvas_item->_edit_get_rect().get_position());
+ Point2 end = p_self_canvas_item->get_screen_transform().xform(p_self_canvas_item->_edit_get_rect().get_position() + p_self_canvas_item->_edit_get_rect().get_size());
+ _snap_if_closer_point(p_target, output, snap_target, begin, SNAP_TARGET_SELF, rotation);
+ _snap_if_closer_point(p_target, output, snap_target, end, SNAP_TARGET_SELF, rotation);
+ }
+ }
+
+ // Self center.
+ if ((is_snap_active && (snap_mode & SNAP_NODE_CENTER) && (p_modes & SNAP_NODE_CENTER)) || (p_forced_modes & SNAP_NODE_CENTER)) {
+ if (p_self_canvas_item->_edit_use_rect()) {
+ Point2 center = p_self_canvas_item->get_screen_transform().xform(p_self_canvas_item->_edit_get_rect().get_center());
+ _snap_if_closer_point(p_target, output, snap_target, center, SNAP_TARGET_SELF, rotation);
+ } else {
+ Point2 position = p_self_canvas_item->get_screen_transform().xform(Point2());
+ _snap_if_closer_point(p_target, output, snap_target, position, SNAP_TARGET_SELF, rotation);
+ }
+ }
+ }
+
+ // Other nodes sides.
+ if ((is_snap_active && (snap_mode & SNAP_OTHER_NODES) && (p_modes & SNAP_OTHER_NODES)) || (p_forced_modes & SNAP_OTHER_NODES)) {
+ Transform2D to_snap_transform;
+ Array exceptions;
+ for (const Variant &var : p_other_nodes_exceptions) {
+ exceptions.push_back(var);
+ }
+ if (p_self_canvas_item) {
+ exceptions.push_back(p_self_canvas_item);
+ to_snap_transform = p_self_canvas_item->get_screen_transform();
+ }
+
+ _snap_other_nodes(
+ p_target, to_snap_transform,
+ output, snap_target,
+ SNAP_TARGET_OTHER_NODE,
+ exceptions,
+ SceneTree::get_singleton()->get_edited_scene_root());
+ }
+
+ // Guides.
+ if (((is_snap_active && (snap_mode & SNAP_GUIDES) && (p_modes & SNAP_GUIDES)) || (p_forced_modes & SNAP_GUIDES)) && Math::fmod(rotation, (real_t)360.0) == 0.0) {
+ Node *start = Object::cast_to<Node>(find_items_start_callback.call());
+ if (start) {
+ Array vguides = start->get_meta("_edit_vertical_guides_", Array());
+ for (int i = 0; i < vguides.size(); i++) {
+ _snap_if_closer_float(p_target.x, output.x, snap_target[0], vguides[i], SNAP_TARGET_GUIDE);
+ }
+
+ Array hguides = start->get_meta("_edit_horizontal_guides_", Array());
+ for (int i = 0; i < hguides.size(); i++) {
+ _snap_if_closer_float(p_target.y, output.y, snap_target[1], hguides[i], SNAP_TARGET_GUIDE);
+ }
+ }
+ }
+
+ // Grid.
+ if (((grid_snap && (p_modes & SNAP_GRID)) || (p_forced_modes & SNAP_GRID)) && Math::fmod(rotation, (real_t)360.0) == 0.0) {
+ Point2 offset = grid_offset;
+ if (snap_relative) {
+ if (drag_selection.size() == 1) {
+ const Node2D *n2d = Object::cast_to<Node2D>(drag_selection.front());
+ offset = n2d->get_global_position();
+ } else if (!drag_selection.is_empty()) {
+ offset = _encompass_selection_rect(drag_selection).position;
+ }
+ }
+
+ Point2 grid_output;
+ grid_output.x = Math::snapped(p_target.x - offset.x, grid_step.x * Math::pow(2.0, grid_step_multiplier)) + offset.x;
+ grid_output.y = Math::snapped(p_target.y - offset.y, grid_step.y * Math::pow(2.0, grid_step_multiplier)) + offset.y;
+ _snap_if_closer_point(p_target, output, snap_target, grid_output, SNAP_TARGET_GRID, 0.0, -1.0);
+ }
+
+ // Pixel.
+ if ((((snap_mode & SNAP_PIXEL) && (p_modes & SNAP_PIXEL)) || (p_forced_modes & SNAP_PIXEL)) && rotation == 0.0) {
+ output = output.snappedf(1);
+ }
+
+ snap_transform = Transform2D(rotation, output);
+
+ return output;
+}
+
+void CanvasItemManipulator::commit_drag() {
+ if (_validate_drag_selection()) {
+ const CanvasItem *ci = Object::cast_to<CanvasItem>(drag_selection.front());
+
+ switch (drag_type) {
+ // Confirm the pivot move.
+ case DRAG_PIVOT: {
+ emit_signal("commit_canvas_state_requested",
+ drag_selection,
+ vformat(RTR("Set CanvasItem \"%s\" Pivot Offset to (%d, %d)"),
+ ci->get_name(),
+ ci->_edit_get_pivot().x,
+ ci->_edit_get_pivot().y),
+ false);
+ } break;
+
+ // Confirm the node rotation.
+ case DRAG_ROTATE: {
+ if (drag_selection.size() != 1) {
+ emit_signal("commit_canvas_state_requested",
+ drag_selection,
+ vformat(RTR("Rotate %d CanvasItems"), drag_selection.size()),
+ true);
+ } else {
+ emit_signal("commit_canvas_state_requested",
+ drag_selection,
+ vformat(RTR("Rotate CanvasItem \"%s\" to %d degrees"),
+ ci->get_name(),
+ Math::rad_to_deg(ci->_edit_get_rotation())),
+ true);
+ }
+ } break;
+
+ // Confirm new anchor position.
+ case DRAG_ANCHOR_TOP_LEFT:
+ case DRAG_ANCHOR_TOP_RIGHT:
+ case DRAG_ANCHOR_BOTTOM_RIGHT:
+ case DRAG_ANCHOR_BOTTOM_LEFT:
+ case DRAG_ANCHOR_ALL: {
+ emit_signal("commit_canvas_state_requested",
+ drag_selection,
+ vformat(RTR("Move CanvasItem \"%s\" Anchor"), ci->get_name()),
+ false);
+
+ snap_target[0] = SNAP_TARGET_NONE;
+ snap_target[1] = SNAP_TARGET_NONE;
+ } break;
+
+ // Confirm resize.
+ case DRAG_LEFT:
+ case DRAG_RIGHT:
+ case DRAG_TOP:
+ case DRAG_BOTTOM:
+ case DRAG_TOP_LEFT:
+ case DRAG_TOP_RIGHT:
+ case DRAG_BOTTOM_LEFT:
+ case DRAG_BOTTOM_RIGHT: {
+ const Node2D *node2d = Object::cast_to<Node2D>(ci);
+ if (node2d) {
+ // Extends from Node2D.
+ // Node2D doesn't have an actual stored rect size, unlike Controls.
+ emit_signal("commit_canvas_state_requested",
+ drag_selection,
+ vformat(RTR("Scale Node2D \"%s\" to (%s, %s)"),
+ ci->get_name(),
+ Math::snapped(ci->_edit_get_scale().x, 0.01),
+ Math::snapped(ci->_edit_get_scale().y, 0.01)),
+ true);
+ } else {
+ // Extends from Control.
+ emit_signal("commit_canvas_state_requested",
+ drag_selection,
+ vformat(
+ RTR("Resize Control \"%s\" to (%d, %d)"),
+ ci->get_name(),
+ ci->_edit_get_rect().size.x,
+ ci->_edit_get_rect().size.y),
+ true);
+ }
+
+ snap_target[0] = SNAP_TARGET_NONE;
+ snap_target[1] = SNAP_TARGET_NONE;
+ } break;
+
+ // Confirm resize.
+ case DRAG_SCALE_BOTH:
+ case DRAG_SCALE_X:
+ case DRAG_SCALE_Y: {
+ if (drag_selection.size() != 1) {
+ emit_signal("commit_canvas_state_requested",
+ drag_selection,
+ vformat(RTR("Scale %d CanvasItems"), drag_selection.size()),
+ true);
+ } else {
+ emit_signal("commit_canvas_state_requested",
+ drag_selection,
+ vformat(RTR("Scale CanvasItem \"%s\" to (%s, %s)"),
+ ci->get_name(),
+ Math::snapped(ci->_edit_get_scale().x, 0.01),
+ Math::snapped(ci->_edit_get_scale().y, 0.01)),
+ true);
+ }
+ } break;
+
+ // Confirm the canvas items move.
+ case DRAG_MOVE:
+ case DRAG_MOVE_X:
+ case DRAG_MOVE_Y: {
+ ERR_FAIL_NULL(viewport);
+ if (Transform2D(global_transform_callback.call()).affine_inverse().xform(viewport->get_mouse_position()) != drag_from) {
+ if (drag_selection.size() != 1) {
+ emit_signal("commit_canvas_state_requested",
+ drag_selection,
+ vformat(RTR("Move %d CanvasItems"), drag_selection.size()),
+ true);
+ } else {
+ emit_signal("commit_canvas_state_requested",
+ drag_selection,
+ vformat(
+ RTR("Move CanvasItem \"%s\" to (%d, %d)"),
+ ci->get_name(),
+ ci->_edit_get_position().x,
+ ci->_edit_get_position().y),
+ true);
+ }
+ }
+
+ // Make sure smart snapping lines disappear.
+ snap_target[0] = SNAP_TARGET_NONE;
+ snap_target[1] = SNAP_TARGET_NONE;
+ } break;
+
+ // Confirm the canvas items move by arrow keys.
+ case DRAG_KEY_MOVE: {
+ if (tool != TOOL_SELECT && tool != TOOL_MOVE) {
+ return;
+ }
+
+ if (drag_selection.size() > 1) {
+ emit_signal("commit_canvas_state_requested",
+ drag_selection,
+ vformat(RTR("Move %d CanvasItems"), drag_selection.size()),
+ true);
+ } else if (drag_selection.size() == 1) {
+ emit_signal("commit_canvas_state_requested",
+ drag_selection,
+ vformat(RTR("Move CanvasItem \"%s\" to (%d, %d)"),
+ ci->get_name(),
+ ci->_edit_get_position().x,
+ ci->_edit_get_position().y),
+ true);
+ }
+ } break;
+
+ default:
+ break;
+ }
+ }
+
+ reset_drag();
+ emit_signal(update_canvas_signal);
+}
+
+bool CanvasItemManipulator::is_node_locked(const Node *p_node) {
+ return p_node->get_meta("_edit_lock_", false);
+}
+
+bool CanvasItemManipulator::is_node_movable(const Node *p_node, bool p_popup_warning) {
+ if (is_node_locked(p_node)) {
+ return false;
+ }
+
+ if (Object::cast_to<Control>(p_node) && Object::cast_to<Container>(p_node->get_parent())) {
+ if (p_popup_warning) {
+ emit_signal("unmovable_items_warn_requested", TTRC("Children of a container get their position and size determined only by their parent."));
+ }
+ return false;
+ }
+
+ return true;
+}
+
+void CanvasItemManipulator::_update_cursor_shape() {
+ Input::CursorShape new_cursor = Input::CursorShape::CURSOR_ARROW;
+ switch (tool) {
+ case TOOL_MOVE: {
+ new_cursor = Input::CursorShape::CURSOR_MOVE;
+ } break;
+
+ case TOOL_EDIT_PIVOT: {
+ new_cursor = Input::CursorShape::CURSOR_CROSS;
+ } break;
+
+ case TOOL_PAN: {
+ new_cursor = Input::CursorShape::CURSOR_DRAG;
+ } break;
+
+ case TOOL_RULER: {
+ new_cursor = Input::CursorShape::CURSOR_CROSS;
+ } break;
+
+ default:
+ break;
+ }
+
+ if (drag_type == DRAG_NONE) {
+ if (is_hovering_h_guide) {
+ new_cursor = Input::CursorShape::CURSOR_VSIZE;
+ } else if (is_hovering_v_guide) {
+ new_cursor = Input::CursorShape::CURSOR_HSIZE;
+ }
+ } else {
+ // Compute an eventual rotation of the cursor.
+ const Input::CursorShape rotation_array[4] = {
+ Input::CursorShape::CURSOR_HSIZE,
+ Input::CursorShape::CURSOR_BDIAGSIZE,
+ Input::CursorShape::CURSOR_VSIZE,
+ Input::CursorShape::CURSOR_FDIAGSIZE,
+ };
+ int rotation_array_index = 0;
+
+ if (drag_selection.size() == 1) {
+ const CanvasItem *ci = Object::cast_to<CanvasItem>(drag_selection.front());
+ const double angle = Math::fposmod((double)ci->get_global_transform_with_canvas().get_rotation(), Math::PI);
+ if (angle > Math::PI * 7.0 / 8.0) {
+ rotation_array_index = 0;
+ } else if (angle > Math::PI * 5.0 / 8.0) {
+ rotation_array_index = 1;
+ } else if (angle > Math::PI * 3.0 / 8.0) {
+ rotation_array_index = 2;
+ } else if (angle > Math::PI * 1.0 / 8.0) {
+ rotation_array_index = 3;
+ } else {
+ rotation_array_index = 0;
+ }
+ }
+
+ switch (drag_type) {
+ case DRAG_LEFT:
+ case DRAG_RIGHT: {
+ new_cursor = rotation_array[rotation_array_index];
+ } break;
+
+ case DRAG_V_GUIDE: {
+ new_cursor = Input::CursorShape::CURSOR_HSIZE;
+ } break;
+
+ case DRAG_TOP:
+ case DRAG_BOTTOM: {
+ new_cursor = rotation_array[(rotation_array_index + 2) % 4];
+ } break;
+
+ case DRAG_H_GUIDE: {
+ new_cursor = Input::CursorShape::CURSOR_VSIZE;
+ } break;
+
+ case DRAG_TOP_LEFT:
+ case DRAG_BOTTOM_RIGHT: {
+ new_cursor = rotation_array[(rotation_array_index + 3) % 4];
+ } break;
+
+ case DRAG_DOUBLE_GUIDE: {
+ new_cursor = Input::CursorShape::CURSOR_FDIAGSIZE;
+ } break;
+
+ case DRAG_TOP_RIGHT:
+ case DRAG_BOTTOM_LEFT: {
+ new_cursor = rotation_array[(rotation_array_index + 1) % 4];
+ } break;
+
+ case DRAG_MOVE: {
+ new_cursor = Input::CursorShape::CURSOR_MOVE;
+ } break;
+
+ default:
+ break;
+ }
+ }
+
+ if (cursor_shape != new_cursor) {
+ cursor_shape = new_cursor;
+ emit_signal("cursor_shape_changed");
+ }
+}
+
+void CanvasItemManipulator::_emit_locked_warn_signal() {
+ emit_signal("warning_emitted", TTRC("All selected CanvasItems are either invisible or locked in some way and can't be transformed."));
+}
+
+void CanvasItemManipulator::get_canvas_items_at_pos(const Point2 &p_pos, Vector<DebuggerHelpers::SelectResult> &r_items, bool p_allow_locked) {
+ Node *start = Object::cast_to<Node>(find_items_start_callback.call());
+ if (!start) {
+ return;
+ }
+
+ 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()) {
+ node = start->get_deepest_editable_node(node);
+ }
+
+ CanvasItem *ci = Object::cast_to<CanvasItem>(node);
+ if (!ci) {
+ r_items.remove_at(i);
+ i--;
+ continue;
+ }
+
+ if (!p_allow_locked) {
+ // Replace the node by the group if grouped.
+ while (node && node != start->get_parent()) {
+ CanvasItem *ci_tmp = Object::cast_to<CanvasItem>(node);
+ if (ci_tmp && node->has_meta("_edit_group_")) {
+ ci = ci_tmp;
+ }
+ node = node->get_parent();
+ }
+ }
+
+ // Check if the canvas item is already in the list (for groups or scenes).
+ bool duplicate = false;
+ for (int j = 0; j < i; j++) {
+ if (r_items[j].item == ci) {
+ duplicate = true;
+ break;
+ }
+ }
+
+ // Remove the item if invalid.
+ bool in_editor_scene = is_editor && 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--;
+ } else {
+ r_items.write[i].item = ci;
+ }
+ }
+}
+
+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;
+
+ 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().has_point(xform.affine_inverse().xform(p_pos))) {
+ return;
+ }
+ }
+ }
+
+ CanvasItem *ci = Object::cast_to<CanvasItem>(p_node);
+
+ for (int i = p_node->get_child_count() - 1; i >= 0; i--) {
+ if (ci) {
+ if (!ci->is_set_as_top_level()) {
+ find_canvas_items_at_pos(p_pos, p_node->get_child(i), r_items, p_parent_xform * ci->get_transform(), xform);
+ } else {
+ find_canvas_items_at_pos(p_pos, p_node->get_child(i), r_items, ci->get_transform(), xform);
+ }
+ } else {
+ find_canvas_items_at_pos(p_pos, p_node->get_child(i), r_items, Transform2D(), xform);
+ }
+ }
+
+ if (!ci || !ci->is_visible_in_tree()) {
+ return;
+ }
+
+ if (!ci->is_set_as_top_level()) {
+ xform *= p_parent_xform;
+ }
+
+ Point2 pos = p_pos;
+
+ // 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())) {
+ Window *root = SceneTree::get_singleton()->get_root();
+ pos = root->get_canvas_transform().affine_inverse().xform(p_pos);
+ }
+
+ xform = (xform * ci->get_transform()).affine_inverse();
+ const real_t local_grab_distance = xform.basis_xform(Vector2(grab_distance, 0)).length() / zoom;
+ if (ci->_edit_is_selected_on_click(xform.xform(pos), local_grab_distance)) {
+ Node2D *node = Object::cast_to<Node2D>(ci);
+
+ DebuggerHelpers::SelectResult res;
+ res.item = ci;
+ res.has_order = node;
+ if (is_editor) {
+ res.order = node ? node->get_z_index() : 0;
+ } else {
+ res.order = ci->get_effective_z_index() + ci->get_canvas_layer();
+ }
+
+ r_items.push_back(res);
+ }
+}
+
+void CanvasItemManipulator::find_canvas_items_in_rect(const Rect2 &p_rect, Node *p_node, Vector<DebuggerHelpers::SelectResult> &r_items, const Transform2D &p_parent_xform, const Transform2D &p_canvas_xform) {
+ if (!p_node) {
+ return;
+ }
+
+ bool is_editor = Engine::get_singleton()->is_editor_hint();
+ 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.
+ }
+
+ Transform2D xform = p_canvas_xform;
+
+ 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;
+ }
+ }
+ }
+
+ CanvasItem *ci = Object::cast_to<CanvasItem>(p_node);
+
+ bool editable = true;
+ if (is_editor) {
+ 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);
+ }
+
+ bool lock_children = p_node->get_meta("_edit_group_", false);
+ bool locked = is_node_locked(p_node);
+
+ if (!lock_children || !editable) {
+ for (int i = p_node->get_child_count() - 1; i >= 0; i--) {
+ if (ci) {
+ if (!ci->is_set_as_top_level()) {
+ find_canvas_items_in_rect(p_rect, p_node->get_child(i), r_items, p_parent_xform * ci->get_transform(), p_canvas_xform);
+ } else {
+ find_canvas_items_in_rect(p_rect, p_node->get_child(i), r_items, ci->get_transform(), p_canvas_xform);
+ }
+ } else {
+ CanvasLayer *cl = Object::cast_to<CanvasLayer>(p_node);
+ find_canvas_items_in_rect(p_rect, p_node->get_child(i), r_items, Transform2D(), cl ? cl->get_transform() : p_canvas_xform);
+ }
+ }
+ }
+
+ if (!ci || !ci->is_visible_in_tree() || locked || !editable) {
+ return;
+ }
+
+ if (!ci->is_set_as_top_level()) {
+ xform *= p_parent_xform;
+ }
+
+ Rect2 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())) {
+ Window *root = SceneTree::get_singleton()->get_root();
+ rect = root->get_canvas_transform().affine_inverse().xform(p_rect);
+ }
+ rect = (xform * ci->get_transform()).affine_inverse().xform(rect);
+
+ bool selected = false;
+ if (ci->_edit_use_rect()) {
+ Rect2 ci_rect = ci->_edit_get_rect();
+ if (rect.has_point(ci_rect.position) &&
+ rect.has_point(ci_rect.position + Vector2(ci_rect.size.x, 0)) &&
+ rect.has_point(ci_rect.position + Vector2(ci_rect.size.x, ci_rect.size.y)) &&
+ rect.has_point(ci_rect.position + Vector2(0, ci_rect.size.y))) {
+ selected = true;
+ }
+ } else if (rect.has_point(Point2())) {
+ selected = true;
+ }
+
+ if (selected) {
+ Node2D *node = Object::cast_to<Node2D>(ci);
+
+ DebuggerHelpers::SelectResult res;
+ res.item = ci;
+ res.has_order = node;
+ if (is_editor) {
+ res.order = node ? node->get_z_index() : 0;
+ } else {
+ res.order = ci->get_effective_z_index() + ci->get_canvas_layer();
+ }
+
+ r_items.push_back(res);
+ }
+}
+
+bool CanvasItemManipulator::gui_input(const Ref<InputEvent> &p_event) {
+ bool accepted = _gui_input_rulers_and_guides(p_event) ||
+ plugin_input_callback.call(p_event) ||
+ _gui_input_open_scene_on_double_click(p_event) ||
+ _gui_input_scale(p_event) ||
+ _gui_input_pivot(p_event) ||
+ _gui_input_resize(p_event) ||
+ _gui_input_rotate(p_event) ||
+ _gui_input_move(p_event) ||
+ _gui_input_anchors(p_event) ||
+ _gui_input_ruler_tool(p_event) ||
+ _gui_input_select(p_event);
+
+ _update_cursor_shape();
+
+ return accepted;
+}
+
+void CanvasItemManipulator::reset_drag() {
+ drag_type = DRAG_NONE;
+ drag_selection.clear();
+}
+
+bool CanvasItemManipulator::reset_temp_pivot() {
+ if (temp_pivot != Vector2(Math::INF, Math::INF)) {
+ temp_pivot = Vector2(Math::INF, Math::INF);
+ return true;
+ }
+
+ return false;
+}
+
+void CanvasItemManipulator::set_tool(Tool p_tool) {
+ if (drag_type != DRAG_NONE) {
+ commit_drag();
+ }
+
+ tool = p_tool;
+
+ if (p_tool == TOOL_EDIT_PIVOT && Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
+ // Special action that places temporary rotation pivot in the middle of the selection.
+ Array selection;
+ get_selection_callback.call(selection);
+
+ if (!selection.is_empty()) {
+ Vector2 center;
+ for (const Variant &var : selection) {
+ const CanvasItem *ci = Object::cast_to<CanvasItem>(var);
+ center += ci->get_viewport()->get_popup_base_transform().xform(ci->_edit_get_position());
+ }
+ temp_pivot = center / selection.size();
+ }
+ }
+}
+
+void CanvasItemManipulator::set_shortcut(const ShortcutName p_name, const Ref<Shortcut> &p_shortcut) {
+ ERR_FAIL_INDEX(0, SHORTCUT_MAX);
+ inputs[p_name] = p_shortcut;
+}
+
+void CanvasItemManipulator::set_callbacks(const Callable &p_find_items_start, const Callable &p_point_selected, const Callable &p_get_selection, const Callable &p_local_xform, const Callable &p_global_xform, const Callable &p_local_mouse_pos, const Callable &p_plugin_input) {
+ find_items_start_callback = p_find_items_start;
+ point_selected_callback = p_point_selected;
+ get_selection_callback = p_get_selection;
+ local_transform_callback = p_local_xform;
+ global_transform_callback = p_global_xform;
+ local_mouse_pos_callback = p_local_mouse_pos;
+ plugin_input_callback = p_plugin_input;
+}
+
+void CanvasItemManipulator::_bind_methods() {
+ ADD_SIGNAL(MethodInfo("box_selected", PropertyInfo(Variant::ARRAY, "selection")));
+ ADD_SIGNAL(MethodInfo("box_selection_updated", PropertyInfo(Variant::RECT2, "area")));
+ ADD_SIGNAL(MethodInfo("clear_selection_requested", PropertyInfo(Variant::BOOL, "selected_from_canvas")));
+ ADD_SIGNAL(MethodInfo("selection_menu_requested", PropertyInfo(Variant::ARRAY, "selection"), PropertyInfo(Variant::VECTOR2, "position"), PropertyInfo(Variant::BOOL, "append")));
+
+ ADD_SIGNAL(MethodInfo("update_canvas_requested"));
+
+ ADD_SIGNAL(MethodInfo("tool_menu_requested", PropertyInfo(Variant::VECTOR2, "position")));
+ ADD_SIGNAL(MethodInfo("scene_double_clicked", PropertyInfo(Variant::STRING, "path")));
+
+ ADD_SIGNAL(MethodInfo("save_canvas_state_requested", PropertyInfo(Variant::ARRAY, "selection"), PropertyInfo(Variant::BOOL, "save_bones")));
+ ADD_SIGNAL(MethodInfo("restore_canvas_state_requested", PropertyInfo(Variant::ARRAY, "selection"), PropertyInfo(Variant::BOOL, "restore_bones")));
+ ADD_SIGNAL(MethodInfo("commit_canvas_state_requested", PropertyInfo(Variant::ARRAY, "selection"), PropertyInfo(Variant::STRING, "message"), PropertyInfo(Variant::BOOL, "commit_bones")));
+
+ ADD_SIGNAL(MethodInfo("commit_guide_meta_requested", PropertyInfo(Variant::STRING, "message"), PropertyInfo(Variant::DICTIONARY, "meta")));
+
+ ADD_SIGNAL(MethodInfo("cursor_shape_changed"));
+
+ ADD_SIGNAL(MethodInfo("warning_emitted", PropertyInfo(Variant::STRING, "message")));
+}
+#endif // DEBUG_ENABLED
diff --git a/scene/debugger/canvas_item_manipulator.h b/scene/debugger/canvas_item_manipulator.h
new file mode 100644
index 00000000000..09e893f5682
--- /dev/null
+++ b/scene/debugger/canvas_item_manipulator.h
@@ -0,0 +1,327 @@
+/**************************************************************************/
+/* canvas_item_manipulator.h */
+/**************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/**************************************************************************/
+
+#pragma once
+
+#ifdef DEBUG_ENABLED
+#include "core/input/input.h"
+#include "core/object/ref_counted.h"
+#include "scene/debugger/debugger_helpers.h"
+
+class CanvasItem;
+class Control;
+class InputEvent;
+class Node;
+class Shortcut;
+class Viewport;
+
+class CanvasItemManipulator : public RefCounted {
+ GDCLASS(CanvasItemManipulator, RefCounted);
+
+public:
+ constexpr static real_t GIZMO_HANDLE_DISTANCE = 25;
+ constexpr static Rect2 GIZMO_HANDLE_X_RECT = Rect2(GIZMO_HANDLE_DISTANCE, -5, 10, 10);
+ constexpr static Rect2 GIZMO_HANDLE_Y_RECT = Rect2(-5, GIZMO_HANDLE_DISTANCE, 10, 10);
+
+ constexpr static real_t RESIZE_HANDLE_DIAMETER = 10;
+
+ constexpr static Size2 ANCHOR_HANDLE_SIZE = Size2(16, 16);
+
+ enum Tool {
+ TOOL_SELECT,
+ TOOL_SCENE_PAINT,
+ TOOL_LIST_SELECT,
+ TOOL_MOVE,
+ TOOL_SCALE,
+ TOOL_ROTATE,
+ TOOL_EDIT_PIVOT,
+ TOOL_PAN,
+ TOOL_RULER,
+ TOOL_MAX,
+ };
+
+ enum DragType {
+ DRAG_NONE,
+ DRAG_BOX_SELECTION,
+ DRAG_LEFT,
+ DRAG_TOP_LEFT,
+ DRAG_TOP,
+ DRAG_TOP_RIGHT,
+ DRAG_RIGHT,
+ DRAG_BOTTOM_RIGHT,
+ DRAG_BOTTOM,
+ DRAG_BOTTOM_LEFT,
+ DRAG_ANCHOR_TOP_LEFT,
+ DRAG_ANCHOR_TOP_RIGHT,
+ DRAG_ANCHOR_BOTTOM_RIGHT,
+ DRAG_ANCHOR_BOTTOM_LEFT,
+ DRAG_ANCHOR_ALL,
+ DRAG_QUEUED,
+ DRAG_MOVE,
+ DRAG_MOVE_X,
+ DRAG_MOVE_Y,
+ DRAG_SCALE_X,
+ DRAG_SCALE_Y,
+ DRAG_SCALE_BOTH,
+ DRAG_ROTATE,
+ DRAG_PIVOT,
+ DRAG_TEMP_PIVOT,
+ DRAG_V_GUIDE,
+ DRAG_H_GUIDE,
+ DRAG_DOUBLE_GUIDE,
+ DRAG_KEY_MOVE,
+ };
+
+ enum ShortcutName {
+ SHORTCUT_CANCEL_TRANSFORM,
+ SHORTCUT_MAX,
+ };
+
+ enum SnapTarget {
+ SNAP_TARGET_NONE,
+ SNAP_TARGET_PARENT,
+ SNAP_TARGET_SELF_ANCHORS,
+ SNAP_TARGET_SELF,
+ SNAP_TARGET_OTHER_NODE,
+ SNAP_TARGET_GUIDE,
+ SNAP_TARGET_GRID,
+ SNAP_TARGET_PIXEL,
+ };
+
+ enum SnapMode {
+ SNAP_GRID = 1 << 0,
+ SNAP_GUIDES = 1 << 1,
+ SNAP_PIXEL = 1 << 2,
+ SNAP_NODE_PARENT = 1 << 3,
+ SNAP_NODE_ANCHORS = 1 << 4,
+ SNAP_NODE_SIDES = 1 << 5,
+ SNAP_NODE_CENTER = 1 << 6,
+ SNAP_OTHER_NODES = 1 << 7,
+
+ SNAP_DEFAULT = SNAP_GRID | SNAP_GUIDES | SNAP_PIXEL,
+ };
+
+private:
+ StringName update_canvas_signal = "update_canvas_requested";
+
+ Tool tool = TOOL_SELECT;
+ DragType drag_type = DRAG_NONE;
+ Input::CursorShape cursor_shape = Input::CursorShape::CURSOR_ARROW;
+
+ bool local_space = false;
+ bool show_transformation_gizmos = false;
+ real_t grab_distance = 1;
+ real_t drag_threshold = 0;
+ real_t scale = 1;
+ real_t zoom = 1;
+ Point2 view_offset;
+ bool anchors_mode = false;
+ Viewport *viewport = nullptr;
+
+ Point2 box_selecting_to;
+ Point2 drag_start_origin;
+
+ Point2 drag_from;
+ Point2 drag_to;
+ Point2 drag_rotation_center;
+ Point2 temp_pivot = Point2(Math::INF, Math::INF);
+ Array drag_selection;
+
+ int snap_mode = SNAP_DEFAULT;
+ bool smart_snap = false;
+ bool snap_relative = false;
+ bool snap_rotation = false;
+ real_t snap_rotation_step = 0;
+ real_t snap_rotation_offset = 0;
+ bool snap_scale = false;
+ real_t snap_scale_step = 0;
+ Transform2D snap_transform;
+ SnapTarget snap_target[2];
+
+ bool grid_snap = false;
+ Point2 grid_step;
+ int grid_step_multiplier = 1;
+ Point2 grid_offset;
+
+ bool ruler_tool_active = false;
+ Point2 ruler_tool_origin;
+
+ bool show_guides = false;
+ bool show_rulers = false;
+ bool is_hovering_h_guide = false;
+ bool is_hovering_v_guide = false;
+ Point2 dragged_guide_pos;
+ int dragged_guide_index = -1;
+ real_t ruler_width = 0;
+
+ HashMap<int, Ref<Shortcut>> inputs;
+
+ Callable find_items_start_callback;
+ Callable point_selected_callback;
+ Callable get_selection_callback;
+ Callable local_transform_callback;
+ Callable global_transform_callback;
+ Callable local_mouse_pos_callback;
+ Callable plugin_input_callback;
+
+ bool _gui_input_rulers_and_guides(const Ref<InputEvent> &p_event);
+ bool _gui_input_open_scene_on_double_click(const Ref<InputEvent> &p_event);
+ bool _gui_input_scale(const Ref<InputEvent> &p_event);
+ bool _gui_input_pivot(const Ref<InputEvent> &p_event);
+ bool _gui_input_resize(const Ref<InputEvent> &p_event);
+ bool _gui_input_rotate(const Ref<InputEvent> &p_event);
+ bool _gui_input_move(const Ref<InputEvent> &p_event);
+ bool _gui_input_anchors(const Ref<InputEvent> &p_event);
+ bool _gui_input_ruler_tool(const Ref<InputEvent> &p_event);
+ bool _gui_input_select(const Ref<InputEvent> &p_event);
+ bool _gui_input_cancel_drag(const Ref<InputEvent> &p_event, bool p_reset_snap_targets = false, bool p_restore_bones = false);
+
+ bool _validate_drag_selection();
+
+ Rect2 _encompass_selection_rect(const Array &p_selected);
+
+ void _snap_if_closer_float(
+ const real_t p_value,
+ real_t &r_current_snap, SnapTarget &r_current_snap_target,
+ const real_t p_target_value, const SnapTarget p_snap_target,
+ const real_t p_radius = 10.0);
+ void _snap_if_closer_point(
+ Point2 p_value,
+ Point2 &r_current_snap, SnapTarget (&r_current_snap_target)[2],
+ Point2 p_target_value, const SnapTarget p_snap_target,
+ const real_t rotation = 0.0,
+ const real_t p_radius = 10.0);
+ void _snap_other_nodes(
+ const Point2 p_value,
+ const Transform2D p_transform_to_snap,
+ Point2 &r_current_snap, SnapTarget (&r_current_snap_target)[2],
+ const SnapTarget p_snap_target, const Array &p_exceptions,
+ const Node *p_current);
+
+ void _update_cursor_shape();
+
+ void _emit_locked_warn_signal();
+
+protected:
+ static void _bind_methods();
+
+public:
+ bool gui_input(const Ref<InputEvent> &p_event);
+
+ Point2 snap_point(Point2 p_target, unsigned int p_modes = SNAP_DEFAULT, unsigned int p_forced_modes = 0, const CanvasItem *p_self_canvas_item = nullptr, const Array &p_other_nodes_exceptions = Array());
+ static Point2 anchor_to_position(const Control *p_control, const Point2 p_anchor);
+ static Point2 position_to_anchor(const Control *p_control, const Point2 &p_position);
+
+ void get_canvas_items_at_pos(const Point2 &p_pos, Vector<DebuggerHelpers::SelectResult> &r_items, bool p_allow_locked = false);
+ void find_canvas_items_at_pos(const Point2 &p_pos, Node *p_node, Vector<DebuggerHelpers::SelectResult> &r_items, const Transform2D &p_parent_xform = Transform2D(), const Transform2D &p_canvas_xform = Transform2D());
+ void find_canvas_items_in_rect(const Rect2 &p_rect, Node *p_node, Vector<DebuggerHelpers::SelectResult> &r_items, const Transform2D &p_parent_xform = Transform2D(), const Transform2D &p_canvas_xform = Transform2D());
+
+ Input::CursorShape get_cursor_shape() const { return cursor_shape; }
+
+ void commit_drag();
+ void reset_drag();
+
+ bool reset_temp_pivot();
+
+ 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; }
+ void set_grab_distance(const real_t p_distance) { grab_distance = p_distance; }
+ void set_drag_threshold(const real_t p_threshold) { drag_threshold = p_threshold; }
+ void set_scale(const real_t p_scale) { scale = p_scale; }
+ void set_zoom(const real_t p_zoom) { zoom = p_zoom; }
+ void set_view_offset(const Point2 p_offset) { view_offset = p_offset; }
+ void set_anchors_mode_enabled(bool p_enabled) { anchors_mode = p_enabled; }
+ void set_viewport(Viewport *p_viewport) { viewport = p_viewport; }
+
+ void set_grid_snap_enabled(bool p_enabled) { grid_snap = p_enabled; }
+ void set_grid_step(const Point2 &p_step) { grid_step = p_step; }
+ void set_grid_step_multiplier(const int &p_multiplier) { grid_step_multiplier = p_multiplier; }
+ void set_grid_offset(const Point2 p_offset) { grid_offset = p_offset; }
+
+ void set_snap_mode(const int p_flag) { snap_mode = p_flag; }
+ void set_smart_snap_enabled(bool p_enabled) { smart_snap = p_enabled; }
+ void set_snap_relative_enabled(bool p_enabled) { snap_relative = p_enabled; }
+ void set_snap_rotation_enabled(bool p_enabled) { snap_rotation = p_enabled; }
+ void set_snap_rotation_step(const real_t p_step) { snap_rotation_step = p_step; }
+ void set_snap_rotation_offset(const real_t p_offset) { snap_rotation_offset = p_offset; }
+ void set_snap_scale_enabled(bool p_enabled) { snap_scale = p_enabled; }
+ void set_snap_scale_step(const real_t p_step) { snap_scale_step = p_step; }
+
+ void set_show_transformation_gizmos(bool p_enabled) { show_transformation_gizmos = p_enabled; }
+ void set_show_guides(bool p_show) { show_guides = p_show; }
+ void set_show_rulers(bool p_show) { show_rulers = p_show; }
+ void set_ruler_width(const real_t p_width) { ruler_width = p_width; }
+
+ void set_shortcut(const ShortcutName p_name, const Ref<Shortcut> &p_shortcut);
+ void set_callbacks(const Callable &p_find_items_start, const Callable &p_point_selected, const Callable &p_get_selection, const Callable &p_local_xform, const Callable &p_global_xform, const Callable &p_local_mouse_pos, const Callable &p_plugin_input);
+
+ static bool is_node_locked(const Node *p_node);
+ bool is_node_movable(const Node *p_node, bool p_popup_warning = false);
+
+ Tool get_tool() const { return tool; }
+ DragType get_drag_type() const { return drag_type; }
+ bool is_local_space_enabled() { return local_space; }
+
+ Point2 get_drag_to() const { return drag_to; }
+ Point2 get_drag_from() const { return drag_from; }
+ Point2 get_drag_rotation_center() const { return drag_rotation_center; }
+ Array get_drag_selection() const { return drag_selection.duplicate(); }
+
+ Point2 get_temp_pivot() const { return temp_pivot; }
+
+ bool is_grid_snap_enabled() { return grid_snap; }
+ Point2 get_grid_step() const { return grid_step; }
+ Point2 get_grid_offset() const { return grid_offset; }
+ int get_grid_step_multiplier() const { return grid_step_multiplier; }
+
+ int get_snap_mode() const { return snap_mode; }
+ bool is_smart_snap_enabled() { return smart_snap; }
+ bool is_snap_relative_enabled() { return snap_relative; }
+ bool is_snap_rotation_enabled() { return snap_rotation; }
+ real_t get_snap_rotation_step() { return snap_rotation_step; }
+ real_t get_snap_rotation_offset() { return snap_rotation_offset; }
+ bool is_snap_scale_enabled() { return snap_scale; }
+ real_t get_snap_scale_step() { return snap_scale_step; }
+ Transform2D get_snap_transform() const { return snap_transform; }
+ Pair<SnapTarget, SnapTarget> get_snap_target() const { return Pair(snap_target[0], snap_target[1]); }
+
+ Point2 get_dragged_guide_position() const { return dragged_guide_pos; }
+ int get_dragged_guide_index() const { return dragged_guide_index; }
+
+ bool is_showing_transformation_gizmos() { return show_transformation_gizmos; }
+ bool is_showing_guides() { return show_guides; }
+ bool is_showing_rulers() { return show_rulers; }
+ real_t get_ruler_width() const { return ruler_width; }
+
+ bool is_ruler_tool_active() { return ruler_tool_active; }
+ Point2 get_ruler_tool_origin() const { return ruler_tool_origin; }
+};
+#endif // DEBUG_ENABLED
diff --git a/scene/debugger/debugger_helpers.cpp b/scene/debugger/debugger_helpers.cpp
new file mode 100644
index 00000000000..de073957270
--- /dev/null
+++ b/scene/debugger/debugger_helpers.cpp
@@ -0,0 +1,59 @@
+/**************************************************************************/
+/* debugger_helpers.cpp */
+/**************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/**************************************************************************/
+
+#ifdef DEBUG_ENABLED
+#include "debugger_helpers.h"
+
+#include "core/input/input.h"
+#include "core/input/shortcut.h"
+
+bool DebuggerHelpers::is_shortcut_pressed(const int p_idx, const HashMap<int, Ref<Shortcut>> &p_shortcuts, bool p_true_if_empty) {
+ ERR_FAIL_COND_V(!p_shortcuts.has(p_idx), p_true_if_empty);
+ if (is_shortcut_empty(p_idx, p_shortcuts)) {
+ return p_true_if_empty;
+ }
+
+ Ref<Shortcut> shortcut = p_shortcuts[p_idx];
+ for (Ref<InputEventKey> k : shortcut->get_events()) {
+ if (k.is_valid() && (Input::get_singleton()->is_key_pressed(k->get_keycode()) || Input::get_singleton()->is_physical_key_pressed(k->get_physical_keycode()))) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+bool DebuggerHelpers::is_shortcut_empty(const int p_idx, const HashMap<int, Ref<Shortcut>> &p_shortcuts) {
+ ERR_FAIL_COND_V(!p_shortcuts.has(p_idx), true);
+ Ref<Shortcut> shortcut = p_shortcuts[p_idx];
+ return shortcut.is_null() || shortcut->get_events().is_empty();
+}
+
+#endif // DEBUG_ENABLED
diff --git a/scene/debugger/debugger_helpers.h b/scene/debugger/debugger_helpers.h
new file mode 100644
index 00000000000..94844b8c8b5
--- /dev/null
+++ b/scene/debugger/debugger_helpers.h
@@ -0,0 +1,50 @@
+/**************************************************************************/
+/* debugger_helpers.h */
+/**************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/**************************************************************************/
+
+#pragma once
+
+#ifdef DEBUG_ENABLED
+#include "core/input/input_event.h"
+
+class Node;
+
+class DebuggerHelpers {
+public:
+ struct SelectResult {
+ Node *item = nullptr;
+ bool has_order = false;
+ real_t order = 0;
+ _FORCE_INLINE_ bool operator<(const SelectResult &p_rr) const { return p_rr.order < order; }
+ };
+
+ static bool is_shortcut_pressed(const int p_idx, const HashMap<int, Ref<Shortcut>> &p_shortcuts, bool p_true_if_empty = false);
+ static bool is_shortcut_empty(const int p_idx, const HashMap<int, Ref<Shortcut>> &p_shortcuts);
+};
+#endif // DEBUG_ENABLED
diff --git a/scene/debugger/runtime_node_select.cpp b/scene/debugger/runtime_node_select.cpp
index 3cff6a0ee67..6c940cfab0d 100644
--- a/scene/debugger/runtime_node_select.cpp
+++ b/scene/debugger/runtime_node_select.cpp
@@ -48,11 +48,10 @@
#include "scene/main/scene_tree.h"
#include "scene/resources/mesh.h"
#include "scene/theme/theme_db.h"
+#include "servers/display/display_server.h"
#include "servers/rendering/rendering_server.h"
#ifndef PHYSICS_2D_DISABLED
-#include "scene/2d/physics/collision_object_2d.h"
-#include "scene/2d/physics/collision_polygon_2d.h"
#include "scene/2d/physics/collision_shape_2d.h"
#endif // PHYSICS_2D_DISABLED
@@ -92,6 +91,16 @@ void RuntimeNodeSelect::_setup(const Dictionary &p_settings) {
root->connect("size_changed", callable_mp(this, &RuntimeNodeSelect::_queue_selection_update), CONNECT_DEFERRED);
max_selection = p_settings.get("debugger/max_node_selection", 1);
+ scale = GLOBAL_GET("display/window/stretch/scale");
+
+ accent_color = p_settings.get("accent_color", Color());
+ axis_x_color = p_settings.get("axis_x_color", Color());
+ axis_y_color = p_settings.get("axis_y_color", Color());
+
+ const Ref<Theme> theme = ThemeDB::get_singleton()->get_default_theme();
+ pivot_icon = theme->get_icon(SNAME("pivot"), SNAME("Debug"));
+ resize_icon = theme->get_icon(SNAME("resize"), SNAME("Debug"));
+ anchor_icon = theme->get_icon(SNAME("anchor"), SNAME("Debug"));
// Panner Setup
@@ -101,13 +110,42 @@ void RuntimeNodeSelect::_setup(const Dictionary &p_settings) {
ViewPanner::ControlScheme panning_scheme = (ViewPanner::ControlScheme)p_settings.get("editors/panning/2d_editor_panning_scheme", 0).operator int();
bool simple_panning = p_settings.get("editors/panning/simple_panning", false);
int pan_speed = p_settings.get("editors/panning/2d_editor_pan_speed", 20);
- Array keys = p_settings.get("canvas_item_editor/pan_view", Array()).operator Array();
- panner->setup(panning_scheme, DebuggerMarshalls::deserialize_key_shortcut(keys), simple_panning);
+ pan_shortcut = DebuggerMarshalls::deserialize_key_shortcut(p_settings.get("canvas_item_editor/pan_view", Array()).operator Array());
+ panner->setup(panning_scheme, pan_shortcut, simple_panning);
panner->setup_warped_panning(root, p_settings.get("editors/panning/warped_mouse_panning", true));
panner->set_scroll_speed(pan_speed);
- sel_2d_grab_dist = p_settings.get("editors/polygon_editor/point_grab_radius", 0);
- sel_2d_scale = MAX(1, Math::ceil(2.0 / (float)GLOBAL_GET("display/window/stretch/scale")));
+ // CanvasItemManipulator Setup
+
+ ci_manipulator.instantiate();
+ ci_manipulator->set_show_transformation_gizmos(true);
+ ci_manipulator->set_grab_distance(p_settings.get("editors/polygon_editor/point_grab_radius", 1));
+ ci_manipulator->set_scale(scale);
+ ci_manipulator->set_viewport(root);
+ ci_manipulator->set_callbacks(
+ callable_mp(this, &RuntimeNodeSelect::_find_ci_start_callback),
+ callable_mp(this, &RuntimeNodeSelect::_point_selected_ci_callback),
+ callable_mp(this, &RuntimeNodeSelect::_get_selection_ci_callback),
+ callable_mp(this, &RuntimeNodeSelect::_local_transform_callback),
+ callable_mp((Viewport *)root, &Viewport::get_screen_transform),
+ callable_mp(this, &RuntimeNodeSelect::_local_mouse_pos_callback),
+ callable_mp(this, &RuntimeNodeSelect::_plugin_input_callback));
+ ci_manipulator->set_shortcut(CanvasItemManipulator::SHORTCUT_CANCEL_TRANSFORM, DebuggerMarshalls::deserialize_key_shortcut(p_settings.get("canvas_item_editor/cancel_transform", Array())));
+ ci_manipulator->connect("box_selected", callable_mp(this, &RuntimeNodeSelect::_box_selected_ci));
+ ci_manipulator->connect("box_selection_updated", callable_mp(this, &RuntimeNodeSelect::_set_selection_area));
+ ci_manipulator->connect("clear_selection_requested", callable_mp(this, &RuntimeNodeSelect::_clear_selection).bind(true).unbind(1));
+ ci_manipulator->connect("selection_menu_requested", callable_mp(this, &RuntimeNodeSelect::_open_selection_list));
+ ci_manipulator->connect("scene_double_clicked", callable_mp(this, &RuntimeNodeSelect::_scene_double_clicked));
+ ci_manipulator->connect("update_canvas_requested", callable_mp(this, &RuntimeNodeSelect::_queue_selection_update));
+ ci_manipulator->connect("save_canvas_state_requested", callable_mp(this, &RuntimeNodeSelect::_save_canvas_state_requested));
+ ci_manipulator->connect("restore_canvas_state_requested", callable_mp(this, &RuntimeNodeSelect::_restore_canvas_state_requested));
+ ci_manipulator->connect("commit_canvas_state_requested", callable_mp(this, &RuntimeNodeSelect::_commit_canvas_state_requested));
+ ci_manipulator->connect("cursor_shape_changed", callable_mp(this, &RuntimeNodeSelect::_update_cursor_shape));
+ ci_manipulator->connect("warning_emitted", callable_mp(this, &RuntimeNodeSelect::_show_toaster).bind(1));
+
+ /// 2D Selection Rectangle Generation
+
+ sel_2d_scale = MAX(1, Math::ceil(2.0 / scale));
selection_area_fill = p_settings.get("box_selection_fill_color", Color());
selection_area_outline = p_settings.get("box_selection_stroke_color", Color());
@@ -115,9 +153,8 @@ void RuntimeNodeSelect::_setup(const Dictionary &p_settings) {
draw_canvas = RS::get_singleton()->canvas_create();
sel_drag_ci = RS::get_singleton()->canvas_item_create();
- /// 2D Selection Rectangle Generation
-
srect_color = p_settings.get("editors/2d/selection_rectangle_color", Color());
+ srect_locked_color = p_settings.get("editors/2d/locked_selection_rectangle_color", Color());
srect_ci = RS::get_singleton()->canvas_item_create();
RS::get_singleton()->viewport_attach_canvas(root->get_viewport_rid(), draw_canvas);
@@ -196,35 +233,25 @@ void RuntimeNodeSelect::_setup(const Dictionary &p_settings) {
}
}
-#define SET_SHORTCUT(p_name, p_setting) \
- { \
- Ref<Shortcut> shortcut = DebuggerMarshalls::deserialize_key_shortcut(p_settings.get(p_setting, Array()).operator Array()); \
- if (shortcut.is_valid()) { \
- view_3d_controller->set_shortcut(p_name, shortcut); \
- } \
- }
-
- SET_SHORTCUT(View3DController::SHORTCUT_FOV_DECREASE, "spatial_editor/decrease_fov");
- SET_SHORTCUT(View3DController::SHORTCUT_FOV_INCREASE, "spatial_editor/increase_fov");
- SET_SHORTCUT(View3DController::SHORTCUT_FOV_RESET, "spatial_editor/reset_fov");
- SET_SHORTCUT(View3DController::SHORTCUT_PAN_MOD_1, "spatial_editor/viewport_pan_modifier_1");
- SET_SHORTCUT(View3DController::SHORTCUT_PAN_MOD_2, "spatial_editor/viewport_pan_modifier_2");
- SET_SHORTCUT(View3DController::SHORTCUT_ORBIT_MOD_1, "spatial_editor/viewport_orbit_modifier_1");
- SET_SHORTCUT(View3DController::SHORTCUT_ORBIT_MOD_2, "spatial_editor/viewport_orbit_modifier_2");
- SET_SHORTCUT(View3DController::SHORTCUT_ORBIT_SNAP_MOD_1, "spatial_editor/viewport_orbit_snap_modifier_1");
- SET_SHORTCUT(View3DController::SHORTCUT_ORBIT_SNAP_MOD_2, "spatial_editor/viewport_orbit_snap_modifier_2");
- SET_SHORTCUT(View3DController::SHORTCUT_ZOOM_MOD_1, "spatial_editor/viewport_zoom_modifier_1");
- SET_SHORTCUT(View3DController::SHORTCUT_ZOOM_MOD_2, "spatial_editor/viewport_zoom_modifier_2");
- SET_SHORTCUT(View3DController::SHORTCUT_FREELOOK_FORWARD, "spatial_editor/freelook_forward");
- SET_SHORTCUT(View3DController::SHORTCUT_FREELOOK_BACKWARDS, "spatial_editor/freelook_backwards");
- SET_SHORTCUT(View3DController::SHORTCUT_FREELOOK_LEFT, "spatial_editor/freelook_left");
- SET_SHORTCUT(View3DController::SHORTCUT_FREELOOK_RIGHT, "spatial_editor/freelook_right");
- SET_SHORTCUT(View3DController::SHORTCUT_FREELOOK_UP, "spatial_editor/freelook_up");
- SET_SHORTCUT(View3DController::SHORTCUT_FREELOOK_DOWN, "spatial_editor/freelook_down");
- SET_SHORTCUT(View3DController::SHORTCUT_FREELOOK_SPEED_MOD, "spatial_editor/freelook_speed_modifier");
- SET_SHORTCUT(View3DController::SHORTCUT_FREELOOK_SLOW_MOD, "spatial_editor/freelook_slow_modifier");
-
-#undef SET_SHORTCUT
+ view_3d_controller->set_shortcut(View3DController::SHORTCUT_FOV_DECREASE, DebuggerMarshalls::deserialize_key_shortcut(p_settings.get("spatial_editor/decrease_fov", Array())));
+ view_3d_controller->set_shortcut(View3DController::SHORTCUT_FOV_INCREASE, DebuggerMarshalls::deserialize_key_shortcut(p_settings.get("spatial_editor/increase_fov", Array())));
+ view_3d_controller->set_shortcut(View3DController::SHORTCUT_FOV_RESET, DebuggerMarshalls::deserialize_key_shortcut(p_settings.get("spatial_editor/reset_fov", Array())));
+ view_3d_controller->set_shortcut(View3DController::SHORTCUT_PAN_MOD_1, DebuggerMarshalls::deserialize_key_shortcut(p_settings.get("spatial_editor/viewport_pan_modifier_1", Array())));
+ view_3d_controller->set_shortcut(View3DController::SHORTCUT_PAN_MOD_2, DebuggerMarshalls::deserialize_key_shortcut(p_settings.get("spatial_editor/viewport_pan_modifier_2", Array())));
+ view_3d_controller->set_shortcut(View3DController::SHORTCUT_ORBIT_MOD_1, DebuggerMarshalls::deserialize_key_shortcut(p_settings.get("spatial_editor/viewport_orbit_modifier_1", Array())));
+ view_3d_controller->set_shortcut(View3DController::SHORTCUT_ORBIT_MOD_2, DebuggerMarshalls::deserialize_key_shortcut(p_settings.get("spatial_editor/viewport_orbit_modifier_2", Array())));
+ view_3d_controller->set_shortcut(View3DController::SHORTCUT_ORBIT_SNAP_MOD_1, DebuggerMarshalls::deserialize_key_shortcut(p_settings.get("spatial_editor/viewport_orbit_snap_modifier_1", Array())));
+ view_3d_controller->set_shortcut(View3DController::SHORTCUT_ORBIT_SNAP_MOD_2, DebuggerMarshalls::deserialize_key_shortcut(p_settings.get("spatial_editor/viewport_orbit_snap_modifier_2", Array())));
+ view_3d_controller->set_shortcut(View3DController::SHORTCUT_ZOOM_MOD_1, DebuggerMarshalls::deserialize_key_shortcut(p_settings.get("spatial_editor/viewport_zoom_modifier_1", Array())));
+ view_3d_controller->set_shortcut(View3DController::SHORTCUT_ZOOM_MOD_2, DebuggerMarshalls::deserialize_key_shortcut(p_settings.get("spatial_editor/viewport_zoom_modifier_2", Array())));
+ view_3d_controller->set_shortcut(View3DController::SHORTCUT_FREELOOK_FORWARD, DebuggerMarshalls::deserialize_key_shortcut(p_settings.get("spatial_editor/freelook_forward", Array())));
+ view_3d_controller->set_shortcut(View3DController::SHORTCUT_FREELOOK_BACKWARDS, DebuggerMarshalls::deserialize_key_shortcut(p_settings.get("spatial_editor/freelook_backwards", Array())));
+ view_3d_controller->set_shortcut(View3DController::SHORTCUT_FREELOOK_LEFT, DebuggerMarshalls::deserialize_key_shortcut(p_settings.get("spatial_editor/freelook_left", Array())));
+ view_3d_controller->set_shortcut(View3DController::SHORTCUT_FREELOOK_RIGHT, DebuggerMarshalls::deserialize_key_shortcut(p_settings.get("spatial_editor/freelook_right", Array())));
+ view_3d_controller->set_shortcut(View3DController::SHORTCUT_FREELOOK_UP, DebuggerMarshalls::deserialize_key_shortcut(p_settings.get("spatial_editor/freelook_up", Array())));
+ view_3d_controller->set_shortcut(View3DController::SHORTCUT_FREELOOK_DOWN, DebuggerMarshalls::deserialize_key_shortcut(p_settings.get("spatial_editor/freelook_down", Array())));
+ view_3d_controller->set_shortcut(View3DController::SHORTCUT_FREELOOK_SPEED_MOD, DebuggerMarshalls::deserialize_key_shortcut(p_settings.get("spatial_editor/freelook_speed_modifier", Array())));
+ view_3d_controller->set_shortcut(View3DController::SHORTCUT_FREELOOK_SLOW_MOD, DebuggerMarshalls::deserialize_key_shortcut(p_settings.get("spatial_editor/freelook_slow_modifier", Array())));
/// 3D Selection Box Generation
// Copied from the Node3DEditor implementation.
@@ -271,22 +298,20 @@ void RuntimeNodeSelect::_setup(const Dictionary &p_settings) {
#endif // _3D_DISABLED
SceneTree::get_singleton()->connect("process_frame", callable_mp(this, &RuntimeNodeSelect::_process_frame));
+#ifndef _3D_DISABLED
SceneTree::get_singleton()->connect("physics_frame", callable_mp(this, &RuntimeNodeSelect::_physics_frame));
+#endif
// This function will be called before the root enters the tree at first when the Game view is passing its settings to
// the debugger, so queue the update for after it enters.
root->connect(SceneStringName(tree_entered), callable_mp(this, &RuntimeNodeSelect::_update_input_state), Object::CONNECT_ONE_SHOT);
}
-void RuntimeNodeSelect::_node_set_type(NodeType p_type) {
+void RuntimeNodeSelect::_set_node_type(NodeType p_type) {
node_select_type = p_type;
_update_input_state();
}
-void RuntimeNodeSelect::_select_set_mode(SelectMode p_mode) {
- node_select_mode = p_mode;
-}
-
void RuntimeNodeSelect::_set_camera_override_enabled(bool p_enabled) {
camera_override = p_enabled;
@@ -314,6 +339,63 @@ void RuntimeNodeSelect::_set_camera_override_enabled(bool p_enabled) {
}
}
+void RuntimeNodeSelect::_set_ci_tool(CanvasItemManipulator::Tool p_tool) {
+ ci_manipulator->set_tool(p_tool);
+ panner->set_force_drag(p_tool == CanvasItemManipulator::TOOL_PAN);
+ _queue_selection_update();
+}
+
+void RuntimeNodeSelect::_set_ci_local_space(bool p_enabled) {
+ ci_manipulator->set_local_space_enabled(p_enabled);
+ _queue_selection_update();
+}
+
+void RuntimeNodeSelect::_set_ci_smart_snap(bool p_enabled) {
+ ci_manipulator->set_smart_snap_enabled(p_enabled);
+}
+
+void RuntimeNodeSelect::_set_n3d_tool(SelectMode p_tool) {
+ node_select_mode = p_tool;
+}
+
+Variant RuntimeNodeSelect::_find_ci_start_callback() const {
+ return Object::cast_to<Node>(SceneTree::get_singleton()->get_root());
+}
+
+bool RuntimeNodeSelect::_point_selected_ci_callback(const Variant &p_node, bool p_append) {
+ Node *node = Object::cast_to<Node>(p_node);
+ if (p_append || !selected_ci_nodes.has(node->get_instance_id())) {
+ _send_ids({ node }, p_append, true);
+ }
+ return selected_ci_nodes.has(node->get_instance_id());
+}
+
+bool RuntimeNodeSelect::_get_selection_ci_callback(Array r_selection) {
+ bool has_locked_items = false;
+ for (const KeyValue<ObjectID, Dictionary> &kv : selected_ci_nodes) {
+ Object *obj = ObjectDB::get_instance(kv.key);
+ if (!obj) {
+ continue;
+ }
+
+ CanvasItem *ci = Object::cast_to<CanvasItem>(obj);
+ if (ci->is_visible_in_tree() && !CanvasItemManipulator::is_node_locked(ci)) {
+ // Ignore items with parents that are already selected.
+ if (!selected_ci_nodes.has(ci->get_parent()->get_instance_id())) {
+ r_selection.append(obj);
+ }
+ } else {
+ has_locked_items = true;
+ }
+ }
+ return has_locked_items;
+}
+
+Point2 RuntimeNodeSelect::_local_mouse_pos_callback() const {
+ Window *root = SceneTree::get_singleton()->get_root();
+ return root->get_screen_transform().affine_inverse().xform(root->get_mouse_position());
+}
+
void RuntimeNodeSelect::_root_window_input(const Ref<InputEvent> &p_event) {
Window *root = SceneTree::get_singleton()->get_root();
if (node_select_type == NODE_TYPE_NONE || (selection_list && selection_list->is_visible())) {
@@ -327,23 +409,64 @@ void RuntimeNodeSelect::_root_window_input(const Ref<InputEvent> &p_event) {
return;
}
+ if (SceneTree::get_singleton()->is_suspended() && ci_manipulator->is_showing_transformation_gizmos()) {
+ Ref<InputEventKey> k = p_event;
+ if (k.is_valid() && !k->is_echo() && (k->get_keycode() == Key::CTRL || k->get_keycode() == Key::ALT || k->get_keycode() == Key::SHIFT)) {
+ _queue_selection_update();
+ }
+ }
+
+ _update_cursor_shape();
+
Ref<InputEventMouseButton> b = p_event;
if (b.is_valid() && b->is_pressed()) {
list_shortcut_pressed = node_select_mode == SELECT_MODE_SINGLE && b->get_button_index() == MouseButton::RIGHT && b->is_alt_pressed();
}
- bool is_dragging_camera = false;
- if (camera_override && !list_shortcut_pressed) {
+ if (camera_override) {
if (node_select_type == NODE_TYPE_2D) {
- is_dragging_camera = panner->gui_input(p_event, root->get_visible_rect());
+ bool is_dragging_camera = panner->gui_input(p_event, root->get_visible_rect());
+ if (is_panning != panner->is_panning()) {
+ is_panning = panner->is_panning();
+ _update_cursor_shape();
+ }
+ if (is_dragging_camera) {
+ return;
+ }
#ifndef _3D_DISABLED
- } else if (node_select_type == NODE_TYPE_3D && selection_drag_state == SELECTION_DRAG_NONE) {
+ } else if (!list_shortcut_pressed && node_select_type == NODE_TYPE_3D && selection_drag_state == SELECTION_DRAG_NONE) {
if (_handle_3d_input(p_event)) {
return;
}
#endif // _3D_DISABLED
}
+ } else if (b.is_valid() && b->is_pressed()) {
+ bool was_input_disabled = Input::get_singleton()->is_input_disabled();
+ Input::get_singleton()->set_disable_input(false);
+
+ // Remind the user to enable the camera override when panning.
+ bool was_pan_attempted = false;
+ if (b->get_button_index() == MouseButton::LEFT) {
+ if (pan_shortcut.is_valid()) {
+ for (Ref<InputEventKey> k : pan_shortcut->get_events()) {
+ if (k.is_valid() && (Input::get_singleton()->is_key_pressed(k->get_keycode()) || Input::get_singleton()->is_physical_key_pressed(k->get_physical_keycode()))) {
+ was_pan_attempted = true;
+ _show_camera_override_message();
+ break;
+ }
+ }
+ }
+ } else if (b->get_button_index() == MouseButton::MIDDLE) {
+ was_pan_attempted = true;
+ _show_camera_override_message();
+ }
+
+ Input::get_singleton()->set_disable_input(was_input_disabled);
+
+ if (was_pan_attempted) {
+ return;
+ }
}
if (selection_drag_state == SELECTION_DRAG_MOVE) {
@@ -357,31 +480,45 @@ void RuntimeNodeSelect::_root_window_input(const Ref<InputEvent> &p_event) {
}
}
- if (b.is_null()) {
- return;
- }
+ if (b.is_valid()) {
+ if (node_select_type == NODE_TYPE_3D) {
+ if (selection_drag_state == SELECTION_DRAG_MOVE && !b->is_pressed() && b->get_button_index() == MouseButton::LEFT) {
+ selection_drag_state = SELECTION_DRAG_END;
+ selection_drag_area = selection_drag_area.abs();
+ _update_selection_drag();
- // Ignore mouse wheel inputs.
- if (b->get_button_index() != MouseButton::LEFT && b->get_button_index() != MouseButton::RIGHT) {
- return;
- }
+ // Trigger a selection in the position on release.
+ if (multi_shortcut_pressed) {
+ selection_position = root->get_screen_transform().affine_inverse().xform(b->get_position());
+ }
+ }
+ }
- if (selection_drag_state == SELECTION_DRAG_MOVE && !b->is_pressed() && b->get_button_index() == MouseButton::LEFT) {
- selection_drag_state = SELECTION_DRAG_END;
- selection_drag_area = selection_drag_area.abs();
- _update_selection_drag();
+ if (b->is_pressed()) {
+ if (b->get_button_index() != MouseButton::NONE) {
+ multi_shortcut_pressed = b->is_shift_pressed();
+ }
- // Trigger a selection in the position on release.
- if (multi_shortcut_pressed) {
- selection_position = root->get_screen_transform().affine_inverse().xform(b->get_position());
+ if (node_select_type == NODE_TYPE_3D) {
+ if (list_shortcut_pressed || b->get_button_index() == MouseButton::LEFT) {
+ selection_position = root->get_screen_transform().affine_inverse().xform(b->get_position());
+ }
+ }
}
}
- if (!is_dragging_camera && b->is_pressed()) {
- multi_shortcut_pressed = b->is_shift_pressed();
- if (list_shortcut_pressed || b->get_button_index() == MouseButton::LEFT) {
- selection_position = root->get_screen_transform().affine_inverse().xform(b->get_position());
+ if (node_select_type == NODE_TYPE_2D) {
+ bool was_input_disabled = Input::get_singleton()->is_input_disabled();
+ Input::get_singleton()->set_disable_input(false);
+
+ 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()) {
+ _show_camera_override_message();
+ }
}
+
+ Input::get_singleton()->set_disable_input(was_input_disabled);
}
}
@@ -390,7 +527,7 @@ void RuntimeNodeSelect::_items_popup_index_pressed(int p_index, PopupMenu *p_pop
if (obj) {
Vector<Node *> node;
node.append(Object::cast_to<Node>(obj));
- _send_ids(node);
+ _send_ids(node, selection_list_appending, true);
}
}
@@ -408,23 +545,52 @@ void RuntimeNodeSelect::_update_input_state() {
scene_tree->get_root()->set_disable_input_override(disable_input);
}
+void RuntimeNodeSelect::_update_cursor_shape() {
+ if (!DisplayServer::get_singleton()->has_feature(DisplayServerEnums::FEATURE_CURSOR_SHAPE)) {
+ return;
+ }
+
+ // Choose the correct default cursor.
+ DisplayServerEnums::CursorShape c = DisplayServerEnums::CURSOR_ARROW;
+ if (panner->is_panning()) {
+ c = DisplayServerEnums::CursorShape::CURSOR_DRAG;
+ } else {
+ c = (DisplayServerEnums::CursorShape)ci_manipulator->get_cursor_shape();
+ }
+
+ // Since this is only used when inputs are disabled, just change the shape directly.
+ DisplayServer::get_singleton()->cursor_set_shape(c);
+}
+
+void RuntimeNodeSelect::_show_toaster(const String &p_message, int p_level) {
+ // Levels are the same as the ones in `EditorToaster`.
+ EngineDebugger::get_singleton()->send_message("game_view:show_toaster", { p_message, p_level });
+}
+
+void RuntimeNodeSelect::_show_camera_override_message() {
+ _show_toaster(RTR("Camera override must be enabled to allow panning."), 0);
+}
+
+void RuntimeNodeSelect::_show_limit_warning() {
+ _show_toaster(vformat(RTR("Some remote nodes were not selected, as the configured maximum selection is %d. This can be changed at \"debugger/max_node_selection\" in the Editor Settings."), max_selection), 1);
+}
+
+void RuntimeNodeSelect::_scene_double_clicked(const String &p_path) {
+ EngineDebugger::get_singleton()->send_message("game_view:open_scene", { p_path });
+}
+
void RuntimeNodeSelect::_process_frame() {
#ifndef _3D_DISABLED
// Calculate the process time manually, as the time scale can be frozen.
const double process_time = (1.0 / Engine::get_singleton()->get_frames_per_second());
if (view_3d_controller->is_freelook_enabled()) {
- Input *input = Input::get_singleton();
- bool was_input_disabled = input->is_input_disabled();
- if (was_input_disabled) {
- input->set_disable_input(false);
- }
+ bool was_input_disabled = Input::get_singleton()->is_input_disabled();
+ Input::get_singleton()->set_disable_input(false);
view_3d_controller->update_freelook(process_time);
- if (was_input_disabled) {
- input->set_disable_input(true);
- }
+ Input::get_singleton()->set_disable_input(was_input_disabled);
}
view_3d_controller->update_camera(process_time);
@@ -432,40 +598,30 @@ void RuntimeNodeSelect::_process_frame() {
if (selection_update_queued || !SceneTree::get_singleton()->is_suspended()) {
selection_update_queued = false;
- if (has_selection) {
+ if (has_selection && selection_visible) {
_update_selection();
}
}
}
+#ifndef _3D_DISABLED
void RuntimeNodeSelect::_physics_frame() {
+ if (node_select_type != NODE_TYPE_3D) {
+ return;
+ }
+
if (selection_drag_state != SELECTION_DRAG_END && (selection_drag_state == SELECTION_DRAG_MOVE || !selection_position.is_finite())) {
return;
}
Window *root = SceneTree::get_singleton()->get_root();
bool selection_drag_valid = selection_drag_state == SELECTION_DRAG_END && selection_drag_area.get_area() > SELECTION_MIN_AREA;
- Vector<SelectResult> items;
-
- if (node_select_type == NODE_TYPE_2D) {
- if (selection_drag_valid) {
- for (int i = 0; i < root->get_child_count(); i++) {
- _find_canvas_items_at_rect(selection_drag_area, root->get_child(i), items);
- }
- } else if (selection_position.is_finite()) {
- for (int i = 0; i < root->get_child_count(); i++) {
- _find_canvas_items_at_pos(selection_position, root->get_child(i), items);
- }
- }
+ Vector<DebuggerHelpers::SelectResult> items;
-#ifndef _3D_DISABLED
- } else if (node_select_type == NODE_TYPE_3D) {
- if (selection_drag_valid) {
- _find_3d_items_at_rect(selection_drag_area, items);
- } else if (selection_position.is_finite()) {
- _find_3d_items_at_pos(selection_position, items);
- }
-#endif // _3D_DISABLED
+ if (selection_drag_valid) {
+ _find_3d_items_at_rect(selection_drag_area, items);
+ } else if (selection_position.is_finite()) {
+ _find_3d_items_at_pos(selection_position, items);
}
if ((prefer_group_selection || avoid_locked_nodes) && !list_shortcut_pressed && node_select_mode == SELECT_MODE_SINGLE) {
@@ -480,16 +636,11 @@ void RuntimeNodeSelect::_physics_frame() {
if (node->has_meta("_edit_group_")) {
final_node = node;
- if (Object::cast_to<CanvasItem>(final_node)) {
- CanvasItem *ci_tmp = Object::cast_to<CanvasItem>(final_node);
- order = ci_tmp->get_effective_z_index() + ci_tmp->get_canvas_layer();
-#ifndef _3D_DISABLED
- } else if (Object::cast_to<Node3D>(final_node)) {
+ if (Object::cast_to<Node3D>(final_node)) {
Node3D *node3d_tmp = Object::cast_to<Node3D>(final_node);
Camera3D *camera = root->get_camera_3d();
Vector3 pos = camera->project_ray_origin(selection_position);
order = -pos.distance_to(node3d_tmp->get_global_transform().origin);
-#endif // _3D_DISABLED
}
}
node = node->get_parent();
@@ -497,7 +648,7 @@ void RuntimeNodeSelect::_physics_frame() {
}
// Filter out locked nodes.
- if (avoid_locked_nodes && final_node->get_meta("_edit_lock_", false)) {
+ if (avoid_locked_nodes && CanvasItemManipulator::is_node_locked(final_node)) {
items.remove_at(i);
i--;
continue;
@@ -531,10 +682,10 @@ void RuntimeNodeSelect::_physics_frame() {
if (selection_drag_area.get_area() > SELECTION_MIN_AREA) {
if (!items.is_empty()) {
Vector<Node *> nodes;
- for (const SelectResult item : items) {
+ for (const DebuggerHelpers::SelectResult item : items) {
nodes.append(item.item);
}
- _send_ids(nodes, false);
+ _send_ids(nodes, multi_shortcut_pressed);
}
_update_selection_drag();
@@ -553,12 +704,7 @@ void RuntimeNodeSelect::_physics_frame() {
// Allow forcing box selection when an item was clicked.
selection_drag_state = SELECTION_DRAG_MOVE;
} else if (items.is_empty()) {
-#ifdef _3D_DISABLED
- if (!selected_ci_nodes.is_empty()) {
-#else
if (!selected_ci_nodes.is_empty() || !selected_3d_nodes.is_empty()) {
-#endif // _3D_DISABLED
- EngineDebugger::get_singleton()->send_message("remote_nothing_selected", Array());
_clear_selection();
}
@@ -584,33 +730,40 @@ void RuntimeNodeSelect::_physics_frame() {
selection_position = Point2(Math::INF, Math::INF);
return;
}
- if ((!list_shortcut_pressed && node_select_mode == SELECT_MODE_SINGLE) || items.size() == 1) {
+
+ if (items.size() == 1 || (!list_shortcut_pressed && node_select_mode == SELECT_MODE_SINGLE)) {
selection_position = Point2(Math::INF, Math::INF);
Vector<Node *> node;
node.append(items[0].item);
- _send_ids(node);
+ _send_ids(node, multi_shortcut_pressed, true);
return;
}
if (!selection_list && (list_shortcut_pressed || node_select_mode == SELECT_MODE_LIST)) {
- _open_selection_list(items, selection_position);
+ Array nodes;
+ nodes.reserve(items.size());
+ for (const DebuggerHelpers::SelectResult item : items) {
+ nodes.append(item.item);
+ }
+ _open_selection_list(nodes, selection_position, multi_shortcut_pressed);
}
selection_position = Point2(Math::INF, Math::INF);
}
+#endif // _3D_DISABLED
-void RuntimeNodeSelect::_send_ids(const Vector<Node *> &p_picked_nodes, bool p_invert_new_selections) {
+void RuntimeNodeSelect::_send_ids(const Vector<Node *> &p_picked_nodes, bool p_append, bool p_invert) {
ERR_FAIL_COND(p_picked_nodes.is_empty());
Vector<Node *> picked_nodes = p_picked_nodes;
Array message;
- if (!multi_shortcut_pressed) {
+ if (!p_append) {
if (picked_nodes.size() > max_selection) {
picked_nodes.resize(max_selection);
- EngineDebugger::get_singleton()->send_message("show_selection_limit_warning", Array());
+ _show_limit_warning();
}
for (const Node *node : picked_nodes) {
@@ -626,60 +779,42 @@ void RuntimeNodeSelect::_send_ids(const Vector<Node *> &p_picked_nodes, bool p_i
return;
}
- LocalVector<Node *> nodes;
LocalVector<ObjectID> ids;
+ LocalVector<Node *> nodes;
+
+ for (const KeyValue<ObjectID, Dictionary> &kv : selected_ci_nodes) {
+ ids.push_back(kv.key);
+ nodes.push_back(ObjectDB::get_instance<Node>(kv.key));
+ }
+#ifndef _3D_DISABLED
+ for (const KeyValue<ObjectID, Ref<SelectionBox>> &kv : selected_3d_nodes) {
+ ids.push_back(kv.key);
+ nodes.push_back(ObjectDB::get_instance<Node>(kv.key));
+ }
+#endif // _3D_DISABLED
+
for (Node *node : picked_nodes) {
ObjectID id = node->get_instance_id();
- if (CanvasItem *ci = Object::cast_to<CanvasItem>(node)) {
- if (selected_ci_nodes.has(id)) {
- if (p_invert_new_selections) {
- selected_ci_nodes.erase(id);
- }
- } else {
- ids.push_back(id);
- nodes.push_back(ci);
+ if (ids.has(id)) {
+ if (p_append && p_invert) {
+ ids.erase(id);
+ nodes.erase(node);
}
} else {
-#ifndef _3D_DISABLED
- if (Node3D *node3d = Object::cast_to<Node3D>(node)) {
- if (selected_3d_nodes.has(id)) {
- if (p_invert_new_selections) {
- selected_3d_nodes.erase(id);
- }
- } else {
- ids.push_back(id);
- nodes.push_back(node3d);
- }
- }
-#endif // _3D_DISABLED
+ ids.push_back(id);
+ nodes.push_back(node);
}
}
- uint32_t limit = max_selection - selected_ci_nodes.size();
-#ifndef _3D_DISABLED
- limit -= selected_3d_nodes.size();
-#endif // _3D_DISABLED
- if (ids.size() > limit) {
- ids.resize(limit);
- nodes.resize(limit);
- EngineDebugger::get_singleton()->send_message("show_selection_limit_warning", Array());
+ if ((int)ids.size() > max_selection) {
+ ids.resize(max_selection);
+ _show_limit_warning();
}
- for (ObjectID id : selected_ci_nodes) {
- ids.push_back(id);
- nodes.push_back(ObjectDB::get_instance<Node>(id));
- }
-#ifndef _3D_DISABLED
- for (const KeyValue<ObjectID, Ref<SelectionBox>> &KV : selected_3d_nodes) {
- ids.push_back(KV.key);
- nodes.push_back(ObjectDB::get_instance<Node>(KV.key));
- }
-#endif // _3D_DISABLED
-
if (ids.is_empty()) {
EngineDebugger::get_singleton()->send_message("remote_nothing_selected", message);
} else {
- for (const ObjectID &id : ids) {
+ for (const ObjectID id : ids) {
SceneDebuggerObject obj(id);
Array arr;
obj.serialize(arr);
@@ -699,10 +834,10 @@ void RuntimeNodeSelect::_set_selected_nodes(const Vector<Node *> &p_nodes) {
}
bool changed = false;
- LocalVector<ObjectID> nodes_ci;
+ HashMap<ObjectID, Dictionary> nodes_ci;
#ifndef _3D_DISABLED
HashMap<ObjectID, Ref<SelectionBox>> nodes_3d;
-#endif // _3D_DISABLED
+#endif
for (Node *node : p_nodes) {
ObjectID id = node->get_instance_id();
@@ -711,7 +846,7 @@ void RuntimeNodeSelect::_set_selected_nodes(const Vector<Node *> &p_nodes) {
changed = true;
}
- nodes_ci.push_back(id);
+ nodes_ci.insert(id, Dictionary());
} else {
#ifndef _3D_DISABLED
Node3D *node_3d = Object::cast_to<Node3D>(node);
@@ -725,7 +860,7 @@ void RuntimeNodeSelect::_set_selected_nodes(const Vector<Node *> &p_nodes) {
if (selected_3d_nodes.has(id)) {
// Assign an already available visual instance.
- nodes_3d[id] = selected_3d_nodes.get(id);
+ nodes_3d[id] = selected_3d_nodes[id];
continue;
}
@@ -760,26 +895,27 @@ void RuntimeNodeSelect::_set_selected_nodes(const Vector<Node *> &p_nodes) {
}
}
-#ifdef _3D_DISABLED
if (!changed && nodes_ci.size() == selected_ci_nodes.size()) {
- return;
- }
-#else
- if (!changed && nodes_ci.size() == selected_ci_nodes.size() && nodes_3d.size() == selected_3d_nodes.size()) {
- return;
+#ifndef _3D_DISABLED
+ if (nodes_3d.size() == selected_3d_nodes.size())
+#endif
+ {
+ return;
+ }
}
-#endif // _3D_DISABLED
- _clear_selection();
+ _clear_selection(false);
+
selected_ci_nodes = nodes_ci;
has_selection = !nodes_ci.is_empty();
+ ci_manipulator->reset_temp_pivot();
#ifndef _3D_DISABLED
if (!nodes_3d.is_empty()) {
selected_3d_nodes = nodes_3d;
has_selection = true;
}
-#endif // _3D_DISABLED
+#endif
_queue_selection_update();
}
@@ -798,22 +934,123 @@ void RuntimeNodeSelect::_update_selection() {
RS::get_singleton()->canvas_item_clear(srect_ci);
RS::get_singleton()->canvas_item_set_visible(srect_ci, selection_visible);
- for (LocalVector<ObjectID>::Iterator E = selected_ci_nodes.begin(); E != selected_ci_nodes.end(); ++E) {
- ObjectID id = *E;
- CanvasItem *ci = ObjectDB::get_instance<CanvasItem>(id);
+ CanvasItemManipulator::Tool tool = ci_manipulator->get_tool();
+ bool transform_tool = tool == CanvasItemManipulator::TOOL_SELECT ||
+ tool == CanvasItemManipulator::TOOL_MOVE ||
+ tool == CanvasItemManipulator::TOOL_SCALE ||
+ tool == CanvasItemManipulator::TOOL_ROTATE ||
+ tool == CanvasItemManipulator::TOOL_EDIT_PIVOT;
+ CanvasItemManipulator::DragType drag = ci_manipulator->get_drag_type();
+ bool single = selected_ci_nodes.size() == 1;
+ bool nodes_missing = false;
+
+ for (HashMap<ObjectID, Dictionary>::ConstIterator kv = selected_ci_nodes.begin(); kv != selected_ci_nodes.end(); ++kv) {
+ CanvasItem *ci = ObjectDB::get_instance<CanvasItem>(kv->key);
if (!ci) {
- selected_ci_nodes.erase(id);
- --E;
+ selected_ci_nodes.erase(kv->key);
+ single = selected_ci_nodes.size() == 1;
+ nodes_missing = true;
+ --kv;
continue;
}
- if (!ci->is_inside_tree()) {
+ if (!ci->is_inside_tree() || !ci->is_visible_in_tree()) {
continue;
}
Transform2D xform = ci->get_global_transform_with_canvas();
+ bool item_locked = CanvasItemManipulator::is_node_locked(ci);
- // Fallback.
+ if (single && node_select_type == NODE_TYPE_2D) {
+ // Pivot
+
+ if (transform_tool && !item_locked && ci->_edit_use_pivot()) {
+ Transform2D xform_unscaled = (xform * ci->get_transform().affine_inverse() * ci->_edit_get_transform()).orthonormalized();
+ Transform2D simple_xform;
+ if (ci_manipulator->is_local_space_enabled()) {
+ simple_xform *= xform_unscaled;
+ } else {
+ simple_xform *= Transform2D(0.0f, xform_unscaled.get_origin());
+ }
+
+ Size2 pivot_size = pivot_icon->get_size() * scale;
+ Rect2 rect(simple_xform.get_origin() - (pivot_size / 2.0).floor(), pivot_size);
+ RS::get_singleton()->canvas_item_add_texture_rect(srect_ci, rect, pivot_icon->get_rid());
+ }
+
+ if (tool == CanvasItemManipulator::TOOL_SELECT) {
+ // Anchors
+
+ Control *control = Object::cast_to<Control>(ci);
+ if (control && ci_manipulator->is_node_movable(ci)) {
+ // Compute the anchors.
+ real_t anchors_values[4];
+ anchors_values[0] = control->get_anchor(SIDE_LEFT);
+ anchors_values[1] = control->get_anchor(SIDE_TOP);
+ anchors_values[2] = control->get_anchor(SIDE_RIGHT);
+ anchors_values[3] = control->get_anchor(SIDE_BOTTOM);
+
+ Vector2 anchors_pos[4];
+ for (int i = 0; i < 4; i++) {
+ Vector2 value = Vector2((i % 2 == 0) ? anchors_values[i] : anchors_values[(i + 1) % 4], (i % 2 == 1) ? anchors_values[i] : anchors_values[(i + 1) % 4]);
+ anchors_pos[i] = xform.xform(CanvasItemManipulator::anchor_to_position(control, value));
+ }
+
+ // Draw the anchors handles.
+ Size2 handle_size = CanvasItemManipulator::ANCHOR_HANDLE_SIZE * scale;
+ Rect2 anchor_rects[4];
+ if (control->is_layout_rtl()) {
+ anchor_rects[0] = Rect2(anchors_pos[0] - Vector2(0.0, handle_size.y), Point2(-handle_size.x, handle_size.y));
+ anchor_rects[1] = Rect2(anchors_pos[1] - handle_size, handle_size);
+ anchor_rects[2] = Rect2(anchors_pos[2] - Vector2(handle_size.x, 0.0), Point2(handle_size.x, -handle_size.y));
+ anchor_rects[3] = Rect2(anchors_pos[3], -handle_size);
+ } else {
+ anchor_rects[0] = Rect2(anchors_pos[0] - handle_size, handle_size);
+ anchor_rects[1] = Rect2(anchors_pos[1] - Vector2(0.0, handle_size.y), Point2(-handle_size.x, handle_size.y));
+ anchor_rects[2] = Rect2(anchors_pos[2], -handle_size);
+ anchor_rects[3] = Rect2(anchors_pos[3] - Vector2(handle_size.x, 0.0), Point2(handle_size.x, -handle_size.y));
+ }
+
+ for (int i = 0; i < 4; i++) {
+ RS::get_singleton()->canvas_item_add_texture_rect(srect_ci, anchor_rects[i], anchor_icon->get_rid());
+ }
+ }
+
+ // Resize
+
+ if (ci->_edit_use_rect() && ci_manipulator->is_node_movable(ci)) {
+ Rect2 rect = ci->_edit_get_rect();
+ const Point2 endpoints[4] = {
+ xform.xform(rect.position),
+ xform.xform(rect.position + Point2(rect.size.x, 0)),
+ xform.xform(rect.position + rect.size),
+ xform.xform(rect.position + Point2(0, rect.size.y))
+ };
+
+ Size2 handle_size = Size2(CanvasItemManipulator::RESIZE_HANDLE_DIAMETER, CanvasItemManipulator::RESIZE_HANDLE_DIAMETER) * scale;
+ for (int i = 0; i < 4; i++) {
+ int prev = (i + 3) % 4;
+ int next = (i + 1) % 4;
+
+ Point2 ofs = ((endpoints[i] - endpoints[prev]).normalized() + ((endpoints[i] - endpoints[next]).normalized())).normalized();
+ ofs *= Math::SQRT2 * (handle_size.width / 2);
+ Rect2 icon_rect((endpoints[i] + ofs - (handle_size / 2)).floor(), handle_size);
+
+ RS::get_singleton()->canvas_item_add_texture_rect(srect_ci, icon_rect, resize_icon->get_rid());
+
+ ofs = (endpoints[i] + endpoints[next]) / 2;
+ ofs += (endpoints[next] - endpoints[i]).orthogonal().normalized() * (handle_size.width / 2);
+ icon_rect.position = (ofs - (handle_size / 2)).floor();
+
+ RS::get_singleton()->canvas_item_add_texture_rect(srect_ci, icon_rect, resize_icon->get_rid());
+ }
+ }
+ }
+ }
+
+ // 2D Selection Rectangle
+
+ // Default fallback.
Rect2 rect = Rect2(Vector2(), Vector2(10, 10));
if (ci->_edit_use_rect()) {
@@ -830,41 +1067,152 @@ void RuntimeNodeSelect::_update_selection() {
#endif // PHYSICS_2D_DISABLED
}
- const Vector2 endpoints[4] = {
+ Vector2 endpoints[4] = {
xform.xform(rect.position),
xform.xform(rect.position + Point2(rect.size.x, 0)),
xform.xform(rect.position + rect.size),
xform.xform(rect.position + Point2(0, rect.size.y))
};
+ Color color = item_locked ? srect_locked_color : srect_color;
for (int i = 0; i < 4; i++) {
- RS::get_singleton()->canvas_item_add_line(srect_ci, endpoints[i], endpoints[(i + 1) % 4], srect_color, sel_2d_scale);
+ RS::get_singleton()->canvas_item_add_line(srect_ci, endpoints[i], endpoints[(i + 1) % 4], color, sel_2d_scale);
+ }
+ }
+
+ if (node_select_type == NODE_TYPE_2D) {
+ Point2 temp_pivot = ci_manipulator->get_temp_pivot();
+
+ if (!selected_ci_nodes.is_empty() && ci_manipulator->is_showing_transformation_gizmos()) {
+ CanvasItem *ci = nullptr;
+
+ // Find the first movable node.
+ for (const KeyValue<ObjectID, Dictionary> &kv : selected_ci_nodes) {
+ ci = ObjectDB::get_instance<CanvasItem>(kv.key);
+ if (ci_manipulator->is_node_movable(ci)) {
+ break;
+ }
+ ci = nullptr;
+ }
+
+ if (ci && ci->is_visible_in_tree()) {
+ bool was_input_disabled = Input::get_singleton()->is_input_disabled();
+ Input::get_singleton()->set_disable_input(false);
+
+ bool is_ctrl = Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL);
+ bool is_alt = Input::get_singleton()->is_key_pressed(Key::ALT);
+ bool is_moving = tool == CanvasItemManipulator::TOOL_MOVE || (tool == CanvasItemManipulator::TOOL_SELECT && is_alt && !is_ctrl);
+
+ Transform2D edit_xform;
+ if (!is_moving && !Math::is_inf(temp_pivot.x) && !Math::is_inf(temp_pivot.y)) {
+ edit_xform = Transform2D(ci->_edit_get_rotation(), temp_pivot);
+ } else {
+ edit_xform = ci->_edit_get_transform();
+ }
+
+ Transform2D xform_unscaled = (ci->get_global_transform_with_canvas() * ci->get_transform().affine_inverse() * edit_xform).orthonormalized();
+ Transform2D simple_xform;
+ if (ci_manipulator->is_local_space_enabled()) {
+ simple_xform *= xform_unscaled;
+ } else {
+ simple_xform *= Transform2D(0.0f, xform_unscaled.get_origin());
+ }
+
+ 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),
+ Point2(CanvasItemManipulator::GIZMO_HANDLE_X_RECT.position.x, -CanvasItemManipulator::GIZMO_HANDLE_X_RECT.size.height / 2.0),
+ Point2(CanvasItemManipulator::GIZMO_HANDLE_X_RECT.position.x + CanvasItemManipulator::GIZMO_HANDLE_X_RECT.size.width, 0)
+ };
+ Vector<Color> colors = { axis_x_color };
+
+ RS::get_singleton()->canvas_item_add_polygon(srect_ci, points, colors);
+ RS::get_singleton()->canvas_item_add_line(srect_ci, Point2(), Point2(CanvasItemManipulator::GIZMO_HANDLE_DISTANCE, 0), axis_x_color, scale);
+
+ points = {
+ Point2(CanvasItemManipulator::GIZMO_HANDLE_Y_RECT.size.width / 2.0, CanvasItemManipulator::GIZMO_HANDLE_Y_RECT.position.y),
+ Point2(-CanvasItemManipulator::GIZMO_HANDLE_Y_RECT.size.width / 2.0, CanvasItemManipulator::GIZMO_HANDLE_Y_RECT.position.y),
+ Point2(0, CanvasItemManipulator::GIZMO_HANDLE_Y_RECT.position.y + CanvasItemManipulator::GIZMO_HANDLE_Y_RECT.size.height)
+ };
+ colors = { axis_y_color };
+
+ RS::get_singleton()->canvas_item_add_polygon(srect_ci, points, colors);
+ RS::get_singleton()->canvas_item_add_line(srect_ci, Point2(), Point2(0, CanvasItemManipulator::GIZMO_HANDLE_DISTANCE), axis_y_color, scale);
+ }
+
+ // 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);
+ Point2 offset = simple_xform.affine_inverse().xform(ci_manipulator->get_drag_to()) - simple_xform.affine_inverse().xform(ci_manipulator->get_drag_from());
+
+ if (drag == CanvasItemManipulator::DRAG_SCALE_X) {
+ scale_factor.x += offset.x;
+ if (uniform) {
+ scale_factor.y += offset.x;
+ }
+ } else if (drag == CanvasItemManipulator::DRAG_SCALE_Y) {
+ scale_factor.y += offset.y;
+ if (uniform) {
+ scale_factor.x += offset.y;
+ }
+ }
+
+ Rect2 handle_x_rect(Point2(scale_factor.x, CanvasItemManipulator::GIZMO_HANDLE_X_RECT.position.y), CanvasItemManipulator::GIZMO_HANDLE_X_RECT.size);
+ RS::get_singleton()->canvas_item_add_rect(srect_ci, handle_x_rect, axis_x_color);
+ RS::get_singleton()->canvas_item_add_line(srect_ci, Point2(), Point2(scale_factor.x, 0), axis_x_color, scale);
+
+ Rect2 handle_y_rect(Point2(CanvasItemManipulator::GIZMO_HANDLE_Y_RECT.position.x, scale_factor.y), CanvasItemManipulator::GIZMO_HANDLE_Y_RECT.size);
+ RS::get_singleton()->canvas_item_add_rect(srect_ci, handle_y_rect, axis_y_color);
+ 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);
+ }
+ }
+ }
+
+ 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);
}
}
#ifndef _3D_DISABLED
- for (HashMap<ObjectID, Ref<SelectionBox>>::ConstIterator KV = selected_3d_nodes.begin(); KV != selected_3d_nodes.end(); ++KV) {
- ObjectID id = KV->key;
- Node3D *node_3d = ObjectDB::get_instance<Node3D>(id);
- if (!node_3d) {
+ for (HashMap<ObjectID, Ref<SelectionBox>>::ConstIterator kv = selected_3d_nodes.begin(); kv != selected_3d_nodes.end(); ++kv) {
+ ObjectID id = kv->key;
+
+ Node3D *n3d = ObjectDB::get_instance<Node3D>(id);
+ if (!n3d) {
selected_3d_nodes.erase(id);
- --KV;
+ nodes_missing = true;
+ --kv;
continue;
}
- if (!node_3d->is_inside_tree()) {
+ if (!n3d->is_inside_tree()) {
continue;
}
// Fallback.
AABB bounds(Vector3(-0.5, -0.5, -0.5), Vector3(1, 1, 1));
- VisualInstance3D *visual_instance = Object::cast_to<VisualInstance3D>(node_3d);
+ VisualInstance3D *visual_instance = Object::cast_to<VisualInstance3D>(n3d);
if (visual_instance) {
bounds = visual_instance->get_aabb();
} else {
#ifndef PHYSICS_3D_DISABLED
- CollisionShape3D *collision_shape = Object::cast_to<CollisionShape3D>(node_3d);
+ CollisionShape3D *collision_shape = Object::cast_to<CollisionShape3D>(n3d);
if (collision_shape) {
Ref<Shape3D> shape = collision_shape->get_shape();
if (shape.is_valid()) {
@@ -874,11 +1222,11 @@ void RuntimeNodeSelect::_update_selection() {
#endif // PHYSICS_3D_DISABLED
}
- Transform3D xform_to_top_level_parent_space = node_3d->get_global_transform().affine_inverse() * node_3d->get_global_transform();
+ Transform3D xform_to_top_level_parent_space = n3d->get_global_transform().affine_inverse() * n3d->get_global_transform();
bounds = xform_to_top_level_parent_space.xform(bounds);
- Transform3D t = node_3d->get_global_transform();
+ Transform3D t = n3d->get_global_transform();
- Ref<SelectionBox> sb = KV->value;
+ Ref<SelectionBox> sb = kv->value;
if (t == sb->transform && bounds == sb->bounds) {
continue; // Nothing changed.
}
@@ -914,9 +1262,35 @@ void RuntimeNodeSelect::_update_selection() {
RS::get_singleton()->instance_set_transform(sb->instance_xray_ofs, t_offset);
}
#endif // _3D_DISABLED
+
+ // Update the editor if the selection lost nodes.
+ if (nodes_missing) {
+ LocalVector<ObjectID> ids;
+ for (const KeyValue<ObjectID, Dictionary> &kv : selected_ci_nodes) {
+ ids.push_back(kv.key);
+ }
+#ifndef _3D_DISABLED
+ for (const KeyValue<ObjectID, Ref<SelectionBox>> &kv : selected_3d_nodes) {
+ ids.push_back(kv.key);
+ }
+#endif // _3D_DISABLED
+
+ Array message;
+ if (ids.is_empty()) {
+ EngineDebugger::get_singleton()->send_message("remote_nothing_selected", message);
+ } else {
+ for (const ObjectID id : ids) {
+ SceneDebuggerObject obj(id);
+ Array arr;
+ obj.serialize(arr);
+ message.append(arr);
+ }
+ EngineDebugger::get_singleton()->send_message("remote_objects_selected", message);
+ }
+ }
}
-void RuntimeNodeSelect::_clear_selection() {
+void RuntimeNodeSelect::_clear_selection(bool p_send_msg) {
selected_ci_nodes.clear();
if (draw_canvas.is_valid()) {
RS::get_singleton()->canvas_item_clear(srect_ci);
@@ -926,6 +1300,10 @@ void RuntimeNodeSelect::_clear_selection() {
selected_3d_nodes.clear();
#endif // _3D_DISABLED
+ if (has_selection && p_send_msg) {
+ EngineDebugger::get_singleton()->send_message("remote_nothing_selected", Array());
+ }
+
has_selection = false;
}
@@ -942,26 +1320,32 @@ void RuntimeNodeSelect::_update_selection_drag(const Point2 &p_end_pos) {
return;
}
- Rect2 selection_drawing = selection_drag_area.abs();
- int thickness = 1;
+ _set_selection_area(selection_drag_area.abs());
+}
+
+void RuntimeNodeSelect::_set_selection_area(const Rect2 &p_area) {
+ RS::get_singleton()->canvas_item_clear(sel_drag_ci);
+ if (!p_area.has_area()) {
+ return;
+ }
const Vector2 endpoints[4] = {
- selection_drawing.position,
- selection_drawing.position + Point2(selection_drawing.size.x, 0),
- selection_drawing.position + selection_drawing.size,
- selection_drawing.position + Point2(0, selection_drawing.size.y)
+ p_area.position,
+ p_area.position + Point2(p_area.size.x, 0),
+ p_area.position + p_area.size,
+ p_area.position + Point2(0, p_area.size.y)
};
// Draw fill.
- RS::get_singleton()->canvas_item_add_rect(sel_drag_ci, selection_drawing, selection_area_fill);
+ RS::get_singleton()->canvas_item_add_rect(sel_drag_ci, p_area, selection_area_fill);
// Draw outline.
for (int i = 0; i < 4; i++) {
- RS::get_singleton()->canvas_item_add_line(sel_drag_ci, endpoints[i], endpoints[(i + 1) % 4], selection_area_outline, thickness);
+ RS::get_singleton()->canvas_item_add_line(sel_drag_ci, endpoints[i], endpoints[(i + 1) % 4], selection_area_outline, 1);
}
}
-void RuntimeNodeSelect::_open_selection_list(const Vector<SelectResult> &p_items, const Point2 &p_pos) {
- Window *root = SceneTree::get_singleton()->get_root();
+void RuntimeNodeSelect::_open_selection_list(const Array &p_selection, const Point2 &p_pos, bool p_append) {
+ selection_list_appending = p_append;
selection_list = memnew(PopupMenu);
selection_list->set_theme(ThemeDB::get_singleton()->get_default_theme());
@@ -970,21 +1354,22 @@ void RuntimeNodeSelect::_open_selection_list(const Vector<SelectResult> &p_items
selection_list->connect("index_pressed", callable_mp(this, &RuntimeNodeSelect::_items_popup_index_pressed).bind(selection_list));
selection_list->connect("popup_hide", callable_mp(this, &RuntimeNodeSelect::_close_selection_list));
+ Window *root = SceneTree::get_singleton()->get_root();
root->add_child(selection_list);
- for (const SelectResult &I : p_items) {
+ for (const Variant &var : p_selection) {
+ Node *node = Object::cast_to<Node>(var);
int locked = 0;
- if (I.item->get_meta("_edit_lock_", false)) {
+
+ if (CanvasItemManipulator::is_node_locked(node)) {
locked = 1;
} else {
- Node *scene = SceneTree::get_singleton()->get_root();
- Node *node = I.item;
-
- while (node && node != scene->get_parent()) {
- if (node->has_meta("_edit_group_")) {
+ Node *parent = node;
+ while (parent && parent != root->get_parent()) {
+ if (parent->has_meta("_edit_group_")) {
locked = 2;
}
- node = node->get_parent();
+ parent = parent->get_parent();
}
}
@@ -995,8 +1380,8 @@ void RuntimeNodeSelect::_open_selection_list(const Vector<SelectResult> &p_items
suffix = " (" + RTR("Grouped") + ")";
}
- selection_list->add_item((String)I.item->get_name() + suffix);
- selection_list->set_item_metadata(-1, I.item);
+ selection_list->add_item((String)node->get_name() + suffix);
+ selection_list->set_item_metadata(-1, node);
}
selection_list->set_position(selection_list->is_embedded() ? p_pos : (Input::get_singleton()->get_mouse_position() + root->get_position()));
@@ -1020,7 +1405,11 @@ void RuntimeNodeSelect::_set_selection_visible(bool p_visible) {
selection_visible = p_visible;
if (has_selection) {
- _update_selection();
+ if (p_visible) {
+ _queue_selection_update();
+ } else {
+ _update_selection();
+ }
}
}
@@ -1032,129 +1421,6 @@ void RuntimeNodeSelect::_set_prefer_group(bool p_enabled) {
prefer_group_selection = p_enabled;
}
-// Copied and trimmed from the CanvasItemEditor implementation.
-void RuntimeNodeSelect::_find_canvas_items_at_pos(const Point2 &p_pos, Node *p_node, Vector<SelectResult> &r_items, const Transform2D &p_parent_xform, const Transform2D &p_canvas_xform) {
- if (!p_node || Object::cast_to<Viewport>(p_node)) {
- return;
- }
-
- CanvasItem *ci = Object::cast_to<CanvasItem>(p_node);
- for (int i = p_node->get_child_count() - 1; i >= 0; i--) {
- if (ci) {
- if (!ci->is_set_as_top_level()) {
- _find_canvas_items_at_pos(p_pos, p_node->get_child(i), r_items, p_parent_xform * ci->get_transform(), p_canvas_xform);
- } else {
- _find_canvas_items_at_pos(p_pos, p_node->get_child(i), r_items, ci->get_transform(), p_canvas_xform);
- }
- } else {
- CanvasLayer *cl = Object::cast_to<CanvasLayer>(p_node);
- _find_canvas_items_at_pos(p_pos, p_node->get_child(i), r_items, Transform2D(), cl ? cl->get_transform() : p_canvas_xform);
- }
- }
-
- if (!ci || !ci->is_visible_in_tree()) {
- return;
- }
-
- Transform2D xform = p_canvas_xform;
- if (!ci->is_set_as_top_level()) {
- xform *= p_parent_xform;
- }
-
- Window *root = SceneTree::get_singleton()->get_root();
- Point2 pos;
-
- // Cameras don't affect `CanvasLayer`s.
- if (!ci->get_canvas_layer_node() || ci->get_canvas_layer_node()->is_following_viewport()) {
- pos = root->get_canvas_transform().affine_inverse().xform(p_pos);
- } else {
- pos = p_pos;
- }
-
- xform = (xform * ci->get_transform()).affine_inverse();
- const real_t local_grab_distance = xform.basis_xform(Vector2(sel_2d_grab_dist, 0)).length() / view_2d_zoom;
- if (ci->_edit_is_selected_on_click(xform.xform(pos), local_grab_distance)) {
- SelectResult res;
- res.item = ci;
- res.order = ci->get_effective_z_index() + ci->get_canvas_layer();
- r_items.push_back(res);
-
-#ifndef PHYSICS_2D_DISABLED
- // If it's a shape, get the collision object it's from.
- // FIXME: If the collision object has multiple shapes, only the topmost will be above it in the list.
- if (Object::cast_to<CollisionShape2D>(ci) || Object::cast_to<CollisionPolygon2D>(ci)) {
- CollisionObject2D *collision_object = Object::cast_to<CollisionObject2D>(ci->get_parent());
- if (collision_object) {
- SelectResult res_col;
- res_col.item = ci->get_parent();
- res_col.order = collision_object->get_z_index() + ci->get_canvas_layer();
- r_items.push_back(res_col);
- }
- }
-#endif // PHYSICS_2D_DISABLED
- }
-}
-
-// Copied and trimmed from the CanvasItemEditor implementation.
-void RuntimeNodeSelect::_find_canvas_items_at_rect(const Rect2 &p_rect, Node *p_node, Vector<SelectResult> &r_items, const Transform2D &p_parent_xform, const Transform2D &p_canvas_xform) {
- if (!p_node || Object::cast_to<Viewport>(p_node)) {
- return;
- }
-
- CanvasItem *ci = Object::cast_to<CanvasItem>(p_node);
- for (int i = p_node->get_child_count() - 1; i >= 0; i--) {
- if (ci) {
- if (!ci->is_set_as_top_level()) {
- _find_canvas_items_at_rect(p_rect, p_node->get_child(i), r_items, p_parent_xform * ci->get_transform(), p_canvas_xform);
- } else {
- _find_canvas_items_at_rect(p_rect, p_node->get_child(i), r_items, ci->get_transform(), p_canvas_xform);
- }
- } else {
- CanvasLayer *cl = Object::cast_to<CanvasLayer>(p_node);
- _find_canvas_items_at_rect(p_rect, p_node->get_child(i), r_items, Transform2D(), cl ? cl->get_transform() : p_canvas_xform);
- }
- }
-
- if (!ci || !ci->is_visible_in_tree()) {
- return;
- }
-
- Transform2D xform = p_canvas_xform;
- if (!ci->is_set_as_top_level()) {
- xform *= p_parent_xform;
- }
-
- Window *root = SceneTree::get_singleton()->get_root();
- Rect2 rect;
- // Cameras don't affect `CanvasLayer`s.
- if (!ci->get_canvas_layer_node() || ci->get_canvas_layer_node()->is_following_viewport()) {
- rect = root->get_canvas_transform().affine_inverse().xform(p_rect);
- } else {
- rect = p_rect;
- }
- rect = (xform * ci->get_transform()).affine_inverse().xform(rect);
-
- bool selected = false;
- if (ci->_edit_use_rect()) {
- Rect2 ci_rect = ci->_edit_get_rect();
- if (rect.has_point(ci_rect.position) &&
- rect.has_point(ci_rect.position + Vector2(ci_rect.size.x, 0)) &&
- rect.has_point(ci_rect.position + Vector2(ci_rect.size.x, ci_rect.size.y)) &&
- rect.has_point(ci_rect.position + Vector2(0, ci_rect.size.y))) {
- selected = true;
- }
- } else if (rect.has_point(Point2())) {
- selected = true;
- }
-
- if (selected) {
- SelectResult res;
- res.item = ci;
- res.order = ci->get_effective_z_index() + ci->get_canvas_layer();
- r_items.push_back(res);
- }
-}
-
void RuntimeNodeSelect::_pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event) {
Vector2 scroll = p_scroll_vec / SceneTree::get_singleton()->get_root()->get_screen_transform().get_scale();
view_2d_offset.x -= scroll.x / view_2d_zoom;
@@ -1211,21 +1477,22 @@ void RuntimeNodeSelect::_reset_camera_2d() {
void RuntimeNodeSelect::_update_view_2d() {
Window *root = SceneTree::get_singleton()->get_root();
- ERR_FAIL_COND(!root->is_camera_2d_override_enabled());
-
Camera2D *override_camera = root->get_override_camera_2d();
ERR_FAIL_NULL(override_camera);
override_camera->set_anchor_mode(Camera2D::ANCHOR_MODE_FIXED_TOP_LEFT);
override_camera->set_zoom(Vector2(view_2d_zoom, view_2d_zoom));
override_camera->set_offset(view_2d_offset);
+ ci_manipulator->set_zoom(view_2d_zoom);
+ ci_manipulator->set_view_offset(view_2d_offset);
+
_queue_selection_update();
}
#endif // _2D_DISABLED
#ifndef _3D_DISABLED
-void RuntimeNodeSelect::_find_3d_items_at_pos(const Point2 &p_pos, Vector<SelectResult> &r_items) {
+void RuntimeNodeSelect::_find_3d_items_at_pos(const Point2 &p_pos, Vector<DebuggerHelpers::SelectResult> &r_items) {
Window *root = SceneTree::get_singleton()->get_root();
Vector3 ray, pos, to;
@@ -1250,7 +1517,7 @@ void RuntimeNodeSelect::_find_3d_items_at_pos(const Point2 &p_pos, Vector<Select
while (true) {
ray_params.exclude = excluded;
if (ss->intersect_ray(ray_params, result)) {
- SelectResult res;
+ DebuggerHelpers::SelectResult res;
res.item = result.collider_id.is_valid() ? ObjectDB::get_instance<Node>(result.collider_id) : nullptr;
res.order = -pos.distance_to(result.position);
@@ -1260,7 +1527,7 @@ void RuntimeNodeSelect::_find_3d_items_at_pos(const Point2 &p_pos, Vector<Select
List<uint32_t> owners;
collision->get_shape_owners(&owners);
for (uint32_t &I : owners) {
- SelectResult res_shape;
+ DebuggerHelpers::SelectResult res_shape;
res_shape.item = Object::cast_to<Node>(collision->shape_owner_get_owner(I));
res_shape.order = res.order;
r_items.push_back(res_shape);
@@ -1290,7 +1557,7 @@ void RuntimeNodeSelect::_find_3d_items_at_pos(const Point2 &p_pos, Vector<Select
Transform3D ai = gt.affine_inverse();
Vector3 point, normal;
if (mesh_collision->intersect_ray(ai.xform(pos), ai.basis.xform(ray).normalized(), point, normal)) {
- SelectResult res;
+ DebuggerHelpers::SelectResult res;
res.item = Object::cast_to<Node>(obj);
res.order = -pos.distance_to(gt.xform(point));
r_items.push_back(res);
@@ -1305,7 +1572,7 @@ void RuntimeNodeSelect::_find_3d_items_at_pos(const Point2 &p_pos, Vector<Select
}
}
-void RuntimeNodeSelect::_find_3d_items_at_rect(const Rect2 &p_rect, Vector<SelectResult> &r_items) {
+void RuntimeNodeSelect::_find_3d_items_at_rect(const Rect2 &p_rect, Vector<DebuggerHelpers::SelectResult> &r_items) {
Window *root = SceneTree::get_singleton()->get_root();
Camera3D *camera = root->get_camera_3d();
if (!camera) {
@@ -1374,7 +1641,7 @@ void RuntimeNodeSelect::_find_3d_items_at_rect(const Rect2 &p_rect, Vector<Selec
const int num_hits = ss->intersect_shape(shape_params, results, 32);
for (int i = 0; i < num_hits; i++) {
const PS3DT::ShapeResult &result = results[i];
- SelectResult res;
+ DebuggerHelpers::SelectResult res;
res.item = result.collider_id.is_valid() ? ObjectDB::get_instance<Node>(result.collider_id) : nullptr;
if (!res.item) {
continue;
@@ -1387,7 +1654,7 @@ void RuntimeNodeSelect::_find_3d_items_at_rect(const Rect2 &p_rect, Vector<Selec
List<uint32_t> owners;
collision->get_shape_owners(&owners);
for (uint32_t &I : owners) {
- SelectResult res_shape;
+ DebuggerHelpers::SelectResult res_shape;
res_shape.item = Object::cast_to<Node>(collision->shape_owner_get_owner(I));
if (!node_list.has(res_shape.item)) {
node_list.insert(res_shape.item);
@@ -1428,7 +1695,7 @@ void RuntimeNodeSelect::_find_3d_items_at_rect(const Rect2 &p_rect, Vector<Selec
}
Vector<Vector3> convex_points = Geometry3D::compute_convex_mesh_points(transformed_frustum.ptr(), plane_count);
if (mesh_collision->inside_convex_shape(transformed_frustum.ptr(), transformed_frustum.size(), convex_points.ptr(), convex_points.size(), mesh_scale)) {
- SelectResult res;
+ DebuggerHelpers::SelectResult res;
res.item = Object::cast_to<Node>(obj);
if (!node_list.has(res.item)) {
node_list.insert(res.item);
@@ -1458,6 +1725,57 @@ Vector3 RuntimeNodeSelect::_get_screen_to_space(const Vector3 &p_vector3) {
return camera_transform.xform(Vector3(((p_vector3.x / size.width) * 2.0 - 1.0) * screen_he.x, ((1.0 - (p_vector3.y / size.height)) * 2.0 - 1.0) * screen_he.y, -(znear + p_vector3.z)));
}
+void RuntimeNodeSelect::_box_selected_ci(const Array &p_selection) {
+ Vector<Node *> nodes;
+ for (Variant node : p_selection) {
+ nodes.append(Object::cast_to<Node>(node));
+ }
+ _send_ids(nodes, true);
+}
+
+void RuntimeNodeSelect::_save_canvas_state_requested(const Array &p_selection, bool p_save_bones) {
+ for (const Variant &var : p_selection) {
+ CanvasItem *ci = Object::cast_to<CanvasItem>(var);
+ ObjectID id = ci->get_instance_id();
+ ERR_CONTINUE(!selected_ci_nodes.has(id));
+ selected_ci_nodes[id] = ci->_edit_get_state();
+ }
+}
+
+void RuntimeNodeSelect::_restore_canvas_state_requested(const Array &p_selection, bool p_restore_bones) {
+ for (const Variant &var : p_selection) {
+ CanvasItem *ci = Object::cast_to<CanvasItem>(var);
+ ObjectID id = ci->get_instance_id();
+ ERR_CONTINUE(!selected_ci_nodes.has(id));
+ ci->_edit_set_state(selected_ci_nodes[id]);
+ }
+}
+
+void RuntimeNodeSelect::_commit_canvas_state_requested(const Array &p_selection, const String &p_message, bool p_restore_bones) {
+ Dictionary ids;
+
+ for (const Variant &var : p_selection) {
+ CanvasItem *ci = Object::cast_to<CanvasItem>(var);
+ if (!ci) {
+ continue;
+ }
+
+ ObjectID id = ci->get_instance_id();
+
+ Dictionary states;
+ states["undo"] = selected_ci_nodes[id];
+ states["redo"] = ci->_edit_get_state();
+
+ if (states["undo"].hash() != states["redo"].hash()) {
+ ids[id] = states;
+ }
+ }
+
+ if (!ids.is_empty()) {
+ EngineDebugger::get_singleton()->send_message("remote_undo_redo_action", { p_message, ids });
+ }
+}
+
void RuntimeNodeSelect::_fov_scaled() {
SceneTree::get_singleton()->get_root()->get_override_camera_3d()->set_perspective(camera_fov * view_3d_controller->cursor.fov_scale, camera_znear, camera_zfar);
}
@@ -1472,18 +1790,13 @@ bool RuntimeNodeSelect::_handle_3d_input(const Ref<InputEvent> &p_event) {
Window *root = SceneTree::get_singleton()->get_root();
ERR_FAIL_COND_V(!root->is_camera_3d_override_enabled(), true);
- Input *input = Input::get_singleton();
- bool was_input_disabled = input->is_input_disabled();
- if (was_input_disabled) {
- input->set_disable_input(false);
- }
+ bool was_input_disabled = Input::get_singleton()->is_input_disabled();
+ Input::get_singleton()->set_disable_input(false);
// Reduce all sides of the area by 1, so warping works when windows are maximized/fullscreen.
bool view_3d_input_received = view_3d_controller->gui_input(p_event, Rect2(Vector2(1, 1), root->get_size() - Vector2(2, 2)));
- if (was_input_disabled) {
- input->set_disable_input(true);
- }
+ Input::get_singleton()->set_disable_input(was_input_disabled);
if (view_3d_input_received) {
root->get_override_camera_3d()->set_transform(view_3d_controller->interp_to_camera_transform());
diff --git a/scene/debugger/runtime_node_select.h b/scene/debugger/runtime_node_select.h
index db4f7747b67..b40d03fc13d 100644
--- a/scene/debugger/runtime_node_select.h
+++ b/scene/debugger/runtime_node_select.h
@@ -33,16 +33,13 @@
#ifdef DEBUG_ENABLED
#include "core/object/ref_counted.h"
+#include "scene/debugger/canvas_item_manipulator.h"
#ifndef _3D_DISABLED
#include "scene/debugger/view_3d_controller.h"
#include "scene/resources/mesh.h"
#endif // _3D_DISABLED
-class InputEvent;
-class InputEventMouseMotion;
-class InputEventWithModifiers;
-class Node;
class PopupMenu;
class ViewPanner;
@@ -73,12 +70,6 @@ private:
NodeType node_select_type = NODE_TYPE_2D;
SelectMode node_select_mode = SELECT_MODE_SINGLE;
- struct SelectResult {
- Node *item = nullptr;
- real_t order = 0;
- _FORCE_INLINE_ bool operator<(const SelectResult &p_rr) const { return p_rr.order < order; }
- };
-
const int SELECTION_MIN_AREA = 8 * 8;
enum SelectionDragState {
SELECTION_DRAG_NONE,
@@ -92,6 +83,7 @@ private:
Point2 selection_position = Point2(Math::INF, Math::INF);
Rect2 selection_drag_area;
PopupMenu *selection_list = nullptr;
+ bool selection_list_appending = false;
Color selection_area_fill;
Color selection_area_outline;
bool selection_visible = true;
@@ -105,8 +97,11 @@ private:
RID draw_canvas;
RID sel_drag_ci;
+ float scale = 1;
+
bool camera_override = false;
bool camera_first_override = true;
+ bool is_panning = false;
// Values taken from EditorZoomWidget.
const float VIEW_2D_MIN_ZOOM = 1.0 / 128;
@@ -116,12 +111,21 @@ private:
Vector2 view_2d_offset;
real_t view_2d_zoom = 1.0;
bool warped_panning = false;
+ Ref<Shortcut> pan_shortcut;
- LocalVector<ObjectID> selected_ci_nodes;
- real_t sel_2d_grab_dist = 0;
+ Ref<CanvasItemManipulator> ci_manipulator;
+ Color accent_color;
+ Color axis_x_color;
+ Color axis_y_color;
+ Ref<Texture2D> pivot_icon;
+ Ref<Texture2D> resize_icon;
+ Ref<Texture2D> anchor_icon;
+
+ HashMap<ObjectID, Dictionary> selected_ci_nodes; // The node's ID and its canvas state.
int sel_2d_scale = 1;
Color srect_color;
+ Color srect_locked_color;
RID srect_ci;
#ifndef _3D_DISABLED
@@ -154,33 +158,44 @@ private:
void _setup(const Dictionary &p_settings);
- void _node_set_type(NodeType p_type);
- void _select_set_mode(SelectMode p_mode);
+ void _set_node_type(NodeType p_type);
void _set_camera_override_enabled(bool p_enabled);
+ void _set_ci_tool(CanvasItemManipulator::Tool p_tool);
+ void _set_ci_local_space(bool p_enabled);
+ void _set_ci_smart_snap(bool p_enabled);
+
+ void _set_n3d_tool(SelectMode p_tool);
+
void _root_window_input(const Ref<InputEvent> &p_event);
void _items_popup_index_pressed(int p_index, PopupMenu *p_popup);
void _update_input_state();
+ void _update_cursor_shape();
+ void _show_toaster(const String &p_message, int p_level);
+ void _show_camera_override_message();
+ void _show_limit_warning();
+ void _scene_double_clicked(const String &p_path);
void _process_frame();
+#ifndef _3D_DISABLED
void _physics_frame();
+#endif
- void _send_ids(const Vector<Node *> &p_picked_nodes, bool p_invert_new_selections = true);
+ void _send_ids(const Vector<Node *> &p_picked_nodes, bool p_append = false, bool p_invert = false);
void _set_selected_nodes(const Vector<Node *> &p_nodes);
void _queue_selection_update();
void _update_selection();
- void _clear_selection();
+ void _clear_selection(bool p_send_msg = true);
void _update_selection_drag(const Point2 &p_end_pos = Point2());
+ void _set_selection_area(const Rect2 &p_area);
void _set_selection_visible(bool p_visible);
void _set_avoid_locked(bool p_enabled);
void _set_prefer_group(bool p_enabled);
- void _open_selection_list(const Vector<SelectResult> &p_items, const Point2 &p_pos);
+ void _open_selection_list(const Array &p_selection, const Point2 &p_pos, bool p_append);
void _close_selection_list();
- void _find_canvas_items_at_pos(const Point2 &p_pos, Node *p_node, Vector<SelectResult> &r_items, const Transform2D &p_parent_xform = Transform2D(), const Transform2D &p_canvas_xform = Transform2D());
- void _find_canvas_items_at_rect(const Rect2 &p_rect, Node *p_node, Vector<SelectResult> &r_items, const Transform2D &p_parent_xform = Transform2D(), const Transform2D &p_canvas_xform = Transform2D());
void _pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event);
void _zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event);
#ifndef _2D_DISABLED
@@ -188,9 +203,21 @@ private:
void _update_view_2d();
#endif // _2D_DISABLED
+ Variant _find_ci_start_callback() const;
+ bool _point_selected_ci_callback(const Variant &p_node, bool p_append);
+ bool _get_selection_ci_callback(Array r_selection);
+ Transform2D _local_transform_callback() const { return Transform2D(); }
+ Point2 _local_mouse_pos_callback() const;
+ bool _plugin_input_callback(const Ref<InputEvent> &p_event) { return false; }
+
+ void _box_selected_ci(const Array &p_nodes);
+ void _save_canvas_state_requested(const Array &p_selection, bool p_save_bones);
+ void _restore_canvas_state_requested(const Array &p_selection, bool p_restore_bones);
+ void _commit_canvas_state_requested(const Array &p_selection, const String &p_message, bool p_commit_bones);
+
#ifndef _3D_DISABLED
- void _find_3d_items_at_pos(const Point2 &p_pos, Vector<SelectResult> &r_items);
- void _find_3d_items_at_rect(const Rect2 &p_rect, Vector<SelectResult> &r_items);
+ void _find_3d_items_at_pos(const Point2 &p_pos, Vector<DebuggerHelpers::SelectResult> &r_items);
+ void _find_3d_items_at_rect(const Rect2 &p_rect, Vector<DebuggerHelpers::SelectResult> &r_items);
Vector3 _get_screen_to_space(const Vector3 &p_vector3);
void _fov_scaled();
diff --git a/scene/debugger/scene_debugger.cpp b/scene/debugger/scene_debugger.cpp
index b4d3afcc9d7..6dbbf6481cc 100644
--- a/scene/debugger/scene_debugger.cpp
+++ b/scene/debugger/scene_debugger.cpp
@@ -33,6 +33,7 @@
#include "core/config/engine.h"
#include "core/debugger/debugger_marshalls.h"
#include "core/debugger/engine_debugger.h"
+#include "core/debugger/remote_debugger.h"
#include "core/input/input.h"
#include "core/input/shortcut.h"
#include "core/io/dir_access.h"
@@ -120,21 +121,70 @@ void SceneDebugger::_handle_embed_input(const Ref<InputEvent> &p_event, const Di
return;
}
- Ref<Shortcut> p_shortcut = p_settings.get("editor/next_frame_embedded_project", Ref<Shortcut>());
- if (p_shortcut.is_valid() && p_shortcut->matches_event(k)) {
- EngineDebugger::get_singleton()->send_message("request_embed_next_frame", Array());
+ LocalVector<String> shortcuts = {
+ "editor/suspend_resume_embedded_project",
+ "canvas_item_editor/select_mode",
+ "canvas_item_editor/move_mode",
+ "canvas_item_editor/rotate_mode",
+ "canvas_item_editor/scale_mode",
+ "canvas_item_editor/pan_mode",
+ "canvas_item_editor/use_local_space",
+ };
+ for (const String &key : shortcuts) {
+ Ref<Shortcut> shortcut = p_settings.get(key, Ref<Shortcut>());
+ if (shortcut.is_valid() && shortcut->matches_event(k) && !k->is_echo()) {
+ EngineDebugger::get_singleton()->send_message("editor_shortcut_pressed", { key });
+ return;
+ }
+ }
+
+ Ref<Shortcut> shortcut = p_settings.get("editor/next_frame_embedded_project", Ref<Shortcut>());
+ if (shortcut.is_valid() && shortcut->matches_event(k)) {
+ EngineDebugger::get_singleton()->send_message("editor_shortcut_pressed", { "editor/next_frame_embedded_project" });
return;
}
- if (k->is_echo()) {
+ if (RuntimeNodeSelect::get_singleton()->node_select_type == RuntimeNodeSelect::NODE_TYPE_NONE) {
+ return; // Ignore undo/redo actions in Input mode, as it can result in unwanted edits.
+ }
+
+ String action;
+ shortcut = p_settings.get("ui_undo", Ref<Shortcut>());
+ if (shortcut.is_valid() && shortcut->matches_event(k)) {
+ action = "ui_undo";
+ } else {
+ shortcut = p_settings.get("ui_redo", Ref<Shortcut>());
+ if (shortcut.is_valid() && shortcut->matches_event(k)) {
+ action = "ui_redo";
+ }
+ }
+
+ if (action.is_empty()) {
return;
- } // Shortcuts that doesn't need is_echo goes below here
+ }
+
+ Input *input = Input::get_singleton();
+ bool was_input_disabled = input->is_input_disabled();
+ if (was_input_disabled) {
+ input->set_disable_input(false);
+ }
- p_shortcut = p_settings.get("editor/suspend_resume_embedded_project", Ref<Shortcut>());
- if (p_shortcut.is_valid() && p_shortcut->matches_event(k)) {
- EngineDebugger::get_singleton()->send_message("request_embed_suspend_toggle", Array());
+ const bool mouse_pressed = (int)Input::get_singleton()->get_mouse_button_mask() & 0x7;
+
+ if (was_input_disabled) {
+ input->set_disable_input(true);
+ }
+
+ // Undo/redo actions should not be done while mouse buttons are pressed,
+ // since it could mean that nodes are being manipulated.
+ if (mouse_pressed) {
+ Vector<String> strings = { RTR("Can't undo while mouse buttons are pressed.") };
+ Vector<int> types = { RemoteDebugger::MESSAGE_TYPE_EDITOR };
+ EngineDebugger::get_singleton()->send_message("output", { strings, types });
return;
}
+
+ EngineDebugger::get_singleton()->send_message("editor_shortcut_pressed", { action });
}
void SceneDebugger::_on_window_size_changed() {
@@ -182,6 +232,17 @@ Error SceneDebugger::_msg_inspect_objects(const Array &p_args) {
return OK;
}
+Error SceneDebugger::_msg_change_canvas_item_states(const Array &p_args) {
+ ERR_FAIL_COND_V(p_args.size() != 1, ERR_INVALID_DATA);
+ for (KeyValue kv : (Dictionary)p_args[0]) {
+ CanvasItem *ci = ObjectDB::get_instance<CanvasItem>(kv.key);
+ if (ci) {
+ ci->_edit_set_state(kv.value);
+ }
+ }
+ return OK;
+}
+
#ifndef DISABLE_DEPRECATED
Error SceneDebugger::_msg_inspect_object(const Array &p_args) {
ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA);
@@ -451,21 +512,35 @@ Error SceneDebugger::_msg_runtime_node_select_setup(const Array &p_args) {
return OK;
}
-Error SceneDebugger::_msg_runtime_node_select_set_type(const Array &p_args) {
+Error SceneDebugger::_msg_runtime_node_select_set_node_type(const Array &p_args) {
+ ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA);
+ RuntimeNodeSelect::NodeType type = (RuntimeNodeSelect::NodeType)p_args[0];
+ RuntimeNodeSelect::get_singleton()->_set_node_type(type);
+ return OK;
+}
+
+Error SceneDebugger::_msg_runtime_node_select_set_ci_tool(const Array &p_args) {
+ ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA);
+ CanvasItemManipulator::Tool tool = (CanvasItemManipulator::Tool)p_args[0];
+ RuntimeNodeSelect::get_singleton()->_set_ci_tool(tool);
+ return OK;
+}
+
+Error SceneDebugger::_msg_runtime_node_select_set_ci_local_space(const Array &p_args) {
ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA);
- RuntimeNodeSelect::NodeType type = (RuntimeNodeSelect::NodeType)(int)p_args[0];
- RuntimeNodeSelect::get_singleton()->_node_set_type(type);
+ bool enabled = p_args[0];
+ RuntimeNodeSelect::get_singleton()->_set_ci_local_space(enabled);
return OK;
}
-Error SceneDebugger::_msg_runtime_node_select_set_mode(const Array &p_args) {
+Error SceneDebugger::_msg_runtime_node_select_set_n3d_tool(const Array &p_args) {
ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA);
- RuntimeNodeSelect::SelectMode mode = (RuntimeNodeSelect::SelectMode)(int)p_args[0];
- RuntimeNodeSelect::get_singleton()->_select_set_mode(mode);
+ RuntimeNodeSelect::SelectMode tool = (RuntimeNodeSelect::SelectMode)p_args[0];
+ RuntimeNodeSelect::get_singleton()->_set_n3d_tool(tool);
return OK;
}
-Error SceneDebugger::_msg_runtime_node_select_set_visible(const Array &p_args) {
+Error SceneDebugger::_msg_runtime_node_select_set_selection_visible(const Array &p_args) {
ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA);
bool visible = p_args[0];
RuntimeNodeSelect::get_singleton()->_set_selection_visible(visible);
@@ -620,6 +695,7 @@ void SceneDebugger::_init_message_handlers() {
message_handlers["request_scene_tree"] = _msg_request_scene_tree;
message_handlers["save_node"] = _msg_save_node;
message_handlers["inspect_objects"] = _msg_inspect_objects;
+ message_handlers["change_canvas_item_states"] = _msg_change_canvas_item_states;
#ifndef DISABLE_DEPRECATED
message_handlers["inspect_object"] = _msg_inspect_object;
#endif // DISABLE_DEPRECATED
@@ -659,9 +735,11 @@ void SceneDebugger::_init_message_handlers() {
message_handlers["live_duplicate_node"] = _msg_live_duplicate_node;
message_handlers["live_reparent_node"] = _msg_live_reparent_node;
message_handlers["runtime_node_select_setup"] = _msg_runtime_node_select_setup;
- message_handlers["runtime_node_select_set_type"] = _msg_runtime_node_select_set_type;
- message_handlers["runtime_node_select_set_mode"] = _msg_runtime_node_select_set_mode;
- message_handlers["runtime_node_select_set_visible"] = _msg_runtime_node_select_set_visible;
+ message_handlers["runtime_node_select_set_node_type"] = _msg_runtime_node_select_set_node_type;
+ message_handlers["runtime_node_select_set_selection_visible"] = _msg_runtime_node_select_set_selection_visible;
+ message_handlers["runtime_node_select_set_ci_tool"] = _msg_runtime_node_select_set_ci_tool;
+ message_handlers["runtime_node_select_set_ci_local_space"] = _msg_runtime_node_select_set_ci_local_space;
+ message_handlers["runtime_node_select_set_n3d_tool"] = _msg_runtime_node_select_set_n3d_tool;
message_handlers["runtime_node_select_set_avoid_locked"] = _msg_runtime_node_select_set_avoid_locked;
message_handlers["runtime_node_select_set_prefer_group"] = _msg_runtime_node_select_set_prefer_group;
#ifndef _2D_DISABLED
@@ -713,7 +791,7 @@ void SceneDebugger::_send_object_ids(const Vector<ObjectID> &p_ids, bool p_updat
Vector<ObjectID> ids = p_ids;
if (ids.size() > RuntimeNodeSelect::get_singleton()->max_selection) {
ids.resize(RuntimeNodeSelect::get_singleton()->max_selection);
- EngineDebugger::get_singleton()->send_message("show_selection_limit_warning", Array());
+ RuntimeNodeSelect::get_singleton()->_show_limit_warning();
}
LocalVector<Node *> nodes;
@@ -747,11 +825,8 @@ void SceneDebugger::_send_object_ids(const Vector<ObjectID> &p_ids, bool p_updat
invalid_selection.append(id);
}
- Array arr;
- arr.append(invalid_selection);
- EngineDebugger::get_singleton()->send_message("remote_selection_invalidated", arr);
-
EngineDebugger::get_singleton()->send_message(objs.is_empty() ? "remote_nothing_selected" : "remote_objects_selected", objs);
+ EngineDebugger::get_singleton()->send_message("remote_selection_invalidated", { invalid_selection });
} else {
EngineDebugger::get_singleton()->send_message(p_update_selection ? "remote_objects_selected" : "scene:inspect_objects", objs);
}
diff --git a/scene/debugger/scene_debugger.h b/scene/debugger/scene_debugger.h
index 8176ebb662a..36eceb118ff 100644
--- a/scene/debugger/scene_debugger.h
+++ b/scene/debugger/scene_debugger.h
@@ -74,6 +74,7 @@ private:
static Error _msg_request_scene_tree(const Array &p_args);
static Error _msg_save_node(const Array &p_args);
static Error _msg_inspect_objects(const Array &p_args);
+ static Error _msg_change_canvas_item_states(const Array &p_args);
#ifndef DISABLE_DEPRECATED
static Error _msg_inspect_object(const Array &p_args);
#endif // DISABLE_DEPRECATED
@@ -107,9 +108,11 @@ private:
static Error _msg_live_duplicate_node(const Array &p_args);
static Error _msg_live_reparent_node(const Array &p_args);
static Error _msg_runtime_node_select_setup(const Array &p_args);
- static Error _msg_runtime_node_select_set_type(const Array &p_args);
- static Error _msg_runtime_node_select_set_mode(const Array &p_args);
- static Error _msg_runtime_node_select_set_visible(const Array &p_args);
+ static Error _msg_runtime_node_select_set_node_type(const Array &p_args);
+ static Error _msg_runtime_node_select_set_ci_tool(const Array &p_args);
+ static Error _msg_runtime_node_select_set_ci_local_space(const Array &p_args);
+ static Error _msg_runtime_node_select_set_n3d_tool(const Array &p_args);
+ static Error _msg_runtime_node_select_set_selection_visible(const Array &p_args);
static Error _msg_runtime_node_select_set_avoid_locked(const Array &p_args);
static Error _msg_runtime_node_select_set_prefer_group(const Array &p_args);
static Error _msg_rq_screenshot(const Array &p_args);
diff --git a/scene/debugger/view_3d_controller.cpp b/scene/debugger/view_3d_controller.cpp
index 486600ac72c..b652b6dbe73 100644
--- a/scene/debugger/view_3d_controller.cpp
+++ b/scene/debugger/view_3d_controller.cpp
@@ -28,13 +28,14 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
-#ifndef _3D_DISABLED
+#if defined(DEBUG_ENABLED) && !defined(_3D_DISABLED)
#include "view_3d_controller.h"
#include "core/config/engine.h"
#include "core/input/input.h"
#include "core/input/shortcut.h"
+#include "scene/debugger/debugger_helpers.h"
#include "scene/main/scene_tree.h"
using namespace View3DControllerConsts;
@@ -54,37 +55,6 @@ Transform3D View3DController::_to_camera_transform(const Cursor &p_cursor) const
return camera_transform;
}
-bool View3DController::_is_shortcut_pressed(const ShortcutName p_name, const bool p_true_if_empty) {
- Ref<Shortcut> shortcut = inputs[p_name];
- if (shortcut.is_null()) {
- return p_true_if_empty;
- }
-
- const Array shortcuts = shortcut->get_events();
- if (shortcuts.is_empty()) {
- return p_true_if_empty;
- }
-
- for (Ref<InputEventKey> k : shortcuts) {
- if (k.is_null()) {
- continue;
- }
-
- if (k->get_physical_keycode() == Key::NONE && Input::get_singleton()->is_key_pressed(emulate_numpad_key(k->get_keycode()))) {
- return true;
- } else if (Input::get_singleton()->is_physical_key_pressed(emulate_numpad_key(k->get_physical_keycode()))) {
- return true;
- }
- }
-
- return false;
-}
-
-bool View3DController::_is_shortcut_empty(const ShortcutName p_name) {
- Ref<Shortcut> shortcut = inputs[p_name];
- return shortcut.is_null() || shortcut->get_events().is_empty();
-}
-
View3DController::NavigationMode View3DController::_get_nav_mode_from_shortcuts(NavigationMouseButton p_mouse_button, const Vector<ShortcutCheck> &p_shortcut_checks, bool p_not_empty) {
if (p_not_empty) {
for (const ShortcutCheck &shortcut_check : p_shortcut_checks) {
@@ -140,15 +110,15 @@ bool View3DController::gui_input(const Ref<InputEvent> &p_event, const Rect2 &p_
if (Input::get_singleton()->get_mouse_mode() != Input::MouseMode::MOUSE_MODE_CAPTURED) {
#define GET_SHORTCUT_COUNT(p_name) (inputs[p_name].is_null() ? 0 : inputs[p_name]->get_events().size())
- bool orbit_mod_pressed = _is_shortcut_pressed(SHORTCUT_ORBIT_MOD_1, true) && _is_shortcut_pressed(SHORTCUT_ORBIT_MOD_2, true);
- bool pan_mod_pressed = _is_shortcut_pressed(SHORTCUT_PAN_MOD_1, true) && _is_shortcut_pressed(SHORTCUT_PAN_MOD_2, true);
- bool zoom_mod_pressed = _is_shortcut_pressed(SHORTCUT_ZOOM_MOD_1, true) && _is_shortcut_pressed(SHORTCUT_ZOOM_MOD_2, true);
+ bool orbit_mod_pressed = DebuggerHelpers::is_shortcut_pressed(SHORTCUT_ORBIT_MOD_1, inputs, true) && DebuggerHelpers::is_shortcut_pressed(SHORTCUT_ORBIT_MOD_2, inputs, true);
+ bool pan_mod_pressed = DebuggerHelpers::is_shortcut_pressed(SHORTCUT_PAN_MOD_1, inputs, true) && DebuggerHelpers::is_shortcut_pressed(SHORTCUT_PAN_MOD_2, inputs, true);
+ bool zoom_mod_pressed = DebuggerHelpers::is_shortcut_pressed(SHORTCUT_ZOOM_MOD_1, inputs, true) && DebuggerHelpers::is_shortcut_pressed(SHORTCUT_ZOOM_MOD_2, inputs, true);
int orbit_mod_input_count = GET_SHORTCUT_COUNT(SHORTCUT_ORBIT_MOD_1) + GET_SHORTCUT_COUNT(SHORTCUT_ORBIT_MOD_2);
int pan_mod_input_count = GET_SHORTCUT_COUNT(SHORTCUT_PAN_MOD_1) + GET_SHORTCUT_COUNT(SHORTCUT_PAN_MOD_2);
int zoom_mod_input_count = GET_SHORTCUT_COUNT(SHORTCUT_ZOOM_MOD_1) + GET_SHORTCUT_COUNT(SHORTCUT_ZOOM_MOD_2);
- bool orbit_not_empty = !_is_shortcut_empty(SHORTCUT_ORBIT_MOD_1) || !_is_shortcut_empty(SHORTCUT_ORBIT_MOD_2);
- bool pan_not_empty = !_is_shortcut_empty(SHORTCUT_PAN_MOD_1) || !_is_shortcut_empty(SHORTCUT_PAN_MOD_2);
- bool zoom_not_empty = !_is_shortcut_empty(SHORTCUT_ZOOM_MOD_1) || !_is_shortcut_empty(SHORTCUT_ZOOM_MOD_2);
+ bool orbit_not_empty = !DebuggerHelpers::is_shortcut_empty(SHORTCUT_ORBIT_MOD_1, inputs) || !DebuggerHelpers::is_shortcut_empty(SHORTCUT_ORBIT_MOD_2, inputs);
+ bool pan_not_empty = !DebuggerHelpers::is_shortcut_empty(SHORTCUT_PAN_MOD_1, inputs) || !DebuggerHelpers::is_shortcut_empty(SHORTCUT_PAN_MOD_2, inputs);
+ bool zoom_not_empty = !DebuggerHelpers::is_shortcut_empty(SHORTCUT_ZOOM_MOD_1, inputs) || !DebuggerHelpers::is_shortcut_empty(SHORTCUT_ZOOM_MOD_2, inputs);
shortcut_checks.push_back(ShortcutCheck(orbit_mod_pressed, orbit_not_empty, orbit_mod_input_count, orbit_mouse_button, NAV_MODE_ORBIT));
shortcut_checks.push_back(ShortcutCheck(pan_mod_pressed, pan_not_empty, pan_mod_input_count, pan_mouse_button, NAV_MODE_PAN));
shortcut_checks.push_back(ShortcutCheck(zoom_mod_pressed, zoom_not_empty, zoom_mod_input_count, zoom_mouse_button, NAV_MODE_ZOOM));
@@ -299,15 +269,15 @@ bool View3DController::gui_input(const Ref<InputEvent> &p_event, const Rect2 &p_
bool pressed = false;
float old_fov_scale = cursor.fov_scale;
- if (_is_shortcut_pressed(SHORTCUT_FOV_DECREASE)) {
+ if (DebuggerHelpers::is_shortcut_pressed(SHORTCUT_FOV_DECREASE, inputs)) {
cursor.fov_scale = CLAMP(cursor.fov_scale - 0.05, CAMERA_MIN_FOV_SCALE, CAMERA_MAX_FOV_SCALE);
pressed = true;
}
- if (_is_shortcut_pressed(SHORTCUT_FOV_INCREASE)) {
+ if (DebuggerHelpers::is_shortcut_pressed(SHORTCUT_FOV_INCREASE, inputs)) {
cursor.fov_scale = CLAMP(cursor.fov_scale + 0.05, CAMERA_MIN_FOV_SCALE, CAMERA_MAX_FOV_SCALE);
pressed = true;
}
- if (_is_shortcut_pressed(SHORTCUT_FOV_RESET)) {
+ if (DebuggerHelpers::is_shortcut_pressed(SHORTCUT_FOV_RESET, inputs)) {
cursor.fov_scale = 1;
pressed = true;
}
@@ -366,8 +336,8 @@ void View3DController::cursor_orbit(const Ref<InputEventWithModifiers> &p_event,
ViewType new_view_type = VIEW_TYPE_USER;
- bool snap_modifier_configured = !_is_shortcut_empty(SHORTCUT_ORBIT_SNAP_MOD_1) || !_is_shortcut_empty(SHORTCUT_ORBIT_SNAP_MOD_2);
- if (snap_modifier_configured && _is_shortcut_pressed(SHORTCUT_ORBIT_SNAP_MOD_1, true) && _is_shortcut_pressed(SHORTCUT_ORBIT_SNAP_MOD_2, true)) {
+ bool snap_modifier_configured = !DebuggerHelpers::is_shortcut_empty(SHORTCUT_ORBIT_SNAP_MOD_1, inputs) || !DebuggerHelpers::is_shortcut_empty(SHORTCUT_ORBIT_SNAP_MOD_2, inputs);
+ if (snap_modifier_configured && DebuggerHelpers::is_shortcut_pressed(SHORTCUT_ORBIT_SNAP_MOD_1, inputs, true) && DebuggerHelpers::is_shortcut_pressed(SHORTCUT_ORBIT_SNAP_MOD_2, inputs, true)) {
const float snap_angle = Math::deg_to_rad(45.0);
const float snap_threshold = Math::deg_to_rad(angle_snap_threshold);
@@ -565,31 +535,31 @@ void View3DController::update_freelook(const float p_delta) {
}
Vector3 direction;
- if (_is_shortcut_pressed(SHORTCUT_FREELOOK_LEFT)) {
+ if (DebuggerHelpers::is_shortcut_pressed(SHORTCUT_FREELOOK_LEFT, inputs)) {
direction -= right;
}
- if (_is_shortcut_pressed(SHORTCUT_FREELOOK_RIGHT)) {
+ if (DebuggerHelpers::is_shortcut_pressed(SHORTCUT_FREELOOK_RIGHT, inputs)) {
direction += right;
}
- if (_is_shortcut_pressed(SHORTCUT_FREELOOK_FORWARD)) {
+ if (DebuggerHelpers::is_shortcut_pressed(SHORTCUT_FREELOOK_FORWARD, inputs)) {
direction += forward;
}
- if (_is_shortcut_pressed(SHORTCUT_FREELOOK_BACKWARDS)) {
+ if (DebuggerHelpers::is_shortcut_pressed(SHORTCUT_FREELOOK_BACKWARDS, inputs)) {
direction -= forward;
}
- if (_is_shortcut_pressed(SHORTCUT_FREELOOK_UP)) {
+ if (DebuggerHelpers::is_shortcut_pressed(SHORTCUT_FREELOOK_UP, inputs)) {
direction += up;
}
- if (_is_shortcut_pressed(SHORTCUT_FREELOOK_DOWN)) {
+ if (DebuggerHelpers::is_shortcut_pressed(SHORTCUT_FREELOOK_DOWN, inputs)) {
direction -= up;
}
real_t speed = freelook_speed;
- if (_is_shortcut_pressed(SHORTCUT_FREELOOK_SPEED_MOD)) {
+ if (DebuggerHelpers::is_shortcut_pressed(SHORTCUT_FREELOOK_SPEED_MOD, inputs)) {
speed *= 3.0;
}
- if (_is_shortcut_pressed(SHORTCUT_FREELOOK_SLOW_MOD)) {
+ if (DebuggerHelpers::is_shortcut_pressed(SHORTCUT_FREELOOK_SLOW_MOD, inputs)) {
speed *= 0.333333;
}
@@ -641,7 +611,6 @@ Key View3DController::emulate_numpad_key(const Key p_code) const {
void View3DController::set_shortcut(const ShortcutName p_name, const Ref<Shortcut> &p_shortcut) {
ERR_FAIL_INDEX(0, SHORTCUT_MAX);
- ERR_FAIL_COND(p_shortcut.is_null());
inputs[p_name] = p_shortcut;
}
@@ -817,4 +786,4 @@ void View3DController::_bind_methods() {
ADD_SIGNAL(MethodInfo("cursor_distance_scaled"));
}
-#endif // _3D_DISABLED
+#endif // DEBUG_ENABLED && _3D_DISABLED
diff --git a/scene/debugger/view_3d_controller.h b/scene/debugger/view_3d_controller.h
index 7d2f7415158..d0cf955a7e7 100644
--- a/scene/debugger/view_3d_controller.h
+++ b/scene/debugger/view_3d_controller.h
@@ -30,7 +30,7 @@
#pragma once
-#ifndef _3D_DISABLED
+#if defined(DEBUG_ENABLED) && !defined(_3D_DISABLED)
#include "core/object/ref_counted.h"
#include "core/os/keyboard.h"
@@ -259,8 +259,6 @@ private:
}
};
- bool _is_shortcut_pressed(const ShortcutName p_name, const bool p_true_if_empty = false);
- bool _is_shortcut_empty(const ShortcutName p_name);
NavigationMode _get_nav_mode_from_shortcuts(NavigationMouseButton p_mouse_button, const Vector<ShortcutCheck> &p_shortcut_checks, bool p_not_empty);
public:
@@ -342,4 +340,4 @@ public:
int get_zoom_failed_attempts_count() const { return zoom_failed_attempts_count; }
};
-#endif // _3D_DISABLED
+#endif // DEBUG_ENABLED && _3D_DISABLED
diff --git a/scene/gui/control.cpp b/scene/gui/control.cpp
index 23052d13699..205c78cf636 100644
--- a/scene/gui/control.cpp
+++ b/scene/gui/control.cpp
@@ -89,8 +89,7 @@ public:
// Editor plugin interoperability.
-// TODO: Decouple controls from their editor plugin and get rid of this.
-#ifdef TOOLS_ENABLED
+#ifdef DEBUG_ENABLED
Dictionary Control::_edit_get_state() const {
Dictionary s;
s["rotation"] = get_rotation();
@@ -153,8 +152,14 @@ void Control::_edit_set_state(const Dictionary &p_state) {
}
void Control::_edit_set_position(const Point2 &p_position) {
- ERR_FAIL_COND_MSG(!Engine::get_singleton()->is_editor_hint(), "This function can only be used from editor plugins.");
- set_position(p_position, ControlEditorToolbar::get_singleton()->is_anchors_mode_enabled() && get_parent_control());
+#ifdef TOOLS_ENABLED
+ if (Engine::get_singleton()->is_editor_hint()) {
+ set_position(p_position, ControlEditorToolbar::get_singleton()->is_anchors_mode_enabled() && get_parent_control());
+ } else
+#endif
+ {
+ set_position(p_position);
+ }
}
Point2 Control::_edit_get_position() const {
@@ -170,13 +175,20 @@ Size2 Control::_edit_get_scale() const {
}
void Control::_edit_set_rect(const Rect2 &p_edit_rect) {
- ERR_FAIL_COND_MSG(!Engine::get_singleton()->is_editor_hint(), "This function can only be used from editor plugins.");
// Changing the size might change the internal transform (in case of non-zero `pivot_offset_ratio`),
// hence `position` (which is in the parent space, and is not always equivalent to the Control's rect top-left corner)
// needs to be changed after `size` (which is local) and needs to account for the possibly changed internal transform.
Vector2 rect_new_pos_in_parent_space = get_transform().xform(p_edit_rect.position);
- set_size(p_edit_rect.size, ControlEditorToolbar::get_singleton()->is_anchors_mode_enabled());
- set_position(rect_new_pos_in_parent_space - _get_internal_transform().get_origin(), ControlEditorToolbar::get_singleton()->is_anchors_mode_enabled());
+#ifdef TOOLS_ENABLED
+ if (Engine::get_singleton()->is_editor_hint()) {
+ set_size(p_edit_rect.size, ControlEditorToolbar::get_singleton()->is_anchors_mode_enabled());
+ set_position(rect_new_pos_in_parent_space - _get_internal_transform().get_origin(), ControlEditorToolbar::get_singleton()->is_anchors_mode_enabled());
+ } else
+#endif
+ {
+ set_size(p_edit_rect.size);
+ set_position(rect_new_pos_in_parent_space - _get_internal_transform().get_origin());
+ }
}
void Control::_edit_set_rotation(real_t p_rotation) {
@@ -210,9 +222,7 @@ bool Control::_edit_use_pivot() const {
Size2 Control::_edit_get_minimum_size() const {
return get_combined_minimum_size();
}
-#endif // TOOLS_ENABLED
-#ifdef DEBUG_ENABLED
Rect2 Control::_edit_get_rect() const {
return Rect2(Point2(), get_size());
}
diff --git a/scene/gui/control.h b/scene/gui/control.h
index a124a3b698b..9a9f90cf0be 100644
--- a/scene/gui/control.h
+++ b/scene/gui/control.h
@@ -517,8 +517,7 @@ public:
// Editor plugin interoperability.
- // TODO: Decouple controls from their editor plugin and get rid of this.
-#ifdef TOOLS_ENABLED
+#ifdef DEBUG_ENABLED
virtual Dictionary _edit_get_state() const override;
virtual void _edit_set_state(const Dictionary &p_state) override;
@@ -539,9 +538,7 @@ public:
virtual bool _edit_use_pivot() const override;
virtual Size2 _edit_get_minimum_size() const override;
-#endif //TOOLS_ENABLED
-#ifdef DEBUG_ENABLED
virtual Rect2 _edit_get_rect() const override;
virtual bool _edit_use_rect() const override;
#endif // DEBUG_ENABLED
diff --git a/scene/main/canvas_item.cpp b/scene/main/canvas_item.cpp
index 02de6900d3d..274c9db4ab4 100644
--- a/scene/main/canvas_item.cpp
+++ b/scene/main/canvas_item.cpp
@@ -63,13 +63,11 @@ bool CanvasItem::_edit_is_selected_on_click(const Point2 &p_point, double p_tole
return p_point.length() < p_tolerance;
}
}
-#endif // DEBUG_ENABLED
-#ifdef TOOLS_ENABLED
Transform2D CanvasItem::_edit_get_transform() const {
return Transform2D(_edit_get_rotation(), _edit_get_position() + _edit_get_pivot());
}
-#endif //TOOLS_ENABLED
+#endif // DEBUG_ENABLED
bool CanvasItem::is_visible_in_tree() const {
ERR_READ_THREAD_GUARD_V(false);
diff --git a/scene/main/canvas_item.h b/scene/main/canvas_item.h
index 90b80f2dd9f..5b40b190396 100644
--- a/scene/main/canvas_item.h
+++ b/scene/main/canvas_item.h
@@ -243,7 +243,7 @@ public:
/* EDITOR AND DEBUGGING */
-#ifdef TOOLS_ENABLED
+#ifdef DEBUG_ENABLED
// Save and restore a CanvasItem state
virtual void _edit_set_state(const Dictionary &p_state) {}
virtual Dictionary _edit_get_state() const { return Dictionary(); }
@@ -271,10 +271,6 @@ public:
virtual Point2 _edit_get_pivot() const { return Point2(); }
virtual Transform2D _edit_get_transform() const;
-#endif // TOOLS_ENABLED
-
-#ifdef DEBUG_ENABLED
- // Those need to be available in debug runtime, to allow for node selection.
// Select the node.
virtual bool _edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const;
diff --git a/scene/main/viewport.cpp b/scene/main/viewport.cpp
index a50214c9c56..cd522ce95b5 100644
--- a/scene/main/viewport.cpp
+++ b/scene/main/viewport.cpp
@@ -4474,7 +4474,7 @@ bool Viewport::get_canvas_cull_mask_bit(uint32_t p_layer) const {
return (canvas_cull_mask & (1 << p_layer));
}
-#ifdef TOOLS_ENABLED
+#ifdef DEBUG_ENABLED
bool Viewport::is_visible_subviewport() const {
if (!is_sub_viewport()) {
return true;
@@ -4482,7 +4482,7 @@ bool Viewport::is_visible_subviewport() const {
SubViewportContainer *container = Object::cast_to<SubViewportContainer>(get_parent());
return container && container->is_visible_in_tree();
}
-#endif // TOOLS_ENABLED
+#endif // DEBUG_ENABLED
void Viewport::_update_audio_listener_2d() {
if (AudioServer::get_singleton()) {
diff --git a/scene/main/viewport.h b/scene/main/viewport.h
index 741277ec5b7..46d432daab4 100644
--- a/scene/main/viewport.h
+++ b/scene/main/viewport.h
@@ -753,9 +753,9 @@ public:
void set_canvas_cull_mask_bit(uint32_t p_layer, bool p_enable);
bool get_canvas_cull_mask_bit(uint32_t p_layer) const;
-#ifdef TOOLS_ENABLED
+#ifdef DEBUG_ENABLED
bool is_visible_subviewport() const;
-#endif // TOOLS_ENABLED
+#endif // DEBUG_ENABLED
virtual bool is_size_2d_override_stretch_enabled() const { return true; }
diff --git a/scene/theme/default_theme.cpp b/scene/theme/default_theme.cpp
index 44a110027fc..eb4599cf48f 100644
--- a/scene/theme/default_theme.cpp
+++ b/scene/theme/default_theme.cpp
@@ -1386,6 +1386,14 @@ void fill_default_theme(Ref<Theme> &theme, const Ref<Font> &default_font, const
theme->set_icon("resizer", "GraphEditMinimap", icons["resizer_nw"]);
theme->set_color("resizer_color", "GraphEditMinimap", Color(1, 1, 1, 0.85));
+ // Debug
+
+#ifdef DEBUG_ENABLED
+ theme->set_icon("pivot", "Debug", icons["debug_pivot"]);
+ theme->set_icon("resize", "Debug", icons["debug_resize"]);
+ theme->set_icon("anchor", "Debug", icons["debug_anchor"]);
+#endif
+
// Theme
default_icon = icons["error_icon"];
diff --git a/scene/theme/icons/SCsub b/scene/theme/icons/SCsub
index 5da556f3468..bbb45ae016a 100644
--- a/scene/theme/icons/SCsub
+++ b/scene/theme/icons/SCsub
@@ -5,8 +5,13 @@ Import("env")
import default_theme_icons_builders
+sources = Glob("*.svg")
+if env["target"] in ["editor", "template_debug"]:
+ # These are only needed for remote debugging.
+ sources += Glob("debug_icons/*.svg")
+
env.CommandNoCache(
"#scene/theme/default_theme_icons.gen.h",
- Glob("*.svg"),
+ sources,
env.Run(default_theme_icons_builders.make_default_theme_icons_action),
)
diff --git a/scene/theme/icons/debug_icons/debug_anchor.svg b/scene/theme/icons/debug_icons/debug_anchor.svg
new file mode 100644
index 00000000000..ffc07d930c4
--- /dev/null
+++ b/scene/theme/icons/debug_icons/debug_anchor.svg
@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16"><path fill-opacity=".4" d="M16 16 9.883 6.06a5 5 0 1 0-3.826 3.82z"/><path fill="#8eef97" d="M16 16 8.832 6.144A4 4 0 1 0 6.14 8.83z"/></svg>
diff --git a/scene/theme/icons/debug_icons/debug_pivot.svg b/scene/theme/icons/debug_icons/debug_pivot.svg
new file mode 100644
index 00000000000..3fb37c1fede
--- /dev/null
+++ b/scene/theme/icons/debug_icons/debug_pivot.svg
@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16"><path fill="#fff" fill-opacity=".706" d="m6 0v6h-6v4h6v6h4v-6h6v-4h-6v-6zm1 7h2v2h-2z"/><path fill="#ff5f5f" d="m7 1v5h2v-5zm-6 6v2h5v-2zm9 0v2h5v-2zm-3 3v5h2v-5z"/></svg>
diff --git a/scene/theme/icons/debug_icons/debug_resize.svg b/scene/theme/icons/debug_icons/debug_resize.svg
new file mode 100644
index 00000000000..b4cc81aa2c5
--- /dev/null
+++ b/scene/theme/icons/debug_icons/debug_resize.svg
@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="10" height="10"><circle cx="5" cy="5" r="5" fill-opacity=".294"/><circle cx="5" cy="5" r="3.5" fill="#ff5f5f" stroke="#fff"/></svg>