editor_data.cpp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. /**************************************************************************/
  2. /* editor_data.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "editor_data.h"
  31. #include "core/io/resource_loader.h"
  32. #include "core/os/dir_access.h"
  33. #include "core/os/file_access.h"
  34. #include "core/project_settings.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. Reference *r = Object::cast_to<Reference>(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(), 0);
  135. ERR_FAIL_INDEX_V(history[p_obj].level, history[p_obj].path.size(), 0);
  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 0;
  172. }
  173. History &h = history.write[current];
  174. Object *obj = ObjectDB::get_instance(h.path[h.level].object);
  175. if (!obj) {
  176. return 0;
  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 0;
  190. }
  191. const History &h = history[current];
  192. ERR_FAIL_INDEX_V(p_index, h.path.size(), 0);
  193. Object *obj = ObjectDB::get_instance(h.path[p_index].object);
  194. if (!obj) {
  195. return 0;
  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. for (int i = 0; i < editor_plugins.size(); i++) {
  216. if (editor_plugins[i]->has_main_screen() && editor_plugins[i]->handles(p_object)) {
  217. return editor_plugins[i];
  218. }
  219. }
  220. return nullptr;
  221. }
  222. EditorPlugin *EditorData::get_subeditor(Object *p_object) {
  223. for (int i = 0; i < editor_plugins.size(); i++) {
  224. if (!editor_plugins[i]->has_main_screen() && editor_plugins[i]->handles(p_object)) {
  225. return editor_plugins[i];
  226. }
  227. }
  228. return nullptr;
  229. }
  230. Vector<EditorPlugin *> EditorData::get_subeditors(Object *p_object) {
  231. Vector<EditorPlugin *> sub_plugins;
  232. for (int i = 0; i < editor_plugins.size(); i++) {
  233. if (!editor_plugins[i]->has_main_screen() && editor_plugins[i]->handles(p_object)) {
  234. sub_plugins.push_back(editor_plugins[i]);
  235. }
  236. }
  237. return sub_plugins;
  238. }
  239. EditorPlugin *EditorData::get_editor(String p_name) {
  240. for (int i = 0; i < editor_plugins.size(); i++) {
  241. if (editor_plugins[i]->get_name() == p_name) {
  242. return editor_plugins[i];
  243. }
  244. }
  245. return nullptr;
  246. }
  247. void EditorData::copy_object_params(Object *p_object) {
  248. clipboard.clear();
  249. List<PropertyInfo> pinfo;
  250. p_object->get_property_list(&pinfo);
  251. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  252. if (!(E->get().usage & PROPERTY_USAGE_EDITOR) || E->get().name == "script" || E->get().name == "scripts") {
  253. continue;
  254. }
  255. PropertyData pd;
  256. pd.name = E->get().name;
  257. pd.value = p_object->get(pd.name);
  258. clipboard.push_back(pd);
  259. }
  260. }
  261. void EditorData::get_editor_breakpoints(List<String> *p_breakpoints) {
  262. for (int i = 0; i < editor_plugins.size(); i++) {
  263. editor_plugins[i]->get_breakpoints(p_breakpoints);
  264. }
  265. }
  266. Dictionary EditorData::get_editor_states() const {
  267. Dictionary metadata;
  268. for (int i = 0; i < editor_plugins.size(); i++) {
  269. Dictionary state = editor_plugins[i]->get_state();
  270. if (state.empty()) {
  271. continue;
  272. }
  273. metadata[editor_plugins[i]->get_name()] = state;
  274. }
  275. return metadata;
  276. }
  277. Dictionary EditorData::get_scene_editor_states(int p_idx) const {
  278. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), Dictionary());
  279. EditedScene es = edited_scene[p_idx];
  280. return es.editor_states;
  281. }
  282. void EditorData::set_editor_states(const Dictionary &p_states) {
  283. List<Variant> keys;
  284. p_states.get_key_list(&keys);
  285. List<Variant>::Element *E = keys.front();
  286. for (; E; E = E->next()) {
  287. String name = E->get();
  288. int idx = -1;
  289. for (int i = 0; i < editor_plugins.size(); i++) {
  290. if (editor_plugins[i]->get_name() == name) {
  291. idx = i;
  292. break;
  293. }
  294. }
  295. if (idx == -1) {
  296. continue;
  297. }
  298. editor_plugins[idx]->set_state(p_states[name]);
  299. }
  300. }
  301. void EditorData::notify_edited_scene_changed() {
  302. for (int i = 0; i < editor_plugins.size(); i++) {
  303. editor_plugins[i]->edited_scene_changed();
  304. editor_plugins[i]->notify_scene_changed(get_edited_scene_root());
  305. }
  306. }
  307. void EditorData::notify_resource_saved(const Ref<Resource> &p_resource) {
  308. for (int i = 0; i < editor_plugins.size(); i++) {
  309. editor_plugins[i]->notify_resource_saved(p_resource);
  310. }
  311. }
  312. void EditorData::clear_editor_states() {
  313. for (int i = 0; i < editor_plugins.size(); i++) {
  314. editor_plugins[i]->clear();
  315. }
  316. }
  317. void EditorData::save_editor_external_data() {
  318. for (int i = 0; i < editor_plugins.size(); i++) {
  319. editor_plugins[i]->save_external_data();
  320. }
  321. }
  322. void EditorData::apply_changes_in_editors() {
  323. for (int i = 0; i < editor_plugins.size(); i++) {
  324. editor_plugins[i]->apply_changes();
  325. }
  326. }
  327. void EditorData::save_editor_global_states() {
  328. for (int i = 0; i < editor_plugins.size(); i++) {
  329. editor_plugins[i]->save_global_state();
  330. }
  331. }
  332. void EditorData::restore_editor_global_states() {
  333. for (int i = 0; i < editor_plugins.size(); i++) {
  334. editor_plugins[i]->restore_global_state();
  335. }
  336. }
  337. void EditorData::paste_object_params(Object *p_object) {
  338. ERR_FAIL_NULL(p_object);
  339. undo_redo.create_action(TTR("Paste Params"));
  340. for (List<PropertyData>::Element *E = clipboard.front(); E; E = E->next()) {
  341. String name = E->get().name;
  342. undo_redo.add_do_property(p_object, name, E->get().value);
  343. undo_redo.add_undo_property(p_object, name, p_object->get(name));
  344. }
  345. undo_redo.commit_action();
  346. }
  347. bool EditorData::call_build() {
  348. bool result = true;
  349. for (int i = 0; i < editor_plugins.size() && result; i++) {
  350. result &= editor_plugins[i]->build();
  351. }
  352. return result;
  353. }
  354. UndoRedo &EditorData::get_undo_redo() {
  355. return undo_redo;
  356. }
  357. void EditorData::remove_editor_plugin(EditorPlugin *p_plugin) {
  358. p_plugin->undo_redo = nullptr;
  359. editor_plugins.erase(p_plugin);
  360. }
  361. void EditorData::add_editor_plugin(EditorPlugin *p_plugin) {
  362. p_plugin->undo_redo = &undo_redo;
  363. editor_plugins.push_back(p_plugin);
  364. }
  365. int EditorData::get_editor_plugin_count() const {
  366. return editor_plugins.size();
  367. }
  368. EditorPlugin *EditorData::get_editor_plugin(int p_idx) {
  369. ERR_FAIL_INDEX_V(p_idx, editor_plugins.size(), nullptr);
  370. return editor_plugins[p_idx];
  371. }
  372. void EditorData::add_custom_type(const String &p_type, const String &p_inherits, const Ref<Script> &p_script, const Ref<Texture> &p_icon) {
  373. ERR_FAIL_COND_MSG(p_script.is_null(), "It's not a reference to a valid Script object.");
  374. CustomType ct;
  375. ct.name = p_type;
  376. ct.icon = p_icon;
  377. ct.script = p_script;
  378. if (!custom_types.has(p_inherits)) {
  379. custom_types[p_inherits] = Vector<CustomType>();
  380. }
  381. custom_types[p_inherits].push_back(ct);
  382. }
  383. Variant EditorData::instance_custom_type(const String &p_type, const String &p_inherits) {
  384. if (get_custom_types().has(p_inherits)) {
  385. for (int i = 0; i < get_custom_types()[p_inherits].size(); i++) {
  386. if (get_custom_types()[p_inherits][i].name == p_type) {
  387. Ref<Script> script = get_custom_types()[p_inherits][i].script;
  388. Variant ob = ClassDB::instance(p_inherits);
  389. ERR_FAIL_COND_V(!ob, Variant());
  390. Node *n = Object::cast_to<Node>(ob);
  391. if (n) {
  392. n->set_name(p_type);
  393. }
  394. ((Object *)ob)->set_script(script.get_ref_ptr());
  395. return ob;
  396. }
  397. }
  398. }
  399. return Variant();
  400. }
  401. void EditorData::remove_custom_type(const String &p_type) {
  402. for (Map<String, Vector<CustomType>>::Element *E = custom_types.front(); E; E = E->next()) {
  403. for (int i = 0; i < E->get().size(); i++) {
  404. if (E->get()[i].name == p_type) {
  405. E->get().remove(i);
  406. if (E->get().empty()) {
  407. custom_types.erase(E->key());
  408. }
  409. return;
  410. }
  411. }
  412. }
  413. }
  414. int EditorData::add_edited_scene(int p_at_pos) {
  415. if (p_at_pos < 0) {
  416. p_at_pos = edited_scene.size();
  417. }
  418. EditedScene es;
  419. es.root = nullptr;
  420. es.path = String();
  421. es.file_modified_time = 0;
  422. es.history_current = -1;
  423. es.version = 0;
  424. es.live_edit_root = NodePath(String("/root"));
  425. if (p_at_pos == edited_scene.size()) {
  426. edited_scene.push_back(es);
  427. } else {
  428. edited_scene.insert(p_at_pos, es);
  429. }
  430. if (current_edited_scene < 0) {
  431. current_edited_scene = 0;
  432. }
  433. return p_at_pos;
  434. }
  435. void EditorData::move_edited_scene_index(int p_idx, int p_to_idx) {
  436. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  437. ERR_FAIL_INDEX(p_to_idx, edited_scene.size());
  438. SWAP(edited_scene.write[p_idx], edited_scene.write[p_to_idx]);
  439. }
  440. void EditorData::remove_scene(int p_idx) {
  441. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  442. if (edited_scene[p_idx].root) {
  443. for (int i = 0; i < editor_plugins.size(); i++) {
  444. editor_plugins[i]->notify_scene_closed(edited_scene[p_idx].root->get_filename());
  445. }
  446. memdelete(edited_scene[p_idx].root);
  447. edited_scene.write[p_idx].root = nullptr;
  448. }
  449. if (current_edited_scene > p_idx) {
  450. current_edited_scene--;
  451. } else if (current_edited_scene == p_idx && current_edited_scene > 0) {
  452. current_edited_scene--;
  453. }
  454. if (edited_scene[p_idx].path != String()) {
  455. ScriptEditor::get_singleton()->close_builtin_scripts_from_scene(edited_scene[p_idx].path);
  456. }
  457. edited_scene.remove(p_idx);
  458. }
  459. bool EditorData::_find_updated_instances(Node *p_root, Node *p_node, Set<String> &checked_paths) {
  460. /*
  461. if (p_root!=p_node && p_node->get_owner()!=p_root && !p_root->is_editable_instance(p_node->get_owner()))
  462. return false;
  463. */
  464. Ref<SceneState> ss;
  465. if (p_node == p_root) {
  466. ss = p_node->get_scene_inherited_state();
  467. } else if (p_node->get_filename() != String()) {
  468. ss = p_node->get_scene_instance_state();
  469. }
  470. if (ss.is_valid()) {
  471. String path = ss->get_path();
  472. if (!checked_paths.has(path)) {
  473. uint64_t modified_time = FileAccess::get_modified_time(path);
  474. if (modified_time != ss->get_last_modified_time()) {
  475. return true; //external scene changed
  476. }
  477. checked_paths.insert(path);
  478. }
  479. }
  480. for (int i = 0; i < p_node->get_child_count(); i++) {
  481. bool found = _find_updated_instances(p_root, p_node->get_child(i), checked_paths);
  482. if (found) {
  483. return true;
  484. }
  485. }
  486. return false;
  487. }
  488. bool EditorData::check_and_update_scene(int p_idx) {
  489. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), false);
  490. if (!edited_scene[p_idx].root) {
  491. return false;
  492. }
  493. Set<String> checked_scenes;
  494. bool must_reload = _find_updated_instances(edited_scene[p_idx].root, edited_scene[p_idx].root, checked_scenes);
  495. if (must_reload) {
  496. Ref<PackedScene> pscene;
  497. pscene.instance();
  498. EditorProgress ep("update_scene", TTR("Updating Scene"), 2);
  499. ep.step(TTR("Storing local changes..."), 0);
  500. //pack first, so it stores diffs to previous version of saved scene
  501. Error err = pscene->pack(edited_scene[p_idx].root);
  502. ERR_FAIL_COND_V(err != OK, false);
  503. ep.step(TTR("Updating scene..."), 1);
  504. Node *new_scene = pscene->instance(PackedScene::GEN_EDIT_STATE_MAIN);
  505. ERR_FAIL_COND_V(!new_scene, false);
  506. //transfer selection
  507. List<Node *> new_selection;
  508. for (List<Node *>::Element *E = edited_scene.write[p_idx].selection.front(); E; E = E->next()) {
  509. NodePath p = edited_scene[p_idx].root->get_path_to(E->get());
  510. Node *new_node = new_scene->get_node(p);
  511. if (new_node) {
  512. new_selection.push_back(new_node);
  513. }
  514. }
  515. new_scene->set_filename(edited_scene[p_idx].root->get_filename());
  516. memdelete(edited_scene[p_idx].root);
  517. edited_scene.write[p_idx].root = new_scene;
  518. if (new_scene->get_filename() != "") {
  519. edited_scene.write[p_idx].path = new_scene->get_filename();
  520. }
  521. edited_scene.write[p_idx].selection = new_selection;
  522. return true;
  523. }
  524. return false;
  525. }
  526. int EditorData::get_edited_scene() const {
  527. return current_edited_scene;
  528. }
  529. void EditorData::set_edited_scene(int p_idx) {
  530. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  531. current_edited_scene = p_idx;
  532. //swap
  533. }
  534. Node *EditorData::get_edited_scene_root(int p_idx) {
  535. if (p_idx < 0) {
  536. ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), nullptr);
  537. return edited_scene[current_edited_scene].root;
  538. } else {
  539. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), nullptr);
  540. return edited_scene[p_idx].root;
  541. }
  542. }
  543. void EditorData::set_edited_scene_root(Node *p_root) {
  544. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  545. edited_scene.write[current_edited_scene].root = p_root;
  546. if (p_root) {
  547. if (p_root->get_filename() != "") {
  548. edited_scene.write[current_edited_scene].path = p_root->get_filename();
  549. } else {
  550. p_root->set_filename(edited_scene[current_edited_scene].path);
  551. }
  552. }
  553. if (edited_scene[current_edited_scene].path != "") {
  554. edited_scene.write[current_edited_scene].file_modified_time = FileAccess::get_modified_time(edited_scene[current_edited_scene].path);
  555. }
  556. }
  557. int EditorData::get_edited_scene_count() const {
  558. return edited_scene.size();
  559. }
  560. Vector<EditorData::EditedScene> EditorData::get_edited_scenes() const {
  561. Vector<EditedScene> out_edited_scenes_list = Vector<EditedScene>();
  562. for (int i = 0; i < edited_scene.size(); i++) {
  563. out_edited_scenes_list.push_back(edited_scene[i]);
  564. }
  565. return out_edited_scenes_list;
  566. }
  567. void EditorData::set_edited_scene_version(uint64_t version, int p_scene_idx) {
  568. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  569. if (p_scene_idx < 0) {
  570. edited_scene.write[current_edited_scene].version = version;
  571. } else {
  572. ERR_FAIL_INDEX(p_scene_idx, edited_scene.size());
  573. edited_scene.write[p_scene_idx].version = version;
  574. }
  575. }
  576. uint64_t EditorData::get_edited_scene_version() const {
  577. ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), 0);
  578. return edited_scene[current_edited_scene].version;
  579. }
  580. uint64_t EditorData::get_scene_version(int p_idx) const {
  581. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), 0);
  582. return edited_scene[p_idx].version;
  583. }
  584. void EditorData::set_scene_modified_time(int p_idx, uint64_t p_time) {
  585. if (p_idx == -1) {
  586. p_idx = current_edited_scene;
  587. }
  588. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  589. edited_scene.write[p_idx].file_modified_time = p_time;
  590. }
  591. uint64_t EditorData::get_scene_modified_time(int p_idx) const {
  592. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), 0);
  593. return edited_scene[p_idx].file_modified_time;
  594. }
  595. String EditorData::get_scene_type(int p_idx) const {
  596. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String());
  597. if (!edited_scene[p_idx].root) {
  598. return "";
  599. }
  600. return edited_scene[p_idx].root->get_class();
  601. }
  602. void EditorData::move_edited_scene_to_index(int p_idx) {
  603. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  604. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  605. EditedScene es = edited_scene[current_edited_scene];
  606. edited_scene.remove(current_edited_scene);
  607. edited_scene.insert(p_idx, es);
  608. current_edited_scene = p_idx;
  609. }
  610. Ref<Script> EditorData::get_scene_root_script(int p_idx) const {
  611. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), Ref<Script>());
  612. if (!edited_scene[p_idx].root) {
  613. return Ref<Script>();
  614. }
  615. Ref<Script> s = edited_scene[p_idx].root->get_script();
  616. if (!s.is_valid() && edited_scene[p_idx].root->get_child_count()) {
  617. Node *n = edited_scene[p_idx].root->get_child(0);
  618. while (!s.is_valid() && n && n->get_filename() == String()) {
  619. s = n->get_script();
  620. n = n->get_parent();
  621. }
  622. }
  623. return s;
  624. }
  625. String EditorData::get_scene_title(int p_idx, bool p_always_strip_extension) const {
  626. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String());
  627. if (!edited_scene[p_idx].root) {
  628. return TTR("[empty]");
  629. }
  630. if (edited_scene[p_idx].root->get_filename() == "") {
  631. return TTR("[unsaved]");
  632. }
  633. const String filename = edited_scene[p_idx].root->get_filename().get_file();
  634. const String basename = filename.get_basename();
  635. if (p_always_strip_extension) {
  636. return basename;
  637. }
  638. // Return the filename including the extension if there's ambiguity (e.g. both `foo.tscn` and `foo.scn` are being edited).
  639. for (int i = 0; i < edited_scene.size(); i++) {
  640. if (i == p_idx) {
  641. // Don't compare the edited scene against itself.
  642. continue;
  643. }
  644. if (edited_scene[i].root && basename == edited_scene[i].root->get_filename().get_file().get_basename()) {
  645. return filename;
  646. }
  647. }
  648. // Else, return just the basename as there's no ambiguity.
  649. return basename;
  650. }
  651. void EditorData::set_scene_path(int p_idx, const String &p_path) {
  652. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  653. edited_scene.write[p_idx].path = p_path;
  654. if (!edited_scene[p_idx].root) {
  655. return;
  656. }
  657. edited_scene[p_idx].root->set_filename(p_path);
  658. }
  659. String EditorData::get_scene_path(int p_idx) const {
  660. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String());
  661. if (edited_scene[p_idx].root) {
  662. if (edited_scene[p_idx].root->get_filename() == "") {
  663. edited_scene[p_idx].root->set_filename(edited_scene[p_idx].path);
  664. } else {
  665. return edited_scene[p_idx].root->get_filename();
  666. }
  667. }
  668. return edited_scene[p_idx].path;
  669. }
  670. void EditorData::set_edited_scene_live_edit_root(const NodePath &p_root) {
  671. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  672. edited_scene.write[current_edited_scene].live_edit_root = p_root;
  673. }
  674. NodePath EditorData::get_edited_scene_live_edit_root() {
  675. ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), String());
  676. return edited_scene[current_edited_scene].live_edit_root;
  677. }
  678. void EditorData::save_edited_scene_state(EditorSelection *p_selection, EditorHistory *p_history, const Dictionary &p_custom) {
  679. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  680. EditedScene &es = edited_scene.write[current_edited_scene];
  681. es.selection = p_selection->get_full_selected_node_list();
  682. es.history_current = p_history->current;
  683. es.history_stored = p_history->history;
  684. es.editor_states = get_editor_states();
  685. es.custom_state = p_custom;
  686. }
  687. Dictionary EditorData::restore_edited_scene_state(EditorSelection *p_selection, EditorHistory *p_history) {
  688. ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), Dictionary());
  689. EditedScene &es = edited_scene.write[current_edited_scene];
  690. p_history->current = es.history_current;
  691. p_history->history = es.history_stored;
  692. p_selection->clear();
  693. for (List<Node *>::Element *E = es.selection.front(); E; E = E->next()) {
  694. p_selection->add_node(E->get());
  695. }
  696. set_editor_states(es.editor_states);
  697. return es.custom_state;
  698. }
  699. void EditorData::clear_edited_scenes() {
  700. for (int i = 0; i < edited_scene.size(); i++) {
  701. if (edited_scene[i].root) {
  702. memdelete(edited_scene[i].root);
  703. }
  704. }
  705. edited_scene.clear();
  706. }
  707. void EditorData::set_plugin_window_layout(Ref<ConfigFile> p_layout) {
  708. for (int i = 0; i < editor_plugins.size(); i++) {
  709. editor_plugins[i]->set_window_layout(p_layout);
  710. }
  711. }
  712. void EditorData::get_plugin_window_layout(Ref<ConfigFile> p_layout) {
  713. for (int i = 0; i < editor_plugins.size(); i++) {
  714. editor_plugins[i]->get_window_layout(p_layout);
  715. }
  716. }
  717. bool EditorData::script_class_is_parent(const String &p_class, const String &p_inherits) {
  718. if (!ScriptServer::is_global_class(p_class)) {
  719. return false;
  720. }
  721. String base = p_class;
  722. while (base != p_inherits) {
  723. if (ClassDB::class_exists(base)) {
  724. return ClassDB::is_parent_class(base, p_inherits);
  725. } else if (ScriptServer::is_global_class(base)) {
  726. base = ScriptServer::get_global_class_base(base);
  727. } else {
  728. return false;
  729. }
  730. }
  731. return true;
  732. }
  733. StringName EditorData::script_class_get_base(const String &p_class) const {
  734. Ref<Script> script = script_class_load_script(p_class);
  735. if (script.is_null()) {
  736. return StringName();
  737. }
  738. Ref<Script> base_script = script->get_base_script();
  739. if (base_script.is_null()) {
  740. return ScriptServer::get_global_class_base(p_class);
  741. }
  742. return script->get_language()->get_global_class_name(base_script->get_path());
  743. }
  744. Variant EditorData::script_class_instance(const String &p_class) {
  745. if (ScriptServer::is_global_class(p_class)) {
  746. Variant obj = ClassDB::instance(ScriptServer::get_global_class_native_base(p_class));
  747. if (obj) {
  748. Ref<Script> script = script_class_load_script(p_class);
  749. if (script.is_valid()) {
  750. ((Object *)obj)->set_script(script.get_ref_ptr());
  751. }
  752. return obj;
  753. }
  754. }
  755. return Variant();
  756. }
  757. Ref<Script> EditorData::script_class_load_script(const String &p_class) const {
  758. if (!ScriptServer::is_global_class(p_class)) {
  759. return Ref<Script>();
  760. }
  761. String path = ScriptServer::get_global_class_path(p_class);
  762. return ResourceLoader::load(path, "Script");
  763. }
  764. void EditorData::script_class_set_icon_path(const String &p_class, const String &p_icon_path) {
  765. _script_class_icon_paths[p_class] = p_icon_path;
  766. }
  767. String EditorData::script_class_get_icon_path(const String &p_class) const {
  768. if (!ScriptServer::is_global_class(p_class)) {
  769. return String();
  770. }
  771. String current = p_class;
  772. String ret = _script_class_icon_paths[current];
  773. while (ret.empty()) {
  774. current = script_class_get_base(current);
  775. if (!ScriptServer::is_global_class(current)) {
  776. return String();
  777. }
  778. ret = _script_class_icon_paths.has(current) ? _script_class_icon_paths[current] : String();
  779. }
  780. return ret;
  781. }
  782. StringName EditorData::script_class_get_name(const String &p_path) const {
  783. return _script_class_file_to_path.has(p_path) ? _script_class_file_to_path[p_path] : StringName();
  784. }
  785. void EditorData::script_class_set_name(const String &p_path, const StringName &p_class) {
  786. _script_class_file_to_path[p_path] = p_class;
  787. }
  788. void EditorData::script_class_save_icon_paths() {
  789. List<StringName> keys;
  790. _script_class_icon_paths.get_key_list(&keys);
  791. Dictionary d;
  792. for (List<StringName>::Element *E = keys.front(); E; E = E->next()) {
  793. if (ScriptServer::is_global_class(E->get())) {
  794. d[E->get()] = _script_class_icon_paths[E->get()];
  795. }
  796. }
  797. Dictionary old;
  798. if (ProjectSettings::get_singleton()->has_setting("_global_script_class_icons")) {
  799. old = ProjectSettings::get_singleton()->get("_global_script_class_icons");
  800. }
  801. if ((!old.empty() || d.empty()) && d.hash() == old.hash()) {
  802. return;
  803. }
  804. if (d.empty()) {
  805. if (ProjectSettings::get_singleton()->has_setting("_global_script_class_icons")) {
  806. ProjectSettings::get_singleton()->clear("_global_script_class_icons");
  807. }
  808. } else {
  809. ProjectSettings::get_singleton()->set("_global_script_class_icons", d);
  810. }
  811. ProjectSettings::get_singleton()->save();
  812. }
  813. void EditorData::script_class_load_icon_paths() {
  814. script_class_clear_icon_paths();
  815. if (ProjectSettings::get_singleton()->has_setting("_global_script_class_icons")) {
  816. Dictionary d = ProjectSettings::get_singleton()->get("_global_script_class_icons");
  817. List<Variant> keys;
  818. d.get_key_list(&keys);
  819. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  820. String name = E->get().operator String();
  821. _script_class_icon_paths[name] = d[name];
  822. String path = ScriptServer::get_global_class_path(name);
  823. script_class_set_name(path, name);
  824. }
  825. }
  826. }
  827. EditorData::EditorData() {
  828. current_edited_scene = -1;
  829. //load_imported_scenes_from_globals();
  830. script_class_load_icon_paths();
  831. }
  832. ///////////
  833. void EditorSelection::_node_removed(Node *p_node) {
  834. if (!selection.has(p_node)) {
  835. return;
  836. }
  837. Object *meta = selection[p_node];
  838. if (meta) {
  839. memdelete(meta);
  840. }
  841. selection.erase(p_node);
  842. changed = true;
  843. nl_changed = true;
  844. }
  845. void EditorSelection::add_node(Node *p_node) {
  846. ERR_FAIL_NULL(p_node);
  847. ERR_FAIL_COND(!p_node->is_inside_tree());
  848. if (selection.has(p_node)) {
  849. return;
  850. }
  851. changed = true;
  852. nl_changed = true;
  853. Object *meta = nullptr;
  854. for (List<Object *>::Element *E = editor_plugins.front(); E; E = E->next()) {
  855. meta = E->get()->call("_get_editor_data", p_node);
  856. if (meta) {
  857. break;
  858. }
  859. }
  860. selection[p_node] = meta;
  861. p_node->connect("tree_exiting", this, "_node_removed", varray(p_node), CONNECT_ONESHOT);
  862. //emit_signal("selection_changed");
  863. }
  864. void EditorSelection::remove_node(Node *p_node) {
  865. ERR_FAIL_NULL(p_node);
  866. if (!selection.has(p_node)) {
  867. return;
  868. }
  869. changed = true;
  870. nl_changed = true;
  871. Object *meta = selection[p_node];
  872. if (meta) {
  873. memdelete(meta);
  874. }
  875. selection.erase(p_node);
  876. p_node->disconnect("tree_exiting", this, "_node_removed");
  877. //emit_signal("selection_changed");
  878. }
  879. bool EditorSelection::is_selected(Node *p_node) const {
  880. return selection.has(p_node);
  881. }
  882. Array EditorSelection::_get_transformable_selected_nodes() {
  883. Array ret;
  884. for (List<Node *>::Element *E = selected_node_list.front(); E; E = E->next()) {
  885. ret.push_back(E->get());
  886. }
  887. return ret;
  888. }
  889. Array EditorSelection::get_selected_nodes() {
  890. Array ret;
  891. for (Map<Node *, Object *>::Element *E = selection.front(); E; E = E->next()) {
  892. ret.push_back(E->key());
  893. }
  894. return ret;
  895. }
  896. void EditorSelection::_bind_methods() {
  897. ClassDB::bind_method(D_METHOD("_node_removed"), &EditorSelection::_node_removed);
  898. ClassDB::bind_method(D_METHOD("clear"), &EditorSelection::clear);
  899. ClassDB::bind_method(D_METHOD("add_node", "node"), &EditorSelection::add_node);
  900. ClassDB::bind_method(D_METHOD("remove_node", "node"), &EditorSelection::remove_node);
  901. ClassDB::bind_method(D_METHOD("get_selected_nodes"), &EditorSelection::get_selected_nodes);
  902. ClassDB::bind_method(D_METHOD("get_transformable_selected_nodes"), &EditorSelection::_get_transformable_selected_nodes);
  903. ClassDB::bind_method(D_METHOD("_emit_change"), &EditorSelection::_emit_change);
  904. ADD_SIGNAL(MethodInfo("selection_changed"));
  905. }
  906. void EditorSelection::add_editor_plugin(Object *p_object) {
  907. editor_plugins.push_back(p_object);
  908. }
  909. void EditorSelection::_update_nl() {
  910. if (!nl_changed) {
  911. return;
  912. }
  913. selected_node_list.clear();
  914. for (Map<Node *, Object *>::Element *E = selection.front(); E; E = E->next()) {
  915. Node *parent = E->key();
  916. parent = parent->get_parent();
  917. bool skip = false;
  918. while (parent) {
  919. if (selection.has(parent)) {
  920. skip = true;
  921. break;
  922. }
  923. parent = parent->get_parent();
  924. }
  925. if (skip) {
  926. continue;
  927. }
  928. selected_node_list.push_back(E->key());
  929. }
  930. nl_changed = true;
  931. }
  932. void EditorSelection::update() {
  933. _update_nl();
  934. if (!changed) {
  935. return;
  936. }
  937. changed = false;
  938. if (!emitted) {
  939. emitted = true;
  940. call_deferred("_emit_change");
  941. }
  942. }
  943. void EditorSelection::_emit_change() {
  944. emit_signal("selection_changed");
  945. emitted = false;
  946. }
  947. List<Node *> &EditorSelection::get_selected_node_list() {
  948. if (changed) {
  949. update();
  950. } else {
  951. _update_nl();
  952. }
  953. return selected_node_list;
  954. }
  955. List<Node *> EditorSelection::get_full_selected_node_list() {
  956. List<Node *> node_list;
  957. for (Map<Node *, Object *>::Element *E = selection.front(); E; E = E->next()) {
  958. node_list.push_back(E->key());
  959. }
  960. return node_list;
  961. }
  962. void EditorSelection::clear() {
  963. while (!selection.empty()) {
  964. remove_node(selection.front()->key());
  965. }
  966. changed = true;
  967. nl_changed = true;
  968. }
  969. EditorSelection::EditorSelection() {
  970. emitted = false;
  971. changed = false;
  972. nl_changed = false;
  973. }
  974. EditorSelection::~EditorSelection() {
  975. clear();
  976. }