resource_preloader_editor_plugin.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_scale.h"
  34. #include "editor/editor_settings.h"
  35. void ResourcePreloaderEditor::_gui_input(Ref<InputEvent> p_event) {
  36. }
  37. void ResourcePreloaderEditor::_notification(int p_what) {
  38. switch (p_what) {
  39. case NOTIFICATION_ENTER_TREE:
  40. case NOTIFICATION_THEME_CHANGED: {
  41. load->set_icon(get_icon("Folder", "EditorIcons"));
  42. } break;
  43. }
  44. }
  45. void ResourcePreloaderEditor::_files_load_request(const Vector<String> &p_paths) {
  46. for (int i = 0; i < p_paths.size(); i++) {
  47. String path = p_paths[i];
  48. RES resource;
  49. resource = ResourceLoader::load(path);
  50. if (resource.is_null()) {
  51. dialog->set_text(TTR("ERROR: Couldn't load resource!"));
  52. dialog->set_title(TTR("Error!"));
  53. //dialog->get_cancel()->set_text("Close");
  54. dialog->get_ok()->set_text(TTR("Close"));
  55. dialog->popup_centered_minsize();
  56. return; ///beh should show an error i guess
  57. }
  58. String basename = path.get_file().get_basename();
  59. String name = basename;
  60. int counter = 1;
  61. while (preloader->has_resource(name)) {
  62. counter++;
  63. name = basename + " " + itos(counter);
  64. }
  65. undo_redo->create_action(TTR("Add Resource"));
  66. undo_redo->add_do_method(preloader, "add_resource", name, resource);
  67. undo_redo->add_undo_method(preloader, "remove_resource", name);
  68. undo_redo->add_do_method(this, "_update_library");
  69. undo_redo->add_undo_method(this, "_update_library");
  70. undo_redo->commit_action();
  71. }
  72. }
  73. void ResourcePreloaderEditor::_load_pressed() {
  74. loading_scene = false;
  75. file->clear_filters();
  76. List<String> extensions;
  77. ResourceLoader::get_recognized_extensions_for_type("", &extensions);
  78. for (int i = 0; i < extensions.size(); i++) {
  79. file->add_filter("*." + extensions[i]);
  80. }
  81. file->set_mode(EditorFileDialog::MODE_OPEN_FILES);
  82. file->popup_centered_ratio();
  83. }
  84. void ResourcePreloaderEditor::_item_edited() {
  85. if (!tree->get_selected()) {
  86. return;
  87. }
  88. TreeItem *s = tree->get_selected();
  89. if (tree->get_selected_column() == 0) {
  90. // renamed
  91. String old_name = s->get_metadata(0);
  92. String new_name = s->get_text(0);
  93. if (old_name == new_name) {
  94. return;
  95. }
  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. }
  132. if (name == "") {
  133. name = r->get_class();
  134. }
  135. String basename = name;
  136. int counter = 1;
  137. while (preloader->has_resource(name)) {
  138. counter++;
  139. name = basename + " " + itos(counter);
  140. }
  141. undo_redo->create_action(TTR("Paste Resource"));
  142. undo_redo->add_do_method(preloader, "add_resource", name, r);
  143. undo_redo->add_undo_method(preloader, "remove_resource", name);
  144. undo_redo->add_do_method(this, "_update_library");
  145. undo_redo->add_undo_method(this, "_update_library");
  146. undo_redo->commit_action();
  147. }
  148. void ResourcePreloaderEditor::_update_library() {
  149. tree->clear();
  150. tree->set_hide_root(true);
  151. TreeItem *root = tree->create_item(nullptr);
  152. List<StringName> rnames;
  153. preloader->get_resource_list(&rnames);
  154. List<String> names;
  155. for (List<StringName>::Element *E = rnames.front(); E; E = E->next()) {
  156. names.push_back(E->get());
  157. }
  158. names.sort();
  159. for (List<String>::Element *E = names.front(); E; E = E->next()) {
  160. TreeItem *ti = tree->create_item(root);
  161. ti->set_cell_mode(0, TreeItem::CELL_MODE_STRING);
  162. ti->set_editable(0, true);
  163. ti->set_selectable(0, true);
  164. ti->set_text(0, E->get());
  165. ti->set_metadata(0, E->get());
  166. RES r = preloader->get_resource(E->get());
  167. ERR_CONTINUE(r.is_null());
  168. String type = r->get_class();
  169. ti->set_icon(0, EditorNode::get_singleton()->get_class_icon(type, "Object"));
  170. ti->set_tooltip(0, TTR("Instance:") + " " + r->get_path() + "\n" + TTR("Type:") + " " + type);
  171. ti->set_text(1, r->get_path());
  172. ti->set_editable(1, false);
  173. ti->set_selectable(1, false);
  174. if (type == "PackedScene") {
  175. ti->add_button(1, get_icon("InstanceOptions", "EditorIcons"), BUTTON_OPEN_SCENE, false, TTR("Open in Editor"));
  176. } else {
  177. ti->add_button(1, get_icon("Load", "EditorIcons"), BUTTON_EDIT_RESOURCE, false, TTR("Open in Editor"));
  178. }
  179. ti->add_button(1, get_icon("Remove", "EditorIcons"), BUTTON_REMOVE, false, TTR("Remove"));
  180. }
  181. //player->add_resource("default",resource);
  182. }
  183. void ResourcePreloaderEditor::_cell_button_pressed(Object *p_item, int p_column, int p_id) {
  184. TreeItem *item = Object::cast_to<TreeItem>(p_item);
  185. ERR_FAIL_COND(!item);
  186. if (p_id == BUTTON_OPEN_SCENE) {
  187. String rpath = item->get_text(p_column);
  188. EditorInterface::get_singleton()->open_scene_from_path(rpath);
  189. } else if (p_id == BUTTON_EDIT_RESOURCE) {
  190. RES r = preloader->get_resource(item->get_text(0));
  191. EditorInterface::get_singleton()->edit_resource(r);
  192. } else if (p_id == BUTTON_REMOVE) {
  193. _remove_resource(item->get_text(0));
  194. }
  195. }
  196. void ResourcePreloaderEditor::edit(ResourcePreloader *p_preloader) {
  197. preloader = p_preloader;
  198. if (p_preloader) {
  199. _update_library();
  200. } else {
  201. hide();
  202. set_physics_process(false);
  203. }
  204. }
  205. Variant ResourcePreloaderEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  206. TreeItem *ti = tree->get_item_at_position(p_point);
  207. if (!ti) {
  208. return Variant();
  209. }
  210. String name = ti->get_metadata(0);
  211. RES res = preloader->get_resource(name);
  212. if (!res.is_valid()) {
  213. return Variant();
  214. }
  215. return EditorNode::get_singleton()->drag_resource(res, p_from);
  216. }
  217. bool ResourcePreloaderEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  218. Dictionary d = p_data;
  219. if (!d.has("type")) {
  220. return false;
  221. }
  222. if (d.has("from") && (Object *)(d["from"]) == tree) {
  223. return false;
  224. }
  225. if (String(d["type"]) == "resource" && d.has("resource")) {
  226. RES r = d["resource"];
  227. return r.is_valid();
  228. }
  229. if (String(d["type"]) == "files") {
  230. Vector<String> files = d["files"];
  231. return files.size() != 0;
  232. }
  233. return false;
  234. }
  235. void ResourcePreloaderEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  236. if (!can_drop_data_fw(p_point, p_data, p_from)) {
  237. return;
  238. }
  239. Dictionary d = p_data;
  240. if (!d.has("type")) {
  241. return;
  242. }
  243. if (String(d["type"]) == "resource" && d.has("resource")) {
  244. RES r = d["resource"];
  245. if (r.is_valid()) {
  246. String basename;
  247. if (r->get_name() != "") {
  248. basename = r->get_name();
  249. } else if (r->get_path().is_resource_file()) {
  250. basename = r->get_path().get_basename();
  251. } else {
  252. basename = "Resource";
  253. }
  254. String name = basename;
  255. int counter = 0;
  256. while (preloader->has_resource(name)) {
  257. counter++;
  258. name = basename + "_" + itos(counter);
  259. }
  260. undo_redo->create_action(TTR("Add Resource"));
  261. undo_redo->add_do_method(preloader, "add_resource", name, r);
  262. undo_redo->add_undo_method(preloader, "remove_resource", name);
  263. undo_redo->add_do_method(this, "_update_library");
  264. undo_redo->add_undo_method(this, "_update_library");
  265. undo_redo->commit_action();
  266. }
  267. }
  268. if (String(d["type"]) == "files") {
  269. Vector<String> files = d["files"];
  270. _files_load_request(files);
  271. }
  272. }
  273. void ResourcePreloaderEditor::_bind_methods() {
  274. ClassDB::bind_method(D_METHOD("_gui_input"), &ResourcePreloaderEditor::_gui_input);
  275. ClassDB::bind_method(D_METHOD("_load_pressed"), &ResourcePreloaderEditor::_load_pressed);
  276. ClassDB::bind_method(D_METHOD("_item_edited"), &ResourcePreloaderEditor::_item_edited);
  277. ClassDB::bind_method(D_METHOD("_paste_pressed"), &ResourcePreloaderEditor::_paste_pressed);
  278. ClassDB::bind_method(D_METHOD("_files_load_request"), &ResourcePreloaderEditor::_files_load_request);
  279. ClassDB::bind_method(D_METHOD("_update_library"), &ResourcePreloaderEditor::_update_library);
  280. ClassDB::bind_method(D_METHOD("_cell_button_pressed"), &ResourcePreloaderEditor::_cell_button_pressed);
  281. ClassDB::bind_method(D_METHOD("_remove_resource", "to_remove"), &ResourcePreloaderEditor::_remove_resource);
  282. ClassDB::bind_method(D_METHOD("get_drag_data_fw"), &ResourcePreloaderEditor::get_drag_data_fw);
  283. ClassDB::bind_method(D_METHOD("can_drop_data_fw"), &ResourcePreloaderEditor::can_drop_data_fw);
  284. ClassDB::bind_method(D_METHOD("drop_data_fw"), &ResourcePreloaderEditor::drop_data_fw);
  285. }
  286. ResourcePreloaderEditor::ResourcePreloaderEditor() {
  287. //add_style_override("panel", EditorNode::get_singleton()->get_gui_base()->get_stylebox("panel","Panel"));
  288. VBoxContainer *vbc = memnew(VBoxContainer);
  289. add_child(vbc);
  290. HBoxContainer *hbc = memnew(HBoxContainer);
  291. vbc->add_child(hbc);
  292. load = memnew(Button);
  293. load->set_tooltip(TTR("Load Resource"));
  294. hbc->add_child(load);
  295. paste = memnew(Button);
  296. paste->set_text(TTR("Paste"));
  297. hbc->add_child(paste);
  298. file = memnew(EditorFileDialog);
  299. add_child(file);
  300. tree = memnew(Tree);
  301. tree->connect("button_pressed", this, "_cell_button_pressed");
  302. tree->set_columns(2);
  303. tree->set_column_min_width(0, 2);
  304. tree->set_column_min_width(1, 3);
  305. tree->set_column_expand(0, true);
  306. tree->set_column_expand(1, true);
  307. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  308. tree->set_drag_forwarding(this);
  309. vbc->add_child(tree);
  310. dialog = memnew(AcceptDialog);
  311. add_child(dialog);
  312. load->connect("pressed", this, "_load_pressed");
  313. paste->connect("pressed", this, "_paste_pressed");
  314. file->connect("files_selected", this, "_files_load_request");
  315. tree->connect("item_edited", this, "_item_edited");
  316. loading_scene = false;
  317. }
  318. void ResourcePreloaderEditorPlugin::edit(Object *p_object) {
  319. preloader_editor->set_undo_redo(&get_undo_redo());
  320. ResourcePreloader *s = Object::cast_to<ResourcePreloader>(p_object);
  321. if (!s) {
  322. return;
  323. }
  324. preloader_editor->edit(s);
  325. }
  326. bool ResourcePreloaderEditorPlugin::handles(Object *p_object) const {
  327. return p_object->is_class("ResourcePreloader");
  328. }
  329. void ResourcePreloaderEditorPlugin::make_visible(bool p_visible) {
  330. if (p_visible) {
  331. //preloader_editor->show();
  332. button->show();
  333. editor->make_bottom_panel_item_visible(preloader_editor);
  334. //preloader_editor->set_process(true);
  335. } else {
  336. if (preloader_editor->is_visible_in_tree()) {
  337. editor->hide_bottom_panel();
  338. }
  339. button->hide();
  340. //preloader_editor->hide();
  341. //preloader_editor->set_process(false);
  342. }
  343. }
  344. ResourcePreloaderEditorPlugin::ResourcePreloaderEditorPlugin(EditorNode *p_node) {
  345. editor = p_node;
  346. preloader_editor = memnew(ResourcePreloaderEditor);
  347. preloader_editor->set_custom_minimum_size(Size2(0, 250) * EDSCALE);
  348. button = editor->add_bottom_panel_item("ResourcePreloader", preloader_editor);
  349. button->hide();
  350. }
  351. ResourcePreloaderEditorPlugin::~ResourcePreloaderEditorPlugin() {
  352. }