editor_data.cpp 40 KB

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