editor_autoload_settings.cpp 29 KB

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