editor_autoload_settings.cpp 18 KB

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