2
0

editor_autoload_settings.cpp 28 KB

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