editor_autoload_settings.cpp 26 KB

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