editor_data.cpp 38 KB

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