editor_data.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  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 "editor_node.h"
  32. #include "editor_settings.h"
  33. #include "globals.h"
  34. #include "io/resource_loader.h"
  35. #include "os/dir_access.h"
  36. #include "os/file_access.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. if (ObjectDB::get_instance(history[i].path[j].object))
  45. continue; //has isntance, try next
  46. if (j <= history[i].level) {
  47. //before or equal level, complete fail
  48. fail = true;
  49. } else {
  50. //after level, clip
  51. history[i].path.resize(j);
  52. }
  53. break;
  54. }
  55. if (fail) {
  56. history.remove(i);
  57. i--;
  58. }
  59. }
  60. if (current >= history.size())
  61. current = history.size() - 1;
  62. }
  63. void EditorHistory::_add_object(ObjectID p_object, const String &p_property, int p_level_change) {
  64. Object *obj = ObjectDB::get_instance(p_object);
  65. ERR_FAIL_COND(!obj);
  66. Reference *r = obj->cast_to<Reference>();
  67. Obj o;
  68. if (r)
  69. o.ref = REF(r);
  70. o.object = p_object;
  71. o.property = p_property;
  72. History h;
  73. bool has_prev = current >= 0 && current < history.size();
  74. if (has_prev) {
  75. if (obj->is_type("ScriptEditorDebuggerInspectedObject")) {
  76. for (int i = current; i >= 0; i--) {
  77. Object *pre_obj = ObjectDB::get_instance(get_history_obj(i));
  78. if (pre_obj != NULL && pre_obj->is_type("ScriptEditorDebuggerInspectedObject")) {
  79. if (pre_obj->call("get_remote_object_id") == obj->call("get_remote_object_id")) {
  80. History &pr = history[i];
  81. pr.path[pr.path.size() - 1] = o;
  82. current = i;
  83. return;
  84. }
  85. }
  86. }
  87. }
  88. history.resize(current + 1); //clip history to next
  89. }
  90. if (p_property != "" && has_prev) {
  91. //add a sub property
  92. History &pr = history[current];
  93. h = pr;
  94. h.path.resize(h.level + 1);
  95. h.path.push_back(o);
  96. h.level++;
  97. } else if (p_level_change != -1 && has_prev) {
  98. //add a sub property
  99. History &pr = history[current];
  100. h = pr;
  101. ERR_FAIL_INDEX(p_level_change, h.path.size());
  102. h.level = p_level_change;
  103. } else {
  104. //add a new node
  105. h.path.push_back(o);
  106. h.level = 0;
  107. }
  108. history.push_back(h);
  109. current++;
  110. }
  111. void EditorHistory::add_object(ObjectID p_object) {
  112. _add_object(p_object, "", -1);
  113. }
  114. void EditorHistory::add_object(ObjectID p_object, const String &p_subprop) {
  115. _add_object(p_object, p_subprop, -1);
  116. }
  117. void EditorHistory::add_object(ObjectID p_object, int p_relevel) {
  118. _add_object(p_object, "", p_relevel);
  119. }
  120. int EditorHistory::get_history_len() {
  121. return history.size();
  122. }
  123. int EditorHistory::get_history_pos() {
  124. return current;
  125. }
  126. ObjectID EditorHistory::get_history_obj(int p_obj) const {
  127. ERR_FAIL_INDEX_V(p_obj, history.size(), 0);
  128. ERR_FAIL_INDEX_V(history[p_obj].level, history[p_obj].path.size(), 0);
  129. return history[p_obj].path[history[p_obj].level].object;
  130. }
  131. bool EditorHistory::is_at_begining() const {
  132. return current <= 0;
  133. }
  134. bool EditorHistory::is_at_end() const {
  135. return ((current + 1) >= history.size());
  136. }
  137. bool EditorHistory::next() {
  138. _cleanup_history();
  139. if ((current + 1) < history.size())
  140. current++;
  141. else
  142. return false;
  143. return true;
  144. }
  145. bool EditorHistory::previous() {
  146. _cleanup_history();
  147. if (current > 0)
  148. current--;
  149. else
  150. return false;
  151. return true;
  152. }
  153. ObjectID EditorHistory::get_current() {
  154. if (current < 0 || current >= history.size())
  155. return 0;
  156. History &h = history[current];
  157. Object *obj = ObjectDB::get_instance(h.path[h.level].object);
  158. if (!obj)
  159. return 0;
  160. return obj->get_instance_ID();
  161. }
  162. int EditorHistory::get_path_size() const {
  163. if (current < 0 || current >= history.size())
  164. return 0;
  165. const History &h = history[current];
  166. return h.path.size();
  167. }
  168. ObjectID EditorHistory::get_path_object(int p_index) const {
  169. if (current < 0 || current >= history.size())
  170. return 0;
  171. const History &h = history[current];
  172. ERR_FAIL_INDEX_V(p_index, h.path.size(), 0);
  173. Object *obj = ObjectDB::get_instance(h.path[p_index].object);
  174. if (!obj)
  175. return 0;
  176. return obj->get_instance_ID();
  177. }
  178. String EditorHistory::get_path_property(int p_index) const {
  179. if (current < 0 || current >= history.size())
  180. return "";
  181. const History &h = history[current];
  182. ERR_FAIL_INDEX_V(p_index, h.path.size(), "");
  183. return h.path[p_index].property;
  184. }
  185. void EditorHistory::clear() {
  186. history.clear();
  187. current = -1;
  188. }
  189. EditorHistory::EditorHistory() {
  190. current = -1;
  191. }
  192. EditorPlugin *EditorData::get_editor(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. EditorPlugin *EditorData::get_subeditor(Object *p_object) {
  200. for (int i = 0; i < editor_plugins.size(); i++) {
  201. if (!editor_plugins[i]->has_main_screen() && editor_plugins[i]->handles(p_object))
  202. return editor_plugins[i];
  203. }
  204. return NULL;
  205. }
  206. Vector<EditorPlugin *> EditorData::get_subeditors(Object *p_object) {
  207. Vector<EditorPlugin *> sub_plugins;
  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. sub_plugins.push_back(editor_plugins[i]);
  211. }
  212. }
  213. return sub_plugins;
  214. }
  215. EditorPlugin *EditorData::get_editor(String p_name) {
  216. for (int i = 0; i < editor_plugins.size(); i++) {
  217. if (editor_plugins[i]->get_name() == p_name)
  218. return editor_plugins[i];
  219. }
  220. return NULL;
  221. }
  222. void EditorData::copy_object_params(Object *p_object) {
  223. clipboard.clear();
  224. List<PropertyInfo> pinfo;
  225. p_object->get_property_list(&pinfo);
  226. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  227. if (!(E->get().usage & PROPERTY_USAGE_EDITOR))
  228. continue;
  229. PropertyData pd;
  230. pd.name = E->get().name;
  231. pd.value = p_object->get(pd.name);
  232. clipboard.push_back(pd);
  233. }
  234. }
  235. void EditorData::get_editor_breakpoints(List<String> *p_breakpoints) {
  236. for (int i = 0; i < editor_plugins.size(); i++) {
  237. editor_plugins[i]->get_breakpoints(p_breakpoints);
  238. }
  239. }
  240. Dictionary EditorData::get_editor_states() const {
  241. Dictionary metadata;
  242. for (int i = 0; i < editor_plugins.size(); i++) {
  243. Dictionary state = editor_plugins[i]->get_state();
  244. if (state.empty())
  245. continue;
  246. metadata[editor_plugins[i]->get_name()] = state;
  247. }
  248. return metadata;
  249. }
  250. Dictionary EditorData::get_scene_editor_states(int p_idx) const {
  251. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), Dictionary());
  252. EditedScene es = edited_scene[p_idx];
  253. return es.editor_states;
  254. }
  255. void EditorData::set_editor_states(const Dictionary &p_states) {
  256. List<Variant> keys;
  257. p_states.get_key_list(&keys);
  258. List<Variant>::Element *E = keys.front();
  259. for (; E; E = E->next()) {
  260. String name = E->get();
  261. int idx = -1;
  262. for (int i = 0; i < editor_plugins.size(); i++) {
  263. if (editor_plugins[i]->get_name() == name) {
  264. idx = i;
  265. break;
  266. }
  267. }
  268. if (idx == -1)
  269. continue;
  270. editor_plugins[idx]->set_state(p_states[name]);
  271. }
  272. }
  273. void EditorData::notify_edited_scene_changed() {
  274. for (int i = 0; i < editor_plugins.size(); i++) {
  275. editor_plugins[i]->edited_scene_changed();
  276. }
  277. }
  278. void EditorData::clear_editor_states() {
  279. for (int i = 0; i < editor_plugins.size(); i++) {
  280. editor_plugins[i]->clear();
  281. }
  282. }
  283. void EditorData::save_editor_external_data() {
  284. for (int i = 0; i < editor_plugins.size(); i++) {
  285. editor_plugins[i]->save_external_data();
  286. }
  287. }
  288. void EditorData::apply_changes_in_editors() {
  289. for (int i = 0; i < editor_plugins.size(); i++) {
  290. editor_plugins[i]->apply_changes();
  291. }
  292. }
  293. void EditorData::save_editor_global_states() {
  294. for (int i = 0; i < editor_plugins.size(); i++) {
  295. editor_plugins[i]->save_global_state();
  296. }
  297. }
  298. void EditorData::restore_editor_global_states() {
  299. for (int i = 0; i < editor_plugins.size(); i++) {
  300. editor_plugins[i]->restore_global_state();
  301. }
  302. }
  303. void EditorData::paste_object_params(Object *p_object) {
  304. for (List<PropertyData>::Element *E = clipboard.front(); E; E = E->next()) {
  305. p_object->set(E->get().name, E->get().value);
  306. }
  307. }
  308. UndoRedo &EditorData::get_undo_redo() {
  309. return undo_redo;
  310. }
  311. void EditorData::remove_editor_plugin(EditorPlugin *p_plugin) {
  312. p_plugin->undo_redo = NULL;
  313. editor_plugins.erase(p_plugin);
  314. }
  315. void EditorData::add_editor_plugin(EditorPlugin *p_plugin) {
  316. p_plugin->undo_redo = &undo_redo;
  317. editor_plugins.push_back(p_plugin);
  318. }
  319. int EditorData::get_editor_plugin_count() const {
  320. return editor_plugins.size();
  321. }
  322. EditorPlugin *EditorData::get_editor_plugin(int p_idx) {
  323. ERR_FAIL_INDEX_V(p_idx, editor_plugins.size(), NULL);
  324. return editor_plugins[p_idx];
  325. }
  326. void EditorData::add_custom_type(const String &p_type, const String &p_inherits, const Ref<Script> &p_script, const Ref<Texture> &p_icon) {
  327. ERR_FAIL_COND(p_script.is_null());
  328. CustomType ct;
  329. ct.name = p_type;
  330. ct.icon = p_icon;
  331. ct.script = p_script;
  332. if (!custom_types.has(p_inherits)) {
  333. custom_types[p_inherits] = Vector<CustomType>();
  334. }
  335. custom_types[p_inherits].push_back(ct);
  336. }
  337. void EditorData::remove_custom_type(const String &p_type) {
  338. for (Map<String, Vector<CustomType> >::Element *E = custom_types.front(); E; E = E->next()) {
  339. for (int i = 0; i < E->get().size(); i++) {
  340. if (E->get()[i].name == p_type) {
  341. E->get().remove(i);
  342. if (E->get().empty()) {
  343. custom_types.erase(E->key());
  344. }
  345. return;
  346. }
  347. }
  348. }
  349. }
  350. int EditorData::add_edited_scene(int p_at_pos) {
  351. if (p_at_pos < 0)
  352. p_at_pos = edited_scene.size();
  353. EditedScene es;
  354. es.root = NULL;
  355. es.history_current = -1;
  356. es.version = 0;
  357. es.live_edit_root = NodePath(String("/root"));
  358. if (p_at_pos == edited_scene.size())
  359. edited_scene.push_back(es);
  360. else
  361. edited_scene.insert(p_at_pos, es);
  362. if (current_edited_scene < 0)
  363. current_edited_scene = 0;
  364. return p_at_pos;
  365. }
  366. void EditorData::move_edited_scene_index(int p_idx, int p_to_idx) {
  367. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  368. ERR_FAIL_INDEX(p_to_idx, edited_scene.size());
  369. SWAP(edited_scene[p_idx], edited_scene[p_to_idx]);
  370. }
  371. void EditorData::remove_scene(int p_idx) {
  372. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  373. if (edited_scene[p_idx].root)
  374. memdelete(edited_scene[p_idx].root);
  375. if (current_edited_scene > p_idx)
  376. current_edited_scene--;
  377. else if (current_edited_scene == p_idx && current_edited_scene > 0) {
  378. current_edited_scene--;
  379. }
  380. edited_scene.remove(p_idx);
  381. }
  382. bool EditorData::_find_updated_instances(Node *p_root, Node *p_node, Set<String> &checked_paths) {
  383. // if (p_root!=p_node && p_node->get_owner()!=p_root && !p_root->is_editable_instance(p_node->get_owner()))
  384. // return false;
  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. if (must_reload) {
  415. Ref<PackedScene> pscene;
  416. pscene.instance();
  417. EditorProgress ep("update_scene", TTR("Updating Scene"), 2);
  418. ep.step(TTR("Storing local changes.."), 0);
  419. //pack first, so it stores diffs to previous version of saved scene
  420. Error err = pscene->pack(edited_scene[p_idx].root);
  421. ERR_FAIL_COND_V(err != OK, false);
  422. ep.step(TTR("Updating scene.."), 1);
  423. Node *new_scene = pscene->instance(true);
  424. ERR_FAIL_COND_V(!new_scene, false);
  425. //transfer selection
  426. List<Node *> new_selection;
  427. for (List<Node *>::Element *E = edited_scene[p_idx].selection.front(); E; E = E->next()) {
  428. NodePath p = edited_scene[p_idx].root->get_path_to(E->get());
  429. Node *new_node = new_scene->get_node(p);
  430. if (new_node)
  431. new_selection.push_back(new_node);
  432. }
  433. new_scene->set_filename(edited_scene[p_idx].root->get_filename());
  434. memdelete(edited_scene[p_idx].root);
  435. edited_scene[p_idx].root = new_scene;
  436. edited_scene[p_idx].selection = new_selection;
  437. return true;
  438. }
  439. return false;
  440. }
  441. int EditorData::get_edited_scene() const {
  442. return current_edited_scene;
  443. }
  444. void EditorData::set_edited_scene(int p_idx) {
  445. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  446. current_edited_scene = p_idx;
  447. //swap
  448. }
  449. Node *EditorData::get_edited_scene_root(int p_idx) {
  450. if (p_idx < 0) {
  451. ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), NULL);
  452. return edited_scene[current_edited_scene].root;
  453. } else {
  454. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), NULL);
  455. return edited_scene[p_idx].root;
  456. }
  457. }
  458. void EditorData::set_edited_scene_root(Node *p_root) {
  459. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  460. edited_scene[current_edited_scene].root = p_root;
  461. }
  462. int EditorData::get_edited_scene_count() const {
  463. return edited_scene.size();
  464. }
  465. void EditorData::set_edited_scene_version(uint64_t version, int scene_idx) {
  466. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  467. if (scene_idx < 0) {
  468. edited_scene[current_edited_scene].version = version;
  469. } else {
  470. ERR_FAIL_INDEX(scene_idx, edited_scene.size());
  471. edited_scene[scene_idx].version = version;
  472. }
  473. }
  474. uint64_t EditorData::get_edited_scene_version() const {
  475. ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), 0);
  476. return edited_scene[current_edited_scene].version;
  477. }
  478. uint64_t EditorData::get_scene_version(int p_idx) const {
  479. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), false);
  480. return edited_scene[p_idx].version;
  481. }
  482. String EditorData::get_scene_type(int p_idx) const {
  483. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String());
  484. if (!edited_scene[p_idx].root)
  485. return "";
  486. return edited_scene[p_idx].root->get_type();
  487. }
  488. void EditorData::move_edited_scene_to_index(int p_idx) {
  489. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  490. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  491. EditedScene es = edited_scene[current_edited_scene];
  492. edited_scene.remove(current_edited_scene);
  493. edited_scene.insert(p_idx, es);
  494. current_edited_scene = p_idx;
  495. }
  496. Ref<Script> EditorData::get_scene_root_script(int p_idx) const {
  497. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), Ref<Script>());
  498. if (!edited_scene[p_idx].root)
  499. return Ref<Script>();
  500. Ref<Script> s = edited_scene[p_idx].root->get_script();
  501. if (!s.is_valid() && edited_scene[p_idx].root->get_child_count()) {
  502. Node *n = edited_scene[p_idx].root->get_child(0);
  503. while (!s.is_valid() && n && n->get_filename() == String()) {
  504. s = n->get_script();
  505. n = n->get_parent();
  506. }
  507. }
  508. return s;
  509. }
  510. String EditorData::get_scene_title(int p_idx) const {
  511. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String());
  512. if (!edited_scene[p_idx].root)
  513. return "[empty]";
  514. if (edited_scene[p_idx].root->get_filename() == "")
  515. return "[unsaved]";
  516. return edited_scene[p_idx].root->get_filename().get_file();
  517. }
  518. String EditorData::get_scene_path(int p_idx) const {
  519. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String());
  520. if (!edited_scene[p_idx].root)
  521. return "";
  522. return edited_scene[p_idx].root->get_filename();
  523. }
  524. void EditorData::set_edited_scene_live_edit_root(const NodePath &p_root) {
  525. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  526. edited_scene[current_edited_scene].live_edit_root = p_root;
  527. }
  528. NodePath EditorData::get_edited_scene_live_edit_root() {
  529. ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), String());
  530. return edited_scene[current_edited_scene].live_edit_root;
  531. }
  532. void EditorData::save_edited_scene_state(EditorSelection *p_selection, EditorHistory *p_history, const Dictionary &p_custom) {
  533. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  534. EditedScene &es = edited_scene[current_edited_scene];
  535. es.selection = p_selection->get_selected_node_list();
  536. es.history_current = p_history->current;
  537. es.history_stored = p_history->history;
  538. es.editor_states = get_editor_states();
  539. es.custom_state = p_custom;
  540. }
  541. Dictionary EditorData::restore_edited_scene_state(EditorSelection *p_selection, EditorHistory *p_history) {
  542. ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), Dictionary());
  543. EditedScene &es = edited_scene[current_edited_scene];
  544. p_history->current = es.history_current;
  545. p_history->history = es.history_stored;
  546. p_selection->clear();
  547. for (List<Node *>::Element *E = es.selection.front(); E; E = E->next()) {
  548. p_selection->add_node(E->get());
  549. }
  550. set_editor_states(es.editor_states);
  551. return es.custom_state;
  552. }
  553. void EditorData::set_edited_scene_import_metadata(Ref<ResourceImportMetadata> p_mdata) {
  554. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  555. edited_scene[current_edited_scene].medatata = p_mdata;
  556. }
  557. Ref<ResourceImportMetadata> EditorData::get_edited_scene_import_metadata(int idx) const {
  558. ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), Ref<ResourceImportMetadata>());
  559. if (idx < 0) {
  560. return edited_scene[current_edited_scene].medatata;
  561. } else {
  562. ERR_FAIL_INDEX_V(idx, edited_scene.size(), Ref<ResourceImportMetadata>());
  563. return edited_scene[idx].medatata;
  564. }
  565. }
  566. void EditorData::clear_edited_scenes() {
  567. for (int i = 0; i < edited_scene.size(); i++) {
  568. if (edited_scene[i].root) {
  569. memdelete(edited_scene[i].root);
  570. }
  571. }
  572. edited_scene.clear();
  573. }
  574. void EditorData::set_plugin_window_layout(Ref<ConfigFile> p_layout) {
  575. for (int i = 0; i < editor_plugins.size(); i++) {
  576. editor_plugins[i]->set_window_layout(p_layout);
  577. }
  578. }
  579. void EditorData::get_plugin_window_layout(Ref<ConfigFile> p_layout) {
  580. for (int i = 0; i < editor_plugins.size(); i++) {
  581. editor_plugins[i]->get_window_layout(p_layout);
  582. }
  583. }
  584. EditorData::EditorData() {
  585. current_edited_scene = -1;
  586. // load_imported_scenes_from_globals();
  587. }
  588. ///////////
  589. void EditorSelection::_node_removed(Node *p_node) {
  590. if (!selection.has(p_node))
  591. return;
  592. Object *meta = selection[p_node];
  593. if (meta)
  594. memdelete(meta);
  595. selection.erase(p_node);
  596. changed = true;
  597. nl_changed = true;
  598. }
  599. void EditorSelection::add_node(Node *p_node) {
  600. ERR_FAIL_NULL(p_node);
  601. ERR_FAIL_COND(!p_node->is_inside_tree());
  602. if (selection.has(p_node))
  603. return;
  604. changed = true;
  605. nl_changed = true;
  606. Object *meta = NULL;
  607. for (List<Object *>::Element *E = editor_plugins.front(); E; E = E->next()) {
  608. meta = E->get()->call("_get_editor_data", p_node);
  609. if (meta) {
  610. break;
  611. }
  612. }
  613. selection[p_node] = meta;
  614. p_node->connect("exit_tree", this, "_node_removed", varray(p_node), CONNECT_ONESHOT);
  615. //emit_signal("selection_changed");
  616. }
  617. void EditorSelection::remove_node(Node *p_node) {
  618. ERR_FAIL_NULL(p_node);
  619. if (!selection.has(p_node))
  620. return;
  621. changed = true;
  622. nl_changed = true;
  623. Object *meta = selection[p_node];
  624. if (meta)
  625. memdelete(meta);
  626. selection.erase(p_node);
  627. p_node->disconnect("exit_tree", this, "_node_removed");
  628. //emit_signal("selection_changed");
  629. }
  630. bool EditorSelection::is_selected(Node *p_node) const {
  631. return selection.has(p_node);
  632. }
  633. Array EditorSelection::_get_selected_nodes() {
  634. Array ret;
  635. for (List<Node *>::Element *E = selected_node_list.front(); E; E = E->next()) {
  636. ret.push_back(E->get());
  637. }
  638. return ret;
  639. }
  640. void EditorSelection::_bind_methods() {
  641. ObjectTypeDB::bind_method(_MD("_node_removed"), &EditorSelection::_node_removed);
  642. ObjectTypeDB::bind_method(_MD("clear"), &EditorSelection::clear);
  643. ObjectTypeDB::bind_method(_MD("add_node", "node:Node"), &EditorSelection::add_node);
  644. ObjectTypeDB::bind_method(_MD("remove_node", "node:Node"), &EditorSelection::remove_node);
  645. ObjectTypeDB::bind_method(_MD("get_selected_nodes"), &EditorSelection::_get_selected_nodes);
  646. ADD_SIGNAL(MethodInfo("selection_changed"));
  647. }
  648. void EditorSelection::add_editor_plugin(Object *p_object) {
  649. editor_plugins.push_back(p_object);
  650. }
  651. void EditorSelection::_update_nl() {
  652. if (!nl_changed)
  653. return;
  654. selected_node_list.clear();
  655. for (Map<Node *, Object *>::Element *E = selection.front(); E; E = E->next()) {
  656. Node *parent = E->key();
  657. parent = parent->get_parent();
  658. bool skip = false;
  659. while (parent) {
  660. if (selection.has(parent)) {
  661. skip = true;
  662. break;
  663. }
  664. parent = parent->get_parent();
  665. }
  666. if (skip)
  667. continue;
  668. selected_node_list.push_back(E->key());
  669. }
  670. nl_changed = true;
  671. }
  672. void EditorSelection::update() {
  673. _update_nl();
  674. if (!changed)
  675. return;
  676. emit_signal("selection_changed");
  677. changed = false;
  678. }
  679. List<Node *> &EditorSelection::get_selected_node_list() {
  680. if (changed)
  681. update();
  682. else
  683. _update_nl();
  684. return selected_node_list;
  685. }
  686. void EditorSelection::clear() {
  687. while (!selection.empty()) {
  688. remove_node(selection.front()->key());
  689. }
  690. changed = true;
  691. nl_changed = true;
  692. }
  693. EditorSelection::EditorSelection() {
  694. changed = false;
  695. nl_changed = false;
  696. }
  697. EditorSelection::~EditorSelection() {
  698. clear();
  699. }