editor_autoload_settings.cpp 30 KB

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