2
0

editor_data.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  1. /*************************************************************************/
  2. /* editor_data.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 "editor_data.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/io/dir_access.h"
  33. #include "core/io/file_access.h"
  34. #include "core/io/resource_loader.h"
  35. #include "editor_node.h"
  36. #include "editor_settings.h"
  37. #include "scene/resources/packed_scene.h"
  38. void EditorHistory::cleanup_history() {
  39. for (int i = 0; i < history.size(); i++) {
  40. bool fail = false;
  41. for (int j = 0; j < history[i].path.size(); j++) {
  42. if (!history[i].path[j].ref.is_null()) {
  43. continue;
  44. }
  45. Object *obj = ObjectDB::get_instance(history[i].path[j].object);
  46. if (obj) {
  47. Node *n = Object::cast_to<Node>(obj);
  48. if (n && n->is_inside_tree()) {
  49. continue;
  50. }
  51. if (!n) { // Possibly still alive
  52. continue;
  53. }
  54. }
  55. if (j <= history[i].level) {
  56. //before or equal level, complete fail
  57. fail = true;
  58. } else {
  59. //after level, clip
  60. history.write[i].path.resize(j);
  61. }
  62. break;
  63. }
  64. if (fail) {
  65. history.remove(i);
  66. i--;
  67. }
  68. }
  69. if (current >= history.size()) {
  70. current = history.size() - 1;
  71. }
  72. }
  73. void EditorHistory::_add_object(ObjectID p_object, const String &p_property, int p_level_change, bool p_inspector_only) {
  74. Object *obj = ObjectDB::get_instance(p_object);
  75. ERR_FAIL_COND(!obj);
  76. RefCounted *r = Object::cast_to<RefCounted>(obj);
  77. Obj o;
  78. if (r) {
  79. o.ref = REF(r);
  80. }
  81. o.object = p_object;
  82. o.property = p_property;
  83. o.inspector_only = p_inspector_only;
  84. History h;
  85. bool has_prev = current >= 0 && current < history.size();
  86. if (has_prev) {
  87. history.resize(current + 1); //clip history to next
  88. }
  89. if (p_property != "" && has_prev) {
  90. //add a sub property
  91. History &pr = history.write[current];
  92. h = pr;
  93. h.path.resize(h.level + 1);
  94. h.path.push_back(o);
  95. h.level++;
  96. } else if (p_level_change != -1 && has_prev) {
  97. //add a sub property
  98. History &pr = history.write[current];
  99. h = pr;
  100. ERR_FAIL_INDEX(p_level_change, h.path.size());
  101. h.level = p_level_change;
  102. } else {
  103. //add a new node
  104. h.path.push_back(o);
  105. h.level = 0;
  106. }
  107. history.push_back(h);
  108. current++;
  109. }
  110. void EditorHistory::add_object_inspector_only(ObjectID p_object) {
  111. _add_object(p_object, "", -1, true);
  112. }
  113. void EditorHistory::add_object(ObjectID p_object) {
  114. _add_object(p_object, "", -1);
  115. }
  116. void EditorHistory::add_object(ObjectID p_object, const String &p_subprop) {
  117. _add_object(p_object, p_subprop, -1);
  118. }
  119. void EditorHistory::add_object(ObjectID p_object, int p_relevel) {
  120. _add_object(p_object, "", p_relevel);
  121. }
  122. int EditorHistory::get_history_len() {
  123. return history.size();
  124. }
  125. int EditorHistory::get_history_pos() {
  126. return current;
  127. }
  128. bool EditorHistory::is_history_obj_inspector_only(int p_obj) const {
  129. ERR_FAIL_INDEX_V(p_obj, history.size(), false);
  130. ERR_FAIL_INDEX_V(history[p_obj].level, history[p_obj].path.size(), false);
  131. return history[p_obj].path[history[p_obj].level].inspector_only;
  132. }
  133. ObjectID EditorHistory::get_history_obj(int p_obj) const {
  134. ERR_FAIL_INDEX_V(p_obj, history.size(), ObjectID());
  135. ERR_FAIL_INDEX_V(history[p_obj].level, history[p_obj].path.size(), ObjectID());
  136. return history[p_obj].path[history[p_obj].level].object;
  137. }
  138. bool EditorHistory::is_at_beginning() const {
  139. return current <= 0;
  140. }
  141. bool EditorHistory::is_at_end() const {
  142. return ((current + 1) >= history.size());
  143. }
  144. bool EditorHistory::next() {
  145. cleanup_history();
  146. if ((current + 1) < history.size()) {
  147. current++;
  148. } else {
  149. return false;
  150. }
  151. return true;
  152. }
  153. bool EditorHistory::previous() {
  154. cleanup_history();
  155. if (current > 0) {
  156. current--;
  157. } else {
  158. return false;
  159. }
  160. return true;
  161. }
  162. bool EditorHistory::is_current_inspector_only() const {
  163. if (current < 0 || current >= history.size()) {
  164. return false;
  165. }
  166. const History &h = history[current];
  167. return h.path[h.level].inspector_only;
  168. }
  169. ObjectID EditorHistory::get_current() {
  170. if (current < 0 || current >= history.size()) {
  171. return ObjectID();
  172. }
  173. History &h = history.write[current];
  174. Object *obj = ObjectDB::get_instance(h.path[h.level].object);
  175. if (!obj) {
  176. return ObjectID();
  177. }
  178. return obj->get_instance_id();
  179. }
  180. int EditorHistory::get_path_size() const {
  181. if (current < 0 || current >= history.size()) {
  182. return 0;
  183. }
  184. const History &h = history[current];
  185. return h.path.size();
  186. }
  187. ObjectID EditorHistory::get_path_object(int p_index) const {
  188. if (current < 0 || current >= history.size()) {
  189. return ObjectID();
  190. }
  191. const History &h = history[current];
  192. ERR_FAIL_INDEX_V(p_index, h.path.size(), ObjectID());
  193. Object *obj = ObjectDB::get_instance(h.path[p_index].object);
  194. if (!obj) {
  195. return ObjectID();
  196. }
  197. return obj->get_instance_id();
  198. }
  199. String EditorHistory::get_path_property(int p_index) const {
  200. if (current < 0 || current >= history.size()) {
  201. return "";
  202. }
  203. const History &h = history[current];
  204. ERR_FAIL_INDEX_V(p_index, h.path.size(), "");
  205. return h.path[p_index].property;
  206. }
  207. void EditorHistory::clear() {
  208. history.clear();
  209. current = -1;
  210. }
  211. EditorHistory::EditorHistory() {
  212. current = -1;
  213. }
  214. EditorPlugin *EditorData::get_editor(Object *p_object) {
  215. // We need to iterate backwards so that we can check user-created plugins first.
  216. // Otherwise, it would not be possible for plugins to handle CanvasItem and Spatial nodes.
  217. for (int i = editor_plugins.size() - 1; i > -1; i--) {
  218. if (editor_plugins[i]->has_main_screen() && editor_plugins[i]->handles(p_object)) {
  219. return editor_plugins[i];
  220. }
  221. }
  222. return nullptr;
  223. }
  224. Vector<EditorPlugin *> EditorData::get_subeditors(Object *p_object) {
  225. Vector<EditorPlugin *> sub_plugins;
  226. for (int i = editor_plugins.size() - 1; i > -1; i--) {
  227. if (!editor_plugins[i]->has_main_screen() && editor_plugins[i]->handles(p_object)) {
  228. sub_plugins.push_back(editor_plugins[i]);
  229. }
  230. }
  231. return sub_plugins;
  232. }
  233. EditorPlugin *EditorData::get_editor(String p_name) {
  234. for (int i = editor_plugins.size() - 1; i > -1; i--) {
  235. if (editor_plugins[i]->get_name() == p_name) {
  236. return editor_plugins[i];
  237. }
  238. }
  239. return nullptr;
  240. }
  241. void EditorData::copy_object_params(Object *p_object) {
  242. clipboard.clear();
  243. List<PropertyInfo> pinfo;
  244. p_object->get_property_list(&pinfo);
  245. for (const PropertyInfo &E : pinfo) {
  246. if (!(E.usage & PROPERTY_USAGE_EDITOR) || E.name == "script" || E.name == "scripts") {
  247. continue;
  248. }
  249. PropertyData pd;
  250. pd.name = E.name;
  251. pd.value = p_object->get(pd.name);
  252. clipboard.push_back(pd);
  253. }
  254. }
  255. void EditorData::get_editor_breakpoints(List<String> *p_breakpoints) {
  256. for (int i = 0; i < editor_plugins.size(); i++) {
  257. editor_plugins[i]->get_breakpoints(p_breakpoints);
  258. }
  259. }
  260. Dictionary EditorData::get_editor_states() const {
  261. Dictionary metadata;
  262. for (int i = 0; i < editor_plugins.size(); i++) {
  263. Dictionary state = editor_plugins[i]->get_state();
  264. if (state.is_empty()) {
  265. continue;
  266. }
  267. metadata[editor_plugins[i]->get_name()] = state;
  268. }
  269. return metadata;
  270. }
  271. Dictionary EditorData::get_scene_editor_states(int p_idx) const {
  272. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), Dictionary());
  273. EditedScene es = edited_scene[p_idx];
  274. return es.editor_states;
  275. }
  276. void EditorData::set_editor_states(const Dictionary &p_states) {
  277. List<Variant> keys;
  278. p_states.get_key_list(&keys);
  279. List<Variant>::Element *E = keys.front();
  280. for (; E; E = E->next()) {
  281. String name = E->get();
  282. int idx = -1;
  283. for (int i = 0; i < editor_plugins.size(); i++) {
  284. if (editor_plugins[i]->get_name() == name) {
  285. idx = i;
  286. break;
  287. }
  288. }
  289. if (idx == -1) {
  290. continue;
  291. }
  292. editor_plugins[idx]->set_state(p_states[name]);
  293. }
  294. }
  295. void EditorData::notify_edited_scene_changed() {
  296. for (int i = 0; i < editor_plugins.size(); i++) {
  297. editor_plugins[i]->edited_scene_changed();
  298. editor_plugins[i]->notify_scene_changed(get_edited_scene_root());
  299. }
  300. }
  301. void EditorData::notify_resource_saved(const Ref<Resource> &p_resource) {
  302. for (int i = 0; i < editor_plugins.size(); i++) {
  303. editor_plugins[i]->notify_resource_saved(p_resource);
  304. }
  305. }
  306. void EditorData::clear_editor_states() {
  307. for (int i = 0; i < editor_plugins.size(); i++) {
  308. editor_plugins[i]->clear();
  309. }
  310. }
  311. void EditorData::save_editor_external_data() {
  312. for (int i = 0; i < editor_plugins.size(); i++) {
  313. editor_plugins[i]->save_external_data();
  314. }
  315. }
  316. void EditorData::apply_changes_in_editors() {
  317. for (int i = 0; i < editor_plugins.size(); i++) {
  318. editor_plugins[i]->apply_changes();
  319. }
  320. }
  321. void EditorData::save_editor_global_states() {
  322. for (int i = 0; i < editor_plugins.size(); i++) {
  323. editor_plugins[i]->save_global_state();
  324. }
  325. }
  326. void EditorData::restore_editor_global_states() {
  327. for (int i = 0; i < editor_plugins.size(); i++) {
  328. editor_plugins[i]->restore_global_state();
  329. }
  330. }
  331. void EditorData::paste_object_params(Object *p_object) {
  332. ERR_FAIL_NULL(p_object);
  333. undo_redo.create_action(TTR("Paste Params"));
  334. for (const PropertyData &E : clipboard) {
  335. String name = E.name;
  336. undo_redo.add_do_property(p_object, name, E.value);
  337. undo_redo.add_undo_property(p_object, name, p_object->get(name));
  338. }
  339. undo_redo.commit_action();
  340. }
  341. bool EditorData::call_build() {
  342. bool result = true;
  343. for (int i = 0; i < editor_plugins.size() && result; i++) {
  344. result &= editor_plugins[i]->build();
  345. }
  346. return result;
  347. }
  348. UndoRedo &EditorData::get_undo_redo() {
  349. return undo_redo;
  350. }
  351. void EditorData::add_undo_redo_inspector_hook_callback(Callable p_callable) {
  352. undo_redo_callbacks.push_back(p_callable);
  353. }
  354. void EditorData::remove_undo_redo_inspector_hook_callback(Callable p_callable) {
  355. undo_redo_callbacks.erase(p_callable);
  356. }
  357. const Vector<Callable> EditorData::get_undo_redo_inspector_hook_callback() {
  358. return undo_redo_callbacks;
  359. }
  360. void EditorData::add_move_array_element_function(const StringName &p_class, Callable p_callable) {
  361. move_element_functions.insert(p_class, p_callable);
  362. }
  363. void EditorData::remove_move_array_element_function(const StringName &p_class) {
  364. move_element_functions.erase(p_class);
  365. }
  366. Callable EditorData::get_move_array_element_function(const StringName &p_class) const {
  367. if (move_element_functions.has(p_class)) {
  368. return move_element_functions[p_class];
  369. }
  370. return Callable();
  371. }
  372. void EditorData::remove_editor_plugin(EditorPlugin *p_plugin) {
  373. p_plugin->undo_redo = nullptr;
  374. editor_plugins.erase(p_plugin);
  375. }
  376. void EditorData::add_editor_plugin(EditorPlugin *p_plugin) {
  377. p_plugin->undo_redo = &undo_redo;
  378. editor_plugins.push_back(p_plugin);
  379. }
  380. int EditorData::get_editor_plugin_count() const {
  381. return editor_plugins.size();
  382. }
  383. EditorPlugin *EditorData::get_editor_plugin(int p_idx) {
  384. ERR_FAIL_INDEX_V(p_idx, editor_plugins.size(), nullptr);
  385. return editor_plugins[p_idx];
  386. }
  387. void EditorData::add_custom_type(const String &p_type, const String &p_inherits, const Ref<Script> &p_script, const Ref<Texture2D> &p_icon) {
  388. ERR_FAIL_COND_MSG(p_script.is_null(), "It's not a reference to a valid Script object.");
  389. CustomType ct;
  390. ct.name = p_type;
  391. ct.icon = p_icon;
  392. ct.script = p_script;
  393. if (!custom_types.has(p_inherits)) {
  394. custom_types[p_inherits] = Vector<CustomType>();
  395. }
  396. custom_types[p_inherits].push_back(ct);
  397. }
  398. Variant EditorData::instance_custom_type(const String &p_type, const String &p_inherits) {
  399. if (get_custom_types().has(p_inherits)) {
  400. for (int i = 0; i < get_custom_types()[p_inherits].size(); i++) {
  401. if (get_custom_types()[p_inherits][i].name == p_type) {
  402. Ref<Script> script = get_custom_types()[p_inherits][i].script;
  403. Variant ob = ClassDB::instantiate(p_inherits);
  404. ERR_FAIL_COND_V(!ob, Variant());
  405. Node *n = Object::cast_to<Node>(ob);
  406. if (n) {
  407. n->set_name(p_type);
  408. }
  409. ((Object *)ob)->set_script(script);
  410. return ob;
  411. }
  412. }
  413. }
  414. return Variant();
  415. }
  416. void EditorData::remove_custom_type(const String &p_type) {
  417. for (Map<String, Vector<CustomType>>::Element *E = custom_types.front(); E; E = E->next()) {
  418. for (int i = 0; i < E->get().size(); i++) {
  419. if (E->get()[i].name == p_type) {
  420. E->get().remove(i);
  421. if (E->get().is_empty()) {
  422. custom_types.erase(E->key());
  423. }
  424. return;
  425. }
  426. }
  427. }
  428. }
  429. int EditorData::add_edited_scene(int p_at_pos) {
  430. if (p_at_pos < 0) {
  431. p_at_pos = edited_scene.size();
  432. }
  433. EditedScene es;
  434. es.root = nullptr;
  435. es.path = String();
  436. es.file_modified_time = 0;
  437. es.history_current = -1;
  438. es.version = 0;
  439. es.live_edit_root = NodePath(String("/root"));
  440. if (p_at_pos == edited_scene.size()) {
  441. edited_scene.push_back(es);
  442. } else {
  443. edited_scene.insert(p_at_pos, es);
  444. }
  445. if (current_edited_scene < 0) {
  446. current_edited_scene = 0;
  447. }
  448. return p_at_pos;
  449. }
  450. void EditorData::move_edited_scene_index(int p_idx, int p_to_idx) {
  451. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  452. ERR_FAIL_INDEX(p_to_idx, edited_scene.size());
  453. SWAP(edited_scene.write[p_idx], edited_scene.write[p_to_idx]);
  454. }
  455. void EditorData::remove_scene(int p_idx) {
  456. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  457. if (edited_scene[p_idx].root) {
  458. for (int i = 0; i < editor_plugins.size(); i++) {
  459. editor_plugins[i]->notify_scene_closed(edited_scene[p_idx].root->get_scene_file_path());
  460. }
  461. memdelete(edited_scene[p_idx].root);
  462. }
  463. if (current_edited_scene > p_idx) {
  464. current_edited_scene--;
  465. } else if (current_edited_scene == p_idx && current_edited_scene > 0) {
  466. current_edited_scene--;
  467. }
  468. if (edited_scene[p_idx].path != String()) {
  469. ScriptEditor::get_singleton()->close_builtin_scripts_from_scene(edited_scene[p_idx].path);
  470. }
  471. edited_scene.remove(p_idx);
  472. }
  473. bool EditorData::_find_updated_instances(Node *p_root, Node *p_node, Set<String> &checked_paths) {
  474. /*
  475. if (p_root!=p_node && p_node->get_owner()!=p_root && !p_root->is_editable_instance(p_node->get_owner()))
  476. return false;
  477. */
  478. Ref<SceneState> ss;
  479. if (p_node == p_root) {
  480. ss = p_node->get_scene_inherited_state();
  481. } else if (p_node->get_scene_file_path() != String()) {
  482. ss = p_node->get_scene_instance_state();
  483. }
  484. if (ss.is_valid()) {
  485. String path = ss->get_path();
  486. if (!checked_paths.has(path)) {
  487. uint64_t modified_time = FileAccess::get_modified_time(path);
  488. if (modified_time != ss->get_last_modified_time()) {
  489. return true; //external scene changed
  490. }
  491. checked_paths.insert(path);
  492. }
  493. }
  494. for (int i = 0; i < p_node->get_child_count(); i++) {
  495. bool found = _find_updated_instances(p_root, p_node->get_child(i), checked_paths);
  496. if (found) {
  497. return true;
  498. }
  499. }
  500. return false;
  501. }
  502. bool EditorData::check_and_update_scene(int p_idx) {
  503. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), false);
  504. if (!edited_scene[p_idx].root) {
  505. return false;
  506. }
  507. Set<String> checked_scenes;
  508. bool must_reload = _find_updated_instances(edited_scene[p_idx].root, edited_scene[p_idx].root, checked_scenes);
  509. if (must_reload) {
  510. Ref<PackedScene> pscene;
  511. pscene.instantiate();
  512. EditorProgress ep("update_scene", TTR("Updating Scene"), 2);
  513. ep.step(TTR("Storing local changes..."), 0);
  514. //pack first, so it stores diffs to previous version of saved scene
  515. Error err = pscene->pack(edited_scene[p_idx].root);
  516. ERR_FAIL_COND_V(err != OK, false);
  517. ep.step(TTR("Updating scene..."), 1);
  518. Node *new_scene = pscene->instantiate(PackedScene::GEN_EDIT_STATE_MAIN);
  519. ERR_FAIL_COND_V(!new_scene, false);
  520. //transfer selection
  521. List<Node *> new_selection;
  522. for (const Node *E : edited_scene.write[p_idx].selection) {
  523. NodePath p = edited_scene[p_idx].root->get_path_to(E);
  524. Node *new_node = new_scene->get_node(p);
  525. if (new_node) {
  526. new_selection.push_back(new_node);
  527. }
  528. }
  529. new_scene->set_scene_file_path(edited_scene[p_idx].root->get_scene_file_path());
  530. memdelete(edited_scene[p_idx].root);
  531. edited_scene.write[p_idx].root = new_scene;
  532. if (new_scene->get_scene_file_path() != "") {
  533. edited_scene.write[p_idx].path = new_scene->get_scene_file_path();
  534. }
  535. edited_scene.write[p_idx].selection = new_selection;
  536. return true;
  537. }
  538. return false;
  539. }
  540. int EditorData::get_edited_scene() const {
  541. return current_edited_scene;
  542. }
  543. void EditorData::set_edited_scene(int p_idx) {
  544. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  545. current_edited_scene = p_idx;
  546. //swap
  547. }
  548. Node *EditorData::get_edited_scene_root(int p_idx) {
  549. if (p_idx < 0) {
  550. ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), nullptr);
  551. return edited_scene[current_edited_scene].root;
  552. } else {
  553. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), nullptr);
  554. return edited_scene[p_idx].root;
  555. }
  556. }
  557. void EditorData::set_edited_scene_root(Node *p_root) {
  558. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  559. edited_scene.write[current_edited_scene].root = p_root;
  560. if (p_root) {
  561. if (p_root->get_scene_file_path() != "") {
  562. edited_scene.write[current_edited_scene].path = p_root->get_scene_file_path();
  563. } else {
  564. p_root->set_scene_file_path(edited_scene[current_edited_scene].path);
  565. }
  566. }
  567. if (edited_scene[current_edited_scene].path != "") {
  568. edited_scene.write[current_edited_scene].file_modified_time = FileAccess::get_modified_time(edited_scene[current_edited_scene].path);
  569. }
  570. }
  571. int EditorData::get_edited_scene_count() const {
  572. return edited_scene.size();
  573. }
  574. Vector<EditorData::EditedScene> EditorData::get_edited_scenes() const {
  575. Vector<EditedScene> out_edited_scenes_list = Vector<EditedScene>();
  576. for (int i = 0; i < edited_scene.size(); i++) {
  577. out_edited_scenes_list.push_back(edited_scene[i]);
  578. }
  579. return out_edited_scenes_list;
  580. }
  581. void EditorData::set_edited_scene_version(uint64_t version, int p_scene_idx) {
  582. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  583. if (p_scene_idx < 0) {
  584. edited_scene.write[current_edited_scene].version = version;
  585. } else {
  586. ERR_FAIL_INDEX(p_scene_idx, edited_scene.size());
  587. edited_scene.write[p_scene_idx].version = version;
  588. }
  589. }
  590. uint64_t EditorData::get_scene_version(int p_idx) const {
  591. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), 0);
  592. return edited_scene[p_idx].version;
  593. }
  594. void EditorData::set_scene_modified_time(int p_idx, uint64_t p_time) {
  595. if (p_idx == -1) {
  596. p_idx = current_edited_scene;
  597. }
  598. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  599. edited_scene.write[p_idx].file_modified_time = p_time;
  600. }
  601. uint64_t EditorData::get_scene_modified_time(int p_idx) const {
  602. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), 0);
  603. return edited_scene[p_idx].file_modified_time;
  604. }
  605. String EditorData::get_scene_type(int p_idx) const {
  606. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String());
  607. if (!edited_scene[p_idx].root) {
  608. return "";
  609. }
  610. return edited_scene[p_idx].root->get_class();
  611. }
  612. void EditorData::move_edited_scene_to_index(int p_idx) {
  613. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  614. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  615. EditedScene es = edited_scene[current_edited_scene];
  616. edited_scene.remove(current_edited_scene);
  617. edited_scene.insert(p_idx, es);
  618. current_edited_scene = p_idx;
  619. }
  620. Ref<Script> EditorData::get_scene_root_script(int p_idx) const {
  621. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), Ref<Script>());
  622. if (!edited_scene[p_idx].root) {
  623. return Ref<Script>();
  624. }
  625. Ref<Script> s = edited_scene[p_idx].root->get_script();
  626. if (!s.is_valid() && edited_scene[p_idx].root->get_child_count()) {
  627. Node *n = edited_scene[p_idx].root->get_child(0);
  628. while (!s.is_valid() && n && n->get_scene_file_path() == String()) {
  629. s = n->get_script();
  630. n = n->get_parent();
  631. }
  632. }
  633. return s;
  634. }
  635. String EditorData::get_scene_title(int p_idx, bool p_always_strip_extension) const {
  636. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String());
  637. if (!edited_scene[p_idx].root) {
  638. return TTR("[empty]");
  639. }
  640. if (edited_scene[p_idx].root->get_scene_file_path() == "") {
  641. return TTR("[unsaved]");
  642. }
  643. const String filename = edited_scene[p_idx].root->get_scene_file_path().get_file();
  644. const String basename = filename.get_basename();
  645. if (p_always_strip_extension) {
  646. return basename;
  647. }
  648. // Return the filename including the extension if there's ambiguity (e.g. both `foo.tscn` and `foo.scn` are being edited).
  649. for (int i = 0; i < edited_scene.size(); i++) {
  650. if (i == p_idx) {
  651. // Don't compare the edited scene against itself.
  652. continue;
  653. }
  654. if (edited_scene[i].root && basename == edited_scene[i].root->get_scene_file_path().get_file().get_basename()) {
  655. return filename;
  656. }
  657. }
  658. // Else, return just the basename as there's no ambiguity.
  659. return basename;
  660. }
  661. void EditorData::set_scene_path(int p_idx, const String &p_path) {
  662. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  663. edited_scene.write[p_idx].path = p_path;
  664. if (!edited_scene[p_idx].root) {
  665. return;
  666. }
  667. edited_scene[p_idx].root->set_scene_file_path(p_path);
  668. }
  669. String EditorData::get_scene_path(int p_idx) const {
  670. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String());
  671. if (edited_scene[p_idx].root) {
  672. if (edited_scene[p_idx].root->get_scene_file_path() == "") {
  673. edited_scene[p_idx].root->set_scene_file_path(edited_scene[p_idx].path);
  674. } else {
  675. return edited_scene[p_idx].root->get_scene_file_path();
  676. }
  677. }
  678. return edited_scene[p_idx].path;
  679. }
  680. void EditorData::set_edited_scene_live_edit_root(const NodePath &p_root) {
  681. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  682. edited_scene.write[current_edited_scene].live_edit_root = p_root;
  683. }
  684. NodePath EditorData::get_edited_scene_live_edit_root() {
  685. ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), String());
  686. return edited_scene[current_edited_scene].live_edit_root;
  687. }
  688. void EditorData::save_edited_scene_state(EditorSelection *p_selection, EditorHistory *p_history, const Dictionary &p_custom) {
  689. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  690. EditedScene &es = edited_scene.write[current_edited_scene];
  691. es.selection = p_selection->get_full_selected_node_list();
  692. es.history_current = p_history->current;
  693. es.history_stored = p_history->history;
  694. es.editor_states = get_editor_states();
  695. es.custom_state = p_custom;
  696. }
  697. Dictionary EditorData::restore_edited_scene_state(EditorSelection *p_selection, EditorHistory *p_history) {
  698. ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), Dictionary());
  699. EditedScene &es = edited_scene.write[current_edited_scene];
  700. p_history->current = es.history_current;
  701. p_history->history = es.history_stored;
  702. p_selection->clear();
  703. for (Node *E : es.selection) {
  704. p_selection->add_node(E);
  705. }
  706. set_editor_states(es.editor_states);
  707. return es.custom_state;
  708. }
  709. void EditorData::clear_edited_scenes() {
  710. for (int i = 0; i < edited_scene.size(); i++) {
  711. if (edited_scene[i].root) {
  712. memdelete(edited_scene[i].root);
  713. }
  714. }
  715. edited_scene.clear();
  716. }
  717. void EditorData::set_plugin_window_layout(Ref<ConfigFile> p_layout) {
  718. for (int i = 0; i < editor_plugins.size(); i++) {
  719. editor_plugins[i]->set_window_layout(p_layout);
  720. }
  721. }
  722. void EditorData::get_plugin_window_layout(Ref<ConfigFile> p_layout) {
  723. for (int i = 0; i < editor_plugins.size(); i++) {
  724. editor_plugins[i]->get_window_layout(p_layout);
  725. }
  726. }
  727. bool EditorData::script_class_is_parent(const String &p_class, const String &p_inherits) {
  728. if (!ScriptServer::is_global_class(p_class)) {
  729. return false;
  730. }
  731. String base = script_class_get_base(p_class);
  732. Ref<Script> script = script_class_load_script(p_class);
  733. Ref<Script> base_script = script->get_base_script();
  734. while (p_inherits != base) {
  735. if (ClassDB::class_exists(base)) {
  736. return ClassDB::is_parent_class(base, p_inherits);
  737. } else if (ScriptServer::is_global_class(base)) {
  738. base = script_class_get_base(base);
  739. } else if (base_script.is_valid()) {
  740. return ClassDB::is_parent_class(base_script->get_instance_base_type(), p_inherits);
  741. } else {
  742. return false;
  743. }
  744. }
  745. return true;
  746. }
  747. StringName EditorData::script_class_get_base(const String &p_class) const {
  748. Ref<Script> script = script_class_load_script(p_class);
  749. if (script.is_null()) {
  750. return StringName();
  751. }
  752. Ref<Script> base_script = script->get_base_script();
  753. if (base_script.is_null()) {
  754. return ScriptServer::get_global_class_base(p_class);
  755. }
  756. return script->get_language()->get_global_class_name(base_script->get_path());
  757. }
  758. Variant EditorData::script_class_instance(const String &p_class) {
  759. if (ScriptServer::is_global_class(p_class)) {
  760. Variant obj = ClassDB::instantiate(ScriptServer::get_global_class_native_base(p_class));
  761. if (obj) {
  762. Ref<Script> script = script_class_load_script(p_class);
  763. if (script.is_valid()) {
  764. ((Object *)obj)->set_script(script);
  765. }
  766. return obj;
  767. }
  768. }
  769. return Variant();
  770. }
  771. Ref<Script> EditorData::script_class_load_script(const String &p_class) const {
  772. if (!ScriptServer::is_global_class(p_class)) {
  773. return Ref<Script>();
  774. }
  775. String path = ScriptServer::get_global_class_path(p_class);
  776. return ResourceLoader::load(path, "Script");
  777. }
  778. void EditorData::script_class_set_icon_path(const String &p_class, const String &p_icon_path) {
  779. _script_class_icon_paths[p_class] = p_icon_path;
  780. }
  781. String EditorData::script_class_get_icon_path(const String &p_class) const {
  782. if (!ScriptServer::is_global_class(p_class)) {
  783. return String();
  784. }
  785. String current = p_class;
  786. String ret = _script_class_icon_paths[current];
  787. while (ret.is_empty()) {
  788. current = script_class_get_base(current);
  789. if (!ScriptServer::is_global_class(current)) {
  790. return String();
  791. }
  792. ret = _script_class_icon_paths.has(current) ? _script_class_icon_paths[current] : String();
  793. }
  794. return ret;
  795. }
  796. StringName EditorData::script_class_get_name(const String &p_path) const {
  797. return _script_class_file_to_path.has(p_path) ? _script_class_file_to_path[p_path] : StringName();
  798. }
  799. void EditorData::script_class_set_name(const String &p_path, const StringName &p_class) {
  800. _script_class_file_to_path[p_path] = p_class;
  801. }
  802. void EditorData::script_class_save_icon_paths() {
  803. List<StringName> keys;
  804. _script_class_icon_paths.get_key_list(&keys);
  805. Dictionary d;
  806. for (const StringName &E : keys) {
  807. if (ScriptServer::is_global_class(E)) {
  808. d[E] = _script_class_icon_paths[E];
  809. }
  810. }
  811. Dictionary old;
  812. if (ProjectSettings::get_singleton()->has_setting("_global_script_class_icons")) {
  813. old = ProjectSettings::get_singleton()->get("_global_script_class_icons");
  814. }
  815. if ((!old.is_empty() || d.is_empty()) && d.hash() == old.hash()) {
  816. return;
  817. }
  818. if (d.is_empty()) {
  819. if (ProjectSettings::get_singleton()->has_setting("_global_script_class_icons")) {
  820. ProjectSettings::get_singleton()->clear("_global_script_class_icons");
  821. }
  822. } else {
  823. ProjectSettings::get_singleton()->set("_global_script_class_icons", d);
  824. }
  825. ProjectSettings::get_singleton()->save();
  826. }
  827. void EditorData::script_class_load_icon_paths() {
  828. script_class_clear_icon_paths();
  829. if (ProjectSettings::get_singleton()->has_setting("_global_script_class_icons")) {
  830. Dictionary d = ProjectSettings::get_singleton()->get("_global_script_class_icons");
  831. List<Variant> keys;
  832. d.get_key_list(&keys);
  833. for (const Variant &E : keys) {
  834. String name = E.operator String();
  835. _script_class_icon_paths[name] = d[name];
  836. String path = ScriptServer::get_global_class_path(name);
  837. script_class_set_name(path, name);
  838. }
  839. }
  840. }
  841. EditorData::EditorData() {
  842. current_edited_scene = -1;
  843. //load_imported_scenes_from_globals();
  844. script_class_load_icon_paths();
  845. }
  846. ///////////
  847. void EditorSelection::_node_removed(Node *p_node) {
  848. if (!selection.has(p_node)) {
  849. return;
  850. }
  851. Object *meta = selection[p_node];
  852. if (meta) {
  853. memdelete(meta);
  854. }
  855. selection.erase(p_node);
  856. changed = true;
  857. nl_changed = true;
  858. }
  859. void EditorSelection::add_node(Node *p_node) {
  860. ERR_FAIL_NULL(p_node);
  861. ERR_FAIL_COND(!p_node->is_inside_tree());
  862. if (selection.has(p_node)) {
  863. return;
  864. }
  865. changed = true;
  866. nl_changed = true;
  867. Object *meta = nullptr;
  868. for (Object *E : editor_plugins) {
  869. meta = E->call("_get_editor_data", p_node);
  870. if (meta) {
  871. break;
  872. }
  873. }
  874. selection[p_node] = meta;
  875. p_node->connect("tree_exiting", callable_mp(this, &EditorSelection::_node_removed), varray(p_node), CONNECT_ONESHOT);
  876. //emit_signal(SNAME("selection_changed"));
  877. }
  878. void EditorSelection::remove_node(Node *p_node) {
  879. ERR_FAIL_NULL(p_node);
  880. if (!selection.has(p_node)) {
  881. return;
  882. }
  883. changed = true;
  884. nl_changed = true;
  885. Object *meta = selection[p_node];
  886. if (meta) {
  887. memdelete(meta);
  888. }
  889. selection.erase(p_node);
  890. p_node->disconnect("tree_exiting", callable_mp(this, &EditorSelection::_node_removed));
  891. //emit_signal(SNAME("selection_changed"));
  892. }
  893. bool EditorSelection::is_selected(Node *p_node) const {
  894. return selection.has(p_node);
  895. }
  896. Array EditorSelection::_get_transformable_selected_nodes() {
  897. Array ret;
  898. for (const Node *E : selected_node_list) {
  899. ret.push_back(E);
  900. }
  901. return ret;
  902. }
  903. TypedArray<Node> EditorSelection::get_selected_nodes() {
  904. TypedArray<Node> ret;
  905. for (const KeyValue<Node *, Object *> &E : selection) {
  906. ret.push_back(E.key);
  907. }
  908. return ret;
  909. }
  910. void EditorSelection::_bind_methods() {
  911. ClassDB::bind_method(D_METHOD("clear"), &EditorSelection::clear);
  912. ClassDB::bind_method(D_METHOD("add_node", "node"), &EditorSelection::add_node);
  913. ClassDB::bind_method(D_METHOD("remove_node", "node"), &EditorSelection::remove_node);
  914. ClassDB::bind_method(D_METHOD("get_selected_nodes"), &EditorSelection::get_selected_nodes);
  915. ClassDB::bind_method(D_METHOD("get_transformable_selected_nodes"), &EditorSelection::_get_transformable_selected_nodes);
  916. ClassDB::bind_method(D_METHOD("_emit_change"), &EditorSelection::_emit_change);
  917. ADD_SIGNAL(MethodInfo("selection_changed"));
  918. }
  919. void EditorSelection::add_editor_plugin(Object *p_object) {
  920. editor_plugins.push_back(p_object);
  921. }
  922. void EditorSelection::_update_nl() {
  923. if (!nl_changed) {
  924. return;
  925. }
  926. selected_node_list.clear();
  927. for (const KeyValue<Node *, Object *> &E : selection) {
  928. Node *parent = E.key;
  929. parent = parent->get_parent();
  930. bool skip = false;
  931. while (parent) {
  932. if (selection.has(parent)) {
  933. skip = true;
  934. break;
  935. }
  936. parent = parent->get_parent();
  937. }
  938. if (skip) {
  939. continue;
  940. }
  941. selected_node_list.push_back(E.key);
  942. }
  943. nl_changed = true;
  944. }
  945. void EditorSelection::update() {
  946. _update_nl();
  947. if (!changed) {
  948. return;
  949. }
  950. changed = false;
  951. if (!emitted) {
  952. emitted = true;
  953. call_deferred(SNAME("_emit_change"));
  954. }
  955. }
  956. void EditorSelection::_emit_change() {
  957. emit_signal(SNAME("selection_changed"));
  958. emitted = false;
  959. }
  960. List<Node *> &EditorSelection::get_selected_node_list() {
  961. if (changed) {
  962. update();
  963. } else {
  964. _update_nl();
  965. }
  966. return selected_node_list;
  967. }
  968. List<Node *> EditorSelection::get_full_selected_node_list() {
  969. List<Node *> node_list;
  970. for (const KeyValue<Node *, Object *> &E : selection) {
  971. node_list.push_back(E.key);
  972. }
  973. return node_list;
  974. }
  975. void EditorSelection::clear() {
  976. while (!selection.is_empty()) {
  977. remove_node(selection.front()->key());
  978. }
  979. changed = true;
  980. nl_changed = true;
  981. }
  982. EditorSelection::EditorSelection() {
  983. emitted = false;
  984. changed = false;
  985. nl_changed = false;
  986. }
  987. EditorSelection::~EditorSelection() {
  988. clear();
  989. }