editor_data.cpp 38 KB

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