editor_autoload_settings.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. /**************************************************************************/
  2. /* editor_autoload_settings.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "editor_autoload_settings.h"
  31. #include "core/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.") + " ";
  60. if (p_name.size() > 0 && p_name.left(1).is_numeric()) {
  61. *r_error += TTR("Cannot begin with a digit.");
  62. } else {
  63. *r_error += TTR("Valid characters:") + " a-z, A-Z, 0-9 or _";
  64. }
  65. }
  66. return false;
  67. }
  68. if (ClassDB::class_exists(p_name)) {
  69. if (r_error) {
  70. *r_error = TTR("Invalid name.") + " " + TTR("Must not collide with an existing engine class name.");
  71. }
  72. return false;
  73. }
  74. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  75. if (Variant::get_type_name(Variant::Type(i)) == p_name) {
  76. if (r_error) {
  77. *r_error = TTR("Invalid name.") + " " + TTR("Must not collide with an existing built-in type name.");
  78. }
  79. return false;
  80. }
  81. }
  82. for (int i = 0; i < GlobalConstants::get_global_constant_count(); i++) {
  83. if (GlobalConstants::get_global_constant_name(i) == p_name) {
  84. if (r_error) {
  85. *r_error = TTR("Invalid name.") + " " + TTR("Must not collide with an existing global constant name.");
  86. }
  87. return false;
  88. }
  89. }
  90. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  91. List<String> keywords;
  92. ScriptServer::get_language(i)->get_reserved_words(&keywords);
  93. for (List<String>::Element *E = keywords.front(); E; E = E->next()) {
  94. if (E->get() == p_name) {
  95. if (r_error) {
  96. *r_error = TTR("Invalid name.") + " " + TTR("Keyword cannot be used as an autoload name.");
  97. }
  98. return false;
  99. }
  100. }
  101. }
  102. return true;
  103. }
  104. void EditorAutoloadSettings::_autoload_add() {
  105. if (autoload_add(autoload_add_name->get_text(), autoload_add_path->get_line_edit()->get_text())) {
  106. autoload_add_path->get_line_edit()->set_text("");
  107. }
  108. autoload_add_name->set_text("");
  109. add_autoload->set_disabled(true);
  110. }
  111. void EditorAutoloadSettings::_autoload_selected() {
  112. TreeItem *ti = tree->get_selected();
  113. if (!ti) {
  114. return;
  115. }
  116. selected_autoload = "autoload/" + ti->get_text(0);
  117. }
  118. void EditorAutoloadSettings::_autoload_edited() {
  119. if (updating_autoload) {
  120. return;
  121. }
  122. TreeItem *ti = tree->get_edited();
  123. int column = tree->get_edited_column();
  124. UndoRedo *undo_redo = EditorNode::get_undo_redo();
  125. if (column == 0) {
  126. String name = ti->get_text(0);
  127. String old_name = selected_autoload.get_slice("/", 1);
  128. if (name == old_name) {
  129. return;
  130. }
  131. String error;
  132. if (!_autoload_name_is_valid(name, &error)) {
  133. ti->set_text(0, old_name);
  134. EditorNode::get_singleton()->show_warning(error);
  135. return;
  136. }
  137. if (ProjectSettings::get_singleton()->has_setting("autoload/" + name)) {
  138. ti->set_text(0, old_name);
  139. EditorNode::get_singleton()->show_warning(vformat(TTR("Autoload '%s' already exists!"), name));
  140. return;
  141. }
  142. updating_autoload = true;
  143. name = "autoload/" + name;
  144. int order = ProjectSettings::get_singleton()->get_order(selected_autoload);
  145. String path = ProjectSettings::get_singleton()->get(selected_autoload);
  146. undo_redo->create_action(TTR("Rename Autoload"));
  147. undo_redo->add_do_property(ProjectSettings::get_singleton(), name, path);
  148. undo_redo->add_do_method(ProjectSettings::get_singleton(), "set_order", name, order);
  149. undo_redo->add_do_method(ProjectSettings::get_singleton(), "clear", selected_autoload);
  150. undo_redo->add_undo_property(ProjectSettings::get_singleton(), selected_autoload, path);
  151. undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", selected_autoload, order);
  152. undo_redo->add_undo_method(ProjectSettings::get_singleton(), "clear", name);
  153. undo_redo->add_do_method(this, "call_deferred", "update_autoload");
  154. undo_redo->add_undo_method(this, "call_deferred", "update_autoload");
  155. undo_redo->add_do_method(this, "emit_signal", autoload_changed);
  156. undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
  157. undo_redo->commit_action();
  158. selected_autoload = name;
  159. } else if (column == 2) {
  160. updating_autoload = true;
  161. bool checked = ti->is_checked(2);
  162. String base = "autoload/" + ti->get_text(0);
  163. int order = ProjectSettings::get_singleton()->get_order(base);
  164. String path = ProjectSettings::get_singleton()->get(base);
  165. if (path.begins_with("*")) {
  166. path = path.substr(1, path.length());
  167. }
  168. // Singleton autoloads are represented with a leading "*" in their path.
  169. if (checked) {
  170. path = "*" + path;
  171. }
  172. undo_redo->create_action(TTR("Toggle AutoLoad Globals"));
  173. undo_redo->add_do_property(ProjectSettings::get_singleton(), base, path);
  174. undo_redo->add_undo_property(ProjectSettings::get_singleton(), base, ProjectSettings::get_singleton()->get(base));
  175. undo_redo->add_do_method(ProjectSettings::get_singleton(), "set_order", base, order);
  176. undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", base, order);
  177. undo_redo->add_do_method(this, "call_deferred", "update_autoload");
  178. undo_redo->add_undo_method(this, "call_deferred", "update_autoload");
  179. undo_redo->add_do_method(this, "emit_signal", autoload_changed);
  180. undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
  181. undo_redo->commit_action();
  182. }
  183. updating_autoload = false;
  184. }
  185. void EditorAutoloadSettings::_autoload_button_pressed(Object *p_item, int p_column, int p_button) {
  186. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  187. String name = "autoload/" + ti->get_text(0);
  188. UndoRedo *undo_redo = EditorNode::get_undo_redo();
  189. switch (p_button) {
  190. case BUTTON_OPEN: {
  191. _autoload_open(ti->get_text(1));
  192. } break;
  193. case BUTTON_MOVE_UP:
  194. case BUTTON_MOVE_DOWN: {
  195. TreeItem *swap = nullptr;
  196. if (p_button == BUTTON_MOVE_UP) {
  197. swap = ti->get_prev();
  198. } else {
  199. swap = ti->get_next();
  200. }
  201. if (!swap) {
  202. return;
  203. }
  204. String swap_name = "autoload/" + swap->get_text(0);
  205. int order = ProjectSettings::get_singleton()->get_order(name);
  206. int swap_order = ProjectSettings::get_singleton()->get_order(swap_name);
  207. undo_redo->create_action(TTR("Move Autoload"));
  208. undo_redo->add_do_method(ProjectSettings::get_singleton(), "set_order", name, swap_order);
  209. undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", name, order);
  210. undo_redo->add_do_method(ProjectSettings::get_singleton(), "set_order", swap_name, order);
  211. undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", swap_name, swap_order);
  212. undo_redo->add_do_method(this, "update_autoload");
  213. undo_redo->add_undo_method(this, "update_autoload");
  214. undo_redo->add_do_method(this, "emit_signal", autoload_changed);
  215. undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
  216. undo_redo->commit_action();
  217. } break;
  218. case BUTTON_DELETE: {
  219. int order = ProjectSettings::get_singleton()->get_order(name);
  220. undo_redo->create_action(TTR("Remove Autoload"));
  221. undo_redo->add_do_property(ProjectSettings::get_singleton(), name, Variant());
  222. undo_redo->add_undo_property(ProjectSettings::get_singleton(), name, ProjectSettings::get_singleton()->get(name));
  223. undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_persisting", name, true);
  224. undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", order);
  225. undo_redo->add_do_method(this, "update_autoload");
  226. undo_redo->add_undo_method(this, "update_autoload");
  227. undo_redo->add_do_method(this, "emit_signal", autoload_changed);
  228. undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
  229. undo_redo->commit_action();
  230. } break;
  231. }
  232. }
  233. void EditorAutoloadSettings::_autoload_activated() {
  234. TreeItem *ti = tree->get_selected();
  235. if (!ti) {
  236. return;
  237. }
  238. _autoload_open(ti->get_text(1));
  239. }
  240. void EditorAutoloadSettings::_autoload_open(const String &fpath) {
  241. if (ResourceLoader::get_resource_type(fpath) == "PackedScene") {
  242. EditorNode::get_singleton()->open_request(fpath);
  243. } else {
  244. EditorNode::get_singleton()->load_resource(fpath);
  245. }
  246. ProjectSettingsEditor::get_singleton()->hide();
  247. }
  248. void EditorAutoloadSettings::_autoload_file_callback(const String &p_path) {
  249. // Convert the file name to PascalCase, which is the convention for classes in GDScript.
  250. const String class_name = p_path.get_file().get_basename().capitalize().replace(" ", "");
  251. // If the name collides with a built-in class, prefix the name to make it possible to add without having to edit the name.
  252. // The prefix is subjective, but it provides better UX than leaving the Add button disabled :)
  253. const String prefix = ClassDB::class_exists(class_name) ? "Global" : "";
  254. autoload_add_name->set_text(prefix + class_name);
  255. add_autoload->set_disabled(false);
  256. }
  257. void EditorAutoloadSettings::_autoload_text_entered(const String p_name) {
  258. if (autoload_add_path->get_line_edit()->get_text() != "" && _autoload_name_is_valid(p_name, nullptr)) {
  259. _autoload_add();
  260. }
  261. }
  262. void EditorAutoloadSettings::_autoload_path_text_changed(const String p_path) {
  263. add_autoload->set_disabled(
  264. p_path == "" || !_autoload_name_is_valid(autoload_add_name->get_text(), nullptr));
  265. }
  266. void EditorAutoloadSettings::_autoload_text_changed(const String p_name) {
  267. String error_string;
  268. bool is_name_valid = _autoload_name_is_valid(p_name, &error_string);
  269. add_autoload->set_disabled(autoload_add_path->get_line_edit()->get_text() == "" || !is_name_valid);
  270. error_message->set_text(error_string);
  271. error_message->set_visible(autoload_add_name->get_text() != "" && !is_name_valid);
  272. }
  273. Node *EditorAutoloadSettings::_create_autoload(const String &p_path) {
  274. RES res = ResourceLoader::load(p_path);
  275. ERR_FAIL_COND_V_MSG(res.is_null(), nullptr, "Can't autoload: " + p_path + ".");
  276. Node *n = nullptr;
  277. if (res->is_class("PackedScene")) {
  278. Ref<PackedScene> ps = res;
  279. n = ps->instance();
  280. } else if (res->is_class("Script")) {
  281. Ref<Script> s = res;
  282. StringName ibt = s->get_instance_base_type();
  283. bool valid_type = ClassDB::is_parent_class(ibt, "Node");
  284. ERR_FAIL_COND_V_MSG(!valid_type, nullptr, "Script does not inherit from Node: " + p_path + ".");
  285. Object *obj = ClassDB::instance(ibt);
  286. ERR_FAIL_COND_V_MSG(obj == nullptr, nullptr, "Cannot instance script for autoload, expected 'Node' inheritance, got: " + String(ibt) + ".");
  287. n = Object::cast_to<Node>(obj);
  288. n->set_script(s.get_ref_ptr());
  289. }
  290. ERR_FAIL_COND_V_MSG(!n, nullptr, "Path in autoload not a node or script: " + p_path + ".");
  291. return n;
  292. }
  293. void EditorAutoloadSettings::update_autoload() {
  294. if (updating_autoload) {
  295. return;
  296. }
  297. updating_autoload = true;
  298. Map<String, AutoLoadInfo> to_remove;
  299. List<AutoLoadInfo *> to_add;
  300. for (List<AutoLoadInfo>::Element *E = autoload_cache.front(); E; E = E->next()) {
  301. AutoLoadInfo &info = E->get();
  302. to_remove.insert(info.name, info);
  303. }
  304. autoload_cache.clear();
  305. tree->clear();
  306. TreeItem *root = tree->create_item();
  307. List<PropertyInfo> props;
  308. ProjectSettings::get_singleton()->get_property_list(&props);
  309. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  310. const PropertyInfo &pi = E->get();
  311. if (!pi.name.begins_with("autoload/")) {
  312. continue;
  313. }
  314. String name = pi.name.get_slice("/", 1);
  315. String path = ProjectSettings::get_singleton()->get(pi.name);
  316. if (name.empty()) {
  317. continue;
  318. }
  319. AutoLoadInfo info;
  320. info.is_singleton = path.begins_with("*");
  321. if (info.is_singleton) {
  322. path = path.substr(1, path.length());
  323. }
  324. info.name = name;
  325. info.path = path;
  326. info.order = ProjectSettings::get_singleton()->get_order(pi.name);
  327. bool need_to_add = true;
  328. if (to_remove.has(name)) {
  329. AutoLoadInfo &old_info = to_remove[name];
  330. if (old_info.path == info.path) {
  331. // Still the same resource, check status
  332. info.node = old_info.node;
  333. if (info.node) {
  334. Ref<Script> scr = info.node->get_script();
  335. info.in_editor = scr.is_valid() && scr->is_tool();
  336. if (info.is_singleton == old_info.is_singleton && info.in_editor == old_info.in_editor) {
  337. to_remove.erase(name);
  338. need_to_add = false;
  339. } else {
  340. info.node = nullptr;
  341. }
  342. }
  343. }
  344. }
  345. autoload_cache.push_back(info);
  346. if (need_to_add) {
  347. to_add.push_back(&(autoload_cache.back()->get()));
  348. }
  349. TreeItem *item = tree->create_item(root);
  350. item->set_text(0, name);
  351. item->set_editable(0, true);
  352. item->set_text(1, path);
  353. item->set_selectable(1, true);
  354. item->set_cell_mode(2, TreeItem::CELL_MODE_CHECK);
  355. item->set_editable(2, true);
  356. item->set_text(2, TTR("Enable"));
  357. item->set_checked(2, info.is_singleton);
  358. item->add_button(3, get_icon("Load", "EditorIcons"), BUTTON_OPEN);
  359. item->add_button(3, get_icon("MoveUp", "EditorIcons"), BUTTON_MOVE_UP);
  360. item->add_button(3, get_icon("MoveDown", "EditorIcons"), BUTTON_MOVE_DOWN);
  361. item->add_button(3, get_icon("Remove", "EditorIcons"), BUTTON_DELETE);
  362. item->set_selectable(3, false);
  363. }
  364. // Remove deleted/changed autoloads
  365. for (Map<String, AutoLoadInfo>::Element *E = to_remove.front(); E; E = E->next()) {
  366. AutoLoadInfo &info = E->get();
  367. if (info.is_singleton) {
  368. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  369. ScriptServer::get_language(i)->remove_named_global_constant(info.name);
  370. }
  371. }
  372. if (info.in_editor) {
  373. ERR_CONTINUE(!info.node);
  374. get_tree()->get_root()->call_deferred("remove_child", info.node);
  375. }
  376. if (info.node) {
  377. info.node->queue_delete();
  378. info.node = nullptr;
  379. }
  380. }
  381. // Load new/changed autoloads
  382. List<Node *> nodes_to_add;
  383. for (List<AutoLoadInfo *>::Element *E = to_add.front(); E; E = E->next()) {
  384. AutoLoadInfo *info = E->get();
  385. info->node = _create_autoload(info->path);
  386. ERR_CONTINUE(!info->node);
  387. info->node->set_name(info->name);
  388. Ref<Script> scr = info->node->get_script();
  389. info->in_editor = scr.is_valid() && scr->is_tool();
  390. if (info->in_editor) {
  391. //defer so references are all valid on _ready()
  392. nodes_to_add.push_back(info->node);
  393. }
  394. if (info->is_singleton) {
  395. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  396. ScriptServer::get_language(i)->add_named_global_constant(info->name, info->node);
  397. }
  398. }
  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. PoolStringArray 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. PoolStringArray 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_add", &EditorAutoloadSettings::_autoload_add);
  567. ClassDB::bind_method("_autoload_selected", &EditorAutoloadSettings::_autoload_selected);
  568. ClassDB::bind_method("_autoload_edited", &EditorAutoloadSettings::_autoload_edited);
  569. ClassDB::bind_method("_autoload_button_pressed", &EditorAutoloadSettings::_autoload_button_pressed);
  570. ClassDB::bind_method("_autoload_activated", &EditorAutoloadSettings::_autoload_activated);
  571. ClassDB::bind_method("_autoload_path_text_changed", &EditorAutoloadSettings::_autoload_path_text_changed);
  572. ClassDB::bind_method("_autoload_text_entered", &EditorAutoloadSettings::_autoload_text_entered);
  573. ClassDB::bind_method("_autoload_text_changed", &EditorAutoloadSettings::_autoload_text_changed);
  574. ClassDB::bind_method("_autoload_open", &EditorAutoloadSettings::_autoload_open);
  575. ClassDB::bind_method("_autoload_file_callback", &EditorAutoloadSettings::_autoload_file_callback);
  576. ClassDB::bind_method("get_drag_data_fw", &EditorAutoloadSettings::get_drag_data_fw);
  577. ClassDB::bind_method("can_drop_data_fw", &EditorAutoloadSettings::can_drop_data_fw);
  578. ClassDB::bind_method("drop_data_fw", &EditorAutoloadSettings::drop_data_fw);
  579. ClassDB::bind_method("update_autoload", &EditorAutoloadSettings::update_autoload);
  580. ClassDB::bind_method("autoload_add", &EditorAutoloadSettings::autoload_add);
  581. ClassDB::bind_method("autoload_remove", &EditorAutoloadSettings::autoload_remove);
  582. ADD_SIGNAL(MethodInfo("autoload_changed"));
  583. }
  584. EditorAutoloadSettings::EditorAutoloadSettings() {
  585. // Make first cache
  586. List<PropertyInfo> props;
  587. ProjectSettings::get_singleton()->get_property_list(&props);
  588. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  589. const PropertyInfo &pi = E->get();
  590. if (!pi.name.begins_with("autoload/")) {
  591. continue;
  592. }
  593. String name = pi.name.get_slice("/", 1);
  594. String path = ProjectSettings::get_singleton()->get(pi.name);
  595. if (name.empty()) {
  596. continue;
  597. }
  598. AutoLoadInfo info;
  599. info.is_singleton = path.begins_with("*");
  600. if (info.is_singleton) {
  601. path = path.substr(1, path.length());
  602. }
  603. info.name = name;
  604. info.path = path;
  605. info.order = ProjectSettings::get_singleton()->get_order(pi.name);
  606. if (info.is_singleton) {
  607. // Make sure name references work before parsing scripts
  608. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  609. ScriptServer::get_language(i)->add_named_global_constant(info.name, Variant());
  610. }
  611. }
  612. autoload_cache.push_back(info);
  613. }
  614. for (List<AutoLoadInfo>::Element *E = autoload_cache.front(); E; E = E->next()) {
  615. AutoLoadInfo &info = E->get();
  616. info.node = _create_autoload(info.path);
  617. if (info.node) {
  618. Ref<Script> scr = info.node->get_script();
  619. info.in_editor = scr.is_valid() && scr->is_tool();
  620. info.node->set_name(info.name);
  621. }
  622. if (info.is_singleton) {
  623. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  624. ScriptServer::get_language(i)->add_named_global_constant(info.name, info.node);
  625. }
  626. }
  627. if (!info.is_singleton && !info.in_editor && info.node != nullptr) {
  628. memdelete(info.node);
  629. info.node = nullptr;
  630. }
  631. }
  632. autoload_changed = "autoload_changed";
  633. updating_autoload = false;
  634. selected_autoload = "";
  635. HBoxContainer *hbc = memnew(HBoxContainer);
  636. add_child(hbc);
  637. error_message = memnew(Label);
  638. error_message->hide();
  639. error_message->set_align(Label::Align::ALIGN_RIGHT);
  640. error_message->add_color_override("font_color", EditorNode::get_singleton()->get_gui_base()->get_color("error_color", "Editor"));
  641. add_child(error_message);
  642. Label *l = memnew(Label);
  643. l->set_text(TTR("Path:"));
  644. hbc->add_child(l);
  645. autoload_add_path = memnew(EditorLineEditFileChooser);
  646. autoload_add_path->set_h_size_flags(SIZE_EXPAND_FILL);
  647. autoload_add_path->get_file_dialog()->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  648. autoload_add_path->get_file_dialog()->connect("file_selected", this, "_autoload_file_callback");
  649. autoload_add_path->get_line_edit()->connect("text_changed", this, "_autoload_path_text_changed");
  650. hbc->add_child(autoload_add_path);
  651. l = memnew(Label);
  652. l->set_text(TTR("Node Name:"));
  653. hbc->add_child(l);
  654. autoload_add_name = memnew(LineEdit);
  655. autoload_add_name->set_h_size_flags(SIZE_EXPAND_FILL);
  656. autoload_add_name->connect("text_entered", this, "_autoload_text_entered");
  657. autoload_add_name->connect("text_changed", this, "_autoload_text_changed");
  658. hbc->add_child(autoload_add_name);
  659. add_autoload = memnew(Button);
  660. add_autoload->set_text(TTR("Add"));
  661. add_autoload->connect("pressed", this, "_autoload_add");
  662. // The button will be enabled once a valid name is entered (either automatically or manually).
  663. add_autoload->set_disabled(true);
  664. hbc->add_child(add_autoload);
  665. tree = memnew(Tree);
  666. tree->set_hide_root(true);
  667. tree->set_select_mode(Tree::SELECT_MULTI);
  668. tree->set_allow_reselect(true);
  669. tree->set_drag_forwarding(this);
  670. tree->set_columns(4);
  671. tree->set_column_titles_visible(true);
  672. tree->set_column_title(0, TTR("Name"));
  673. tree->set_column_expand(0, true);
  674. tree->set_column_min_width(0, 100 * EDSCALE);
  675. tree->set_column_title(1, TTR("Path"));
  676. tree->set_column_expand(1, true);
  677. tree->set_column_min_width(1, 100 * EDSCALE);
  678. tree->set_column_title(2, TTR("Global Variable"));
  679. tree->set_column_expand(2, false);
  680. // Reserve enough space for translations of "Global Variable" which may be longer.
  681. tree->set_column_min_width(2, 150 * EDSCALE);
  682. tree->set_column_expand(3, false);
  683. tree->set_column_min_width(3, 120 * EDSCALE);
  684. tree->connect("cell_selected", this, "_autoload_selected");
  685. tree->connect("item_edited", this, "_autoload_edited");
  686. tree->connect("button_pressed", this, "_autoload_button_pressed");
  687. tree->connect("item_activated", this, "_autoload_activated");
  688. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  689. add_child(tree, true);
  690. }
  691. EditorAutoloadSettings::~EditorAutoloadSettings() {
  692. for (List<AutoLoadInfo>::Element *E = autoload_cache.front(); E; E = E->next()) {
  693. AutoLoadInfo &info = E->get();
  694. if (info.node && !info.in_editor) {
  695. memdelete(info.node);
  696. }
  697. }
  698. }