| 1 | /**************************************************************************/ |
| 2 | /* scene_debugger.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "scene_debugger.h" |
| 32 | |
| 33 | #include "core/config/project_settings.h" |
| 34 | #include "core/debugger/debugger_marshalls.h" |
| 35 | #include "core/debugger/engine_debugger.h" |
| 36 | #include "core/io/dir_access.h" |
| 37 | #include "core/io/marshalls.h" |
| 38 | #include "core/io/resource_loader.h" |
| 39 | #include "core/io/resource_saver.h" |
| 40 | #include "core/math/math_fieldwise.h" |
| 41 | #include "core/object/script_language.h" |
| 42 | #include "core/os/time.h" |
| 43 | #include "core/templates/local_vector.h" |
| 44 | #include "scene/2d/camera_2d.h" |
| 45 | #include "scene/gui/popup_menu.h" |
| 46 | #include "scene/main/canvas_layer.h" |
| 47 | #include "scene/main/scene_tree.h" |
| 48 | #include "scene/main/window.h" |
| 49 | #include "scene/resources/packed_scene.h" |
| 50 | #include "scene/theme/theme_db.h" |
| 51 | #include "servers/audio/audio_server.h" |
| 52 | |
| 53 | #ifndef PHYSICS_2D_DISABLED |
| 54 | #include "scene/2d/physics/collision_object_2d.h" |
| 55 | #include "scene/2d/physics/collision_polygon_2d.h" |
| 56 | #include "scene/2d/physics/collision_shape_2d.h" |
| 57 | #endif // PHYSICS_2D_DISABLED |
| 58 | |
| 59 | #ifndef _3D_DISABLED |
| 60 | #include "scene/3d/camera_3d.h" |
| 61 | #ifndef PHYSICS_3D_DISABLED |
| 62 | #include "scene/3d/physics/collision_object_3d.h" |
| 63 | #include "scene/3d/physics/collision_shape_3d.h" |
| 64 | #endif // PHYSICS_3D_DISABLED |
| 65 | #include "scene/3d/visual_instance_3d.h" |
| 66 | #include "scene/resources/3d/convex_polygon_shape_3d.h" |
| 67 | #include "scene/resources/surface_tool.h" |
| 68 | #endif // _3D_DISABLED |
| 69 | |
| 70 | SceneDebugger::SceneDebugger() { |
| 71 | singleton = this; |
| 72 | |
| 73 | #ifdef DEBUG_ENABLED |
| 74 | LiveEditor::singleton = memnew(LiveEditor); |
| 75 | RuntimeNodeSelect::singleton = memnew(RuntimeNodeSelect); |
| 76 | |
| 77 | EngineDebugger::register_message_capture("scene", EngineDebugger::Capture(nullptr, SceneDebugger::parse_message)); |
| 78 | #endif // DEBUG_ENABLED |
| 79 | } |
| 80 | |
| 81 | SceneDebugger::~SceneDebugger() { |
| 82 | #ifdef DEBUG_ENABLED |
| 83 | if (LiveEditor::singleton) { |
| 84 | EngineDebugger::unregister_message_capture("scene"); |
| 85 | memdelete(LiveEditor::singleton); |
| 86 | LiveEditor::singleton = nullptr; |
| 87 | } |
| 88 | |
| 89 | if (RuntimeNodeSelect::singleton) { |
| 90 | memdelete(RuntimeNodeSelect::singleton); |
| 91 | RuntimeNodeSelect::singleton = nullptr; |
| 92 | } |
| 93 | #endif // DEBUG_ENABLED |
| 94 | |
| 95 | singleton = nullptr; |
| 96 | } |
| 97 | |
| 98 | void SceneDebugger::initialize() { |
| 99 | if (EngineDebugger::is_active()) { |
| 100 | #ifdef DEBUG_ENABLED |
| 101 | _init_message_handlers(); |
| 102 | #endif |
| 103 | memnew(SceneDebugger); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void SceneDebugger::deinitialize() { |
| 108 | if (singleton) { |
| 109 | memdelete(singleton); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | #ifdef DEBUG_ENABLED |
| 114 | |
| 115 | void SceneDebugger::_handle_input(const Ref<InputEvent> &p_event, const Ref<Shortcut> &p_shortcut) { |
| 116 | Ref<InputEventKey> k = p_event; |
| 117 | if (p_shortcut.is_valid() && k.is_valid() && k->is_pressed() && !k->is_echo() && p_shortcut->matches_event(k)) { |
| 118 | EngineDebugger::get_singleton()->send_message("request_quit", Array()); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | void SceneDebugger::_handle_embed_input(const Ref<InputEvent> &p_event, const Dictionary &p_settings) { |
| 123 | Ref<InputEventKey> k = p_event; |
| 124 | if (k.is_null() || !k->is_pressed()) { |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | Ref<Shortcut> p_shortcut = p_settings.get("editor/next_frame_embedded_project", Ref<Shortcut>()); |
| 129 | if (p_shortcut.is_valid() && p_shortcut->matches_event(k)) { |
| 130 | EngineDebugger::get_singleton()->send_message("request_embed_next_frame", Array()); |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | if (k->is_echo()) { |
| 135 | return; |
| 136 | } // Shortcuts that doesn't need is_echo goes below here |
| 137 | |
| 138 | p_shortcut = p_settings.get("editor/suspend_resume_embedded_project", Ref<Shortcut>()); |
| 139 | if (p_shortcut.is_valid() && p_shortcut->matches_event(k)) { |
| 140 | EngineDebugger::get_singleton()->send_message("request_embed_suspend_toggle", Array()); |
| 141 | return; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | Error SceneDebugger::_msg_setup_scene(const Array &p_args) { |
| 146 | SceneTree::get_singleton()->get_root()->connect(SceneStringName(window_input), callable_mp_static(SceneDebugger::_handle_input).bind(DebuggerMarshalls::deserialize_key_shortcut(p_args))); |
| 147 | return OK; |
| 148 | } |
| 149 | |
| 150 | Error SceneDebugger::_msg_request_scene_tree(const Array &p_args) { |
| 151 | LiveEditor::get_singleton()->_send_tree(); |
| 152 | return OK; |
| 153 | } |
| 154 | |
| 155 | Error SceneDebugger::_msg_save_node(const Array &p_args) { |
| 156 | ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA); |
| 157 | _save_node(p_args[0], p_args[1]); |
| 158 | Array arr; |
| 159 | arr.append(p_args[1]); |
| 160 | EngineDebugger::get_singleton()->send_message("filesystem:update_file", { arr }); |
| 161 | return OK; |
| 162 | } |
| 163 | |
| 164 | Error SceneDebugger::_msg_inspect_objects(const Array &p_args) { |
| 165 | ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA); |
| 166 | Vector<ObjectID> ids; |
| 167 | for (const Variant &id : (Array)p_args[0]) { |
| 168 | ids.append(ObjectID(id.operator uint64_t())); |
| 169 | } |
| 170 | _send_object_ids(ids, p_args[1]); |
| 171 | return OK; |
| 172 | } |
| 173 | |
| 174 | #ifndef DISABLE_DEPRECATED |
| 175 | Error SceneDebugger::_msg_inspect_object(const Array &p_args) { |
| 176 | ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA); |
| 177 | // Legacy compatibility: convert single object ID to new format, then send single object response. |
| 178 | Vector<ObjectID> ids; |
| 179 | ids.append(ObjectID(p_args[0].operator uint64_t())); |
| 180 | |
| 181 | SceneDebuggerObject obj(ids[0]); |
| 182 | if (obj.id.is_null()) { |
| 183 | EngineDebugger::get_singleton()->send_message("scene:inspect_object", Array()); |
| 184 | return OK; |
| 185 | } |
| 186 | |
| 187 | Array arr; |
| 188 | obj.serialize(arr); |
| 189 | EngineDebugger::get_singleton()->send_message("scene:inspect_object", arr); |
| 190 | return OK; |
| 191 | } |
| 192 | #endif // DISABLE_DEPRECATED |
| 193 | |
| 194 | Error SceneDebugger::_msg_clear_selection(const Array &p_args) { |
| 195 | RuntimeNodeSelect::get_singleton()->_clear_selection(); |
| 196 | return OK; |
| 197 | } |
| 198 | |
| 199 | Error SceneDebugger::_msg_suspend_changed(const Array &p_args) { |
| 200 | ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA); |
| 201 | bool suspended = p_args[0]; |
| 202 | SceneTree::get_singleton()->set_suspend(suspended); |
| 203 | RuntimeNodeSelect::get_singleton()->_update_input_state(); |
| 204 | return OK; |
| 205 | } |
| 206 | |
| 207 | Error SceneDebugger::_msg_next_frame(const Array &p_args) { |
| 208 | _next_frame(); |
| 209 | return OK; |
| 210 | } |
| 211 | |
| 212 | Error SceneDebugger::_msg_speed_changed(const Array &p_args) { |
| 213 | ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA); |
| 214 | double time_scale_user = p_args[0]; |
| 215 | Engine::get_singleton()->set_user_time_scale(time_scale_user); |
| 216 | return OK; |
| 217 | } |
| 218 | |
| 219 | Error SceneDebugger::_msg_debug_mute_audio(const Array &p_args) { |
| 220 | ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA); |
| 221 | bool do_mute = p_args[0]; |
| 222 | AudioServer::get_singleton()->set_debug_mute(do_mute); |
| 223 | return OK; |
| 224 | } |
| 225 | |
| 226 | Error SceneDebugger::_msg_override_cameras(const Array &p_args) { |
| 227 | ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA); |
| 228 | bool enable = p_args[0]; |
| 229 | bool from_editor = p_args[1]; |
| 230 | SceneTree::get_singleton()->get_root()->enable_camera_2d_override(enable); |
| 231 | #ifndef _3D_DISABLED |
| 232 | SceneTree::get_singleton()->get_root()->enable_camera_3d_override(enable); |
| 233 | #endif // _3D_DISABLED |
| 234 | RuntimeNodeSelect::get_singleton()->_set_camera_override_enabled(enable && !from_editor); |
| 235 | return OK; |
| 236 | } |
| 237 | |
| 238 | Error SceneDebugger::_msg_set_object_property(const Array &p_args) { |
| 239 | ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA); |
| 240 | _set_object_property(p_args[0], p_args[1], p_args[2]); |
| 241 | RuntimeNodeSelect::get_singleton()->_queue_selection_update(); |
| 242 | return OK; |
| 243 | } |
| 244 | |
| 245 | Error SceneDebugger::_msg_set_object_property_field(const Array &p_args) { |
| 246 | ERR_FAIL_COND_V(p_args.size() < 4, ERR_INVALID_DATA); |
| 247 | _set_object_property(p_args[0], p_args[1], p_args[2], p_args[3]); |
| 248 | RuntimeNodeSelect::get_singleton()->_queue_selection_update(); |
| 249 | return OK; |
| 250 | } |
| 251 | |
| 252 | Error SceneDebugger::_msg_reload_cached_files(const Array &p_args) { |
| 253 | ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA); |
| 254 | PackedStringArray files = p_args[0]; |
| 255 | reload_cached_files(files); |
| 256 | return OK; |
| 257 | } |
| 258 | |
| 259 | Error SceneDebugger::_msg_setup_embedded_shortcuts(const Array &p_args) { |
| 260 | ERR_FAIL_COND_V(p_args.is_empty() || p_args[0].get_type() != Variant::DICTIONARY, ERR_INVALID_DATA); |
| 261 | Dictionary dict = p_args[0]; |
| 262 | LocalVector<Variant> keys = dict.get_key_list(); |
| 263 | |
| 264 | for (const Variant &key : keys) { |
| 265 | dict[key] = DebuggerMarshalls::deserialize_key_shortcut(dict[key]); |
| 266 | } |
| 267 | |
| 268 | SceneTree::get_singleton()->get_root()->connect(SceneStringName(window_input), callable_mp_static(SceneDebugger::_handle_embed_input).bind(dict)); |
| 269 | return OK; |
| 270 | } |
| 271 | |
| 272 | // region Live editing. |
| 273 | |
| 274 | Error SceneDebugger::_msg_live_set_root(const Array &p_args) { |
| 275 | ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA); |
| 276 | LiveEditor::get_singleton()->_root_func(p_args[0], p_args[1]); |
| 277 | return OK; |
| 278 | } |
| 279 | |
| 280 | Error SceneDebugger::_msg_live_node_path(const Array &p_args) { |
| 281 | ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA); |
| 282 | LiveEditor::get_singleton()->_node_path_func(p_args[0], p_args[1]); |
| 283 | return OK; |
| 284 | } |
| 285 | |
| 286 | Error SceneDebugger::_msg_live_res_path(const Array &p_args) { |
| 287 | ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA); |
| 288 | LiveEditor::get_singleton()->_res_path_func(p_args[0], p_args[1]); |
| 289 | return OK; |
| 290 | } |
| 291 | |
| 292 | Error SceneDebugger::_msg_live_node_prop_res(const Array &p_args) { |
| 293 | ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA); |
| 294 | LiveEditor::get_singleton()->_node_set_res_func(p_args[0], p_args[1], p_args[2]); |
| 295 | return OK; |
| 296 | } |
| 297 | |
| 298 | Error SceneDebugger::_msg_live_node_prop(const Array &p_args) { |
| 299 | ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA); |
| 300 | LiveEditor::get_singleton()->_node_set_func(p_args[0], p_args[1], p_args[2]); |
| 301 | return OK; |
| 302 | } |
| 303 | |
| 304 | Error SceneDebugger::_msg_live_res_prop_res(const Array &p_args) { |
| 305 | ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA); |
| 306 | LiveEditor::get_singleton()->_res_set_res_func(p_args[0], p_args[1], p_args[2]); |
| 307 | return OK; |
| 308 | } |
| 309 | |
| 310 | Error SceneDebugger::_msg_live_res_prop(const Array &p_args) { |
| 311 | ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA); |
| 312 | LiveEditor::get_singleton()->_res_set_func(p_args[0], p_args[1], p_args[2]); |
| 313 | return OK; |
| 314 | } |
| 315 | |
| 316 | Error SceneDebugger::_msg_live_node_call(const Array &p_args) { |
| 317 | ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA); |
| 318 | LocalVector<Variant> args; |
| 319 | LocalVector<Variant *> argptrs; |
| 320 | args.resize(p_args.size() - 2); |
| 321 | argptrs.resize(args.size()); |
| 322 | for (uint32_t i = 0; i < args.size(); i++) { |
| 323 | args[i] = p_args[i + 2]; |
| 324 | argptrs[i] = &args[i]; |
| 325 | } |
| 326 | LiveEditor::get_singleton()->_node_call_func(p_args[0], p_args[1], argptrs.size() ? (const Variant **)argptrs.ptr() : nullptr, argptrs.size()); |
| 327 | return OK; |
| 328 | } |
| 329 | |
| 330 | Error SceneDebugger::_msg_live_res_call(const Array &p_args) { |
| 331 | ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA); |
| 332 | LocalVector<Variant> args; |
| 333 | LocalVector<Variant *> argptrs; |
| 334 | args.resize(p_args.size() - 2); |
| 335 | argptrs.resize(args.size()); |
| 336 | for (uint32_t i = 0; i < args.size(); i++) { |
| 337 | args[i] = p_args[i + 2]; |
| 338 | argptrs[i] = &args[i]; |
| 339 | } |
| 340 | LiveEditor::get_singleton()->_res_call_func(p_args[0], p_args[1], argptrs.size() ? (const Variant **)argptrs.ptr() : nullptr, argptrs.size()); |
| 341 | return OK; |
| 342 | } |
| 343 | |
| 344 | Error SceneDebugger::_msg_live_create_node(const Array &p_args) { |
| 345 | ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA); |
| 346 | LiveEditor::get_singleton()->_create_node_func(p_args[0], p_args[1], p_args[2]); |
| 347 | return OK; |
| 348 | } |
| 349 | |
| 350 | Error SceneDebugger::_msg_live_instantiate_node(const Array &p_args) { |
| 351 | ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA); |
| 352 | LiveEditor::get_singleton()->_instance_node_func(p_args[0], p_args[1], p_args[2]); |
| 353 | return OK; |
| 354 | } |
| 355 | |
| 356 | Error SceneDebugger::_msg_live_remove_node(const Array &p_args) { |
| 357 | ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA); |
| 358 | LiveEditor::get_singleton()->_remove_node_func(p_args[0]); |
| 359 | RuntimeNodeSelect::get_singleton()->_queue_selection_update(); |
| 360 | return OK; |
| 361 | } |
| 362 | |
| 363 | Error SceneDebugger::_msg_live_remove_and_keep_node(const Array &p_args) { |
| 364 | ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA); |
| 365 | LiveEditor::get_singleton()->_remove_and_keep_node_func(p_args[0], p_args[1]); |
| 366 | RuntimeNodeSelect::get_singleton()->_queue_selection_update(); |
| 367 | return OK; |
| 368 | } |
| 369 | |
| 370 | Error SceneDebugger::_msg_live_restore_node(const Array &p_args) { |
| 371 | ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA); |
| 372 | LiveEditor::get_singleton()->_restore_node_func(p_args[0], p_args[1], p_args[2]); |
| 373 | return OK; |
| 374 | } |
| 375 | |
| 376 | Error SceneDebugger::_msg_live_duplicate_node(const Array &p_args) { |
| 377 | ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA); |
| 378 | LiveEditor::get_singleton()->_duplicate_node_func(p_args[0], p_args[1]); |
| 379 | return OK; |
| 380 | } |
| 381 | |
| 382 | Error SceneDebugger::_msg_live_reparent_node(const Array &p_args) { |
| 383 | ERR_FAIL_COND_V(p_args.size() < 4, ERR_INVALID_DATA); |
| 384 | LiveEditor::get_singleton()->_reparent_node_func(p_args[0], p_args[1], p_args[2], p_args[3]); |
| 385 | return OK; |
| 386 | } |
| 387 | |
| 388 | // endregion |
| 389 | |
| 390 | // region Runtime Node Selection. |
| 391 | |
| 392 | Error SceneDebugger::_msg_runtime_node_select_setup(const Array &p_args) { |
| 393 | ERR_FAIL_COND_V(p_args.is_empty() || p_args[0].get_type() != Variant::DICTIONARY, ERR_INVALID_DATA); |
| 394 | RuntimeNodeSelect::get_singleton()->_setup(p_args[0]); |
| 395 | return OK; |
| 396 | } |
| 397 | |
| 398 | Error SceneDebugger::_msg_runtime_node_select_set_type(const Array &p_args) { |
| 399 | ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA); |
| 400 | RuntimeNodeSelect::NodeType type = (RuntimeNodeSelect::NodeType)(int)p_args[0]; |
| 401 | RuntimeNodeSelect::get_singleton()->_node_set_type(type); |
| 402 | return OK; |
| 403 | } |
| 404 | |
| 405 | Error SceneDebugger::_msg_runtime_node_select_set_mode(const Array &p_args) { |
| 406 | ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA); |
| 407 | RuntimeNodeSelect::SelectMode mode = (RuntimeNodeSelect::SelectMode)(int)p_args[0]; |
| 408 | RuntimeNodeSelect::get_singleton()->_select_set_mode(mode); |
| 409 | return OK; |
| 410 | } |
| 411 | |
| 412 | Error SceneDebugger::_msg_runtime_node_select_set_visible(const Array &p_args) { |
| 413 | ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA); |
| 414 | bool visible = p_args[0]; |
| 415 | RuntimeNodeSelect::get_singleton()->_set_selection_visible(visible); |
| 416 | return OK; |
| 417 | } |
| 418 | |
| 419 | Error SceneDebugger::_msg_runtime_node_select_set_avoid_locked(const Array &p_args) { |
| 420 | ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA); |
| 421 | bool avoid_locked = p_args[0]; |
| 422 | RuntimeNodeSelect::get_singleton()->_set_avoid_locked(avoid_locked); |
| 423 | return OK; |
| 424 | } |
| 425 | |
| 426 | Error SceneDebugger::_msg_runtime_node_select_set_prefer_group(const Array &p_args) { |
| 427 | ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA); |
| 428 | bool prefer_group = p_args[0]; |
| 429 | RuntimeNodeSelect::get_singleton()->_set_prefer_group(prefer_group); |
| 430 | return OK; |
| 431 | } |
| 432 | |
| 433 | Error SceneDebugger::_msg_runtime_node_select_reset_camera_2d(const Array &p_args) { |
| 434 | RuntimeNodeSelect::get_singleton()->_reset_camera_2d(); |
| 435 | return OK; |
| 436 | } |
| 437 | |
| 438 | Error SceneDebugger::_msg_transform_camera_2d(const Array &p_args) { |
| 439 | ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA); |
| 440 | ERR_FAIL_COND_V(!SceneTree::get_singleton()->get_root()->is_camera_2d_override_enabled(), ERR_BUG); |
| 441 | Transform2D transform = p_args[0]; |
| 442 | Camera2D *override_camera = SceneTree::get_singleton()->get_root()->get_override_camera_2d(); |
| 443 | override_camera->set_offset(transform.affine_inverse().get_origin()); |
| 444 | override_camera->set_zoom(transform.get_scale()); |
| 445 | RuntimeNodeSelect::get_singleton()->_queue_selection_update(); |
| 446 | return OK; |
| 447 | } |
| 448 | |
| 449 | #ifndef _3D_DISABLED |
| 450 | Error SceneDebugger::_msg_runtime_node_select_reset_camera_3d(const Array &p_args) { |
| 451 | RuntimeNodeSelect::get_singleton()->_reset_camera_3d(); |
| 452 | return OK; |
| 453 | } |
| 454 | |
| 455 | Error SceneDebugger::_msg_transform_camera_3d(const Array &p_args) { |
| 456 | ERR_FAIL_COND_V(p_args.size() < 5, ERR_INVALID_DATA); |
| 457 | ERR_FAIL_COND_V(!SceneTree::get_singleton()->get_root()->is_camera_3d_override_enabled(), ERR_BUG); |
| 458 | Transform3D transform = p_args[0]; |
| 459 | bool is_perspective = p_args[1]; |
| 460 | float size_or_fov = p_args[2]; |
| 461 | float depth_near = p_args[3]; |
| 462 | float depth_far = p_args[4]; |
| 463 | |
| 464 | Camera3D *override_camera = SceneTree::get_singleton()->get_root()->get_override_camera_3d(); |
| 465 | if (is_perspective) { |
| 466 | override_camera->set_perspective(size_or_fov, depth_near, depth_far); |
| 467 | } else { |
| 468 | override_camera->set_orthogonal(size_or_fov, depth_near, depth_far); |
| 469 | } |
| 470 | override_camera->set_transform(transform); |
| 471 | RuntimeNodeSelect::get_singleton()->_queue_selection_update(); |
| 472 | return OK; |
| 473 | } |
| 474 | #endif // _3D_DISABLED |
| 475 | |
| 476 | // endregion |
| 477 | |
| 478 | // region Embedded process screenshot. |
| 479 | |
| 480 | Error SceneDebugger::_msg_rq_screenshot(const Array &p_args) { |
| 481 | ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA); |
| 482 | |
| 483 | Viewport *viewport = SceneTree::get_singleton()->get_root(); |
| 484 | ERR_FAIL_NULL_V_MSG(viewport, ERR_UNCONFIGURED, "Cannot get a viewport from the main screen."); |
| 485 | Ref<ViewportTexture> texture = viewport->get_texture(); |
| 486 | ERR_FAIL_COND_V_MSG(texture.is_null(), ERR_UNCONFIGURED, "Cannot get a viewport texture from the main screen."); |
| 487 | Ref<Image> img = texture->get_image(); |
| 488 | ERR_FAIL_COND_V_MSG(img.is_null(), ERR_UNCONFIGURED, "Cannot get an image from a viewport texture of the main screen."); |
| 489 | img->clear_mipmaps(); |
| 490 | |
| 491 | const String TEMP_DIR = OS::get_singleton()->get_temp_path(); |
| 492 | uint32_t suffix_i = 0; |
| 493 | String path; |
| 494 | while (true) { |
| 495 | String datetime = Time::get_singleton()->get_datetime_string_from_system().remove_chars("-T:"); |
| 496 | datetime += itos(Time::get_singleton()->get_ticks_usec()); |
| 497 | String suffix = datetime + (suffix_i > 0 ? itos(suffix_i) : ""); |
| 498 | path = TEMP_DIR.path_join("scr-" + suffix + ".png"); |
| 499 | if (!DirAccess::exists(path)) { |
| 500 | break; |
| 501 | } |
| 502 | suffix_i += 1; |
| 503 | } |
| 504 | img->save_png(path); |
| 505 | |
| 506 | Array arr; |
| 507 | arr.append(p_args[0]); |
| 508 | arr.append(img->get_width()); |
| 509 | arr.append(img->get_height()); |
| 510 | arr.append(path); |
| 511 | EngineDebugger::get_singleton()->send_message("game_view:get_screenshot", arr); |
| 512 | |
| 513 | return OK; |
| 514 | } |
| 515 | |
| 516 | // endregion |
| 517 | |
| 518 | HashMap<String, SceneDebugger::ParseMessageFunc> SceneDebugger::message_handlers; |
| 519 | |
| 520 | Error SceneDebugger::parse_message(void *p_user, const String &p_msg, const Array &p_args, bool &r_captured) { |
| 521 | ERR_FAIL_NULL_V(SceneTree::get_singleton(), ERR_UNCONFIGURED); |
| 522 | ERR_FAIL_NULL_V(LiveEditor::get_singleton(), ERR_UNCONFIGURED); |
| 523 | ERR_FAIL_NULL_V(RuntimeNodeSelect::get_singleton(), ERR_UNCONFIGURED); |
| 524 | |
| 525 | ParseMessageFunc *fn_ptr = message_handlers.getptr(p_msg); |
| 526 | if (fn_ptr) { |
| 527 | r_captured = true; |
| 528 | return (*fn_ptr)(p_args); |
| 529 | } |
| 530 | |
| 531 | if (p_msg.begins_with("live_") || p_msg.begins_with("runtime_node_select_")) { |
| 532 | // Messages with these prefixes are reserved and should be handled by the LiveEditor or RuntimeNodeSelect classes, |
| 533 | // so return ERR_SKIP. |
| 534 | r_captured = true; |
| 535 | return ERR_SKIP; |
| 536 | } |
| 537 | |
| 538 | r_captured = false; |
| 539 | |
| 540 | return OK; |
| 541 | } |
| 542 | |
| 543 | void SceneDebugger::_init_message_handlers() { |
| 544 | message_handlers["setup_scene"] = _msg_setup_scene; |
| 545 | message_handlers["setup_embedded_shortcuts"] = _msg_setup_embedded_shortcuts; |
| 546 | message_handlers["request_scene_tree"] = _msg_request_scene_tree; |
| 547 | message_handlers["save_node"] = _msg_save_node; |
| 548 | message_handlers["inspect_objects"] = _msg_inspect_objects; |
| 549 | #ifndef DISABLE_DEPRECATED |
| 550 | message_handlers["inspect_object"] = _msg_inspect_object; |
| 551 | #endif // DISABLE_DEPRECATED |
| 552 | message_handlers["clear_selection"] = _msg_clear_selection; |
| 553 | message_handlers["suspend_changed"] = _msg_suspend_changed; |
| 554 | message_handlers["next_frame"] = _msg_next_frame; |
| 555 | message_handlers["speed_changed"] = _msg_speed_changed; |
| 556 | message_handlers["debug_mute_audio"] = _msg_debug_mute_audio; |
| 557 | message_handlers["override_cameras"] = _msg_override_cameras; |
| 558 | message_handlers["transform_camera_2d"] = _msg_transform_camera_2d; |
| 559 | #ifndef _3D_DISABLED |
| 560 | message_handlers["transform_camera_3d"] = _msg_transform_camera_3d; |
| 561 | #endif // _3D_DISABLED |
| 562 | message_handlers["set_object_property"] = _msg_set_object_property; |
| 563 | message_handlers["set_object_property_field"] = _msg_set_object_property_field; |
| 564 | message_handlers["reload_cached_files"] = _msg_reload_cached_files; |
| 565 | message_handlers["live_set_root"] = _msg_live_set_root; |
| 566 | message_handlers["live_node_path"] = _msg_live_node_path; |
| 567 | message_handlers["live_res_path"] = _msg_live_res_path; |
| 568 | message_handlers["live_node_prop_res"] = _msg_live_node_prop_res; |
| 569 | message_handlers["live_node_prop"] = _msg_live_node_prop; |
| 570 | message_handlers["live_res_prop_res"] = _msg_live_res_prop_res; |
| 571 | message_handlers["live_res_prop"] = _msg_live_res_prop; |
| 572 | message_handlers["live_node_call"] = _msg_live_node_call; |
| 573 | message_handlers["live_res_call"] = _msg_live_res_call; |
| 574 | message_handlers["live_create_node"] = _msg_live_create_node; |
| 575 | message_handlers["live_instantiate_node"] = _msg_live_instantiate_node; |
| 576 | message_handlers["live_remove_node"] = _msg_live_remove_node; |
| 577 | message_handlers["live_remove_and_keep_node"] = _msg_live_remove_and_keep_node; |
| 578 | message_handlers["live_restore_node"] = _msg_live_restore_node; |
| 579 | message_handlers["live_duplicate_node"] = _msg_live_duplicate_node; |
| 580 | message_handlers["live_reparent_node"] = _msg_live_reparent_node; |
| 581 | message_handlers["runtime_node_select_setup"] = _msg_runtime_node_select_setup; |
| 582 | message_handlers["runtime_node_select_set_type"] = _msg_runtime_node_select_set_type; |
| 583 | message_handlers["runtime_node_select_set_mode"] = _msg_runtime_node_select_set_mode; |
| 584 | message_handlers["runtime_node_select_set_visible"] = _msg_runtime_node_select_set_visible; |
| 585 | message_handlers["runtime_node_select_set_avoid_locked"] = _msg_runtime_node_select_set_avoid_locked; |
| 586 | message_handlers["runtime_node_select_set_prefer_group"] = _msg_runtime_node_select_set_prefer_group; |
| 587 | message_handlers["runtime_node_select_reset_camera_2d"] = _msg_runtime_node_select_reset_camera_2d; |
| 588 | #ifndef _3D_DISABLED |
| 589 | message_handlers["runtime_node_select_reset_camera_3d"] = _msg_runtime_node_select_reset_camera_3d; |
| 590 | #endif |
| 591 | message_handlers["rq_screenshot"] = _msg_rq_screenshot; |
| 592 | } |
| 593 | |
| 594 | void SceneDebugger::_save_node(ObjectID id, const String &p_path) { |
| 595 | Node *node = ObjectDB::get_instance<Node>(id); |
| 596 | ERR_FAIL_NULL(node); |
| 597 | |
| 598 | #ifdef TOOLS_ENABLED |
| 599 | HashMap<const Node *, Node *> duplimap; |
| 600 | Node *copy = node->duplicate_from_editor(duplimap); |
| 601 | #else |
| 602 | Node *copy = node->duplicate(); |
| 603 | #endif // TOOLS_ENABLED |
| 604 | |
| 605 | // Handle Unique Nodes. |
| 606 | for (int i = 0; i < copy->get_child_count(false); i++) { |
| 607 | _set_node_owner_recursive(copy->get_child(i, false), copy); |
| 608 | } |
| 609 | // Root node cannot ever be unique name in its own Scene! |
| 610 | copy->set_unique_name_in_owner(false); |
| 611 | |
| 612 | Ref<PackedScene> ps = memnew(PackedScene); |
| 613 | ps->pack(copy); |
| 614 | ResourceSaver::save(ps, p_path); |
| 615 | |
| 616 | memdelete(copy); |
| 617 | } |
| 618 | |
| 619 | void SceneDebugger::_set_node_owner_recursive(Node *p_node, Node *p_owner) { |
| 620 | if (!p_node->get_owner()) { |
| 621 | p_node->set_owner(p_owner); |
| 622 | } |
| 623 | |
| 624 | for (int i = 0; i < p_node->get_child_count(false); i++) { |
| 625 | _set_node_owner_recursive(p_node->get_child(i, false), p_owner); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | void SceneDebugger::_send_object_ids(const Vector<ObjectID> &p_ids, bool p_update_selection) { |
| 630 | Vector<ObjectID> ids = p_ids; |
| 631 | if (ids.size() > RuntimeNodeSelect::get_singleton()->max_selection) { |
| 632 | ids.resize(RuntimeNodeSelect::get_singleton()->max_selection); |
| 633 | EngineDebugger::get_singleton()->send_message("show_selection_limit_warning", Array()); |
| 634 | } |
| 635 | |
| 636 | LocalVector<Node *> nodes; |
| 637 | Array objs; |
| 638 | bool objs_missing = false; |
| 639 | for (const ObjectID &id : ids) { |
| 640 | SceneDebuggerObject obj(id); |
| 641 | if (obj.id.is_null()) { |
| 642 | objs_missing = true; |
| 643 | continue; |
| 644 | } |
| 645 | |
| 646 | if (p_update_selection) { |
| 647 | if (Node *node = ObjectDB::get_instance<Node>(id)) { |
| 648 | nodes.push_back(node); |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | Array arr; |
| 653 | obj.serialize(arr); |
| 654 | objs.append(arr); |
| 655 | } |
| 656 | |
| 657 | if (p_update_selection) { |
| 658 | RuntimeNodeSelect::get_singleton()->_set_selected_nodes(Vector<Node *>(nodes)); |
| 659 | } |
| 660 | |
| 661 | if (objs_missing) { |
| 662 | Array invalid_selection; |
| 663 | for (const ObjectID &id : ids) { |
| 664 | invalid_selection.append(id); |
| 665 | } |
| 666 | |
| 667 | Array arr; |
| 668 | arr.append(invalid_selection); |
| 669 | EngineDebugger::get_singleton()->send_message("remote_selection_invalidated", arr); |
| 670 | |
| 671 | EngineDebugger::get_singleton()->send_message(objs.is_empty() ? "remote_nothing_selected" : "remote_objects_selected", objs); |
| 672 | } else { |
| 673 | EngineDebugger::get_singleton()->send_message(p_update_selection ? "remote_objects_selected" : "scene:inspect_objects", objs); |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | void SceneDebugger::_set_object_property(ObjectID p_id, const String &p_property, const Variant &p_value, const String &p_field) { |
| 678 | Object *obj = ObjectDB::get_instance(p_id); |
| 679 | if (!obj) { |
| 680 | return; |
| 681 | } |
| 682 | |
| 683 | String prop_name; |
| 684 | if (p_property.begins_with("Members/")) { |
| 685 | prop_name = p_property.get_slicec('/', p_property.get_slice_count("/") - 1); |
| 686 | } else { |
| 687 | prop_name = p_property; |
| 688 | } |
| 689 | |
| 690 | Variant value = p_value; |
| 691 | if (p_value.is_string() && (obj->get_static_property_type(prop_name) == Variant::OBJECT || p_property == "script")) { |
| 692 | value = ResourceLoader::load(p_value); |
| 693 | } |
| 694 | |
| 695 | if (!p_field.is_empty()) { |
| 696 | // Only one specific field. |
| 697 | value = fieldwise_assign(obj->get(prop_name), value, p_field); |
| 698 | } |
| 699 | |
| 700 | obj->set(prop_name, value); |
| 701 | } |
| 702 | |
| 703 | void SceneDebugger::_next_frame() { |
| 704 | SceneTree *scene_tree = SceneTree::get_singleton(); |
| 705 | if (!scene_tree->is_suspended()) { |
| 706 | return; |
| 707 | } |
| 708 | |
| 709 | scene_tree->set_suspend(false); |
| 710 | RenderingServer::get_singleton()->connect("frame_post_draw", callable_mp(scene_tree, &SceneTree::set_suspend).bind(true), Object::CONNECT_ONE_SHOT); |
| 711 | } |
| 712 | |
| 713 | void SceneDebugger::add_to_cache(const String &p_filename, Node *p_node) { |
| 714 | LiveEditor *debugger = LiveEditor::get_singleton(); |
| 715 | if (!debugger) { |
| 716 | return; |
| 717 | } |
| 718 | |
| 719 | if (EngineDebugger::get_script_debugger() && !p_filename.is_empty()) { |
| 720 | debugger->live_scene_edit_cache[p_filename].insert(p_node); |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | void SceneDebugger::remove_from_cache(const String &p_filename, Node *p_node) { |
| 725 | LiveEditor *debugger = LiveEditor::get_singleton(); |
| 726 | if (!debugger) { |
| 727 | return; |
| 728 | } |
| 729 | |
| 730 | HashMap<String, HashSet<Node *>> &edit_cache = debugger->live_scene_edit_cache; |
| 731 | HashMap<String, HashSet<Node *>>::Iterator E = edit_cache.find(p_filename); |
| 732 | if (E) { |
| 733 | E->value.erase(p_node); |
| 734 | if (E->value.is_empty()) { |
| 735 | edit_cache.remove(E); |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | HashMap<Node *, HashMap<ObjectID, Node *>> &remove_list = debugger->live_edit_remove_list; |
| 740 | HashMap<Node *, HashMap<ObjectID, Node *>>::Iterator F = remove_list.find(p_node); |
| 741 | if (F) { |
| 742 | for (const KeyValue<ObjectID, Node *> &G : F->value) { |
| 743 | memdelete(G.value); |
| 744 | } |
| 745 | remove_list.remove(F); |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | void SceneDebugger::reload_cached_files(const PackedStringArray &p_files) { |
| 750 | for (const String &file : p_files) { |
| 751 | Ref<Resource> res = ResourceCache::get_ref(file); |
| 752 | if (res.is_valid()) { |
| 753 | res->reload_from_file(); |
| 754 | } |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | SceneDebuggerObject::SceneDebuggerObject(ObjectID p_id) : |
| 759 | SceneDebuggerObject(ObjectDB::get_instance(p_id)) { |
| 760 | } |
| 761 | |
| 762 | /// SceneDebuggerObject |
| 763 | SceneDebuggerObject::SceneDebuggerObject(Object *p_obj) { |
| 764 | if (!p_obj) { |
| 765 | return; |
| 766 | } |
| 767 | |
| 768 | id = p_obj->get_instance_id(); |
| 769 | class_name = p_obj->get_class(); |
| 770 | |
| 771 | if (ScriptInstance *si = p_obj->get_script_instance()) { |
| 772 | // Read script instance constants and variables. |
| 773 | if (!si->get_script().is_null()) { |
| 774 | Script *s = si->get_script().ptr(); |
| 775 | _parse_script_properties(s, si); |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | if (Node *node = Object::cast_to<Node>(p_obj)) { |
| 780 | // For debugging multiplayer. |
| 781 | { |
| 782 | PropertyInfo pi(Variant::INT, String("Node/multiplayer_authority"), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_READ_ONLY); |
| 783 | properties.push_back(SceneDebuggerProperty(pi, node->get_multiplayer_authority())); |
| 784 | } |
| 785 | |
| 786 | // Add specialized NodePath info (if inside tree). |
| 787 | if (node->is_inside_tree()) { |
| 788 | PropertyInfo pi(Variant::NODE_PATH, String("Node/path")); |
| 789 | properties.push_back(SceneDebuggerProperty(pi, node->get_path())); |
| 790 | } else { // Can't ask for path if a node is not in tree. |
| 791 | PropertyInfo pi(Variant::STRING, String("Node/path")); |
| 792 | properties.push_back(SceneDebuggerProperty(pi, "[Orphan]")); |
| 793 | } |
| 794 | } else if (Script *s = Object::cast_to<Script>(p_obj)) { |
| 795 | // Add script constants (no instance). |
| 796 | _parse_script_properties(s, nullptr); |
| 797 | } |
| 798 | |
| 799 | // Add base object properties. |
| 800 | List<PropertyInfo> pinfo; |
| 801 | p_obj->get_property_list(&pinfo, true); |
| 802 | for (const PropertyInfo &E : pinfo) { |
| 803 | if (E.usage & (PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CATEGORY)) { |
| 804 | properties.push_back(SceneDebuggerProperty(E, p_obj->get(E.name))); |
| 805 | } |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | void SceneDebuggerObject::_parse_script_properties(Script *p_script, ScriptInstance *p_instance) { |
| 810 | typedef HashMap<const Script *, HashSet<StringName>> ScriptMemberMap; |
| 811 | typedef HashMap<const Script *, HashMap<StringName, Variant>> ScriptConstantsMap; |
| 812 | |
| 813 | ScriptMemberMap members; |
| 814 | if (p_instance) { |
| 815 | members[p_script] = HashSet<StringName>(); |
| 816 | p_script->get_members(&(members[p_script])); |
| 817 | } |
| 818 | |
| 819 | ScriptConstantsMap constants; |
| 820 | constants[p_script] = HashMap<StringName, Variant>(); |
| 821 | p_script->get_constants(&(constants[p_script])); |
| 822 | |
| 823 | Ref<Script> base = p_script->get_base_script(); |
| 824 | while (base.is_valid()) { |
| 825 | if (p_instance) { |
| 826 | members[base.ptr()] = HashSet<StringName>(); |
| 827 | base->get_members(&(members[base.ptr()])); |
| 828 | } |
| 829 | |
| 830 | constants[base.ptr()] = HashMap<StringName, Variant>(); |
| 831 | base->get_constants(&(constants[base.ptr()])); |
| 832 | |
| 833 | base = base->get_base_script(); |
| 834 | } |
| 835 | |
| 836 | HashSet<String> exported_members; |
| 837 | |
| 838 | if (p_instance) { |
| 839 | List<PropertyInfo> pinfo; |
| 840 | p_instance->get_property_list(&pinfo); |
| 841 | for (const PropertyInfo &E : pinfo) { |
| 842 | if (E.usage & (PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CATEGORY)) { |
| 843 | exported_members.insert(E.name); |
| 844 | } |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | // Members |
| 849 | for (KeyValue<const Script *, HashSet<StringName>> sm : members) { |
| 850 | for (const StringName &E : sm.value) { |
| 851 | if (exported_members.has(E)) { |
| 852 | continue; // Exported variables already show up in the inspector. |
| 853 | } |
| 854 | if (String(E).begins_with("@")) { |
| 855 | continue; // Skip groups. |
| 856 | } |
| 857 | |
| 858 | Variant m; |
| 859 | if (p_instance->get(E, m)) { |
| 860 | String script_path = sm.key == p_script ? "" : sm.key->get_path().get_file() + "/"; |
| 861 | PropertyInfo pi(m.get_type(), "Members/" + script_path + E); |
| 862 | properties.push_back(SceneDebuggerProperty(pi, m)); |
| 863 | } |
| 864 | } |
| 865 | } |
| 866 | // Constants |
| 867 | for (KeyValue<const Script *, HashMap<StringName, Variant>> &sc : constants) { |
| 868 | for (const KeyValue<StringName, Variant> &E : sc.value) { |
| 869 | String script_path = sc.key == p_script ? "" : sc.key->get_path().get_file() + "/"; |
| 870 | if (E.value.get_type() == Variant::OBJECT) { |
| 871 | Variant inst_id = ((Object *)E.value)->get_instance_id(); |
| 872 | PropertyInfo pi(inst_id.get_type(), "Constants/" + E.key, PROPERTY_HINT_OBJECT_ID, "Object"); |
| 873 | properties.push_back(SceneDebuggerProperty(pi, inst_id)); |
| 874 | } else { |
| 875 | PropertyInfo pi(E.value.get_type(), "Constants/" + script_path + E.key); |
| 876 | properties.push_back(SceneDebuggerProperty(pi, E.value)); |
| 877 | } |
| 878 | } |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | void SceneDebuggerObject::serialize(Array &r_arr, int p_max_size) { |
| 883 | Array send_props; |
| 884 | for (SceneDebuggerProperty &property : properties) { |
| 885 | const PropertyInfo &pi = property.first; |
| 886 | Variant &var = property.second; |
| 887 | |
| 888 | Ref<Resource> res = var; |
| 889 | |
| 890 | Array prop = { pi.name, pi.type }; |
| 891 | PropertyHint hint = pi.hint; |
| 892 | String hint_string = pi.hint_string; |
| 893 | if (res.is_valid() && !res->get_path().is_empty()) { |
| 894 | var = res->get_path(); |
| 895 | } else { //only send information that can be sent.. |
| 896 | int len = 0; //test how big is this to encode |
| 897 | encode_variant(var, nullptr, len); |
| 898 | if (len > p_max_size) { //limit to max size |
| 899 | hint = PROPERTY_HINT_OBJECT_TOO_BIG; |
| 900 | hint_string = ""; |
| 901 | var = Variant(); |
| 902 | } |
| 903 | } |
| 904 | prop.push_back(hint); |
| 905 | prop.push_back(hint_string); |
| 906 | prop.push_back(pi.usage); |
| 907 | prop.push_back(var); |
| 908 | send_props.push_back(prop); |
| 909 | } |
| 910 | r_arr.push_back(uint64_t(id)); |
| 911 | r_arr.push_back(class_name); |
| 912 | r_arr.push_back(send_props); |
| 913 | } |
| 914 | |
| 915 | void SceneDebuggerObject::deserialize(const Array &p_arr) { |
| 916 | #define CHECK_TYPE(p_what, p_type) ERR_FAIL_COND(p_what.get_type() != Variant::p_type); |
| 917 | ERR_FAIL_COND(p_arr.size() < 3); |
| 918 | CHECK_TYPE(p_arr[0], INT); |
| 919 | CHECK_TYPE(p_arr[1], STRING); |
| 920 | CHECK_TYPE(p_arr[2], ARRAY); |
| 921 | |
| 922 | deserialize(uint64_t(p_arr[0]), p_arr[1], p_arr[2]); |
| 923 | } |
| 924 | |
| 925 | void SceneDebuggerObject::deserialize(uint64_t p_id, const String &p_class_name, const Array &p_props) { |
| 926 | id = p_id; |
| 927 | class_name = p_class_name; |
| 928 | |
| 929 | for (int i = 0; i < p_props.size(); i++) { |
| 930 | CHECK_TYPE(p_props[i], ARRAY); |
| 931 | Array prop = p_props[i]; |
| 932 | |
| 933 | ERR_FAIL_COND(prop.size() != 6); |
| 934 | CHECK_TYPE(prop[0], STRING); |
| 935 | CHECK_TYPE(prop[1], INT); |
| 936 | CHECK_TYPE(prop[2], INT); |
| 937 | CHECK_TYPE(prop[3], STRING); |
| 938 | CHECK_TYPE(prop[4], INT); |
| 939 | |
| 940 | PropertyInfo pinfo; |
| 941 | pinfo.name = prop[0]; |
| 942 | pinfo.type = Variant::Type(int(prop[1])); |
| 943 | pinfo.hint = PropertyHint(int(prop[2])); |
| 944 | pinfo.hint_string = prop[3]; |
| 945 | pinfo.usage = PropertyUsageFlags(int(prop[4])); |
| 946 | Variant var = prop[5]; |
| 947 | |
| 948 | if (pinfo.type == Variant::OBJECT) { |
| 949 | if (var.is_zero()) { |
| 950 | var = Ref<Resource>(); |
| 951 | } else if (var.get_type() == Variant::OBJECT) { |
| 952 | if (((Object *)var)->is_class("EncodedObjectAsID")) { |
| 953 | var = Object::cast_to<EncodedObjectAsID>(var)->get_object_id(); |
| 954 | pinfo.type = var.get_type(); |
| 955 | pinfo.hint = PROPERTY_HINT_OBJECT_ID; |
| 956 | pinfo.hint_string = "Object"; |
| 957 | } |
| 958 | } |
| 959 | } |
| 960 | properties.push_back(SceneDebuggerProperty(pinfo, var)); |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | /// SceneDebuggerTree |
| 965 | SceneDebuggerTree::SceneDebuggerTree(Node *p_root) { |
| 966 | // Flatten tree into list, depth first, use stack to avoid recursion. |
| 967 | List<Node *> stack; |
| 968 | stack.push_back(p_root); |
| 969 | bool is_root = true; |
| 970 | const StringName &is_visible_sn = SNAME("is_visible"); |
| 971 | const StringName &is_visible_in_tree_sn = SNAME("is_visible_in_tree"); |
| 972 | while (stack.size()) { |
| 973 | Node *n = stack.front()->get(); |
| 974 | stack.pop_front(); |
| 975 | |
| 976 | int count = n->get_child_count(); |
| 977 | for (int i = 0; i < count; i++) { |
| 978 | stack.push_front(n->get_child(count - i - 1)); |
| 979 | } |
| 980 | |
| 981 | int view_flags = 0; |
| 982 | if (is_root) { |
| 983 | // Prevent root window visibility from being changed. |
| 984 | is_root = false; |
| 985 | } else if (n->has_method(is_visible_sn)) { |
| 986 | const Variant visible = n->call(is_visible_sn); |
| 987 | if (visible.get_type() == Variant::BOOL) { |
| 988 | view_flags = RemoteNode::VIEW_HAS_VISIBLE_METHOD; |
| 989 | view_flags |= uint8_t(visible) * RemoteNode::VIEW_VISIBLE; |
| 990 | } |
| 991 | if (n->has_method(is_visible_in_tree_sn)) { |
| 992 | const Variant visible_in_tree = n->call(is_visible_in_tree_sn); |
| 993 | if (visible_in_tree.get_type() == Variant::BOOL) { |
| 994 | view_flags |= uint8_t(visible_in_tree) * RemoteNode::VIEW_VISIBLE_IN_TREE; |
| 995 | } |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | String class_name; |
| 1000 | ScriptInstance *script_instance = n->get_script_instance(); |
| 1001 | if (script_instance) { |
| 1002 | Ref<Script> script = script_instance->get_script(); |
| 1003 | if (script.is_valid()) { |
| 1004 | class_name = script->get_global_name(); |
| 1005 | |
| 1006 | if (class_name.is_empty()) { |
| 1007 | // If there is no class_name in this script we just take the script path. |
| 1008 | class_name = script->get_path(); |
| 1009 | } |
| 1010 | } |
| 1011 | } |
| 1012 | nodes.push_back(RemoteNode(count, n->get_name(), class_name.is_empty() ? n->get_class() : class_name, n->get_instance_id(), n->get_scene_file_path(), view_flags)); |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | void SceneDebuggerTree::serialize(Array &p_arr) { |
| 1017 | for (const RemoteNode &n : nodes) { |
| 1018 | p_arr.push_back(n.child_count); |
| 1019 | p_arr.push_back(n.name); |
| 1020 | p_arr.push_back(n.type_name); |
| 1021 | p_arr.push_back(n.id); |
| 1022 | p_arr.push_back(n.scene_file_path); |
| 1023 | p_arr.push_back(n.view_flags); |
| 1024 | } |
| 1025 | } |
| 1026 | |
| 1027 | void SceneDebuggerTree::deserialize(const Array &p_arr) { |
| 1028 | int idx = 0; |
| 1029 | while (p_arr.size() > idx) { |
| 1030 | ERR_FAIL_COND(p_arr.size() < 6); |
| 1031 | CHECK_TYPE(p_arr[idx], INT); // child_count. |
| 1032 | CHECK_TYPE(p_arr[idx + 1], STRING); // name. |
| 1033 | CHECK_TYPE(p_arr[idx + 2], STRING); // type_name. |
| 1034 | CHECK_TYPE(p_arr[idx + 3], INT); // id. |
| 1035 | CHECK_TYPE(p_arr[idx + 4], STRING); // scene_file_path. |
| 1036 | CHECK_TYPE(p_arr[idx + 5], INT); // view_flags. |
| 1037 | nodes.push_back(RemoteNode(p_arr[idx], p_arr[idx + 1], p_arr[idx + 2], p_arr[idx + 3], p_arr[idx + 4], p_arr[idx + 5])); |
| 1038 | idx += 6; |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | /// LiveEditor |
| 1043 | LiveEditor *LiveEditor::get_singleton() { |
| 1044 | return singleton; |
| 1045 | } |
| 1046 | |
| 1047 | void LiveEditor::_send_tree() { |
| 1048 | SceneTree *scene_tree = SceneTree::get_singleton(); |
| 1049 | if (!scene_tree) { |
| 1050 | return; |
| 1051 | } |
| 1052 | |
| 1053 | Array arr; |
| 1054 | // Encoded as a flat list depth first. |
| 1055 | SceneDebuggerTree tree(scene_tree->root); |
| 1056 | tree.serialize(arr); |
| 1057 | EngineDebugger::get_singleton()->send_message("scene:scene_tree", arr); |
| 1058 | } |
| 1059 | |
| 1060 | void LiveEditor::_node_path_func(const NodePath &p_path, int p_id) { |
| 1061 | live_edit_node_path_cache[p_id] = p_path; |
| 1062 | } |
| 1063 | |
| 1064 | void LiveEditor::_res_path_func(const String &p_path, int p_id) { |
| 1065 | live_edit_resource_cache[p_id] = p_path; |
| 1066 | } |
| 1067 | |
| 1068 | void LiveEditor::_node_set_func(int p_id, const StringName &p_prop, const Variant &p_value) { |
| 1069 | SceneTree *scene_tree = SceneTree::get_singleton(); |
| 1070 | if (!scene_tree) { |
| 1071 | return; |
| 1072 | } |
| 1073 | |
| 1074 | if (!live_edit_node_path_cache.has(p_id)) { |
| 1075 | return; |
| 1076 | } |
| 1077 | |
| 1078 | NodePath np = live_edit_node_path_cache[p_id]; |
| 1079 | Node *base = nullptr; |
| 1080 | if (scene_tree->root->has_node(live_edit_root)) { |
| 1081 | base = scene_tree->root->get_node(live_edit_root); |
| 1082 | } |
| 1083 | |
| 1084 | HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene); |
| 1085 | if (!E) { |
| 1086 | return; //scene not editable |
| 1087 | } |
| 1088 | |
| 1089 | for (Node *F : E->value) { |
| 1090 | Node *n = F; |
| 1091 | |
| 1092 | if (base && !base->is_ancestor_of(n)) { |
| 1093 | continue; |
| 1094 | } |
| 1095 | |
| 1096 | if (!n->has_node(np)) { |
| 1097 | continue; |
| 1098 | } |
| 1099 | Node *n2 = n->get_node(np); |
| 1100 | |
| 1101 | // Do not change transform of edited scene root, unless it's the scene being played. |
| 1102 | // See GH-86659 for additional context. |
| 1103 | bool keep_transform = (n2 == n) && (n2->get_parent() != scene_tree->root); |
| 1104 | Variant orig_tf; |
| 1105 | |
| 1106 | if (keep_transform) { |
| 1107 | if (n2->is_class("Node3D")) { |
| 1108 | orig_tf = n2->call("get_transform"); |
| 1109 | } else if (n2->is_class("CanvasItem")) { |
| 1110 | orig_tf = n2->call("_edit_get_state"); |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | n2->set(p_prop, p_value); |
| 1115 | |
| 1116 | if (keep_transform) { |
| 1117 | if (n2->is_class("Node3D")) { |
| 1118 | Variant new_tf = n2->call("get_transform"); |
| 1119 | if (new_tf != orig_tf) { |
| 1120 | n2->call("set_transform", orig_tf); |
| 1121 | } |
| 1122 | } else if (n2->is_class("CanvasItem")) { |
| 1123 | Variant new_tf = n2->call("_edit_get_state"); |
| 1124 | if (new_tf != orig_tf) { |
| 1125 | n2->call("_edit_set_state", orig_tf); |
| 1126 | } |
| 1127 | } |
| 1128 | } |
| 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | void LiveEditor::_node_set_res_func(int p_id, const StringName &p_prop, const String &p_value) { |
| 1133 | Ref<Resource> r = ResourceLoader::load(p_value); |
| 1134 | if (r.is_null()) { |
| 1135 | return; |
| 1136 | } |
| 1137 | _node_set_func(p_id, p_prop, r); |
| 1138 | } |
| 1139 | |
| 1140 | void LiveEditor::_node_call_func(int p_id, const StringName &p_method, const Variant **p_args, int p_argcount) { |
| 1141 | SceneTree *scene_tree = SceneTree::get_singleton(); |
| 1142 | if (!scene_tree) { |
| 1143 | return; |
| 1144 | } |
| 1145 | if (!live_edit_node_path_cache.has(p_id)) { |
| 1146 | return; |
| 1147 | } |
| 1148 | |
| 1149 | NodePath np = live_edit_node_path_cache[p_id]; |
| 1150 | Node *base = nullptr; |
| 1151 | if (scene_tree->root->has_node(live_edit_root)) { |
| 1152 | base = scene_tree->root->get_node(live_edit_root); |
| 1153 | } |
| 1154 | |
| 1155 | HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene); |
| 1156 | if (!E) { |
| 1157 | return; //scene not editable |
| 1158 | } |
| 1159 | |
| 1160 | for (Node *F : E->value) { |
| 1161 | Node *n = F; |
| 1162 | |
| 1163 | if (base && !base->is_ancestor_of(n)) { |
| 1164 | continue; |
| 1165 | } |
| 1166 | |
| 1167 | if (!n->has_node(np)) { |
| 1168 | continue; |
| 1169 | } |
| 1170 | Node *n2 = n->get_node(np); |
| 1171 | |
| 1172 | // Do not change transform of edited scene root, unless it's the scene being played. |
| 1173 | // See GH-86659 for additional context. |
| 1174 | bool keep_transform = (n2 == n) && (n2->get_parent() != scene_tree->root); |
| 1175 | Variant orig_tf; |
| 1176 | |
| 1177 | if (keep_transform) { |
| 1178 | if (n2->is_class("Node3D")) { |
| 1179 | orig_tf = n2->call("get_transform"); |
| 1180 | } else if (n2->is_class("CanvasItem")) { |
| 1181 | orig_tf = n2->call("_edit_get_state"); |
| 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | Callable::CallError ce; |
| 1186 | n2->callp(p_method, p_args, p_argcount, ce); |
| 1187 | |
| 1188 | if (keep_transform) { |
| 1189 | if (n2->is_class("Node3D")) { |
| 1190 | Variant new_tf = n2->call("get_transform"); |
| 1191 | if (new_tf != orig_tf) { |
| 1192 | n2->call("set_transform", orig_tf); |
| 1193 | } |
| 1194 | } else if (n2->is_class("CanvasItem")) { |
| 1195 | Variant new_tf = n2->call("_edit_get_state"); |
| 1196 | if (new_tf != orig_tf) { |
| 1197 | n2->call("_edit_set_state", orig_tf); |
| 1198 | } |
| 1199 | } |
| 1200 | } |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | void LiveEditor::_res_set_func(int p_id, const StringName &p_prop, const Variant &p_value) { |
| 1205 | if (!live_edit_resource_cache.has(p_id)) { |
| 1206 | return; |
| 1207 | } |
| 1208 | |
| 1209 | String resp = live_edit_resource_cache[p_id]; |
| 1210 | |
| 1211 | if (!ResourceCache::has(resp)) { |
| 1212 | return; |
| 1213 | } |
| 1214 | |
| 1215 | Ref<Resource> r = ResourceCache::get_ref(resp); |
| 1216 | if (r.is_null()) { |
| 1217 | return; |
| 1218 | } |
| 1219 | |
| 1220 | r->set(p_prop, p_value); |
| 1221 | } |
| 1222 | |
| 1223 | void LiveEditor::_res_set_res_func(int p_id, const StringName &p_prop, const String &p_value) { |
| 1224 | Ref<Resource> r = ResourceLoader::load(p_value); |
| 1225 | if (r.is_null()) { |
| 1226 | return; |
| 1227 | } |
| 1228 | _res_set_func(p_id, p_prop, r); |
| 1229 | } |
| 1230 | |
| 1231 | void LiveEditor::_res_call_func(int p_id, const StringName &p_method, const Variant **p_args, int p_argcount) { |
| 1232 | if (!live_edit_resource_cache.has(p_id)) { |
| 1233 | return; |
| 1234 | } |
| 1235 | |
| 1236 | String resp = live_edit_resource_cache[p_id]; |
| 1237 | |
| 1238 | if (!ResourceCache::has(resp)) { |
| 1239 | return; |
| 1240 | } |
| 1241 | |
| 1242 | Ref<Resource> r = ResourceCache::get_ref(resp); |
| 1243 | if (r.is_null()) { |
| 1244 | return; |
| 1245 | } |
| 1246 | |
| 1247 | Callable::CallError ce; |
| 1248 | r->callp(p_method, p_args, p_argcount, ce); |
| 1249 | } |
| 1250 | |
| 1251 | void LiveEditor::_root_func(const NodePath &p_scene_path, const String &p_scene_from) { |
| 1252 | live_edit_root = p_scene_path; |
| 1253 | live_edit_scene = p_scene_from; |
| 1254 | } |
| 1255 | |
| 1256 | void LiveEditor::_create_node_func(const NodePath &p_parent, const String &p_type, const String &p_name) { |
| 1257 | SceneTree *scene_tree = SceneTree::get_singleton(); |
| 1258 | if (!scene_tree) { |
| 1259 | return; |
| 1260 | } |
| 1261 | |
| 1262 | Node *base = nullptr; |
| 1263 | if (scene_tree->root->has_node(live_edit_root)) { |
| 1264 | base = scene_tree->root->get_node(live_edit_root); |
| 1265 | } |
| 1266 | |
| 1267 | HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene); |
| 1268 | if (!E) { |
| 1269 | return; //scene not editable |
| 1270 | } |
| 1271 | |
| 1272 | for (Node *F : E->value) { |
| 1273 | Node *n = F; |
| 1274 | |
| 1275 | if (base && !base->is_ancestor_of(n)) { |
| 1276 | continue; |
| 1277 | } |
| 1278 | |
| 1279 | if (!n->has_node(p_parent)) { |
| 1280 | continue; |
| 1281 | } |
| 1282 | Node *n2 = n->get_node(p_parent); |
| 1283 | |
| 1284 | Node *no = Object::cast_to<Node>(ClassDB::instantiate(p_type)); |
| 1285 | if (!no) { |
| 1286 | continue; |
| 1287 | } |
| 1288 | |
| 1289 | no->set_name(p_name); |
| 1290 | n2->add_child(no); |
| 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | void LiveEditor::_instance_node_func(const NodePath &p_parent, const String &p_path, const String &p_name) { |
| 1295 | SceneTree *scene_tree = SceneTree::get_singleton(); |
| 1296 | if (!scene_tree) { |
| 1297 | return; |
| 1298 | } |
| 1299 | |
| 1300 | Ref<PackedScene> ps = ResourceLoader::load(p_path); |
| 1301 | |
| 1302 | if (ps.is_null()) { |
| 1303 | return; |
| 1304 | } |
| 1305 | |
| 1306 | Node *base = nullptr; |
| 1307 | if (scene_tree->root->has_node(live_edit_root)) { |
| 1308 | base = scene_tree->root->get_node(live_edit_root); |
| 1309 | } |
| 1310 | |
| 1311 | HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene); |
| 1312 | if (!E) { |
| 1313 | return; //scene not editable |
| 1314 | } |
| 1315 | |
| 1316 | for (Node *F : E->value) { |
| 1317 | Node *n = F; |
| 1318 | |
| 1319 | if (base && !base->is_ancestor_of(n)) { |
| 1320 | continue; |
| 1321 | } |
| 1322 | |
| 1323 | if (!n->has_node(p_parent)) { |
| 1324 | continue; |
| 1325 | } |
| 1326 | Node *n2 = n->get_node(p_parent); |
| 1327 | |
| 1328 | Node *no = ps->instantiate(); |
| 1329 | if (!no) { |
| 1330 | continue; |
| 1331 | } |
| 1332 | |
| 1333 | no->set_name(p_name); |
| 1334 | n2->add_child(no); |
| 1335 | } |
| 1336 | } |
| 1337 | |
| 1338 | void LiveEditor::_remove_node_func(const NodePath &p_at) { |
| 1339 | SceneTree *scene_tree = SceneTree::get_singleton(); |
| 1340 | if (!scene_tree) { |
| 1341 | return; |
| 1342 | } |
| 1343 | |
| 1344 | Node *base = nullptr; |
| 1345 | if (scene_tree->root->has_node(live_edit_root)) { |
| 1346 | base = scene_tree->root->get_node(live_edit_root); |
| 1347 | } |
| 1348 | |
| 1349 | HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene); |
| 1350 | if (!E) { |
| 1351 | return; //scene not editable |
| 1352 | } |
| 1353 | |
| 1354 | LocalVector<Node *> to_delete; |
| 1355 | |
| 1356 | for (const Node *n : E->value) { |
| 1357 | if (base && !base->is_ancestor_of(n)) { |
| 1358 | continue; |
| 1359 | } |
| 1360 | |
| 1361 | if (!n->has_node(p_at)) { |
| 1362 | continue; |
| 1363 | } |
| 1364 | Node *n2 = n->get_node(p_at); |
| 1365 | |
| 1366 | to_delete.push_back(n2); |
| 1367 | } |
| 1368 | |
| 1369 | for (Node *node : to_delete) { |
| 1370 | memdelete(node); |
| 1371 | } |
| 1372 | } |
| 1373 | |
| 1374 | void LiveEditor::_remove_and_keep_node_func(const NodePath &p_at, ObjectID p_keep_id) { |
| 1375 | SceneTree *scene_tree = SceneTree::get_singleton(); |
| 1376 | if (!scene_tree) { |
| 1377 | return; |
| 1378 | } |
| 1379 | |
| 1380 | Node *base = nullptr; |
| 1381 | if (scene_tree->root->has_node(live_edit_root)) { |
| 1382 | base = scene_tree->root->get_node(live_edit_root); |
| 1383 | } |
| 1384 | |
| 1385 | HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene); |
| 1386 | if (!E) { |
| 1387 | return; //scene not editable |
| 1388 | } |
| 1389 | |
| 1390 | LocalVector<Node *> to_remove; |
| 1391 | for (Node *n : E->value) { |
| 1392 | if (base && !base->is_ancestor_of(n)) { |
| 1393 | continue; |
| 1394 | } |
| 1395 | |
| 1396 | if (!n->has_node(p_at)) { |
| 1397 | continue; |
| 1398 | } |
| 1399 | |
| 1400 | to_remove.push_back(n); |
| 1401 | } |
| 1402 | |
| 1403 | for (Node *n : to_remove) { |
| 1404 | Node *n2 = n->get_node(p_at); |
| 1405 | n2->get_parent()->remove_child(n2); |
| 1406 | live_edit_remove_list[n][p_keep_id] = n2; |
| 1407 | } |
| 1408 | } |
| 1409 | |
| 1410 | void LiveEditor::_restore_node_func(ObjectID p_id, const NodePath &p_at, int p_at_pos) { |
| 1411 | SceneTree *scene_tree = SceneTree::get_singleton(); |
| 1412 | if (!scene_tree) { |
| 1413 | return; |
| 1414 | } |
| 1415 | |
| 1416 | Node *base = nullptr; |
| 1417 | if (scene_tree->root->has_node(live_edit_root)) { |
| 1418 | base = scene_tree->root->get_node(live_edit_root); |
| 1419 | } |
| 1420 | |
| 1421 | HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene); |
| 1422 | if (!E) { |
| 1423 | return; //scene not editable |
| 1424 | } |
| 1425 | |
| 1426 | for (HashSet<Node *>::Iterator F = E->value.begin(); F;) { |
| 1427 | HashSet<Node *>::Iterator N = F; |
| 1428 | ++N; |
| 1429 | |
| 1430 | Node *n = *F; |
| 1431 | |
| 1432 | if (base && !base->is_ancestor_of(n)) { |
| 1433 | continue; |
| 1434 | } |
| 1435 | |
| 1436 | if (!n->has_node(p_at)) { |
| 1437 | continue; |
| 1438 | } |
| 1439 | Node *n2 = n->get_node(p_at); |
| 1440 | |
| 1441 | HashMap<Node *, HashMap<ObjectID, Node *>>::Iterator EN = live_edit_remove_list.find(n); |
| 1442 | |
| 1443 | if (!EN) { |
| 1444 | continue; |
| 1445 | } |
| 1446 | |
| 1447 | HashMap<ObjectID, Node *>::Iterator FN = EN->value.find(p_id); |
| 1448 | |
| 1449 | if (!FN) { |
| 1450 | continue; |
| 1451 | } |
| 1452 | n2->add_child(FN->value); |
| 1453 | |
| 1454 | EN->value.remove(FN); |
| 1455 | |
| 1456 | if (EN->value.is_empty()) { |
| 1457 | live_edit_remove_list.remove(EN); |
| 1458 | } |
| 1459 | |
| 1460 | F = N; |
| 1461 | } |
| 1462 | } |
| 1463 | |
| 1464 | void LiveEditor::_duplicate_node_func(const NodePath &p_at, const String &p_new_name) { |
| 1465 | SceneTree *scene_tree = SceneTree::get_singleton(); |
| 1466 | if (!scene_tree) { |
| 1467 | return; |
| 1468 | } |
| 1469 | |
| 1470 | Node *base = nullptr; |
| 1471 | if (scene_tree->root->has_node(live_edit_root)) { |
| 1472 | base = scene_tree->root->get_node(live_edit_root); |
| 1473 | } |
| 1474 | |
| 1475 | HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene); |
| 1476 | if (!E) { |
| 1477 | return; //scene not editable |
| 1478 | } |
| 1479 | |
| 1480 | for (Node *F : E->value) { |
| 1481 | Node *n = F; |
| 1482 | |
| 1483 | if (base && !base->is_ancestor_of(n)) { |
| 1484 | continue; |
| 1485 | } |
| 1486 | |
| 1487 | if (!n->has_node(p_at)) { |
| 1488 | continue; |
| 1489 | } |
| 1490 | Node *n2 = n->get_node(p_at); |
| 1491 | |
| 1492 | Node *dup = n2->duplicate(Node::DUPLICATE_SIGNALS | Node::DUPLICATE_GROUPS | Node::DUPLICATE_SCRIPTS); |
| 1493 | |
| 1494 | if (!dup) { |
| 1495 | continue; |
| 1496 | } |
| 1497 | |
| 1498 | dup->set_name(p_new_name); |
| 1499 | n2->get_parent()->add_child(dup); |
| 1500 | } |
| 1501 | } |
| 1502 | |
| 1503 | void LiveEditor::_reparent_node_func(const NodePath &p_at, const NodePath &p_new_place, const String &p_new_name, int p_at_pos) { |
| 1504 | SceneTree *scene_tree = SceneTree::get_singleton(); |
| 1505 | if (!scene_tree) { |
| 1506 | return; |
| 1507 | } |
| 1508 | |
| 1509 | Node *base = nullptr; |
| 1510 | if (scene_tree->root->has_node(live_edit_root)) { |
| 1511 | base = scene_tree->root->get_node(live_edit_root); |
| 1512 | } |
| 1513 | |
| 1514 | HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene); |
| 1515 | if (!E) { |
| 1516 | return; //scene not editable |
| 1517 | } |
| 1518 | |
| 1519 | for (Node *F : E->value) { |
| 1520 | Node *n = F; |
| 1521 | |
| 1522 | if (base && !base->is_ancestor_of(n)) { |
| 1523 | continue; |
| 1524 | } |
| 1525 | |
| 1526 | if (!n->has_node(p_at)) { |
| 1527 | continue; |
| 1528 | } |
| 1529 | Node *nfrom = n->get_node(p_at); |
| 1530 | |
| 1531 | if (!n->has_node(p_new_place)) { |
| 1532 | continue; |
| 1533 | } |
| 1534 | Node *nto = n->get_node(p_new_place); |
| 1535 | |
| 1536 | nfrom->get_parent()->remove_child(nfrom); |
| 1537 | nfrom->set_name(p_new_name); |
| 1538 | |
| 1539 | nto->add_child(nfrom); |
| 1540 | if (p_at_pos >= 0) { |
| 1541 | nto->move_child(nfrom, p_at_pos); |
| 1542 | } |
| 1543 | } |
| 1544 | } |
| 1545 | |
| 1546 | /// RuntimeNodeSelect |
| 1547 | RuntimeNodeSelect *RuntimeNodeSelect::get_singleton() { |
| 1548 | return singleton; |
| 1549 | } |
| 1550 | |
| 1551 | RuntimeNodeSelect::~RuntimeNodeSelect() { |
| 1552 | if (selection_list && !selection_list->is_visible()) { |
| 1553 | memdelete(selection_list); |
| 1554 | } |
| 1555 | |
| 1556 | if (draw_canvas.is_valid()) { |
| 1557 | RS::get_singleton()->free_rid(sel_drag_ci); |
| 1558 | RS::get_singleton()->free_rid(sbox_2d_ci); |
| 1559 | RS::get_singleton()->free_rid(draw_canvas); |
| 1560 | } |
| 1561 | } |
| 1562 | |
| 1563 | void RuntimeNodeSelect::_setup(const Dictionary &p_settings) { |
| 1564 | Window *root = SceneTree::get_singleton()->get_root(); |
| 1565 | ERR_FAIL_COND(root->is_connected(SceneStringName(window_input), callable_mp(this, &RuntimeNodeSelect::_root_window_input))); |
| 1566 | |
| 1567 | root->connect(SceneStringName(window_input), callable_mp(this, &RuntimeNodeSelect::_root_window_input)); |
| 1568 | root->connect("size_changed", callable_mp(this, &RuntimeNodeSelect::_queue_selection_update), CONNECT_DEFERRED); |
| 1569 | |
| 1570 | max_selection = p_settings.get("debugger/max_node_selection", 1); |
| 1571 | |
| 1572 | panner.instantiate(); |
| 1573 | panner->set_callbacks(callable_mp(this, &RuntimeNodeSelect::_pan_callback), callable_mp(this, &RuntimeNodeSelect::_zoom_callback)); |
| 1574 | |
| 1575 | ViewPanner::ControlScheme panning_scheme = (ViewPanner::ControlScheme)p_settings.get("editors/panning/2d_editor_panning_scheme", 0).operator int(); |
| 1576 | bool simple_panning = p_settings.get("editors/panning/simple_panning", false); |
| 1577 | int pan_speed = p_settings.get("editors/panning/2d_editor_pan_speed", 20); |
| 1578 | Array keys = p_settings.get("canvas_item_editor/pan_view", Array()).operator Array(); |
| 1579 | panner->setup(panning_scheme, DebuggerMarshalls::deserialize_key_shortcut(keys), simple_panning); |
| 1580 | panner->setup_warped_panning(root, p_settings.get("editors/panning/warped_mouse_panning", true)); |
| 1581 | panner->set_scroll_speed(pan_speed); |
| 1582 | |
| 1583 | sel_2d_grab_dist = p_settings.get("editors/polygon_editor/point_grab_radius", 0); |
| 1584 | sel_2d_scale = MAX(1, Math::ceil(2.0 / (float)GLOBAL_GET("display/window/stretch/scale"))); |
| 1585 | |
| 1586 | selection_area_fill = p_settings.get("box_selection_fill_color", Color()); |
| 1587 | selection_area_outline = p_settings.get("box_selection_stroke_color", Color()); |
| 1588 | |
| 1589 | draw_canvas = RS::get_singleton()->canvas_create(); |
| 1590 | sel_drag_ci = RS::get_singleton()->canvas_item_create(); |
| 1591 | |
| 1592 | /// 2D Selection Box Generation |
| 1593 | |
| 1594 | sbox_2d_ci = RS::get_singleton()->canvas_item_create(); |
| 1595 | RS::get_singleton()->viewport_attach_canvas(root->get_viewport_rid(), draw_canvas); |
| 1596 | RS::get_singleton()->canvas_item_set_parent(sel_drag_ci, draw_canvas); |
| 1597 | RS::get_singleton()->canvas_item_set_parent(sbox_2d_ci, draw_canvas); |
| 1598 | |
| 1599 | #ifndef _3D_DISABLED |
| 1600 | cursor = Cursor(); |
| 1601 | |
| 1602 | camera_fov = p_settings.get("editors/3d/default_fov", 70); |
| 1603 | camera_znear = p_settings.get("editors/3d/default_z_near", 0.05); |
| 1604 | camera_zfar = p_settings.get("editors/3d/default_z_far", 4'000); |
| 1605 | |
| 1606 | invert_x_axis = p_settings.get("editors/3d/navigation/invert_x_axis", false); |
| 1607 | invert_y_axis = p_settings.get("editors/3d/navigation/invert_y_axis", false); |
| 1608 | warped_mouse_panning_3d = p_settings.get("editors/3d/navigation/warped_mouse_panning", true); |
| 1609 | |
| 1610 | freelook_base_speed = p_settings.get("editors/3d/freelook/freelook_base_speed", 5); |
| 1611 | freelook_sensitivity = Math::deg_to_rad((real_t)p_settings.get("editors/3d/freelook/freelook_sensitivity", 0.25)); |
| 1612 | orbit_sensitivity = Math::deg_to_rad((real_t)p_settings.get("editors/3d/navigation_feel/orbit_sensitivity", 0.004)); |
| 1613 | translation_sensitivity = p_settings.get("editors/3d/navigation_feel/translation_sensitivity", 1); |
| 1614 | |
| 1615 | /// 3D Selection Box Generation |
| 1616 | // Copied from the Node3DEditor implementation. |
| 1617 | |
| 1618 | sbox_3d_color = p_settings.get("editors/3d/selection_box_color", Color()); |
| 1619 | |
| 1620 | // Use two AABBs to create the illusion of a slightly thicker line. |
| 1621 | AABB aabb(Vector3(), Vector3(1, 1, 1)); |
| 1622 | |
| 1623 | // Create a x-ray (visible through solid surfaces) and standard version of the selection box. |
| 1624 | // Both will be drawn at the same position, but with different opacity. |
| 1625 | // This lets the user see where the selection is while still having a sense of depth. |
| 1626 | Ref<SurfaceTool> st = memnew(SurfaceTool); |
| 1627 | Ref<SurfaceTool> st_xray = memnew(SurfaceTool); |
| 1628 | |
| 1629 | st->begin(Mesh::PRIMITIVE_LINES); |
| 1630 | st_xray->begin(Mesh::PRIMITIVE_LINES); |
| 1631 | for (int i = 0; i < 12; i++) { |
| 1632 | Vector3 a, b; |
| 1633 | aabb.get_edge(i, a, b); |
| 1634 | |
| 1635 | st->add_vertex(a); |
| 1636 | st->add_vertex(b); |
| 1637 | st_xray->add_vertex(a); |
| 1638 | st_xray->add_vertex(b); |
| 1639 | } |
| 1640 | |
| 1641 | Ref<StandardMaterial3D> mat = memnew(StandardMaterial3D); |
| 1642 | mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED); |
| 1643 | mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true); |
| 1644 | mat->set_albedo(sbox_3d_color); |
| 1645 | mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA); |
| 1646 | st->set_material(mat); |
| 1647 | sbox_3d_mesh = st->commit(); |
| 1648 | |
| 1649 | Ref<StandardMaterial3D> mat_xray = memnew(StandardMaterial3D); |
| 1650 | mat_xray->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED); |
| 1651 | mat_xray->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true); |
| 1652 | mat_xray->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, true); |
| 1653 | mat_xray->set_albedo(sbox_3d_color * Color(1, 1, 1, 0.15)); |
| 1654 | mat_xray->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA); |
| 1655 | st_xray->set_material(mat_xray); |
| 1656 | sbox_3d_mesh_xray = st_xray->commit(); |
| 1657 | #endif // _3D_DISABLED |
| 1658 | |
| 1659 | SceneTree::get_singleton()->connect("process_frame", callable_mp(this, &RuntimeNodeSelect::_process_frame)); |
| 1660 | SceneTree::get_singleton()->connect("physics_frame", callable_mp(this, &RuntimeNodeSelect::_physics_frame)); |
| 1661 | |
| 1662 | // This function will be called before the root enters the tree at first when the Game view is passing its settings to |
| 1663 | // the debugger, so queue the update for after it enters. |
| 1664 | root->connect(SceneStringName(tree_entered), callable_mp(this, &RuntimeNodeSelect::_update_input_state), Object::CONNECT_ONE_SHOT); |
| 1665 | } |
| 1666 | |
| 1667 | void RuntimeNodeSelect::_node_set_type(NodeType p_type) { |
| 1668 | node_select_type = p_type; |
| 1669 | _update_input_state(); |
| 1670 | } |
| 1671 | |
| 1672 | void RuntimeNodeSelect::_select_set_mode(SelectMode p_mode) { |
| 1673 | node_select_mode = p_mode; |
| 1674 | } |
| 1675 | |
| 1676 | void RuntimeNodeSelect::_set_camera_override_enabled(bool p_enabled) { |
| 1677 | camera_override = p_enabled; |
| 1678 | |
| 1679 | if (camera_first_override) { |
| 1680 | _reset_camera_2d(); |
| 1681 | #ifndef _3D_DISABLED |
| 1682 | _reset_camera_3d(); |
| 1683 | #endif // _3D_DISABLED |
| 1684 | |
| 1685 | camera_first_override = false; |
| 1686 | } else if (p_enabled) { |
| 1687 | _update_view_2d(); |
| 1688 | |
| 1689 | #ifndef _3D_DISABLED |
| 1690 | Window *root = SceneTree::get_singleton()->get_root(); |
| 1691 | ERR_FAIL_COND(!root->is_camera_3d_override_enabled()); |
| 1692 | Camera3D *override_camera = root->get_override_camera_3d(); |
| 1693 | override_camera->set_transform(_get_cursor_transform()); |
| 1694 | override_camera->set_perspective(camera_fov * cursor.fov_scale, camera_znear, camera_zfar); |
| 1695 | #endif // _3D_DISABLED |
| 1696 | } |
| 1697 | } |
| 1698 | |
| 1699 | void RuntimeNodeSelect::_root_window_input(const Ref<InputEvent> &p_event) { |
| 1700 | Window *root = SceneTree::get_singleton()->get_root(); |
| 1701 | if (node_select_type == NODE_TYPE_NONE || (selection_list && selection_list->is_visible())) { |
| 1702 | // Workaround for platforms that don't allow subwindows. |
| 1703 | if (selection_list && selection_list->is_visible() && selection_list->is_embedded()) { |
| 1704 | root->set_disable_input_override(false); |
| 1705 | selection_list->push_input(p_event); |
| 1706 | callable_mp(root->get_viewport(), &Viewport::set_disable_input_override).call_deferred(true); |
| 1707 | } |
| 1708 | |
| 1709 | return; |
| 1710 | } |
| 1711 | |
| 1712 | bool is_dragging_camera = false; |
| 1713 | if (camera_override) { |
| 1714 | if (node_select_type == NODE_TYPE_2D) { |
| 1715 | is_dragging_camera = panner->gui_input(p_event, Rect2(Vector2(), root->get_visible_rect().get_size())); |
| 1716 | #ifndef _3D_DISABLED |
| 1717 | } else if (node_select_type == NODE_TYPE_3D && selection_drag_state == SELECTION_DRAG_NONE) { |
| 1718 | if (_handle_3d_input(p_event)) { |
| 1719 | return; |
| 1720 | } |
| 1721 | #endif // _3D_DISABLED |
| 1722 | } |
| 1723 | } |
| 1724 | |
| 1725 | Ref<InputEventMouseButton> b = p_event; |
| 1726 | |
| 1727 | if (selection_drag_state == SELECTION_DRAG_MOVE) { |
| 1728 | Ref<InputEventMouseMotion> m = p_event; |
| 1729 | if (m.is_valid()) { |
| 1730 | _update_selection_drag(root->get_screen_transform().affine_inverse().xform(m->get_position())); |
| 1731 | return; |
| 1732 | } else if (b.is_valid()) { |
| 1733 | // Account for actions like zooming. |
| 1734 | _update_selection_drag(root->get_screen_transform().affine_inverse().xform(b->get_position())); |
| 1735 | } |
| 1736 | } |
| 1737 | |
| 1738 | if (b.is_null()) { |
| 1739 | return; |
| 1740 | } |
| 1741 | |
| 1742 | // Ignore mouse wheel inputs. |
| 1743 | if (b->get_button_index() != MouseButton::LEFT && b->get_button_index() != MouseButton::RIGHT) { |
| 1744 | return; |
| 1745 | } |
| 1746 | |
| 1747 | if (selection_drag_state == SELECTION_DRAG_MOVE && !b->is_pressed() && b->get_button_index() == MouseButton::LEFT) { |
| 1748 | selection_drag_state = SELECTION_DRAG_END; |
| 1749 | selection_drag_area = selection_drag_area.abs(); |
| 1750 | _update_selection_drag(); |
| 1751 | |
| 1752 | // Trigger a selection in the position on release. |
| 1753 | if (multi_shortcut_pressed) { |
| 1754 | selection_position = root->get_screen_transform().affine_inverse().xform(b->get_position()); |
| 1755 | } |
| 1756 | } |
| 1757 | |
| 1758 | if (!is_dragging_camera && b->is_pressed()) { |
| 1759 | multi_shortcut_pressed = b->is_shift_pressed(); |
| 1760 | list_shortcut_pressed = node_select_mode == SELECT_MODE_SINGLE && b->get_button_index() == MouseButton::RIGHT && b->is_alt_pressed(); |
| 1761 | if (list_shortcut_pressed || b->get_button_index() == MouseButton::LEFT) { |
| 1762 | selection_position = root->get_screen_transform().affine_inverse().xform(b->get_position()); |
| 1763 | } |
| 1764 | } |
| 1765 | } |
| 1766 | |
| 1767 | void RuntimeNodeSelect::_items_popup_index_pressed(int p_index, PopupMenu *p_popup) { |
| 1768 | Object *obj = p_popup->get_item_metadata(p_index).get_validated_object(); |
| 1769 | if (obj) { |
| 1770 | Vector<Node *> node; |
| 1771 | node.append(Object::cast_to<Node>(obj)); |
| 1772 | _send_ids(node); |
| 1773 | } |
| 1774 | } |
| 1775 | |
| 1776 | void RuntimeNodeSelect::_update_input_state() { |
| 1777 | SceneTree *scene_tree = SceneTree::get_singleton(); |
| 1778 | // This function can be called at the very beginning, when the root hasn't entered the tree yet. |
| 1779 | // So check first to avoid a crash. |
| 1780 | if (!scene_tree->get_root()->is_inside_tree()) { |
| 1781 | return; |
| 1782 | } |
| 1783 | |
| 1784 | bool disable_input = scene_tree->is_suspended() || node_select_type != RuntimeNodeSelect::NODE_TYPE_NONE; |
| 1785 | Input::get_singleton()->set_disable_input(disable_input); |
| 1786 | Input::get_singleton()->set_mouse_mode_override_enabled(disable_input); |
| 1787 | scene_tree->get_root()->set_disable_input_override(disable_input); |
| 1788 | } |
| 1789 | |
| 1790 | void RuntimeNodeSelect::_process_frame() { |
| 1791 | #ifndef _3D_DISABLED |
| 1792 | if (camera_freelook) { |
| 1793 | Transform3D transform = _get_cursor_transform(); |
| 1794 | Vector3 forward = transform.basis.xform(Vector3(0, 0, -1)); |
| 1795 | const Vector3 right = transform.basis.xform(Vector3(1, 0, 0)); |
| 1796 | Vector3 up = transform.basis.xform(Vector3(0, 1, 0)); |
| 1797 | |
| 1798 | Vector3 direction; |
| 1799 | |
| 1800 | Input *input = Input::get_singleton(); |
| 1801 | bool was_input_disabled = input->is_input_disabled(); |
| 1802 | if (was_input_disabled) { |
| 1803 | input->set_disable_input(false); |
| 1804 | } |
| 1805 | |
| 1806 | if (input->is_physical_key_pressed(Key::A)) { |
| 1807 | direction -= right; |
| 1808 | } |
| 1809 | if (input->is_physical_key_pressed(Key::D)) { |
| 1810 | direction += right; |
| 1811 | } |
| 1812 | if (input->is_physical_key_pressed(Key::W)) { |
| 1813 | direction += forward; |
| 1814 | } |
| 1815 | if (input->is_physical_key_pressed(Key::S)) { |
| 1816 | direction -= forward; |
| 1817 | } |
| 1818 | if (input->is_physical_key_pressed(Key::E)) { |
| 1819 | direction += up; |
| 1820 | } |
| 1821 | if (input->is_physical_key_pressed(Key::Q)) { |
| 1822 | direction -= up; |
| 1823 | } |
| 1824 | |
| 1825 | real_t speed = freelook_base_speed; |
| 1826 | if (input->is_physical_key_pressed(Key::SHIFT)) { |
| 1827 | speed *= 3.0; |
| 1828 | } |
| 1829 | if (input->is_physical_key_pressed(Key::ALT)) { |
| 1830 | speed *= 0.333333; |
| 1831 | } |
| 1832 | |
| 1833 | if (was_input_disabled) { |
| 1834 | input->set_disable_input(true); |
| 1835 | } |
| 1836 | |
| 1837 | if (direction != Vector3()) { |
| 1838 | Window *root = SceneTree::get_singleton()->get_root(); |
| 1839 | ERR_FAIL_COND(!root->is_camera_3d_override_enabled()); |
| 1840 | |
| 1841 | // Calculate the process time manually, as the time scale is frozen. |
| 1842 | const double process_time = (1.0 / Engine::get_singleton()->get_frames_per_second()) * Engine::get_singleton()->get_unfrozen_time_scale(); |
| 1843 | const Vector3 motion = direction * speed * process_time; |
| 1844 | cursor.pos += motion; |
| 1845 | cursor.eye_pos += motion; |
| 1846 | |
| 1847 | root->get_override_camera_3d()->set_transform(_get_cursor_transform()); |
| 1848 | } |
| 1849 | } |
| 1850 | #endif // _3D_DISABLED |
| 1851 | |
| 1852 | if (selection_update_queued || !SceneTree::get_singleton()->is_suspended()) { |
| 1853 | selection_update_queued = false; |
| 1854 | if (has_selection) { |
| 1855 | _update_selection(); |
| 1856 | } |
| 1857 | } |
| 1858 | } |
| 1859 | |
| 1860 | void RuntimeNodeSelect::_physics_frame() { |
| 1861 | if (selection_drag_state != SELECTION_DRAG_END && (selection_drag_state == SELECTION_DRAG_MOVE || Math::is_inf(selection_position.x))) { |
| 1862 | return; |
| 1863 | } |
| 1864 | |
| 1865 | Window *root = SceneTree::get_singleton()->get_root(); |
| 1866 | bool selection_drag_valid = selection_drag_state == SELECTION_DRAG_END && selection_drag_area.get_area() > SELECTION_MIN_AREA; |
| 1867 | Vector<SelectResult> items; |
| 1868 | |
| 1869 | if (node_select_type == NODE_TYPE_2D) { |
| 1870 | if (selection_drag_valid) { |
| 1871 | for (int i = 0; i < root->get_child_count(); i++) { |
| 1872 | _find_canvas_items_at_rect(selection_drag_area, root->get_child(i), items); |
| 1873 | } |
| 1874 | } else if (!Math::is_inf(selection_position.x)) { |
| 1875 | for (int i = 0; i < root->get_child_count(); i++) { |
| 1876 | _find_canvas_items_at_pos(selection_position, root->get_child(i), items); |
| 1877 | } |
| 1878 | } |
| 1879 | |
| 1880 | #ifndef _3D_DISABLED |
| 1881 | } else if (node_select_type == NODE_TYPE_3D) { |
| 1882 | if (selection_drag_valid) { |
| 1883 | _find_3d_items_at_rect(selection_drag_area, items); |
| 1884 | } else { |
| 1885 | _find_3d_items_at_pos(selection_position, items); |
| 1886 | } |
| 1887 | #endif // _3D_DISABLED |
| 1888 | } |
| 1889 | |
| 1890 | if ((prefer_group_selection || avoid_locked_nodes) && !list_shortcut_pressed && node_select_mode == SELECT_MODE_SINGLE) { |
| 1891 | for (int i = 0; i < items.size(); i++) { |
| 1892 | Node *node = items[i].item; |
| 1893 | Node *final_node = node; |
| 1894 | real_t order = items[i].order; |
| 1895 | |
| 1896 | // Replace the node by the group if grouped. |
| 1897 | if (prefer_group_selection) { |
| 1898 | while (node && node != root) { |
| 1899 | if (node->has_meta("_edit_group_")) { |
| 1900 | final_node = node; |
| 1901 | |
| 1902 | if (Object::cast_to<CanvasItem>(final_node)) { |
| 1903 | CanvasItem *ci_tmp = Object::cast_to<CanvasItem>(final_node); |
| 1904 | order = ci_tmp->get_effective_z_index() + ci_tmp->get_canvas_layer(); |
| 1905 | #ifndef _3D_DISABLED |
| 1906 | } else if (Object::cast_to<Node3D>(final_node)) { |
| 1907 | Node3D *node3d_tmp = Object::cast_to<Node3D>(final_node); |
| 1908 | Camera3D *camera = root->get_camera_3d(); |
| 1909 | Vector3 pos = camera->project_ray_origin(selection_position); |
| 1910 | order = -pos.distance_to(node3d_tmp->get_global_transform().origin); |
| 1911 | #endif // _3D_DISABLED |
| 1912 | } |
| 1913 | } |
| 1914 | node = node->get_parent(); |
| 1915 | } |
| 1916 | } |
| 1917 | |
| 1918 | // Filter out locked nodes. |
| 1919 | if (avoid_locked_nodes && final_node->get_meta("_edit_lock_", false)) { |
| 1920 | items.remove_at(i); |
| 1921 | i--; |
| 1922 | continue; |
| 1923 | } |
| 1924 | |
| 1925 | items.write[i].item = final_node; |
| 1926 | items.write[i].order = order; |
| 1927 | } |
| 1928 | } |
| 1929 | |
| 1930 | // Remove possible duplicates. |
| 1931 | for (int i = 0; i < items.size(); i++) { |
| 1932 | Node *item = items[i].item; |
| 1933 | for (int j = 0; j < i; j++) { |
| 1934 | if (items[j].item == item) { |
| 1935 | items.remove_at(i); |
| 1936 | i--; |
| 1937 | |
| 1938 | break; |
| 1939 | } |
| 1940 | } |
| 1941 | } |
| 1942 | |
| 1943 | items.sort(); |
| 1944 | |
| 1945 | switch (selection_drag_state) { |
| 1946 | case SELECTION_DRAG_END: { |
| 1947 | selection_position = Point2(Math::INF, Math::INF); |
| 1948 | selection_drag_state = SELECTION_DRAG_NONE; |
| 1949 | |
| 1950 | if (selection_drag_area.get_area() > SELECTION_MIN_AREA) { |
| 1951 | if (!items.is_empty()) { |
| 1952 | Vector<Node *> nodes; |
| 1953 | for (const SelectResult item : items) { |
| 1954 | nodes.append(item.item); |
| 1955 | } |
| 1956 | _send_ids(nodes, false); |
| 1957 | } |
| 1958 | |
| 1959 | _update_selection_drag(); |
| 1960 | return; |
| 1961 | } |
| 1962 | |
| 1963 | _update_selection_drag(); |
| 1964 | } break; |
| 1965 | |
| 1966 | case SELECTION_DRAG_NONE: { |
| 1967 | if (node_select_mode == SELECT_MODE_LIST) { |
| 1968 | break; |
| 1969 | } |
| 1970 | |
| 1971 | if (multi_shortcut_pressed) { |
| 1972 | // Allow forcing box selection when an item was clicked. |
| 1973 | selection_drag_state = SELECTION_DRAG_MOVE; |
| 1974 | } else if (items.is_empty()) { |
| 1975 | #ifdef _3D_DISABLED |
| 1976 | if (!selected_ci_nodes.is_empty()) { |
| 1977 | #else |
| 1978 | if (!selected_ci_nodes.is_empty() || !selected_3d_nodes.is_empty()) { |
| 1979 | #endif // _3D_DISABLED |
| 1980 | EngineDebugger::get_singleton()->send_message("remote_nothing_selected", Array()); |
| 1981 | _clear_selection(); |
| 1982 | } |
| 1983 | |
| 1984 | selection_drag_state = SELECTION_DRAG_MOVE; |
| 1985 | } else { |
| 1986 | break; |
| 1987 | } |
| 1988 | |
| 1989 | [[fallthrough]]; |
| 1990 | } |
| 1991 | |
| 1992 | case SELECTION_DRAG_MOVE: { |
| 1993 | selection_drag_area.position = selection_position; |
| 1994 | |
| 1995 | // Stop selection on click, so it can happen on release if the selection area doesn't pass the threshold. |
| 1996 | if (multi_shortcut_pressed) { |
| 1997 | return; |
| 1998 | } |
| 1999 | } |
| 2000 | } |
| 2001 | |
| 2002 | if (items.is_empty()) { |
| 2003 | selection_position = Point2(Math::INF, Math::INF); |
| 2004 | return; |
| 2005 | } |
| 2006 | if ((!list_shortcut_pressed && node_select_mode == SELECT_MODE_SINGLE) || items.size() == 1) { |
| 2007 | selection_position = Point2(Math::INF, Math::INF); |
| 2008 | |
| 2009 | Vector<Node *> node; |
| 2010 | node.append(items[0].item); |
| 2011 | _send_ids(node); |
| 2012 | |
| 2013 | return; |
| 2014 | } |
| 2015 | |
| 2016 | if (!selection_list && (list_shortcut_pressed || node_select_mode == SELECT_MODE_LIST)) { |
| 2017 | _open_selection_list(items, selection_position); |
| 2018 | } |
| 2019 | |
| 2020 | selection_position = Point2(Math::INF, Math::INF); |
| 2021 | } |
| 2022 | |
| 2023 | void RuntimeNodeSelect::_send_ids(const Vector<Node *> &p_picked_nodes, bool p_invert_new_selections) { |
| 2024 | ERR_FAIL_COND(p_picked_nodes.is_empty()); |
| 2025 | |
| 2026 | Vector<Node *> picked_nodes = p_picked_nodes; |
| 2027 | Array message; |
| 2028 | |
| 2029 | if (!multi_shortcut_pressed) { |
| 2030 | if (picked_nodes.size() > max_selection) { |
| 2031 | picked_nodes.resize(max_selection); |
| 2032 | EngineDebugger::get_singleton()->send_message("show_selection_limit_warning", Array()); |
| 2033 | } |
| 2034 | |
| 2035 | for (const Node *node : picked_nodes) { |
| 2036 | SceneDebuggerObject obj(node->get_instance_id()); |
| 2037 | Array arr; |
| 2038 | obj.serialize(arr); |
| 2039 | message.append(arr); |
| 2040 | } |
| 2041 | |
| 2042 | EngineDebugger::get_singleton()->send_message("remote_objects_selected", message); |
| 2043 | _set_selected_nodes(picked_nodes); |
| 2044 | |
| 2045 | return; |
| 2046 | } |
| 2047 | |
| 2048 | LocalVector<Node *> nodes; |
| 2049 | LocalVector<ObjectID> ids; |
| 2050 | for (Node *node : picked_nodes) { |
| 2051 | ObjectID id = node->get_instance_id(); |
| 2052 | if (CanvasItem *ci = Object::cast_to<CanvasItem>(node)) { |
| 2053 | if (selected_ci_nodes.has(id)) { |
| 2054 | if (p_invert_new_selections) { |
| 2055 | selected_ci_nodes.erase(id); |
| 2056 | } |
| 2057 | } else { |
| 2058 | ids.push_back(id); |
| 2059 | nodes.push_back(ci); |
| 2060 | } |
| 2061 | } else { |
| 2062 | #ifndef _3D_DISABLED |
| 2063 | if (Node3D *node3d = Object::cast_to<Node3D>(node)) { |
| 2064 | if (selected_3d_nodes.has(id)) { |
| 2065 | if (p_invert_new_selections) { |
| 2066 | selected_3d_nodes.erase(id); |
| 2067 | } |
| 2068 | } else { |
| 2069 | ids.push_back(id); |
| 2070 | nodes.push_back(node3d); |
| 2071 | } |
| 2072 | } |
| 2073 | #endif // _3D_DISABLED |
| 2074 | } |
| 2075 | } |
| 2076 | |
| 2077 | uint32_t limit = max_selection - selected_ci_nodes.size(); |
| 2078 | #ifndef _3D_DISABLED |
| 2079 | limit -= selected_3d_nodes.size(); |
| 2080 | #endif // _3D_DISABLED |
| 2081 | if (ids.size() > limit) { |
| 2082 | ids.resize(limit); |
| 2083 | nodes.resize(limit); |
| 2084 | EngineDebugger::get_singleton()->send_message("show_selection_limit_warning", Array()); |
| 2085 | } |
| 2086 | |
| 2087 | for (ObjectID id : selected_ci_nodes) { |
| 2088 | ids.push_back(id); |
| 2089 | nodes.push_back(ObjectDB::get_instance<Node>(id)); |
| 2090 | } |
| 2091 | #ifndef _3D_DISABLED |
| 2092 | for (const KeyValue<ObjectID, Ref<SelectionBox3D>> &KV : selected_3d_nodes) { |
| 2093 | ids.push_back(KV.key); |
| 2094 | nodes.push_back(ObjectDB::get_instance<Node>(KV.key)); |
| 2095 | } |
| 2096 | #endif // _3D_DISABLED |
| 2097 | |
| 2098 | if (ids.is_empty()) { |
| 2099 | EngineDebugger::get_singleton()->send_message("remote_nothing_selected", message); |
| 2100 | } else { |
| 2101 | for (const ObjectID &id : ids) { |
| 2102 | SceneDebuggerObject obj(id); |
| 2103 | Array arr; |
| 2104 | obj.serialize(arr); |
| 2105 | message.append(arr); |
| 2106 | } |
| 2107 | |
| 2108 | EngineDebugger::get_singleton()->send_message("remote_objects_selected", message); |
| 2109 | } |
| 2110 | |
| 2111 | _set_selected_nodes(Vector<Node *>(nodes)); |
| 2112 | } |
| 2113 | |
| 2114 | void RuntimeNodeSelect::_set_selected_nodes(const Vector<Node *> &p_nodes) { |
| 2115 | if (p_nodes.is_empty()) { |
| 2116 | _clear_selection(); |
| 2117 | return; |
| 2118 | } |
| 2119 | |
| 2120 | bool changed = false; |
| 2121 | LocalVector<ObjectID> nodes_ci; |
| 2122 | #ifndef _3D_DISABLED |
| 2123 | HashMap<ObjectID, Ref<SelectionBox3D>> nodes_3d; |
| 2124 | #endif // _3D_DISABLED |
| 2125 | |
| 2126 | for (Node *node : p_nodes) { |
| 2127 | ObjectID id = node->get_instance_id(); |
| 2128 | if (Object::cast_to<CanvasItem>(node)) { |
| 2129 | if (!changed || !selected_ci_nodes.has(id)) { |
| 2130 | changed = true; |
| 2131 | } |
| 2132 | |
| 2133 | nodes_ci.push_back(id); |
| 2134 | } else { |
| 2135 | #ifndef _3D_DISABLED |
| 2136 | Node3D *node_3d = Object::cast_to<Node3D>(node); |
| 2137 | if (!node_3d || !node_3d->is_inside_world()) { |
| 2138 | continue; |
| 2139 | } |
| 2140 | |
| 2141 | if (!changed || !selected_3d_nodes.has(id)) { |
| 2142 | changed = true; |
| 2143 | } |
| 2144 | |
| 2145 | if (selected_3d_nodes.has(id)) { |
| 2146 | // Assign an already available visual instance. |
| 2147 | nodes_3d[id] = selected_3d_nodes.get(id); |
| 2148 | continue; |
| 2149 | } |
| 2150 | |
| 2151 | if (sbox_3d_mesh.is_null() || sbox_3d_mesh_xray.is_null()) { |
| 2152 | continue; |
| 2153 | } |
| 2154 | |
| 2155 | Ref<SelectionBox3D> sb; |
| 2156 | sb.instantiate(); |
| 2157 | nodes_3d[id] = sb; |
| 2158 | |
| 2159 | RID scenario = node_3d->get_world_3d()->get_scenario(); |
| 2160 | |
| 2161 | sb->instance = RS::get_singleton()->instance_create2(sbox_3d_mesh->get_rid(), scenario); |
| 2162 | sb->instance_ofs = RS::get_singleton()->instance_create2(sbox_3d_mesh->get_rid(), scenario); |
| 2163 | RS::get_singleton()->instance_geometry_set_cast_shadows_setting(sb->instance, RS::SHADOW_CASTING_SETTING_OFF); |
| 2164 | RS::get_singleton()->instance_geometry_set_cast_shadows_setting(sb->instance_ofs, RS::SHADOW_CASTING_SETTING_OFF); |
| 2165 | RS::get_singleton()->instance_geometry_set_flag(sb->instance, RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true); |
| 2166 | RS::get_singleton()->instance_geometry_set_flag(sb->instance, RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false); |
| 2167 | RS::get_singleton()->instance_geometry_set_flag(sb->instance_ofs, RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true); |
| 2168 | RS::get_singleton()->instance_geometry_set_flag(sb->instance_ofs, RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false); |
| 2169 | |
| 2170 | sb->instance_xray = RS::get_singleton()->instance_create2(sbox_3d_mesh_xray->get_rid(), scenario); |
| 2171 | sb->instance_xray_ofs = RS::get_singleton()->instance_create2(sbox_3d_mesh_xray->get_rid(), scenario); |
| 2172 | RS::get_singleton()->instance_geometry_set_cast_shadows_setting(sb->instance_xray, RS::SHADOW_CASTING_SETTING_OFF); |
| 2173 | RS::get_singleton()->instance_geometry_set_cast_shadows_setting(sb->instance_xray_ofs, RS::SHADOW_CASTING_SETTING_OFF); |
| 2174 | RS::get_singleton()->instance_geometry_set_flag(sb->instance_xray, RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true); |
| 2175 | RS::get_singleton()->instance_geometry_set_flag(sb->instance_xray, RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false); |
| 2176 | RS::get_singleton()->instance_geometry_set_flag(sb->instance_xray_ofs, RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true); |
| 2177 | RS::get_singleton()->instance_geometry_set_flag(sb->instance_xray_ofs, RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false); |
| 2178 | #endif // _3D_DISABLED |
| 2179 | } |
| 2180 | } |
| 2181 | |
| 2182 | #ifdef _3D_DISABLED |
| 2183 | if (!changed && nodes_ci.size() == selected_ci_nodes.size()) { |
| 2184 | return; |
| 2185 | } |
| 2186 | #else |
| 2187 | if (!changed && nodes_ci.size() == selected_ci_nodes.size() && nodes_3d.size() == selected_3d_nodes.size()) { |
| 2188 | return; |
| 2189 | } |
| 2190 | #endif // _3D_DISABLED |
| 2191 | |
| 2192 | _clear_selection(); |
| 2193 | selected_ci_nodes = nodes_ci; |
| 2194 | has_selection = !nodes_ci.is_empty(); |
| 2195 | |
| 2196 | #ifndef _3D_DISABLED |
| 2197 | if (!nodes_3d.is_empty()) { |
| 2198 | selected_3d_nodes = nodes_3d; |
| 2199 | has_selection = true; |
| 2200 | } |
| 2201 | #endif // _3D_DISABLED |
| 2202 | |
| 2203 | _queue_selection_update(); |
| 2204 | } |
| 2205 | |
| 2206 | void RuntimeNodeSelect::_queue_selection_update() { |
| 2207 | if (has_selection && selection_visible) { |
| 2208 | if (SceneTree::get_singleton()->is_suspended()) { |
| 2209 | _update_selection(); |
| 2210 | } else { |
| 2211 | selection_update_queued = true; |
| 2212 | } |
| 2213 | } |
| 2214 | } |
| 2215 | |
| 2216 | void RuntimeNodeSelect::_update_selection() { |
| 2217 | RS::get_singleton()->canvas_item_clear(sbox_2d_ci); |
| 2218 | RS::get_singleton()->canvas_item_set_visible(sbox_2d_ci, selection_visible); |
| 2219 | |
| 2220 | for (LocalVector<ObjectID>::Iterator E = selected_ci_nodes.begin(); E != selected_ci_nodes.end(); ++E) { |
| 2221 | ObjectID id = *E; |
| 2222 | CanvasItem *ci = ObjectDB::get_instance<CanvasItem>(id); |
| 2223 | if (!ci) { |
| 2224 | selected_ci_nodes.erase(id); |
| 2225 | --E; |
| 2226 | continue; |
| 2227 | } |
| 2228 | |
| 2229 | if (!ci->is_inside_tree()) { |
| 2230 | continue; |
| 2231 | } |
| 2232 | |
| 2233 | Transform2D xform = ci->get_global_transform_with_canvas(); |
| 2234 | |
| 2235 | // Fallback. |
| 2236 | Rect2 rect = Rect2(Vector2(), Vector2(10, 10)); |
| 2237 | |
| 2238 | if (ci->_edit_use_rect()) { |
| 2239 | rect = ci->_edit_get_rect(); |
| 2240 | } else { |
| 2241 | #ifndef PHYSICS_2D_DISABLED |
| 2242 | CollisionShape2D *collision_shape = Object::cast_to<CollisionShape2D>(ci); |
| 2243 | if (collision_shape) { |
| 2244 | Ref<Shape2D> shape = collision_shape->get_shape(); |
| 2245 | if (shape.is_valid()) { |
| 2246 | rect = shape->get_rect(); |
| 2247 | } |
| 2248 | } |
| 2249 | #endif // PHYSICS_2D_DISABLED |
| 2250 | } |
| 2251 | |
| 2252 | const Vector2 endpoints[4] = { |
| 2253 | xform.xform(rect.position), |
| 2254 | xform.xform(rect.position + Point2(rect.size.x, 0)), |
| 2255 | xform.xform(rect.position + rect.size), |
| 2256 | xform.xform(rect.position + Point2(0, rect.size.y)) |
| 2257 | }; |
| 2258 | |
| 2259 | const Color selection_color_2d = Color(1, 0.6, 0.4, 0.7); |
| 2260 | for (int i = 0; i < 4; i++) { |
| 2261 | RS::get_singleton()->canvas_item_add_line(sbox_2d_ci, endpoints[i], endpoints[(i + 1) % 4], selection_color_2d, sel_2d_scale); |
| 2262 | } |
| 2263 | } |
| 2264 | |
| 2265 | #ifndef _3D_DISABLED |
| 2266 | for (HashMap<ObjectID, Ref<SelectionBox3D>>::ConstIterator KV = selected_3d_nodes.begin(); KV != selected_3d_nodes.end(); ++KV) { |
| 2267 | ObjectID id = KV->key; |
| 2268 | Node3D *node_3d = ObjectDB::get_instance<Node3D>(id); |
| 2269 | if (!node_3d) { |
| 2270 | selected_3d_nodes.erase(id); |
| 2271 | --KV; |
| 2272 | continue; |
| 2273 | } |
| 2274 | |
| 2275 | if (!node_3d->is_inside_tree()) { |
| 2276 | continue; |
| 2277 | } |
| 2278 | |
| 2279 | // Fallback. |
| 2280 | AABB bounds(Vector3(-0.5, -0.5, -0.5), Vector3(1, 1, 1)); |
| 2281 | |
| 2282 | VisualInstance3D *visual_instance = Object::cast_to<VisualInstance3D>(node_3d); |
| 2283 | if (visual_instance) { |
| 2284 | bounds = visual_instance->get_aabb(); |
| 2285 | } else { |
| 2286 | #ifndef PHYSICS_3D_DISABLED |
| 2287 | CollisionShape3D *collision_shape = Object::cast_to<CollisionShape3D>(node_3d); |
| 2288 | if (collision_shape) { |
| 2289 | Ref<Shape3D> shape = collision_shape->get_shape(); |
| 2290 | if (shape.is_valid()) { |
| 2291 | bounds = shape->get_debug_mesh()->get_aabb(); |
| 2292 | } |
| 2293 | } |
| 2294 | #endif // PHYSICS_3D_DISABLED |
| 2295 | } |
| 2296 | |
| 2297 | Transform3D xform_to_top_level_parent_space = node_3d->get_global_transform().affine_inverse() * node_3d->get_global_transform(); |
| 2298 | bounds = xform_to_top_level_parent_space.xform(bounds); |
| 2299 | Transform3D t = node_3d->get_global_transform(); |
| 2300 | |
| 2301 | Ref<SelectionBox3D> sb = KV->value; |
| 2302 | if (t == sb->transform && bounds == sb->bounds) { |
| 2303 | continue; // Nothing changed. |
| 2304 | } |
| 2305 | sb->transform = t; |
| 2306 | sb->bounds = bounds; |
| 2307 | |
| 2308 | Transform3D t_offset = t; |
| 2309 | |
| 2310 | // Apply AABB scaling before item's global transform. |
| 2311 | { |
| 2312 | const Vector3 offset(0.005, 0.005, 0.005); |
| 2313 | Basis aabb_s; |
| 2314 | aabb_s.scale(bounds.size + offset); |
| 2315 | t.translate_local(bounds.position - offset / 2); |
| 2316 | t.basis = t.basis * aabb_s; |
| 2317 | } |
| 2318 | { |
| 2319 | const Vector3 offset(0.01, 0.01, 0.01); |
| 2320 | Basis aabb_s; |
| 2321 | aabb_s.scale(bounds.size + offset); |
| 2322 | t_offset.translate_local(bounds.position - offset / 2); |
| 2323 | t_offset.basis = t_offset.basis * aabb_s; |
| 2324 | } |
| 2325 | |
| 2326 | RS::get_singleton()->instance_set_visible(sb->instance, selection_visible); |
| 2327 | RS::get_singleton()->instance_set_visible(sb->instance_ofs, selection_visible); |
| 2328 | RS::get_singleton()->instance_set_visible(sb->instance_xray, selection_visible); |
| 2329 | RS::get_singleton()->instance_set_visible(sb->instance_xray_ofs, selection_visible); |
| 2330 | |
| 2331 | RS::get_singleton()->instance_set_transform(sb->instance, t); |
| 2332 | RS::get_singleton()->instance_set_transform(sb->instance_ofs, t_offset); |
| 2333 | RS::get_singleton()->instance_set_transform(sb->instance_xray, t); |
| 2334 | RS::get_singleton()->instance_set_transform(sb->instance_xray_ofs, t_offset); |
| 2335 | } |
| 2336 | #endif // _3D_DISABLED |
| 2337 | } |
| 2338 | |
| 2339 | void RuntimeNodeSelect::_clear_selection() { |
| 2340 | selected_ci_nodes.clear(); |
| 2341 | if (draw_canvas.is_valid()) { |
| 2342 | RS::get_singleton()->canvas_item_clear(sbox_2d_ci); |
| 2343 | } |
| 2344 | |
| 2345 | #ifndef _3D_DISABLED |
| 2346 | selected_3d_nodes.clear(); |
| 2347 | #endif // _3D_DISABLED |
| 2348 | |
| 2349 | has_selection = false; |
| 2350 | } |
| 2351 | |
| 2352 | void RuntimeNodeSelect::_update_selection_drag(const Point2 &p_end_pos) { |
| 2353 | RS::get_singleton()->canvas_item_clear(sel_drag_ci); |
| 2354 | |
| 2355 | if (selection_drag_state != SELECTION_DRAG_MOVE) { |
| 2356 | return; |
| 2357 | } |
| 2358 | |
| 2359 | selection_drag_area.size = p_end_pos - selection_drag_area.position; |
| 2360 | |
| 2361 | if (selection_drag_state == SELECTION_DRAG_END) { |
| 2362 | return; |
| 2363 | } |
| 2364 | |
| 2365 | Rect2 selection_drawing = selection_drag_area.abs(); |
| 2366 | int thickness = 1; |
| 2367 | |
| 2368 | const Vector2 endpoints[4] = { |
| 2369 | selection_drawing.position, |
| 2370 | selection_drawing.position + Point2(selection_drawing.size.x, 0), |
| 2371 | selection_drawing.position + selection_drawing.size, |
| 2372 | selection_drawing.position + Point2(0, selection_drawing.size.y) |
| 2373 | }; |
| 2374 | |
| 2375 | // Draw fill. |
| 2376 | RS::get_singleton()->canvas_item_add_rect(sel_drag_ci, selection_drawing, selection_area_fill); |
| 2377 | // Draw outline. |
| 2378 | for (int i = 0; i < 4; i++) { |
| 2379 | RS::get_singleton()->canvas_item_add_line(sel_drag_ci, endpoints[i], endpoints[(i + 1) % 4], selection_area_outline, thickness); |
| 2380 | } |
| 2381 | } |
| 2382 | |
| 2383 | void RuntimeNodeSelect::_open_selection_list(const Vector<SelectResult> &p_items, const Point2 &p_pos) { |
| 2384 | Window *root = SceneTree::get_singleton()->get_root(); |
| 2385 | |
| 2386 | selection_list = memnew(PopupMenu); |
| 2387 | selection_list->set_theme(ThemeDB::get_singleton()->get_default_theme()); |
| 2388 | selection_list->set_auto_translate_mode(Node::AUTO_TRANSLATE_MODE_DISABLED); |
| 2389 | selection_list->set_force_native(true); |
| 2390 | selection_list->connect("index_pressed", callable_mp(this, &RuntimeNodeSelect::_items_popup_index_pressed).bind(selection_list)); |
| 2391 | selection_list->connect("popup_hide", callable_mp(this, &RuntimeNodeSelect::_close_selection_list)); |
| 2392 | |
| 2393 | root->add_child(selection_list); |
| 2394 | |
| 2395 | for (const SelectResult &I : p_items) { |
| 2396 | int locked = 0; |
| 2397 | if (I.item->get_meta("_edit_lock_", false)) { |
| 2398 | locked = 1; |
| 2399 | } else { |
| 2400 | Node *scene = SceneTree::get_singleton()->get_root(); |
| 2401 | Node *node = I.item; |
| 2402 | |
| 2403 | while (node && node != scene->get_parent()) { |
| 2404 | if (node->has_meta("_edit_group_")) { |
| 2405 | locked = 2; |
| 2406 | } |
| 2407 | node = node->get_parent(); |
| 2408 | } |
| 2409 | } |
| 2410 | |
| 2411 | String suffix; |
| 2412 | if (locked == 1) { |
| 2413 | suffix = " (" + RTR("Locked") + ")"; |
| 2414 | } else if (locked == 2) { |
| 2415 | suffix = " (" + RTR("Grouped") + ")"; |
| 2416 | } |
| 2417 | |
| 2418 | selection_list->add_item((String)I.item->get_name() + suffix); |
| 2419 | selection_list->set_item_metadata(-1, I.item); |
| 2420 | } |
| 2421 | |
| 2422 | selection_list->set_position(selection_list->is_embedded() ? p_pos : (Input::get_singleton()->get_mouse_position() + root->get_position())); |
| 2423 | selection_list->reset_size(); |
| 2424 | selection_list->popup(); |
| 2425 | |
| 2426 | selection_list->set_content_scale_factor(1); |
| 2427 | selection_list->set_min_size(selection_list->get_contents_minimum_size()); |
| 2428 | selection_list->reset_size(); |
| 2429 | |
| 2430 | // FIXME: Ugly hack that stops the popup from hiding when the button is released. |
| 2431 | selection_list->call_deferred(SNAME("set_position"), selection_list->get_position() + Point2(1, 0)); |
| 2432 | } |
| 2433 | |
| 2434 | void RuntimeNodeSelect::_close_selection_list() { |
| 2435 | selection_list->queue_free(); |
| 2436 | selection_list = nullptr; |
| 2437 | } |
| 2438 | |
| 2439 | void RuntimeNodeSelect::_set_selection_visible(bool p_visible) { |
| 2440 | selection_visible = p_visible; |
| 2441 | |
| 2442 | if (has_selection) { |
| 2443 | _update_selection(); |
| 2444 | } |
| 2445 | } |
| 2446 | |
| 2447 | void RuntimeNodeSelect::_set_avoid_locked(bool p_enabled) { |
| 2448 | avoid_locked_nodes = p_enabled; |
| 2449 | } |
| 2450 | |
| 2451 | void RuntimeNodeSelect::_set_prefer_group(bool p_enabled) { |
| 2452 | prefer_group_selection = p_enabled; |
| 2453 | } |
| 2454 | |
| 2455 | // Copied and trimmed from the CanvasItemEditor implementation. |
| 2456 | 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) { |
| 2457 | if (!p_node || Object::cast_to<Viewport>(p_node)) { |
| 2458 | return; |
| 2459 | } |
| 2460 | |
| 2461 | CanvasItem *ci = Object::cast_to<CanvasItem>(p_node); |
| 2462 | for (int i = p_node->get_child_count() - 1; i >= 0; i--) { |
| 2463 | if (ci) { |
| 2464 | if (!ci->is_set_as_top_level()) { |
| 2465 | _find_canvas_items_at_pos(p_pos, p_node->get_child(i), r_items, p_parent_xform * ci->get_transform(), p_canvas_xform); |
| 2466 | } else { |
| 2467 | _find_canvas_items_at_pos(p_pos, p_node->get_child(i), r_items, ci->get_transform(), p_canvas_xform); |
| 2468 | } |
| 2469 | } else { |
| 2470 | CanvasLayer *cl = Object::cast_to<CanvasLayer>(p_node); |
| 2471 | _find_canvas_items_at_pos(p_pos, p_node->get_child(i), r_items, Transform2D(), cl ? cl->get_transform() : p_canvas_xform); |
| 2472 | } |
| 2473 | } |
| 2474 | |
| 2475 | if (!ci || !ci->is_visible_in_tree()) { |
| 2476 | return; |
| 2477 | } |
| 2478 | |
| 2479 | Transform2D xform = p_canvas_xform; |
| 2480 | if (!ci->is_set_as_top_level()) { |
| 2481 | xform *= p_parent_xform; |
| 2482 | } |
| 2483 | |
| 2484 | Window *root = SceneTree::get_singleton()->get_root(); |
| 2485 | Point2 pos; |
| 2486 | |
| 2487 | // Cameras don't affect `CanvasLayer`s. |
| 2488 | if (!ci->get_canvas_layer_node() || ci->get_canvas_layer_node()->is_following_viewport()) { |
| 2489 | pos = root->get_canvas_transform().affine_inverse().xform(p_pos); |
| 2490 | } else { |
| 2491 | pos = p_pos; |
| 2492 | } |
| 2493 | |
| 2494 | xform = (xform * ci->get_transform()).affine_inverse(); |
| 2495 | const real_t local_grab_distance = xform.basis_xform(Vector2(sel_2d_grab_dist, 0)).length() / view_2d_zoom; |
| 2496 | if (ci->_edit_is_selected_on_click(xform.xform(pos), local_grab_distance)) { |
| 2497 | SelectResult res; |
| 2498 | res.item = ci; |
| 2499 | res.order = ci->get_effective_z_index() + ci->get_canvas_layer(); |
| 2500 | r_items.push_back(res); |
| 2501 | |
| 2502 | #ifndef PHYSICS_2D_DISABLED |
| 2503 | // If it's a shape, get the collision object it's from. |
| 2504 | // FIXME: If the collision object has multiple shapes, only the topmost will be above it in the list. |
| 2505 | if (Object::cast_to<CollisionShape2D>(ci) || Object::cast_to<CollisionPolygon2D>(ci)) { |
| 2506 | CollisionObject2D *collision_object = Object::cast_to<CollisionObject2D>(ci->get_parent()); |
| 2507 | if (collision_object) { |
| 2508 | SelectResult res_col; |
| 2509 | res_col.item = ci->get_parent(); |
| 2510 | res_col.order = collision_object->get_z_index() + ci->get_canvas_layer(); |
| 2511 | r_items.push_back(res_col); |
| 2512 | } |
| 2513 | } |
| 2514 | #endif // PHYSICS_2D_DISABLED |
| 2515 | } |
| 2516 | } |
| 2517 | |
| 2518 | // Copied and trimmed from the CanvasItemEditor implementation. |
| 2519 | 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) { |
| 2520 | if (!p_node || Object::cast_to<Viewport>(p_node)) { |
| 2521 | return; |
| 2522 | } |
| 2523 | |
| 2524 | CanvasItem *ci = Object::cast_to<CanvasItem>(p_node); |
| 2525 | for (int i = p_node->get_child_count() - 1; i >= 0; i--) { |
| 2526 | if (ci) { |
| 2527 | if (!ci->is_set_as_top_level()) { |
| 2528 | _find_canvas_items_at_rect(p_rect, p_node->get_child(i), r_items, p_parent_xform * ci->get_transform(), p_canvas_xform); |
| 2529 | } else { |
| 2530 | _find_canvas_items_at_rect(p_rect, p_node->get_child(i), r_items, ci->get_transform(), p_canvas_xform); |
| 2531 | } |
| 2532 | } else { |
| 2533 | CanvasLayer *cl = Object::cast_to<CanvasLayer>(p_node); |
| 2534 | _find_canvas_items_at_rect(p_rect, p_node->get_child(i), r_items, Transform2D(), cl ? cl->get_transform() : p_canvas_xform); |
| 2535 | } |
| 2536 | } |
| 2537 | |
| 2538 | if (!ci || !ci->is_visible_in_tree()) { |
| 2539 | return; |
| 2540 | } |
| 2541 | |
| 2542 | Transform2D xform = p_canvas_xform; |
| 2543 | if (!ci->is_set_as_top_level()) { |
| 2544 | xform *= p_parent_xform; |
| 2545 | } |
| 2546 | |
| 2547 | Window *root = SceneTree::get_singleton()->get_root(); |
| 2548 | Rect2 rect; |
| 2549 | // Cameras don't affect `CanvasLayer`s. |
| 2550 | if (!ci->get_canvas_layer_node() || ci->get_canvas_layer_node()->is_following_viewport()) { |
| 2551 | rect = root->get_canvas_transform().affine_inverse().xform(p_rect); |
| 2552 | } else { |
| 2553 | rect = p_rect; |
| 2554 | } |
| 2555 | rect = (xform * ci->get_transform()).affine_inverse().xform(rect); |
| 2556 | |
| 2557 | bool selected = false; |
| 2558 | if (ci->_edit_use_rect()) { |
| 2559 | Rect2 ci_rect = ci->_edit_get_rect(); |
| 2560 | if (rect.has_point(ci_rect.position) && |
| 2561 | rect.has_point(ci_rect.position + Vector2(ci_rect.size.x, 0)) && |
| 2562 | rect.has_point(ci_rect.position + Vector2(ci_rect.size.x, ci_rect.size.y)) && |
| 2563 | rect.has_point(ci_rect.position + Vector2(0, ci_rect.size.y))) { |
| 2564 | selected = true; |
| 2565 | } |
| 2566 | } else if (rect.has_point(Point2())) { |
| 2567 | selected = true; |
| 2568 | } |
| 2569 | |
| 2570 | if (selected) { |
| 2571 | SelectResult res; |
| 2572 | res.item = ci; |
| 2573 | res.order = ci->get_effective_z_index() + ci->get_canvas_layer(); |
| 2574 | r_items.push_back(res); |
| 2575 | } |
| 2576 | } |
| 2577 | |
| 2578 | void RuntimeNodeSelect::_pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event) { |
| 2579 | Vector2 scroll = SceneTree::get_singleton()->get_root()->get_screen_transform().affine_inverse().xform(p_scroll_vec); |
| 2580 | view_2d_offset.x -= scroll.x / view_2d_zoom; |
| 2581 | view_2d_offset.y -= scroll.y / view_2d_zoom; |
| 2582 | |
| 2583 | _update_view_2d(); |
| 2584 | } |
| 2585 | |
| 2586 | // A very shallow copy of the same function inside CanvasItemEditor. |
| 2587 | void RuntimeNodeSelect::_zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event) { |
| 2588 | real_t prev_zoom = view_2d_zoom; |
| 2589 | view_2d_zoom = CLAMP(view_2d_zoom * p_zoom_factor, VIEW_2D_MIN_ZOOM, VIEW_2D_MAX_ZOOM); |
| 2590 | |
| 2591 | Vector2 pos = SceneTree::get_singleton()->get_root()->get_screen_transform().affine_inverse().xform(p_origin); |
| 2592 | view_2d_offset += pos / prev_zoom - pos / view_2d_zoom; |
| 2593 | |
| 2594 | // We want to align in-scene pixels to screen pixels, this prevents blurry rendering |
| 2595 | // of small details (texts, lines). |
| 2596 | // This correction adds a jitter movement when zooming, so we correct only when the |
| 2597 | // zoom factor is an integer. (in the other cases, all pixels won't be aligned anyway) |
| 2598 | const real_t closest_zoom_factor = Math::round(view_2d_zoom); |
| 2599 | if (Math::is_zero_approx(view_2d_zoom - closest_zoom_factor)) { |
| 2600 | // Make sure scene pixel at view_offset is aligned on a screen pixel. |
| 2601 | Vector2 view_offset_int = view_2d_offset.floor(); |
| 2602 | Vector2 view_offset_frac = view_2d_offset - view_offset_int; |
| 2603 | view_2d_offset = view_offset_int + (view_offset_frac * closest_zoom_factor).round() / closest_zoom_factor; |
| 2604 | } |
| 2605 | |
| 2606 | _update_view_2d(); |
| 2607 | } |
| 2608 | |
| 2609 | void RuntimeNodeSelect::_reset_camera_2d() { |
| 2610 | camera_first_override = true; |
| 2611 | Window *root = SceneTree::get_singleton()->get_root(); |
| 2612 | Camera2D *game_camera = root->is_camera_2d_override_enabled() ? root->get_overridden_camera_2d() : root->get_camera_2d(); |
| 2613 | if (game_camera) { |
| 2614 | // Ideally we should be using Camera2D::get_camera_transform() but it's not so this hack will have to do for now. |
| 2615 | view_2d_offset = game_camera->get_camera_screen_center() - (0.5 * root->get_visible_rect().size); |
| 2616 | } else { |
| 2617 | view_2d_offset = Vector2(); |
| 2618 | } |
| 2619 | |
| 2620 | view_2d_zoom = 1; |
| 2621 | |
| 2622 | if (root->is_camera_2d_override_enabled()) { |
| 2623 | _update_view_2d(); |
| 2624 | } |
| 2625 | } |
| 2626 | |
| 2627 | void RuntimeNodeSelect::_update_view_2d() { |
| 2628 | Window *root = SceneTree::get_singleton()->get_root(); |
| 2629 | ERR_FAIL_COND(!root->is_camera_2d_override_enabled()); |
| 2630 | |
| 2631 | Camera2D *override_camera = root->get_override_camera_2d(); |
| 2632 | override_camera->set_anchor_mode(Camera2D::ANCHOR_MODE_FIXED_TOP_LEFT); |
| 2633 | override_camera->set_zoom(Vector2(view_2d_zoom, view_2d_zoom)); |
| 2634 | override_camera->set_offset(view_2d_offset); |
| 2635 | |
| 2636 | _queue_selection_update(); |
| 2637 | } |
| 2638 | |
| 2639 | #ifndef _3D_DISABLED |
| 2640 | void RuntimeNodeSelect::_find_3d_items_at_pos(const Point2 &p_pos, Vector<SelectResult> &r_items) { |
| 2641 | Window *root = SceneTree::get_singleton()->get_root(); |
| 2642 | |
| 2643 | Vector3 ray, pos, to; |
| 2644 | Camera3D *camera = root->get_camera_3d(); |
| 2645 | if (!camera) { |
| 2646 | return; |
| 2647 | } |
| 2648 | |
| 2649 | ray = camera->project_ray_normal(p_pos); |
| 2650 | pos = camera->project_ray_origin(p_pos); |
| 2651 | to = pos + ray * camera->get_far(); |
| 2652 | |
| 2653 | #ifndef PHYSICS_3D_DISABLED |
| 2654 | // Start with physical objects. |
| 2655 | PhysicsDirectSpaceState3D *ss = root->get_world_3d()->get_direct_space_state(); |
| 2656 | PhysicsDirectSpaceState3D::RayResult result; |
| 2657 | HashSet<RID> excluded; |
| 2658 | PhysicsDirectSpaceState3D::RayParameters ray_params; |
| 2659 | ray_params.from = pos; |
| 2660 | ray_params.to = to; |
| 2661 | ray_params.collide_with_areas = true; |
| 2662 | while (true) { |
| 2663 | ray_params.exclude = excluded; |
| 2664 | if (ss->intersect_ray(ray_params, result)) { |
| 2665 | SelectResult res; |
| 2666 | res.item = Object::cast_to<Node>(result.collider); |
| 2667 | res.order = -pos.distance_to(result.position); |
| 2668 | |
| 2669 | // Fetch collision shapes. |
| 2670 | CollisionObject3D *collision = Object::cast_to<CollisionObject3D>(result.collider); |
| 2671 | if (collision) { |
| 2672 | List<uint32_t> owners; |
| 2673 | collision->get_shape_owners(&owners); |
| 2674 | for (uint32_t &I : owners) { |
| 2675 | SelectResult res_shape; |
| 2676 | res_shape.item = Object::cast_to<Node>(collision->shape_owner_get_owner(I)); |
| 2677 | res_shape.order = res.order; |
| 2678 | r_items.push_back(res_shape); |
| 2679 | } |
| 2680 | } |
| 2681 | |
| 2682 | r_items.push_back(res); |
| 2683 | |
| 2684 | excluded.insert(result.rid); |
| 2685 | } else { |
| 2686 | break; |
| 2687 | } |
| 2688 | } |
| 2689 | #endif // PHYSICS_3D_DISABLED |
| 2690 | |
| 2691 | // Then go for the meshes. |
| 2692 | Vector<ObjectID> items = RS::get_singleton()->instances_cull_ray(pos, to, root->get_world_3d()->get_scenario()); |
| 2693 | for (int i = 0; i < items.size(); i++) { |
| 2694 | Object *obj = ObjectDB::get_instance(items[i]); |
| 2695 | |
| 2696 | GeometryInstance3D *geo_instance = Object::cast_to<GeometryInstance3D>(obj); |
| 2697 | if (geo_instance) { |
| 2698 | Ref<TriangleMesh> mesh_collision = geo_instance->generate_triangle_mesh(); |
| 2699 | |
| 2700 | if (mesh_collision.is_valid()) { |
| 2701 | Transform3D gt = geo_instance->get_global_transform(); |
| 2702 | Transform3D ai = gt.affine_inverse(); |
| 2703 | Vector3 point, normal; |
| 2704 | if (mesh_collision->intersect_ray(ai.xform(pos), ai.basis.xform(ray).normalized(), point, normal)) { |
| 2705 | SelectResult res; |
| 2706 | res.item = Object::cast_to<Node>(obj); |
| 2707 | res.order = -pos.distance_to(gt.xform(point)); |
| 2708 | r_items.push_back(res); |
| 2709 | |
| 2710 | continue; |
| 2711 | } |
| 2712 | } |
| 2713 | } |
| 2714 | |
| 2715 | items.remove_at(i); |
| 2716 | i--; |
| 2717 | } |
| 2718 | } |
| 2719 | |
| 2720 | void RuntimeNodeSelect::_find_3d_items_at_rect(const Rect2 &p_rect, Vector<SelectResult> &r_items) { |
| 2721 | Window *root = SceneTree::get_singleton()->get_root(); |
| 2722 | Camera3D *camera = root->get_camera_3d(); |
| 2723 | if (!camera) { |
| 2724 | return; |
| 2725 | } |
| 2726 | |
| 2727 | Vector3 cam_pos = camera->get_global_position(); |
| 2728 | Vector3 dist_pos = camera->project_ray_origin(p_rect.position + p_rect.size / 2); |
| 2729 | |
| 2730 | real_t znear = camera->get_near(); |
| 2731 | real_t zfar = camera->get_far(); |
| 2732 | real_t zofs = MAX(0.0, 5.0 - znear); |
| 2733 | |
| 2734 | const Point2 pos_end = p_rect.position + p_rect.size; |
| 2735 | Vector3 box[4] = { |
| 2736 | Vector3( |
| 2737 | MIN(p_rect.position.x, pos_end.x), |
| 2738 | MIN(p_rect.position.y, pos_end.y), |
| 2739 | zofs), |
| 2740 | Vector3( |
| 2741 | MAX(p_rect.position.x, pos_end.x), |
| 2742 | MIN(p_rect.position.y, pos_end.y), |
| 2743 | zofs), |
| 2744 | Vector3( |
| 2745 | MAX(p_rect.position.x, pos_end.x), |
| 2746 | MAX(p_rect.position.y, pos_end.y), |
| 2747 | zofs), |
| 2748 | Vector3( |
| 2749 | MIN(p_rect.position.x, pos_end.x), |
| 2750 | MAX(p_rect.position.y, pos_end.y), |
| 2751 | zofs) |
| 2752 | }; |
| 2753 | |
| 2754 | Vector<Plane> frustum; |
| 2755 | for (int i = 0; i < 4; i++) { |
| 2756 | Vector3 a = _get_screen_to_space(box[i]); |
| 2757 | Vector3 b = _get_screen_to_space(box[(i + 1) % 4]); |
| 2758 | frustum.push_back(Plane(a, b, cam_pos)); |
| 2759 | } |
| 2760 | |
| 2761 | // Get the camera normal. |
| 2762 | Plane near_plane = Plane(camera->get_global_transform().basis.get_column(2), cam_pos); |
| 2763 | |
| 2764 | near_plane.d -= znear; |
| 2765 | frustum.push_back(near_plane); |
| 2766 | |
| 2767 | Plane far_plane = -near_plane; |
| 2768 | far_plane.d += zfar; |
| 2769 | frustum.push_back(far_plane); |
| 2770 | |
| 2771 | // Keep track of the currently listed nodes, so repeats can be ignored. |
| 2772 | HashSet<Node *> node_list; |
| 2773 | |
| 2774 | #ifndef PHYSICS_3D_DISABLED |
| 2775 | Vector<Vector3> points = Geometry3D::compute_convex_mesh_points(&frustum[0], frustum.size()); |
| 2776 | Ref<ConvexPolygonShape3D> shape; |
| 2777 | shape.instantiate(); |
| 2778 | shape->set_points(points); |
| 2779 | |
| 2780 | // Start with physical objects. |
| 2781 | PhysicsDirectSpaceState3D *ss = root->get_world_3d()->get_direct_space_state(); |
| 2782 | PhysicsDirectSpaceState3D::ShapeResult results[32]; |
| 2783 | PhysicsDirectSpaceState3D::ShapeParameters shape_params; |
| 2784 | shape_params.shape_rid = shape->get_rid(); |
| 2785 | shape_params.collide_with_areas = true; |
| 2786 | const int num_hits = ss->intersect_shape(shape_params, results, 32); |
| 2787 | for (int i = 0; i < num_hits; i++) { |
| 2788 | const PhysicsDirectSpaceState3D::ShapeResult &result = results[i]; |
| 2789 | SelectResult res; |
| 2790 | res.item = Object::cast_to<Node>(result.collider); |
| 2791 | res.order = -dist_pos.distance_to(Object::cast_to<Node3D>(res.item)->get_global_transform().origin); |
| 2792 | |
| 2793 | // Fetch collision shapes. |
| 2794 | CollisionObject3D *collision = Object::cast_to<CollisionObject3D>(result.collider); |
| 2795 | if (collision) { |
| 2796 | List<uint32_t> owners; |
| 2797 | collision->get_shape_owners(&owners); |
| 2798 | for (uint32_t &I : owners) { |
| 2799 | SelectResult res_shape; |
| 2800 | res_shape.item = Object::cast_to<Node>(collision->shape_owner_get_owner(I)); |
| 2801 | if (!node_list.has(res_shape.item)) { |
| 2802 | node_list.insert(res_shape.item); |
| 2803 | res_shape.order = res.order; |
| 2804 | r_items.push_back(res_shape); |
| 2805 | } |
| 2806 | } |
| 2807 | } |
| 2808 | |
| 2809 | if (!node_list.has(res.item)) { |
| 2810 | node_list.insert(res.item); |
| 2811 | r_items.push_back(res); |
| 2812 | } |
| 2813 | } |
| 2814 | #endif // PHYSICS_3D_DISABLED |
| 2815 | |
| 2816 | // Then go for the meshes. |
| 2817 | Vector<ObjectID> items = RS::get_singleton()->instances_cull_convex(frustum, root->get_world_3d()->get_scenario()); |
| 2818 | for (int i = 0; i < items.size(); i++) { |
| 2819 | Object *obj = ObjectDB::get_instance(items[i]); |
| 2820 | GeometryInstance3D *geo_instance = Object::cast_to<GeometryInstance3D>(obj); |
| 2821 | if (geo_instance) { |
| 2822 | Ref<TriangleMesh> mesh_collision = geo_instance->generate_triangle_mesh(); |
| 2823 | |
| 2824 | if (mesh_collision.is_valid()) { |
| 2825 | Transform3D gt = geo_instance->get_global_transform(); |
| 2826 | Vector3 mesh_scale = gt.get_basis().get_scale(); |
| 2827 | gt.orthonormalize(); |
| 2828 | |
| 2829 | Transform3D it = gt.affine_inverse(); |
| 2830 | |
| 2831 | Vector<Plane> transformed_frustum; |
| 2832 | int plane_count = frustum.size(); |
| 2833 | transformed_frustum.resize(plane_count); |
| 2834 | |
| 2835 | for (int j = 0; j < plane_count; j++) { |
| 2836 | transformed_frustum.write[j] = it.xform(frustum[j]); |
| 2837 | } |
| 2838 | Vector<Vector3> convex_points = Geometry3D::compute_convex_mesh_points(transformed_frustum.ptr(), plane_count); |
| 2839 | if (mesh_collision->inside_convex_shape(transformed_frustum.ptr(), transformed_frustum.size(), convex_points.ptr(), convex_points.size(), mesh_scale)) { |
| 2840 | SelectResult res; |
| 2841 | res.item = Object::cast_to<Node>(obj); |
| 2842 | if (!node_list.has(res.item)) { |
| 2843 | node_list.insert(res.item); |
| 2844 | res.order = -dist_pos.distance_to(gt.origin); |
| 2845 | r_items.push_back(res); |
| 2846 | } |
| 2847 | |
| 2848 | continue; |
| 2849 | } |
| 2850 | } |
| 2851 | } |
| 2852 | |
| 2853 | items.remove_at(i); |
| 2854 | i--; |
| 2855 | } |
| 2856 | } |
| 2857 | |
| 2858 | Vector3 RuntimeNodeSelect::_get_screen_to_space(const Vector3 &p_vector3) { |
| 2859 | Window *root = SceneTree::get_singleton()->get_root(); |
| 2860 | Camera3D *camera = root->get_camera_3d(); |
| 2861 | |
| 2862 | Transform3D camera_transform = camera->get_camera_transform(); |
| 2863 | Size2 size = root->get_size(); |
| 2864 | real_t znear = camera->get_near(); |
| 2865 | Projection cm = Projection::create_perspective(camera->get_fov(), size.aspect(), znear + p_vector3.z, camera->get_far()); |
| 2866 | Vector2 screen_he = cm.get_viewport_half_extents(); |
| 2867 | 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))); |
| 2868 | } |
| 2869 | |
| 2870 | bool RuntimeNodeSelect::_handle_3d_input(const Ref<InputEvent> &p_event) { |
| 2871 | Ref<InputEventMouseButton> b = p_event; |
| 2872 | if (b.is_valid()) { |
| 2873 | const real_t zoom_factor = 1.08 * b->get_factor(); |
| 2874 | switch (b->get_button_index()) { |
| 2875 | case MouseButton::WHEEL_UP: { |
| 2876 | if (!camera_freelook) { |
| 2877 | _cursor_scale_distance(1.0 / zoom_factor); |
| 2878 | } else { |
| 2879 | _scale_freelook_speed(zoom_factor); |
| 2880 | } |
| 2881 | |
| 2882 | return true; |
| 2883 | } break; |
| 2884 | case MouseButton::WHEEL_DOWN: { |
| 2885 | if (!camera_freelook) { |
| 2886 | _cursor_scale_distance(zoom_factor); |
| 2887 | } else { |
| 2888 | _scale_freelook_speed(1.0 / zoom_factor); |
| 2889 | } |
| 2890 | |
| 2891 | return true; |
| 2892 | } break; |
| 2893 | case MouseButton::RIGHT: { |
| 2894 | _set_camera_freelook_enabled(b->is_pressed()); |
| 2895 | return true; |
| 2896 | } break; |
| 2897 | default: { |
| 2898 | } |
| 2899 | } |
| 2900 | } |
| 2901 | |
| 2902 | Ref<InputEventMouseMotion> m = p_event; |
| 2903 | if (m.is_valid()) { |
| 2904 | if (camera_freelook) { |
| 2905 | _cursor_look(m); |
| 2906 | } else if (m->get_button_mask().has_flag(MouseButtonMask::MIDDLE)) { |
| 2907 | if (m->is_shift_pressed()) { |
| 2908 | _cursor_pan(m); |
| 2909 | } else { |
| 2910 | _cursor_orbit(m); |
| 2911 | } |
| 2912 | } |
| 2913 | |
| 2914 | return true; |
| 2915 | } |
| 2916 | |
| 2917 | Ref<InputEventKey> k = p_event; |
| 2918 | if (k.is_valid()) { |
| 2919 | if (k->get_physical_keycode() == Key::ESCAPE) { |
| 2920 | _set_camera_freelook_enabled(false); |
| 2921 | return true; |
| 2922 | } else if (k->is_ctrl_pressed()) { |
| 2923 | switch (k->get_physical_keycode()) { |
| 2924 | case Key::EQUAL: { |
| 2925 | ERR_FAIL_COND_V(!SceneTree::get_singleton()->get_root()->is_camera_3d_override_enabled(), false); |
| 2926 | cursor.fov_scale = CLAMP(cursor.fov_scale - 0.05, CAMERA_MIN_FOV_SCALE, CAMERA_MAX_FOV_SCALE); |
| 2927 | SceneTree::get_singleton()->get_root()->get_override_camera_3d()->set_perspective(camera_fov * cursor.fov_scale, camera_znear, camera_zfar); |
| 2928 | |
| 2929 | return true; |
| 2930 | } break; |
| 2931 | case Key::MINUS: { |
| 2932 | ERR_FAIL_COND_V(!SceneTree::get_singleton()->get_root()->is_camera_3d_override_enabled(), false); |
| 2933 | cursor.fov_scale = CLAMP(cursor.fov_scale + 0.05, CAMERA_MIN_FOV_SCALE, CAMERA_MAX_FOV_SCALE); |
| 2934 | SceneTree::get_singleton()->get_root()->get_override_camera_3d()->set_perspective(camera_fov * cursor.fov_scale, camera_znear, camera_zfar); |
| 2935 | |
| 2936 | return true; |
| 2937 | } break; |
| 2938 | case Key::KEY_0: { |
| 2939 | ERR_FAIL_COND_V(!SceneTree::get_singleton()->get_root()->is_camera_3d_override_enabled(), false); |
| 2940 | cursor.fov_scale = 1; |
| 2941 | SceneTree::get_singleton()->get_root()->get_override_camera_3d()->set_perspective(camera_fov, camera_znear, camera_zfar); |
| 2942 | |
| 2943 | return true; |
| 2944 | } break; |
| 2945 | default: { |
| 2946 | } |
| 2947 | } |
| 2948 | } |
| 2949 | } |
| 2950 | |
| 2951 | // TODO: Handle magnify and pan input gestures. |
| 2952 | |
| 2953 | return false; |
| 2954 | } |
| 2955 | |
| 2956 | void RuntimeNodeSelect::_set_camera_freelook_enabled(bool p_enabled) { |
| 2957 | camera_freelook = p_enabled; |
| 2958 | |
| 2959 | if (p_enabled) { |
| 2960 | // Make sure eye_pos is synced, because freelook referential is eye pos rather than orbit pos |
| 2961 | Vector3 forward = _get_cursor_transform().basis.xform(Vector3(0, 0, -1)); |
| 2962 | cursor.eye_pos = cursor.pos - cursor.distance * forward; |
| 2963 | |
| 2964 | previous_mouse_position = SceneTree::get_singleton()->get_root()->get_mouse_position(); |
| 2965 | |
| 2966 | // Hide mouse like in an FPS (warping doesn't work). |
| 2967 | Input::get_singleton()->set_mouse_mode_override(Input::MOUSE_MODE_CAPTURED); |
| 2968 | |
| 2969 | } else { |
| 2970 | // Restore mouse. |
| 2971 | Input::get_singleton()->set_mouse_mode_override(Input::MOUSE_MODE_VISIBLE); |
| 2972 | |
| 2973 | // Restore the previous mouse position when leaving freelook mode. |
| 2974 | // This is done because leaving `Input.MOUSE_MODE_CAPTURED` will center the cursor |
| 2975 | // due to OS limitations. |
| 2976 | Input::get_singleton()->warp_mouse(previous_mouse_position); |
| 2977 | } |
| 2978 | } |
| 2979 | |
| 2980 | void RuntimeNodeSelect::_cursor_scale_distance(real_t p_scale) { |
| 2981 | ERR_FAIL_COND(!SceneTree::get_singleton()->get_root()->is_camera_3d_override_enabled()); |
| 2982 | real_t min_distance = MAX(camera_znear * 4, VIEW_3D_MIN_ZOOM); |
| 2983 | real_t max_distance = MIN(camera_zfar / 4, VIEW_3D_MAX_ZOOM); |
| 2984 | cursor.distance = CLAMP(cursor.distance * p_scale, min_distance, max_distance); |
| 2985 | |
| 2986 | SceneTree::get_singleton()->get_root()->get_override_camera_3d()->set_transform(_get_cursor_transform()); |
| 2987 | } |
| 2988 | |
| 2989 | void RuntimeNodeSelect::_scale_freelook_speed(real_t p_scale) { |
| 2990 | real_t min_speed = MAX(camera_znear * 4, VIEW_3D_MIN_ZOOM); |
| 2991 | real_t max_speed = MIN(camera_zfar / 4, VIEW_3D_MAX_ZOOM); |
| 2992 | if (unlikely(min_speed > max_speed)) { |
| 2993 | freelook_base_speed = (min_speed + max_speed) / 2; |
| 2994 | } else { |
| 2995 | freelook_base_speed = CLAMP(freelook_base_speed * p_scale, min_speed, max_speed); |
| 2996 | } |
| 2997 | } |
| 2998 | |
| 2999 | void RuntimeNodeSelect::_cursor_look(Ref<InputEventWithModifiers> p_event) { |
| 3000 | Window *root = SceneTree::get_singleton()->get_root(); |
| 3001 | ERR_FAIL_COND(!root->is_camera_3d_override_enabled()); |
| 3002 | |
| 3003 | const Vector2 relative = _get_warped_mouse_motion(p_event, Rect2(Vector2(), root->get_size())); |
| 3004 | const Transform3D prev_camera_transform = _get_cursor_transform(); |
| 3005 | |
| 3006 | if (invert_y_axis) { |
| 3007 | cursor.x_rot -= relative.y * freelook_sensitivity; |
| 3008 | } else { |
| 3009 | cursor.x_rot += relative.y * freelook_sensitivity; |
| 3010 | } |
| 3011 | // Clamp the Y rotation to roughly -90..90 degrees so the user can't look upside-down and end up disoriented. |
| 3012 | cursor.x_rot = CLAMP(cursor.x_rot, -1.57, 1.57); |
| 3013 | |
| 3014 | cursor.y_rot += relative.x * freelook_sensitivity; |
| 3015 | |
| 3016 | // Look is like the opposite of Orbit: the focus point rotates around the camera. |
| 3017 | Transform3D camera_transform = _get_cursor_transform(); |
| 3018 | Vector3 pos = camera_transform.xform(Vector3(0, 0, 0)); |
| 3019 | Vector3 prev_pos = prev_camera_transform.xform(Vector3(0, 0, 0)); |
| 3020 | Vector3 diff = prev_pos - pos; |
| 3021 | cursor.pos += diff; |
| 3022 | |
| 3023 | root->get_override_camera_3d()->set_transform(_get_cursor_transform()); |
| 3024 | } |
| 3025 | |
| 3026 | void RuntimeNodeSelect::_cursor_pan(Ref<InputEventWithModifiers> p_event) { |
| 3027 | Window *root = SceneTree::get_singleton()->get_root(); |
| 3028 | ERR_FAIL_COND(!root->is_camera_3d_override_enabled()); |
| 3029 | |
| 3030 | // Reduce all sides of the area by 1, so warping works when windows are maximized/fullscreen. |
| 3031 | const Vector2 relative = _get_warped_mouse_motion(p_event, Rect2(Vector2(1, 1), root->get_size() - Vector2(2, 2))); |
| 3032 | const real_t pan_speed = translation_sensitivity / 150.0; |
| 3033 | |
| 3034 | Transform3D camera_transform; |
| 3035 | camera_transform.translate_local(cursor.pos); |
| 3036 | camera_transform.basis.rotate(Vector3(1, 0, 0), -cursor.x_rot); |
| 3037 | camera_transform.basis.rotate(Vector3(0, 1, 0), -cursor.y_rot); |
| 3038 | |
| 3039 | Vector3 translation(1 * -relative.x * pan_speed, relative.y * pan_speed, 0); |
| 3040 | translation *= cursor.distance / 4; |
| 3041 | camera_transform.translate_local(translation); |
| 3042 | cursor.pos = camera_transform.origin; |
| 3043 | |
| 3044 | root->get_override_camera_3d()->set_transform(_get_cursor_transform()); |
| 3045 | } |
| 3046 | |
| 3047 | void RuntimeNodeSelect::_cursor_orbit(Ref<InputEventWithModifiers> p_event) { |
| 3048 | Window *root = SceneTree::get_singleton()->get_root(); |
| 3049 | ERR_FAIL_COND(!root->is_camera_3d_override_enabled()); |
| 3050 | |
| 3051 | // Reduce all sides of the area by 1, so warping works when windows are maximized/fullscreen. |
| 3052 | const Vector2 relative = _get_warped_mouse_motion(p_event, Rect2(Vector2(1, 1), root->get_size() - Vector2(2, 2))); |
| 3053 | |
| 3054 | if (invert_y_axis) { |
| 3055 | cursor.x_rot -= relative.y * orbit_sensitivity; |
| 3056 | } else { |
| 3057 | cursor.x_rot += relative.y * orbit_sensitivity; |
| 3058 | } |
| 3059 | // Clamp the Y rotation to roughly -90..90 degrees so the user can't look upside-down and end up disoriented. |
| 3060 | cursor.x_rot = CLAMP(cursor.x_rot, -1.57, 1.57); |
| 3061 | |
| 3062 | if (invert_x_axis) { |
| 3063 | cursor.y_rot -= relative.x * orbit_sensitivity; |
| 3064 | } else { |
| 3065 | cursor.y_rot += relative.x * orbit_sensitivity; |
| 3066 | } |
| 3067 | |
| 3068 | root->get_override_camera_3d()->set_transform(_get_cursor_transform()); |
| 3069 | } |
| 3070 | |
| 3071 | Point2 RuntimeNodeSelect::_get_warped_mouse_motion(const Ref<InputEventMouseMotion> &p_event, Rect2 p_area) const { |
| 3072 | ERR_FAIL_COND_V(p_event.is_null(), Point2()); |
| 3073 | |
| 3074 | if (warped_mouse_panning_3d) { |
| 3075 | return Input::get_singleton()->warp_mouse_motion(p_event, p_area); |
| 3076 | } |
| 3077 | |
| 3078 | return p_event->get_relative(); |
| 3079 | } |
| 3080 | |
| 3081 | Transform3D RuntimeNodeSelect::_get_cursor_transform() { |
| 3082 | Transform3D camera_transform; |
| 3083 | camera_transform.translate_local(cursor.pos); |
| 3084 | camera_transform.basis.rotate(Vector3(1, 0, 0), -cursor.x_rot); |
| 3085 | camera_transform.basis.rotate(Vector3(0, 1, 0), -cursor.y_rot); |
| 3086 | camera_transform.translate_local(0, 0, cursor.distance); |
| 3087 | |
| 3088 | return camera_transform; |
| 3089 | } |
| 3090 | |
| 3091 | void RuntimeNodeSelect::_reset_camera_3d() { |
| 3092 | camera_first_override = true; |
| 3093 | |
| 3094 | cursor = Cursor(); |
| 3095 | Window *root = SceneTree::get_singleton()->get_root(); |
| 3096 | Camera3D *game_camera = root->is_camera_3d_override_enabled() ? root->get_overridden_camera_3d() : root->get_camera_3d(); |
| 3097 | if (game_camera) { |
| 3098 | Transform3D transform = game_camera->get_camera_transform(); |
| 3099 | transform.translate_local(0, 0, -cursor.distance); |
| 3100 | cursor.pos = transform.origin; |
| 3101 | |
| 3102 | cursor.x_rot = -game_camera->get_global_rotation().x; |
| 3103 | cursor.y_rot = -game_camera->get_global_rotation().y; |
| 3104 | |
| 3105 | cursor.fov_scale = CLAMP(game_camera->get_fov() / camera_fov, CAMERA_MIN_FOV_SCALE, CAMERA_MAX_FOV_SCALE); |
| 3106 | } else { |
| 3107 | cursor.fov_scale = 1.0; |
| 3108 | } |
| 3109 | |
| 3110 | if (root->is_camera_3d_override_enabled()) { |
| 3111 | Camera3D *override_camera = root->get_override_camera_3d(); |
| 3112 | override_camera->set_transform(_get_cursor_transform()); |
| 3113 | override_camera->set_perspective(camera_fov * cursor.fov_scale, camera_znear, camera_zfar); |
| 3114 | } |
| 3115 | } |
| 3116 | #endif // _3D_DISABLED |
| 3117 | |
| 3118 | #endif // DEBUG_ENABLED |