scene_debugger.cpp 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170
  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. #include "scene_debugger.h"
  31. #include "core/debugger/debugger_marshalls.h"
  32. #include "core/debugger/engine_debugger.h"
  33. #include "core/io/marshalls.h"
  34. #include "core/object/script_language.h"
  35. #include "core/templates/local_vector.h"
  36. #include "scene/2d/physics/collision_object_2d.h"
  37. #include "scene/2d/physics/collision_polygon_2d.h"
  38. #include "scene/2d/physics/collision_shape_2d.h"
  39. #ifndef _3D_DISABLED
  40. #include "scene/3d/label_3d.h"
  41. #include "scene/3d/mesh_instance_3d.h"
  42. #include "scene/3d/physics/collision_object_3d.h"
  43. #include "scene/3d/physics/collision_shape_3d.h"
  44. #include "scene/3d/sprite_3d.h"
  45. #include "scene/resources/surface_tool.h"
  46. #endif // _3D_DISABLED
  47. #include "scene/gui/popup_menu.h"
  48. #include "scene/main/canvas_layer.h"
  49. #include "scene/main/scene_tree.h"
  50. #include "scene/main/window.h"
  51. #include "scene/resources/packed_scene.h"
  52. #include "scene/theme/theme_db.h"
  53. SceneDebugger::SceneDebugger() {
  54. singleton = this;
  55. #ifdef DEBUG_ENABLED
  56. LiveEditor::singleton = memnew(LiveEditor);
  57. RuntimeNodeSelect::singleton = memnew(RuntimeNodeSelect);
  58. EngineDebugger::register_message_capture("scene", EngineDebugger::Capture(nullptr, SceneDebugger::parse_message));
  59. #endif
  60. }
  61. SceneDebugger::~SceneDebugger() {
  62. #ifdef DEBUG_ENABLED
  63. if (LiveEditor::singleton) {
  64. EngineDebugger::unregister_message_capture("scene");
  65. memdelete(LiveEditor::singleton);
  66. LiveEditor::singleton = nullptr;
  67. }
  68. if (RuntimeNodeSelect::singleton) {
  69. memdelete(RuntimeNodeSelect::singleton);
  70. RuntimeNodeSelect::singleton = nullptr;
  71. }
  72. #endif
  73. singleton = nullptr;
  74. }
  75. void SceneDebugger::initialize() {
  76. if (EngineDebugger::is_active()) {
  77. memnew(SceneDebugger);
  78. }
  79. }
  80. void SceneDebugger::deinitialize() {
  81. if (singleton) {
  82. memdelete(singleton);
  83. }
  84. }
  85. #ifdef DEBUG_ENABLED
  86. void SceneDebugger::_handle_input(const Ref<InputEvent> &p_event, const Ref<Shortcut> &p_shortcut) {
  87. Ref<InputEventKey> k = p_event;
  88. if (p_shortcut.is_valid() && k.is_valid() && k->is_pressed() && !k->is_echo() && p_shortcut->matches_event(k)) {
  89. EngineDebugger::get_singleton()->send_message("request_quit", Array());
  90. }
  91. }
  92. Error SceneDebugger::parse_message(void *p_user, const String &p_msg, const Array &p_args, bool &r_captured) {
  93. SceneTree *scene_tree = SceneTree::get_singleton();
  94. if (!scene_tree) {
  95. return ERR_UNCONFIGURED;
  96. }
  97. LiveEditor *live_editor = LiveEditor::get_singleton();
  98. if (!live_editor) {
  99. return ERR_UNCONFIGURED;
  100. }
  101. RuntimeNodeSelect *runtime_node_select = RuntimeNodeSelect::get_singleton();
  102. if (!runtime_node_select) {
  103. return ERR_UNCONFIGURED;
  104. }
  105. r_captured = true;
  106. if (p_msg == "setup_scene") {
  107. SceneTree::get_singleton()->get_root()->connect(SceneStringName(window_input), callable_mp_static(SceneDebugger::_handle_input).bind(DebuggerMarshalls::deserialize_key_shortcut(p_args)));
  108. } else if (p_msg == "request_scene_tree") { // Scene tree
  109. live_editor->_send_tree();
  110. } else if (p_msg == "save_node") { // Save node.
  111. ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA);
  112. _save_node(p_args[0], p_args[1]);
  113. Array arr;
  114. arr.append(p_args[1]);
  115. EngineDebugger::get_singleton()->send_message("filesystem:update_file", { arr });
  116. } else if (p_msg == "inspect_object") { // Object Inspect
  117. ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA);
  118. ObjectID id = p_args[0];
  119. _send_object_id(id);
  120. } else if (p_msg == "suspend_changed") {
  121. ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA);
  122. bool suspended = p_args[0];
  123. scene_tree->set_suspend(suspended);
  124. runtime_node_select->_update_input_state();
  125. } else if (p_msg == "next_frame") {
  126. _next_frame();
  127. } else if (p_msg == "override_cameras") { // Camera
  128. ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA);
  129. bool enable = p_args[0];
  130. bool from_editor = p_args[1];
  131. scene_tree->get_root()->enable_canvas_transform_override(enable);
  132. #ifndef _3D_DISABLED
  133. scene_tree->get_root()->enable_camera_3d_override(enable);
  134. #endif // _3D_DISABLED
  135. runtime_node_select->_set_camera_override_enabled(enable && !from_editor);
  136. } else if (p_msg == "transform_camera_2d") {
  137. ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA);
  138. Transform2D transform = p_args[0];
  139. scene_tree->get_root()->set_canvas_transform_override(transform);
  140. runtime_node_select->_queue_selection_update();
  141. #ifndef _3D_DISABLED
  142. } else if (p_msg == "transform_camera_3d") {
  143. ERR_FAIL_COND_V(p_args.size() < 5, ERR_INVALID_DATA);
  144. Transform3D transform = p_args[0];
  145. bool is_perspective = p_args[1];
  146. float size_or_fov = p_args[2];
  147. float depth_near = p_args[3];
  148. float depth_far = p_args[4];
  149. if (is_perspective) {
  150. scene_tree->get_root()->set_camera_3d_override_perspective(size_or_fov, depth_near, depth_far);
  151. } else {
  152. scene_tree->get_root()->set_camera_3d_override_orthogonal(size_or_fov, depth_near, depth_far);
  153. }
  154. scene_tree->get_root()->set_camera_3d_override_transform(transform);
  155. runtime_node_select->_queue_selection_update();
  156. #endif // _3D_DISABLED
  157. } else if (p_msg == "set_object_property") {
  158. ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA);
  159. _set_object_property(p_args[0], p_args[1], p_args[2]);
  160. runtime_node_select->_queue_selection_update();
  161. } else if (p_msg == "reload_cached_files") {
  162. ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA);
  163. PackedStringArray files = p_args[0];
  164. reload_cached_files(files);
  165. } else if (p_msg.begins_with("live_")) { /// Live Edit
  166. if (p_msg == "live_set_root") {
  167. ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA);
  168. live_editor->_root_func(p_args[0], p_args[1]);
  169. } else if (p_msg == "live_node_path") {
  170. ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA);
  171. live_editor->_node_path_func(p_args[0], p_args[1]);
  172. } else if (p_msg == "live_res_path") {
  173. ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA);
  174. live_editor->_res_path_func(p_args[0], p_args[1]);
  175. } else if (p_msg == "live_node_prop_res") {
  176. ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA);
  177. live_editor->_node_set_res_func(p_args[0], p_args[1], p_args[2]);
  178. } else if (p_msg == "live_node_prop") {
  179. ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA);
  180. live_editor->_node_set_func(p_args[0], p_args[1], p_args[2]);
  181. } else if (p_msg == "live_res_prop_res") {
  182. ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA);
  183. live_editor->_res_set_res_func(p_args[0], p_args[1], p_args[2]);
  184. } else if (p_msg == "live_res_prop") {
  185. ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA);
  186. live_editor->_res_set_func(p_args[0], p_args[1], p_args[2]);
  187. } else if (p_msg == "live_node_call") {
  188. ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA);
  189. LocalVector<Variant> args;
  190. LocalVector<Variant *> argptrs;
  191. args.resize(p_args.size() - 2);
  192. argptrs.resize(args.size());
  193. for (uint32_t i = 0; i < args.size(); i++) {
  194. args[i] = p_args[i + 2];
  195. argptrs[i] = &args[i];
  196. }
  197. live_editor->_node_call_func(p_args[0], p_args[1], argptrs.size() ? (const Variant **)argptrs.ptr() : nullptr, argptrs.size());
  198. } else if (p_msg == "live_res_call") {
  199. ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA);
  200. LocalVector<Variant> args;
  201. LocalVector<Variant *> argptrs;
  202. args.resize(p_args.size() - 2);
  203. argptrs.resize(args.size());
  204. for (uint32_t i = 0; i < args.size(); i++) {
  205. args[i] = p_args[i + 2];
  206. argptrs[i] = &args[i];
  207. }
  208. live_editor->_res_call_func(p_args[0], p_args[1], argptrs.size() ? (const Variant **)argptrs.ptr() : nullptr, argptrs.size());
  209. } else if (p_msg == "live_create_node") {
  210. ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA);
  211. live_editor->_create_node_func(p_args[0], p_args[1], p_args[2]);
  212. } else if (p_msg == "live_instantiate_node") {
  213. ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA);
  214. live_editor->_instance_node_func(p_args[0], p_args[1], p_args[2]);
  215. } else if (p_msg == "live_remove_node") {
  216. ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA);
  217. live_editor->_remove_node_func(p_args[0]);
  218. if (!runtime_node_select->has_selection) {
  219. runtime_node_select->_clear_selection();
  220. }
  221. } else if (p_msg == "live_remove_and_keep_node") {
  222. ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA);
  223. live_editor->_remove_and_keep_node_func(p_args[0], p_args[1]);
  224. if (!runtime_node_select->has_selection) {
  225. runtime_node_select->_clear_selection();
  226. }
  227. } else if (p_msg == "live_restore_node") {
  228. ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA);
  229. live_editor->_restore_node_func(p_args[0], p_args[1], p_args[2]);
  230. } else if (p_msg == "live_duplicate_node") {
  231. ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA);
  232. live_editor->_duplicate_node_func(p_args[0], p_args[1]);
  233. } else if (p_msg == "live_reparent_node") {
  234. ERR_FAIL_COND_V(p_args.size() < 4, ERR_INVALID_DATA);
  235. live_editor->_reparent_node_func(p_args[0], p_args[1], p_args[2], p_args[3]);
  236. } else {
  237. return ERR_SKIP;
  238. }
  239. } else if (p_msg.begins_with("runtime_node_select_")) { /// Runtime Node Selection
  240. if (p_msg == "runtime_node_select_setup") {
  241. ERR_FAIL_COND_V(p_args.is_empty() || p_args[0].get_type() != Variant::DICTIONARY, ERR_INVALID_DATA);
  242. runtime_node_select->_setup(p_args[0]);
  243. } else if (p_msg == "runtime_node_select_set_type") {
  244. ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA);
  245. RuntimeNodeSelect::NodeType type = (RuntimeNodeSelect::NodeType)(int)p_args[0];
  246. runtime_node_select->_node_set_type(type);
  247. } else if (p_msg == "runtime_node_select_set_mode") {
  248. ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA);
  249. RuntimeNodeSelect::SelectMode mode = (RuntimeNodeSelect::SelectMode)(int)p_args[0];
  250. runtime_node_select->_select_set_mode(mode);
  251. } else if (p_msg == "runtime_node_select_set_visible") {
  252. ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA);
  253. bool visible = p_args[0];
  254. runtime_node_select->_set_selection_visible(visible);
  255. } else if (p_msg == "runtime_node_select_reset_camera_2d") {
  256. runtime_node_select->_reset_camera_2d();
  257. #ifndef _3D_DISABLED
  258. } else if (p_msg == "runtime_node_select_reset_camera_3d") {
  259. runtime_node_select->_reset_camera_3d();
  260. #endif // _3D_DISABLED
  261. } else {
  262. return ERR_SKIP;
  263. }
  264. } else {
  265. r_captured = false;
  266. }
  267. return OK;
  268. }
  269. void SceneDebugger::_save_node(ObjectID id, const String &p_path) {
  270. Node *node = Object::cast_to<Node>(ObjectDB::get_instance(id));
  271. ERR_FAIL_NULL(node);
  272. #ifdef TOOLS_ENABLED
  273. HashMap<const Node *, Node *> duplimap;
  274. Node *copy = node->duplicate_from_editor(duplimap);
  275. #else
  276. Node *copy = node->duplicate();
  277. #endif
  278. // Handle Unique Nodes.
  279. for (int i = 0; i < copy->get_child_count(false); i++) {
  280. _set_node_owner_recursive(copy->get_child(i, false), copy);
  281. }
  282. // Root node cannot ever be unique name in its own Scene!
  283. copy->set_unique_name_in_owner(false);
  284. Ref<PackedScene> ps = memnew(PackedScene);
  285. ps->pack(copy);
  286. ResourceSaver::save(ps, p_path);
  287. memdelete(copy);
  288. }
  289. void SceneDebugger::_set_node_owner_recursive(Node *p_node, Node *p_owner) {
  290. if (!p_node->get_owner()) {
  291. p_node->set_owner(p_owner);
  292. }
  293. for (int i = 0; i < p_node->get_child_count(false); i++) {
  294. _set_node_owner_recursive(p_node->get_child(i, false), p_owner);
  295. }
  296. }
  297. void SceneDebugger::_send_object_id(ObjectID p_id, int p_max_size) {
  298. SceneDebuggerObject obj(p_id);
  299. if (obj.id.is_null()) {
  300. return;
  301. }
  302. Node *node = Object::cast_to<Node>(ObjectDB::get_instance(p_id));
  303. RuntimeNodeSelect::get_singleton()->_select_node(node);
  304. Array arr;
  305. obj.serialize(arr);
  306. EngineDebugger::get_singleton()->send_message("scene:inspect_object", arr);
  307. }
  308. void SceneDebugger::_set_object_property(ObjectID p_id, const String &p_property, const Variant &p_value) {
  309. Object *obj = ObjectDB::get_instance(p_id);
  310. if (!obj) {
  311. return;
  312. }
  313. String prop_name = p_property;
  314. if (p_property.begins_with("Members/")) {
  315. Vector<String> ss = p_property.split("/");
  316. prop_name = ss[ss.size() - 1];
  317. }
  318. obj->set(prop_name, p_value);
  319. }
  320. void SceneDebugger::_next_frame() {
  321. SceneTree *scene_tree = SceneTree::get_singleton();
  322. if (!scene_tree->is_suspended()) {
  323. return;
  324. }
  325. scene_tree->set_suspend(false);
  326. RenderingServer::get_singleton()->connect("frame_post_draw", callable_mp(scene_tree, &SceneTree::set_suspend).bind(true), Object::CONNECT_ONE_SHOT);
  327. }
  328. void SceneDebugger::add_to_cache(const String &p_filename, Node *p_node) {
  329. LiveEditor *debugger = LiveEditor::get_singleton();
  330. if (!debugger) {
  331. return;
  332. }
  333. if (EngineDebugger::get_script_debugger() && !p_filename.is_empty()) {
  334. debugger->live_scene_edit_cache[p_filename].insert(p_node);
  335. }
  336. }
  337. void SceneDebugger::remove_from_cache(const String &p_filename, Node *p_node) {
  338. LiveEditor *debugger = LiveEditor::get_singleton();
  339. if (!debugger) {
  340. return;
  341. }
  342. HashMap<String, HashSet<Node *>> &edit_cache = debugger->live_scene_edit_cache;
  343. HashMap<String, HashSet<Node *>>::Iterator E = edit_cache.find(p_filename);
  344. if (E) {
  345. E->value.erase(p_node);
  346. if (E->value.size() == 0) {
  347. edit_cache.remove(E);
  348. }
  349. }
  350. HashMap<Node *, HashMap<ObjectID, Node *>> &remove_list = debugger->live_edit_remove_list;
  351. HashMap<Node *, HashMap<ObjectID, Node *>>::Iterator F = remove_list.find(p_node);
  352. if (F) {
  353. for (const KeyValue<ObjectID, Node *> &G : F->value) {
  354. memdelete(G.value);
  355. }
  356. remove_list.remove(F);
  357. }
  358. }
  359. void SceneDebugger::reload_cached_files(const PackedStringArray &p_files) {
  360. for (const String &file : p_files) {
  361. Ref<Resource> res = ResourceCache::get_ref(file);
  362. if (res.is_valid()) {
  363. res->reload_from_file();
  364. }
  365. }
  366. }
  367. /// SceneDebuggerObject
  368. SceneDebuggerObject::SceneDebuggerObject(ObjectID p_id) {
  369. id = ObjectID();
  370. Object *obj = ObjectDB::get_instance(p_id);
  371. if (!obj) {
  372. return;
  373. }
  374. id = p_id;
  375. class_name = obj->get_class();
  376. if (ScriptInstance *si = obj->get_script_instance()) {
  377. // Read script instance constants and variables
  378. if (!si->get_script().is_null()) {
  379. Script *s = si->get_script().ptr();
  380. _parse_script_properties(s, si);
  381. }
  382. }
  383. if (Node *node = Object::cast_to<Node>(obj)) {
  384. // For debugging multiplayer.
  385. {
  386. PropertyInfo pi(Variant::INT, String("Node/multiplayer_authority"), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_READ_ONLY);
  387. properties.push_back(SceneDebuggerProperty(pi, node->get_multiplayer_authority()));
  388. }
  389. // Add specialized NodePath info (if inside tree).
  390. if (node->is_inside_tree()) {
  391. PropertyInfo pi(Variant::NODE_PATH, String("Node/path"));
  392. properties.push_back(SceneDebuggerProperty(pi, node->get_path()));
  393. } else { // Can't ask for path if a node is not in tree.
  394. PropertyInfo pi(Variant::STRING, String("Node/path"));
  395. properties.push_back(SceneDebuggerProperty(pi, "[Orphan]"));
  396. }
  397. } else if (Script *s = Object::cast_to<Script>(obj)) {
  398. // Add script constants (no instance).
  399. _parse_script_properties(s, nullptr);
  400. }
  401. // Add base object properties.
  402. List<PropertyInfo> pinfo;
  403. obj->get_property_list(&pinfo, true);
  404. for (const PropertyInfo &E : pinfo) {
  405. if (E.usage & (PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CATEGORY)) {
  406. properties.push_back(SceneDebuggerProperty(E, obj->get(E.name)));
  407. }
  408. }
  409. }
  410. void SceneDebuggerObject::_parse_script_properties(Script *p_script, ScriptInstance *p_instance) {
  411. typedef HashMap<const Script *, HashSet<StringName>> ScriptMemberMap;
  412. typedef HashMap<const Script *, HashMap<StringName, Variant>> ScriptConstantsMap;
  413. ScriptMemberMap members;
  414. if (p_instance) {
  415. members[p_script] = HashSet<StringName>();
  416. p_script->get_members(&(members[p_script]));
  417. }
  418. ScriptConstantsMap constants;
  419. constants[p_script] = HashMap<StringName, Variant>();
  420. p_script->get_constants(&(constants[p_script]));
  421. Ref<Script> base = p_script->get_base_script();
  422. while (base.is_valid()) {
  423. if (p_instance) {
  424. members[base.ptr()] = HashSet<StringName>();
  425. base->get_members(&(members[base.ptr()]));
  426. }
  427. constants[base.ptr()] = HashMap<StringName, Variant>();
  428. base->get_constants(&(constants[base.ptr()]));
  429. base = base->get_base_script();
  430. }
  431. // Members
  432. for (KeyValue<const Script *, HashSet<StringName>> sm : members) {
  433. for (const StringName &E : sm.value) {
  434. Variant m;
  435. if (p_instance->get(E, m)) {
  436. String script_path = sm.key == p_script ? "" : sm.key->get_path().get_file() + "/";
  437. PropertyInfo pi(m.get_type(), "Members/" + script_path + E);
  438. properties.push_back(SceneDebuggerProperty(pi, m));
  439. }
  440. }
  441. }
  442. // Constants
  443. for (KeyValue<const Script *, HashMap<StringName, Variant>> &sc : constants) {
  444. for (const KeyValue<StringName, Variant> &E : sc.value) {
  445. String script_path = sc.key == p_script ? "" : sc.key->get_path().get_file() + "/";
  446. if (E.value.get_type() == Variant::OBJECT) {
  447. Variant inst_id = ((Object *)E.value)->get_instance_id();
  448. PropertyInfo pi(inst_id.get_type(), "Constants/" + E.key, PROPERTY_HINT_OBJECT_ID, "Object");
  449. properties.push_back(SceneDebuggerProperty(pi, inst_id));
  450. } else {
  451. PropertyInfo pi(E.value.get_type(), "Constants/" + script_path + E.key);
  452. properties.push_back(SceneDebuggerProperty(pi, E.value));
  453. }
  454. }
  455. }
  456. }
  457. void SceneDebuggerObject::serialize(Array &r_arr, int p_max_size) {
  458. Array send_props;
  459. for (SceneDebuggerObject::SceneDebuggerProperty &property : properties) {
  460. const PropertyInfo &pi = property.first;
  461. Variant &var = property.second;
  462. Ref<Resource> res = var;
  463. Array prop;
  464. prop.push_back(pi.name);
  465. prop.push_back(pi.type);
  466. PropertyHint hint = pi.hint;
  467. String hint_string = pi.hint_string;
  468. if (!res.is_null() && !res->get_path().is_empty()) {
  469. var = res->get_path();
  470. } else { //only send information that can be sent..
  471. int len = 0; //test how big is this to encode
  472. encode_variant(var, nullptr, len);
  473. if (len > p_max_size) { //limit to max size
  474. hint = PROPERTY_HINT_OBJECT_TOO_BIG;
  475. hint_string = "";
  476. var = Variant();
  477. }
  478. }
  479. prop.push_back(hint);
  480. prop.push_back(hint_string);
  481. prop.push_back(pi.usage);
  482. prop.push_back(var);
  483. send_props.push_back(prop);
  484. }
  485. r_arr.push_back(uint64_t(id));
  486. r_arr.push_back(class_name);
  487. r_arr.push_back(send_props);
  488. }
  489. void SceneDebuggerObject::deserialize(const Array &p_arr) {
  490. #define CHECK_TYPE(p_what, p_type) ERR_FAIL_COND(p_what.get_type() != Variant::p_type);
  491. ERR_FAIL_COND(p_arr.size() < 3);
  492. CHECK_TYPE(p_arr[0], INT);
  493. CHECK_TYPE(p_arr[1], STRING);
  494. CHECK_TYPE(p_arr[2], ARRAY);
  495. id = uint64_t(p_arr[0]);
  496. class_name = p_arr[1];
  497. Array props = p_arr[2];
  498. for (int i = 0; i < props.size(); i++) {
  499. CHECK_TYPE(props[i], ARRAY);
  500. Array prop = props[i];
  501. ERR_FAIL_COND(prop.size() != 6);
  502. CHECK_TYPE(prop[0], STRING);
  503. CHECK_TYPE(prop[1], INT);
  504. CHECK_TYPE(prop[2], INT);
  505. CHECK_TYPE(prop[3], STRING);
  506. CHECK_TYPE(prop[4], INT);
  507. PropertyInfo pinfo;
  508. pinfo.name = prop[0];
  509. pinfo.type = Variant::Type(int(prop[1]));
  510. pinfo.hint = PropertyHint(int(prop[2]));
  511. pinfo.hint_string = prop[3];
  512. pinfo.usage = PropertyUsageFlags(int(prop[4]));
  513. Variant var = prop[5];
  514. if (pinfo.type == Variant::OBJECT) {
  515. if (var.is_zero()) {
  516. var = Ref<Resource>();
  517. } else if (var.get_type() == Variant::OBJECT) {
  518. if (((Object *)var)->is_class("EncodedObjectAsID")) {
  519. var = Object::cast_to<EncodedObjectAsID>(var)->get_object_id();
  520. pinfo.type = var.get_type();
  521. pinfo.hint = PROPERTY_HINT_OBJECT_ID;
  522. pinfo.hint_string = "Object";
  523. }
  524. }
  525. }
  526. properties.push_back(SceneDebuggerProperty(pinfo, var));
  527. }
  528. }
  529. /// SceneDebuggerTree
  530. SceneDebuggerTree::SceneDebuggerTree(Node *p_root) {
  531. // Flatten tree into list, depth first, use stack to avoid recursion.
  532. List<Node *> stack;
  533. stack.push_back(p_root);
  534. bool is_root = true;
  535. const StringName &is_visible_sn = SNAME("is_visible");
  536. const StringName &is_visible_in_tree_sn = SNAME("is_visible_in_tree");
  537. while (stack.size()) {
  538. Node *n = stack.front()->get();
  539. stack.pop_front();
  540. int count = n->get_child_count();
  541. for (int i = 0; i < count; i++) {
  542. stack.push_front(n->get_child(count - i - 1));
  543. }
  544. int view_flags = 0;
  545. if (is_root) {
  546. // Prevent root window visibility from being changed.
  547. is_root = false;
  548. } else if (n->has_method(is_visible_sn)) {
  549. const Variant visible = n->call(is_visible_sn);
  550. if (visible.get_type() == Variant::BOOL) {
  551. view_flags = RemoteNode::VIEW_HAS_VISIBLE_METHOD;
  552. view_flags |= uint8_t(visible) * RemoteNode::VIEW_VISIBLE;
  553. }
  554. if (n->has_method(is_visible_in_tree_sn)) {
  555. const Variant visible_in_tree = n->call(is_visible_in_tree_sn);
  556. if (visible_in_tree.get_type() == Variant::BOOL) {
  557. view_flags |= uint8_t(visible_in_tree) * RemoteNode::VIEW_VISIBLE_IN_TREE;
  558. }
  559. }
  560. }
  561. String class_name;
  562. ScriptInstance *script_instance = n->get_script_instance();
  563. if (script_instance) {
  564. Ref<Script> script = script_instance->get_script();
  565. if (script.is_valid()) {
  566. class_name = script->get_global_name();
  567. if (class_name.is_empty()) {
  568. // If there is no class_name in this script we just take the script path.
  569. class_name = script->get_path();
  570. }
  571. }
  572. }
  573. 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));
  574. }
  575. }
  576. void SceneDebuggerTree::serialize(Array &p_arr) {
  577. for (const RemoteNode &n : nodes) {
  578. p_arr.push_back(n.child_count);
  579. p_arr.push_back(n.name);
  580. p_arr.push_back(n.type_name);
  581. p_arr.push_back(n.id);
  582. p_arr.push_back(n.scene_file_path);
  583. p_arr.push_back(n.view_flags);
  584. }
  585. }
  586. void SceneDebuggerTree::deserialize(const Array &p_arr) {
  587. int idx = 0;
  588. while (p_arr.size() > idx) {
  589. ERR_FAIL_COND(p_arr.size() < 6);
  590. CHECK_TYPE(p_arr[idx], INT); // child_count.
  591. CHECK_TYPE(p_arr[idx + 1], STRING); // name.
  592. CHECK_TYPE(p_arr[idx + 2], STRING); // type_name.
  593. CHECK_TYPE(p_arr[idx + 3], INT); // id.
  594. CHECK_TYPE(p_arr[idx + 4], STRING); // scene_file_path.
  595. CHECK_TYPE(p_arr[idx + 5], INT); // view_flags.
  596. 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]));
  597. idx += 6;
  598. }
  599. }
  600. /// LiveEditor
  601. LiveEditor *LiveEditor::get_singleton() {
  602. return singleton;
  603. }
  604. void LiveEditor::_send_tree() {
  605. SceneTree *scene_tree = SceneTree::get_singleton();
  606. if (!scene_tree) {
  607. return;
  608. }
  609. Array arr;
  610. // Encoded as a flat list depth first.
  611. SceneDebuggerTree tree(scene_tree->root);
  612. tree.serialize(arr);
  613. EngineDebugger::get_singleton()->send_message("scene:scene_tree", arr);
  614. }
  615. void LiveEditor::_node_path_func(const NodePath &p_path, int p_id) {
  616. live_edit_node_path_cache[p_id] = p_path;
  617. }
  618. void LiveEditor::_res_path_func(const String &p_path, int p_id) {
  619. live_edit_resource_cache[p_id] = p_path;
  620. }
  621. void LiveEditor::_node_set_func(int p_id, const StringName &p_prop, const Variant &p_value) {
  622. SceneTree *scene_tree = SceneTree::get_singleton();
  623. if (!scene_tree) {
  624. return;
  625. }
  626. if (!live_edit_node_path_cache.has(p_id)) {
  627. return;
  628. }
  629. NodePath np = live_edit_node_path_cache[p_id];
  630. Node *base = nullptr;
  631. if (scene_tree->root->has_node(live_edit_root)) {
  632. base = scene_tree->root->get_node(live_edit_root);
  633. }
  634. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  635. if (!E) {
  636. return; //scene not editable
  637. }
  638. for (Node *F : E->value) {
  639. Node *n = F;
  640. if (base && !base->is_ancestor_of(n)) {
  641. continue;
  642. }
  643. if (!n->has_node(np)) {
  644. continue;
  645. }
  646. Node *n2 = n->get_node(np);
  647. // Do not change transform of edited scene root, unless it's the scene being played.
  648. // See GH-86659 for additional context.
  649. bool keep_transform = (n2 == n) && (n2->get_parent() != scene_tree->root);
  650. Variant orig_tf;
  651. if (keep_transform) {
  652. if (n2->is_class("Node3D")) {
  653. orig_tf = n2->call("get_transform");
  654. } else if (n2->is_class("CanvasItem")) {
  655. orig_tf = n2->call("_edit_get_state");
  656. }
  657. }
  658. n2->set(p_prop, p_value);
  659. if (keep_transform) {
  660. if (n2->is_class("Node3D")) {
  661. Variant new_tf = n2->call("get_transform");
  662. if (new_tf != orig_tf) {
  663. n2->call("set_transform", orig_tf);
  664. }
  665. } else if (n2->is_class("CanvasItem")) {
  666. Variant new_tf = n2->call("_edit_get_state");
  667. if (new_tf != orig_tf) {
  668. n2->call("_edit_set_state", orig_tf);
  669. }
  670. }
  671. }
  672. }
  673. }
  674. void LiveEditor::_node_set_res_func(int p_id, const StringName &p_prop, const String &p_value) {
  675. Ref<Resource> r = ResourceLoader::load(p_value);
  676. if (!r.is_valid()) {
  677. return;
  678. }
  679. _node_set_func(p_id, p_prop, r);
  680. }
  681. void LiveEditor::_node_call_func(int p_id, const StringName &p_method, const Variant **p_args, int p_argcount) {
  682. SceneTree *scene_tree = SceneTree::get_singleton();
  683. if (!scene_tree) {
  684. return;
  685. }
  686. if (!live_edit_node_path_cache.has(p_id)) {
  687. return;
  688. }
  689. NodePath np = live_edit_node_path_cache[p_id];
  690. Node *base = nullptr;
  691. if (scene_tree->root->has_node(live_edit_root)) {
  692. base = scene_tree->root->get_node(live_edit_root);
  693. }
  694. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  695. if (!E) {
  696. return; //scene not editable
  697. }
  698. for (Node *F : E->value) {
  699. Node *n = F;
  700. if (base && !base->is_ancestor_of(n)) {
  701. continue;
  702. }
  703. if (!n->has_node(np)) {
  704. continue;
  705. }
  706. Node *n2 = n->get_node(np);
  707. // Do not change transform of edited scene root, unless it's the scene being played.
  708. // See GH-86659 for additional context.
  709. bool keep_transform = (n2 == n) && (n2->get_parent() != scene_tree->root);
  710. Variant orig_tf;
  711. if (keep_transform) {
  712. if (n2->is_class("Node3D")) {
  713. orig_tf = n2->call("get_transform");
  714. } else if (n2->is_class("CanvasItem")) {
  715. orig_tf = n2->call("_edit_get_state");
  716. }
  717. }
  718. Callable::CallError ce;
  719. n2->callp(p_method, p_args, p_argcount, ce);
  720. if (keep_transform) {
  721. if (n2->is_class("Node3D")) {
  722. Variant new_tf = n2->call("get_transform");
  723. if (new_tf != orig_tf) {
  724. n2->call("set_transform", orig_tf);
  725. }
  726. } else if (n2->is_class("CanvasItem")) {
  727. Variant new_tf = n2->call("_edit_get_state");
  728. if (new_tf != orig_tf) {
  729. n2->call("_edit_set_state", orig_tf);
  730. }
  731. }
  732. }
  733. }
  734. }
  735. void LiveEditor::_res_set_func(int p_id, const StringName &p_prop, const Variant &p_value) {
  736. if (!live_edit_resource_cache.has(p_id)) {
  737. return;
  738. }
  739. String resp = live_edit_resource_cache[p_id];
  740. if (!ResourceCache::has(resp)) {
  741. return;
  742. }
  743. Ref<Resource> r = ResourceCache::get_ref(resp);
  744. if (!r.is_valid()) {
  745. return;
  746. }
  747. r->set(p_prop, p_value);
  748. }
  749. void LiveEditor::_res_set_res_func(int p_id, const StringName &p_prop, const String &p_value) {
  750. Ref<Resource> r = ResourceLoader::load(p_value);
  751. if (!r.is_valid()) {
  752. return;
  753. }
  754. _res_set_func(p_id, p_prop, r);
  755. }
  756. void LiveEditor::_res_call_func(int p_id, const StringName &p_method, const Variant **p_args, int p_argcount) {
  757. if (!live_edit_resource_cache.has(p_id)) {
  758. return;
  759. }
  760. String resp = live_edit_resource_cache[p_id];
  761. if (!ResourceCache::has(resp)) {
  762. return;
  763. }
  764. Ref<Resource> r = ResourceCache::get_ref(resp);
  765. if (!r.is_valid()) {
  766. return;
  767. }
  768. Callable::CallError ce;
  769. r->callp(p_method, p_args, p_argcount, ce);
  770. }
  771. void LiveEditor::_root_func(const NodePath &p_scene_path, const String &p_scene_from) {
  772. live_edit_root = p_scene_path;
  773. live_edit_scene = p_scene_from;
  774. }
  775. void LiveEditor::_create_node_func(const NodePath &p_parent, const String &p_type, const String &p_name) {
  776. SceneTree *scene_tree = SceneTree::get_singleton();
  777. if (!scene_tree) {
  778. return;
  779. }
  780. Node *base = nullptr;
  781. if (scene_tree->root->has_node(live_edit_root)) {
  782. base = scene_tree->root->get_node(live_edit_root);
  783. }
  784. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  785. if (!E) {
  786. return; //scene not editable
  787. }
  788. for (Node *F : E->value) {
  789. Node *n = F;
  790. if (base && !base->is_ancestor_of(n)) {
  791. continue;
  792. }
  793. if (!n->has_node(p_parent)) {
  794. continue;
  795. }
  796. Node *n2 = n->get_node(p_parent);
  797. Node *no = Object::cast_to<Node>(ClassDB::instantiate(p_type));
  798. if (!no) {
  799. continue;
  800. }
  801. no->set_name(p_name);
  802. n2->add_child(no);
  803. }
  804. }
  805. void LiveEditor::_instance_node_func(const NodePath &p_parent, const String &p_path, const String &p_name) {
  806. SceneTree *scene_tree = SceneTree::get_singleton();
  807. if (!scene_tree) {
  808. return;
  809. }
  810. Ref<PackedScene> ps = ResourceLoader::load(p_path);
  811. if (!ps.is_valid()) {
  812. return;
  813. }
  814. Node *base = nullptr;
  815. if (scene_tree->root->has_node(live_edit_root)) {
  816. base = scene_tree->root->get_node(live_edit_root);
  817. }
  818. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  819. if (!E) {
  820. return; //scene not editable
  821. }
  822. for (Node *F : E->value) {
  823. Node *n = F;
  824. if (base && !base->is_ancestor_of(n)) {
  825. continue;
  826. }
  827. if (!n->has_node(p_parent)) {
  828. continue;
  829. }
  830. Node *n2 = n->get_node(p_parent);
  831. Node *no = ps->instantiate();
  832. if (!no) {
  833. continue;
  834. }
  835. no->set_name(p_name);
  836. n2->add_child(no);
  837. }
  838. }
  839. void LiveEditor::_remove_node_func(const NodePath &p_at) {
  840. SceneTree *scene_tree = SceneTree::get_singleton();
  841. if (!scene_tree) {
  842. return;
  843. }
  844. Node *base = nullptr;
  845. if (scene_tree->root->has_node(live_edit_root)) {
  846. base = scene_tree->root->get_node(live_edit_root);
  847. }
  848. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  849. if (!E) {
  850. return; //scene not editable
  851. }
  852. Vector<Node *> to_delete;
  853. for (HashSet<Node *>::Iterator F = E->value.begin(); F; ++F) {
  854. Node *n = *F;
  855. if (base && !base->is_ancestor_of(n)) {
  856. continue;
  857. }
  858. if (!n->has_node(p_at)) {
  859. continue;
  860. }
  861. Node *n2 = n->get_node(p_at);
  862. to_delete.push_back(n2);
  863. }
  864. for (int i = 0; i < to_delete.size(); i++) {
  865. memdelete(to_delete[i]);
  866. }
  867. }
  868. void LiveEditor::_remove_and_keep_node_func(const NodePath &p_at, ObjectID p_keep_id) {
  869. SceneTree *scene_tree = SceneTree::get_singleton();
  870. if (!scene_tree) {
  871. return;
  872. }
  873. Node *base = nullptr;
  874. if (scene_tree->root->has_node(live_edit_root)) {
  875. base = scene_tree->root->get_node(live_edit_root);
  876. }
  877. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  878. if (!E) {
  879. return; //scene not editable
  880. }
  881. Vector<Node *> to_remove;
  882. for (HashSet<Node *>::Iterator F = E->value.begin(); F; ++F) {
  883. Node *n = *F;
  884. if (base && !base->is_ancestor_of(n)) {
  885. continue;
  886. }
  887. if (!n->has_node(p_at)) {
  888. continue;
  889. }
  890. to_remove.push_back(n);
  891. }
  892. for (int i = 0; i < to_remove.size(); i++) {
  893. Node *n = to_remove[i];
  894. Node *n2 = n->get_node(p_at);
  895. n2->get_parent()->remove_child(n2);
  896. live_edit_remove_list[n][p_keep_id] = n2;
  897. }
  898. }
  899. void LiveEditor::_restore_node_func(ObjectID p_id, const NodePath &p_at, int p_at_pos) {
  900. SceneTree *scene_tree = SceneTree::get_singleton();
  901. if (!scene_tree) {
  902. return;
  903. }
  904. Node *base = nullptr;
  905. if (scene_tree->root->has_node(live_edit_root)) {
  906. base = scene_tree->root->get_node(live_edit_root);
  907. }
  908. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  909. if (!E) {
  910. return; //scene not editable
  911. }
  912. for (HashSet<Node *>::Iterator F = E->value.begin(); F;) {
  913. HashSet<Node *>::Iterator N = F;
  914. ++N;
  915. Node *n = *F;
  916. if (base && !base->is_ancestor_of(n)) {
  917. continue;
  918. }
  919. if (!n->has_node(p_at)) {
  920. continue;
  921. }
  922. Node *n2 = n->get_node(p_at);
  923. HashMap<Node *, HashMap<ObjectID, Node *>>::Iterator EN = live_edit_remove_list.find(n);
  924. if (!EN) {
  925. continue;
  926. }
  927. HashMap<ObjectID, Node *>::Iterator FN = EN->value.find(p_id);
  928. if (!FN) {
  929. continue;
  930. }
  931. n2->add_child(FN->value);
  932. EN->value.remove(FN);
  933. if (EN->value.size() == 0) {
  934. live_edit_remove_list.remove(EN);
  935. }
  936. F = N;
  937. }
  938. }
  939. void LiveEditor::_duplicate_node_func(const NodePath &p_at, const String &p_new_name) {
  940. SceneTree *scene_tree = SceneTree::get_singleton();
  941. if (!scene_tree) {
  942. return;
  943. }
  944. Node *base = nullptr;
  945. if (scene_tree->root->has_node(live_edit_root)) {
  946. base = scene_tree->root->get_node(live_edit_root);
  947. }
  948. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  949. if (!E) {
  950. return; //scene not editable
  951. }
  952. for (Node *F : E->value) {
  953. Node *n = F;
  954. if (base && !base->is_ancestor_of(n)) {
  955. continue;
  956. }
  957. if (!n->has_node(p_at)) {
  958. continue;
  959. }
  960. Node *n2 = n->get_node(p_at);
  961. Node *dup = n2->duplicate(Node::DUPLICATE_SIGNALS | Node::DUPLICATE_GROUPS | Node::DUPLICATE_SCRIPTS);
  962. if (!dup) {
  963. continue;
  964. }
  965. dup->set_name(p_new_name);
  966. n2->get_parent()->add_child(dup);
  967. }
  968. }
  969. void LiveEditor::_reparent_node_func(const NodePath &p_at, const NodePath &p_new_place, const String &p_new_name, int p_at_pos) {
  970. SceneTree *scene_tree = SceneTree::get_singleton();
  971. if (!scene_tree) {
  972. return;
  973. }
  974. Node *base = nullptr;
  975. if (scene_tree->root->has_node(live_edit_root)) {
  976. base = scene_tree->root->get_node(live_edit_root);
  977. }
  978. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  979. if (!E) {
  980. return; //scene not editable
  981. }
  982. for (Node *F : E->value) {
  983. Node *n = F;
  984. if (base && !base->is_ancestor_of(n)) {
  985. continue;
  986. }
  987. if (!n->has_node(p_at)) {
  988. continue;
  989. }
  990. Node *nfrom = n->get_node(p_at);
  991. if (!n->has_node(p_new_place)) {
  992. continue;
  993. }
  994. Node *nto = n->get_node(p_new_place);
  995. nfrom->get_parent()->remove_child(nfrom);
  996. nfrom->set_name(p_new_name);
  997. nto->add_child(nfrom);
  998. if (p_at_pos >= 0) {
  999. nto->move_child(nfrom, p_at_pos);
  1000. }
  1001. }
  1002. }
  1003. /// RuntimeNodeSelect
  1004. RuntimeNodeSelect *RuntimeNodeSelect::get_singleton() {
  1005. return singleton;
  1006. }
  1007. RuntimeNodeSelect::~RuntimeNodeSelect() {
  1008. if (selection_list && !selection_list->is_visible()) {
  1009. memdelete(selection_list);
  1010. }
  1011. if (sbox_2d_canvas.is_valid()) {
  1012. RS::get_singleton()->free(sbox_2d_canvas);
  1013. RS::get_singleton()->free(sbox_2d_ci);
  1014. }
  1015. #ifndef _3D_DISABLED
  1016. if (sbox_3d_instance.is_valid()) {
  1017. RS::get_singleton()->free(sbox_3d_instance);
  1018. RS::get_singleton()->free(sbox_3d_instance_ofs);
  1019. RS::get_singleton()->free(sbox_3d_instance_xray);
  1020. RS::get_singleton()->free(sbox_3d_instance_xray_ofs);
  1021. }
  1022. #endif // _3D_DISABLED
  1023. }
  1024. void RuntimeNodeSelect::_setup(const Dictionary &p_settings) {
  1025. Window *root = SceneTree::get_singleton()->get_root();
  1026. ERR_FAIL_COND(root->is_connected(SceneStringName(window_input), callable_mp(this, &RuntimeNodeSelect::_root_window_input)));
  1027. root->connect(SceneStringName(window_input), callable_mp(this, &RuntimeNodeSelect::_root_window_input));
  1028. root->connect("size_changed", callable_mp(this, &RuntimeNodeSelect::_queue_selection_update), CONNECT_DEFERRED);
  1029. panner.instantiate();
  1030. panner->set_callbacks(callable_mp(this, &RuntimeNodeSelect::_pan_callback), callable_mp(this, &RuntimeNodeSelect::_zoom_callback));
  1031. ViewPanner::ControlScheme panning_scheme = (ViewPanner::ControlScheme)p_settings.get("editors/panning/2d_editor_panning_scheme", 0).operator int();
  1032. bool simple_panning = p_settings.get("editors/panning/simple_panning", false);
  1033. int pan_speed = p_settings.get("editors/panning/2d_editor_pan_speed", 20);
  1034. Array keys = p_settings.get("canvas_item_editor/pan_view", Array()).operator Array();
  1035. panner->setup(panning_scheme, DebuggerMarshalls::deserialize_key_shortcut(keys), simple_panning);
  1036. panner->setup_warped_panning(root, p_settings.get("editors/panning/warped_mouse_panning", true));
  1037. panner->set_scroll_speed(pan_speed);
  1038. /// 2D Selection Box Generation
  1039. sbox_2d_canvas = RS::get_singleton()->canvas_create();
  1040. sbox_2d_ci = RS::get_singleton()->canvas_item_create();
  1041. RS::get_singleton()->viewport_attach_canvas(root->get_viewport_rid(), sbox_2d_canvas);
  1042. RS::get_singleton()->canvas_item_set_parent(sbox_2d_ci, sbox_2d_canvas);
  1043. #ifndef _3D_DISABLED
  1044. cursor = Cursor();
  1045. /// 3D Selection Box Generation
  1046. // Copied from the Node3DEditor implementation.
  1047. // Use two AABBs to create the illusion of a slightly thicker line.
  1048. AABB aabb(Vector3(), Vector3(1, 1, 1));
  1049. // Create a x-ray (visible through solid surfaces) and standard version of the selection box.
  1050. // Both will be drawn at the same position, but with different opacity.
  1051. // This lets the user see where the selection is while still having a sense of depth.
  1052. Ref<SurfaceTool> st = memnew(SurfaceTool);
  1053. Ref<SurfaceTool> st_xray = memnew(SurfaceTool);
  1054. st->begin(Mesh::PRIMITIVE_LINES);
  1055. st_xray->begin(Mesh::PRIMITIVE_LINES);
  1056. for (int i = 0; i < 12; i++) {
  1057. Vector3 a, b;
  1058. aabb.get_edge(i, a, b);
  1059. st->add_vertex(a);
  1060. st->add_vertex(b);
  1061. st_xray->add_vertex(a);
  1062. st_xray->add_vertex(b);
  1063. }
  1064. Ref<StandardMaterial3D> mat = memnew(StandardMaterial3D);
  1065. mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  1066. mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  1067. // In the original Node3DEditor, this value would be fetched from the "editors/3d/selection_box_color" editor property,
  1068. // but since this is not accessible from here, we will just use the default value.
  1069. const Color selection_color_3d = Color(1, 0.5, 0);
  1070. mat->set_albedo(selection_color_3d);
  1071. mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  1072. st->set_material(mat);
  1073. sbox_3d_mesh = st->commit();
  1074. Ref<StandardMaterial3D> mat_xray = memnew(StandardMaterial3D);
  1075. mat_xray->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  1076. mat_xray->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  1077. mat_xray->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, true);
  1078. mat_xray->set_albedo(selection_color_3d * Color(1, 1, 1, 0.15));
  1079. mat_xray->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  1080. st_xray->set_material(mat_xray);
  1081. sbox_3d_mesh_xray = st_xray->commit();
  1082. #endif // _3D_DISABLED
  1083. SceneTree::get_singleton()->connect("process_frame", callable_mp(this, &RuntimeNodeSelect::_process_frame));
  1084. SceneTree::get_singleton()->connect("physics_frame", callable_mp(this, &RuntimeNodeSelect::_physics_frame));
  1085. // This function will be called before the root enters the tree at first when the Game view is passing its settings to
  1086. // the debugger, so queue the update for after it enters.
  1087. root->connect(SceneStringName(tree_entered), callable_mp(this, &RuntimeNodeSelect::_update_input_state), Object::CONNECT_ONE_SHOT);
  1088. }
  1089. void RuntimeNodeSelect::_node_set_type(NodeType p_type) {
  1090. node_select_type = p_type;
  1091. _update_input_state();
  1092. }
  1093. void RuntimeNodeSelect::_select_set_mode(SelectMode p_mode) {
  1094. node_select_mode = p_mode;
  1095. }
  1096. void RuntimeNodeSelect::_set_camera_override_enabled(bool p_enabled) {
  1097. camera_override = p_enabled;
  1098. if (p_enabled) {
  1099. _update_view_2d();
  1100. }
  1101. #ifndef _3D_DISABLED
  1102. if (camera_first_override) {
  1103. _reset_camera_2d();
  1104. _reset_camera_3d();
  1105. camera_first_override = false;
  1106. } else if (p_enabled) {
  1107. _update_view_2d();
  1108. SceneTree::get_singleton()->get_root()->set_camera_3d_override_transform(_get_cursor_transform());
  1109. SceneTree::get_singleton()->get_root()->set_camera_3d_override_perspective(CAMERA_BASE_FOV * cursor.fov_scale, CAMERA_ZNEAR, CAMERA_ZFAR);
  1110. }
  1111. #endif // _3D_DISABLED
  1112. }
  1113. void RuntimeNodeSelect::_root_window_input(const Ref<InputEvent> &p_event) {
  1114. Window *root = SceneTree::get_singleton()->get_root();
  1115. if (node_select_type == NODE_TYPE_NONE || (selection_list && selection_list->is_visible())) {
  1116. // Workaround for platforms that don't allow subwindows.
  1117. if (selection_list && selection_list->is_visible() && selection_list->is_embedded()) {
  1118. root->set_disable_input_override(false);
  1119. selection_list->push_input(p_event);
  1120. callable_mp(root->get_viewport(), &Viewport::set_disable_input_override).call_deferred(true);
  1121. }
  1122. return;
  1123. }
  1124. if (camera_override) {
  1125. if (node_select_type == NODE_TYPE_2D) {
  1126. if (panner->gui_input(p_event, Rect2(Vector2(), root->get_size()))) {
  1127. return;
  1128. }
  1129. } else if (node_select_type == NODE_TYPE_3D) {
  1130. #ifndef _3D_DISABLED
  1131. if (root->get_camera_3d() && _handle_3d_input(p_event)) {
  1132. return;
  1133. }
  1134. #endif // _3D_DISABLED
  1135. }
  1136. }
  1137. Ref<InputEventMouseButton> b = p_event;
  1138. if (!b.is_valid() || !b->is_pressed()) {
  1139. return;
  1140. }
  1141. list_shortcut_pressed = node_select_mode == SELECT_MODE_SINGLE && b->get_button_index() == MouseButton::RIGHT && b->is_alt_pressed();
  1142. if (list_shortcut_pressed || b->get_button_index() == MouseButton::LEFT) {
  1143. selection_position = b->get_position();
  1144. }
  1145. }
  1146. void RuntimeNodeSelect::_items_popup_index_pressed(int p_index, PopupMenu *p_popup) {
  1147. Object *obj = p_popup->get_item_metadata(p_index).get_validated_object();
  1148. if (!obj) {
  1149. return;
  1150. }
  1151. Array message;
  1152. message.append(obj->get_instance_id());
  1153. EngineDebugger::get_singleton()->send_message("remote_node_clicked", message);
  1154. }
  1155. void RuntimeNodeSelect::_update_input_state() {
  1156. SceneTree *scene_tree = SceneTree::get_singleton();
  1157. // This function can be called at the very beginning, when the root hasn't entered the tree yet.
  1158. // So check first to avoid a crash.
  1159. if (!scene_tree->get_root()->is_inside_tree()) {
  1160. return;
  1161. }
  1162. bool disable_input = scene_tree->is_suspended() || node_select_type != RuntimeNodeSelect::NODE_TYPE_NONE;
  1163. Input::get_singleton()->set_disable_input(disable_input);
  1164. Input::get_singleton()->set_mouse_mode_override_enabled(disable_input);
  1165. scene_tree->get_root()->set_disable_input_override(disable_input);
  1166. }
  1167. void RuntimeNodeSelect::_process_frame() {
  1168. #ifndef _3D_DISABLED
  1169. if (camera_freelook) {
  1170. Transform3D transform = _get_cursor_transform();
  1171. Vector3 forward = transform.basis.xform(Vector3(0, 0, -1));
  1172. const Vector3 right = transform.basis.xform(Vector3(1, 0, 0));
  1173. Vector3 up = transform.basis.xform(Vector3(0, 1, 0));
  1174. Vector3 direction;
  1175. Input *input = Input::get_singleton();
  1176. bool was_input_disabled = input->is_input_disabled();
  1177. if (was_input_disabled) {
  1178. input->set_disable_input(false);
  1179. }
  1180. if (input->is_physical_key_pressed(Key::A)) {
  1181. direction -= right;
  1182. }
  1183. if (input->is_physical_key_pressed(Key::D)) {
  1184. direction += right;
  1185. }
  1186. if (input->is_physical_key_pressed(Key::W)) {
  1187. direction += forward;
  1188. }
  1189. if (input->is_physical_key_pressed(Key::S)) {
  1190. direction -= forward;
  1191. }
  1192. if (input->is_physical_key_pressed(Key::E)) {
  1193. direction += up;
  1194. }
  1195. if (input->is_physical_key_pressed(Key::Q)) {
  1196. direction -= up;
  1197. }
  1198. real_t speed = FREELOOK_BASE_SPEED;
  1199. if (input->is_physical_key_pressed(Key::SHIFT)) {
  1200. speed *= 3.0;
  1201. }
  1202. if (input->is_physical_key_pressed(Key::ALT)) {
  1203. speed *= 0.333333;
  1204. }
  1205. if (was_input_disabled) {
  1206. input->set_disable_input(true);
  1207. }
  1208. if (direction != Vector3()) {
  1209. // Calculate the process time manually, as the time scale is frozen.
  1210. const double process_time = (1.0 / Engine::get_singleton()->get_frames_per_second()) * Engine::get_singleton()->get_unfrozen_time_scale();
  1211. const Vector3 motion = direction * speed * process_time;
  1212. cursor.pos += motion;
  1213. cursor.eye_pos += motion;
  1214. SceneTree::get_singleton()->get_root()->set_camera_3d_override_transform(_get_cursor_transform());
  1215. }
  1216. }
  1217. #endif // _3D_DISABLED
  1218. if (selection_update_queued || !SceneTree::get_singleton()->is_suspended()) {
  1219. selection_update_queued = false;
  1220. if (has_selection) {
  1221. _update_selection();
  1222. }
  1223. }
  1224. }
  1225. void RuntimeNodeSelect::_physics_frame() {
  1226. if (!Math::is_inf(selection_position.x) || !Math::is_inf(selection_position.y)) {
  1227. _click_point();
  1228. selection_position = Point2(INFINITY, INFINITY);
  1229. }
  1230. }
  1231. void RuntimeNodeSelect::_click_point() {
  1232. Window *root = SceneTree::get_singleton()->get_root();
  1233. Point2 pos = root->get_screen_transform().affine_inverse().xform(selection_position);
  1234. Vector<SelectResult> items;
  1235. if (node_select_type == NODE_TYPE_2D) {
  1236. for (int i = 0; i < root->get_child_count(); i++) {
  1237. _find_canvas_items_at_pos(pos, root->get_child(i), items);
  1238. }
  1239. // Remove possible duplicates.
  1240. for (int i = 0; i < items.size(); i++) {
  1241. Node *item = items[i].item;
  1242. for (int j = 0; j < i; j++) {
  1243. if (items[j].item == item) {
  1244. items.remove_at(i);
  1245. i--;
  1246. break;
  1247. }
  1248. }
  1249. }
  1250. } else if (node_select_type == NODE_TYPE_3D) {
  1251. #ifndef _3D_DISABLED
  1252. _find_3d_items_at_pos(pos, items);
  1253. #endif // _3D_DISABLED
  1254. }
  1255. if (items.is_empty()) {
  1256. return;
  1257. }
  1258. items.sort();
  1259. if ((!list_shortcut_pressed && node_select_mode == SELECT_MODE_SINGLE) || items.size() == 1) {
  1260. Array message;
  1261. message.append(items[0].item->get_instance_id());
  1262. EngineDebugger::get_singleton()->send_message("remote_node_clicked", message);
  1263. } else if (list_shortcut_pressed || node_select_mode == SELECT_MODE_LIST) {
  1264. if (!selection_list) {
  1265. _open_selection_list(items, pos);
  1266. }
  1267. }
  1268. }
  1269. void RuntimeNodeSelect::_select_node(Node *p_node) {
  1270. if (p_node == selected_node) {
  1271. return;
  1272. }
  1273. _clear_selection();
  1274. CanvasItem *ci = Object::cast_to<CanvasItem>(p_node);
  1275. if (ci) {
  1276. selected_node = p_node;
  1277. } else {
  1278. #ifndef _3D_DISABLED
  1279. Node3D *node_3d = Object::cast_to<Node3D>(p_node);
  1280. if (node_3d) {
  1281. if (!node_3d->is_inside_world()) {
  1282. return;
  1283. }
  1284. selected_node = p_node;
  1285. sbox_3d_instance = RS::get_singleton()->instance_create2(sbox_3d_mesh->get_rid(), node_3d->get_world_3d()->get_scenario());
  1286. sbox_3d_instance_ofs = RS::get_singleton()->instance_create2(sbox_3d_mesh->get_rid(), node_3d->get_world_3d()->get_scenario());
  1287. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(sbox_3d_instance, RS::SHADOW_CASTING_SETTING_OFF);
  1288. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(sbox_3d_instance_ofs, RS::SHADOW_CASTING_SETTING_OFF);
  1289. RS::get_singleton()->instance_geometry_set_flag(sbox_3d_instance, RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true);
  1290. RS::get_singleton()->instance_geometry_set_flag(sbox_3d_instance, RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false);
  1291. RS::get_singleton()->instance_geometry_set_flag(sbox_3d_instance_ofs, RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true);
  1292. RS::get_singleton()->instance_geometry_set_flag(sbox_3d_instance_ofs, RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false);
  1293. sbox_3d_instance_xray = RS::get_singleton()->instance_create2(sbox_3d_mesh_xray->get_rid(), node_3d->get_world_3d()->get_scenario());
  1294. sbox_3d_instance_xray_ofs = RS::get_singleton()->instance_create2(sbox_3d_mesh_xray->get_rid(), node_3d->get_world_3d()->get_scenario());
  1295. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(sbox_3d_instance_xray, RS::SHADOW_CASTING_SETTING_OFF);
  1296. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(sbox_3d_instance_xray_ofs, RS::SHADOW_CASTING_SETTING_OFF);
  1297. RS::get_singleton()->instance_geometry_set_flag(sbox_3d_instance_xray, RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true);
  1298. RS::get_singleton()->instance_geometry_set_flag(sbox_3d_instance_xray, RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false);
  1299. RS::get_singleton()->instance_geometry_set_flag(sbox_3d_instance_xray_ofs, RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true);
  1300. RS::get_singleton()->instance_geometry_set_flag(sbox_3d_instance_xray_ofs, RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false);
  1301. }
  1302. #endif // _3D_DISABLED
  1303. }
  1304. has_selection = selected_node;
  1305. _queue_selection_update();
  1306. }
  1307. void RuntimeNodeSelect::_queue_selection_update() {
  1308. if (has_selection && selection_visible) {
  1309. if (SceneTree::get_singleton()->is_suspended()) {
  1310. _update_selection();
  1311. } else {
  1312. selection_update_queued = true;
  1313. }
  1314. }
  1315. }
  1316. void RuntimeNodeSelect::_update_selection() {
  1317. if (has_selection && (!selected_node || !selected_node->is_inside_tree())) {
  1318. _clear_selection();
  1319. return;
  1320. }
  1321. CanvasItem *ci = Object::cast_to<CanvasItem>(selected_node);
  1322. if (ci) {
  1323. Window *root = SceneTree::get_singleton()->get_root();
  1324. Transform2D xform;
  1325. if (root->is_canvas_transform_override_enabled() && !ci->get_canvas_layer_node()) {
  1326. RS::get_singleton()->canvas_item_set_transform(sbox_2d_ci, (root->get_canvas_transform_override()));
  1327. xform = ci->get_global_transform();
  1328. } else {
  1329. RS::get_singleton()->canvas_item_set_transform(sbox_2d_ci, Transform2D());
  1330. xform = ci->get_global_transform_with_canvas();
  1331. }
  1332. // Fallback.
  1333. Rect2 rect = Rect2(Vector2(), Vector2(10, 10));
  1334. if (ci->_edit_use_rect()) {
  1335. rect = ci->_edit_get_rect();
  1336. } else {
  1337. CollisionShape2D *collision_shape = Object::cast_to<CollisionShape2D>(ci);
  1338. if (collision_shape) {
  1339. Ref<Shape2D> shape = collision_shape->get_shape();
  1340. if (shape.is_valid()) {
  1341. rect = shape->get_rect();
  1342. }
  1343. }
  1344. }
  1345. RS::get_singleton()->canvas_item_set_visible(sbox_2d_ci, selection_visible);
  1346. if (xform == sbox_2d_xform && rect == sbox_2d_rect) {
  1347. return; // Nothing changed.
  1348. }
  1349. sbox_2d_xform = xform;
  1350. sbox_2d_rect = rect;
  1351. RS::get_singleton()->canvas_item_clear(sbox_2d_ci);
  1352. const Vector2 endpoints[4] = {
  1353. xform.xform(rect.position),
  1354. xform.xform(rect.position + Vector2(rect.size.x, 0)),
  1355. xform.xform(rect.position + rect.size),
  1356. xform.xform(rect.position + Vector2(0, rect.size.y))
  1357. };
  1358. const Color selection_color_2d = Color(1, 0.6, 0.4, 0.7);
  1359. for (int i = 0; i < 4; i++) {
  1360. RS::get_singleton()->canvas_item_add_line(sbox_2d_ci, endpoints[i], endpoints[(i + 1) % 4], selection_color_2d, Math::round(2.f));
  1361. }
  1362. } else {
  1363. #ifndef _3D_DISABLED
  1364. Node3D *node_3d = Object::cast_to<Node3D>(selected_node);
  1365. // Fallback.
  1366. AABB bounds(Vector3(-0.5, -0.5, -0.5), Vector3(1, 1, 1));
  1367. VisualInstance3D *visual_instance = Object::cast_to<VisualInstance3D>(node_3d);
  1368. if (visual_instance) {
  1369. bounds = visual_instance->get_aabb();
  1370. } else {
  1371. CollisionShape3D *collision_shape = Object::cast_to<CollisionShape3D>(node_3d);
  1372. if (collision_shape) {
  1373. Ref<Shape3D> shape = collision_shape->get_shape();
  1374. if (shape.is_valid()) {
  1375. bounds = shape->get_debug_mesh()->get_aabb();
  1376. }
  1377. }
  1378. }
  1379. RS::get_singleton()->instance_set_visible(sbox_3d_instance, selection_visible);
  1380. RS::get_singleton()->instance_set_visible(sbox_3d_instance_ofs, selection_visible);
  1381. RS::get_singleton()->instance_set_visible(sbox_3d_instance_xray, selection_visible);
  1382. RS::get_singleton()->instance_set_visible(sbox_3d_instance_xray_ofs, selection_visible);
  1383. Transform3D xform_to_top_level_parent_space = node_3d->get_global_transform().affine_inverse() * node_3d->get_global_transform();
  1384. bounds = xform_to_top_level_parent_space.xform(bounds);
  1385. Transform3D t = node_3d->get_global_transform();
  1386. if (t == sbox_3d_xform && bounds == sbox_3d_bounds) {
  1387. return; // Nothing changed.
  1388. }
  1389. sbox_3d_xform = t;
  1390. sbox_3d_bounds = bounds;
  1391. Transform3D t_offset = t;
  1392. // Apply AABB scaling before item's global transform.
  1393. {
  1394. const Vector3 offset(0.005, 0.005, 0.005);
  1395. Basis aabb_s;
  1396. aabb_s.scale(bounds.size + offset);
  1397. t.translate_local(bounds.position - offset / 2);
  1398. t.basis = t.basis * aabb_s;
  1399. }
  1400. {
  1401. const Vector3 offset(0.01, 0.01, 0.01);
  1402. Basis aabb_s;
  1403. aabb_s.scale(bounds.size + offset);
  1404. t_offset.translate_local(bounds.position - offset / 2);
  1405. t_offset.basis = t_offset.basis * aabb_s;
  1406. }
  1407. RS::get_singleton()->instance_set_transform(sbox_3d_instance, t);
  1408. RS::get_singleton()->instance_set_transform(sbox_3d_instance_ofs, t_offset);
  1409. RS::get_singleton()->instance_set_transform(sbox_3d_instance_xray, t);
  1410. RS::get_singleton()->instance_set_transform(sbox_3d_instance_xray_ofs, t_offset);
  1411. #endif // _3D_DISABLED
  1412. }
  1413. }
  1414. void RuntimeNodeSelect::_clear_selection() {
  1415. selected_node = nullptr;
  1416. has_selection = false;
  1417. if (sbox_2d_canvas.is_valid()) {
  1418. RS::get_singleton()->canvas_item_clear(sbox_2d_ci);
  1419. }
  1420. #ifndef _3D_DISABLED
  1421. if (sbox_3d_instance.is_valid()) {
  1422. RS::get_singleton()->free(sbox_3d_instance);
  1423. RS::get_singleton()->free(sbox_3d_instance_ofs);
  1424. RS::get_singleton()->free(sbox_3d_instance_xray);
  1425. RS::get_singleton()->free(sbox_3d_instance_xray_ofs);
  1426. }
  1427. #endif // _3D_DISABLED
  1428. }
  1429. void RuntimeNodeSelect::_open_selection_list(const Vector<SelectResult> &p_items, const Point2 &p_pos) {
  1430. Window *root = SceneTree::get_singleton()->get_root();
  1431. selection_list = memnew(PopupMenu);
  1432. selection_list->set_theme(ThemeDB::get_singleton()->get_default_theme());
  1433. selection_list->set_auto_translate_mode(Node::AUTO_TRANSLATE_MODE_DISABLED);
  1434. selection_list->set_force_native(true);
  1435. selection_list->connect("index_pressed", callable_mp(this, &RuntimeNodeSelect::_items_popup_index_pressed).bind(selection_list));
  1436. selection_list->connect("popup_hide", callable_mp(this, &RuntimeNodeSelect::_close_selection_list));
  1437. root->add_child(selection_list);
  1438. for (const SelectResult &I : p_items) {
  1439. selection_list->add_item(I.item->get_name());
  1440. selection_list->set_item_metadata(-1, I.item);
  1441. }
  1442. selection_list->set_position(selection_list->is_embedded() ? p_pos : selection_position + root->get_position());
  1443. selection_list->reset_size();
  1444. selection_list->popup();
  1445. // FIXME: Ugly hack that stops the popup from hiding when the button is released.
  1446. selection_list->call_deferred(SNAME("set_position"), selection_list->get_position() + Point2(1, 0));
  1447. }
  1448. void RuntimeNodeSelect::_close_selection_list() {
  1449. selection_list->queue_free();
  1450. selection_list = nullptr;
  1451. }
  1452. void RuntimeNodeSelect::_set_selection_visible(bool p_visible) {
  1453. selection_visible = p_visible;
  1454. if (has_selection) {
  1455. _update_selection();
  1456. }
  1457. }
  1458. // Copied and trimmed from the CanvasItemEditor implementation.
  1459. 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) {
  1460. if (!p_node || Object::cast_to<Viewport>(p_node)) {
  1461. return;
  1462. }
  1463. // In the original CanvasItemEditor, this value would be fetched from the "editors/polygon_editor/point_grab_radius" editor property,
  1464. // but since this is not accessible from here, we will just use the default value.
  1465. const real_t grab_distance = 8;
  1466. CanvasItem *ci = Object::cast_to<CanvasItem>(p_node);
  1467. for (int i = p_node->get_child_count() - 1; i >= 0; i--) {
  1468. if (ci) {
  1469. if (!ci->is_set_as_top_level()) {
  1470. _find_canvas_items_at_pos(p_pos, p_node->get_child(i), r_items, p_parent_xform * ci->get_transform(), p_canvas_xform);
  1471. } else {
  1472. _find_canvas_items_at_pos(p_pos, p_node->get_child(i), r_items, ci->get_transform(), p_canvas_xform);
  1473. }
  1474. } else {
  1475. CanvasLayer *cl = Object::cast_to<CanvasLayer>(p_node);
  1476. _find_canvas_items_at_pos(p_pos, p_node->get_child(i), r_items, Transform2D(), cl ? cl->get_transform() : p_canvas_xform);
  1477. }
  1478. }
  1479. if (ci && ci->is_visible_in_tree()) {
  1480. Transform2D xform = p_canvas_xform;
  1481. if (!ci->is_set_as_top_level()) {
  1482. xform *= p_parent_xform;
  1483. }
  1484. Vector2 pos;
  1485. // Cameras (overridden or not) don't affect `CanvasLayer`s.
  1486. if (!ci->get_canvas_layer_node()) {
  1487. Window *root = SceneTree::get_singleton()->get_root();
  1488. pos = (root->is_canvas_transform_override_enabled() ? root->get_canvas_transform_override() : root->get_canvas_transform()).affine_inverse().xform(p_pos);
  1489. } else {
  1490. pos = p_pos;
  1491. }
  1492. xform = (xform * ci->get_transform()).affine_inverse();
  1493. const real_t local_grab_distance = xform.basis_xform(Vector2(grab_distance, 0)).length() / view_2d_zoom;
  1494. if (ci->_edit_is_selected_on_click(xform.xform(pos), local_grab_distance)) {
  1495. SelectResult res;
  1496. res.item = ci;
  1497. res.order = ci->get_effective_z_index() + ci->get_canvas_layer();
  1498. r_items.push_back(res);
  1499. // If it's a shape, get the collision object it's from.
  1500. // FIXME: If the collision object has multiple shapes, only the topmost will be above it in the list.
  1501. if (Object::cast_to<CollisionShape2D>(ci) || Object::cast_to<CollisionPolygon2D>(ci)) {
  1502. CollisionObject2D *collision_object = Object::cast_to<CollisionObject2D>(ci->get_parent());
  1503. if (collision_object) {
  1504. SelectResult res_col;
  1505. res_col.item = ci->get_parent();
  1506. res_col.order = collision_object->get_z_index() + ci->get_canvas_layer();
  1507. r_items.push_back(res_col);
  1508. }
  1509. }
  1510. }
  1511. }
  1512. }
  1513. void RuntimeNodeSelect::_pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event) {
  1514. view_2d_offset.x -= p_scroll_vec.x / view_2d_zoom;
  1515. view_2d_offset.y -= p_scroll_vec.y / view_2d_zoom;
  1516. _update_view_2d();
  1517. }
  1518. // A very shallow copy of the same function inside CanvasItemEditor.
  1519. void RuntimeNodeSelect::_zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event) {
  1520. real_t prev_zoom = view_2d_zoom;
  1521. view_2d_zoom = CLAMP(view_2d_zoom * p_zoom_factor, VIEW_2D_MIN_ZOOM, VIEW_2D_MAX_ZOOM);
  1522. Vector2 pos = SceneTree::get_singleton()->get_root()->get_screen_transform().affine_inverse().xform(p_origin);
  1523. view_2d_offset += pos / prev_zoom - pos / view_2d_zoom;
  1524. // We want to align in-scene pixels to screen pixels, this prevents blurry rendering
  1525. // of small details (texts, lines).
  1526. // This correction adds a jitter movement when zooming, so we correct only when the
  1527. // zoom factor is an integer. (in the other cases, all pixels won't be aligned anyway)
  1528. const real_t closest_zoom_factor = Math::round(view_2d_zoom);
  1529. if (Math::is_zero_approx(view_2d_zoom - closest_zoom_factor)) {
  1530. // Make sure scene pixel at view_offset is aligned on a screen pixel.
  1531. Vector2 view_offset_int = view_2d_offset.floor();
  1532. Vector2 view_offset_frac = view_2d_offset - view_offset_int;
  1533. view_2d_offset = view_offset_int + (view_offset_frac * closest_zoom_factor).round() / closest_zoom_factor;
  1534. }
  1535. _update_view_2d();
  1536. }
  1537. void RuntimeNodeSelect::_reset_camera_2d() {
  1538. view_2d_offset = -SceneTree::get_singleton()->get_root()->get_canvas_transform().get_origin();
  1539. view_2d_zoom = 1;
  1540. _update_view_2d();
  1541. }
  1542. void RuntimeNodeSelect::_update_view_2d() {
  1543. Transform2D transform = Transform2D();
  1544. transform.scale_basis(Size2(view_2d_zoom, view_2d_zoom));
  1545. transform.columns[2] = -view_2d_offset * view_2d_zoom;
  1546. SceneTree::get_singleton()->get_root()->set_canvas_transform_override(transform);
  1547. _queue_selection_update();
  1548. }
  1549. #ifndef _3D_DISABLED
  1550. void RuntimeNodeSelect::_find_3d_items_at_pos(const Point2 &p_pos, Vector<SelectResult> &r_items) {
  1551. Window *root = SceneTree::get_singleton()->get_root();
  1552. Camera3D *camera = root->get_viewport()->get_camera_3d();
  1553. if (!camera) {
  1554. return;
  1555. }
  1556. Vector3 ray, pos, to;
  1557. if (root->get_viewport()->is_camera_3d_override_enabled()) {
  1558. Viewport *vp = root->get_viewport();
  1559. ray = vp->camera_3d_override_project_ray_normal(p_pos);
  1560. pos = vp->camera_3d_override_project_ray_origin(p_pos);
  1561. to = pos + ray * vp->get_camera_3d_override_properties()["z_far"];
  1562. } else {
  1563. ray = camera->project_ray_normal(p_pos);
  1564. pos = camera->project_ray_origin(p_pos);
  1565. to = pos + ray * camera->get_far();
  1566. }
  1567. // Start with physical objects.
  1568. PhysicsDirectSpaceState3D *ss = root->get_world_3d()->get_direct_space_state();
  1569. PhysicsDirectSpaceState3D::RayResult result;
  1570. HashSet<RID> excluded;
  1571. PhysicsDirectSpaceState3D::RayParameters ray_params;
  1572. ray_params.from = pos;
  1573. ray_params.to = to;
  1574. ray_params.collide_with_areas = true;
  1575. while (true) {
  1576. ray_params.exclude = excluded;
  1577. if (ss->intersect_ray(ray_params, result)) {
  1578. SelectResult res;
  1579. res.item = Object::cast_to<Node>(result.collider);
  1580. res.order = -pos.distance_to(Object::cast_to<Node3D>(res.item)->get_global_transform().xform(result.position));
  1581. // Fetch collision shapes.
  1582. CollisionObject3D *collision = Object::cast_to<CollisionObject3D>(result.collider);
  1583. if (collision) {
  1584. List<uint32_t> owners;
  1585. collision->get_shape_owners(&owners);
  1586. for (const uint32_t &I : owners) {
  1587. SelectResult res_shape;
  1588. res_shape.item = Object::cast_to<Node>(collision->shape_owner_get_owner(I));
  1589. res_shape.order = res.order;
  1590. r_items.push_back(res_shape);
  1591. }
  1592. }
  1593. r_items.push_back(res);
  1594. excluded.insert(result.rid);
  1595. } else {
  1596. break;
  1597. }
  1598. }
  1599. // Then go for the meshes.
  1600. Vector<ObjectID> items = RS::get_singleton()->instances_cull_ray(pos, to, root->get_world_3d()->get_scenario());
  1601. for (int i = 0; i < items.size(); i++) {
  1602. Object *obj = ObjectDB::get_instance(items[i]);
  1603. GeometryInstance3D *geo_instance = nullptr;
  1604. Ref<TriangleMesh> mesh_collision;
  1605. MeshInstance3D *mesh_instance = Object::cast_to<MeshInstance3D>(obj);
  1606. if (mesh_instance) {
  1607. if (mesh_instance->get_mesh().is_valid()) {
  1608. geo_instance = mesh_instance;
  1609. mesh_collision = mesh_instance->get_mesh()->generate_triangle_mesh();
  1610. }
  1611. } else {
  1612. Label3D *label = Object::cast_to<Label3D>(obj);
  1613. if (label) {
  1614. geo_instance = label;
  1615. mesh_collision = label->generate_triangle_mesh();
  1616. } else {
  1617. Sprite3D *sprite = Object::cast_to<Sprite3D>(obj);
  1618. if (sprite) {
  1619. geo_instance = sprite;
  1620. mesh_collision = sprite->generate_triangle_mesh();
  1621. }
  1622. }
  1623. }
  1624. if (mesh_collision.is_valid()) {
  1625. Transform3D gt = geo_instance->get_global_transform();
  1626. Transform3D ai = gt.affine_inverse();
  1627. Vector3 point, normal;
  1628. if (mesh_collision->intersect_ray(ai.xform(pos), ai.basis.xform(ray).normalized(), point, normal)) {
  1629. SelectResult res;
  1630. res.item = Object::cast_to<Node>(obj);
  1631. res.order = -pos.distance_to(gt.xform(point));
  1632. r_items.push_back(res);
  1633. continue;
  1634. }
  1635. }
  1636. items.remove_at(i);
  1637. i--;
  1638. }
  1639. }
  1640. bool RuntimeNodeSelect::_handle_3d_input(const Ref<InputEvent> &p_event) {
  1641. Ref<InputEventMouseButton> b = p_event;
  1642. if (b.is_valid()) {
  1643. const real_t zoom_factor = 1.08 * b->get_factor();
  1644. switch (b->get_button_index()) {
  1645. case MouseButton::WHEEL_UP: {
  1646. if (!camera_freelook) {
  1647. _cursor_scale_distance(1.0 / zoom_factor);
  1648. }
  1649. return true;
  1650. } break;
  1651. case MouseButton::WHEEL_DOWN: {
  1652. if (!camera_freelook) {
  1653. _cursor_scale_distance(zoom_factor);
  1654. }
  1655. return true;
  1656. } break;
  1657. case MouseButton::RIGHT: {
  1658. _set_camera_freelook_enabled(b->is_pressed());
  1659. return true;
  1660. } break;
  1661. default: {
  1662. }
  1663. }
  1664. }
  1665. Ref<InputEventMouseMotion> m = p_event;
  1666. if (m.is_valid()) {
  1667. if (camera_freelook) {
  1668. _cursor_look(m);
  1669. } else if (m->get_button_mask().has_flag(MouseButtonMask::MIDDLE)) {
  1670. if (m->is_shift_pressed()) {
  1671. _cursor_pan(m);
  1672. } else {
  1673. _cursor_orbit(m);
  1674. }
  1675. }
  1676. return true;
  1677. }
  1678. Ref<InputEventKey> k = p_event;
  1679. if (k.is_valid()) {
  1680. if (k->get_physical_keycode() == Key::ESCAPE) {
  1681. _set_camera_freelook_enabled(false);
  1682. return true;
  1683. } else if (k->is_ctrl_pressed()) {
  1684. switch (k->get_physical_keycode()) {
  1685. case Key::EQUAL: {
  1686. cursor.fov_scale = CLAMP(cursor.fov_scale - 0.05, CAMERA_MIN_FOV_SCALE, CAMERA_MAX_FOV_SCALE);
  1687. SceneTree::get_singleton()->get_root()->set_camera_3d_override_perspective(CAMERA_BASE_FOV * cursor.fov_scale, CAMERA_ZNEAR, CAMERA_ZFAR);
  1688. return true;
  1689. } break;
  1690. case Key::MINUS: {
  1691. cursor.fov_scale = CLAMP(cursor.fov_scale + 0.05, CAMERA_MIN_FOV_SCALE, CAMERA_MAX_FOV_SCALE);
  1692. SceneTree::get_singleton()->get_root()->set_camera_3d_override_perspective(CAMERA_BASE_FOV * cursor.fov_scale, CAMERA_ZNEAR, CAMERA_ZFAR);
  1693. return true;
  1694. } break;
  1695. case Key::KEY_0: {
  1696. cursor.fov_scale = 1;
  1697. SceneTree::get_singleton()->get_root()->set_camera_3d_override_perspective(CAMERA_BASE_FOV, CAMERA_ZNEAR, CAMERA_ZFAR);
  1698. return true;
  1699. } break;
  1700. default: {
  1701. }
  1702. }
  1703. }
  1704. }
  1705. // TODO: Handle magnify and pan input gestures.
  1706. return false;
  1707. }
  1708. void RuntimeNodeSelect::_set_camera_freelook_enabled(bool p_enabled) {
  1709. camera_freelook = p_enabled;
  1710. if (p_enabled) {
  1711. // Make sure eye_pos is synced, because freelook referential is eye pos rather than orbit pos
  1712. Vector3 forward = _get_cursor_transform().basis.xform(Vector3(0, 0, -1));
  1713. cursor.eye_pos = cursor.pos - cursor.distance * forward;
  1714. previous_mouse_position = SceneTree::get_singleton()->get_root()->get_mouse_position();
  1715. // Hide mouse like in an FPS (warping doesn't work).
  1716. Input::get_singleton()->set_mouse_mode_override(Input::MOUSE_MODE_CAPTURED);
  1717. } else {
  1718. // Restore mouse.
  1719. Input::get_singleton()->set_mouse_mode_override(Input::MOUSE_MODE_VISIBLE);
  1720. // Restore the previous mouse position when leaving freelook mode.
  1721. // This is done because leaving `Input.MOUSE_MODE_CAPTURED` will center the cursor
  1722. // due to OS limitations.
  1723. Input::get_singleton()->warp_mouse(previous_mouse_position);
  1724. }
  1725. }
  1726. void RuntimeNodeSelect::_cursor_scale_distance(real_t p_scale) {
  1727. real_t min_distance = MAX(CAMERA_ZNEAR * 4, VIEW_3D_MIN_ZOOM);
  1728. real_t max_distance = MIN(CAMERA_ZFAR / 4, VIEW_3D_MAX_ZOOM);
  1729. cursor.distance = CLAMP(cursor.distance * p_scale, min_distance, max_distance);
  1730. SceneTree::get_singleton()->get_root()->set_camera_3d_override_transform(_get_cursor_transform());
  1731. }
  1732. void RuntimeNodeSelect::_cursor_look(Ref<InputEventWithModifiers> p_event) {
  1733. Window *root = SceneTree::get_singleton()->get_root();
  1734. const Vector2 relative = Input::get_singleton()->warp_mouse_motion(p_event, Rect2(Vector2(), root->get_size()));
  1735. const Transform3D prev_camera_transform = _get_cursor_transform();
  1736. cursor.x_rot += relative.y * RADS_PER_PIXEL;
  1737. // Clamp the Y rotation to roughly -90..90 degrees so the user can't look upside-down and end up disoriented.
  1738. cursor.x_rot = CLAMP(cursor.x_rot, -1.57, 1.57);
  1739. cursor.y_rot += relative.x * RADS_PER_PIXEL;
  1740. // Look is like the opposite of Orbit: the focus point rotates around the camera.
  1741. Transform3D camera_transform = _get_cursor_transform();
  1742. Vector3 pos = camera_transform.xform(Vector3(0, 0, 0));
  1743. Vector3 prev_pos = prev_camera_transform.xform(Vector3(0, 0, 0));
  1744. Vector3 diff = prev_pos - pos;
  1745. cursor.pos += diff;
  1746. SceneTree::get_singleton()->get_root()->set_camera_3d_override_transform(_get_cursor_transform());
  1747. }
  1748. void RuntimeNodeSelect::_cursor_pan(Ref<InputEventWithModifiers> p_event) {
  1749. Window *root = SceneTree::get_singleton()->get_root();
  1750. // Reduce all sides of the area by 1, so warping works when windows are maximized/fullscreen.
  1751. const Vector2 relative = Input::get_singleton()->warp_mouse_motion(p_event, Rect2(Vector2(1, 1), root->get_size() - Vector2(2, 2)));
  1752. const real_t pan_speed = 1 / 150.0;
  1753. Transform3D camera_transform;
  1754. camera_transform.translate_local(cursor.pos);
  1755. camera_transform.basis.rotate(Vector3(1, 0, 0), -cursor.x_rot);
  1756. camera_transform.basis.rotate(Vector3(0, 1, 0), -cursor.y_rot);
  1757. Vector3 translation(1 * -relative.x * pan_speed, relative.y * pan_speed, 0);
  1758. translation *= cursor.distance / 4;
  1759. camera_transform.translate_local(translation);
  1760. cursor.pos = camera_transform.origin;
  1761. SceneTree::get_singleton()->get_root()->set_camera_3d_override_transform(_get_cursor_transform());
  1762. }
  1763. void RuntimeNodeSelect::_cursor_orbit(Ref<InputEventWithModifiers> p_event) {
  1764. Window *root = SceneTree::get_singleton()->get_root();
  1765. // Reduce all sides of the area by 1, so warping works when windows are maximized/fullscreen.
  1766. const Vector2 relative = Input::get_singleton()->warp_mouse_motion(p_event, Rect2(Vector2(1, 1), root->get_size() - Vector2(2, 2)));
  1767. cursor.x_rot += relative.y * RADS_PER_PIXEL;
  1768. // Clamp the Y rotation to roughly -90..90 degrees so the user can't look upside-down and end up disoriented.
  1769. cursor.x_rot = CLAMP(cursor.x_rot, -1.57, 1.57);
  1770. cursor.y_rot += relative.x * RADS_PER_PIXEL;
  1771. SceneTree::get_singleton()->get_root()->set_camera_3d_override_transform(_get_cursor_transform());
  1772. }
  1773. Transform3D RuntimeNodeSelect::_get_cursor_transform() {
  1774. Transform3D camera_transform;
  1775. camera_transform.translate_local(cursor.pos);
  1776. camera_transform.basis.rotate(Vector3(1, 0, 0), -cursor.x_rot);
  1777. camera_transform.basis.rotate(Vector3(0, 1, 0), -cursor.y_rot);
  1778. camera_transform.translate_local(0, 0, cursor.distance);
  1779. return camera_transform;
  1780. }
  1781. void RuntimeNodeSelect::_reset_camera_3d() {
  1782. camera_first_override = true;
  1783. Window *root = SceneTree::get_singleton()->get_root();
  1784. Camera3D *camera = root->get_camera_3d();
  1785. if (!camera) {
  1786. return;
  1787. }
  1788. cursor = Cursor();
  1789. Transform3D transform = camera->get_global_transform();
  1790. transform.translate_local(0, 0, -cursor.distance);
  1791. cursor.pos = transform.origin;
  1792. cursor.x_rot = -camera->get_global_rotation().x;
  1793. cursor.y_rot = -camera->get_global_rotation().y;
  1794. cursor.fov_scale = CLAMP(camera->get_fov() / CAMERA_BASE_FOV, CAMERA_MIN_FOV_SCALE, CAMERA_MAX_FOV_SCALE);
  1795. SceneTree::get_singleton()->get_root()->set_camera_3d_override_transform(_get_cursor_transform());
  1796. SceneTree::get_singleton()->get_root()->set_camera_3d_override_perspective(CAMERA_BASE_FOV * cursor.fov_scale, CAMERA_ZNEAR, CAMERA_ZFAR);
  1797. }
  1798. #endif // _3D_DISABLED
  1799. #endif