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