scene_debugger.cpp 26 KB

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