editor_data.cpp 34 KB

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