editor_autoload_settings.cpp 28 KB

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