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-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 "resource_preloader_editor_plugin.h"
  31. #include "core/io/resource_loader.h"
  32. #include "core/project_settings.h"
  33. #include "editor/editor_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. }
  42. if (p_what == NOTIFICATION_READY) {
  43. //NodePath("/root")->connect("node_removed", this,"_node_removed",Vector<Variant>(),true);
  44. }
  45. if (p_what == NOTIFICATION_DRAW) {
  46. }
  47. }
  48. void ResourcePreloaderEditor::_files_load_request(const Vector<String> &p_paths) {
  49. for (int i = 0; i < p_paths.size(); i++) {
  50. String path = p_paths[i];
  51. RES resource;
  52. resource = ResourceLoader::load(path);
  53. if (resource.is_null()) {
  54. dialog->set_text(TTR("ERROR: Couldn't load resource!"));
  55. dialog->set_title(TTR("Error!"));
  56. //dialog->get_cancel()->set_text("Close");
  57. dialog->get_ok()->set_text(TTR("Close"));
  58. dialog->popup_centered_minsize();
  59. return; ///beh should show an error i guess
  60. }
  61. String basename = path.get_file().get_basename();
  62. String name = basename;
  63. int counter = 1;
  64. while (preloader->has_resource(name)) {
  65. counter++;
  66. name = basename + " " + itos(counter);
  67. }
  68. undo_redo->create_action(TTR("Add Resource"));
  69. undo_redo->add_do_method(preloader, "add_resource", name, resource);
  70. undo_redo->add_undo_method(preloader, "remove_resource", name);
  71. undo_redo->add_do_method(this, "_update_library");
  72. undo_redo->add_undo_method(this, "_update_library");
  73. undo_redo->commit_action();
  74. }
  75. }
  76. void ResourcePreloaderEditor::_load_pressed() {
  77. loading_scene = false;
  78. file->clear_filters();
  79. List<String> extensions;
  80. ResourceLoader::get_recognized_extensions_for_type("", &extensions);
  81. for (int i = 0; i < extensions.size(); i++)
  82. file->add_filter("*." + extensions[i]);
  83. file->set_mode(EditorFileDialog::MODE_OPEN_FILES);
  84. file->popup_centered_ratio();
  85. }
  86. void ResourcePreloaderEditor::_item_edited() {
  87. if (!tree->get_selected())
  88. return;
  89. TreeItem *s = tree->get_selected();
  90. if (tree->get_selected_column() == 0) {
  91. // renamed
  92. String old_name = s->get_metadata(0);
  93. String new_name = s->get_text(0);
  94. if (old_name == new_name)
  95. return;
  96. if (new_name == "" || new_name.find("\\") != -1 || new_name.find("/") != -1 || preloader->has_resource(new_name)) {
  97. s->set_text(0, old_name);
  98. return;
  99. }
  100. RES samp = preloader->get_resource(old_name);
  101. undo_redo->create_action(TTR("Rename Resource"));
  102. undo_redo->add_do_method(preloader, "remove_resource", old_name);
  103. undo_redo->add_do_method(preloader, "add_resource", new_name, samp);
  104. undo_redo->add_undo_method(preloader, "remove_resource", new_name);
  105. undo_redo->add_undo_method(preloader, "add_resource", old_name, samp);
  106. undo_redo->add_do_method(this, "_update_library");
  107. undo_redo->add_undo_method(this, "_update_library");
  108. undo_redo->commit_action();
  109. }
  110. }
  111. void ResourcePreloaderEditor::_remove_resource(const String &p_to_remove) {
  112. undo_redo->create_action(TTR("Delete Resource"));
  113. undo_redo->add_do_method(preloader, "remove_resource", p_to_remove);
  114. undo_redo->add_undo_method(preloader, "add_resource", p_to_remove, preloader->get_resource(p_to_remove));
  115. undo_redo->add_do_method(this, "_update_library");
  116. undo_redo->add_undo_method(this, "_update_library");
  117. undo_redo->commit_action();
  118. }
  119. void ResourcePreloaderEditor::_paste_pressed() {
  120. RES r = EditorSettings::get_singleton()->get_resource_clipboard();
  121. if (!r.is_valid()) {
  122. dialog->set_text(TTR("Resource clipboard is empty!"));
  123. dialog->set_title(TTR("Error!"));
  124. dialog->get_ok()->set_text(TTR("Close"));
  125. dialog->popup_centered_minsize();
  126. return; ///beh should show an error i guess
  127. }
  128. String name = r->get_name();
  129. if (name == "")
  130. name = r->get_path().get_file();
  131. if (name == "")
  132. name = r->get_class();
  133. String basename = name;
  134. int counter = 1;
  135. while (preloader->has_resource(name)) {
  136. counter++;
  137. name = basename + " " + itos(counter);
  138. }
  139. undo_redo->create_action(TTR("Paste Resource"));
  140. undo_redo->add_do_method(preloader, "add_resource", name, r);
  141. undo_redo->add_undo_method(preloader, "remove_resource", name);
  142. undo_redo->add_do_method(this, "_update_library");
  143. undo_redo->add_undo_method(this, "_update_library");
  144. undo_redo->commit_action();
  145. }
  146. void ResourcePreloaderEditor::_update_library() {
  147. tree->clear();
  148. tree->set_hide_root(true);
  149. TreeItem *root = tree->create_item(NULL);
  150. List<StringName> rnames;
  151. preloader->get_resource_list(&rnames);
  152. List<String> names;
  153. for (List<StringName>::Element *E = rnames.front(); E; E = E->next()) {
  154. names.push_back(E->get());
  155. }
  156. names.sort();
  157. for (List<String>::Element *E = names.front(); E; E = E->next()) {
  158. TreeItem *ti = tree->create_item(root);
  159. ti->set_cell_mode(0, TreeItem::CELL_MODE_STRING);
  160. ti->set_editable(0, true);
  161. ti->set_selectable(0, true);
  162. ti->set_text(0, E->get());
  163. ti->set_metadata(0, E->get());
  164. RES r = preloader->get_resource(E->get());
  165. ERR_CONTINUE(r.is_null());
  166. String type = r->get_class();
  167. ti->set_icon(0, EditorNode::get_singleton()->get_class_icon(type, "Object"));
  168. ti->set_tooltip(0, TTR("Instance:") + " " + r->get_path() + "\n" + TTR("Type:") + " " + type);
  169. ti->set_text(1, r->get_path());
  170. ti->set_editable(1, false);
  171. ti->set_selectable(1, false);
  172. if (type == "PackedScene") {
  173. ti->add_button(1, get_icon("InstanceOptions", "EditorIcons"), BUTTON_OPEN_SCENE, false, TTR("Open in Editor"));
  174. } else {
  175. ti->add_button(1, get_icon("Load", "EditorIcons"), BUTTON_EDIT_RESOURCE, false, TTR("Open in Editor"));
  176. }
  177. ti->add_button(1, get_icon("Remove", "EditorIcons"), BUTTON_REMOVE, false, TTR("Remove"));
  178. }
  179. //player->add_resource("default",resource);
  180. }
  181. void ResourcePreloaderEditor::_cell_button_pressed(Object *p_item, int p_column, int p_id) {
  182. TreeItem *item = Object::cast_to<TreeItem>(p_item);
  183. ERR_FAIL_COND(!item);
  184. if (p_id == BUTTON_OPEN_SCENE) {
  185. String rpath = item->get_text(p_column);
  186. EditorInterface::get_singleton()->open_scene_from_path(rpath);
  187. } else if (p_id == BUTTON_EDIT_RESOURCE) {
  188. RES r = preloader->get_resource(item->get_text(0));
  189. EditorInterface::get_singleton()->edit_resource(r);
  190. } else if (p_id == BUTTON_REMOVE) {
  191. _remove_resource(item->get_text(0));
  192. }
  193. }
  194. void ResourcePreloaderEditor::edit(ResourcePreloader *p_preloader) {
  195. preloader = p_preloader;
  196. if (p_preloader) {
  197. _update_library();
  198. } else {
  199. hide();
  200. set_physics_process(false);
  201. }
  202. }
  203. Variant ResourcePreloaderEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  204. TreeItem *ti = tree->get_item_at_position(p_point);
  205. if (!ti)
  206. return Variant();
  207. String name = ti->get_metadata(0);
  208. RES res = preloader->get_resource(name);
  209. if (!res.is_valid())
  210. return Variant();
  211. return EditorNode::get_singleton()->drag_resource(res, p_from);
  212. }
  213. bool ResourcePreloaderEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  214. Dictionary d = p_data;
  215. if (!d.has("type"))
  216. return false;
  217. if (d.has("from") && (Object *)(d["from"]) == tree)
  218. return false;
  219. if (String(d["type"]) == "resource" && d.has("resource")) {
  220. RES r = d["resource"];
  221. return r.is_valid();
  222. }
  223. if (String(d["type"]) == "files") {
  224. Vector<String> files = d["files"];
  225. if (files.size() == 0)
  226. return false;
  227. return true;
  228. }
  229. return false;
  230. }
  231. void ResourcePreloaderEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  232. if (!can_drop_data_fw(p_point, p_data, p_from))
  233. return;
  234. Dictionary d = p_data;
  235. if (!d.has("type"))
  236. return;
  237. if (String(d["type"]) == "resource" && d.has("resource")) {
  238. RES r = d["resource"];
  239. if (r.is_valid()) {
  240. String basename;
  241. if (r->get_name() != "") {
  242. basename = r->get_name();
  243. } else if (r->get_path().is_resource_file()) {
  244. basename = r->get_path().get_basename();
  245. } else {
  246. basename = "Resource";
  247. }
  248. String name = basename;
  249. int counter = 0;
  250. while (preloader->has_resource(name)) {
  251. counter++;
  252. name = basename + "_" + itos(counter);
  253. }
  254. undo_redo->create_action(TTR("Add Resource"));
  255. undo_redo->add_do_method(preloader, "add_resource", name, r);
  256. undo_redo->add_undo_method(preloader, "remove_resource", name);
  257. undo_redo->add_do_method(this, "_update_library");
  258. undo_redo->add_undo_method(this, "_update_library");
  259. undo_redo->commit_action();
  260. }
  261. }
  262. if (String(d["type"]) == "files") {
  263. Vector<String> files = d["files"];
  264. _files_load_request(files);
  265. }
  266. }
  267. void ResourcePreloaderEditor::_bind_methods() {
  268. ClassDB::bind_method(D_METHOD("_gui_input"), &ResourcePreloaderEditor::_gui_input);
  269. ClassDB::bind_method(D_METHOD("_load_pressed"), &ResourcePreloaderEditor::_load_pressed);
  270. ClassDB::bind_method(D_METHOD("_item_edited"), &ResourcePreloaderEditor::_item_edited);
  271. ClassDB::bind_method(D_METHOD("_paste_pressed"), &ResourcePreloaderEditor::_paste_pressed);
  272. ClassDB::bind_method(D_METHOD("_files_load_request"), &ResourcePreloaderEditor::_files_load_request);
  273. ClassDB::bind_method(D_METHOD("_update_library"), &ResourcePreloaderEditor::_update_library);
  274. ClassDB::bind_method(D_METHOD("_cell_button_pressed"), &ResourcePreloaderEditor::_cell_button_pressed);
  275. ClassDB::bind_method(D_METHOD("_remove_resource", "to_remove"), &ResourcePreloaderEditor::_remove_resource);
  276. ClassDB::bind_method(D_METHOD("get_drag_data_fw"), &ResourcePreloaderEditor::get_drag_data_fw);
  277. ClassDB::bind_method(D_METHOD("can_drop_data_fw"), &ResourcePreloaderEditor::can_drop_data_fw);
  278. ClassDB::bind_method(D_METHOD("drop_data_fw"), &ResourcePreloaderEditor::drop_data_fw);
  279. }
  280. ResourcePreloaderEditor::ResourcePreloaderEditor() {
  281. //add_style_override("panel", EditorNode::get_singleton()->get_gui_base()->get_stylebox("panel","Panel"));
  282. VBoxContainer *vbc = memnew(VBoxContainer);
  283. add_child(vbc);
  284. HBoxContainer *hbc = memnew(HBoxContainer);
  285. vbc->add_child(hbc);
  286. load = memnew(Button);
  287. load->set_tooltip(TTR("Load Resource"));
  288. hbc->add_child(load);
  289. paste = memnew(Button);
  290. paste->set_text(TTR("Paste"));
  291. hbc->add_child(paste);
  292. file = memnew(EditorFileDialog);
  293. add_child(file);
  294. tree = memnew(Tree);
  295. tree->connect("button_pressed", this, "_cell_button_pressed");
  296. tree->set_columns(2);
  297. tree->set_column_min_width(0, 2);
  298. tree->set_column_min_width(1, 3);
  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. paste->connect("pressed", this, "_paste_pressed");
  308. file->connect("files_selected", this, "_files_load_request");
  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(TTR("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. }