editor_autoload_settings.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  1. /**************************************************************************/
  2. /* editor_autoload_settings.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_autoload_settings.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/core_constants.h"
  33. #include "editor/editor_node.h"
  34. #include "editor/editor_string_names.h"
  35. #include "editor/editor_undo_redo_manager.h"
  36. #include "editor/filesystem_dock.h"
  37. #include "editor/gui/editor_file_dialog.h"
  38. #include "editor/project_settings_editor.h"
  39. #include "editor/themes/editor_scale.h"
  40. #include "scene/main/window.h"
  41. #include "scene/resources/packed_scene.h"
  42. #define PREVIEW_LIST_MAX_SIZE 10
  43. void EditorAutoloadSettings::_notification(int p_what) {
  44. switch (p_what) {
  45. case NOTIFICATION_ENTER_TREE: {
  46. List<String> afn;
  47. ResourceLoader::get_recognized_extensions_for_type("Script", &afn);
  48. ResourceLoader::get_recognized_extensions_for_type("PackedScene", &afn);
  49. for (const String &E : afn) {
  50. file_dialog->add_filter("*." + E);
  51. }
  52. browse_button->set_button_icon(get_editor_theme_icon(SNAME("Folder")));
  53. } break;
  54. case NOTIFICATION_THEME_CHANGED: {
  55. browse_button->set_button_icon(get_editor_theme_icon(SNAME("Folder")));
  56. add_autoload->set_button_icon(get_editor_theme_icon(SNAME("Add")));
  57. } break;
  58. case NOTIFICATION_VISIBILITY_CHANGED: {
  59. FileSystemDock *dock = FileSystemDock::get_singleton();
  60. if (dock != nullptr) {
  61. ScriptCreateDialog *dialog = dock->get_script_create_dialog();
  62. if (dialog != nullptr) {
  63. Callable script_created = callable_mp(this, &EditorAutoloadSettings::_script_created);
  64. if (is_visible_in_tree()) {
  65. if (!dialog->is_connected(SNAME("script_created"), script_created)) {
  66. dialog->connect("script_created", script_created);
  67. }
  68. } else {
  69. if (dialog->is_connected(SNAME("script_created"), script_created)) {
  70. dialog->disconnect("script_created", script_created);
  71. }
  72. }
  73. }
  74. }
  75. } break;
  76. }
  77. }
  78. bool EditorAutoloadSettings::_autoload_name_is_valid(const String &p_name, String *r_error) {
  79. if (!p_name.is_valid_unicode_identifier()) {
  80. if (r_error) {
  81. *r_error = TTR("Invalid name.") + " " + TTR("Must be a valid Unicode identifier.");
  82. }
  83. return false;
  84. }
  85. if (ClassDB::class_exists(p_name)) {
  86. if (r_error) {
  87. *r_error = TTR("Invalid name.") + " " + TTR("Must not collide with an existing engine class name.");
  88. }
  89. return false;
  90. }
  91. if (ScriptServer::is_global_class(p_name)) {
  92. if (r_error) {
  93. *r_error = TTR("Invalid name.") + "\n" + TTR("Must not collide with an existing global script class name.");
  94. }
  95. return false;
  96. }
  97. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  98. if (Variant::get_type_name(Variant::Type(i)) == p_name) {
  99. if (r_error) {
  100. *r_error = TTR("Invalid name.") + " " + TTR("Must not collide with an existing built-in type name.");
  101. }
  102. return false;
  103. }
  104. }
  105. for (int i = 0; i < CoreConstants::get_global_constant_count(); i++) {
  106. if (CoreConstants::get_global_constant_name(i) == p_name) {
  107. if (r_error) {
  108. *r_error = TTR("Invalid name.") + " " + TTR("Must not collide with an existing global constant name.");
  109. }
  110. return false;
  111. }
  112. }
  113. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  114. List<String> keywords;
  115. ScriptServer::get_language(i)->get_reserved_words(&keywords);
  116. for (const String &E : keywords) {
  117. if (E == p_name) {
  118. if (r_error) {
  119. *r_error = TTR("Invalid name.") + " " + TTR("Keyword cannot be used as an Autoload name.");
  120. }
  121. return false;
  122. }
  123. }
  124. }
  125. return true;
  126. }
  127. void EditorAutoloadSettings::_autoload_add() {
  128. if (autoload_add_path->get_text().is_empty()) {
  129. ScriptCreateDialog *dialog = FileSystemDock::get_singleton()->get_script_create_dialog();
  130. String fpath = path;
  131. if (!fpath.ends_with("/")) {
  132. fpath = fpath.get_base_dir();
  133. }
  134. dialog->config("Node", fpath.path_join(vformat("%s.gd", autoload_add_name->get_text())), false, false);
  135. dialog->popup_centered();
  136. } else {
  137. if (autoload_add(autoload_add_name->get_text(), autoload_add_path->get_text())) {
  138. autoload_add_path->set_text("");
  139. }
  140. autoload_add_name->set_text("");
  141. add_autoload->set_disabled(true);
  142. }
  143. }
  144. void EditorAutoloadSettings::_autoload_selected() {
  145. TreeItem *ti = tree->get_selected();
  146. if (!ti) {
  147. return;
  148. }
  149. selected_autoload = "autoload/" + ti->get_text(0);
  150. }
  151. void EditorAutoloadSettings::_autoload_edited() {
  152. if (updating_autoload) {
  153. return;
  154. }
  155. TreeItem *ti = tree->get_edited();
  156. int column = tree->get_edited_column();
  157. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  158. if (column == 0) {
  159. String name = ti->get_text(0);
  160. String old_name = selected_autoload.get_slice("/", 1);
  161. if (name == old_name) {
  162. return;
  163. }
  164. String error;
  165. if (!_autoload_name_is_valid(name, &error)) {
  166. ti->set_text(0, old_name);
  167. EditorNode::get_singleton()->show_warning(error);
  168. return;
  169. }
  170. if (ProjectSettings::get_singleton()->has_setting("autoload/" + name)) {
  171. ti->set_text(0, old_name);
  172. EditorNode::get_singleton()->show_warning(vformat(TTR("Autoload '%s' already exists!"), name));
  173. return;
  174. }
  175. updating_autoload = true;
  176. name = "autoload/" + name;
  177. int order = ProjectSettings::get_singleton()->get_order(selected_autoload);
  178. String scr_path = GLOBAL_GET(selected_autoload);
  179. undo_redo->create_action(TTR("Rename Autoload"));
  180. undo_redo->add_do_property(ProjectSettings::get_singleton(), name, scr_path);
  181. undo_redo->add_do_method(ProjectSettings::get_singleton(), "set_order", name, order);
  182. undo_redo->add_do_method(ProjectSettings::get_singleton(), "clear", selected_autoload);
  183. undo_redo->add_undo_property(ProjectSettings::get_singleton(), selected_autoload, scr_path);
  184. undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", selected_autoload, order);
  185. undo_redo->add_undo_method(ProjectSettings::get_singleton(), "clear", name);
  186. undo_redo->add_do_method(this, CoreStringName(call_deferred), "update_autoload");
  187. undo_redo->add_undo_method(this, CoreStringName(call_deferred), "update_autoload");
  188. undo_redo->add_do_method(this, "emit_signal", autoload_changed);
  189. undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
  190. undo_redo->commit_action();
  191. selected_autoload = name;
  192. } else if (column == 2) {
  193. updating_autoload = true;
  194. bool checked = ti->is_checked(2);
  195. String base = "autoload/" + ti->get_text(0);
  196. int order = ProjectSettings::get_singleton()->get_order(base);
  197. String scr_path = GLOBAL_GET(base);
  198. if (scr_path.begins_with("*")) {
  199. scr_path = scr_path.substr(1, scr_path.length());
  200. }
  201. // Singleton autoloads are represented with a leading "*" in their path.
  202. if (checked) {
  203. scr_path = "*" + scr_path;
  204. }
  205. undo_redo->create_action(TTR("Toggle Autoload Globals"));
  206. undo_redo->add_do_property(ProjectSettings::get_singleton(), base, scr_path);
  207. undo_redo->add_undo_property(ProjectSettings::get_singleton(), base, GLOBAL_GET(base));
  208. undo_redo->add_do_method(ProjectSettings::get_singleton(), "set_order", base, order);
  209. undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", base, order);
  210. undo_redo->add_do_method(this, CoreStringName(call_deferred), "update_autoload");
  211. undo_redo->add_undo_method(this, CoreStringName(call_deferred), "update_autoload");
  212. undo_redo->add_do_method(this, "emit_signal", autoload_changed);
  213. undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
  214. undo_redo->commit_action();
  215. }
  216. updating_autoload = false;
  217. }
  218. void EditorAutoloadSettings::_autoload_button_pressed(Object *p_item, int p_column, int p_button, MouseButton p_mouse_button) {
  219. if (p_mouse_button != MouseButton::LEFT) {
  220. return;
  221. }
  222. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  223. String name = "autoload/" + ti->get_text(0);
  224. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  225. switch (p_button) {
  226. case BUTTON_OPEN: {
  227. _autoload_open(ti->get_text(1));
  228. } break;
  229. case BUTTON_MOVE_UP:
  230. case BUTTON_MOVE_DOWN: {
  231. TreeItem *swap = nullptr;
  232. if (p_button == BUTTON_MOVE_UP) {
  233. swap = ti->get_prev();
  234. } else {
  235. swap = ti->get_next();
  236. }
  237. if (!swap) {
  238. return;
  239. }
  240. String swap_name = "autoload/" + swap->get_text(0);
  241. int order = ProjectSettings::get_singleton()->get_order(name);
  242. int swap_order = ProjectSettings::get_singleton()->get_order(swap_name);
  243. undo_redo->create_action(TTR("Move Autoload"));
  244. undo_redo->add_do_method(ProjectSettings::get_singleton(), "set_order", name, swap_order);
  245. undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", name, order);
  246. undo_redo->add_do_method(ProjectSettings::get_singleton(), "set_order", swap_name, order);
  247. undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", swap_name, swap_order);
  248. undo_redo->add_do_method(this, "update_autoload");
  249. undo_redo->add_undo_method(this, "update_autoload");
  250. undo_redo->add_do_method(this, "emit_signal", autoload_changed);
  251. undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
  252. undo_redo->commit_action();
  253. } break;
  254. case BUTTON_DELETE: {
  255. int order = ProjectSettings::get_singleton()->get_order(name);
  256. undo_redo->create_action(TTR("Remove Autoload"));
  257. undo_redo->add_do_property(ProjectSettings::get_singleton(), name, Variant());
  258. undo_redo->add_undo_property(ProjectSettings::get_singleton(), name, GLOBAL_GET(name));
  259. undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", name, order);
  260. undo_redo->add_do_method(this, "update_autoload");
  261. undo_redo->add_undo_method(this, "update_autoload");
  262. undo_redo->add_do_method(this, "emit_signal", autoload_changed);
  263. undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
  264. undo_redo->commit_action();
  265. } break;
  266. }
  267. }
  268. void EditorAutoloadSettings::_autoload_activated() {
  269. TreeItem *ti = tree->get_selected();
  270. if (!ti) {
  271. return;
  272. }
  273. _autoload_open(ti->get_text(1));
  274. }
  275. void EditorAutoloadSettings::_autoload_open(const String &fpath) {
  276. if (ResourceLoader::get_resource_type(fpath) == "PackedScene") {
  277. EditorNode::get_singleton()->open_request(fpath);
  278. } else {
  279. EditorNode::get_singleton()->load_resource(fpath);
  280. }
  281. ProjectSettingsEditor::get_singleton()->hide();
  282. }
  283. void EditorAutoloadSettings::_autoload_file_callback(const String &p_path) {
  284. // Convert the file name to PascalCase, which is the convention for classes in GDScript.
  285. const String class_name = p_path.get_file().get_basename().to_pascal_case();
  286. // If the name collides with a built-in class, prefix the name to make it possible to add without having to edit the name.
  287. // The prefix is subjective, but it provides better UX than leaving the Add button disabled :)
  288. const String prefix = ClassDB::class_exists(class_name) ? "Global" : "";
  289. autoload_add_name->set_text(prefix + class_name);
  290. add_autoload->set_disabled(false);
  291. }
  292. void EditorAutoloadSettings::_autoload_text_submitted(const String &p_name) {
  293. if (!autoload_add_path->get_text().is_empty() && _autoload_name_is_valid(p_name, nullptr)) {
  294. _autoload_add();
  295. }
  296. }
  297. void EditorAutoloadSettings::_autoload_path_text_changed(const String &p_path) {
  298. add_autoload->set_disabled(!_autoload_name_is_valid(autoload_add_name->get_text(), nullptr));
  299. }
  300. void EditorAutoloadSettings::_autoload_text_changed(const String &p_name) {
  301. String error_string;
  302. bool is_name_valid = _autoload_name_is_valid(p_name, &error_string);
  303. add_autoload->set_disabled(!is_name_valid);
  304. error_message->set_text(error_string);
  305. error_message->set_visible(!autoload_add_name->get_text().is_empty() && !is_name_valid);
  306. }
  307. Node *EditorAutoloadSettings::_create_autoload(const String &p_path) {
  308. Node *n = nullptr;
  309. if (ResourceLoader::get_resource_type(p_path) == "PackedScene") {
  310. // Cache the scene reference before loading it (for cyclic references)
  311. Ref<PackedScene> scn;
  312. scn.instantiate();
  313. scn->set_path(p_path);
  314. scn->reload_from_file();
  315. ERR_FAIL_COND_V_MSG(!scn.is_valid(), nullptr, vformat("Failed to create an autoload, can't load from path: %s.", p_path));
  316. if (scn.is_valid()) {
  317. n = scn->instantiate();
  318. }
  319. } else {
  320. Ref<Resource> res = ResourceLoader::load(p_path);
  321. ERR_FAIL_COND_V_MSG(res.is_null(), nullptr, vformat("Failed to create an autoload, can't load from path: %s.", p_path));
  322. Ref<Script> scr = res;
  323. if (scr.is_valid()) {
  324. ERR_FAIL_COND_V_MSG(!scr->is_valid(), nullptr, vformat("Failed to create an autoload, script '%s' is not compiling.", p_path));
  325. StringName ibt = scr->get_instance_base_type();
  326. bool valid_type = ClassDB::is_parent_class(ibt, "Node");
  327. ERR_FAIL_COND_V_MSG(!valid_type, nullptr, vformat("Failed to create an autoload, script '%s' does not inherit from 'Node'.", p_path));
  328. Object *obj = ClassDB::instantiate(ibt);
  329. ERR_FAIL_NULL_V_MSG(obj, nullptr, vformat("Failed to create an autoload, cannot instantiate '%s'.", ibt));
  330. n = Object::cast_to<Node>(obj);
  331. n->set_script(scr);
  332. }
  333. }
  334. ERR_FAIL_NULL_V_MSG(n, nullptr, vformat("Failed to create an autoload, path is not pointing to a scene or a script: %s.", p_path));
  335. return n;
  336. }
  337. void EditorAutoloadSettings::init_autoloads() {
  338. for (AutoloadInfo &info : autoload_cache) {
  339. info.node = _create_autoload(info.path);
  340. if (info.node) {
  341. Ref<Script> scr = info.node->get_script();
  342. info.in_editor = scr.is_valid() && scr->is_tool();
  343. info.node->set_name(info.name);
  344. }
  345. if (info.is_singleton) {
  346. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  347. ScriptServer::get_language(i)->add_named_global_constant(info.name, info.node);
  348. }
  349. }
  350. if (!info.is_singleton && !info.in_editor && info.node != nullptr) {
  351. memdelete(info.node);
  352. info.node = nullptr;
  353. }
  354. }
  355. for (const AutoloadInfo &info : autoload_cache) {
  356. if (info.node && info.in_editor) {
  357. // It's important to add the node without deferring because code in plugins or tool scripts
  358. // could use the autoload node when they are enabled.
  359. get_tree()->get_root()->add_child(info.node);
  360. }
  361. }
  362. }
  363. void EditorAutoloadSettings::update_autoload() {
  364. if (updating_autoload) {
  365. return;
  366. }
  367. updating_autoload = true;
  368. HashMap<String, AutoloadInfo> to_remove;
  369. List<AutoloadInfo *> to_add;
  370. for (const AutoloadInfo &info : autoload_cache) {
  371. to_remove.insert(info.name, info);
  372. }
  373. autoload_cache.clear();
  374. tree->clear();
  375. TreeItem *root = tree->create_item();
  376. List<PropertyInfo> props;
  377. ProjectSettings::get_singleton()->get_property_list(&props);
  378. for (const PropertyInfo &pi : props) {
  379. if (!pi.name.begins_with("autoload/")) {
  380. continue;
  381. }
  382. String name = pi.name.get_slice("/", 1);
  383. String scr_path = GLOBAL_GET(pi.name);
  384. if (name.is_empty()) {
  385. continue;
  386. }
  387. AutoloadInfo info;
  388. info.is_singleton = scr_path.begins_with("*");
  389. if (info.is_singleton) {
  390. scr_path = scr_path.substr(1, scr_path.length());
  391. }
  392. info.name = name;
  393. info.path = scr_path;
  394. info.order = ProjectSettings::get_singleton()->get_order(pi.name);
  395. bool need_to_add = true;
  396. if (to_remove.has(name)) {
  397. AutoloadInfo &old_info = to_remove[name];
  398. if (old_info.path == info.path) {
  399. // Still the same resource, check status
  400. info.node = old_info.node;
  401. if (info.node) {
  402. Ref<Script> scr = info.node->get_script();
  403. info.in_editor = scr.is_valid() && scr->is_tool();
  404. if (info.is_singleton == old_info.is_singleton && info.in_editor == old_info.in_editor) {
  405. to_remove.erase(name);
  406. need_to_add = false;
  407. } else {
  408. info.node = nullptr;
  409. }
  410. }
  411. }
  412. }
  413. autoload_cache.push_back(info);
  414. if (need_to_add) {
  415. to_add.push_back(&(autoload_cache.back()->get()));
  416. }
  417. TreeItem *item = tree->create_item(root);
  418. item->set_text(0, name);
  419. item->set_editable(0, true);
  420. item->set_text(1, scr_path);
  421. item->set_selectable(1, true);
  422. item->set_cell_mode(2, TreeItem::CELL_MODE_CHECK);
  423. item->set_editable(2, true);
  424. item->set_text(2, TTR("Enable"));
  425. item->set_checked(2, info.is_singleton);
  426. item->add_button(3, get_editor_theme_icon(SNAME("Load")), BUTTON_OPEN);
  427. item->add_button(3, get_editor_theme_icon(SNAME("MoveUp")), BUTTON_MOVE_UP);
  428. item->add_button(3, get_editor_theme_icon(SNAME("MoveDown")), BUTTON_MOVE_DOWN);
  429. item->add_button(3, get_editor_theme_icon(SNAME("Remove")), BUTTON_DELETE);
  430. item->set_selectable(3, false);
  431. }
  432. // Remove deleted/changed autoloads
  433. for (KeyValue<String, AutoloadInfo> &E : to_remove) {
  434. AutoloadInfo &info = E.value;
  435. if (info.is_singleton) {
  436. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  437. ScriptServer::get_language(i)->remove_named_global_constant(info.name);
  438. }
  439. }
  440. if (info.in_editor) {
  441. ERR_CONTINUE(!info.node);
  442. callable_mp((Node *)get_tree()->get_root(), &Node::remove_child).call_deferred(info.node);
  443. }
  444. if (info.node) {
  445. info.node->queue_free();
  446. info.node = nullptr;
  447. }
  448. }
  449. // Load new/changed autoloads
  450. List<Node *> nodes_to_add;
  451. for (AutoloadInfo *info : to_add) {
  452. info->node = _create_autoload(info->path);
  453. ERR_CONTINUE(!info->node);
  454. info->node->set_name(info->name);
  455. Ref<Script> scr = info->node->get_script();
  456. info->in_editor = scr.is_valid() && scr->is_tool();
  457. if (info->in_editor) {
  458. //defer so references are all valid on _ready()
  459. nodes_to_add.push_back(info->node);
  460. }
  461. if (info->is_singleton) {
  462. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  463. ScriptServer::get_language(i)->add_named_global_constant(info->name, info->node);
  464. }
  465. }
  466. if (!info->in_editor && !info->is_singleton) {
  467. // No reason to keep this node
  468. memdelete(info->node);
  469. info->node = nullptr;
  470. }
  471. }
  472. for (Node *E : nodes_to_add) {
  473. get_tree()->get_root()->add_child(E);
  474. }
  475. updating_autoload = false;
  476. }
  477. void EditorAutoloadSettings::_script_created(Ref<Script> p_script) {
  478. FileSystemDock::get_singleton()->get_script_create_dialog()->hide();
  479. path = p_script->get_path().get_base_dir();
  480. autoload_add_path->set_text(p_script->get_path());
  481. autoload_add_name->set_text(p_script->get_path().get_file().get_basename().to_pascal_case());
  482. _autoload_add();
  483. }
  484. LineEdit *EditorAutoloadSettings::get_path_box() const {
  485. return autoload_add_path;
  486. }
  487. Variant EditorAutoloadSettings::get_drag_data_fw(const Point2 &p_point, Control *p_control) {
  488. if (autoload_cache.size() <= 1) {
  489. return false;
  490. }
  491. PackedStringArray autoloads;
  492. TreeItem *next = tree->get_next_selected(nullptr);
  493. while (next) {
  494. autoloads.push_back(next->get_text(0));
  495. next = tree->get_next_selected(next);
  496. }
  497. if (autoloads.size() == 0 || autoloads.size() == autoload_cache.size()) {
  498. return Variant();
  499. }
  500. VBoxContainer *preview = memnew(VBoxContainer);
  501. int max_size = MIN(PREVIEW_LIST_MAX_SIZE, autoloads.size());
  502. for (int i = 0; i < max_size; i++) {
  503. Label *label = memnew(Label(autoloads[i]));
  504. label->set_self_modulate(Color(1, 1, 1, Math::lerp(1, 0, float(i) / PREVIEW_LIST_MAX_SIZE)));
  505. label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  506. preview->add_child(label);
  507. }
  508. tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN);
  509. tree->set_drag_preview(preview);
  510. Dictionary drop_data;
  511. drop_data["type"] = "autoload";
  512. drop_data["autoloads"] = autoloads;
  513. return drop_data;
  514. }
  515. bool EditorAutoloadSettings::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_control) const {
  516. if (updating_autoload) {
  517. return false;
  518. }
  519. Dictionary drop_data = p_data;
  520. if (!drop_data.has("type")) {
  521. return false;
  522. }
  523. if (drop_data.has("type")) {
  524. TreeItem *ti = tree->get_item_at_position(p_point);
  525. if (!ti) {
  526. return false;
  527. }
  528. int section = tree->get_drop_section_at_position(p_point);
  529. return section >= -1;
  530. }
  531. return false;
  532. }
  533. void EditorAutoloadSettings::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_control) {
  534. TreeItem *ti = tree->get_item_at_position(p_point);
  535. if (!ti) {
  536. return;
  537. }
  538. int section = tree->get_drop_section_at_position(p_point);
  539. if (section < -1) {
  540. return;
  541. }
  542. String name;
  543. bool move_to_back = false;
  544. if (section < 0) {
  545. name = ti->get_text(0);
  546. } else if (ti->get_next()) {
  547. name = ti->get_next()->get_text(0);
  548. } else {
  549. name = ti->get_text(0);
  550. move_to_back = true;
  551. }
  552. int order = ProjectSettings::get_singleton()->get_order("autoload/" + name);
  553. AutoloadInfo aux;
  554. List<AutoloadInfo>::Element *E = nullptr;
  555. if (!move_to_back) {
  556. aux.order = order;
  557. E = autoload_cache.find(aux);
  558. }
  559. Dictionary drop_data = p_data;
  560. PackedStringArray autoloads = drop_data["autoloads"];
  561. Vector<int> orders;
  562. orders.resize(autoload_cache.size());
  563. for (int i = 0; i < autoloads.size(); i++) {
  564. aux.order = ProjectSettings::get_singleton()->get_order("autoload/" + autoloads[i]);
  565. List<AutoloadInfo>::Element *I = autoload_cache.find(aux);
  566. if (move_to_back) {
  567. autoload_cache.move_to_back(I);
  568. } else if (E != I) {
  569. autoload_cache.move_before(I, E);
  570. } else if (E->next()) {
  571. E = E->next();
  572. } else {
  573. break;
  574. }
  575. }
  576. int i = 0;
  577. for (const AutoloadInfo &F : autoload_cache) {
  578. orders.write[i++] = F.order;
  579. }
  580. orders.sort();
  581. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  582. undo_redo->create_action(TTR("Rearrange Autoloads"));
  583. i = 0;
  584. for (const AutoloadInfo &F : autoload_cache) {
  585. undo_redo->add_do_method(ProjectSettings::get_singleton(), "set_order", "autoload/" + F.name, orders[i++]);
  586. undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", "autoload/" + F.name, F.order);
  587. }
  588. orders.clear();
  589. undo_redo->add_do_method(this, "update_autoload");
  590. undo_redo->add_undo_method(this, "update_autoload");
  591. undo_redo->add_do_method(this, "emit_signal", autoload_changed);
  592. undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
  593. undo_redo->commit_action();
  594. }
  595. bool EditorAutoloadSettings::autoload_add(const String &p_name, const String &p_path) {
  596. String name = p_name;
  597. String error;
  598. if (!_autoload_name_is_valid(name, &error)) {
  599. EditorNode::get_singleton()->show_warning(TTR("Can't add Autoload:") + "\n" + error);
  600. return false;
  601. }
  602. if (!FileAccess::exists(p_path)) {
  603. EditorNode::get_singleton()->show_warning(TTR("Can't add Autoload:") + "\n" + vformat(TTR("%s is an invalid path. File does not exist."), p_path));
  604. return false;
  605. }
  606. if (!p_path.begins_with("res://")) {
  607. EditorNode::get_singleton()->show_warning(TTR("Can't add Autoload:") + "\n" + vformat(TTR("%s is an invalid path. Not in resource path (res://)."), p_path));
  608. return false;
  609. }
  610. name = "autoload/" + name;
  611. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  612. undo_redo->create_action(TTR("Add Autoload"));
  613. // Singleton autoloads are represented with a leading "*" in their path.
  614. undo_redo->add_do_property(ProjectSettings::get_singleton(), name, "*" + p_path);
  615. if (ProjectSettings::get_singleton()->has_setting(name)) {
  616. undo_redo->add_undo_property(ProjectSettings::get_singleton(), name, GLOBAL_GET(name));
  617. } else {
  618. undo_redo->add_undo_property(ProjectSettings::get_singleton(), name, Variant());
  619. }
  620. undo_redo->add_do_method(this, "update_autoload");
  621. undo_redo->add_undo_method(this, "update_autoload");
  622. undo_redo->add_do_method(this, "emit_signal", autoload_changed);
  623. undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
  624. undo_redo->commit_action();
  625. return true;
  626. }
  627. void EditorAutoloadSettings::autoload_remove(const String &p_name) {
  628. String name = "autoload/" + p_name;
  629. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  630. int order = ProjectSettings::get_singleton()->get_order(name);
  631. undo_redo->create_action(TTR("Remove Autoload"));
  632. undo_redo->add_do_property(ProjectSettings::get_singleton(), name, Variant());
  633. undo_redo->add_undo_property(ProjectSettings::get_singleton(), name, GLOBAL_GET(name));
  634. undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", name, order);
  635. undo_redo->add_do_method(this, "update_autoload");
  636. undo_redo->add_undo_method(this, "update_autoload");
  637. undo_redo->add_do_method(this, "emit_signal", autoload_changed);
  638. undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
  639. undo_redo->commit_action();
  640. }
  641. void EditorAutoloadSettings::_bind_methods() {
  642. ClassDB::bind_method("update_autoload", &EditorAutoloadSettings::update_autoload);
  643. ClassDB::bind_method("autoload_add", &EditorAutoloadSettings::autoload_add);
  644. ClassDB::bind_method("autoload_remove", &EditorAutoloadSettings::autoload_remove);
  645. ADD_SIGNAL(MethodInfo("autoload_changed"));
  646. }
  647. EditorAutoloadSettings::EditorAutoloadSettings() {
  648. ProjectSettings::get_singleton()->add_hidden_prefix("autoload/");
  649. // Make first cache
  650. List<PropertyInfo> props;
  651. ProjectSettings::get_singleton()->get_property_list(&props);
  652. for (const PropertyInfo &pi : props) {
  653. if (!pi.name.begins_with("autoload/")) {
  654. continue;
  655. }
  656. String name = pi.name.get_slice("/", 1);
  657. String scr_path = GLOBAL_GET(pi.name);
  658. if (name.is_empty()) {
  659. continue;
  660. }
  661. AutoloadInfo info;
  662. info.is_singleton = scr_path.begins_with("*");
  663. if (info.is_singleton) {
  664. scr_path = scr_path.substr(1, scr_path.length());
  665. }
  666. info.name = name;
  667. info.path = scr_path;
  668. info.order = ProjectSettings::get_singleton()->get_order(pi.name);
  669. if (info.is_singleton) {
  670. // Make sure name references work before parsing scripts
  671. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  672. ScriptServer::get_language(i)->add_named_global_constant(info.name, Variant());
  673. }
  674. }
  675. autoload_cache.push_back(info);
  676. }
  677. HBoxContainer *hbc = memnew(HBoxContainer);
  678. add_child(hbc);
  679. error_message = memnew(Label);
  680. error_message->hide();
  681. error_message->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  682. error_message->add_theme_color_override(SceneStringName(font_color), EditorNode::get_singleton()->get_editor_theme()->get_color(SNAME("error_color"), EditorStringName(Editor)));
  683. add_child(error_message);
  684. Label *l = memnew(Label);
  685. l->set_text(TTR("Path:"));
  686. hbc->add_child(l);
  687. autoload_add_path = memnew(LineEdit);
  688. hbc->add_child(autoload_add_path);
  689. autoload_add_path->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  690. autoload_add_path->set_clear_button_enabled(true);
  691. autoload_add_path->set_placeholder(vformat(TTR("Set path or press \"%s\" to create a script."), TTR("Add")));
  692. autoload_add_path->connect(SceneStringName(text_changed), callable_mp(this, &EditorAutoloadSettings::_autoload_path_text_changed));
  693. browse_button = memnew(Button);
  694. hbc->add_child(browse_button);
  695. browse_button->connect(SceneStringName(pressed), callable_mp(this, &EditorAutoloadSettings::_browse_autoload_add_path));
  696. file_dialog = memnew(EditorFileDialog);
  697. hbc->add_child(file_dialog);
  698. file_dialog->connect("file_selected", callable_mp(this, &EditorAutoloadSettings::_set_autoload_add_path));
  699. file_dialog->connect("dir_selected", callable_mp(this, &EditorAutoloadSettings::_set_autoload_add_path));
  700. file_dialog->connect("files_selected", callable_mp(this, &EditorAutoloadSettings::_set_autoload_add_path));
  701. hbc->set_h_size_flags(SIZE_EXPAND_FILL);
  702. file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  703. file_dialog->connect("file_selected", callable_mp(this, &EditorAutoloadSettings::_autoload_file_callback));
  704. l = memnew(Label);
  705. l->set_text(TTR("Node Name:"));
  706. hbc->add_child(l);
  707. autoload_add_name = memnew(LineEdit);
  708. autoload_add_name->set_h_size_flags(SIZE_EXPAND_FILL);
  709. autoload_add_name->connect("text_submitted", callable_mp(this, &EditorAutoloadSettings::_autoload_text_submitted));
  710. autoload_add_name->connect(SceneStringName(text_changed), callable_mp(this, &EditorAutoloadSettings::_autoload_text_changed));
  711. hbc->add_child(autoload_add_name);
  712. add_autoload = memnew(Button);
  713. add_autoload->set_text(TTR("Add"));
  714. add_autoload->connect(SceneStringName(pressed), callable_mp(this, &EditorAutoloadSettings::_autoload_add));
  715. // The button will be enabled once a valid name is entered (either automatically or manually).
  716. add_autoload->set_disabled(true);
  717. hbc->add_child(add_autoload);
  718. tree = memnew(Tree);
  719. tree->set_hide_root(true);
  720. tree->set_select_mode(Tree::SELECT_MULTI);
  721. tree->set_allow_reselect(true);
  722. SET_DRAG_FORWARDING_GCD(tree, EditorAutoloadSettings);
  723. tree->set_columns(4);
  724. tree->set_column_titles_visible(true);
  725. tree->set_column_title(0, TTR("Name"));
  726. tree->set_column_expand(0, true);
  727. tree->set_column_expand_ratio(0, 1);
  728. tree->set_column_title(1, TTR("Path"));
  729. tree->set_column_expand(1, true);
  730. tree->set_column_clip_content(1, true);
  731. tree->set_column_expand_ratio(1, 2);
  732. tree->set_column_title(2, TTR("Global Variable"));
  733. tree->set_column_expand(2, false);
  734. tree->set_column_expand(3, false);
  735. tree->connect("cell_selected", callable_mp(this, &EditorAutoloadSettings::_autoload_selected));
  736. tree->connect("item_edited", callable_mp(this, &EditorAutoloadSettings::_autoload_edited));
  737. tree->connect("button_clicked", callable_mp(this, &EditorAutoloadSettings::_autoload_button_pressed));
  738. tree->connect("item_activated", callable_mp(this, &EditorAutoloadSettings::_autoload_activated));
  739. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  740. add_child(tree, true);
  741. }
  742. EditorAutoloadSettings::~EditorAutoloadSettings() {
  743. for (const AutoloadInfo &info : autoload_cache) {
  744. if (info.node && !info.in_editor) {
  745. memdelete(info.node);
  746. }
  747. }
  748. }
  749. void EditorAutoloadSettings::_set_autoload_add_path(const String &p_text) {
  750. autoload_add_path->set_text(p_text);
  751. autoload_add_path->emit_signal(SNAME("text_submitted"), p_text);
  752. }
  753. void EditorAutoloadSettings::_browse_autoload_add_path() {
  754. file_dialog->popup_file_dialog();
  755. }