resource_preloader_editor_plugin.cpp 14 KB

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