editor_data.cpp 39 KB

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