editor_autoload_settings.cpp 30 KB

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