editor_data.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  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/inspector/editor_context_menu_plugin.h"
  38. #include "editor/inspector/multi_node_edit.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 (const PropertyInfo &pi : pinfo) {
  502. if (pi.type == Variant::OBJECT && pi.usage & PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT) {
  503. Object *prop = ClassDB::instantiate(pi.class_name);
  504. p_object->set(pi.name, prop);
  505. }
  506. }
  507. }
  508. int EditorData::add_edited_scene(int p_at_pos) {
  509. if (p_at_pos < 0) {
  510. p_at_pos = edited_scene.size();
  511. }
  512. EditedScene es;
  513. es.root = nullptr;
  514. es.path = String();
  515. es.file_modified_time = 0;
  516. es.history_current = -1;
  517. es.live_edit_root = NodePath(String("/root"));
  518. es.history_id = last_created_scene++;
  519. if (p_at_pos == edited_scene.size()) {
  520. edited_scene.push_back(es);
  521. } else {
  522. edited_scene.insert(p_at_pos, es);
  523. }
  524. if (current_edited_scene < 0) {
  525. current_edited_scene = 0;
  526. }
  527. return p_at_pos;
  528. }
  529. void EditorData::move_edited_scene_index(int p_idx, int p_to_idx) {
  530. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  531. ERR_FAIL_INDEX(p_to_idx, edited_scene.size());
  532. SWAP(edited_scene.write[p_idx], edited_scene.write[p_to_idx]);
  533. }
  534. void EditorData::remove_scene(int p_idx) {
  535. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  536. if (edited_scene[p_idx].root) {
  537. for (int i = 0; i < editor_plugins.size(); i++) {
  538. editor_plugins[i]->notify_scene_closed(edited_scene[p_idx].root->get_scene_file_path());
  539. }
  540. memdelete(edited_scene[p_idx].root);
  541. edited_scene.write[p_idx].root = nullptr;
  542. }
  543. if (current_edited_scene > p_idx) {
  544. current_edited_scene--;
  545. } else if (current_edited_scene == p_idx && current_edited_scene > 0) {
  546. current_edited_scene--;
  547. }
  548. if (!edited_scene[p_idx].path.is_empty()) {
  549. EditorNode::get_singleton()->emit_signal("scene_closed", edited_scene[p_idx].path);
  550. }
  551. if (undo_redo_manager->has_history(edited_scene[p_idx].history_id)) { // Might not exist if scene failed to load.
  552. undo_redo_manager->discard_history(edited_scene[p_idx].history_id);
  553. }
  554. edited_scene.remove_at(p_idx);
  555. }
  556. bool EditorData::_find_updated_instances(Node *p_root, Node *p_node, HashSet<String> &checked_paths) {
  557. Ref<SceneState> ss;
  558. if (p_node == p_root) {
  559. ss = p_node->get_scene_inherited_state();
  560. } else if (!p_node->get_scene_file_path().is_empty()) {
  561. ss = p_node->get_scene_instance_state();
  562. }
  563. if (ss.is_valid()) {
  564. String path = ss->get_path();
  565. if (!checked_paths.has(path)) {
  566. uint64_t modified_time = FileAccess::get_modified_time(path);
  567. if (modified_time != ss->get_last_modified_time()) {
  568. return true; //external scene changed
  569. }
  570. checked_paths.insert(path);
  571. }
  572. }
  573. for (int i = 0; i < p_node->get_child_count(); i++) {
  574. bool found = _find_updated_instances(p_root, p_node->get_child(i), checked_paths);
  575. if (found) {
  576. return true;
  577. }
  578. }
  579. return false;
  580. }
  581. bool EditorData::check_and_update_scene(int p_idx) {
  582. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), false);
  583. if (!edited_scene[p_idx].root) {
  584. return false;
  585. }
  586. HashSet<String> checked_scenes;
  587. bool must_reload = _find_updated_instances(edited_scene[p_idx].root, edited_scene[p_idx].root, checked_scenes);
  588. if (must_reload) {
  589. Ref<PackedScene> pscene;
  590. pscene.instantiate();
  591. EditorProgress ep("update_scene", TTR("Updating Scene"), 2);
  592. ep.step(TTR("Storing local changes..."), 0);
  593. // Pack first, so it stores diffs to previous version of saved scene.
  594. Error err = pscene->pack(edited_scene[p_idx].root);
  595. ERR_FAIL_COND_V(err != OK, false);
  596. ep.step(TTR("Updating scene..."), 1);
  597. Node *new_scene = pscene->instantiate(PackedScene::GEN_EDIT_STATE_MAIN);
  598. ERR_FAIL_NULL_V(new_scene, false);
  599. // Transfer selection.
  600. List<Node *> new_selection;
  601. for (const Node *E : edited_scene.write[p_idx].selection) {
  602. NodePath p = edited_scene[p_idx].root->get_path_to(E);
  603. Node *new_node = new_scene->get_node(p);
  604. if (new_node) {
  605. new_selection.push_back(new_node);
  606. }
  607. }
  608. new_scene->set_scene_file_path(edited_scene[p_idx].root->get_scene_file_path());
  609. Node *old_root = edited_scene[p_idx].root;
  610. EditorNode::get_singleton()->set_edited_scene(new_scene);
  611. memdelete(old_root);
  612. edited_scene.write[p_idx].selection = new_selection;
  613. return true;
  614. }
  615. return false;
  616. }
  617. int EditorData::get_edited_scene() const {
  618. return current_edited_scene;
  619. }
  620. int EditorData::get_edited_scene_from_path(const String &p_path) const {
  621. for (int i = 0; i < edited_scene.size(); i++) {
  622. if (edited_scene[i].path == p_path) {
  623. return i;
  624. }
  625. }
  626. return -1;
  627. }
  628. void EditorData::set_edited_scene(int p_idx) {
  629. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  630. current_edited_scene = p_idx;
  631. }
  632. Node *EditorData::get_edited_scene_root(int p_idx) {
  633. if (p_idx < 0) {
  634. ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), nullptr);
  635. return edited_scene[current_edited_scene].root;
  636. } else {
  637. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), nullptr);
  638. return edited_scene[p_idx].root;
  639. }
  640. }
  641. void EditorData::set_edited_scene_root(Node *p_root) {
  642. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  643. edited_scene.write[current_edited_scene].root = p_root;
  644. if (p_root) {
  645. if (!p_root->get_scene_file_path().is_empty()) {
  646. edited_scene.write[current_edited_scene].path = p_root->get_scene_file_path();
  647. } else {
  648. p_root->set_scene_file_path(edited_scene[current_edited_scene].path);
  649. }
  650. }
  651. if (!edited_scene[current_edited_scene].path.is_empty()) {
  652. edited_scene.write[current_edited_scene].file_modified_time = FileAccess::get_modified_time(edited_scene[current_edited_scene].path);
  653. }
  654. }
  655. int EditorData::get_edited_scene_count() const {
  656. return edited_scene.size();
  657. }
  658. Vector<EditorData::EditedScene> EditorData::get_edited_scenes() const {
  659. Vector<EditedScene> out_edited_scenes_list = Vector<EditedScene>();
  660. for (int i = 0; i < edited_scene.size(); i++) {
  661. out_edited_scenes_list.push_back(edited_scene[i]);
  662. }
  663. return out_edited_scenes_list;
  664. }
  665. void EditorData::set_scene_modified_time(int p_idx, uint64_t p_time) {
  666. if (p_idx == -1) {
  667. p_idx = current_edited_scene;
  668. }
  669. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  670. edited_scene.write[p_idx].file_modified_time = p_time;
  671. }
  672. uint64_t EditorData::get_scene_modified_time(int p_idx) const {
  673. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), 0);
  674. return edited_scene[p_idx].file_modified_time;
  675. }
  676. String EditorData::get_scene_type(int p_idx) const {
  677. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String());
  678. if (!edited_scene[p_idx].root) {
  679. return "";
  680. }
  681. return edited_scene[p_idx].root->get_class();
  682. }
  683. void EditorData::move_edited_scene_to_index(int p_idx) {
  684. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  685. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  686. EditedScene es = edited_scene[current_edited_scene];
  687. edited_scene.remove_at(current_edited_scene);
  688. edited_scene.insert(p_idx, es);
  689. current_edited_scene = p_idx;
  690. }
  691. Ref<Script> EditorData::get_scene_root_script(int p_idx) const {
  692. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), Ref<Script>());
  693. if (!edited_scene[p_idx].root) {
  694. return Ref<Script>();
  695. }
  696. Ref<Script> s = edited_scene[p_idx].root->get_script();
  697. if (s.is_null() && edited_scene[p_idx].root->get_child_count()) {
  698. Node *n = edited_scene[p_idx].root->get_child(0);
  699. while (s.is_null() && n && n->get_scene_file_path().is_empty()) {
  700. s = n->get_script();
  701. n = n->get_parent();
  702. }
  703. }
  704. return s;
  705. }
  706. String EditorData::get_scene_title(int p_idx, bool p_always_strip_extension) const {
  707. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String());
  708. if (!edited_scene[p_idx].root) {
  709. return TTR("[empty]");
  710. }
  711. if (edited_scene[p_idx].root->get_scene_file_path().is_empty()) {
  712. return TTR("[unsaved]");
  713. }
  714. const String filename = edited_scene[p_idx].root->get_scene_file_path().get_file();
  715. const String basename = filename.get_basename();
  716. if (p_always_strip_extension) {
  717. return basename;
  718. }
  719. // Return the filename including the extension if there's ambiguity (e.g. both `foo.tscn` and `foo.scn` are being edited).
  720. for (int i = 0; i < edited_scene.size(); i++) {
  721. if (i == p_idx) {
  722. // Don't compare the edited scene against itself.
  723. continue;
  724. }
  725. if (edited_scene[i].root && basename == edited_scene[i].root->get_scene_file_path().get_file().get_basename()) {
  726. return filename;
  727. }
  728. }
  729. // Else, return just the basename as there's no ambiguity.
  730. return basename;
  731. }
  732. void EditorData::set_scene_path(int p_idx, const String &p_path) {
  733. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  734. edited_scene.write[p_idx].path = p_path;
  735. if (!edited_scene[p_idx].root) {
  736. return;
  737. }
  738. edited_scene[p_idx].root->set_scene_file_path(p_path);
  739. }
  740. String EditorData::get_scene_path(int p_idx) const {
  741. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String());
  742. if (edited_scene[p_idx].root) {
  743. if (edited_scene[p_idx].root->get_scene_file_path().is_empty()) {
  744. edited_scene[p_idx].root->set_scene_file_path(edited_scene[p_idx].path);
  745. } else {
  746. return edited_scene[p_idx].root->get_scene_file_path();
  747. }
  748. }
  749. return edited_scene[p_idx].path;
  750. }
  751. void EditorData::set_edited_scene_live_edit_root(const NodePath &p_root) {
  752. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  753. edited_scene.write[current_edited_scene].live_edit_root = p_root;
  754. }
  755. NodePath EditorData::get_edited_scene_live_edit_root() {
  756. ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), String());
  757. return edited_scene[current_edited_scene].live_edit_root;
  758. }
  759. void EditorData::save_edited_scene_state(EditorSelection *p_selection, EditorSelectionHistory *p_history, const Dictionary &p_custom) {
  760. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  761. EditedScene &es = edited_scene.write[current_edited_scene];
  762. es.selection = p_selection->get_full_selected_node_list();
  763. es.history_current = p_history->current_elem_idx;
  764. es.history_stored = p_history->history;
  765. es.editor_states = get_editor_plugin_states();
  766. es.custom_state = p_custom;
  767. }
  768. Dictionary EditorData::restore_edited_scene_state(EditorSelection *p_selection, EditorSelectionHistory *p_history) {
  769. ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), Dictionary());
  770. const EditedScene &es = edited_scene.write[current_edited_scene];
  771. p_history->current_elem_idx = es.history_current;
  772. p_history->history = es.history_stored;
  773. p_selection->clear();
  774. for (Node *E : es.selection) {
  775. p_selection->add_node(E);
  776. }
  777. set_editor_plugin_states(es.editor_states);
  778. return es.custom_state;
  779. }
  780. void EditorData::clear_edited_scenes() {
  781. for (int i = 0; i < edited_scene.size(); i++) {
  782. if (edited_scene[i].root) {
  783. memdelete(edited_scene[i].root);
  784. }
  785. }
  786. edited_scene.clear();
  787. }
  788. void EditorData::set_plugin_window_layout(Ref<ConfigFile> p_layout) {
  789. for (int i = 0; i < editor_plugins.size(); i++) {
  790. editor_plugins[i]->set_window_layout(p_layout);
  791. }
  792. }
  793. void EditorData::get_plugin_window_layout(Ref<ConfigFile> p_layout) {
  794. for (int i = 0; i < editor_plugins.size(); i++) {
  795. editor_plugins[i]->get_window_layout(p_layout);
  796. }
  797. }
  798. bool EditorData::script_class_is_parent(const String &p_class, const String &p_inherits) {
  799. if (!ScriptServer::is_global_class(p_class)) {
  800. return false;
  801. }
  802. String base = p_class;
  803. while (base != p_inherits) {
  804. if (ClassDB::class_exists(base)) {
  805. return ClassDB::is_parent_class(base, p_inherits);
  806. } else if (ScriptServer::is_global_class(base)) {
  807. base = ScriptServer::get_global_class_base(base);
  808. } else {
  809. return false;
  810. }
  811. }
  812. return true;
  813. }
  814. Variant EditorData::script_class_instance(const String &p_class) {
  815. if (ScriptServer::is_global_class(p_class)) {
  816. Ref<Script> script = script_class_load_script(p_class);
  817. if (script.is_valid()) {
  818. // Store in a variant to initialize the refcount if needed.
  819. Variant v = ClassDB::instantiate(script->get_instance_base_type());
  820. if (v) {
  821. Object *obj = v;
  822. PropertyUtils::assign_custom_type_script(obj, script);
  823. obj->set_script(script);
  824. }
  825. return v;
  826. }
  827. }
  828. return Variant();
  829. }
  830. Ref<Script> EditorData::script_class_load_script(const String &p_class) const {
  831. if (!ScriptServer::is_global_class(p_class)) {
  832. return Ref<Script>();
  833. }
  834. String path = ScriptServer::get_global_class_path(p_class);
  835. return ResourceLoader::load(path, "Script");
  836. }
  837. void EditorData::script_class_set_icon_path(const String &p_class, const String &p_icon_path) {
  838. _script_class_icon_paths[p_class] = p_icon_path;
  839. }
  840. String EditorData::script_class_get_icon_path(const String &p_class, bool *r_valid) const {
  841. String current = p_class;
  842. while (true) {
  843. if (!ScriptServer::is_global_class(current)) {
  844. // If the classnames chain has a native class ancestor, we're done with success.
  845. if (r_valid) {
  846. *r_valid = ClassDB::class_exists(current);
  847. }
  848. return String();
  849. }
  850. HashMap<StringName, String>::ConstIterator E = _script_class_icon_paths.find(current);
  851. if ((bool)E) {
  852. if (r_valid) {
  853. *r_valid = !E->value.is_empty();
  854. return E->value;
  855. } else if (!E->value.is_empty()) {
  856. return E->value;
  857. }
  858. }
  859. current = ScriptServer::get_global_class_base(current);
  860. }
  861. }
  862. StringName EditorData::script_class_get_name(const String &p_path) const {
  863. return _script_class_file_to_path.has(p_path) ? _script_class_file_to_path[p_path] : StringName();
  864. }
  865. void EditorData::script_class_set_name(const String &p_path, const StringName &p_class) {
  866. _script_class_file_to_path[p_path] = p_class;
  867. }
  868. void EditorData::script_class_save_global_classes() {
  869. List<StringName> global_classes;
  870. ScriptServer::get_global_class_list(&global_classes);
  871. Array array_classes;
  872. for (const StringName &class_name : global_classes) {
  873. Dictionary d;
  874. String *icon = _script_class_icon_paths.getptr(class_name);
  875. d["class"] = class_name;
  876. d["language"] = ScriptServer::get_global_class_language(class_name);
  877. d["path"] = ScriptServer::get_global_class_path(class_name);
  878. d["base"] = ScriptServer::get_global_class_base(class_name);
  879. d["icon"] = icon ? *icon : String();
  880. d["is_abstract"] = ScriptServer::is_global_class_abstract(class_name);
  881. d["is_tool"] = ScriptServer::is_global_class_tool(class_name);
  882. array_classes.push_back(d);
  883. }
  884. ProjectSettings::get_singleton()->store_global_class_list(array_classes);
  885. }
  886. void EditorData::script_class_load_icon_paths() {
  887. script_class_clear_icon_paths();
  888. #ifndef DISABLE_DEPRECATED
  889. if (ProjectSettings::get_singleton()->has_setting("_global_script_class_icons")) {
  890. Dictionary d = GLOBAL_GET("_global_script_class_icons");
  891. for (const KeyValue<Variant, Variant> &kv : d) {
  892. String name = kv.key.operator String();
  893. _script_class_icon_paths[name] = kv.value;
  894. String path = ScriptServer::get_global_class_path(name);
  895. script_class_set_name(path, name);
  896. }
  897. ProjectSettings::get_singleton()->clear("_global_script_class_icons");
  898. }
  899. #endif
  900. Array script_classes = ProjectSettings::get_singleton()->get_global_class_list();
  901. for (int i = 0; i < script_classes.size(); i++) {
  902. Dictionary d = script_classes[i];
  903. if (!d.has("class") || !d.has("path") || !d.has("icon")) {
  904. continue;
  905. }
  906. String name = d["class"];
  907. _script_class_icon_paths[name] = d["icon"];
  908. script_class_set_name(d["path"], name);
  909. }
  910. }
  911. Ref<Texture2D> EditorData::extension_class_get_icon(const String &p_class) const {
  912. if (GDExtensionManager::get_singleton()->class_has_icon_path(p_class)) {
  913. String icon_path = GDExtensionManager::get_singleton()->class_get_icon_path(p_class);
  914. Ref<Texture2D> icon = _load_script_icon(icon_path);
  915. if (icon.is_valid()) {
  916. return icon;
  917. }
  918. }
  919. return nullptr;
  920. }
  921. Ref<Texture2D> EditorData::_load_script_icon(const String &p_path) const {
  922. if (!p_path.is_empty() && ResourceLoader::exists(p_path)) {
  923. Ref<Texture2D> icon = ResourceLoader::load(p_path);
  924. if (icon.is_valid()) {
  925. return icon;
  926. }
  927. }
  928. return nullptr;
  929. }
  930. Ref<Texture2D> EditorData::get_script_icon(const String &p_script_path) {
  931. // Take from the local cache, if available.
  932. {
  933. Ref<Texture2D> *icon = _script_icon_cache.getptr(p_script_path);
  934. if (icon) {
  935. // Can be an empty value if we can't resolve any icon for this script.
  936. // An empty value is still cached to avoid unnecessary attempts at resolving it again.
  937. return *icon;
  938. }
  939. }
  940. // Fast path in case the whole hierarchy is made of global classes.
  941. StringName class_name = script_class_get_name(p_script_path);
  942. {
  943. if (class_name != StringName()) {
  944. bool icon_valid = false;
  945. String icon_path = script_class_get_icon_path(class_name, &icon_valid);
  946. if (icon_valid) {
  947. Ref<Texture2D> icon = _load_script_icon(icon_path);
  948. _script_icon_cache[p_script_path] = icon;
  949. return icon;
  950. }
  951. }
  952. }
  953. Ref<Script> base_scr = ResourceLoader::load(p_script_path, "Script");
  954. while (base_scr.is_valid()) {
  955. // Check for scripted classes.
  956. String icon_path;
  957. StringName base_class_name = script_class_get_name(base_scr->get_path());
  958. if (base_scr->is_built_in() || base_class_name == StringName()) {
  959. icon_path = base_scr->get_class_icon_path();
  960. } else {
  961. icon_path = script_class_get_icon_path(base_class_name);
  962. }
  963. Ref<Texture2D> icon = _load_script_icon(icon_path);
  964. if (icon.is_valid()) {
  965. _script_icon_cache[p_script_path] = icon;
  966. return icon;
  967. }
  968. // Check for legacy custom classes defined by plugins.
  969. // TODO: Should probably be deprecated in 4.x
  970. const EditorData::CustomType *ctype = get_custom_type_by_path(base_scr->get_path());
  971. if (ctype && ctype->icon.is_valid()) {
  972. _script_icon_cache[p_script_path] = ctype->icon;
  973. return ctype->icon;
  974. }
  975. // Move to the base class.
  976. base_scr = base_scr->get_base_script();
  977. }
  978. // Check if the base type is an extension-defined type.
  979. Ref<Texture2D> ext_icon = extension_class_get_icon(class_name);
  980. if (ext_icon.is_valid()) {
  981. _script_icon_cache[p_script_path] = ext_icon;
  982. return ext_icon;
  983. }
  984. // If no icon found, cache it as null.
  985. _script_icon_cache[p_script_path] = Ref<Texture2D>();
  986. return Ref<Texture2D>();
  987. }
  988. void EditorData::clear_script_icon_cache() {
  989. _script_icon_cache.clear();
  990. }
  991. EditorData::EditorData() {
  992. undo_redo_manager = memnew(EditorUndoRedoManager);
  993. script_class_load_icon_paths();
  994. }
  995. EditorData::~EditorData() {
  996. memdelete(undo_redo_manager);
  997. }
  998. ///////////////////////////////////////////////////////////////////////////////
  999. void EditorSelection::_node_removed(Node *p_node) {
  1000. if (!selection.has(p_node)) {
  1001. return;
  1002. }
  1003. Object *meta = selection[p_node];
  1004. if (meta) {
  1005. memdelete(meta);
  1006. }
  1007. selection.erase(p_node);
  1008. changed = true;
  1009. node_list_changed = true;
  1010. }
  1011. void EditorSelection::add_node(Node *p_node) {
  1012. ERR_FAIL_NULL(p_node);
  1013. ERR_FAIL_COND(!p_node->is_inside_tree());
  1014. if (selection.has(p_node)) {
  1015. return;
  1016. }
  1017. changed = true;
  1018. node_list_changed = true;
  1019. Object *meta = nullptr;
  1020. for (Object *E : editor_plugins) {
  1021. meta = E->call("_get_editor_data", p_node);
  1022. if (meta) {
  1023. break;
  1024. }
  1025. }
  1026. selection[p_node] = meta;
  1027. p_node->connect(SceneStringName(tree_exiting), callable_mp(this, &EditorSelection::_node_removed).bind(p_node), CONNECT_ONE_SHOT);
  1028. }
  1029. void EditorSelection::remove_node(Node *p_node) {
  1030. ERR_FAIL_NULL(p_node);
  1031. if (!selection.has(p_node)) {
  1032. return;
  1033. }
  1034. changed = true;
  1035. node_list_changed = true;
  1036. Object *meta = selection[p_node];
  1037. if (meta) {
  1038. memdelete(meta);
  1039. }
  1040. selection.erase(p_node);
  1041. p_node->disconnect(SceneStringName(tree_exiting), callable_mp(this, &EditorSelection::_node_removed));
  1042. }
  1043. bool EditorSelection::is_selected(Node *p_node) const {
  1044. return selection.has(p_node);
  1045. }
  1046. void EditorSelection::_bind_methods() {
  1047. ClassDB::bind_method(D_METHOD("clear"), &EditorSelection::clear);
  1048. ClassDB::bind_method(D_METHOD("add_node", "node"), &EditorSelection::add_node);
  1049. ClassDB::bind_method(D_METHOD("remove_node", "node"), &EditorSelection::remove_node);
  1050. ClassDB::bind_method(D_METHOD("get_selected_nodes"), &EditorSelection::get_selected_nodes);
  1051. ClassDB::bind_method(D_METHOD("get_top_selected_nodes"), &EditorSelection::get_top_selected_nodes);
  1052. #ifndef DISABLE_DEPRECATED
  1053. ClassDB::bind_method(D_METHOD("get_transformable_selected_nodes"), &EditorSelection::get_top_selected_nodes);
  1054. #endif // DISABLE_DEPRECATED
  1055. ADD_SIGNAL(MethodInfo("selection_changed"));
  1056. }
  1057. void EditorSelection::add_editor_plugin(Object *p_object) {
  1058. editor_plugins.push_back(p_object);
  1059. }
  1060. void EditorSelection::_update_node_list() {
  1061. if (!node_list_changed) {
  1062. return;
  1063. }
  1064. top_selected_node_list.clear();
  1065. // If the selection does not have the parent of the selected node, then add the node to the node list.
  1066. // However, if the parent is already selected, then adding this node is redundant as
  1067. // it is included with the parent, so skip it.
  1068. for (const KeyValue<Node *, Object *> &E : selection) {
  1069. Node *parent = E.key;
  1070. parent = parent->get_parent();
  1071. bool skip = false;
  1072. while (parent) {
  1073. if (selection.has(parent)) {
  1074. skip = true;
  1075. break;
  1076. }
  1077. parent = parent->get_parent();
  1078. }
  1079. if (skip) {
  1080. continue;
  1081. }
  1082. top_selected_node_list.push_back(E.key);
  1083. }
  1084. node_list_changed = true;
  1085. }
  1086. void EditorSelection::update() {
  1087. _update_node_list();
  1088. if (!changed) {
  1089. return;
  1090. }
  1091. changed = false;
  1092. if (!emitted) {
  1093. emitted = true;
  1094. callable_mp(this, &EditorSelection::_emit_change).call_deferred();
  1095. }
  1096. }
  1097. void EditorSelection::_emit_change() {
  1098. emit_signal(SNAME("selection_changed"));
  1099. emitted = false;
  1100. }
  1101. TypedArray<Node> EditorSelection::get_top_selected_nodes() {
  1102. TypedArray<Node> ret;
  1103. for (const Node *E : top_selected_node_list) {
  1104. ret.push_back(E);
  1105. }
  1106. return ret;
  1107. }
  1108. TypedArray<Node> EditorSelection::get_selected_nodes() {
  1109. TypedArray<Node> ret;
  1110. for (const KeyValue<Node *, Object *> &E : selection) {
  1111. ret.push_back(E.key);
  1112. }
  1113. return ret;
  1114. }
  1115. const List<Node *> &EditorSelection::get_top_selected_node_list() {
  1116. if (changed) {
  1117. update();
  1118. } else {
  1119. _update_node_list();
  1120. }
  1121. return top_selected_node_list;
  1122. }
  1123. List<Node *> EditorSelection::get_full_selected_node_list() {
  1124. List<Node *> node_list;
  1125. for (const KeyValue<Node *, Object *> &E : selection) {
  1126. node_list.push_back(E.key);
  1127. }
  1128. return node_list;
  1129. }
  1130. void EditorSelection::clear() {
  1131. while (!selection.is_empty()) {
  1132. remove_node(selection.begin()->key);
  1133. }
  1134. changed = true;
  1135. node_list_changed = true;
  1136. }
  1137. EditorSelection::~EditorSelection() {
  1138. clear();
  1139. }