editor_data.cpp 33 KB

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