editor_autoload_settings.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "editor_node.h"
  32. #include "global_constants.h"
  33. #include "project_settings.h"
  34. #define PREVIEW_LIST_MAX_SIZE 10
  35. void EditorAutoloadSettings::_notification(int p_what) {
  36. if (p_what == NOTIFICATION_ENTER_TREE) {
  37. List<String> afn;
  38. ResourceLoader::get_recognized_extensions_for_type("Script", &afn);
  39. ResourceLoader::get_recognized_extensions_for_type("PackedScene", &afn);
  40. EditorFileDialog *file_dialog = autoload_add_path->get_file_dialog();
  41. for (List<String>::Element *E = afn.front(); E; E = E->next()) {
  42. file_dialog->add_filter("*." + E->get());
  43. }
  44. }
  45. }
  46. bool EditorAutoloadSettings::_autoload_name_is_valid(const String &p_name, String *r_error) {
  47. if (!p_name.is_valid_identifier()) {
  48. if (r_error)
  49. *r_error = TTR("Invalid name.") + "\n" + TTR("Valid characters:") + " a-z, A-Z, 0-9 or _";
  50. return false;
  51. }
  52. if (ClassDB::class_exists(p_name)) {
  53. if (r_error)
  54. *r_error = TTR("Invalid name. Must not collide with an existing engine class name.");
  55. return false;
  56. }
  57. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  58. if (Variant::get_type_name(Variant::Type(i)) == p_name) {
  59. if (r_error)
  60. *r_error = TTR("Invalid name. Must not collide with an existing buit-in type name.");
  61. return false;
  62. }
  63. }
  64. for (int i = 0; i < GlobalConstants::get_global_constant_count(); i++) {
  65. if (GlobalConstants::get_global_constant_name(i) == p_name) {
  66. if (r_error)
  67. *r_error = TTR("Invalid name. Must not collide with an existing global constant name.");
  68. return false;
  69. }
  70. }
  71. return true;
  72. }
  73. void EditorAutoloadSettings::_autoload_add() {
  74. String name = autoload_add_name->get_text();
  75. String error;
  76. if (!_autoload_name_is_valid(name, &error)) {
  77. EditorNode::get_singleton()->show_warning(error);
  78. return;
  79. }
  80. String path = autoload_add_path->get_line_edit()->get_text();
  81. if (!FileAccess::exists(path)) {
  82. EditorNode::get_singleton()->show_warning(TTR("Invalid Path.") + "\n" + TTR("File does not exist."));
  83. return;
  84. }
  85. if (!path.begins_with("res://")) {
  86. EditorNode::get_singleton()->show_warning(TTR("Invalid Path.") + "\n" + TTR("Not in resource path."));
  87. return;
  88. }
  89. name = "autoload/" + name;
  90. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  91. undo_redo->create_action(TTR("Add AutoLoad"));
  92. undo_redo->add_do_property(ProjectSettings::get_singleton(), name, "*" + path);
  93. if (ProjectSettings::get_singleton()->has_setting(name)) {
  94. undo_redo->add_undo_property(ProjectSettings::get_singleton(), name, ProjectSettings::get_singleton()->get(name));
  95. } else {
  96. undo_redo->add_undo_property(ProjectSettings::get_singleton(), name, Variant());
  97. }
  98. undo_redo->add_do_method(this, "update_autoload");
  99. undo_redo->add_undo_method(this, "update_autoload");
  100. undo_redo->add_do_method(this, "emit_signal", autoload_changed);
  101. undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
  102. undo_redo->commit_action();
  103. autoload_add_path->get_line_edit()->set_text("");
  104. autoload_add_name->set_text("");
  105. }
  106. void EditorAutoloadSettings::_autoload_selected() {
  107. TreeItem *ti = tree->get_selected();
  108. if (!ti)
  109. return;
  110. selected_autoload = "autoload/" + ti->get_text(0);
  111. }
  112. void EditorAutoloadSettings::_autoload_edited() {
  113. if (updating_autoload)
  114. return;
  115. TreeItem *ti = tree->get_edited();
  116. int column = tree->get_edited_column();
  117. UndoRedo *undo_redo = EditorNode::get_undo_redo();
  118. if (column == 0) {
  119. String name = ti->get_text(0);
  120. String old_name = selected_autoload.get_slice("/", 1);
  121. if (name == old_name)
  122. return;
  123. String error;
  124. if (!_autoload_name_is_valid(name, &error)) {
  125. ti->set_text(0, old_name);
  126. EditorNode::get_singleton()->show_warning(error);
  127. return;
  128. }
  129. if (ProjectSettings::get_singleton()->has_setting("autoload/" + name)) {
  130. ti->set_text(0, old_name);
  131. EditorNode::get_singleton()->show_warning(vformat(TTR("Autoload '%s' already exists!"), name));
  132. return;
  133. }
  134. updating_autoload = true;
  135. name = "autoload/" + name;
  136. int order = ProjectSettings::get_singleton()->get_order(selected_autoload);
  137. String path = ProjectSettings::get_singleton()->get(selected_autoload);
  138. undo_redo->create_action(TTR("Rename Autoload"));
  139. undo_redo->add_do_property(ProjectSettings::get_singleton(), name, path);
  140. undo_redo->add_do_method(ProjectSettings::get_singleton(), "set_order", name, order);
  141. undo_redo->add_do_method(ProjectSettings::get_singleton(), "clear", selected_autoload);
  142. undo_redo->add_undo_property(ProjectSettings::get_singleton(), selected_autoload, path);
  143. undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", selected_autoload, order);
  144. undo_redo->add_undo_method(ProjectSettings::get_singleton(), "clear", name);
  145. undo_redo->add_do_method(this, "update_autoload");
  146. undo_redo->add_undo_method(this, "update_autoload");
  147. undo_redo->add_do_method(this, "emit_signal", autoload_changed);
  148. undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
  149. undo_redo->commit_action();
  150. selected_autoload = name;
  151. } else if (column == 2) {
  152. updating_autoload = true;
  153. bool checked = ti->is_checked(2);
  154. String base = "autoload/" + ti->get_text(0);
  155. int order = ProjectSettings::get_singleton()->get_order(base);
  156. String path = ProjectSettings::get_singleton()->get(base);
  157. if (path.begins_with("*"))
  158. path = path.substr(1, path.length());
  159. if (checked)
  160. path = "*" + path;
  161. undo_redo->create_action(TTR("Toggle AutoLoad Globals"));
  162. undo_redo->add_do_property(ProjectSettings::get_singleton(), base, path);
  163. undo_redo->add_undo_property(ProjectSettings::get_singleton(), base, ProjectSettings::get_singleton()->get(base));
  164. undo_redo->add_do_method(ProjectSettings::get_singleton(), "set_order", base, order);
  165. undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", base, order);
  166. undo_redo->add_do_method(this, "update_autoload");
  167. undo_redo->add_undo_method(this, "update_autoload");
  168. undo_redo->add_do_method(this, "emit_signal", autoload_changed);
  169. undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
  170. undo_redo->commit_action();
  171. }
  172. updating_autoload = false;
  173. }
  174. void EditorAutoloadSettings::_autoload_button_pressed(Object *p_item, int p_column, int p_button) {
  175. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  176. String name = "autoload/" + ti->get_text(0);
  177. UndoRedo *undo_redo = EditorNode::get_undo_redo();
  178. switch (p_button) {
  179. case BUTTON_MOVE_UP:
  180. case BUTTON_MOVE_DOWN: {
  181. TreeItem *swap = NULL;
  182. if (p_button == BUTTON_MOVE_UP) {
  183. swap = ti->get_prev();
  184. } else {
  185. swap = ti->get_next();
  186. }
  187. if (!swap)
  188. return;
  189. String swap_name = "autoload/" + swap->get_text(0);
  190. int order = ProjectSettings::get_singleton()->get_order(name);
  191. int swap_order = ProjectSettings::get_singleton()->get_order(swap_name);
  192. undo_redo->create_action(TTR("Move Autoload"));
  193. undo_redo->add_do_method(ProjectSettings::get_singleton(), "set_order", name, swap_order);
  194. undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", name, order);
  195. undo_redo->add_do_method(ProjectSettings::get_singleton(), "set_order", swap_name, order);
  196. undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", swap_name, swap_order);
  197. undo_redo->add_do_method(this, "update_autoload");
  198. undo_redo->add_undo_method(this, "update_autoload");
  199. undo_redo->add_do_method(this, "emit_signal", autoload_changed);
  200. undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
  201. undo_redo->commit_action();
  202. } break;
  203. case BUTTON_DELETE: {
  204. int order = ProjectSettings::get_singleton()->get_order(name);
  205. undo_redo->create_action(TTR("Remove Autoload"));
  206. undo_redo->add_do_property(ProjectSettings::get_singleton(), name, Variant());
  207. undo_redo->add_undo_property(ProjectSettings::get_singleton(), name, ProjectSettings::get_singleton()->get(name));
  208. undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_persisting", name, true);
  209. undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", order);
  210. undo_redo->add_do_method(this, "update_autoload");
  211. undo_redo->add_undo_method(this, "update_autoload");
  212. undo_redo->add_do_method(this, "emit_signal", autoload_changed);
  213. undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
  214. undo_redo->commit_action();
  215. } break;
  216. }
  217. }
  218. void EditorAutoloadSettings::_autoload_file_callback(const String &p_path) {
  219. autoload_add_name->set_text(p_path.get_file().get_basename());
  220. }
  221. void EditorAutoloadSettings::update_autoload() {
  222. if (updating_autoload)
  223. return;
  224. updating_autoload = true;
  225. autoload_cache.clear();
  226. tree->clear();
  227. TreeItem *root = tree->create_item();
  228. List<PropertyInfo> props;
  229. ProjectSettings::get_singleton()->get_property_list(&props);
  230. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  231. const PropertyInfo &pi = E->get();
  232. if (!pi.name.begins_with("autoload/"))
  233. continue;
  234. String name = pi.name.get_slice("/", 1);
  235. String path = ProjectSettings::get_singleton()->get(pi.name);
  236. if (name.empty())
  237. continue;
  238. AutoLoadInfo info;
  239. info.name = pi.name;
  240. info.order = ProjectSettings::get_singleton()->get_order(pi.name);
  241. autoload_cache.push_back(info);
  242. bool global = false;
  243. if (path.begins_with("*")) {
  244. global = true;
  245. path = path.substr(1, path.length());
  246. }
  247. TreeItem *item = tree->create_item(root);
  248. item->set_text(0, name);
  249. item->set_editable(0, true);
  250. item->set_text(1, path);
  251. item->set_selectable(1, false);
  252. item->set_cell_mode(2, TreeItem::CELL_MODE_CHECK);
  253. item->set_editable(2, true);
  254. item->set_text(2, TTR("Enable"));
  255. item->set_checked(2, global);
  256. item->add_button(3, get_icon("MoveUp", "EditorIcons"), BUTTON_MOVE_UP);
  257. item->add_button(3, get_icon("MoveDown", "EditorIcons"), BUTTON_MOVE_DOWN);
  258. item->add_button(3, get_icon("Remove", "EditorIcons"), BUTTON_DELETE);
  259. item->set_selectable(3, false);
  260. }
  261. updating_autoload = false;
  262. }
  263. Variant EditorAutoloadSettings::get_drag_data_fw(const Point2 &p_point, Control *p_control) {
  264. if (autoload_cache.size() <= 1)
  265. return false;
  266. PoolStringArray autoloads;
  267. TreeItem *next = tree->get_next_selected(NULL);
  268. while (next) {
  269. autoloads.push_back(next->get_text(0));
  270. next = tree->get_next_selected(next);
  271. }
  272. if (autoloads.size() == 0 || autoloads.size() == autoload_cache.size())
  273. return Variant();
  274. VBoxContainer *preview = memnew(VBoxContainer);
  275. int max_size = MIN(PREVIEW_LIST_MAX_SIZE, autoloads.size());
  276. for (int i = 0; i < max_size; i++) {
  277. Label *label = memnew(Label(autoloads[i]));
  278. label->set_self_modulate(Color(1, 1, 1, Math::lerp(1, 0, float(i) / PREVIEW_LIST_MAX_SIZE)));
  279. preview->add_child(label);
  280. }
  281. tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN);
  282. tree->set_drag_preview(preview);
  283. Dictionary drop_data;
  284. drop_data["type"] = "autoload";
  285. drop_data["autoloads"] = autoloads;
  286. return drop_data;
  287. }
  288. bool EditorAutoloadSettings::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_control) const {
  289. if (updating_autoload)
  290. return false;
  291. Dictionary drop_data = p_data;
  292. if (!drop_data.has("type"))
  293. return false;
  294. if (drop_data.has("type")) {
  295. TreeItem *ti = tree->get_item_at_position(p_point);
  296. if (!ti)
  297. return false;
  298. int section = tree->get_drop_section_at_position(p_point);
  299. if (section < -1)
  300. return false;
  301. return true;
  302. }
  303. return false;
  304. }
  305. void EditorAutoloadSettings::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_control) {
  306. TreeItem *ti = tree->get_item_at_position(p_point);
  307. if (!ti)
  308. return;
  309. int section = tree->get_drop_section_at_position(p_point);
  310. if (section < -1)
  311. return;
  312. String name;
  313. bool move_to_back = false;
  314. if (section < 0) {
  315. name = ti->get_text(0);
  316. } else if (ti->get_next()) {
  317. name = ti->get_next()->get_text(0);
  318. } else {
  319. name = ti->get_text(0);
  320. move_to_back = true;
  321. }
  322. int order = ProjectSettings::get_singleton()->get_order("autoload/" + name);
  323. AutoLoadInfo aux;
  324. List<AutoLoadInfo>::Element *E = NULL;
  325. if (!move_to_back) {
  326. aux.order = order;
  327. E = autoload_cache.find(aux);
  328. }
  329. Dictionary drop_data = p_data;
  330. PoolStringArray autoloads = drop_data["autoloads"];
  331. Vector<int> orders;
  332. orders.resize(autoload_cache.size());
  333. for (int i = 0; i < autoloads.size(); i++) {
  334. aux.order = ProjectSettings::get_singleton()->get_order("autoload/" + autoloads[i]);
  335. List<AutoLoadInfo>::Element *I = autoload_cache.find(aux);
  336. if (move_to_back) {
  337. autoload_cache.move_to_back(I);
  338. } else if (E != I) {
  339. autoload_cache.move_before(I, E);
  340. } else if (E->next()) {
  341. E = E->next();
  342. } else {
  343. break;
  344. }
  345. }
  346. int i = 0;
  347. for (List<AutoLoadInfo>::Element *E = autoload_cache.front(); E; E = E->next()) {
  348. orders[i++] = E->get().order;
  349. }
  350. orders.sort();
  351. UndoRedo *undo_redo = EditorNode::get_undo_redo();
  352. undo_redo->create_action(TTR("Rearrange Autoloads"));
  353. i = 0;
  354. for (List<AutoLoadInfo>::Element *E = autoload_cache.front(); E; E = E->next()) {
  355. undo_redo->add_do_method(ProjectSettings::get_singleton(), "set_order", E->get().name, orders[i++]);
  356. undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", E->get().name, E->get().order);
  357. }
  358. orders.clear();
  359. undo_redo->add_do_method(this, "update_autoload");
  360. undo_redo->add_undo_method(this, "update_autoload");
  361. undo_redo->add_do_method(this, "emit_signal", autoload_changed);
  362. undo_redo->add_undo_method(this, "emit_signal", autoload_changed);
  363. undo_redo->commit_action();
  364. }
  365. void EditorAutoloadSettings::_bind_methods() {
  366. ClassDB::bind_method("_autoload_add", &EditorAutoloadSettings::_autoload_add);
  367. ClassDB::bind_method("_autoload_selected", &EditorAutoloadSettings::_autoload_selected);
  368. ClassDB::bind_method("_autoload_edited", &EditorAutoloadSettings::_autoload_edited);
  369. ClassDB::bind_method("_autoload_button_pressed", &EditorAutoloadSettings::_autoload_button_pressed);
  370. ClassDB::bind_method("_autoload_file_callback", &EditorAutoloadSettings::_autoload_file_callback);
  371. ClassDB::bind_method("get_drag_data_fw", &EditorAutoloadSettings::get_drag_data_fw);
  372. ClassDB::bind_method("can_drop_data_fw", &EditorAutoloadSettings::can_drop_data_fw);
  373. ClassDB::bind_method("drop_data_fw", &EditorAutoloadSettings::drop_data_fw);
  374. ClassDB::bind_method("update_autoload", &EditorAutoloadSettings::update_autoload);
  375. ADD_SIGNAL(MethodInfo("autoload_changed"));
  376. }
  377. EditorAutoloadSettings::EditorAutoloadSettings() {
  378. autoload_changed = "autoload_changed";
  379. updating_autoload = false;
  380. selected_autoload = "";
  381. HBoxContainer *hbc = memnew(HBoxContainer);
  382. add_child(hbc);
  383. VBoxContainer *vbc_path = memnew(VBoxContainer);
  384. vbc_path->set_h_size_flags(SIZE_EXPAND_FILL);
  385. autoload_add_path = memnew(EditorLineEditFileChooser);
  386. autoload_add_path->set_h_size_flags(SIZE_EXPAND_FILL);
  387. autoload_add_path->get_file_dialog()->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  388. autoload_add_path->get_file_dialog()->connect("file_selected", this, "_autoload_file_callback");
  389. vbc_path->add_margin_child(TTR("Path:"), autoload_add_path);
  390. hbc->add_child(vbc_path);
  391. VBoxContainer *vbc_name = memnew(VBoxContainer);
  392. vbc_name->set_h_size_flags(SIZE_EXPAND_FILL);
  393. HBoxContainer *hbc_name = memnew(HBoxContainer);
  394. autoload_add_name = memnew(LineEdit);
  395. autoload_add_name->set_h_size_flags(SIZE_EXPAND_FILL);
  396. hbc_name->add_child(autoload_add_name);
  397. Button *add_autoload = memnew(Button);
  398. add_autoload->set_text(TTR("Add"));
  399. hbc_name->add_child(add_autoload);
  400. add_autoload->connect("pressed", this, "_autoload_add");
  401. vbc_name->add_margin_child(TTR("Node Name:"), hbc_name);
  402. hbc->add_child(vbc_name);
  403. tree = memnew(Tree);
  404. tree->set_hide_root(true);
  405. tree->set_select_mode(Tree::SELECT_MULTI);
  406. tree->set_allow_reselect(true);
  407. tree->set_drag_forwarding(this);
  408. tree->set_columns(4);
  409. tree->set_column_titles_visible(true);
  410. tree->set_column_title(0, TTR("Name"));
  411. tree->set_column_expand(0, true);
  412. tree->set_column_min_width(0, 100);
  413. tree->set_column_title(1, TTR("Path"));
  414. tree->set_column_expand(1, true);
  415. tree->set_column_min_width(1, 100);
  416. tree->set_column_title(2, TTR("Singleton"));
  417. tree->set_column_expand(2, false);
  418. tree->set_column_min_width(2, 80);
  419. tree->set_column_expand(3, false);
  420. tree->set_column_min_width(3, 80);
  421. tree->connect("cell_selected", this, "_autoload_selected");
  422. tree->connect("item_edited", this, "_autoload_edited");
  423. tree->connect("button_pressed", this, "_autoload_button_pressed");
  424. add_margin_child(TTR("List:"), tree, true);
  425. }