editor_autoload_settings.cpp 31 KB

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