scene_debugger.cpp 26 KB

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