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/image_loader.h"
  35. #include "core/io/resource_loader.h"
  36. #include "editor/editor_node.h"
  37. #include "editor/editor_undo_redo_manager.h"
  38. #include "editor/multi_node_edit.h"
  39. #include "editor/plugins/editor_plugin.h"
  40. #include "editor/plugins/script_editor_plugin.h"
  41. #include "editor/themes/editor_scale.h"
  42. #include "scene/resources/packed_scene.h"
  43. void EditorSelectionHistory::cleanup_history() {
  44. for (int i = 0; i < history.size(); i++) {
  45. bool fail = false;
  46. for (int j = 0; j < history[i].path.size(); j++) {
  47. if (!history[i].path[j].ref.is_null()) {
  48. // If the node is a MultiNodeEdit node, examine it and see if anything is missing from it.
  49. Ref<MultiNodeEdit> multi_node_edit = history[i].path[j].ref;
  50. if (multi_node_edit.is_valid()) {
  51. Node *root = EditorNode::get_singleton()->get_edited_scene();
  52. if (root) {
  53. for (int k = 0; k < multi_node_edit->get_node_count(); k++) {
  54. NodePath np = multi_node_edit->get_node(k);
  55. Node *multi_node_selected_node = root->get_node_or_null(np);
  56. if (!multi_node_selected_node) {
  57. fail = true;
  58. break;
  59. }
  60. }
  61. } else {
  62. fail = true;
  63. }
  64. } else {
  65. // Reference is not null - object still alive.
  66. continue;
  67. }
  68. }
  69. if (!fail) {
  70. Object *obj = ObjectDB::get_instance(history[i].path[j].object);
  71. if (obj) {
  72. Node *n = Object::cast_to<Node>(obj);
  73. if (n && n->is_inside_tree()) {
  74. // Node valid and inside tree - object still alive.
  75. continue;
  76. }
  77. if (!n) {
  78. // Node possibly still alive.
  79. continue;
  80. }
  81. } // Else: object not valid - not alive.
  82. fail = true;
  83. }
  84. if (fail) {
  85. break;
  86. }
  87. }
  88. if (fail) {
  89. history.remove_at(i);
  90. i--;
  91. }
  92. }
  93. if (current_elem_idx >= history.size()) {
  94. current_elem_idx = history.size() - 1;
  95. }
  96. }
  97. void EditorSelectionHistory::add_object(ObjectID p_object, const String &p_property, bool p_inspector_only) {
  98. Object *obj = ObjectDB::get_instance(p_object);
  99. ERR_FAIL_NULL(obj);
  100. RefCounted *r = Object::cast_to<RefCounted>(obj);
  101. _Object o;
  102. if (r) {
  103. o.ref = Ref<RefCounted>(r);
  104. }
  105. o.object = p_object;
  106. o.property = p_property;
  107. o.inspector_only = p_inspector_only;
  108. bool has_prev = current_elem_idx >= 0 && current_elem_idx < history.size();
  109. if (has_prev) {
  110. history.resize(current_elem_idx + 1); // Clip history to next.
  111. }
  112. HistoryElement h;
  113. if (!p_property.is_empty() && has_prev) {
  114. // Add a sub property.
  115. HistoryElement &prev_element = history.write[current_elem_idx];
  116. h = prev_element;
  117. h.path.resize(h.level + 1);
  118. h.path.push_back(o);
  119. h.level++;
  120. } else {
  121. // Create a new history item.
  122. h.path.push_back(o);
  123. h.level = 0;
  124. }
  125. history.push_back(h);
  126. current_elem_idx++;
  127. }
  128. void EditorSelectionHistory::replace_object(ObjectID p_old_object, ObjectID p_new_object) {
  129. for (HistoryElement &element : history) {
  130. for (int index = 0; index < element.path.size(); index++) {
  131. if (element.path[index].object == p_old_object) {
  132. element.path.write[index].object = p_new_object;
  133. }
  134. }
  135. }
  136. }
  137. int EditorSelectionHistory::get_history_len() {
  138. return history.size();
  139. }
  140. int EditorSelectionHistory::get_history_pos() {
  141. return current_elem_idx;
  142. }
  143. ObjectID EditorSelectionHistory::get_history_obj(int p_obj) const {
  144. ERR_FAIL_INDEX_V(p_obj, history.size(), ObjectID());
  145. ERR_FAIL_INDEX_V(history[p_obj].level, history[p_obj].path.size(), ObjectID());
  146. return history[p_obj].path[history[p_obj].level].object;
  147. }
  148. bool EditorSelectionHistory::is_at_beginning() const {
  149. return current_elem_idx <= 0;
  150. }
  151. bool EditorSelectionHistory::is_at_end() const {
  152. return ((current_elem_idx + 1) >= history.size());
  153. }
  154. bool EditorSelectionHistory::next() {
  155. cleanup_history();
  156. if ((current_elem_idx + 1) < history.size()) {
  157. current_elem_idx++;
  158. } else {
  159. return false;
  160. }
  161. return true;
  162. }
  163. bool EditorSelectionHistory::previous() {
  164. cleanup_history();
  165. if (current_elem_idx > 0) {
  166. current_elem_idx--;
  167. } else {
  168. return false;
  169. }
  170. return true;
  171. }
  172. bool EditorSelectionHistory::is_current_inspector_only() const {
  173. if (current_elem_idx < 0 || current_elem_idx >= history.size()) {
  174. return false;
  175. }
  176. const HistoryElement &h = history[current_elem_idx];
  177. return h.path[h.level].inspector_only;
  178. }
  179. ObjectID EditorSelectionHistory::get_current() {
  180. if (current_elem_idx < 0 || current_elem_idx >= history.size()) {
  181. return ObjectID();
  182. }
  183. Object *obj = ObjectDB::get_instance(get_history_obj(current_elem_idx));
  184. return obj ? obj->get_instance_id() : ObjectID();
  185. }
  186. int EditorSelectionHistory::get_path_size() const {
  187. if (current_elem_idx < 0 || current_elem_idx >= history.size()) {
  188. return 0;
  189. }
  190. return history[current_elem_idx].path.size();
  191. }
  192. ObjectID EditorSelectionHistory::get_path_object(int p_index) const {
  193. if (current_elem_idx < 0 || current_elem_idx >= history.size()) {
  194. return ObjectID();
  195. }
  196. ERR_FAIL_INDEX_V(p_index, history[current_elem_idx].path.size(), ObjectID());
  197. Object *obj = ObjectDB::get_instance(history[current_elem_idx].path[p_index].object);
  198. return obj ? obj->get_instance_id() : ObjectID();
  199. }
  200. String EditorSelectionHistory::get_path_property(int p_index) const {
  201. if (current_elem_idx < 0 || current_elem_idx >= history.size()) {
  202. return "";
  203. }
  204. ERR_FAIL_INDEX_V(p_index, history[current_elem_idx].path.size(), "");
  205. return history[current_elem_idx].path[p_index].property;
  206. }
  207. void EditorSelectionHistory::clear() {
  208. history.clear();
  209. current_elem_idx = -1;
  210. }
  211. EditorSelectionHistory::EditorSelectionHistory() {
  212. current_elem_idx = -1;
  213. }
  214. ////////////////////////////////////////////////////////////
  215. EditorPlugin *EditorData::get_handling_main_editor(Object *p_object) {
  216. // We need to iterate backwards so that we can check user-created plugins first.
  217. // Otherwise, it would not be possible for plugins to handle CanvasItem and Spatial nodes.
  218. for (int i = editor_plugins.size() - 1; i > -1; i--) {
  219. if (editor_plugins[i]->has_main_screen() && editor_plugins[i]->handles(p_object)) {
  220. return editor_plugins[i];
  221. }
  222. }
  223. return nullptr;
  224. }
  225. Vector<EditorPlugin *> EditorData::get_handling_sub_editors(Object *p_object) {
  226. Vector<EditorPlugin *> sub_plugins;
  227. for (int i = editor_plugins.size() - 1; i > -1; i--) {
  228. if (!editor_plugins[i]->has_main_screen() && editor_plugins[i]->handles(p_object)) {
  229. sub_plugins.push_back(editor_plugins[i]);
  230. }
  231. }
  232. return sub_plugins;
  233. }
  234. EditorPlugin *EditorData::get_editor_by_name(const String &p_name) {
  235. for (int i = editor_plugins.size() - 1; i > -1; i--) {
  236. if (editor_plugins[i]->get_name() == p_name) {
  237. return editor_plugins[i];
  238. }
  239. }
  240. return nullptr;
  241. }
  242. void EditorData::copy_object_params(Object *p_object) {
  243. clipboard.clear();
  244. List<PropertyInfo> pinfo;
  245. p_object->get_property_list(&pinfo);
  246. for (const PropertyInfo &E : pinfo) {
  247. if (!(E.usage & PROPERTY_USAGE_EDITOR) || E.name == "script" || E.name == "scripts" || E.name == "resource_path") {
  248. continue;
  249. }
  250. PropertyData pd;
  251. pd.name = E.name;
  252. pd.value = p_object->get(pd.name);
  253. clipboard.push_back(pd);
  254. }
  255. }
  256. void EditorData::get_editor_breakpoints(List<String> *p_breakpoints) {
  257. for (int i = 0; i < editor_plugins.size(); i++) {
  258. editor_plugins[i]->get_breakpoints(p_breakpoints);
  259. }
  260. }
  261. Dictionary EditorData::get_editor_plugin_states() const {
  262. Dictionary metadata;
  263. for (int i = 0; i < editor_plugins.size(); i++) {
  264. Dictionary state = editor_plugins[i]->get_state();
  265. if (state.is_empty()) {
  266. continue;
  267. }
  268. metadata[editor_plugins[i]->get_name()] = state;
  269. }
  270. return metadata;
  271. }
  272. Dictionary EditorData::get_scene_editor_states(int p_idx) const {
  273. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), Dictionary());
  274. EditedScene es = edited_scene[p_idx];
  275. return es.editor_states;
  276. }
  277. void EditorData::set_editor_plugin_states(const Dictionary &p_states) {
  278. if (p_states.is_empty()) {
  279. for (EditorPlugin *ep : editor_plugins) {
  280. ep->clear();
  281. }
  282. return;
  283. }
  284. List<Variant> keys;
  285. p_states.get_key_list(&keys);
  286. List<Variant>::Element *E = keys.front();
  287. for (; E; E = E->next()) {
  288. String name = E->get();
  289. int idx = -1;
  290. for (int i = 0; i < editor_plugins.size(); i++) {
  291. if (editor_plugins[i]->get_name() == name) {
  292. idx = i;
  293. break;
  294. }
  295. }
  296. if (idx == -1) {
  297. continue;
  298. }
  299. editor_plugins[idx]->set_state(p_states[name]);
  300. }
  301. }
  302. void EditorData::notify_edited_scene_changed() {
  303. for (int i = 0; i < editor_plugins.size(); i++) {
  304. editor_plugins[i]->edited_scene_changed();
  305. editor_plugins[i]->notify_scene_changed(get_edited_scene_root());
  306. }
  307. }
  308. void EditorData::notify_resource_saved(const Ref<Resource> &p_resource) {
  309. for (int i = 0; i < editor_plugins.size(); i++) {
  310. editor_plugins[i]->notify_resource_saved(p_resource);
  311. }
  312. }
  313. void EditorData::notify_scene_saved(const String &p_path) {
  314. for (int i = 0; i < editor_plugins.size(); i++) {
  315. editor_plugins[i]->notify_scene_saved(p_path);
  316. }
  317. }
  318. void EditorData::clear_editor_states() {
  319. for (int i = 0; i < editor_plugins.size(); i++) {
  320. editor_plugins[i]->clear();
  321. }
  322. }
  323. void EditorData::save_editor_external_data() {
  324. for (int i = 0; i < editor_plugins.size(); i++) {
  325. editor_plugins[i]->save_external_data();
  326. }
  327. }
  328. void EditorData::apply_changes_in_editors() {
  329. for (int i = 0; i < editor_plugins.size(); i++) {
  330. editor_plugins[i]->apply_changes();
  331. }
  332. }
  333. void EditorData::paste_object_params(Object *p_object) {
  334. ERR_FAIL_NULL(p_object);
  335. undo_redo_manager->create_action(TTR("Paste Params"));
  336. for (const PropertyData &E : clipboard) {
  337. String name = E.name;
  338. undo_redo_manager->add_do_property(p_object, name, E.value);
  339. undo_redo_manager->add_undo_property(p_object, name, p_object->get(name));
  340. }
  341. undo_redo_manager->commit_action();
  342. }
  343. bool EditorData::call_build() {
  344. bool result = true;
  345. for (int i = 0; i < editor_plugins.size() && result; i++) {
  346. result &= editor_plugins[i]->build();
  347. }
  348. return result;
  349. }
  350. void EditorData::set_scene_as_saved(int p_idx) {
  351. if (p_idx == -1) {
  352. p_idx = current_edited_scene;
  353. }
  354. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  355. undo_redo_manager->set_history_as_saved(edited_scene[p_idx].history_id);
  356. }
  357. bool EditorData::is_scene_changed(int p_idx) {
  358. if (p_idx == -1) {
  359. p_idx = current_edited_scene;
  360. }
  361. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), false);
  362. uint64_t current_scene_version = undo_redo_manager->get_or_create_history(edited_scene[p_idx].history_id).undo_redo->get_version();
  363. bool is_changed = edited_scene[p_idx].last_checked_version != current_scene_version;
  364. edited_scene.write[p_idx].last_checked_version = current_scene_version;
  365. return is_changed;
  366. }
  367. int EditorData::get_scene_history_id_from_path(const String &p_path) const {
  368. for (const EditedScene &E : edited_scene) {
  369. if (E.path == p_path) {
  370. return E.history_id;
  371. }
  372. }
  373. return 0;
  374. }
  375. int EditorData::get_current_edited_scene_history_id() const {
  376. if (current_edited_scene != -1) {
  377. return edited_scene[current_edited_scene].history_id;
  378. }
  379. return 0;
  380. }
  381. int EditorData::get_scene_history_id(int p_idx) const {
  382. return edited_scene[p_idx].history_id;
  383. }
  384. void EditorData::add_undo_redo_inspector_hook_callback(Callable p_callable) {
  385. undo_redo_callbacks.push_back(p_callable);
  386. }
  387. void EditorData::remove_undo_redo_inspector_hook_callback(Callable p_callable) {
  388. undo_redo_callbacks.erase(p_callable);
  389. }
  390. const Vector<Callable> EditorData::get_undo_redo_inspector_hook_callback() {
  391. return undo_redo_callbacks;
  392. }
  393. void EditorData::add_move_array_element_function(const StringName &p_class, Callable p_callable) {
  394. move_element_functions.insert(p_class, p_callable);
  395. }
  396. void EditorData::remove_move_array_element_function(const StringName &p_class) {
  397. move_element_functions.erase(p_class);
  398. }
  399. Callable EditorData::get_move_array_element_function(const StringName &p_class) const {
  400. if (move_element_functions.has(p_class)) {
  401. return move_element_functions[p_class];
  402. }
  403. return Callable();
  404. }
  405. void EditorData::remove_editor_plugin(EditorPlugin *p_plugin) {
  406. editor_plugins.erase(p_plugin);
  407. }
  408. void EditorData::add_editor_plugin(EditorPlugin *p_plugin) {
  409. editor_plugins.push_back(p_plugin);
  410. }
  411. int EditorData::get_editor_plugin_count() const {
  412. return editor_plugins.size();
  413. }
  414. EditorPlugin *EditorData::get_editor_plugin(int p_idx) {
  415. ERR_FAIL_INDEX_V(p_idx, editor_plugins.size(), nullptr);
  416. return editor_plugins[p_idx];
  417. }
  418. void EditorData::add_extension_editor_plugin(const StringName &p_class_name, EditorPlugin *p_plugin) {
  419. ERR_FAIL_COND(extension_editor_plugins.has(p_class_name));
  420. extension_editor_plugins.insert(p_class_name, p_plugin);
  421. }
  422. void EditorData::remove_extension_editor_plugin(const StringName &p_class_name) {
  423. extension_editor_plugins.erase(p_class_name);
  424. }
  425. bool EditorData::has_extension_editor_plugin(const StringName &p_class_name) {
  426. return extension_editor_plugins.has(p_class_name);
  427. }
  428. EditorPlugin *EditorData::get_extension_editor_plugin(const StringName &p_class_name) {
  429. EditorPlugin **plugin = extension_editor_plugins.getptr(p_class_name);
  430. return plugin == nullptr ? nullptr : *plugin;
  431. }
  432. void EditorData::add_custom_type(const String &p_type, const String &p_inherits, const Ref<Script> &p_script, const Ref<Texture2D> &p_icon) {
  433. ERR_FAIL_COND_MSG(p_script.is_null(), "It's not a reference to a valid Script object.");
  434. CustomType ct;
  435. ct.name = p_type;
  436. ct.icon = p_icon;
  437. ct.script = p_script;
  438. if (!custom_types.has(p_inherits)) {
  439. custom_types[p_inherits] = Vector<CustomType>();
  440. }
  441. custom_types[p_inherits].push_back(ct);
  442. }
  443. Variant EditorData::instantiate_custom_type(const String &p_type, const String &p_inherits) {
  444. if (get_custom_types().has(p_inherits)) {
  445. for (int i = 0; i < get_custom_types()[p_inherits].size(); i++) {
  446. if (get_custom_types()[p_inherits][i].name == p_type) {
  447. Ref<Script> script = get_custom_types()[p_inherits][i].script;
  448. Variant ob = ClassDB::instantiate(p_inherits);
  449. ERR_FAIL_COND_V(!ob, Variant());
  450. Node *n = Object::cast_to<Node>(ob);
  451. if (n) {
  452. n->set_name(p_type);
  453. }
  454. ((Object *)ob)->set_script(script);
  455. return ob;
  456. }
  457. }
  458. }
  459. return Variant();
  460. }
  461. const EditorData::CustomType *EditorData::get_custom_type_by_name(const String &p_type) const {
  462. for (const KeyValue<String, Vector<CustomType>> &E : custom_types) {
  463. for (const CustomType &F : E.value) {
  464. if (F.name == p_type) {
  465. return &F;
  466. }
  467. }
  468. }
  469. return nullptr;
  470. }
  471. const EditorData::CustomType *EditorData::get_custom_type_by_path(const String &p_path) const {
  472. for (const KeyValue<String, Vector<CustomType>> &E : custom_types) {
  473. for (const CustomType &F : E.value) {
  474. if (F.script->get_path() == p_path) {
  475. return &F;
  476. }
  477. }
  478. }
  479. return nullptr;
  480. }
  481. bool EditorData::is_type_recognized(const String &p_type) const {
  482. return ClassDB::class_exists(p_type) || ScriptServer::is_global_class(p_type) || get_custom_type_by_name(p_type);
  483. }
  484. void EditorData::remove_custom_type(const String &p_type) {
  485. for (KeyValue<String, Vector<CustomType>> &E : custom_types) {
  486. for (int i = 0; i < E.value.size(); i++) {
  487. if (E.value[i].name == p_type) {
  488. E.value.remove_at(i);
  489. if (E.value.is_empty()) {
  490. custom_types.erase(E.key);
  491. }
  492. return;
  493. }
  494. }
  495. }
  496. }
  497. void EditorData::instantiate_object_properties(Object *p_object) {
  498. ERR_FAIL_NULL(p_object);
  499. // Check if any Object-type property should be instantiated.
  500. List<PropertyInfo> pinfo;
  501. p_object->get_property_list(&pinfo);
  502. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  503. PropertyInfo pi = E->get();
  504. if (pi.type == Variant::OBJECT && pi.usage & PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT) {
  505. Object *prop = ClassDB::instantiate(pi.class_name);
  506. p_object->set(pi.name, prop);
  507. }
  508. }
  509. }
  510. int EditorData::add_edited_scene(int p_at_pos) {
  511. if (p_at_pos < 0) {
  512. p_at_pos = edited_scene.size();
  513. }
  514. EditedScene es;
  515. es.root = nullptr;
  516. es.path = String();
  517. es.file_modified_time = 0;
  518. es.history_current = -1;
  519. es.live_edit_root = NodePath(String("/root"));
  520. es.history_id = last_created_scene++;
  521. if (p_at_pos == edited_scene.size()) {
  522. edited_scene.push_back(es);
  523. } else {
  524. edited_scene.insert(p_at_pos, es);
  525. }
  526. if (current_edited_scene < 0) {
  527. current_edited_scene = 0;
  528. }
  529. return p_at_pos;
  530. }
  531. void EditorData::move_edited_scene_index(int p_idx, int p_to_idx) {
  532. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  533. ERR_FAIL_INDEX(p_to_idx, edited_scene.size());
  534. SWAP(edited_scene.write[p_idx], edited_scene.write[p_to_idx]);
  535. }
  536. void EditorData::remove_scene(int p_idx) {
  537. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  538. if (edited_scene[p_idx].root) {
  539. for (int i = 0; i < editor_plugins.size(); i++) {
  540. editor_plugins[i]->notify_scene_closed(edited_scene[p_idx].root->get_scene_file_path());
  541. }
  542. memdelete(edited_scene[p_idx].root);
  543. edited_scene.write[p_idx].root = nullptr;
  544. }
  545. if (current_edited_scene > p_idx) {
  546. current_edited_scene--;
  547. } else if (current_edited_scene == p_idx && current_edited_scene > 0) {
  548. current_edited_scene--;
  549. }
  550. if (!edited_scene[p_idx].path.is_empty()) {
  551. EditorNode::get_singleton()->emit_signal("scene_closed", edited_scene[p_idx].path);
  552. }
  553. if (undo_redo_manager->has_history(edited_scene[p_idx].history_id)) { // Might not exist if scene failed to load.
  554. undo_redo_manager->discard_history(edited_scene[p_idx].history_id);
  555. }
  556. edited_scene.remove_at(p_idx);
  557. }
  558. bool EditorData::_find_updated_instances(Node *p_root, Node *p_node, HashSet<String> &checked_paths) {
  559. Ref<SceneState> ss;
  560. if (p_node == p_root) {
  561. ss = p_node->get_scene_inherited_state();
  562. } else if (!p_node->get_scene_file_path().is_empty()) {
  563. ss = p_node->get_scene_instance_state();
  564. }
  565. if (ss.is_valid()) {
  566. String path = ss->get_path();
  567. if (!checked_paths.has(path)) {
  568. uint64_t modified_time = FileAccess::get_modified_time(path);
  569. if (modified_time != ss->get_last_modified_time()) {
  570. return true; //external scene changed
  571. }
  572. checked_paths.insert(path);
  573. }
  574. }
  575. for (int i = 0; i < p_node->get_child_count(); i++) {
  576. bool found = _find_updated_instances(p_root, p_node->get_child(i), checked_paths);
  577. if (found) {
  578. return true;
  579. }
  580. }
  581. return false;
  582. }
  583. bool EditorData::check_and_update_scene(int p_idx) {
  584. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), false);
  585. if (!edited_scene[p_idx].root) {
  586. return false;
  587. }
  588. HashSet<String> checked_scenes;
  589. bool must_reload = _find_updated_instances(edited_scene[p_idx].root, edited_scene[p_idx].root, checked_scenes);
  590. if (must_reload) {
  591. Ref<PackedScene> pscene;
  592. pscene.instantiate();
  593. EditorProgress ep("update_scene", TTR("Updating Scene"), 2);
  594. ep.step(TTR("Storing local changes..."), 0);
  595. // Pack first, so it stores diffs to previous version of saved scene.
  596. Error err = pscene->pack(edited_scene[p_idx].root);
  597. ERR_FAIL_COND_V(err != OK, false);
  598. ep.step(TTR("Updating scene..."), 1);
  599. Node *new_scene = pscene->instantiate(PackedScene::GEN_EDIT_STATE_MAIN);
  600. ERR_FAIL_NULL_V(new_scene, false);
  601. // Transfer selection.
  602. List<Node *> new_selection;
  603. for (const Node *E : edited_scene.write[p_idx].selection) {
  604. NodePath p = edited_scene[p_idx].root->get_path_to(E);
  605. Node *new_node = new_scene->get_node(p);
  606. if (new_node) {
  607. new_selection.push_back(new_node);
  608. }
  609. }
  610. new_scene->set_scene_file_path(edited_scene[p_idx].root->get_scene_file_path());
  611. Node *old_root = edited_scene[p_idx].root;
  612. EditorNode::get_singleton()->set_edited_scene(new_scene);
  613. memdelete(old_root);
  614. edited_scene.write[p_idx].selection = new_selection;
  615. return true;
  616. }
  617. return false;
  618. }
  619. int EditorData::get_edited_scene() const {
  620. return current_edited_scene;
  621. }
  622. int EditorData::get_edited_scene_from_path(const String &p_path) const {
  623. for (int i = 0; i < edited_scene.size(); i++) {
  624. if (edited_scene[i].path == p_path) {
  625. return i;
  626. }
  627. }
  628. return -1;
  629. }
  630. void EditorData::set_edited_scene(int p_idx) {
  631. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  632. current_edited_scene = p_idx;
  633. }
  634. Node *EditorData::get_edited_scene_root(int p_idx) {
  635. if (p_idx < 0) {
  636. ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), nullptr);
  637. return edited_scene[current_edited_scene].root;
  638. } else {
  639. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), nullptr);
  640. return edited_scene[p_idx].root;
  641. }
  642. }
  643. void EditorData::set_edited_scene_root(Node *p_root) {
  644. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  645. edited_scene.write[current_edited_scene].root = p_root;
  646. if (p_root) {
  647. if (!p_root->get_scene_file_path().is_empty()) {
  648. edited_scene.write[current_edited_scene].path = p_root->get_scene_file_path();
  649. } else {
  650. p_root->set_scene_file_path(edited_scene[current_edited_scene].path);
  651. }
  652. }
  653. if (!edited_scene[current_edited_scene].path.is_empty()) {
  654. edited_scene.write[current_edited_scene].file_modified_time = FileAccess::get_modified_time(edited_scene[current_edited_scene].path);
  655. }
  656. }
  657. int EditorData::get_edited_scene_count() const {
  658. return edited_scene.size();
  659. }
  660. Vector<EditorData::EditedScene> EditorData::get_edited_scenes() const {
  661. Vector<EditedScene> out_edited_scenes_list = Vector<EditedScene>();
  662. for (int i = 0; i < edited_scene.size(); i++) {
  663. out_edited_scenes_list.push_back(edited_scene[i]);
  664. }
  665. return out_edited_scenes_list;
  666. }
  667. void EditorData::set_scene_modified_time(int p_idx, uint64_t p_time) {
  668. if (p_idx == -1) {
  669. p_idx = current_edited_scene;
  670. }
  671. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  672. edited_scene.write[p_idx].file_modified_time = p_time;
  673. }
  674. uint64_t EditorData::get_scene_modified_time(int p_idx) const {
  675. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), 0);
  676. return edited_scene[p_idx].file_modified_time;
  677. }
  678. String EditorData::get_scene_type(int p_idx) const {
  679. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String());
  680. if (!edited_scene[p_idx].root) {
  681. return "";
  682. }
  683. return edited_scene[p_idx].root->get_class();
  684. }
  685. void EditorData::move_edited_scene_to_index(int p_idx) {
  686. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  687. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  688. EditedScene es = edited_scene[current_edited_scene];
  689. edited_scene.remove_at(current_edited_scene);
  690. edited_scene.insert(p_idx, es);
  691. current_edited_scene = p_idx;
  692. }
  693. Ref<Script> EditorData::get_scene_root_script(int p_idx) const {
  694. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), Ref<Script>());
  695. if (!edited_scene[p_idx].root) {
  696. return Ref<Script>();
  697. }
  698. Ref<Script> s = edited_scene[p_idx].root->get_script();
  699. if (!s.is_valid() && edited_scene[p_idx].root->get_child_count()) {
  700. Node *n = edited_scene[p_idx].root->get_child(0);
  701. while (!s.is_valid() && n && n->get_scene_file_path().is_empty()) {
  702. s = n->get_script();
  703. n = n->get_parent();
  704. }
  705. }
  706. return s;
  707. }
  708. String EditorData::get_scene_title(int p_idx, bool p_always_strip_extension) const {
  709. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String());
  710. if (!edited_scene[p_idx].root) {
  711. return TTR("[empty]");
  712. }
  713. if (edited_scene[p_idx].root->get_scene_file_path().is_empty()) {
  714. return TTR("[unsaved]");
  715. }
  716. const String filename = edited_scene[p_idx].root->get_scene_file_path().get_file();
  717. const String basename = filename.get_basename();
  718. if (p_always_strip_extension) {
  719. return basename;
  720. }
  721. // Return the filename including the extension if there's ambiguity (e.g. both `foo.tscn` and `foo.scn` are being edited).
  722. for (int i = 0; i < edited_scene.size(); i++) {
  723. if (i == p_idx) {
  724. // Don't compare the edited scene against itself.
  725. continue;
  726. }
  727. if (edited_scene[i].root && basename == edited_scene[i].root->get_scene_file_path().get_file().get_basename()) {
  728. return filename;
  729. }
  730. }
  731. // Else, return just the basename as there's no ambiguity.
  732. return basename;
  733. }
  734. void EditorData::set_scene_path(int p_idx, const String &p_path) {
  735. ERR_FAIL_INDEX(p_idx, edited_scene.size());
  736. edited_scene.write[p_idx].path = p_path;
  737. if (!edited_scene[p_idx].root) {
  738. return;
  739. }
  740. edited_scene[p_idx].root->set_scene_file_path(p_path);
  741. }
  742. String EditorData::get_scene_path(int p_idx) const {
  743. ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String());
  744. if (edited_scene[p_idx].root) {
  745. if (edited_scene[p_idx].root->get_scene_file_path().is_empty()) {
  746. edited_scene[p_idx].root->set_scene_file_path(edited_scene[p_idx].path);
  747. } else {
  748. return edited_scene[p_idx].root->get_scene_file_path();
  749. }
  750. }
  751. return edited_scene[p_idx].path;
  752. }
  753. void EditorData::set_edited_scene_live_edit_root(const NodePath &p_root) {
  754. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  755. edited_scene.write[current_edited_scene].live_edit_root = p_root;
  756. }
  757. NodePath EditorData::get_edited_scene_live_edit_root() {
  758. ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), String());
  759. return edited_scene[current_edited_scene].live_edit_root;
  760. }
  761. void EditorData::save_edited_scene_state(EditorSelection *p_selection, EditorSelectionHistory *p_history, const Dictionary &p_custom) {
  762. ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
  763. EditedScene &es = edited_scene.write[current_edited_scene];
  764. es.selection = p_selection->get_full_selected_node_list();
  765. es.history_current = p_history->current_elem_idx;
  766. es.history_stored = p_history->history;
  767. es.editor_states = get_editor_plugin_states();
  768. es.custom_state = p_custom;
  769. }
  770. Dictionary EditorData::restore_edited_scene_state(EditorSelection *p_selection, EditorSelectionHistory *p_history) {
  771. ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), Dictionary());
  772. const EditedScene &es = edited_scene.write[current_edited_scene];
  773. p_history->current_elem_idx = es.history_current;
  774. p_history->history = es.history_stored;
  775. p_selection->clear();
  776. for (Node *E : es.selection) {
  777. p_selection->add_node(E);
  778. }
  779. set_editor_plugin_states(es.editor_states);
  780. return es.custom_state;
  781. }
  782. void EditorData::clear_edited_scenes() {
  783. for (int i = 0; i < edited_scene.size(); i++) {
  784. if (edited_scene[i].root) {
  785. memdelete(edited_scene[i].root);
  786. }
  787. }
  788. edited_scene.clear();
  789. }
  790. void EditorData::set_plugin_window_layout(Ref<ConfigFile> p_layout) {
  791. for (int i = 0; i < editor_plugins.size(); i++) {
  792. editor_plugins[i]->set_window_layout(p_layout);
  793. }
  794. }
  795. void EditorData::get_plugin_window_layout(Ref<ConfigFile> p_layout) {
  796. for (int i = 0; i < editor_plugins.size(); i++) {
  797. editor_plugins[i]->get_window_layout(p_layout);
  798. }
  799. }
  800. bool EditorData::script_class_is_parent(const String &p_class, const String &p_inherits) {
  801. if (!ScriptServer::is_global_class(p_class)) {
  802. return false;
  803. }
  804. String base = p_class;
  805. while (base != p_inherits) {
  806. if (ClassDB::class_exists(base)) {
  807. return ClassDB::is_parent_class(base, p_inherits);
  808. } else if (ScriptServer::is_global_class(base)) {
  809. base = ScriptServer::get_global_class_base(base);
  810. } else {
  811. return false;
  812. }
  813. }
  814. return true;
  815. }
  816. StringName EditorData::script_class_get_base(const String &p_class) const {
  817. Ref<Script> script = script_class_load_script(p_class);
  818. if (script.is_null()) {
  819. return StringName();
  820. }
  821. Ref<Script> base_script = script->get_base_script();
  822. if (base_script.is_null()) {
  823. return ScriptServer::get_global_class_base(p_class);
  824. }
  825. return script->get_language()->get_global_class_name(base_script->get_path());
  826. }
  827. Variant EditorData::script_class_instance(const String &p_class) {
  828. if (ScriptServer::is_global_class(p_class)) {
  829. Ref<Script> script = script_class_load_script(p_class);
  830. if (script.is_valid()) {
  831. // Store in a variant to initialize the refcount if needed.
  832. Variant obj = ClassDB::instantiate(script->get_instance_base_type());
  833. if (obj) {
  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. }