resource_preloader_editor_plugin.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*************************************************************************/
  2. /* resource_preloader_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "resource_preloader_editor_plugin.h"
  31. #include "editor/editor_settings.h"
  32. #include "globals.h"
  33. #include "io/resource_loader.h"
  34. #include "scene/resources/scene_preloader.h"
  35. void ResourcePreloaderEditor::_input_event(InputEvent p_event) {
  36. }
  37. void ResourcePreloaderEditor::_notification(int p_what) {
  38. if (p_what == NOTIFICATION_FIXED_PROCESS) {
  39. }
  40. if (p_what == NOTIFICATION_ENTER_TREE) {
  41. load->set_icon(get_icon("Folder", "EditorIcons"));
  42. _delete->set_icon(get_icon("Del", "EditorIcons"));
  43. }
  44. if (p_what == NOTIFICATION_READY) {
  45. // NodePath("/root")->connect("node_removed", this,"_node_removed",Vector<Variant>(),true);
  46. }
  47. if (p_what == NOTIFICATION_DRAW) {
  48. }
  49. }
  50. void ResourcePreloaderEditor::_files_load_request(const Vector<String> &p_paths) {
  51. for (int i = 0; i < p_paths.size(); i++) {
  52. String path = p_paths[i];
  53. RES resource;
  54. resource = ResourceLoader::load(path);
  55. if (resource.is_null()) {
  56. dialog->set_text(TTR("ERROR: Couldn't load resource!"));
  57. dialog->set_title(TTR("Error!"));
  58. //dialog->get_cancel()->set_text("Close");
  59. dialog->get_ok()->set_text(TTR("Close"));
  60. dialog->popup_centered_minsize();
  61. return; ///beh should show an error i guess
  62. }
  63. String basename = path.get_file().basename();
  64. String name = basename;
  65. int counter = 1;
  66. while (preloader->has_resource(name)) {
  67. counter++;
  68. name = basename + " " + itos(counter);
  69. }
  70. undo_redo->create_action(TTR("Add Resource"));
  71. undo_redo->add_do_method(preloader, "add_resource", name, resource);
  72. undo_redo->add_undo_method(preloader, "remove_resource", name);
  73. undo_redo->add_do_method(this, "_update_library");
  74. undo_redo->add_undo_method(this, "_update_library");
  75. undo_redo->commit_action();
  76. }
  77. }
  78. void ResourcePreloaderEditor::_load_pressed() {
  79. loading_scene = false;
  80. file->clear_filters();
  81. List<String> extensions;
  82. ResourceLoader::get_recognized_extensions_for_type("", &extensions);
  83. for (int i = 0; i < extensions.size(); i++)
  84. file->add_filter("*." + extensions[i]);
  85. file->set_mode(EditorFileDialog::MODE_OPEN_FILES);
  86. file->popup_centered_ratio();
  87. }
  88. void ResourcePreloaderEditor::_item_edited() {
  89. if (!tree->get_selected())
  90. return;
  91. TreeItem *s = tree->get_selected();
  92. if (tree->get_selected_column() == 0) {
  93. // renamed
  94. String old_name = s->get_metadata(0);
  95. String new_name = s->get_text(0);
  96. if (old_name == new_name)
  97. return;
  98. if (new_name == "" || new_name.find("\\") != -1 || new_name.find("/") != -1 || preloader->has_resource(new_name)) {
  99. s->set_text(0, old_name);
  100. return;
  101. }
  102. RES samp = preloader->get_resource(old_name);
  103. undo_redo->create_action(TTR("Rename Resource"));
  104. undo_redo->add_do_method(preloader, "remove_resource", old_name);
  105. undo_redo->add_do_method(preloader, "add_resource", new_name, samp);
  106. undo_redo->add_undo_method(preloader, "remove_resource", new_name);
  107. undo_redo->add_undo_method(preloader, "add_resource", old_name, samp);
  108. undo_redo->add_do_method(this, "_update_library");
  109. undo_redo->add_undo_method(this, "_update_library");
  110. undo_redo->commit_action();
  111. }
  112. }
  113. void ResourcePreloaderEditor::_delete_confirm_pressed() {
  114. if (!tree->get_selected())
  115. return;
  116. String to_remove = tree->get_selected()->get_text(0);
  117. undo_redo->create_action(TTR("Delete Resource"));
  118. undo_redo->add_do_method(preloader, "remove_resource", to_remove);
  119. undo_redo->add_undo_method(preloader, "add_resource", to_remove, preloader->get_resource(to_remove));
  120. undo_redo->add_do_method(this, "_update_library");
  121. undo_redo->add_undo_method(this, "_update_library");
  122. undo_redo->commit_action();
  123. }
  124. void ResourcePreloaderEditor::_paste_pressed() {
  125. RES r = EditorSettings::get_singleton()->get_resource_clipboard();
  126. if (!r.is_valid()) {
  127. dialog->set_text(TTR("Resource clipboard is empty!"));
  128. dialog->set_title(TTR("Error!"));
  129. //dialog->get_cancel()->set_text("Close");
  130. dialog->get_ok()->set_text(TTR("Close"));
  131. dialog->popup_centered_minsize();
  132. return; ///beh should show an error i guess
  133. }
  134. String name = r->get_name();
  135. if (name == "")
  136. name = r->get_path().get_file();
  137. if (name == "")
  138. name = r->get_type();
  139. String basename = name;
  140. int counter = 1;
  141. while (preloader->has_resource(name)) {
  142. counter++;
  143. name = basename + " " + itos(counter);
  144. }
  145. undo_redo->create_action(TTR("Paste Resource"));
  146. undo_redo->add_do_method(preloader, "add_resource", name, r);
  147. undo_redo->add_undo_method(preloader, "remove_resource", name);
  148. undo_redo->add_do_method(this, "_update_library");
  149. undo_redo->add_undo_method(this, "_update_library");
  150. undo_redo->commit_action();
  151. }
  152. void ResourcePreloaderEditor::_delete_pressed() {
  153. if (!tree->get_selected())
  154. return;
  155. _delete_confirm_pressed(); //it has undo.. why bother with a dialog..
  156. /*
  157. dialog->set_title("Confirm...");
  158. dialog->set_text("Remove Resource '"+tree->get_selected()->get_text(0)+"' ?");
  159. //dialog->get_cancel()->set_text("Cancel");
  160. //dialog->get_ok()->show();
  161. dialog->get_ok()->set_text("Remove");
  162. dialog->popup_centered(Size2(300,60));*/
  163. }
  164. void ResourcePreloaderEditor::_update_library() {
  165. tree->clear();
  166. tree->set_hide_root(true);
  167. TreeItem *root = tree->create_item(NULL);
  168. List<StringName> rnames;
  169. preloader->get_resource_list(&rnames);
  170. List<String> names;
  171. for (List<StringName>::Element *E = rnames.front(); E; E = E->next()) {
  172. names.push_back(E->get());
  173. }
  174. names.sort();
  175. for (List<String>::Element *E = names.front(); E; E = E->next()) {
  176. TreeItem *ti = tree->create_item(root);
  177. ti->set_cell_mode(0, TreeItem::CELL_MODE_STRING);
  178. ti->set_editable(0, true);
  179. ti->set_selectable(0, true);
  180. ti->set_text(0, E->get());
  181. ti->set_metadata(0, E->get());
  182. RES r = preloader->get_resource(E->get());
  183. ERR_CONTINUE(r.is_null());
  184. ti->set_tooltip(0, r->get_path());
  185. String type = r->get_type();
  186. ti->set_text(1, type);
  187. ti->set_selectable(1, false);
  188. if (has_icon(type, "EditorIcons"))
  189. ti->set_icon(1, get_icon(type, "EditorIcons"));
  190. }
  191. //player->add_resource("default",resource);
  192. }
  193. void ResourcePreloaderEditor::edit(ResourcePreloader *p_preloader) {
  194. preloader = p_preloader;
  195. if (p_preloader) {
  196. _update_library();
  197. } else {
  198. hide();
  199. set_fixed_process(false);
  200. }
  201. }
  202. Variant ResourcePreloaderEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  203. TreeItem *ti = tree->get_item_at_pos(p_point);
  204. if (!ti)
  205. return Variant();
  206. String name = ti->get_metadata(0);
  207. RES res = preloader->get_resource(name);
  208. if (!res.is_valid())
  209. return Variant();
  210. return EditorNode::get_singleton()->drag_resource(res, p_from);
  211. }
  212. bool ResourcePreloaderEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  213. Dictionary d = p_data;
  214. if (!d.has("type"))
  215. return false;
  216. if (d.has("from") && (Object *)(d["from"]) == tree)
  217. return false;
  218. if (String(d["type"]) == "resource" && d.has("resource")) {
  219. RES r = d["resource"];
  220. return r.is_valid();
  221. }
  222. if (String(d["type"]) == "files") {
  223. Vector<String> files = d["files"];
  224. if (files.size() == 0)
  225. return false;
  226. return true;
  227. }
  228. return false;
  229. }
  230. void ResourcePreloaderEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  231. if (!can_drop_data_fw(p_point, p_data, p_from))
  232. return;
  233. Dictionary d = p_data;
  234. if (!d.has("type"))
  235. return;
  236. if (String(d["type"]) == "resource" && d.has("resource")) {
  237. RES r = d["resource"];
  238. if (r.is_valid()) {
  239. String basename;
  240. if (r->get_name() != "") {
  241. basename = r->get_name();
  242. } else if (r->get_path().is_resource_file()) {
  243. basename = r->get_path().basename();
  244. } else {
  245. basename = "Resource";
  246. }
  247. String name = basename;
  248. int counter = 0;
  249. while (preloader->has_resource(name)) {
  250. counter++;
  251. name = basename + "_" + itos(counter);
  252. }
  253. undo_redo->create_action(TTR("Add Resource"));
  254. undo_redo->add_do_method(preloader, "add_resource", name, r);
  255. undo_redo->add_undo_method(preloader, "remove_resource", name);
  256. undo_redo->add_do_method(this, "_update_library");
  257. undo_redo->add_undo_method(this, "_update_library");
  258. undo_redo->commit_action();
  259. }
  260. }
  261. if (String(d["type"]) == "files") {
  262. Vector<String> files = d["files"];
  263. _files_load_request(files);
  264. }
  265. }
  266. void ResourcePreloaderEditor::_bind_methods() {
  267. ObjectTypeDB::bind_method(_MD("_input_event"), &ResourcePreloaderEditor::_input_event);
  268. ObjectTypeDB::bind_method(_MD("_load_pressed"), &ResourcePreloaderEditor::_load_pressed);
  269. ObjectTypeDB::bind_method(_MD("_item_edited"), &ResourcePreloaderEditor::_item_edited);
  270. ObjectTypeDB::bind_method(_MD("_delete_pressed"), &ResourcePreloaderEditor::_delete_pressed);
  271. ObjectTypeDB::bind_method(_MD("_paste_pressed"), &ResourcePreloaderEditor::_paste_pressed);
  272. ObjectTypeDB::bind_method(_MD("_delete_confirm_pressed"), &ResourcePreloaderEditor::_delete_confirm_pressed);
  273. ObjectTypeDB::bind_method(_MD("_files_load_request"), &ResourcePreloaderEditor::_files_load_request);
  274. ObjectTypeDB::bind_method(_MD("_update_library"), &ResourcePreloaderEditor::_update_library);
  275. ObjectTypeDB::bind_method(_MD("get_drag_data_fw"), &ResourcePreloaderEditor::get_drag_data_fw);
  276. ObjectTypeDB::bind_method(_MD("can_drop_data_fw"), &ResourcePreloaderEditor::can_drop_data_fw);
  277. ObjectTypeDB::bind_method(_MD("drop_data_fw"), &ResourcePreloaderEditor::drop_data_fw);
  278. }
  279. ResourcePreloaderEditor::ResourcePreloaderEditor() {
  280. //add_style_override("panel", get_stylebox("panel","Panel"));
  281. VBoxContainer *vbc = memnew(VBoxContainer);
  282. add_child(vbc);
  283. HBoxContainer *hbc = memnew(HBoxContainer);
  284. vbc->add_child(hbc);
  285. load = memnew(Button);
  286. load->set_tooltip(TTR("Load Resource"));
  287. hbc->add_child(load);
  288. _delete = memnew(Button);
  289. hbc->add_child(_delete);
  290. paste = memnew(Button);
  291. paste->set_text(TTR("Paste"));
  292. hbc->add_child(paste);
  293. file = memnew(EditorFileDialog);
  294. add_child(file);
  295. tree = memnew(Tree);
  296. tree->set_columns(2);
  297. tree->set_column_min_width(0, 3);
  298. tree->set_column_min_width(1, 1);
  299. tree->set_column_expand(0, true);
  300. tree->set_column_expand(1, true);
  301. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  302. tree->set_drag_forwarding(this);
  303. vbc->add_child(tree);
  304. dialog = memnew(AcceptDialog);
  305. add_child(dialog);
  306. load->connect("pressed", this, "_load_pressed");
  307. _delete->connect("pressed", this, "_delete_pressed");
  308. paste->connect("pressed", this, "_paste_pressed");
  309. file->connect("files_selected", this, "_files_load_request");
  310. //dialog->connect("confirmed", this,"_delete_confirm_pressed");
  311. tree->connect("item_edited", this, "_item_edited");
  312. loading_scene = false;
  313. }
  314. void ResourcePreloaderEditorPlugin::edit(Object *p_object) {
  315. preloader_editor->set_undo_redo(&get_undo_redo());
  316. ResourcePreloader *s = p_object->cast_to<ResourcePreloader>();
  317. if (!s)
  318. return;
  319. preloader_editor->edit(s);
  320. }
  321. bool ResourcePreloaderEditorPlugin::handles(Object *p_object) const {
  322. return p_object->is_type("ResourcePreloader");
  323. }
  324. void ResourcePreloaderEditorPlugin::make_visible(bool p_visible) {
  325. if (p_visible) {
  326. //preloader_editor->show();
  327. button->show();
  328. editor->make_bottom_panel_item_visible(preloader_editor);
  329. // preloader_editor->set_process(true);
  330. } else {
  331. if (preloader_editor->is_visible())
  332. editor->hide_bottom_panel();
  333. button->hide();
  334. //preloader_editor->hide();
  335. // preloader_editor->set_process(false);
  336. }
  337. }
  338. ResourcePreloaderEditorPlugin::ResourcePreloaderEditorPlugin(EditorNode *p_node) {
  339. editor = p_node;
  340. preloader_editor = memnew(ResourcePreloaderEditor);
  341. preloader_editor->set_custom_minimum_size(Size2(0, 250));
  342. button = editor->add_bottom_panel_item("ResourcePreloader", preloader_editor);
  343. button->hide();
  344. // preloader_editor->set_anchor( MARGIN_TOP, Control::ANCHOR_END);
  345. // preloader_editor->set_margin( MARGIN_TOP, 120 );
  346. }
  347. ResourcePreloaderEditorPlugin::~ResourcePreloaderEditorPlugin() {
  348. }