editor_data.cpp 30 KB

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