editor_bitmask_import_plugin.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*************************************************************************/
  2. /* editor_bitmask_import_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 "editor_bitmask_import_plugin.h"
  31. #include "editor/editor_dir_dialog.h"
  32. #include "editor/editor_file_dialog.h"
  33. #include "editor/editor_node.h"
  34. #include "editor/editor_settings.h"
  35. #include "editor/property_editor.h"
  36. #include "io/image_loader.h"
  37. #include "io/marshalls.h"
  38. #include "io/resource_saver.h"
  39. #include "os/file_access.h"
  40. class _EditorBitMaskImportOptions : public Object {
  41. OBJ_TYPE(_EditorBitMaskImportOptions, Object);
  42. public:
  43. bool _set(const StringName &p_name, const Variant &p_value) {
  44. return false;
  45. }
  46. bool _get(const StringName &p_name, Variant &r_ret) const {
  47. return false;
  48. }
  49. void _get_property_list(List<PropertyInfo> *p_list) const {
  50. }
  51. static void _bind_methods() {
  52. ADD_SIGNAL(MethodInfo("changed"));
  53. }
  54. _EditorBitMaskImportOptions() {
  55. }
  56. };
  57. class EditorBitMaskImportDialog : public ConfirmationDialog {
  58. OBJ_TYPE(EditorBitMaskImportDialog, ConfirmationDialog);
  59. EditorBitMaskImportPlugin *plugin;
  60. LineEdit *import_path;
  61. LineEdit *save_path;
  62. EditorFileDialog *file_select;
  63. EditorDirDialog *save_select;
  64. ConfirmationDialog *error_dialog;
  65. PropertyEditor *option_editor;
  66. public:
  67. void _choose_files(const Vector<String> &p_path) {
  68. String files;
  69. for (int i = 0; i < p_path.size(); i++) {
  70. if (i > 0)
  71. files += ",";
  72. files += p_path[i];
  73. }
  74. import_path->set_text(files);
  75. }
  76. void _choose_save_dir(const String &p_path) {
  77. save_path->set_text(p_path);
  78. }
  79. void _browse() {
  80. file_select->popup_centered_ratio();
  81. }
  82. void _browse_target() {
  83. save_select->popup_centered_ratio();
  84. }
  85. void popup_import(const String &p_path) {
  86. popup_centered(Size2(400, 100) * EDSCALE);
  87. if (p_path != "") {
  88. Ref<ResourceImportMetadata> rimd = ResourceLoader::load_import_metadata(p_path);
  89. ERR_FAIL_COND(!rimd.is_valid());
  90. save_path->set_text(p_path.get_base_dir());
  91. String src = "";
  92. for (int i = 0; i < rimd->get_source_count(); i++) {
  93. if (i > 0)
  94. src += ",";
  95. src += EditorImportPlugin::expand_source_path(rimd->get_source_path(i));
  96. }
  97. import_path->set_text(src);
  98. }
  99. }
  100. void _import() {
  101. Vector<String> bitmasks = import_path->get_text().split(",");
  102. if (bitmasks.size() == 0) {
  103. error_dialog->set_text(TTR("No bit masks to import!"));
  104. error_dialog->popup_centered(Size2(200, 100) * EDSCALE);
  105. }
  106. if (save_path->get_text().strip_edges() == "") {
  107. error_dialog->set_text(TTR("Target path is empty."));
  108. error_dialog->popup_centered_minsize();
  109. return;
  110. }
  111. if (!save_path->get_text().begins_with("res://")) {
  112. error_dialog->set_text(TTR("Target path must be a complete resource path."));
  113. error_dialog->popup_centered_minsize();
  114. return;
  115. }
  116. if (!DirAccess::exists(save_path->get_text())) {
  117. error_dialog->set_text(TTR("Target path must exist."));
  118. error_dialog->popup_centered_minsize();
  119. return;
  120. }
  121. for (int i = 0; i < bitmasks.size(); i++) {
  122. Ref<ResourceImportMetadata> imd = memnew(ResourceImportMetadata);
  123. imd->add_source(EditorImportPlugin::validate_source_path(bitmasks[i]));
  124. String dst = save_path->get_text();
  125. if (dst == "") {
  126. error_dialog->set_text(TTR("Save path is empty!"));
  127. error_dialog->popup_centered(Size2(200, 100) * EDSCALE);
  128. }
  129. dst = dst.plus_file(bitmasks[i].get_file().basename() + ".pbm");
  130. Error err = plugin->import(dst, imd);
  131. }
  132. hide();
  133. }
  134. void _notification(int p_what) {
  135. }
  136. static void _bind_methods() {
  137. ObjectTypeDB::bind_method("_choose_files", &EditorBitMaskImportDialog::_choose_files);
  138. ObjectTypeDB::bind_method("_choose_save_dir", &EditorBitMaskImportDialog::_choose_save_dir);
  139. ObjectTypeDB::bind_method("_import", &EditorBitMaskImportDialog::_import);
  140. ObjectTypeDB::bind_method("_browse", &EditorBitMaskImportDialog::_browse);
  141. ObjectTypeDB::bind_method("_browse_target", &EditorBitMaskImportDialog::_browse_target);
  142. // ADD_SIGNAL( MethodInfo("imported",PropertyInfo(Variant::OBJECT,"scene")) );
  143. }
  144. EditorBitMaskImportDialog(EditorBitMaskImportPlugin *p_plugin) {
  145. plugin = p_plugin;
  146. set_title(TTR("Import BitMasks"));
  147. VBoxContainer *vbc = memnew(VBoxContainer);
  148. add_child(vbc);
  149. set_child_rect(vbc);
  150. HBoxContainer *hbc = memnew(HBoxContainer);
  151. vbc->add_margin_child(TTR("Source Texture(s):"), hbc);
  152. import_path = memnew(LineEdit);
  153. import_path->set_h_size_flags(SIZE_EXPAND_FILL);
  154. hbc->add_child(import_path);
  155. Button *import_choose = memnew(Button);
  156. import_choose->set_text(" .. ");
  157. hbc->add_child(import_choose);
  158. import_choose->connect("pressed", this, "_browse");
  159. hbc = memnew(HBoxContainer);
  160. vbc->add_margin_child(TTR("Target Path:"), hbc);
  161. save_path = memnew(LineEdit);
  162. save_path->set_h_size_flags(SIZE_EXPAND_FILL);
  163. hbc->add_child(save_path);
  164. Button *save_choose = memnew(Button);
  165. save_choose->set_text(" .. ");
  166. hbc->add_child(save_choose);
  167. save_choose->connect("pressed", this, "_browse_target");
  168. file_select = memnew(EditorFileDialog);
  169. file_select->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  170. add_child(file_select);
  171. file_select->set_mode(EditorFileDialog::MODE_OPEN_FILES);
  172. file_select->connect("files_selected", this, "_choose_files");
  173. List<String> extensions;
  174. ImageLoader::get_recognized_extensions(&extensions);
  175. file_select->clear_filters();
  176. for (int i = 0; i < extensions.size(); i++) {
  177. file_select->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
  178. }
  179. save_select = memnew(EditorDirDialog);
  180. add_child(save_select);
  181. // save_select->set_mode(EditorFileDialog::MODE_OPEN_DIR);
  182. save_select->connect("dir_selected", this, "_choose_save_dir");
  183. get_ok()->connect("pressed", this, "_import");
  184. get_ok()->set_text(TTR("Import"));
  185. error_dialog = memnew(ConfirmationDialog);
  186. add_child(error_dialog);
  187. error_dialog->get_ok()->set_text(TTR("Accept"));
  188. // error_dialog->get_cancel()->hide();
  189. set_hide_on_ok(false);
  190. }
  191. ~EditorBitMaskImportDialog() {
  192. }
  193. };
  194. String EditorBitMaskImportPlugin::get_name() const {
  195. return "bitmask";
  196. }
  197. String EditorBitMaskImportPlugin::get_visible_name() const {
  198. return TTR("Bit Mask");
  199. }
  200. void EditorBitMaskImportPlugin::import_dialog(const String &p_from) {
  201. dialog->popup_import(p_from);
  202. }
  203. Error EditorBitMaskImportPlugin::import(const String &p_path, const Ref<ResourceImportMetadata> &p_from) {
  204. ERR_FAIL_COND_V(p_from->get_source_count() != 1, ERR_INVALID_PARAMETER);
  205. Ref<ResourceImportMetadata> from = p_from;
  206. String src_path = EditorImportPlugin::expand_source_path(from->get_source_path(0));
  207. Ref<ImageTexture> it = ResourceLoader::load(src_path);
  208. ERR_FAIL_COND_V(it.is_null(), ERR_CANT_OPEN);
  209. Ref<BitMap> target = memnew(BitMap);
  210. target->create_from_image_alpha(it.ptr()->get_data());
  211. from->set_source_md5(0, FileAccess::get_md5(src_path));
  212. from->set_editor(get_name());
  213. target->set_import_metadata(from);
  214. Error err = ResourceSaver::save(p_path, target);
  215. return err;
  216. }
  217. EditorBitMaskImportPlugin *EditorBitMaskImportPlugin::singleton = NULL;
  218. void EditorBitMaskImportPlugin::import_from_drop(const Vector<String> &p_drop, const String &p_dest_path) {
  219. Vector<String> files;
  220. List<String> valid_extensions;
  221. ImageLoader::get_recognized_extensions(&valid_extensions);
  222. for (int i = 0; i < p_drop.size(); i++) {
  223. String extension = p_drop[i].extension().to_lower();
  224. for (List<String>::Element *E = valid_extensions.front(); E; E = E->next()) {
  225. if (E->get() == extension) {
  226. files.push_back(p_drop[i]);
  227. break;
  228. }
  229. }
  230. }
  231. if (files.size()) {
  232. import_dialog();
  233. dialog->_choose_files(files);
  234. dialog->_choose_save_dir(p_dest_path);
  235. }
  236. }
  237. void EditorBitMaskImportPlugin::reimport_multiple_files(const Vector<String> &p_list) {
  238. if (p_list.size() == 0)
  239. return;
  240. Vector<String> sources;
  241. for (int i = 0; i < p_list.size(); i++) {
  242. int idx;
  243. EditorFileSystemDirectory *efsd = EditorFileSystem::get_singleton()->find_file(p_list[i], &idx);
  244. if (efsd) {
  245. for (int j = 0; j < efsd->get_source_count(idx); j++) {
  246. String file = expand_source_path(efsd->get_source_file(idx, j));
  247. if (sources.find(file) == -1) {
  248. sources.push_back(file);
  249. }
  250. }
  251. }
  252. }
  253. if (sources.size()) {
  254. dialog->popup_import(p_list[0]);
  255. dialog->_choose_files(sources);
  256. dialog->_choose_save_dir(p_list[0].get_base_dir());
  257. }
  258. }
  259. bool EditorBitMaskImportPlugin::can_reimport_multiple_files() const {
  260. return true;
  261. }
  262. EditorBitMaskImportPlugin::EditorBitMaskImportPlugin(EditorNode *p_editor) {
  263. singleton = this;
  264. dialog = memnew(EditorBitMaskImportDialog(this));
  265. p_editor->get_gui_base()->add_child(dialog);
  266. }
  267. EditorBitMaskExportPlugin::EditorBitMaskExportPlugin() {
  268. }