resources_dock.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*************************************************************************/
  2. /* resources_dock.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 "resources_dock.h"
  31. #include "editor_file_system.h"
  32. #include "editor_node.h"
  33. #include "editor_settings.h"
  34. #include "globals.h"
  35. #include "io/resource_loader.h"
  36. #include "io/resource_saver.h"
  37. #include "project_settings.h"
  38. void ResourcesDock::_tool_selected(int p_tool) {
  39. current_action = p_tool;
  40. switch (p_tool) {
  41. case TOOL_NEW: {
  42. create_dialog->popup_centered_ratio();
  43. } break;
  44. case TOOL_OPEN: {
  45. editor->open_resource();
  46. } break;
  47. case TOOL_SAVE: {
  48. TreeItem *ti = resources->get_selected();
  49. if (!ti)
  50. break;
  51. Ref<Resource> current_res = ti->get_metadata(0);
  52. if (current_res->get_path() != "" && current_res->get_path().find("::") == -1) {
  53. _file_action(current_res->get_path());
  54. break;
  55. };
  56. }; /* fallthrough */
  57. case TOOL_SAVE_AS: {
  58. TreeItem *ti = resources->get_selected();
  59. if (!ti)
  60. break;
  61. save_resource_as(ti->get_metadata(0));
  62. } break;
  63. case TOOL_MAKE_LOCAL: {
  64. TreeItem *ti = resources->get_selected();
  65. if (!ti)
  66. break;
  67. Ref<Resource> current_res = ti->get_metadata(0);
  68. current_res->set_path("");
  69. _update_name(ti);
  70. } break;
  71. case TOOL_COPY: {
  72. TreeItem *ti = resources->get_selected();
  73. if (!ti)
  74. break;
  75. Ref<Resource> current_res = ti->get_metadata(0);
  76. EditorSettings::get_singleton()->set_resource_clipboard(current_res);
  77. } break;
  78. case TOOL_PASTE: {
  79. add_resource(EditorSettings::get_singleton()->get_resource_clipboard());
  80. } break;
  81. }
  82. }
  83. void ResourcesDock::_notification(int p_what) {
  84. switch (p_what) {
  85. case NOTIFICATION_ENTER_TREE: {
  86. button_new->set_icon(get_icon("New", "EditorIcons"));
  87. button_open->set_icon(get_icon("Folder", "EditorIcons"));
  88. button_save->set_icon(get_icon("Save", "EditorIcons"));
  89. button_tools->set_icon(get_icon("Tools", "EditorIcons"));
  90. } break;
  91. }
  92. }
  93. void ResourcesDock::save_resource(const String &p_path, const Ref<Resource> &p_resource) {
  94. editor->get_editor_data().apply_changes_in_editors();
  95. int flg = 0;
  96. if (EditorSettings::get_singleton()->get("on_save/compress_binary_resources"))
  97. flg |= ResourceSaver::FLAG_COMPRESS;
  98. //if (EditorSettings::get_singleton()->get("on_save/save_paths_as_relative"))
  99. // flg|=ResourceSaver::FLAG_RELATIVE_PATHS;
  100. String path = Globals::get_singleton()->localize_path(p_path);
  101. Error err = ResourceSaver::save(path, p_resource, flg | ResourceSaver::FLAG_REPLACE_SUBRESOURCE_PATHS);
  102. if (err != OK) {
  103. accept->set_text(TTR("Error saving resource!"));
  104. accept->popup_centered_minsize();
  105. return;
  106. }
  107. // EditorFileSystem::get_singleton()->update_file(path,p_resource->get_type());
  108. ((Resource *)p_resource.ptr())->set_path(path);
  109. editor->emit_signal("resource_saved", p_resource);
  110. }
  111. void ResourcesDock::save_resource_as(const Ref<Resource> &p_resource) {
  112. current_action = TOOL_SAVE_AS;
  113. RES res(p_resource);
  114. List<String> extensions;
  115. ResourceSaver::get_recognized_extensions(res, &extensions);
  116. file->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  117. if (p_resource->get_path() != "" && p_resource->get_path().find("::") == -1) {
  118. file->set_current_path(p_resource->get_path());
  119. } else {
  120. String existing;
  121. if (extensions.size()) {
  122. existing = "new_" + res->get_type().to_lower() + "." + extensions.front()->get().to_lower();
  123. }
  124. file->set_current_file(existing);
  125. }
  126. file->clear_filters();
  127. for (int i = 0; i < extensions.size(); i++) {
  128. file->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
  129. }
  130. file->popup_centered_ratio();
  131. }
  132. void ResourcesDock::_file_action(const String &p_path) {
  133. switch (current_action) {
  134. case TOOL_OPEN: {
  135. } break;
  136. case TOOL_SAVE:
  137. case TOOL_SAVE_AS: {
  138. TreeItem *ti = resources->get_selected();
  139. if (!ti)
  140. break;
  141. Ref<Resource> current_res = ti->get_metadata(0);
  142. RES res(current_res);
  143. save_resource(p_path, res);
  144. _update_name(ti);
  145. } break;
  146. }
  147. }
  148. void ResourcesDock::_update_name(TreeItem *item) {
  149. Ref<Resource> res = item->get_metadata(0);
  150. if (res->get_name() != "")
  151. item->set_text(0, res->get_name());
  152. else if (res->get_path() != "" && res->get_path().find("::") == -1)
  153. item->set_text(0, res->get_path().get_file());
  154. else
  155. item->set_text(0, res->get_type() + " (" + itos(res->get_instance_ID()) + ")");
  156. }
  157. void ResourcesDock::remove_resource(const Ref<Resource> &p_resource) {
  158. TreeItem *root = resources->get_root();
  159. ERR_FAIL_COND(!root);
  160. TreeItem *existing = root->get_children();
  161. while (existing) {
  162. Ref<Resource> r = existing->get_metadata(0);
  163. if (r == p_resource) {
  164. //existing->move_to_top();
  165. memdelete(existing);
  166. return;
  167. }
  168. existing = existing->get_next();
  169. }
  170. }
  171. void ResourcesDock::add_resource(const Ref<Resource> &p_resource) {
  172. if (block_add)
  173. return;
  174. if (!p_resource.is_valid())
  175. return;
  176. TreeItem *root = resources->get_root();
  177. ERR_FAIL_COND(!root);
  178. TreeItem *existing = root->get_children();
  179. while (existing) {
  180. Ref<Resource> r = existing->get_metadata(0);
  181. if (r == p_resource) {
  182. //existing->move_to_top();
  183. existing->select(0);
  184. resources->ensure_cursor_is_visible();
  185. return; // existing
  186. }
  187. existing = existing->get_next();
  188. }
  189. TreeItem *res = resources->create_item(root);
  190. res->set_metadata(0, p_resource);
  191. if (has_icon(p_resource->get_type(), "EditorIcons")) {
  192. res->set_icon(0, get_icon(p_resource->get_type(), "EditorIcons"));
  193. }
  194. _update_name(res);
  195. res->add_button(0, get_icon("Del", "EditorIcons"));
  196. res->move_to_top();
  197. res->select(0);
  198. resources->ensure_cursor_is_visible();
  199. }
  200. void ResourcesDock::_resource_selected() {
  201. TreeItem *sel = resources->get_selected();
  202. ERR_FAIL_COND(!sel);
  203. Ref<Resource> r = sel->get_metadata(0);
  204. if (r.is_null())
  205. return;
  206. block_add = true;
  207. editor->push_item(r.ptr());
  208. block_add = false;
  209. }
  210. void ResourcesDock::_delete(Object *p_item, int p_column, int p_id) {
  211. TreeItem *ti = p_item->cast_to<TreeItem>();
  212. ERR_FAIL_COND(!ti);
  213. call_deferred("remove_resource", ti->get_metadata(0));
  214. }
  215. void ResourcesDock::_create() {
  216. Object *c = create_dialog->instance_selected();
  217. ERR_FAIL_COND(!c);
  218. Resource *r = c->cast_to<Resource>();
  219. ERR_FAIL_COND(!r);
  220. REF res(r);
  221. editor->push_item(c);
  222. }
  223. void ResourcesDock::_bind_methods() {
  224. ObjectTypeDB::bind_method(_MD("_tool_selected"), &ResourcesDock::_tool_selected);
  225. ObjectTypeDB::bind_method(_MD("_create"), &ResourcesDock::_create);
  226. ObjectTypeDB::bind_method(_MD("_resource_selected"), &ResourcesDock::_resource_selected);
  227. ObjectTypeDB::bind_method(_MD("_delete"), &ResourcesDock::_delete);
  228. ObjectTypeDB::bind_method(_MD("remove_resource"), &ResourcesDock::remove_resource);
  229. ObjectTypeDB::bind_method(_MD("_file_action"), &ResourcesDock::_file_action);
  230. }
  231. void ResourcesDock::cleanup() {
  232. resources->clear();
  233. resources->create_item(); //root
  234. }
  235. ResourcesDock::ResourcesDock(EditorNode *p_editor) {
  236. editor = p_editor;
  237. VBoxContainer *vbc = this;
  238. HBoxContainer *hbc = memnew(HBoxContainer);
  239. vbc->add_child(hbc);
  240. Button *b;
  241. b = memnew(ToolButton);
  242. b->set_tooltip(TTR("Create New Resource"));
  243. b->connect("pressed", this, "_tool_selected", make_binds(TOOL_NEW));
  244. hbc->add_child(b);
  245. button_new = b;
  246. b = memnew(ToolButton);
  247. b->set_tooltip(TTR("Open Resource"));
  248. b->connect("pressed", this, "_tool_selected", make_binds(TOOL_OPEN));
  249. hbc->add_child(b);
  250. button_open = b;
  251. MenuButton *mb = memnew(MenuButton);
  252. mb->set_tooltip(TTR("Save Resource"));
  253. mb->get_popup()->add_item(TTR("Save Resource"), TOOL_SAVE);
  254. mb->get_popup()->add_item(TTR("Save Resource As.."), TOOL_SAVE_AS);
  255. mb->get_popup()->connect("item_pressed", this, "_tool_selected");
  256. hbc->add_child(mb);
  257. button_save = mb;
  258. hbc->add_spacer();
  259. mb = memnew(MenuButton);
  260. mb->set_tooltip(TTR("Resource Tools"));
  261. mb->get_popup()->add_item(TTR("Make Local"), TOOL_MAKE_LOCAL);
  262. mb->get_popup()->add_item(TTR("Copy"), TOOL_COPY);
  263. mb->get_popup()->add_item(TTR("Paste"), TOOL_PASTE);
  264. mb->get_popup()->connect("item_pressed", this, "_tool_selected");
  265. hbc->add_child(mb);
  266. button_tools = mb;
  267. resources = memnew(Tree);
  268. vbc->add_child(resources);
  269. resources->set_v_size_flags(SIZE_EXPAND_FILL);
  270. resources->create_item(); //root
  271. resources->set_hide_root(true);
  272. resources->connect("cell_selected", this, "_resource_selected");
  273. resources->connect("button_pressed", this, "_delete");
  274. create_dialog = memnew(CreateDialog);
  275. add_child(create_dialog);
  276. create_dialog->set_base_type("Resource");
  277. create_dialog->connect("create", this, "_create");
  278. accept = memnew(AcceptDialog);
  279. add_child(accept);
  280. file = memnew(EditorFileDialog);
  281. add_child(file);
  282. file->connect("file_selected", this, "_file_action");
  283. block_add = false;
  284. }