editor_data.cpp 38 KB

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