editor_data.cpp 33 KB

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