editor_data.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  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. List<Variant> keys;
  284. p_states.get_key_list(&keys);
  285. List<Variant>::Element *E = keys.front();
  286. for (; E; E = E->next()) {
  287. String name = E->get();
  288. int idx = -1;
  289. for (int i = 0; i < editor_plugins.size(); i++) {
  290. if (editor_plugins[i]->get_plugin_name() == name) {
  291. idx = i;
  292. break;
  293. }
  294. }
  295. if (idx == -1) {
  296. continue;
  297. }
  298. editor_plugins[idx]->set_state(p_states[name]);
  299. }
  300. }
  301. void EditorData::notify_edited_scene_changed() {
  302. for (int i = 0; i < editor_plugins.size(); i++) {
  303. editor_plugins[i]->edited_scene_changed();
  304. editor_plugins[i]->notify_scene_changed(get_edited_scene_root());
  305. }
  306. }
  307. void EditorData::notify_resource_saved(const Ref<Resource> &p_resource) {
  308. for (int i = 0; i < editor_plugins.size(); i++) {
  309. editor_plugins[i]->notify_resource_saved(p_resource);
  310. }
  311. }
  312. void EditorData::notify_scene_saved(const String &p_path) {
  313. for (int i = 0; i < editor_plugins.size(); i++) {
  314. editor_plugins[i]->notify_scene_saved(p_path);
  315. }
  316. }
  317. void EditorData::clear_editor_states() {
  318. for (int i = 0; i < editor_plugins.size(); i++) {
  319. editor_plugins[i]->clear();
  320. }
  321. }
  322. void EditorData::save_editor_external_data() {
  323. for (int i = 0; i < editor_plugins.size(); i++) {
  324. editor_plugins[i]->save_external_data();
  325. }
  326. }
  327. void EditorData::apply_changes_in_editors() {
  328. for (int i = 0; i < editor_plugins.size(); i++) {
  329. editor_plugins[i]->apply_changes();
  330. }
  331. }
  332. void EditorData::paste_object_params(Object *p_object) {
  333. ERR_FAIL_NULL(p_object);
  334. undo_redo_manager->create_action(TTR("Paste Params"));
  335. for (const PropertyData &E : clipboard) {
  336. String name = E.name;
  337. undo_redo_manager->add_do_property(p_object, name, E.value);
  338. undo_redo_manager->add_undo_property(p_object, name, p_object->get(name));
  339. }
  340. undo_redo_manager->commit_action();
  341. }
  342. bool EditorData::call_build() {
  343. bool result = true;
  344. for (int i = 0; i < editor_plugins.size() && result; i++) {
  345. result &= editor_plugins[i]->build();
  346. }
  347. return result;
  348. }
  349. void EditorData::set_scene_as_saved(int p_idx) {
  350. if (p_idx == -1) {
  351. p_idx = current_edited_scene;
  352. }
  353. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  354. undo_redo_manager->set_history_as_saved(edited_scene[p_idx].history_id);
  355. }
  356. bool EditorData::is_scene_changed(int p_idx) {
  357. if (p_idx == -1) {
  358. p_idx = current_edited_scene;
  359. }
  360. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), false);
  361. uint64_t current_scene_version = undo_redo_manager->get_or_create_history(edited_scene[p_idx].history_id).undo_redo->get_version();
  362. bool is_changed = edited_scene[p_idx].last_checked_version != current_scene_version;
  363. edited_scene.write[p_idx].last_checked_version = current_scene_version;
  364. return is_changed;
  365. }
  366. int EditorData::get_scene_history_id_from_path(const String &p_path) const {
  367. for (const EditedScene &E : edited_scene) {
  368. if (E.path == p_path) {
  369. return E.history_id;
  370. }
  371. }
  372. return 0;
  373. }
  374. int EditorData::get_current_edited_scene_history_id() const {
  375. if (current_edited_scene != -1) {
  376. return edited_scene[current_edited_scene].history_id;
  377. }
  378. return 0;
  379. }
  380. int EditorData::get_scene_history_id(int p_idx) const {
  381. return edited_scene[p_idx].history_id;
  382. }
  383. void EditorData::add_undo_redo_inspector_hook_callback(Callable p_callable) {
  384. undo_redo_callbacks.push_back(p_callable);
  385. }
  386. void EditorData::remove_undo_redo_inspector_hook_callback(Callable p_callable) {
  387. undo_redo_callbacks.erase(p_callable);
  388. }
  389. const Vector<Callable> EditorData::get_undo_redo_inspector_hook_callback() {
  390. return undo_redo_callbacks;
  391. }
  392. void EditorData::add_move_array_element_function(const StringName &p_class, Callable p_callable) {
  393. move_element_functions.insert(p_class, p_callable);
  394. }
  395. void EditorData::remove_move_array_element_function(const StringName &p_class) {
  396. move_element_functions.erase(p_class);
  397. }
  398. Callable EditorData::get_move_array_element_function(const StringName &p_class) const {
  399. if (move_element_functions.has(p_class)) {
  400. return move_element_functions[p_class];
  401. }
  402. return Callable();
  403. }
  404. void EditorData::remove_editor_plugin(EditorPlugin *p_plugin) {
  405. editor_plugins.erase(p_plugin);
  406. }
  407. void EditorData::add_editor_plugin(EditorPlugin *p_plugin) {
  408. editor_plugins.push_back(p_plugin);
  409. }
  410. int EditorData::get_editor_plugin_count() const {
  411. return editor_plugins.size();
  412. }
  413. EditorPlugin *EditorData::get_editor_plugin(int p_idx) {
  414. ERR_FAIL_INDEX_V(p_idx, editor_plugins.size(), nullptr);
  415. return editor_plugins[p_idx];
  416. }
  417. void EditorData::add_extension_editor_plugin(const StringName &p_class_name, EditorPlugin *p_plugin) {
  418. ERR_FAIL_COND(extension_editor_plugins.has(p_class_name));
  419. extension_editor_plugins.insert(p_class_name, p_plugin);
  420. }
  421. void EditorData::remove_extension_editor_plugin(const StringName &p_class_name) {
  422. extension_editor_plugins.erase(p_class_name);
  423. }
  424. bool EditorData::has_extension_editor_plugin(const StringName &p_class_name) {
  425. return extension_editor_plugins.has(p_class_name);
  426. }
  427. EditorPlugin *EditorData::get_extension_editor_plugin(const StringName &p_class_name) {
  428. EditorPlugin **plugin = extension_editor_plugins.getptr(p_class_name);
  429. return plugin == nullptr ? nullptr : *plugin;
  430. }
  431. void EditorData::add_custom_type(const String &p_type, const String &p_inherits, const Ref<Script> &p_script, const Ref<Texture2D> &p_icon) {
  432. ERR_FAIL_COND_MSG(p_script.is_null(), "It's not a reference to a valid Script object.");
  433. CustomType ct;
  434. ct.name = p_type;
  435. ct.icon = p_icon;
  436. ct.script = p_script;
  437. if (!custom_types.has(p_inherits)) {
  438. custom_types[p_inherits] = Vector<CustomType>();
  439. }
  440. custom_types[p_inherits].push_back(ct);
  441. }
  442. Variant EditorData::instantiate_custom_type(const String &p_type, const String &p_inherits) {
  443. if (get_custom_types().has(p_inherits)) {
  444. for (int i = 0; i < get_custom_types()[p_inherits].size(); i++) {
  445. if (get_custom_types()[p_inherits][i].name == p_type) {
  446. Ref<Script> script = get_custom_types()[p_inherits][i].script;
  447. // Store in a variant to initialize the refcount if needed.
  448. Variant v = ClassDB::instantiate(p_inherits);
  449. ERR_FAIL_COND_V(!v, Variant());
  450. Object *ob = v;
  451. Node *n = Object::cast_to<Node>(ob);
  452. if (n) {
  453. n->set_name(p_type);
  454. }
  455. PropertyUtils::assign_custom_type_script(ob, script);
  456. ob->set_script(script);
  457. return v;
  458. }
  459. }
  460. }
  461. return Variant();
  462. }
  463. const EditorData::CustomType *EditorData::get_custom_type_by_name(const String &p_type) const {
  464. for (const KeyValue<String, Vector<CustomType>> &E : custom_types) {
  465. for (const CustomType &F : E.value) {
  466. if (F.name == p_type) {
  467. return &F;
  468. }
  469. }
  470. }
  471. return nullptr;
  472. }
  473. const EditorData::CustomType *EditorData::get_custom_type_by_path(const String &p_path) const {
  474. for (const KeyValue<String, Vector<CustomType>> &E : custom_types) {
  475. for (const CustomType &F : E.value) {
  476. if (F.script->get_path() == p_path) {
  477. return &F;
  478. }
  479. }
  480. }
  481. return nullptr;
  482. }
  483. bool EditorData::is_type_recognized(const String &p_type) const {
  484. return ClassDB::class_exists(p_type) || ScriptServer::is_global_class(p_type) || get_custom_type_by_name(p_type);
  485. }
  486. void EditorData::remove_custom_type(const String &p_type) {
  487. for (KeyValue<String, Vector<CustomType>> &E : custom_types) {
  488. for (int i = 0; i < E.value.size(); i++) {
  489. if (E.value[i].name == p_type) {
  490. E.value.remove_at(i);
  491. if (E.value.is_empty()) {
  492. custom_types.erase(E.key);
  493. }
  494. return;
  495. }
  496. }
  497. }
  498. }
  499. void EditorData::instantiate_object_properties(Object *p_object) {
  500. ERR_FAIL_NULL(p_object);
  501. // Check if any Object-type property should be instantiated.
  502. List<PropertyInfo> pinfo;
  503. p_object->get_property_list(&pinfo);
  504. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  505. PropertyInfo pi = E->get();
  506. if (pi.type == Variant::OBJECT && pi.usage & PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT) {
  507. Object *prop = ClassDB::instantiate(pi.class_name);
  508. p_object->set(pi.name, prop);
  509. }
  510. }
  511. }
  512. int EditorData::add_edited_scene(int p_at_pos) {
  513. if (p_at_pos < 0) {
  514. p_at_pos = edited_scene.size();
  515. }
  516. EditedScene es;
  517. es.root = nullptr;
  518. es.path = String();
  519. es.file_modified_time = 0;
  520. es.history_current = -1;
  521. es.live_edit_root = NodePath(String("/root"));
  522. es.history_id = last_created_scene++;
  523. if (p_at_pos == edited_scene.size()) {
  524. edited_scene.push_back(es);
  525. } else {
  526. edited_scene.insert(p_at_pos, es);
  527. }
  528. if (current_edited_scene < 0) {
  529. current_edited_scene = 0;
  530. }
  531. return p_at_pos;
  532. }
  533. void EditorData::move_edited_scene_index(int p_idx, int p_to_idx) {
  534. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  535. ERR_FAIL_INDEX(p_to_idx, edited_scene.size());
  536. SWAP(edited_scene.write[p_idx], edited_scene.write[p_to_idx]);
  537. }
  538. void EditorData::remove_scene(int p_idx) {
  539. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  540. if (edited_scene[p_idx].root) {
  541. for (int i = 0; i < editor_plugins.size(); i++) {
  542. editor_plugins[i]->notify_scene_closed(edited_scene[p_idx].root->get_scene_file_path());
  543. }
  544. memdelete(edited_scene[p_idx].root);
  545. edited_scene.write[p_idx].root = nullptr;
  546. }
  547. if (current_edited_scene > p_idx) {
  548. current_edited_scene--;
  549. } else if (current_edited_scene == p_idx && current_edited_scene > 0) {
  550. current_edited_scene--;
  551. }
  552. if (!edited_scene[p_idx].path.is_empty()) {
  553. EditorNode::get_singleton()->emit_signal("scene_closed", edited_scene[p_idx].path);
  554. }
  555. if (undo_redo_manager->has_history(edited_scene[p_idx].history_id)) { // Might not exist if scene failed to load.
  556. undo_redo_manager->discard_history(edited_scene[p_idx].history_id);
  557. }
  558. edited_scene.remove_at(p_idx);
  559. }
  560. bool EditorData::_find_updated_instances(Node *p_root, Node *p_node, HashSet<String> &checked_paths) {
  561. Ref<SceneState> ss;
  562. if (p_node == p_root) {
  563. ss = p_node->get_scene_inherited_state();
  564. } else if (!p_node->get_scene_file_path().is_empty()) {
  565. ss = p_node->get_scene_instance_state();
  566. }
  567. if (ss.is_valid()) {
  568. String path = ss->get_path();
  569. if (!checked_paths.has(path)) {
  570. uint64_t modified_time = FileAccess::get_modified_time(path);
  571. if (modified_time != ss->get_last_modified_time()) {
  572. return true; //external scene changed
  573. }
  574. checked_paths.insert(path);
  575. }
  576. }
  577. for (int i = 0; i < p_node->get_child_count(); i++) {
  578. bool found = _find_updated_instances(p_root, p_node->get_child(i), checked_paths);
  579. if (found) {
  580. return true;
  581. }
  582. }
  583. return false;
  584. }
  585. bool EditorData::check_and_update_scene(int p_idx) {
  586. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), false);
  587. if (!edited_scene[p_idx].root) {
  588. return false;
  589. }
  590. HashSet<String> checked_scenes;
  591. bool must_reload = _find_updated_instances(edited_scene[p_idx].root, edited_scene[p_idx].root, checked_scenes);
  592. if (must_reload) {
  593. Ref<PackedScene> pscene;
  594. pscene.instantiate();
  595. EditorProgress ep("update_scene", TTR("Updating Scene"), 2);
  596. ep.step(TTR("Storing local changes..."), 0);
  597. // Pack first, so it stores diffs to previous version of saved scene.
  598. Error err = pscene->pack(edited_scene[p_idx].root);
  599. ERR_FAIL_COND_V(err != OK, false);
  600. ep.step(TTR("Updating scene..."), 1);
  601. Node *new_scene = pscene->instantiate(PackedScene::GEN_EDIT_STATE_MAIN);
  602. ERR_FAIL_NULL_V(new_scene, false);
  603. // Transfer selection.
  604. List<Node *> new_selection;
  605. for (const Node *E : edited_scene.write[p_idx].selection) {
  606. NodePath p = edited_scene[p_idx].root->get_path_to(E);
  607. Node *new_node = new_scene->get_node(p);
  608. if (new_node) {
  609. new_selection.push_back(new_node);
  610. }
  611. }
  612. new_scene->set_scene_file_path(edited_scene[p_idx].root->get_scene_file_path());
  613. Node *old_root = edited_scene[p_idx].root;
  614. EditorNode::get_singleton()->set_edited_scene(new_scene);
  615. memdelete(old_root);
  616. edited_scene.write[p_idx].selection = new_selection;
  617. return true;
  618. }
  619. return false;
  620. }
  621. int EditorData::get_edited_scene() const {
  622. return current_edited_scene;
  623. }
  624. int EditorData::get_edited_scene_from_path(const String &p_path) const {
  625. for (int i = 0; i < edited_scene.size(); i++) {
  626. if (edited_scene[i].path == p_path) {
  627. return i;
  628. }
  629. }
  630. return -1;
  631. }
  632. void EditorData::set_edited_scene(int p_idx) {
  633. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  634. current_edited_scene = p_idx;
  635. }
  636. Node *EditorData::get_edited_scene_root(int p_idx) {
  637. if (p_idx < 0) {
  638. ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), nullptr);
  639. return edited_scene[current_edited_scene].root;
  640. } else {
  641. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), nullptr);
  642. return edited_scene[p_idx].root;
  643. }
  644. }
  645. void EditorData::set_edited_scene_root(Node *p_root) {
  646. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  647. edited_scene.write[current_edited_scene].root = p_root;
  648. if (p_root) {
  649. if (!p_root->get_scene_file_path().is_empty()) {
  650. edited_scene.write[current_edited_scene].path = p_root->get_scene_file_path();
  651. } else {
  652. p_root->set_scene_file_path(edited_scene[current_edited_scene].path);
  653. }
  654. }
  655. if (!edited_scene[current_edited_scene].path.is_empty()) {
  656. edited_scene.write[current_edited_scene].file_modified_time = FileAccess::get_modified_time(edited_scene[current_edited_scene].path);
  657. }
  658. }
  659. int EditorData::get_edited_scene_count() const {
  660. return edited_scene.size();
  661. }
  662. Vector<EditorData::EditedScene> EditorData::get_edited_scenes() const {
  663. Vector<EditedScene> out_edited_scenes_list = Vector<EditedScene>();
  664. for (int i = 0; i < edited_scene.size(); i++) {
  665. out_edited_scenes_list.push_back(edited_scene[i]);
  666. }
  667. return out_edited_scenes_list;
  668. }
  669. void EditorData::set_scene_modified_time(int p_idx, uint64_t p_time) {
  670. if (p_idx == -1) {
  671. p_idx = current_edited_scene;
  672. }
  673. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  674. edited_scene.write[p_idx].file_modified_time = p_time;
  675. }
  676. uint64_t EditorData::get_scene_modified_time(int p_idx) const {
  677. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), 0);
  678. return edited_scene[p_idx].file_modified_time;
  679. }
  680. String EditorData::get_scene_type(int p_idx) const {
  681. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String());
  682. if (!edited_scene[p_idx].root) {
  683. return "";
  684. }
  685. return edited_scene[p_idx].root->get_class();
  686. }
  687. void EditorData::move_edited_scene_to_index(int p_idx) {
  688. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  689. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  690. EditedScene es = edited_scene[current_edited_scene];
  691. edited_scene.remove_at(current_edited_scene);
  692. edited_scene.insert(p_idx, es);
  693. current_edited_scene = p_idx;
  694. }
  695. Ref<Script> EditorData::get_scene_root_script(int p_idx) const {
  696. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), Ref<Script>());
  697. if (!edited_scene[p_idx].root) {
  698. return Ref<Script>();
  699. }
  700. Ref<Script> s = edited_scene[p_idx].root->get_script();
  701. if (s.is_null() && edited_scene[p_idx].root->get_child_count()) {
  702. Node *n = edited_scene[p_idx].root->get_child(0);
  703. while (s.is_null() && n && n->get_scene_file_path().is_empty()) {
  704. s = n->get_script();
  705. n = n->get_parent();
  706. }
  707. }
  708. return s;
  709. }
  710. String EditorData::get_scene_title(int p_idx, bool p_always_strip_extension) const {
  711. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String());
  712. if (!edited_scene[p_idx].root) {
  713. return TTR("[empty]");
  714. }
  715. if (edited_scene[p_idx].root->get_scene_file_path().is_empty()) {
  716. return TTR("[unsaved]");
  717. }
  718. const String filename = edited_scene[p_idx].root->get_scene_file_path().get_file();
  719. const String basename = filename.get_basename();
  720. if (p_always_strip_extension) {
  721. return basename;
  722. }
  723. // Return the filename including the extension if there's ambiguity (e.g. both `foo.tscn` and `foo.scn` are being edited).
  724. for (int i = 0; i < edited_scene.size(); i++) {
  725. if (i == p_idx) {
  726. // Don't compare the edited scene against itself.
  727. continue;
  728. }
  729. if (edited_scene[i].root && basename == edited_scene[i].root->get_scene_file_path().get_file().get_basename()) {
  730. return filename;
  731. }
  732. }
  733. // Else, return just the basename as there's no ambiguity.
  734. return basename;
  735. }
  736. void EditorData::set_scene_path(int p_idx, const String &p_path) {
  737. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  738. edited_scene.write[p_idx].path = p_path;
  739. if (!edited_scene[p_idx].root) {
  740. return;
  741. }
  742. edited_scene[p_idx].root->set_scene_file_path(p_path);
  743. }
  744. String EditorData::get_scene_path(int p_idx) const {
  745. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String());
  746. if (edited_scene[p_idx].root) {
  747. if (edited_scene[p_idx].root->get_scene_file_path().is_empty()) {
  748. edited_scene[p_idx].root->set_scene_file_path(edited_scene[p_idx].path);
  749. } else {
  750. return edited_scene[p_idx].root->get_scene_file_path();
  751. }
  752. }
  753. return edited_scene[p_idx].path;
  754. }
  755. void EditorData::set_edited_scene_live_edit_root(const NodePath &p_root) {
  756. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  757. edited_scene.write[current_edited_scene].live_edit_root = p_root;
  758. }
  759. NodePath EditorData::get_edited_scene_live_edit_root() {
  760. ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), String());
  761. return edited_scene[current_edited_scene].live_edit_root;
  762. }
  763. void EditorData::save_edited_scene_state(EditorSelection *p_selection, EditorSelectionHistory *p_history, const Dictionary &p_custom) {
  764. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  765. EditedScene &es = edited_scene.write[current_edited_scene];
  766. es.selection = p_selection->get_full_selected_node_list();
  767. es.history_current = p_history->current_elem_idx;
  768. es.history_stored = p_history->history;
  769. es.editor_states = get_editor_plugin_states();
  770. es.custom_state = p_custom;
  771. }
  772. Dictionary EditorData::restore_edited_scene_state(EditorSelection *p_selection, EditorSelectionHistory *p_history) {
  773. ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), Dictionary());
  774. const EditedScene &es = edited_scene.write[current_edited_scene];
  775. p_history->current_elem_idx = es.history_current;
  776. p_history->history = es.history_stored;
  777. p_selection->clear();
  778. for (Node *E : es.selection) {
  779. p_selection->add_node(E);
  780. }
  781. set_editor_plugin_states(es.editor_states);
  782. return es.custom_state;
  783. }
  784. void EditorData::clear_edited_scenes() {
  785. for (int i = 0; i < edited_scene.size(); i++) {
  786. if (edited_scene[i].root) {
  787. memdelete(edited_scene[i].root);
  788. }
  789. }
  790. edited_scene.clear();
  791. }
  792. void EditorData::set_plugin_window_layout(Ref<ConfigFile> p_layout) {
  793. for (int i = 0; i < editor_plugins.size(); i++) {
  794. editor_plugins[i]->set_window_layout(p_layout);
  795. }
  796. }
  797. void EditorData::get_plugin_window_layout(Ref<ConfigFile> p_layout) {
  798. for (int i = 0; i < editor_plugins.size(); i++) {
  799. editor_plugins[i]->get_window_layout(p_layout);
  800. }
  801. }
  802. bool EditorData::script_class_is_parent(const String &p_class, const String &p_inherits) {
  803. if (!ScriptServer::is_global_class(p_class)) {
  804. return false;
  805. }
  806. String base = p_class;
  807. while (base != p_inherits) {
  808. if (ClassDB::class_exists(base)) {
  809. return ClassDB::is_parent_class(base, p_inherits);
  810. } else if (ScriptServer::is_global_class(base)) {
  811. base = ScriptServer::get_global_class_base(base);
  812. } else {
  813. return false;
  814. }
  815. }
  816. return true;
  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 v = ClassDB::instantiate(script->get_instance_base_type());
  824. if (v) {
  825. Object *obj = v;
  826. PropertyUtils::assign_custom_type_script(obj, script);
  827. obj->set_script(script);
  828. }
  829. return v;
  830. }
  831. }
  832. return Variant();
  833. }
  834. Ref<Script> EditorData::script_class_load_script(const String &p_class) const {
  835. if (!ScriptServer::is_global_class(p_class)) {
  836. return Ref<Script>();
  837. }
  838. String path = ScriptServer::get_global_class_path(p_class);
  839. return ResourceLoader::load(path, "Script");
  840. }
  841. void EditorData::script_class_set_icon_path(const String &p_class, const String &p_icon_path) {
  842. _script_class_icon_paths[p_class] = p_icon_path;
  843. }
  844. String EditorData::script_class_get_icon_path(const String &p_class, bool *r_valid) const {
  845. String current = p_class;
  846. while (true) {
  847. if (!ScriptServer::is_global_class(current)) {
  848. // If the classnames chain has a native class ancestor, we're done with success.
  849. if (r_valid) {
  850. *r_valid = ClassDB::class_exists(current);
  851. }
  852. return String();
  853. }
  854. HashMap<StringName, String>::ConstIterator E = _script_class_icon_paths.find(current);
  855. if ((bool)E && !E->value.is_empty()) {
  856. if (r_valid) {
  857. *r_valid = true;
  858. }
  859. return E->value;
  860. }
  861. current = ScriptServer::get_global_class_base(current);
  862. }
  863. }
  864. StringName EditorData::script_class_get_name(const String &p_path) const {
  865. return _script_class_file_to_path.has(p_path) ? _script_class_file_to_path[p_path] : StringName();
  866. }
  867. void EditorData::script_class_set_name(const String &p_path, const StringName &p_class) {
  868. _script_class_file_to_path[p_path] = p_class;
  869. }
  870. void EditorData::script_class_save_global_classes() {
  871. List<StringName> global_classes;
  872. ScriptServer::get_global_class_list(&global_classes);
  873. Array array_classes;
  874. for (const StringName &class_name : global_classes) {
  875. Dictionary d;
  876. String *icon = _script_class_icon_paths.getptr(class_name);
  877. d["class"] = class_name;
  878. d["language"] = ScriptServer::get_global_class_language(class_name);
  879. d["path"] = ScriptServer::get_global_class_path(class_name);
  880. d["base"] = ScriptServer::get_global_class_base(class_name);
  881. d["icon"] = icon ? *icon : String();
  882. d["is_abstract"] = ScriptServer::is_global_class_abstract(class_name);
  883. d["is_tool"] = ScriptServer::is_global_class_tool(class_name);
  884. array_classes.push_back(d);
  885. }
  886. ProjectSettings::get_singleton()->store_global_class_list(array_classes);
  887. }
  888. void EditorData::script_class_load_icon_paths() {
  889. script_class_clear_icon_paths();
  890. #ifndef DISABLE_DEPRECATED
  891. if (ProjectSettings::get_singleton()->has_setting("_global_script_class_icons")) {
  892. Dictionary d = GLOBAL_GET("_global_script_class_icons");
  893. List<Variant> keys;
  894. d.get_key_list(&keys);
  895. for (const Variant &E : keys) {
  896. String name = E.operator String();
  897. _script_class_icon_paths[name] = d[name];
  898. String path = ScriptServer::get_global_class_path(name);
  899. script_class_set_name(path, name);
  900. }
  901. ProjectSettings::get_singleton()->clear("_global_script_class_icons");
  902. }
  903. #endif
  904. Array script_classes = ProjectSettings::get_singleton()->get_global_class_list();
  905. for (int i = 0; i < script_classes.size(); i++) {
  906. Dictionary d = script_classes[i];
  907. if (!d.has("class") || !d.has("path") || !d.has("icon")) {
  908. continue;
  909. }
  910. String name = d["class"];
  911. _script_class_icon_paths[name] = d["icon"];
  912. script_class_set_name(d["path"], name);
  913. }
  914. }
  915. Ref<Texture2D> EditorData::extension_class_get_icon(const String &p_class) const {
  916. if (GDExtensionManager::get_singleton()->class_has_icon_path(p_class)) {
  917. String icon_path = GDExtensionManager::get_singleton()->class_get_icon_path(p_class);
  918. Ref<Texture2D> icon = _load_script_icon(icon_path);
  919. if (icon.is_valid()) {
  920. return icon;
  921. }
  922. }
  923. return nullptr;
  924. }
  925. Ref<Texture2D> EditorData::_load_script_icon(const String &p_path) const {
  926. if (!p_path.is_empty() && ResourceLoader::exists(p_path)) {
  927. Ref<Texture2D> icon = ResourceLoader::load(p_path);
  928. if (icon.is_valid()) {
  929. return icon;
  930. }
  931. }
  932. return nullptr;
  933. }
  934. Ref<Texture2D> EditorData::get_script_icon(const String &p_script_path) {
  935. // Take from the local cache, if available.
  936. if (_script_icon_cache.has(p_script_path)) {
  937. // Can be an empty value if we can't resolve any icon for this script.
  938. // An empty value is still cached to avoid unnecessary attempts at resolving it again.
  939. return _script_icon_cache[p_script_path];
  940. }
  941. // Fast path in case the whole hierarchy is made of global classes.
  942. StringName class_name = script_class_get_name(p_script_path);
  943. {
  944. if (class_name != StringName()) {
  945. bool icon_valid = false;
  946. String icon_path = script_class_get_icon_path(class_name, &icon_valid);
  947. if (icon_valid) {
  948. Ref<Texture2D> icon = _load_script_icon(icon_path);
  949. _script_icon_cache[p_script_path] = icon;
  950. return icon;
  951. }
  952. }
  953. }
  954. Ref<Script> base_scr = ResourceLoader::load(p_script_path, "Script");
  955. while (base_scr.is_valid()) {
  956. // Check for scripted classes.
  957. String icon_path;
  958. StringName base_class_name = script_class_get_name(base_scr->get_path());
  959. if (base_scr->is_built_in() || base_class_name == StringName()) {
  960. icon_path = base_scr->get_class_icon_path();
  961. } else {
  962. icon_path = script_class_get_icon_path(base_class_name);
  963. }
  964. Ref<Texture2D> icon = _load_script_icon(icon_path);
  965. if (icon.is_valid()) {
  966. _script_icon_cache[p_script_path] = icon;
  967. return icon;
  968. }
  969. // Check for legacy custom classes defined by plugins.
  970. // TODO: Should probably be deprecated in 4.x
  971. const EditorData::CustomType *ctype = get_custom_type_by_path(base_scr->get_path());
  972. if (ctype && ctype->icon.is_valid()) {
  973. _script_icon_cache[p_script_path] = ctype->icon;
  974. return ctype->icon;
  975. }
  976. // Move to the base class.
  977. base_scr = base_scr->get_base_script();
  978. }
  979. // Check if the base type is an extension-defined type.
  980. Ref<Texture2D> ext_icon = extension_class_get_icon(class_name);
  981. if (ext_icon.is_valid()) {
  982. _script_icon_cache[p_script_path] = ext_icon;
  983. return ext_icon;
  984. }
  985. // If no icon found, cache it as null.
  986. _script_icon_cache[p_script_path] = Ref<Texture2D>();
  987. return Ref<Texture2D>();
  988. }
  989. void EditorData::clear_script_icon_cache() {
  990. _script_icon_cache.clear();
  991. }
  992. EditorData::EditorData() {
  993. undo_redo_manager = memnew(EditorUndoRedoManager);
  994. script_class_load_icon_paths();
  995. }
  996. EditorData::~EditorData() {
  997. memdelete(undo_redo_manager);
  998. }
  999. ///////////////////////////////////////////////////////////////////////////////
  1000. void EditorSelection::_node_removed(Node *p_node) {
  1001. if (!selection.has(p_node)) {
  1002. return;
  1003. }
  1004. Object *meta = selection[p_node];
  1005. if (meta) {
  1006. memdelete(meta);
  1007. }
  1008. selection.erase(p_node);
  1009. changed = true;
  1010. node_list_changed = true;
  1011. }
  1012. void EditorSelection::add_node(Node *p_node) {
  1013. ERR_FAIL_NULL(p_node);
  1014. ERR_FAIL_COND(!p_node->is_inside_tree());
  1015. if (selection.has(p_node)) {
  1016. return;
  1017. }
  1018. changed = true;
  1019. node_list_changed = true;
  1020. Object *meta = nullptr;
  1021. for (Object *E : editor_plugins) {
  1022. meta = E->call("_get_editor_data", p_node);
  1023. if (meta) {
  1024. break;
  1025. }
  1026. }
  1027. selection[p_node] = meta;
  1028. p_node->connect(SceneStringName(tree_exiting), callable_mp(this, &EditorSelection::_node_removed).bind(p_node), CONNECT_ONE_SHOT);
  1029. }
  1030. void EditorSelection::remove_node(Node *p_node) {
  1031. ERR_FAIL_NULL(p_node);
  1032. if (!selection.has(p_node)) {
  1033. return;
  1034. }
  1035. changed = true;
  1036. node_list_changed = true;
  1037. Object *meta = selection[p_node];
  1038. if (meta) {
  1039. memdelete(meta);
  1040. }
  1041. selection.erase(p_node);
  1042. p_node->disconnect(SceneStringName(tree_exiting), callable_mp(this, &EditorSelection::_node_removed));
  1043. }
  1044. bool EditorSelection::is_selected(Node *p_node) const {
  1045. return selection.has(p_node);
  1046. }
  1047. void EditorSelection::_bind_methods() {
  1048. ClassDB::bind_method(D_METHOD("clear"), &EditorSelection::clear);
  1049. ClassDB::bind_method(D_METHOD("add_node", "node"), &EditorSelection::add_node);
  1050. ClassDB::bind_method(D_METHOD("remove_node", "node"), &EditorSelection::remove_node);
  1051. ClassDB::bind_method(D_METHOD("get_selected_nodes"), &EditorSelection::get_selected_nodes);
  1052. ClassDB::bind_method(D_METHOD("get_transformable_selected_nodes"), &EditorSelection::_get_transformable_selected_nodes);
  1053. ADD_SIGNAL(MethodInfo("selection_changed"));
  1054. }
  1055. void EditorSelection::add_editor_plugin(Object *p_object) {
  1056. editor_plugins.push_back(p_object);
  1057. }
  1058. void EditorSelection::_update_node_list() {
  1059. if (!node_list_changed) {
  1060. return;
  1061. }
  1062. selected_node_list.clear();
  1063. // If the selection does not have the parent of the selected node, then add the node to the node list.
  1064. // However, if the parent is already selected, then adding this node is redundant as
  1065. // it is included with the parent, so skip it.
  1066. for (const KeyValue<Node *, Object *> &E : selection) {
  1067. Node *parent = E.key;
  1068. parent = parent->get_parent();
  1069. bool skip = false;
  1070. while (parent) {
  1071. if (selection.has(parent)) {
  1072. skip = true;
  1073. break;
  1074. }
  1075. parent = parent->get_parent();
  1076. }
  1077. if (skip) {
  1078. continue;
  1079. }
  1080. selected_node_list.push_back(E.key);
  1081. }
  1082. node_list_changed = true;
  1083. }
  1084. void EditorSelection::update() {
  1085. _update_node_list();
  1086. if (!changed) {
  1087. return;
  1088. }
  1089. changed = false;
  1090. if (!emitted) {
  1091. emitted = true;
  1092. callable_mp(this, &EditorSelection::_emit_change).call_deferred();
  1093. }
  1094. }
  1095. void EditorSelection::_emit_change() {
  1096. emit_signal(SNAME("selection_changed"));
  1097. emitted = false;
  1098. }
  1099. TypedArray<Node> EditorSelection::_get_transformable_selected_nodes() {
  1100. TypedArray<Node> ret;
  1101. for (const Node *E : selected_node_list) {
  1102. ret.push_back(E);
  1103. }
  1104. return ret;
  1105. }
  1106. TypedArray<Node> EditorSelection::get_selected_nodes() {
  1107. TypedArray<Node> ret;
  1108. for (const KeyValue<Node *, Object *> &E : selection) {
  1109. ret.push_back(E.key);
  1110. }
  1111. return ret;
  1112. }
  1113. List<Node *> &EditorSelection::get_selected_node_list() {
  1114. if (changed) {
  1115. update();
  1116. } else {
  1117. _update_node_list();
  1118. }
  1119. return selected_node_list;
  1120. }
  1121. List<Node *> EditorSelection::get_full_selected_node_list() {
  1122. List<Node *> node_list;
  1123. for (const KeyValue<Node *, Object *> &E : selection) {
  1124. node_list.push_back(E.key);
  1125. }
  1126. return node_list;
  1127. }
  1128. void EditorSelection::clear() {
  1129. while (!selection.is_empty()) {
  1130. remove_node(selection.begin()->key);
  1131. }
  1132. changed = true;
  1133. node_list_changed = true;
  1134. }
  1135. EditorSelection::EditorSelection() {
  1136. }
  1137. EditorSelection::~EditorSelection() {
  1138. clear();
  1139. }