editor_data.cpp 31 KB

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