editor_data.cpp 31 KB

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