editor_autoload_settings.cpp 29 KB

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