editor_data.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  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::notify_scene_saved(const String &p_path) {
  313. for (int i = 0; i < editor_plugins.size(); i++) {
  314. editor_plugins[i]->notify_scene_saved(p_path);
  315. }
  316. }
  317. void EditorData::clear_editor_states() {
  318. for (int i = 0; i < editor_plugins.size(); i++) {
  319. editor_plugins[i]->clear();
  320. }
  321. }
  322. void EditorData::save_editor_external_data() {
  323. for (int i = 0; i < editor_plugins.size(); i++) {
  324. editor_plugins[i]->save_external_data();
  325. }
  326. }
  327. void EditorData::apply_changes_in_editors() {
  328. for (int i = 0; i < editor_plugins.size(); i++) {
  329. editor_plugins[i]->apply_changes();
  330. }
  331. }
  332. void EditorData::save_editor_global_states() {
  333. for (int i = 0; i < editor_plugins.size(); i++) {
  334. editor_plugins[i]->save_global_state();
  335. }
  336. }
  337. void EditorData::restore_editor_global_states() {
  338. for (int i = 0; i < editor_plugins.size(); i++) {
  339. editor_plugins[i]->restore_global_state();
  340. }
  341. }
  342. void EditorData::paste_object_params(Object *p_object) {
  343. ERR_FAIL_NULL(p_object);
  344. undo_redo.create_action(TTR("Paste Params"));
  345. for (List<PropertyData>::Element *E = clipboard.front(); E; E = E->next()) {
  346. String name = E->get().name;
  347. undo_redo.add_do_property(p_object, name, E->get().value);
  348. undo_redo.add_undo_property(p_object, name, p_object->get(name));
  349. }
  350. undo_redo.commit_action();
  351. }
  352. bool EditorData::call_build() {
  353. bool result = true;
  354. for (int i = 0; i < editor_plugins.size() && result; i++) {
  355. result &= editor_plugins[i]->build();
  356. }
  357. return result;
  358. }
  359. UndoRedo &EditorData::get_undo_redo() {
  360. return undo_redo;
  361. }
  362. void EditorData::remove_editor_plugin(EditorPlugin *p_plugin) {
  363. p_plugin->undo_redo = nullptr;
  364. editor_plugins.erase(p_plugin);
  365. }
  366. void EditorData::add_editor_plugin(EditorPlugin *p_plugin) {
  367. p_plugin->undo_redo = &undo_redo;
  368. editor_plugins.push_back(p_plugin);
  369. }
  370. int EditorData::get_editor_plugin_count() const {
  371. return editor_plugins.size();
  372. }
  373. EditorPlugin *EditorData::get_editor_plugin(int p_idx) {
  374. ERR_FAIL_INDEX_V(p_idx, editor_plugins.size(), nullptr);
  375. return editor_plugins[p_idx];
  376. }
  377. void EditorData::add_custom_type(const String &p_type, const String &p_inherits, const Ref<Script> &p_script, const Ref<Texture> &p_icon) {
  378. ERR_FAIL_COND_MSG(p_script.is_null(), "It's not a reference to a valid Script object.");
  379. CustomType ct;
  380. ct.name = p_type;
  381. ct.icon = p_icon;
  382. ct.script = p_script;
  383. if (!custom_types.has(p_inherits)) {
  384. custom_types[p_inherits] = Vector<CustomType>();
  385. }
  386. custom_types[p_inherits].push_back(ct);
  387. }
  388. Variant EditorData::instance_custom_type(const String &p_type, const String &p_inherits) {
  389. if (get_custom_types().has(p_inherits)) {
  390. for (int i = 0; i < get_custom_types()[p_inherits].size(); i++) {
  391. if (get_custom_types()[p_inherits][i].name == p_type) {
  392. Ref<Script> script = get_custom_types()[p_inherits][i].script;
  393. Variant ob = ClassDB::instance(p_inherits);
  394. ERR_FAIL_COND_V(!ob, Variant());
  395. Node *n = Object::cast_to<Node>(ob);
  396. if (n) {
  397. n->set_name(p_type);
  398. }
  399. ((Object *)ob)->set_script(script.get_ref_ptr());
  400. return ob;
  401. }
  402. }
  403. }
  404. return Variant();
  405. }
  406. void EditorData::remove_custom_type(const String &p_type) {
  407. for (Map<String, Vector<CustomType>>::Element *E = custom_types.front(); E; E = E->next()) {
  408. for (int i = 0; i < E->get().size(); i++) {
  409. if (E->get()[i].name == p_type) {
  410. E->get().remove(i);
  411. if (E->get().empty()) {
  412. custom_types.erase(E->key());
  413. }
  414. return;
  415. }
  416. }
  417. }
  418. }
  419. int EditorData::add_edited_scene(int p_at_pos) {
  420. if (p_at_pos < 0) {
  421. p_at_pos = edited_scene.size();
  422. }
  423. EditedScene es;
  424. es.root = nullptr;
  425. es.path = String();
  426. es.file_modified_time = 0;
  427. es.history_current = -1;
  428. es.version = 0;
  429. es.live_edit_root = NodePath(String("/root"));
  430. if (p_at_pos == edited_scene.size()) {
  431. edited_scene.push_back(es);
  432. } else {
  433. edited_scene.insert(p_at_pos, es);
  434. }
  435. if (current_edited_scene < 0) {
  436. current_edited_scene = 0;
  437. }
  438. return p_at_pos;
  439. }
  440. void EditorData::move_edited_scene_index(int p_idx, int p_to_idx) {
  441. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  442. ERR_FAIL_INDEX(p_to_idx, edited_scene.size());
  443. SWAP(edited_scene.write[p_idx], edited_scene.write[p_to_idx]);
  444. }
  445. void EditorData::remove_scene(int p_idx) {
  446. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  447. if (edited_scene[p_idx].root) {
  448. for (int i = 0; i < editor_plugins.size(); i++) {
  449. editor_plugins[i]->notify_scene_closed(edited_scene[p_idx].root->get_filename());
  450. }
  451. memdelete(edited_scene[p_idx].root);
  452. edited_scene.write[p_idx].root = nullptr;
  453. }
  454. if (current_edited_scene > p_idx) {
  455. current_edited_scene--;
  456. } else if (current_edited_scene == p_idx && current_edited_scene > 0) {
  457. current_edited_scene--;
  458. }
  459. if (edited_scene[p_idx].path != String()) {
  460. ScriptEditor::get_singleton()->close_builtin_scripts_from_scene(edited_scene[p_idx].path);
  461. }
  462. edited_scene.remove(p_idx);
  463. }
  464. bool EditorData::_find_updated_instances(Node *p_root, Node *p_node, Set<String> &checked_paths) {
  465. /*
  466. if (p_root!=p_node && p_node->get_owner()!=p_root && !p_root->is_editable_instance(p_node->get_owner()))
  467. return false;
  468. */
  469. Ref<SceneState> ss;
  470. if (p_node == p_root) {
  471. ss = p_node->get_scene_inherited_state();
  472. } else if (p_node->get_filename() != String()) {
  473. ss = p_node->get_scene_instance_state();
  474. }
  475. if (ss.is_valid()) {
  476. String path = ss->get_path();
  477. if (!checked_paths.has(path)) {
  478. uint64_t modified_time = FileAccess::get_modified_time(path);
  479. if (modified_time != ss->get_last_modified_time()) {
  480. return true; //external scene changed
  481. }
  482. checked_paths.insert(path);
  483. }
  484. }
  485. for (int i = 0; i < p_node->get_child_count(); i++) {
  486. bool found = _find_updated_instances(p_root, p_node->get_child(i), checked_paths);
  487. if (found) {
  488. return true;
  489. }
  490. }
  491. return false;
  492. }
  493. bool EditorData::check_and_update_scene(int p_idx) {
  494. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), false);
  495. if (!edited_scene[p_idx].root) {
  496. return false;
  497. }
  498. Set<String> checked_scenes;
  499. bool must_reload = _find_updated_instances(edited_scene[p_idx].root, edited_scene[p_idx].root, checked_scenes);
  500. if (must_reload) {
  501. Ref<PackedScene> pscene;
  502. pscene.instance();
  503. EditorProgress ep("update_scene", TTR("Updating Scene"), 2);
  504. ep.step(TTR("Storing local changes..."), 0);
  505. //pack first, so it stores diffs to previous version of saved scene
  506. Error err = pscene->pack(edited_scene[p_idx].root);
  507. ERR_FAIL_COND_V(err != OK, false);
  508. ep.step(TTR("Updating scene..."), 1);
  509. Node *new_scene = pscene->instance(PackedScene::GEN_EDIT_STATE_MAIN);
  510. ERR_FAIL_COND_V(!new_scene, false);
  511. //transfer selection
  512. List<Node *> new_selection;
  513. for (List<Node *>::Element *E = edited_scene.write[p_idx].selection.front(); E; E = E->next()) {
  514. NodePath p = edited_scene[p_idx].root->get_path_to(E->get());
  515. Node *new_node = new_scene->get_node(p);
  516. if (new_node) {
  517. new_selection.push_back(new_node);
  518. }
  519. }
  520. new_scene->set_filename(edited_scene[p_idx].root->get_filename());
  521. memdelete(edited_scene[p_idx].root);
  522. edited_scene.write[p_idx].root = new_scene;
  523. if (new_scene->get_filename() != "") {
  524. edited_scene.write[p_idx].path = new_scene->get_filename();
  525. }
  526. edited_scene.write[p_idx].selection = new_selection;
  527. return true;
  528. }
  529. return false;
  530. }
  531. int EditorData::get_edited_scene() const {
  532. return current_edited_scene;
  533. }
  534. void EditorData::set_edited_scene(int p_idx) {
  535. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  536. current_edited_scene = p_idx;
  537. //swap
  538. }
  539. Node *EditorData::get_edited_scene_root(int p_idx) {
  540. if (p_idx < 0) {
  541. ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), nullptr);
  542. return edited_scene[current_edited_scene].root;
  543. } else {
  544. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), nullptr);
  545. return edited_scene[p_idx].root;
  546. }
  547. }
  548. void EditorData::set_edited_scene_root(Node *p_root) {
  549. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  550. edited_scene.write[current_edited_scene].root = p_root;
  551. if (p_root) {
  552. if (p_root->get_filename() != "") {
  553. edited_scene.write[current_edited_scene].path = p_root->get_filename();
  554. } else {
  555. p_root->set_filename(edited_scene[current_edited_scene].path);
  556. }
  557. }
  558. if (edited_scene[current_edited_scene].path != "") {
  559. edited_scene.write[current_edited_scene].file_modified_time = FileAccess::get_modified_time(edited_scene[current_edited_scene].path);
  560. }
  561. }
  562. int EditorData::get_edited_scene_count() const {
  563. return edited_scene.size();
  564. }
  565. Vector<EditorData::EditedScene> EditorData::get_edited_scenes() const {
  566. Vector<EditedScene> out_edited_scenes_list = Vector<EditedScene>();
  567. for (int i = 0; i < edited_scene.size(); i++) {
  568. out_edited_scenes_list.push_back(edited_scene[i]);
  569. }
  570. return out_edited_scenes_list;
  571. }
  572. void EditorData::set_edited_scene_version(uint64_t version, int p_scene_idx) {
  573. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  574. if (p_scene_idx < 0) {
  575. edited_scene.write[current_edited_scene].version = version;
  576. } else {
  577. ERR_FAIL_INDEX(p_scene_idx, edited_scene.size());
  578. edited_scene.write[p_scene_idx].version = version;
  579. }
  580. }
  581. uint64_t EditorData::get_edited_scene_version() const {
  582. ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), 0);
  583. return edited_scene[current_edited_scene].version;
  584. }
  585. uint64_t EditorData::get_scene_version(int p_idx) const {
  586. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), 0);
  587. return edited_scene[p_idx].version;
  588. }
  589. void EditorData::set_scene_modified_time(int p_idx, uint64_t p_time) {
  590. if (p_idx == -1) {
  591. p_idx = current_edited_scene;
  592. }
  593. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  594. edited_scene.write[p_idx].file_modified_time = p_time;
  595. }
  596. uint64_t EditorData::get_scene_modified_time(int p_idx) const {
  597. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), 0);
  598. return edited_scene[p_idx].file_modified_time;
  599. }
  600. String EditorData::get_scene_type(int p_idx) const {
  601. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String());
  602. if (!edited_scene[p_idx].root) {
  603. return "";
  604. }
  605. return edited_scene[p_idx].root->get_class();
  606. }
  607. void EditorData::move_edited_scene_to_index(int p_idx) {
  608. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  609. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  610. EditedScene es = edited_scene[current_edited_scene];
  611. edited_scene.remove(current_edited_scene);
  612. edited_scene.insert(p_idx, es);
  613. current_edited_scene = p_idx;
  614. }
  615. Ref<Script> EditorData::get_scene_root_script(int p_idx) const {
  616. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), Ref<Script>());
  617. if (!edited_scene[p_idx].root) {
  618. return Ref<Script>();
  619. }
  620. Ref<Script> s = edited_scene[p_idx].root->get_script();
  621. if (!s.is_valid() && edited_scene[p_idx].root->get_child_count()) {
  622. Node *n = edited_scene[p_idx].root->get_child(0);
  623. while (!s.is_valid() && n && n->get_filename() == String()) {
  624. s = n->get_script();
  625. n = n->get_parent();
  626. }
  627. }
  628. return s;
  629. }
  630. String EditorData::get_scene_title(int p_idx, bool p_always_strip_extension) const {
  631. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String());
  632. if (!edited_scene[p_idx].root) {
  633. return TTR("[empty]");
  634. }
  635. if (edited_scene[p_idx].root->get_filename() == "") {
  636. return TTR("[unsaved]");
  637. }
  638. const String filename = edited_scene[p_idx].root->get_filename().get_file();
  639. const String basename = filename.get_basename();
  640. if (p_always_strip_extension) {
  641. return basename;
  642. }
  643. // Return the filename including the extension if there's ambiguity (e.g. both `foo.tscn` and `foo.scn` are being edited).
  644. for (int i = 0; i < edited_scene.size(); i++) {
  645. if (i == p_idx) {
  646. // Don't compare the edited scene against itself.
  647. continue;
  648. }
  649. if (edited_scene[i].root && basename == edited_scene[i].root->get_filename().get_file().get_basename()) {
  650. return filename;
  651. }
  652. }
  653. // Else, return just the basename as there's no ambiguity.
  654. return basename;
  655. }
  656. void EditorData::set_scene_path(int p_idx, const String &p_path) {
  657. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  658. edited_scene.write[p_idx].path = p_path;
  659. if (!edited_scene[p_idx].root) {
  660. return;
  661. }
  662. edited_scene[p_idx].root->set_filename(p_path);
  663. }
  664. String EditorData::get_scene_path(int p_idx) const {
  665. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String());
  666. if (edited_scene[p_idx].root) {
  667. if (edited_scene[p_idx].root->get_filename() == "") {
  668. edited_scene[p_idx].root->set_filename(edited_scene[p_idx].path);
  669. } else {
  670. return edited_scene[p_idx].root->get_filename();
  671. }
  672. }
  673. return edited_scene[p_idx].path;
  674. }
  675. void EditorData::set_edited_scene_live_edit_root(const NodePath &p_root) {
  676. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  677. edited_scene.write[current_edited_scene].live_edit_root = p_root;
  678. }
  679. NodePath EditorData::get_edited_scene_live_edit_root() {
  680. ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), String());
  681. return edited_scene[current_edited_scene].live_edit_root;
  682. }
  683. void EditorData::save_edited_scene_state(EditorSelection *p_selection, EditorHistory *p_history, const Dictionary &p_custom) {
  684. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  685. EditedScene &es = edited_scene.write[current_edited_scene];
  686. es.selection = p_selection->get_full_selected_node_list();
  687. es.history_current = p_history->current;
  688. es.history_stored = p_history->history;
  689. es.editor_states = get_editor_states();
  690. es.custom_state = p_custom;
  691. }
  692. Dictionary EditorData::restore_edited_scene_state(EditorSelection *p_selection, EditorHistory *p_history) {
  693. ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), Dictionary());
  694. EditedScene &es = edited_scene.write[current_edited_scene];
  695. p_history->current = es.history_current;
  696. p_history->history = es.history_stored;
  697. p_selection->clear();
  698. for (List<Node *>::Element *E = es.selection.front(); E; E = E->next()) {
  699. p_selection->add_node(E->get());
  700. }
  701. set_editor_states(es.editor_states);
  702. return es.custom_state;
  703. }
  704. void EditorData::clear_edited_scenes() {
  705. for (int i = 0; i < edited_scene.size(); i++) {
  706. if (edited_scene[i].root) {
  707. memdelete(edited_scene[i].root);
  708. }
  709. }
  710. edited_scene.clear();
  711. }
  712. void EditorData::set_plugin_window_layout(Ref<ConfigFile> p_layout) {
  713. for (int i = 0; i < editor_plugins.size(); i++) {
  714. editor_plugins[i]->set_window_layout(p_layout);
  715. }
  716. }
  717. void EditorData::get_plugin_window_layout(Ref<ConfigFile> p_layout) {
  718. for (int i = 0; i < editor_plugins.size(); i++) {
  719. editor_plugins[i]->get_window_layout(p_layout);
  720. }
  721. }
  722. bool EditorData::script_class_is_parent(const String &p_class, const String &p_inherits) {
  723. if (!ScriptServer::is_global_class(p_class)) {
  724. return false;
  725. }
  726. String base = p_class;
  727. while (base != p_inherits) {
  728. if (ClassDB::class_exists(base)) {
  729. return ClassDB::is_parent_class(base, p_inherits);
  730. } else if (ScriptServer::is_global_class(base)) {
  731. base = ScriptServer::get_global_class_base(base);
  732. } else {
  733. return false;
  734. }
  735. }
  736. return true;
  737. }
  738. StringName EditorData::script_class_get_base(const String &p_class) const {
  739. Ref<Script> script = script_class_load_script(p_class);
  740. if (script.is_null()) {
  741. return StringName();
  742. }
  743. Ref<Script> base_script = script->get_base_script();
  744. if (base_script.is_null()) {
  745. return ScriptServer::get_global_class_base(p_class);
  746. }
  747. return script->get_language()->get_global_class_name(base_script->get_path());
  748. }
  749. Variant EditorData::script_class_instance(const String &p_class) {
  750. if (ScriptServer::is_global_class(p_class)) {
  751. Variant obj = ClassDB::instance(ScriptServer::get_global_class_native_base(p_class));
  752. if (obj) {
  753. Ref<Script> script = script_class_load_script(p_class);
  754. if (script.is_valid()) {
  755. ((Object *)obj)->set_script(script.get_ref_ptr());
  756. }
  757. return obj;
  758. }
  759. }
  760. return Variant();
  761. }
  762. Ref<Script> EditorData::script_class_load_script(const String &p_class) const {
  763. if (!ScriptServer::is_global_class(p_class)) {
  764. return Ref<Script>();
  765. }
  766. String path = ScriptServer::get_global_class_path(p_class);
  767. return ResourceLoader::load(path, "Script");
  768. }
  769. void EditorData::script_class_set_icon_path(const String &p_class, const String &p_icon_path) {
  770. _script_class_icon_paths[p_class] = p_icon_path;
  771. }
  772. String EditorData::script_class_get_icon_path(const String &p_class) const {
  773. if (!ScriptServer::is_global_class(p_class)) {
  774. return String();
  775. }
  776. String current = p_class;
  777. String ret = _script_class_icon_paths[current];
  778. while (ret.empty()) {
  779. current = script_class_get_base(current);
  780. if (!ScriptServer::is_global_class(current)) {
  781. return String();
  782. }
  783. ret = _script_class_icon_paths.has(current) ? _script_class_icon_paths[current] : String();
  784. }
  785. return ret;
  786. }
  787. StringName EditorData::script_class_get_name(const String &p_path) const {
  788. return _script_class_file_to_path.has(p_path) ? _script_class_file_to_path[p_path] : StringName();
  789. }
  790. void EditorData::script_class_set_name(const String &p_path, const StringName &p_class) {
  791. _script_class_file_to_path[p_path] = p_class;
  792. }
  793. void EditorData::script_class_save_icon_paths() {
  794. List<StringName> keys;
  795. _script_class_icon_paths.get_key_list(&keys);
  796. Dictionary d;
  797. for (List<StringName>::Element *E = keys.front(); E; E = E->next()) {
  798. if (ScriptServer::is_global_class(E->get())) {
  799. d[E->get()] = _script_class_icon_paths[E->get()];
  800. }
  801. }
  802. Dictionary old;
  803. if (ProjectSettings::get_singleton()->has_setting("_global_script_class_icons")) {
  804. old = ProjectSettings::get_singleton()->get("_global_script_class_icons");
  805. }
  806. if ((!old.empty() || d.empty()) && d.hash() == old.hash()) {
  807. return;
  808. }
  809. if (d.empty()) {
  810. if (ProjectSettings::get_singleton()->has_setting("_global_script_class_icons")) {
  811. ProjectSettings::get_singleton()->clear("_global_script_class_icons");
  812. }
  813. } else {
  814. ProjectSettings::get_singleton()->set("_global_script_class_icons", d);
  815. }
  816. ProjectSettings::get_singleton()->save();
  817. }
  818. void EditorData::script_class_load_icon_paths() {
  819. script_class_clear_icon_paths();
  820. if (ProjectSettings::get_singleton()->has_setting("_global_script_class_icons")) {
  821. Dictionary d = ProjectSettings::get_singleton()->get("_global_script_class_icons");
  822. List<Variant> keys;
  823. d.get_key_list(&keys);
  824. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  825. String name = E->get().operator String();
  826. _script_class_icon_paths[name] = d[name];
  827. String path = ScriptServer::get_global_class_path(name);
  828. script_class_set_name(path, name);
  829. }
  830. }
  831. }
  832. EditorData::EditorData() {
  833. current_edited_scene = -1;
  834. //load_imported_scenes_from_globals();
  835. script_class_load_icon_paths();
  836. }
  837. ///////////
  838. void EditorSelection::_node_removed(Node *p_node) {
  839. if (!selection.has(p_node)) {
  840. return;
  841. }
  842. Object *meta = selection[p_node];
  843. if (meta) {
  844. memdelete(meta);
  845. }
  846. selection.erase(p_node);
  847. changed = true;
  848. nl_changed = true;
  849. }
  850. void EditorSelection::add_node(Node *p_node) {
  851. ERR_FAIL_NULL(p_node);
  852. ERR_FAIL_COND(!p_node->is_inside_tree());
  853. if (selection.has(p_node)) {
  854. return;
  855. }
  856. changed = true;
  857. nl_changed = true;
  858. Object *meta = nullptr;
  859. for (List<Object *>::Element *E = editor_plugins.front(); E; E = E->next()) {
  860. meta = E->get()->call("_get_editor_data", p_node);
  861. if (meta) {
  862. break;
  863. }
  864. }
  865. selection[p_node] = meta;
  866. p_node->connect("tree_exiting", this, "_node_removed", varray(p_node), CONNECT_ONESHOT);
  867. //emit_signal("selection_changed");
  868. }
  869. void EditorSelection::remove_node(Node *p_node) {
  870. ERR_FAIL_NULL(p_node);
  871. if (!selection.has(p_node)) {
  872. return;
  873. }
  874. changed = true;
  875. nl_changed = true;
  876. Object *meta = selection[p_node];
  877. if (meta) {
  878. memdelete(meta);
  879. }
  880. selection.erase(p_node);
  881. p_node->disconnect("tree_exiting", this, "_node_removed");
  882. //emit_signal("selection_changed");
  883. }
  884. bool EditorSelection::is_selected(Node *p_node) const {
  885. return selection.has(p_node);
  886. }
  887. Array EditorSelection::_get_transformable_selected_nodes() {
  888. Array ret;
  889. for (List<Node *>::Element *E = selected_node_list.front(); E; E = E->next()) {
  890. ret.push_back(E->get());
  891. }
  892. return ret;
  893. }
  894. Array EditorSelection::get_selected_nodes() {
  895. Array ret;
  896. for (Map<Node *, Object *>::Element *E = selection.front(); E; E = E->next()) {
  897. ret.push_back(E->key());
  898. }
  899. return ret;
  900. }
  901. void EditorSelection::_bind_methods() {
  902. ClassDB::bind_method(D_METHOD("_node_removed"), &EditorSelection::_node_removed);
  903. ClassDB::bind_method(D_METHOD("clear"), &EditorSelection::clear);
  904. ClassDB::bind_method(D_METHOD("add_node", "node"), &EditorSelection::add_node);
  905. ClassDB::bind_method(D_METHOD("remove_node", "node"), &EditorSelection::remove_node);
  906. ClassDB::bind_method(D_METHOD("get_selected_nodes"), &EditorSelection::get_selected_nodes);
  907. ClassDB::bind_method(D_METHOD("get_transformable_selected_nodes"), &EditorSelection::_get_transformable_selected_nodes);
  908. ClassDB::bind_method(D_METHOD("_emit_change"), &EditorSelection::_emit_change);
  909. ADD_SIGNAL(MethodInfo("selection_changed"));
  910. }
  911. void EditorSelection::add_editor_plugin(Object *p_object) {
  912. editor_plugins.push_back(p_object);
  913. }
  914. void EditorSelection::_update_nl() {
  915. if (!nl_changed) {
  916. return;
  917. }
  918. selected_node_list.clear();
  919. for (Map<Node *, Object *>::Element *E = selection.front(); E; E = E->next()) {
  920. Node *parent = E->key();
  921. parent = parent->get_parent();
  922. bool skip = false;
  923. while (parent) {
  924. if (selection.has(parent)) {
  925. skip = true;
  926. break;
  927. }
  928. parent = parent->get_parent();
  929. }
  930. if (skip) {
  931. continue;
  932. }
  933. selected_node_list.push_back(E->key());
  934. }
  935. nl_changed = true;
  936. }
  937. void EditorSelection::update() {
  938. _update_nl();
  939. if (!changed) {
  940. return;
  941. }
  942. changed = false;
  943. if (!emitted) {
  944. emitted = true;
  945. call_deferred("_emit_change");
  946. }
  947. }
  948. void EditorSelection::_emit_change() {
  949. emit_signal("selection_changed");
  950. emitted = false;
  951. }
  952. List<Node *> &EditorSelection::get_selected_node_list() {
  953. if (changed) {
  954. update();
  955. } else {
  956. _update_nl();
  957. }
  958. return selected_node_list;
  959. }
  960. List<Node *> EditorSelection::get_full_selected_node_list() {
  961. List<Node *> node_list;
  962. for (Map<Node *, Object *>::Element *E = selection.front(); E; E = E->next()) {
  963. node_list.push_back(E->key());
  964. }
  965. return node_list;
  966. }
  967. void EditorSelection::clear() {
  968. while (!selection.empty()) {
  969. remove_node(selection.front()->key());
  970. }
  971. changed = true;
  972. nl_changed = true;
  973. }
  974. EditorSelection::EditorSelection() {
  975. emitted = false;
  976. changed = false;
  977. nl_changed = false;
  978. }
  979. EditorSelection::~EditorSelection() {
  980. clear();
  981. }