editor_data.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. /*************************************************************************/
  2. /* editor_data.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "editor_node.h"
  32. #include "editor_settings.h"
  33. #include "io/resource_loader.h"
  34. #include "os/dir_access.h"
  35. #include "os/file_access.h"
  36. #include "project_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[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) {
  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. History h;
  79. bool has_prev = current >= 0 && current < history.size();
  80. if (has_prev) {
  81. history.resize(current + 1); //clip history to next
  82. }
  83. if (p_property != "" && has_prev) {
  84. //add a sub property
  85. History &pr = history[current];
  86. h = pr;
  87. h.path.resize(h.level + 1);
  88. h.path.push_back(o);
  89. h.level++;
  90. } else if (p_level_change != -1 && has_prev) {
  91. //add a sub property
  92. History &pr = history[current];
  93. h = pr;
  94. ERR_FAIL_INDEX(p_level_change, h.path.size());
  95. h.level = p_level_change;
  96. } else {
  97. //add a new node
  98. h.path.push_back(o);
  99. h.level = 0;
  100. }
  101. history.push_back(h);
  102. current++;
  103. }
  104. void EditorHistory::add_object(ObjectID p_object) {
  105. _add_object(p_object, "", -1);
  106. }
  107. void EditorHistory::add_object(ObjectID p_object, const String &p_subprop) {
  108. _add_object(p_object, p_subprop, -1);
  109. }
  110. void EditorHistory::add_object(ObjectID p_object, int p_relevel) {
  111. _add_object(p_object, "", p_relevel);
  112. }
  113. int EditorHistory::get_history_len() {
  114. return history.size();
  115. }
  116. int EditorHistory::get_history_pos() {
  117. return current;
  118. }
  119. ObjectID EditorHistory::get_history_obj(int p_obj) const {
  120. ERR_FAIL_INDEX_V(p_obj, history.size(), 0);
  121. ERR_FAIL_INDEX_V(history[p_obj].level, history[p_obj].path.size(), 0);
  122. return history[p_obj].path[history[p_obj].level].object;
  123. }
  124. bool EditorHistory::is_at_beginning() const {
  125. return current <= 0;
  126. }
  127. bool EditorHistory::is_at_end() const {
  128. return ((current + 1) >= history.size());
  129. }
  130. bool EditorHistory::next() {
  131. cleanup_history();
  132. if ((current + 1) < history.size())
  133. current++;
  134. else
  135. return false;
  136. return true;
  137. }
  138. bool EditorHistory::previous() {
  139. cleanup_history();
  140. if (current > 0)
  141. current--;
  142. else
  143. return false;
  144. return true;
  145. }
  146. ObjectID EditorHistory::get_current() {
  147. if (current < 0 || current >= history.size())
  148. return 0;
  149. History &h = history[current];
  150. Object *obj = ObjectDB::get_instance(h.path[h.level].object);
  151. if (!obj)
  152. return 0;
  153. return obj->get_instance_id();
  154. }
  155. int EditorHistory::get_path_size() const {
  156. if (current < 0 || current >= history.size())
  157. return 0;
  158. const History &h = history[current];
  159. return h.path.size();
  160. }
  161. ObjectID EditorHistory::get_path_object(int p_index) const {
  162. if (current < 0 || current >= history.size())
  163. return 0;
  164. const History &h = history[current];
  165. ERR_FAIL_INDEX_V(p_index, h.path.size(), 0);
  166. Object *obj = ObjectDB::get_instance(h.path[p_index].object);
  167. if (!obj)
  168. return 0;
  169. return obj->get_instance_id();
  170. }
  171. String EditorHistory::get_path_property(int p_index) const {
  172. if (current < 0 || current >= history.size())
  173. return "";
  174. const History &h = history[current];
  175. ERR_FAIL_INDEX_V(p_index, h.path.size(), "");
  176. return h.path[p_index].property;
  177. }
  178. void EditorHistory::clear() {
  179. history.clear();
  180. current = -1;
  181. }
  182. EditorHistory::EditorHistory() {
  183. current = -1;
  184. }
  185. EditorPlugin *EditorData::get_editor(Object *p_object) {
  186. for (int i = 0; i < editor_plugins.size(); i++) {
  187. if (editor_plugins[i]->has_main_screen() && editor_plugins[i]->handles(p_object))
  188. return editor_plugins[i];
  189. }
  190. return NULL;
  191. }
  192. EditorPlugin *EditorData::get_subeditor(Object *p_object) {
  193. for (int i = 0; i < editor_plugins.size(); i++) {
  194. if (!editor_plugins[i]->has_main_screen() && editor_plugins[i]->handles(p_object))
  195. return editor_plugins[i];
  196. }
  197. return NULL;
  198. }
  199. Vector<EditorPlugin *> EditorData::get_subeditors(Object *p_object) {
  200. Vector<EditorPlugin *> sub_plugins;
  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. sub_plugins.push_back(editor_plugins[i]);
  204. }
  205. }
  206. return sub_plugins;
  207. }
  208. EditorPlugin *EditorData::get_editor(String p_name) {
  209. for (int i = 0; i < editor_plugins.size(); i++) {
  210. if (editor_plugins[i]->get_name() == p_name)
  211. return editor_plugins[i];
  212. }
  213. return NULL;
  214. }
  215. void EditorData::copy_object_params(Object *p_object) {
  216. clipboard.clear();
  217. List<PropertyInfo> pinfo;
  218. p_object->get_property_list(&pinfo);
  219. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  220. if (!(E->get().usage & PROPERTY_USAGE_EDITOR))
  221. continue;
  222. PropertyData pd;
  223. pd.name = E->get().name;
  224. pd.value = p_object->get(pd.name);
  225. clipboard.push_back(pd);
  226. }
  227. }
  228. void EditorData::get_editor_breakpoints(List<String> *p_breakpoints) {
  229. for (int i = 0; i < editor_plugins.size(); i++) {
  230. editor_plugins[i]->get_breakpoints(p_breakpoints);
  231. }
  232. }
  233. Dictionary EditorData::get_editor_states() const {
  234. Dictionary metadata;
  235. for (int i = 0; i < editor_plugins.size(); i++) {
  236. Dictionary state = editor_plugins[i]->get_state();
  237. if (state.empty())
  238. continue;
  239. metadata[editor_plugins[i]->get_name()] = state;
  240. }
  241. return metadata;
  242. }
  243. Dictionary EditorData::get_scene_editor_states(int p_idx) const {
  244. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), Dictionary());
  245. EditedScene es = edited_scene[p_idx];
  246. return es.editor_states;
  247. }
  248. void EditorData::set_editor_states(const Dictionary &p_states) {
  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. editor_plugins[idx]->set_state(p_states[name]);
  264. }
  265. }
  266. void EditorData::notify_edited_scene_changed() {
  267. for (int i = 0; i < editor_plugins.size(); i++) {
  268. editor_plugins[i]->edited_scene_changed();
  269. editor_plugins[i]->notify_scene_changed(get_edited_scene_root());
  270. }
  271. }
  272. void EditorData::clear_editor_states() {
  273. for (int i = 0; i < editor_plugins.size(); i++) {
  274. editor_plugins[i]->clear();
  275. }
  276. }
  277. void EditorData::save_editor_external_data() {
  278. for (int i = 0; i < editor_plugins.size(); i++) {
  279. editor_plugins[i]->save_external_data();
  280. }
  281. }
  282. void EditorData::apply_changes_in_editors() {
  283. for (int i = 0; i < editor_plugins.size(); i++) {
  284. editor_plugins[i]->apply_changes();
  285. }
  286. }
  287. void EditorData::save_editor_global_states() {
  288. for (int i = 0; i < editor_plugins.size(); i++) {
  289. editor_plugins[i]->save_global_state();
  290. }
  291. }
  292. void EditorData::restore_editor_global_states() {
  293. for (int i = 0; i < editor_plugins.size(); i++) {
  294. editor_plugins[i]->restore_global_state();
  295. }
  296. }
  297. void EditorData::paste_object_params(Object *p_object) {
  298. for (List<PropertyData>::Element *E = clipboard.front(); E; E = E->next()) {
  299. p_object->set(E->get().name, E->get().value);
  300. }
  301. }
  302. UndoRedo &EditorData::get_undo_redo() {
  303. return undo_redo;
  304. }
  305. void EditorData::remove_editor_plugin(EditorPlugin *p_plugin) {
  306. p_plugin->undo_redo = NULL;
  307. editor_plugins.erase(p_plugin);
  308. }
  309. void EditorData::add_editor_plugin(EditorPlugin *p_plugin) {
  310. p_plugin->undo_redo = &undo_redo;
  311. editor_plugins.push_back(p_plugin);
  312. }
  313. int EditorData::get_editor_plugin_count() const {
  314. return editor_plugins.size();
  315. }
  316. EditorPlugin *EditorData::get_editor_plugin(int p_idx) {
  317. ERR_FAIL_INDEX_V(p_idx, editor_plugins.size(), NULL);
  318. return editor_plugins[p_idx];
  319. }
  320. void EditorData::add_custom_type(const String &p_type, const String &p_inherits, const Ref<Script> &p_script, const Ref<Texture> &p_icon) {
  321. ERR_FAIL_COND(p_script.is_null());
  322. CustomType ct;
  323. ct.name = p_type;
  324. ct.icon = p_icon;
  325. ct.script = p_script;
  326. if (!custom_types.has(p_inherits)) {
  327. custom_types[p_inherits] = Vector<CustomType>();
  328. }
  329. custom_types[p_inherits].push_back(ct);
  330. }
  331. void EditorData::remove_custom_type(const String &p_type) {
  332. for (Map<String, Vector<CustomType> >::Element *E = custom_types.front(); E; E = E->next()) {
  333. for (int i = 0; i < E->get().size(); i++) {
  334. if (E->get()[i].name == p_type) {
  335. E->get().remove(i);
  336. if (E->get().empty()) {
  337. custom_types.erase(E->key());
  338. }
  339. return;
  340. }
  341. }
  342. }
  343. }
  344. int EditorData::add_edited_scene(int p_at_pos) {
  345. if (p_at_pos < 0)
  346. p_at_pos = edited_scene.size();
  347. EditedScene es;
  348. es.root = NULL;
  349. es.history_current = -1;
  350. es.version = 0;
  351. es.live_edit_root = NodePath(String("/root"));
  352. if (p_at_pos == edited_scene.size())
  353. edited_scene.push_back(es);
  354. else
  355. edited_scene.insert(p_at_pos, es);
  356. if (current_edited_scene < 0)
  357. current_edited_scene = 0;
  358. return p_at_pos;
  359. }
  360. void EditorData::move_edited_scene_index(int p_idx, int p_to_idx) {
  361. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  362. ERR_FAIL_INDEX(p_to_idx, edited_scene.size());
  363. SWAP(edited_scene[p_idx], edited_scene[p_to_idx]);
  364. }
  365. void EditorData::remove_scene(int p_idx) {
  366. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  367. if (edited_scene[p_idx].root) {
  368. for (int i = 0; i < editor_plugins.size(); i++) {
  369. editor_plugins[i]->notify_scene_closed(edited_scene[p_idx].root->get_filename());
  370. }
  371. memdelete(edited_scene[p_idx].root);
  372. }
  373. if (current_edited_scene > p_idx)
  374. current_edited_scene--;
  375. else if (current_edited_scene == p_idx && current_edited_scene > 0) {
  376. current_edited_scene--;
  377. }
  378. edited_scene.remove(p_idx);
  379. }
  380. bool EditorData::_find_updated_instances(Node *p_root, Node *p_node, Set<String> &checked_paths) {
  381. /*
  382. if (p_root!=p_node && p_node->get_owner()!=p_root && !p_root->is_editable_instance(p_node->get_owner()))
  383. return false;
  384. */
  385. Ref<SceneState> ss;
  386. if (p_node == p_root) {
  387. ss = p_node->get_scene_inherited_state();
  388. } else if (p_node->get_filename() != String()) {
  389. ss = p_node->get_scene_instance_state();
  390. }
  391. if (ss.is_valid()) {
  392. String path = ss->get_path();
  393. if (!checked_paths.has(path)) {
  394. uint64_t modified_time = FileAccess::get_modified_time(path);
  395. if (modified_time != ss->get_last_modified_time()) {
  396. return true; //external scene changed
  397. }
  398. checked_paths.insert(path);
  399. }
  400. }
  401. for (int i = 0; i < p_node->get_child_count(); i++) {
  402. bool found = _find_updated_instances(p_root, p_node->get_child(i), checked_paths);
  403. if (found)
  404. return true;
  405. }
  406. return false;
  407. }
  408. bool EditorData::check_and_update_scene(int p_idx) {
  409. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), false);
  410. if (!edited_scene[p_idx].root)
  411. return false;
  412. Set<String> checked_scenes;
  413. bool must_reload = _find_updated_instances(edited_scene[p_idx].root, edited_scene[p_idx].root, checked_scenes);
  414. print_line("MUST RELOAD? " + itos(must_reload));
  415. if (must_reload) {
  416. Ref<PackedScene> pscene;
  417. pscene.instance();
  418. EditorProgress ep("update_scene", TTR("Updating Scene"), 2);
  419. ep.step(TTR("Storing local changes.."), 0);
  420. //pack first, so it stores diffs to previous version of saved scene
  421. Error err = pscene->pack(edited_scene[p_idx].root);
  422. ERR_FAIL_COND_V(err != OK, false);
  423. ep.step(TTR("Updating scene.."), 1);
  424. Node *new_scene = pscene->instance(PackedScene::GEN_EDIT_STATE_MAIN);
  425. ERR_FAIL_COND_V(!new_scene, false);
  426. //transfer selection
  427. List<Node *> new_selection;
  428. for (List<Node *>::Element *E = edited_scene[p_idx].selection.front(); E; E = E->next()) {
  429. NodePath p = edited_scene[p_idx].root->get_path_to(E->get());
  430. Node *new_node = new_scene->get_node(p);
  431. if (new_node)
  432. new_selection.push_back(new_node);
  433. }
  434. new_scene->set_filename(edited_scene[p_idx].root->get_filename());
  435. memdelete(edited_scene[p_idx].root);
  436. edited_scene[p_idx].root = new_scene;
  437. edited_scene[p_idx].selection = new_selection;
  438. return true;
  439. }
  440. return false;
  441. }
  442. int EditorData::get_edited_scene() const {
  443. return current_edited_scene;
  444. }
  445. void EditorData::set_edited_scene(int p_idx) {
  446. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  447. current_edited_scene = p_idx;
  448. //swap
  449. }
  450. Node *EditorData::get_edited_scene_root(int p_idx) {
  451. if (p_idx < 0) {
  452. ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), NULL);
  453. return edited_scene[current_edited_scene].root;
  454. } else {
  455. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), NULL);
  456. return edited_scene[p_idx].root;
  457. }
  458. }
  459. void EditorData::set_edited_scene_root(Node *p_root) {
  460. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  461. edited_scene[current_edited_scene].root = p_root;
  462. }
  463. int EditorData::get_edited_scene_count() const {
  464. return edited_scene.size();
  465. }
  466. Vector<EditorData::EditedScene> EditorData::get_edited_scenes() const {
  467. Vector<EditedScene> out_edited_scenes_list = Vector<EditedScene>();
  468. for (int i = 0; i < edited_scene.size(); i++) {
  469. out_edited_scenes_list.push_back(edited_scene[i]);
  470. }
  471. return out_edited_scenes_list;
  472. }
  473. void EditorData::set_edited_scene_version(uint64_t version, int p_scene_idx) {
  474. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  475. if (p_scene_idx < 0) {
  476. edited_scene[current_edited_scene].version = version;
  477. } else {
  478. ERR_FAIL_INDEX(p_scene_idx, edited_scene.size());
  479. edited_scene[p_scene_idx].version = version;
  480. }
  481. }
  482. uint64_t EditorData::get_edited_scene_version() const {
  483. ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), 0);
  484. return edited_scene[current_edited_scene].version;
  485. }
  486. uint64_t EditorData::get_scene_version(int p_idx) const {
  487. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), false);
  488. return edited_scene[p_idx].version;
  489. }
  490. String EditorData::get_scene_type(int p_idx) const {
  491. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String());
  492. if (!edited_scene[p_idx].root)
  493. return "";
  494. return edited_scene[p_idx].root->get_class();
  495. }
  496. void EditorData::move_edited_scene_to_index(int p_idx) {
  497. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  498. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  499. EditedScene es = edited_scene[current_edited_scene];
  500. edited_scene.remove(current_edited_scene);
  501. edited_scene.insert(p_idx, es);
  502. current_edited_scene = p_idx;
  503. }
  504. Ref<Script> EditorData::get_scene_root_script(int p_idx) const {
  505. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), Ref<Script>());
  506. if (!edited_scene[p_idx].root)
  507. return Ref<Script>();
  508. Ref<Script> s = edited_scene[p_idx].root->get_script();
  509. if (!s.is_valid() && edited_scene[p_idx].root->get_child_count()) {
  510. Node *n = edited_scene[p_idx].root->get_child(0);
  511. while (!s.is_valid() && n && n->get_filename() == String()) {
  512. s = n->get_script();
  513. n = n->get_parent();
  514. }
  515. }
  516. return s;
  517. }
  518. String EditorData::get_scene_title(int p_idx) const {
  519. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String());
  520. if (!edited_scene[p_idx].root)
  521. return TTR("[empty]");
  522. if (edited_scene[p_idx].root->get_filename() == "")
  523. return TTR("[unsaved]");
  524. bool show_ext = EDITOR_DEF("interface/scene_tabs/show_extension", false);
  525. String name = edited_scene[p_idx].root->get_filename().get_file();
  526. if (!show_ext) {
  527. name = name.get_basename();
  528. }
  529. return name;
  530. }
  531. void EditorData::set_scene_path(int p_idx, const String &p_path) {
  532. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  533. if (!edited_scene[p_idx].root)
  534. return;
  535. edited_scene[p_idx].root->set_filename(p_path);
  536. }
  537. String EditorData::get_scene_path(int p_idx) const {
  538. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String());
  539. if (!edited_scene[p_idx].root)
  540. return "";
  541. return edited_scene[p_idx].root->get_filename();
  542. }
  543. void EditorData::set_edited_scene_live_edit_root(const NodePath &p_root) {
  544. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  545. edited_scene[current_edited_scene].live_edit_root = p_root;
  546. }
  547. NodePath EditorData::get_edited_scene_live_edit_root() {
  548. ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), String());
  549. return edited_scene[current_edited_scene].live_edit_root;
  550. }
  551. void EditorData::save_edited_scene_state(EditorSelection *p_selection, EditorHistory *p_history, const Dictionary &p_custom) {
  552. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  553. EditedScene &es = edited_scene[current_edited_scene];
  554. es.selection = p_selection->get_selected_node_list();
  555. es.history_current = p_history->current;
  556. es.history_stored = p_history->history;
  557. es.editor_states = get_editor_states();
  558. es.custom_state = p_custom;
  559. }
  560. Dictionary EditorData::restore_edited_scene_state(EditorSelection *p_selection, EditorHistory *p_history) {
  561. ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), Dictionary());
  562. EditedScene &es = edited_scene[current_edited_scene];
  563. p_history->current = es.history_current;
  564. p_history->history = es.history_stored;
  565. p_selection->clear();
  566. for (List<Node *>::Element *E = es.selection.front(); E; E = E->next()) {
  567. p_selection->add_node(E->get());
  568. }
  569. set_editor_states(es.editor_states);
  570. return es.custom_state;
  571. }
  572. void EditorData::clear_edited_scenes() {
  573. for (int i = 0; i < edited_scene.size(); i++) {
  574. if (edited_scene[i].root) {
  575. memdelete(edited_scene[i].root);
  576. }
  577. }
  578. edited_scene.clear();
  579. }
  580. void EditorData::set_plugin_window_layout(Ref<ConfigFile> p_layout) {
  581. for (int i = 0; i < editor_plugins.size(); i++) {
  582. editor_plugins[i]->set_window_layout(p_layout);
  583. }
  584. }
  585. void EditorData::get_plugin_window_layout(Ref<ConfigFile> p_layout) {
  586. for (int i = 0; i < editor_plugins.size(); i++) {
  587. editor_plugins[i]->get_window_layout(p_layout);
  588. }
  589. }
  590. EditorData::EditorData() {
  591. current_edited_scene = -1;
  592. //load_imported_scenes_from_globals();
  593. }
  594. ///////////
  595. void EditorSelection::_node_removed(Node *p_node) {
  596. if (!selection.has(p_node))
  597. return;
  598. Object *meta = selection[p_node];
  599. if (meta)
  600. memdelete(meta);
  601. selection.erase(p_node);
  602. changed = true;
  603. nl_changed = true;
  604. }
  605. void EditorSelection::add_node(Node *p_node) {
  606. ERR_FAIL_NULL(p_node);
  607. ERR_FAIL_COND(!p_node->is_inside_tree());
  608. if (selection.has(p_node))
  609. return;
  610. changed = true;
  611. nl_changed = true;
  612. Object *meta = NULL;
  613. for (List<Object *>::Element *E = editor_plugins.front(); E; E = E->next()) {
  614. meta = E->get()->call("_get_editor_data", p_node);
  615. if (meta) {
  616. break;
  617. }
  618. }
  619. selection[p_node] = meta;
  620. p_node->connect("tree_exiting", this, "_node_removed", varray(p_node), CONNECT_ONESHOT);
  621. //emit_signal("selection_changed");
  622. }
  623. void EditorSelection::remove_node(Node *p_node) {
  624. ERR_FAIL_NULL(p_node);
  625. if (!selection.has(p_node))
  626. return;
  627. changed = true;
  628. nl_changed = true;
  629. Object *meta = selection[p_node];
  630. if (meta)
  631. memdelete(meta);
  632. selection.erase(p_node);
  633. p_node->disconnect("tree_exiting", this, "_node_removed");
  634. //emit_signal("selection_changed");
  635. }
  636. bool EditorSelection::is_selected(Node *p_node) const {
  637. return selection.has(p_node);
  638. }
  639. Array EditorSelection::_get_transformable_selected_nodes() {
  640. Array ret;
  641. for (List<Node *>::Element *E = selected_node_list.front(); E; E = E->next()) {
  642. ret.push_back(E->get());
  643. }
  644. return ret;
  645. }
  646. Array EditorSelection::_get_selected_nodes() {
  647. Array ret;
  648. for (Map<Node *, Object *>::Element *E = selection.front(); E; E = E->next()) {
  649. ret.push_back(E->key());
  650. }
  651. return ret;
  652. }
  653. void EditorSelection::_bind_methods() {
  654. ClassDB::bind_method(D_METHOD("_node_removed"), &EditorSelection::_node_removed);
  655. ClassDB::bind_method(D_METHOD("clear"), &EditorSelection::clear);
  656. ClassDB::bind_method(D_METHOD("add_node", "node"), &EditorSelection::add_node);
  657. ClassDB::bind_method(D_METHOD("remove_node", "node"), &EditorSelection::remove_node);
  658. ClassDB::bind_method(D_METHOD("get_selected_nodes"), &EditorSelection::_get_selected_nodes);
  659. ClassDB::bind_method(D_METHOD("get_transformable_selected_nodes"), &EditorSelection::_get_transformable_selected_nodes);
  660. ClassDB::bind_method(D_METHOD("_emit_change"), &EditorSelection::_emit_change);
  661. ADD_SIGNAL(MethodInfo("selection_changed"));
  662. }
  663. void EditorSelection::add_editor_plugin(Object *p_object) {
  664. editor_plugins.push_back(p_object);
  665. }
  666. void EditorSelection::_update_nl() {
  667. if (!nl_changed)
  668. return;
  669. selected_node_list.clear();
  670. for (Map<Node *, Object *>::Element *E = selection.front(); E; E = E->next()) {
  671. Node *parent = E->key();
  672. parent = parent->get_parent();
  673. bool skip = false;
  674. while (parent) {
  675. if (selection.has(parent)) {
  676. skip = true;
  677. break;
  678. }
  679. parent = parent->get_parent();
  680. }
  681. if (skip)
  682. continue;
  683. selected_node_list.push_back(E->key());
  684. }
  685. nl_changed = true;
  686. }
  687. void EditorSelection::update() {
  688. _update_nl();
  689. if (!changed)
  690. return;
  691. changed = false;
  692. if (!emitted) {
  693. emitted = true;
  694. call_deferred("_emit_change");
  695. }
  696. }
  697. void EditorSelection::_emit_change() {
  698. emit_signal("selection_changed");
  699. emitted = false;
  700. }
  701. List<Node *> &EditorSelection::get_selected_node_list() {
  702. if (changed)
  703. update();
  704. else
  705. _update_nl();
  706. return selected_node_list;
  707. }
  708. void EditorSelection::clear() {
  709. while (!selection.empty()) {
  710. remove_node(selection.front()->key());
  711. }
  712. changed = true;
  713. nl_changed = true;
  714. }
  715. EditorSelection::EditorSelection() {
  716. emitted = false;
  717. changed = false;
  718. nl_changed = false;
  719. }
  720. EditorSelection::~EditorSelection() {
  721. clear();
  722. }