scene_debugger.cpp 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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/engine_debugger.h"
  32. #include "core/debugger/engine_profiler.h"
  33. #include "core/io/marshalls.h"
  34. #include "core/object/script_language.h"
  35. #include "core/templates/local_vector.h"
  36. #include "scene/main/scene_tree.h"
  37. #include "scene/main/window.h"
  38. #include "scene/resources/packed_scene.h"
  39. SceneDebugger *SceneDebugger::singleton = nullptr;
  40. SceneDebugger::SceneDebugger() {
  41. singleton = this;
  42. #ifdef DEBUG_ENABLED
  43. LiveEditor::singleton = memnew(LiveEditor);
  44. EngineDebugger::register_message_capture("scene", EngineDebugger::Capture(nullptr, SceneDebugger::parse_message));
  45. #endif
  46. }
  47. SceneDebugger::~SceneDebugger() {
  48. #ifdef DEBUG_ENABLED
  49. if (LiveEditor::singleton) {
  50. EngineDebugger::unregister_message_capture("scene");
  51. memdelete(LiveEditor::singleton);
  52. LiveEditor::singleton = nullptr;
  53. }
  54. #endif
  55. singleton = nullptr;
  56. }
  57. void SceneDebugger::initialize() {
  58. if (EngineDebugger::is_active()) {
  59. memnew(SceneDebugger);
  60. }
  61. }
  62. void SceneDebugger::deinitialize() {
  63. if (singleton) {
  64. memdelete(singleton);
  65. }
  66. }
  67. #ifdef DEBUG_ENABLED
  68. Error SceneDebugger::parse_message(void *p_user, const String &p_msg, const Array &p_args, bool &r_captured) {
  69. SceneTree *scene_tree = SceneTree::get_singleton();
  70. if (!scene_tree) {
  71. return ERR_UNCONFIGURED;
  72. }
  73. LiveEditor *live_editor = LiveEditor::get_singleton();
  74. if (!live_editor) {
  75. return ERR_UNCONFIGURED;
  76. }
  77. r_captured = true;
  78. if (p_msg == "request_scene_tree") { // Scene tree
  79. live_editor->_send_tree();
  80. } else if (p_msg == "save_node") { // Save node.
  81. ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA);
  82. _save_node(p_args[0], p_args[1]);
  83. } else if (p_msg == "inspect_object") { // Object Inspect
  84. ERR_FAIL_COND_V(p_args.size() < 1, ERR_INVALID_DATA);
  85. ObjectID id = p_args[0];
  86. _send_object_id(id);
  87. } else if (p_msg == "override_camera_2D:set") { // Camera
  88. ERR_FAIL_COND_V(p_args.size() < 1, ERR_INVALID_DATA);
  89. bool enforce = p_args[0];
  90. scene_tree->get_root()->enable_canvas_transform_override(enforce);
  91. } else if (p_msg == "override_camera_2D:transform") {
  92. ERR_FAIL_COND_V(p_args.size() < 1, ERR_INVALID_DATA);
  93. Transform2D transform = p_args[0];
  94. scene_tree->get_root()->set_canvas_transform_override(transform);
  95. #ifndef _3D_DISABLED
  96. } else if (p_msg == "override_camera_3D:set") {
  97. ERR_FAIL_COND_V(p_args.size() < 1, ERR_INVALID_DATA);
  98. bool enable = p_args[0];
  99. scene_tree->get_root()->enable_camera_3d_override(enable);
  100. } else if (p_msg == "override_camera_3D:transform") {
  101. ERR_FAIL_COND_V(p_args.size() < 5, ERR_INVALID_DATA);
  102. Transform3D transform = p_args[0];
  103. bool is_perspective = p_args[1];
  104. float size_or_fov = p_args[2];
  105. float near = p_args[3];
  106. float far = p_args[4];
  107. if (is_perspective) {
  108. scene_tree->get_root()->set_camera_3d_override_perspective(size_or_fov, near, far);
  109. } else {
  110. scene_tree->get_root()->set_camera_3d_override_orthogonal(size_or_fov, near, far);
  111. }
  112. scene_tree->get_root()->set_camera_3d_override_transform(transform);
  113. #endif // _3D_DISABLED
  114. } else if (p_msg == "set_object_property") {
  115. ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA);
  116. _set_object_property(p_args[0], p_args[1], p_args[2]);
  117. } else if (!p_msg.begins_with("live_")) { // Live edits below.
  118. return ERR_SKIP;
  119. } else if (p_msg == "live_set_root") {
  120. ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA);
  121. live_editor->_root_func(p_args[0], p_args[1]);
  122. } else if (p_msg == "live_node_path") {
  123. ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA);
  124. live_editor->_node_path_func(p_args[0], p_args[1]);
  125. } else if (p_msg == "live_res_path") {
  126. ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA);
  127. live_editor->_res_path_func(p_args[0], p_args[1]);
  128. } else if (p_msg == "live_node_prop_res") {
  129. ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA);
  130. live_editor->_node_set_res_func(p_args[0], p_args[1], p_args[2]);
  131. } else if (p_msg == "live_node_prop") {
  132. ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA);
  133. live_editor->_node_set_func(p_args[0], p_args[1], p_args[2]);
  134. } else if (p_msg == "live_res_prop_res") {
  135. ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA);
  136. live_editor->_res_set_res_func(p_args[0], p_args[1], p_args[2]);
  137. } else if (p_msg == "live_res_prop") {
  138. ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA);
  139. live_editor->_res_set_func(p_args[0], p_args[1], p_args[2]);
  140. } else if (p_msg == "live_node_call") {
  141. LocalVector<Variant> args;
  142. LocalVector<Variant *> argptrs;
  143. args.resize(p_args.size() - 2);
  144. argptrs.resize(args.size());
  145. for (uint32_t i = 0; i < args.size(); i++) {
  146. args[i] = p_args[i + 2];
  147. argptrs[i] = &args[i];
  148. }
  149. live_editor->_node_call_func(p_args[0], p_args[1], (const Variant **)argptrs.ptr(), argptrs.size());
  150. } else if (p_msg == "live_res_call") {
  151. ERR_FAIL_COND_V(p_args.size() < 10, ERR_INVALID_DATA);
  152. LocalVector<Variant> args;
  153. LocalVector<Variant *> argptrs;
  154. args.resize(p_args.size() - 2);
  155. argptrs.resize(args.size());
  156. for (uint32_t i = 0; i < args.size(); i++) {
  157. args[i] = p_args[i + 2];
  158. argptrs[i] = &args[i];
  159. }
  160. live_editor->_res_call_func(p_args[0], p_args[1], (const Variant **)argptrs.ptr(), argptrs.size());
  161. } else if (p_msg == "live_create_node") {
  162. ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA);
  163. live_editor->_create_node_func(p_args[0], p_args[1], p_args[2]);
  164. } else if (p_msg == "live_instantiate_node") {
  165. ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA);
  166. live_editor->_instance_node_func(p_args[0], p_args[1], p_args[2]);
  167. } else if (p_msg == "live_remove_node") {
  168. ERR_FAIL_COND_V(p_args.size() < 1, ERR_INVALID_DATA);
  169. live_editor->_remove_node_func(p_args[0]);
  170. } else if (p_msg == "live_remove_and_keep_node") {
  171. ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA);
  172. live_editor->_remove_and_keep_node_func(p_args[0], p_args[1]);
  173. } else if (p_msg == "live_restore_node") {
  174. ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA);
  175. live_editor->_restore_node_func(p_args[0], p_args[1], p_args[2]);
  176. } else if (p_msg == "live_duplicate_node") {
  177. ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA);
  178. live_editor->_duplicate_node_func(p_args[0], p_args[1]);
  179. } else if (p_msg == "live_reparent_node") {
  180. ERR_FAIL_COND_V(p_args.size() < 4, ERR_INVALID_DATA);
  181. live_editor->_reparent_node_func(p_args[0], p_args[1], p_args[2], p_args[3]);
  182. } else {
  183. r_captured = false;
  184. }
  185. return OK;
  186. }
  187. void SceneDebugger::_save_node(ObjectID id, const String &p_path) {
  188. Node *node = Object::cast_to<Node>(ObjectDB::get_instance(id));
  189. ERR_FAIL_COND(!node);
  190. #ifdef TOOLS_ENABLED
  191. HashMap<const Node *, Node *> duplimap;
  192. Node *copy = node->duplicate_from_editor(duplimap);
  193. #else
  194. Node *copy = node->duplicate();
  195. #endif
  196. // Handle Unique Nodes.
  197. for (int i = 0; i < copy->get_child_count(false); i++) {
  198. _set_node_owner_recursive(copy->get_child(i, false), copy);
  199. }
  200. // Root node cannot ever be unique name in its own Scene!
  201. copy->set_unique_name_in_owner(false);
  202. Ref<PackedScene> ps = memnew(PackedScene);
  203. ps->pack(copy);
  204. ResourceSaver::save(ps, p_path);
  205. memdelete(copy);
  206. }
  207. void SceneDebugger::_set_node_owner_recursive(Node *p_node, Node *p_owner) {
  208. if (!p_node->get_owner()) {
  209. p_node->set_owner(p_owner);
  210. }
  211. for (int i = 0; i < p_node->get_child_count(false); i++) {
  212. _set_node_owner_recursive(p_node->get_child(i, false), p_owner);
  213. }
  214. }
  215. void SceneDebugger::_send_object_id(ObjectID p_id, int p_max_size) {
  216. SceneDebuggerObject obj(p_id);
  217. if (obj.id.is_null()) {
  218. return;
  219. }
  220. Array arr;
  221. obj.serialize(arr);
  222. EngineDebugger::get_singleton()->send_message("scene:inspect_object", arr);
  223. }
  224. void SceneDebugger::_set_object_property(ObjectID p_id, const String &p_property, const Variant &p_value) {
  225. Object *obj = ObjectDB::get_instance(p_id);
  226. if (!obj) {
  227. return;
  228. }
  229. String prop_name = p_property;
  230. if (p_property.begins_with("Members/")) {
  231. Vector<String> ss = p_property.split("/");
  232. prop_name = ss[ss.size() - 1];
  233. }
  234. obj->set(prop_name, p_value);
  235. }
  236. void SceneDebugger::add_to_cache(const String &p_filename, Node *p_node) {
  237. LiveEditor *debugger = LiveEditor::get_singleton();
  238. if (!debugger) {
  239. return;
  240. }
  241. if (EngineDebugger::get_script_debugger() && !p_filename.is_empty()) {
  242. debugger->live_scene_edit_cache[p_filename].insert(p_node);
  243. }
  244. }
  245. void SceneDebugger::remove_from_cache(const String &p_filename, Node *p_node) {
  246. LiveEditor *debugger = LiveEditor::get_singleton();
  247. if (!debugger) {
  248. return;
  249. }
  250. HashMap<String, HashSet<Node *>> &edit_cache = debugger->live_scene_edit_cache;
  251. HashMap<String, HashSet<Node *>>::Iterator E = edit_cache.find(p_filename);
  252. if (E) {
  253. E->value.erase(p_node);
  254. if (E->value.size() == 0) {
  255. edit_cache.remove(E);
  256. }
  257. }
  258. HashMap<Node *, HashMap<ObjectID, Node *>> &remove_list = debugger->live_edit_remove_list;
  259. HashMap<Node *, HashMap<ObjectID, Node *>>::Iterator F = remove_list.find(p_node);
  260. if (F) {
  261. for (const KeyValue<ObjectID, Node *> &G : F->value) {
  262. memdelete(G.value);
  263. }
  264. remove_list.remove(F);
  265. }
  266. }
  267. /// SceneDebuggerObject
  268. SceneDebuggerObject::SceneDebuggerObject(ObjectID p_id) {
  269. id = ObjectID();
  270. Object *obj = ObjectDB::get_instance(p_id);
  271. if (!obj) {
  272. return;
  273. }
  274. id = p_id;
  275. class_name = obj->get_class();
  276. if (ScriptInstance *si = obj->get_script_instance()) {
  277. // Read script instance constants and variables
  278. if (!si->get_script().is_null()) {
  279. Script *s = si->get_script().ptr();
  280. _parse_script_properties(s, si);
  281. }
  282. }
  283. if (Node *node = Object::cast_to<Node>(obj)) {
  284. // Add specialized NodePath info (if inside tree).
  285. if (node->is_inside_tree()) {
  286. PropertyInfo pi(Variant::NODE_PATH, String("Node/path"));
  287. properties.push_back(SceneDebuggerProperty(pi, node->get_path()));
  288. } else { // Can't ask for path if a node is not in tree.
  289. PropertyInfo pi(Variant::STRING, String("Node/path"));
  290. properties.push_back(SceneDebuggerProperty(pi, "[Orphan]"));
  291. }
  292. } else if (Script *s = Object::cast_to<Script>(obj)) {
  293. // Add script constants (no instance).
  294. _parse_script_properties(s, nullptr);
  295. }
  296. // Add base object properties.
  297. List<PropertyInfo> pinfo;
  298. obj->get_property_list(&pinfo, true);
  299. for (const PropertyInfo &E : pinfo) {
  300. if (E.usage & (PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CATEGORY)) {
  301. properties.push_back(SceneDebuggerProperty(E, obj->get(E.name)));
  302. }
  303. }
  304. }
  305. void SceneDebuggerObject::_parse_script_properties(Script *p_script, ScriptInstance *p_instance) {
  306. typedef HashMap<const Script *, HashSet<StringName>> ScriptMemberMap;
  307. typedef HashMap<const Script *, HashMap<StringName, Variant>> ScriptConstantsMap;
  308. ScriptMemberMap members;
  309. if (p_instance) {
  310. members[p_script] = HashSet<StringName>();
  311. p_script->get_members(&(members[p_script]));
  312. }
  313. ScriptConstantsMap constants;
  314. constants[p_script] = HashMap<StringName, Variant>();
  315. p_script->get_constants(&(constants[p_script]));
  316. Ref<Script> base = p_script->get_base_script();
  317. while (base.is_valid()) {
  318. if (p_instance) {
  319. members[base.ptr()] = HashSet<StringName>();
  320. base->get_members(&(members[base.ptr()]));
  321. }
  322. constants[base.ptr()] = HashMap<StringName, Variant>();
  323. base->get_constants(&(constants[base.ptr()]));
  324. base = base->get_base_script();
  325. }
  326. // Members
  327. for (KeyValue<const Script *, HashSet<StringName>> sm : members) {
  328. for (const StringName &E : sm.value) {
  329. Variant m;
  330. if (p_instance->get(E, m)) {
  331. String script_path = sm.key == p_script ? "" : sm.key->get_path().get_file() + "/";
  332. PropertyInfo pi(m.get_type(), "Members/" + script_path + E);
  333. properties.push_back(SceneDebuggerProperty(pi, m));
  334. }
  335. }
  336. }
  337. // Constants
  338. for (KeyValue<const Script *, HashMap<StringName, Variant>> &sc : constants) {
  339. for (const KeyValue<StringName, Variant> &E : sc.value) {
  340. String script_path = sc.key == p_script ? "" : sc.key->get_path().get_file() + "/";
  341. if (E.value.get_type() == Variant::OBJECT) {
  342. Variant inst_id = ((Object *)E.value)->get_instance_id();
  343. PropertyInfo pi(inst_id.get_type(), "Constants/" + E.key, PROPERTY_HINT_OBJECT_ID, "Object");
  344. properties.push_back(SceneDebuggerProperty(pi, inst_id));
  345. } else {
  346. PropertyInfo pi(E.value.get_type(), "Constants/" + script_path + E.key);
  347. properties.push_back(SceneDebuggerProperty(pi, E.value));
  348. }
  349. }
  350. }
  351. }
  352. void SceneDebuggerObject::serialize(Array &r_arr, int p_max_size) {
  353. Array send_props;
  354. for (int i = 0; i < properties.size(); i++) {
  355. const PropertyInfo &pi = properties[i].first;
  356. Variant &var = properties[i].second;
  357. Ref<Resource> res = var;
  358. Array prop;
  359. prop.push_back(pi.name);
  360. prop.push_back(pi.type);
  361. PropertyHint hint = pi.hint;
  362. String hint_string = pi.hint_string;
  363. if (!res.is_null() && !res->get_path().is_empty()) {
  364. var = res->get_path();
  365. } else { //only send information that can be sent..
  366. int len = 0; //test how big is this to encode
  367. encode_variant(var, nullptr, len);
  368. if (len > p_max_size) { //limit to max size
  369. hint = PROPERTY_HINT_OBJECT_TOO_BIG;
  370. hint_string = "";
  371. var = Variant();
  372. }
  373. }
  374. prop.push_back(hint);
  375. prop.push_back(hint_string);
  376. prop.push_back(pi.usage);
  377. prop.push_back(var);
  378. send_props.push_back(prop);
  379. }
  380. r_arr.push_back(uint64_t(id));
  381. r_arr.push_back(class_name);
  382. r_arr.push_back(send_props);
  383. }
  384. void SceneDebuggerObject::deserialize(const Array &p_arr) {
  385. #define CHECK_TYPE(p_what, p_type) ERR_FAIL_COND(p_what.get_type() != Variant::p_type);
  386. ERR_FAIL_COND(p_arr.size() < 3);
  387. CHECK_TYPE(p_arr[0], INT);
  388. CHECK_TYPE(p_arr[1], STRING);
  389. CHECK_TYPE(p_arr[2], ARRAY);
  390. id = uint64_t(p_arr[0]);
  391. class_name = p_arr[1];
  392. Array props = p_arr[2];
  393. for (int i = 0; i < props.size(); i++) {
  394. CHECK_TYPE(props[i], ARRAY);
  395. Array prop = props[i];
  396. ERR_FAIL_COND(prop.size() != 6);
  397. CHECK_TYPE(prop[0], STRING);
  398. CHECK_TYPE(prop[1], INT);
  399. CHECK_TYPE(prop[2], INT);
  400. CHECK_TYPE(prop[3], STRING);
  401. CHECK_TYPE(prop[4], INT);
  402. PropertyInfo pinfo;
  403. pinfo.name = prop[0];
  404. pinfo.type = Variant::Type(int(prop[1]));
  405. pinfo.hint = PropertyHint(int(prop[2]));
  406. pinfo.hint_string = prop[3];
  407. pinfo.usage = PropertyUsageFlags(int(prop[4]));
  408. Variant var = prop[5];
  409. if (pinfo.type == Variant::OBJECT) {
  410. if (var.is_zero()) {
  411. var = Ref<Resource>();
  412. } else if (var.get_type() == Variant::OBJECT) {
  413. if (((Object *)var)->is_class("EncodedObjectAsID")) {
  414. var = Object::cast_to<EncodedObjectAsID>(var)->get_object_id();
  415. pinfo.type = var.get_type();
  416. pinfo.hint = PROPERTY_HINT_OBJECT_ID;
  417. pinfo.hint_string = "Object";
  418. }
  419. }
  420. }
  421. properties.push_back(SceneDebuggerProperty(pinfo, var));
  422. }
  423. }
  424. /// SceneDebuggerTree
  425. SceneDebuggerTree::SceneDebuggerTree(Node *p_root) {
  426. // Flatten tree into list, depth first, use stack to avoid recursion.
  427. List<Node *> stack;
  428. stack.push_back(p_root);
  429. bool is_root = true;
  430. const StringName &is_visible_sn = SNAME("is_visible");
  431. const StringName &is_visible_in_tree_sn = SNAME("is_visible_in_tree");
  432. while (stack.size()) {
  433. Node *n = stack[0];
  434. stack.pop_front();
  435. int count = n->get_child_count();
  436. for (int i = 0; i < count; i++) {
  437. stack.push_front(n->get_child(count - i - 1));
  438. }
  439. int view_flags = 0;
  440. if (is_root) {
  441. // Prevent root window visibility from being changed.
  442. is_root = false;
  443. } else if (n->has_method(is_visible_sn)) {
  444. const Variant visible = n->call(is_visible_sn);
  445. if (visible.get_type() == Variant::BOOL) {
  446. view_flags = RemoteNode::VIEW_HAS_VISIBLE_METHOD;
  447. view_flags |= uint8_t(visible) * RemoteNode::VIEW_VISIBLE;
  448. }
  449. if (n->has_method(is_visible_in_tree_sn)) {
  450. const Variant visible_in_tree = n->call(is_visible_in_tree_sn);
  451. if (visible_in_tree.get_type() == Variant::BOOL) {
  452. view_flags |= uint8_t(visible_in_tree) * RemoteNode::VIEW_VISIBLE_IN_TREE;
  453. }
  454. }
  455. }
  456. nodes.push_back(RemoteNode(count, n->get_name(), n->get_class(), n->get_instance_id(), n->get_scene_file_path(), view_flags));
  457. }
  458. }
  459. void SceneDebuggerTree::serialize(Array &p_arr) {
  460. for (const RemoteNode &n : nodes) {
  461. p_arr.push_back(n.child_count);
  462. p_arr.push_back(n.name);
  463. p_arr.push_back(n.type_name);
  464. p_arr.push_back(n.id);
  465. p_arr.push_back(n.scene_file_path);
  466. p_arr.push_back(n.view_flags);
  467. }
  468. }
  469. void SceneDebuggerTree::deserialize(const Array &p_arr) {
  470. int idx = 0;
  471. while (p_arr.size() > idx) {
  472. ERR_FAIL_COND(p_arr.size() < 6);
  473. CHECK_TYPE(p_arr[idx], INT); // child_count.
  474. CHECK_TYPE(p_arr[idx + 1], STRING); // name.
  475. CHECK_TYPE(p_arr[idx + 2], STRING); // type_name.
  476. CHECK_TYPE(p_arr[idx + 3], INT); // id.
  477. CHECK_TYPE(p_arr[idx + 4], STRING); // scene_file_path.
  478. CHECK_TYPE(p_arr[idx + 5], INT); // view_flags.
  479. 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]));
  480. idx += 6;
  481. }
  482. }
  483. /// LiveEditor
  484. LiveEditor *LiveEditor::singleton = nullptr;
  485. LiveEditor *LiveEditor::get_singleton() {
  486. return singleton;
  487. }
  488. void LiveEditor::_send_tree() {
  489. SceneTree *scene_tree = SceneTree::get_singleton();
  490. if (!scene_tree) {
  491. return;
  492. }
  493. Array arr;
  494. // Encoded as a flat list depth first.
  495. SceneDebuggerTree tree(scene_tree->root);
  496. tree.serialize(arr);
  497. EngineDebugger::get_singleton()->send_message("scene:scene_tree", arr);
  498. }
  499. void LiveEditor::_node_path_func(const NodePath &p_path, int p_id) {
  500. live_edit_node_path_cache[p_id] = p_path;
  501. }
  502. void LiveEditor::_res_path_func(const String &p_path, int p_id) {
  503. live_edit_resource_cache[p_id] = p_path;
  504. }
  505. void LiveEditor::_node_set_func(int p_id, const StringName &p_prop, const Variant &p_value) {
  506. SceneTree *scene_tree = SceneTree::get_singleton();
  507. if (!scene_tree) {
  508. return;
  509. }
  510. if (!live_edit_node_path_cache.has(p_id)) {
  511. return;
  512. }
  513. NodePath np = live_edit_node_path_cache[p_id];
  514. Node *base = nullptr;
  515. if (scene_tree->root->has_node(live_edit_root)) {
  516. base = scene_tree->root->get_node(live_edit_root);
  517. }
  518. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  519. if (!E) {
  520. return; //scene not editable
  521. }
  522. for (Node *F : E->value) {
  523. Node *n = F;
  524. if (base && !base->is_ancestor_of(n)) {
  525. continue;
  526. }
  527. if (!n->has_node(np)) {
  528. continue;
  529. }
  530. Node *n2 = n->get_node(np);
  531. n2->set(p_prop, p_value);
  532. }
  533. }
  534. void LiveEditor::_node_set_res_func(int p_id, const StringName &p_prop, const String &p_value) {
  535. Ref<Resource> r = ResourceLoader::load(p_value);
  536. if (!r.is_valid()) {
  537. return;
  538. }
  539. _node_set_func(p_id, p_prop, r);
  540. }
  541. void LiveEditor::_node_call_func(int p_id, const StringName &p_method, const Variant **p_args, int p_argcount) {
  542. SceneTree *scene_tree = SceneTree::get_singleton();
  543. if (!scene_tree) {
  544. return;
  545. }
  546. if (!live_edit_node_path_cache.has(p_id)) {
  547. return;
  548. }
  549. NodePath np = live_edit_node_path_cache[p_id];
  550. Node *base = nullptr;
  551. if (scene_tree->root->has_node(live_edit_root)) {
  552. base = scene_tree->root->get_node(live_edit_root);
  553. }
  554. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  555. if (!E) {
  556. return; //scene not editable
  557. }
  558. for (Node *F : E->value) {
  559. Node *n = F;
  560. if (base && !base->is_ancestor_of(n)) {
  561. continue;
  562. }
  563. if (!n->has_node(np)) {
  564. continue;
  565. }
  566. Node *n2 = n->get_node(np);
  567. Callable::CallError ce;
  568. n2->callp(p_method, p_args, p_argcount, ce);
  569. }
  570. }
  571. void LiveEditor::_res_set_func(int p_id, const StringName &p_prop, const Variant &p_value) {
  572. if (!live_edit_resource_cache.has(p_id)) {
  573. return;
  574. }
  575. String resp = live_edit_resource_cache[p_id];
  576. if (!ResourceCache::has(resp)) {
  577. return;
  578. }
  579. Ref<Resource> r = ResourceCache::get_ref(resp);
  580. if (!r.is_valid()) {
  581. return;
  582. }
  583. r->set(p_prop, p_value);
  584. }
  585. void LiveEditor::_res_set_res_func(int p_id, const StringName &p_prop, const String &p_value) {
  586. Ref<Resource> r = ResourceLoader::load(p_value);
  587. if (!r.is_valid()) {
  588. return;
  589. }
  590. _res_set_func(p_id, p_prop, r);
  591. }
  592. void LiveEditor::_res_call_func(int p_id, const StringName &p_method, const Variant **p_args, int p_argcount) {
  593. if (!live_edit_resource_cache.has(p_id)) {
  594. return;
  595. }
  596. String resp = live_edit_resource_cache[p_id];
  597. if (!ResourceCache::has(resp)) {
  598. return;
  599. }
  600. Ref<Resource> r = ResourceCache::get_ref(resp);
  601. if (!r.is_valid()) {
  602. return;
  603. }
  604. Callable::CallError ce;
  605. r->callp(p_method, p_args, p_argcount, ce);
  606. }
  607. void LiveEditor::_root_func(const NodePath &p_scene_path, const String &p_scene_from) {
  608. live_edit_root = p_scene_path;
  609. live_edit_scene = p_scene_from;
  610. }
  611. void LiveEditor::_create_node_func(const NodePath &p_parent, const String &p_type, const String &p_name) {
  612. SceneTree *scene_tree = SceneTree::get_singleton();
  613. if (!scene_tree) {
  614. return;
  615. }
  616. Node *base = nullptr;
  617. if (scene_tree->root->has_node(live_edit_root)) {
  618. base = scene_tree->root->get_node(live_edit_root);
  619. }
  620. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  621. if (!E) {
  622. return; //scene not editable
  623. }
  624. for (Node *F : E->value) {
  625. Node *n = F;
  626. if (base && !base->is_ancestor_of(n)) {
  627. continue;
  628. }
  629. if (!n->has_node(p_parent)) {
  630. continue;
  631. }
  632. Node *n2 = n->get_node(p_parent);
  633. Node *no = Object::cast_to<Node>(ClassDB::instantiate(p_type));
  634. if (!no) {
  635. continue;
  636. }
  637. no->set_name(p_name);
  638. n2->add_child(no);
  639. }
  640. }
  641. void LiveEditor::_instance_node_func(const NodePath &p_parent, const String &p_path, const String &p_name) {
  642. SceneTree *scene_tree = SceneTree::get_singleton();
  643. if (!scene_tree) {
  644. return;
  645. }
  646. Ref<PackedScene> ps = ResourceLoader::load(p_path);
  647. if (!ps.is_valid()) {
  648. return;
  649. }
  650. Node *base = nullptr;
  651. if (scene_tree->root->has_node(live_edit_root)) {
  652. base = scene_tree->root->get_node(live_edit_root);
  653. }
  654. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  655. if (!E) {
  656. return; //scene not editable
  657. }
  658. for (Node *F : E->value) {
  659. Node *n = F;
  660. if (base && !base->is_ancestor_of(n)) {
  661. continue;
  662. }
  663. if (!n->has_node(p_parent)) {
  664. continue;
  665. }
  666. Node *n2 = n->get_node(p_parent);
  667. Node *no = ps->instantiate();
  668. if (!no) {
  669. continue;
  670. }
  671. no->set_name(p_name);
  672. n2->add_child(no);
  673. }
  674. }
  675. void LiveEditor::_remove_node_func(const NodePath &p_at) {
  676. SceneTree *scene_tree = SceneTree::get_singleton();
  677. if (!scene_tree) {
  678. return;
  679. }
  680. Node *base = nullptr;
  681. if (scene_tree->root->has_node(live_edit_root)) {
  682. base = scene_tree->root->get_node(live_edit_root);
  683. }
  684. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  685. if (!E) {
  686. return; //scene not editable
  687. }
  688. Vector<Node *> to_delete;
  689. for (HashSet<Node *>::Iterator F = E->value.begin(); F; ++F) {
  690. Node *n = *F;
  691. if (base && !base->is_ancestor_of(n)) {
  692. continue;
  693. }
  694. if (!n->has_node(p_at)) {
  695. continue;
  696. }
  697. Node *n2 = n->get_node(p_at);
  698. to_delete.push_back(n2);
  699. }
  700. for (int i = 0; i < to_delete.size(); i++) {
  701. memdelete(to_delete[i]);
  702. }
  703. }
  704. void LiveEditor::_remove_and_keep_node_func(const NodePath &p_at, ObjectID p_keep_id) {
  705. SceneTree *scene_tree = SceneTree::get_singleton();
  706. if (!scene_tree) {
  707. return;
  708. }
  709. Node *base = nullptr;
  710. if (scene_tree->root->has_node(live_edit_root)) {
  711. base = scene_tree->root->get_node(live_edit_root);
  712. }
  713. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  714. if (!E) {
  715. return; //scene not editable
  716. }
  717. Vector<Node *> to_remove;
  718. for (HashSet<Node *>::Iterator F = E->value.begin(); F; ++F) {
  719. Node *n = *F;
  720. if (base && !base->is_ancestor_of(n)) {
  721. continue;
  722. }
  723. if (!n->has_node(p_at)) {
  724. continue;
  725. }
  726. to_remove.push_back(n);
  727. }
  728. for (int i = 0; i < to_remove.size(); i++) {
  729. Node *n = to_remove[i];
  730. Node *n2 = n->get_node(p_at);
  731. n2->get_parent()->remove_child(n2);
  732. live_edit_remove_list[n][p_keep_id] = n2;
  733. }
  734. }
  735. void LiveEditor::_restore_node_func(ObjectID p_id, const NodePath &p_at, int p_at_pos) {
  736. SceneTree *scene_tree = SceneTree::get_singleton();
  737. if (!scene_tree) {
  738. return;
  739. }
  740. Node *base = nullptr;
  741. if (scene_tree->root->has_node(live_edit_root)) {
  742. base = scene_tree->root->get_node(live_edit_root);
  743. }
  744. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  745. if (!E) {
  746. return; //scene not editable
  747. }
  748. for (HashSet<Node *>::Iterator F = E->value.begin(); F;) {
  749. HashSet<Node *>::Iterator N = F;
  750. ++N;
  751. Node *n = *F;
  752. if (base && !base->is_ancestor_of(n)) {
  753. continue;
  754. }
  755. if (!n->has_node(p_at)) {
  756. continue;
  757. }
  758. Node *n2 = n->get_node(p_at);
  759. HashMap<Node *, HashMap<ObjectID, Node *>>::Iterator EN = live_edit_remove_list.find(n);
  760. if (!EN) {
  761. continue;
  762. }
  763. HashMap<ObjectID, Node *>::Iterator FN = EN->value.find(p_id);
  764. if (!FN) {
  765. continue;
  766. }
  767. n2->add_child(FN->value);
  768. EN->value.remove(FN);
  769. if (EN->value.size() == 0) {
  770. live_edit_remove_list.remove(EN);
  771. }
  772. F = N;
  773. }
  774. }
  775. void LiveEditor::_duplicate_node_func(const NodePath &p_at, const String &p_new_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_at)) {
  794. continue;
  795. }
  796. Node *n2 = n->get_node(p_at);
  797. Node *dup = n2->duplicate(Node::DUPLICATE_SIGNALS | Node::DUPLICATE_GROUPS | Node::DUPLICATE_SCRIPTS);
  798. if (!dup) {
  799. continue;
  800. }
  801. dup->set_name(p_new_name);
  802. n2->get_parent()->add_child(dup);
  803. }
  804. }
  805. void LiveEditor::_reparent_node_func(const NodePath &p_at, const NodePath &p_new_place, const String &p_new_name, int p_at_pos) {
  806. SceneTree *scene_tree = SceneTree::get_singleton();
  807. if (!scene_tree) {
  808. return;
  809. }
  810. Node *base = nullptr;
  811. if (scene_tree->root->has_node(live_edit_root)) {
  812. base = scene_tree->root->get_node(live_edit_root);
  813. }
  814. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  815. if (!E) {
  816. return; //scene not editable
  817. }
  818. for (Node *F : E->value) {
  819. Node *n = F;
  820. if (base && !base->is_ancestor_of(n)) {
  821. continue;
  822. }
  823. if (!n->has_node(p_at)) {
  824. continue;
  825. }
  826. Node *nfrom = n->get_node(p_at);
  827. if (!n->has_node(p_new_place)) {
  828. continue;
  829. }
  830. Node *nto = n->get_node(p_new_place);
  831. nfrom->get_parent()->remove_child(nfrom);
  832. nfrom->set_name(p_new_name);
  833. nto->add_child(nfrom);
  834. if (p_at_pos >= 0) {
  835. nto->move_child(nfrom, p_at_pos);
  836. }
  837. }
  838. }
  839. #endif