editor_data.cpp 39 KB

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