From 4a5ddcbad22ce5f30e204ade872cfdf69bb60e7f Mon Sep 17 00:00:00 2001 From: Aman Jain Date: Mon, 13 Jul 2020 13:25:16 -0400 Subject: [PATCH] Copy icons to Gradle project --- platform/android/export/export.cpp | 153 +++++++++++++++++++---------- 1 file changed, 100 insertions(+), 53 deletions(-) diff --git a/platform/android/export/export.cpp b/platform/android/export/export.cpp index ed85256695..0213094c60 100644 --- a/platform/android/export/export.cpp +++ b/platform/android/export/export.cpp @@ -29,6 +29,7 @@ /*************************************************************************/ #include "export.h" +#include "gradle_export_util.h" #include "core/io/image_loader.h" #include "core/io/marshalls.h" @@ -1408,23 +1409,81 @@ class EditorExportPlatformAndroid : public EditorExportPlatform { //printf("end\n"); } - void _process_launcher_icons(const String &p_processing_file_name, const Ref &p_source_image, const LauncherIcon p_icon, Vector &p_data) { - if (p_processing_file_name == p_icon.export_path) { - Ref working_image = p_source_image; + void _process_launcher_icons(const String &p_file_name, const Ref &p_source_image, int dimension, Vector &p_data) { + Ref working_image = p_source_image; - if (p_source_image->get_width() != p_icon.dimensions || p_source_image->get_height() != p_icon.dimensions) { - working_image = p_source_image->duplicate(); - working_image->resize(p_icon.dimensions, p_icon.dimensions, Image::Interpolation::INTERPOLATE_LANCZOS); + if (p_source_image->get_width() != dimension || p_source_image->get_height() != dimension) { + working_image = p_source_image->duplicate(); + working_image->resize(dimension, dimension, Image::Interpolation::INTERPOLATE_LANCZOS); + } + + Vector png_buffer; + Error err = PNGDriverCommon::image_to_png(working_image, png_buffer); + if (err == OK) { + p_data.resize(png_buffer.size()); + memcpy(p_data.ptrw(), png_buffer.ptr(), p_data.size()); + } else { + String err_str = String("Failed to convert resized icon (") + p_file_name + ") to png."; + WARN_PRINT(err_str.utf8().get_data()); + } + } + + void load_icon_refs(const Ref &p_preset, Ref &icon, Ref &foreground, Ref &background) { + String project_icon_path = ProjectSettings::get_singleton()->get("application/config/icon"); + + icon.instance(); + foreground.instance(); + background.instance(); + + // Regular icon: user selection -> project icon -> default. + String path = static_cast(p_preset->get(launcher_icon_option)).strip_edges(); + if (path.empty() || ImageLoader::load_image(path, icon) != OK) { + ImageLoader::load_image(project_icon_path, icon); + } + + // Adaptive foreground: user selection -> regular icon (user selection -> project icon -> default). + path = static_cast(p_preset->get(launcher_adaptive_icon_foreground_option)).strip_edges(); + if (path.empty() || ImageLoader::load_image(path, foreground) != OK) { + foreground = icon; + } + + // Adaptive background: user selection -> default. + path = static_cast(p_preset->get(launcher_adaptive_icon_background_option)).strip_edges(); + if (!path.empty()) { + ImageLoader::load_image(path, background); + } + } + + void store_image(const LauncherIcon launcher_icon, const Vector &data) { + String img_path = launcher_icon.export_path; + img_path = img_path.insert(0, "res://android/build/"); + store_file_at_path(img_path, data); + } + + void _copy_icons_to_gradle_project(const Ref &p_preset, const Ref &main_image, + const Ref &foreground, const Ref &background) { + // Prepare images to be resized for the icons. If some image ends up being uninitialized, + // the default image from the export template will be used. + + for (int i = 0; i < icon_densities_count; ++i) { + if (main_image.is_valid() && !main_image->empty()) { + Vector data; + _process_launcher_icons(launcher_icons[i].export_path, main_image, launcher_icons[i].dimensions, data); + store_image(launcher_icons[i], data); } - Vector png_buffer; - Error err = PNGDriverCommon::image_to_png(working_image, png_buffer); - if (err == OK) { - p_data.resize(png_buffer.size()); - memcpy(p_data.ptrw(), png_buffer.ptr(), p_data.size()); - } else { - String err_str = String("Failed to convert resized icon (") + p_processing_file_name + ") to png."; - WARN_PRINT(err_str.utf8().get_data()); + if (foreground.is_valid() && !foreground->empty()) { + Vector data; + _process_launcher_icons(launcher_adaptive_icon_foregrounds[i].export_path, foreground, + launcher_adaptive_icon_foregrounds[i].dimensions, data); + store_image(launcher_adaptive_icon_foregrounds[i], data); + } + + if (background.is_valid() && !background->empty()) { + Vector data; + _process_launcher_icons(launcher_adaptive_icon_backgrounds[i].export_path, background, + launcher_adaptive_icon_backgrounds[i].dimensions, data); + store_image(launcher_adaptive_icon_backgrounds[i], data); } } } @@ -2006,6 +2065,12 @@ public: bool use_custom_build = bool(p_preset->get("custom_template/use_custom_build")); + Ref main_image; + Ref foreground; + Ref background; + + load_icon_refs(p_preset, main_image, foreground, background); + if (use_custom_build) { //re-generate build.gradle and AndroidManifest.xml { //test that installed build version is alright @@ -2028,6 +2093,9 @@ public: if (err != OK) { EditorNode::add_io_error("Unable to overwrite res://android/build/res/*.xml files with project name"); } + // Copies the project icon files into the appropriate Gradle project directory + _copy_icons_to_gradle_project(p_preset, main_image, foreground, background); + //build project if custom build is enabled String sdk_path = EDITOR_GET("export/android/custom_build_sdk_path"); @@ -2157,34 +2225,7 @@ public: Vector enabled_abis = get_enabled_abis(p_preset); - String project_icon_path = ProjectSettings::get_singleton()->get("application/config/icon"); - // Prepare images to be resized for the icons. If some image ends up being uninitialized, the default image from the export template will be used. - Ref launcher_icon_image; - Ref launcher_adaptive_icon_foreground_image; - Ref launcher_adaptive_icon_background_image; - - launcher_icon_image.instance(); - launcher_adaptive_icon_foreground_image.instance(); - launcher_adaptive_icon_background_image.instance(); - - // Regular icon: user selection -> project icon -> default. - String path = static_cast(p_preset->get(launcher_icon_option)).strip_edges(); - if (path.empty() || ImageLoader::load_image(path, launcher_icon_image) != OK) { - ImageLoader::load_image(project_icon_path, launcher_icon_image); - } - - // Adaptive foreground: user selection -> regular icon (user selection -> project icon -> default). - path = static_cast(p_preset->get(launcher_adaptive_icon_foreground_option)).strip_edges(); - if (path.empty() || ImageLoader::load_image(path, launcher_adaptive_icon_foreground_image) != OK) { - launcher_adaptive_icon_foreground_image = launcher_icon_image; - } - - // Adaptive background: user selection -> default. - path = static_cast(p_preset->get(launcher_adaptive_icon_background_option)).strip_edges(); - if (!path.empty()) { - ImageLoader::load_image(path, launcher_adaptive_icon_background_image); - } Vector invalid_abis(enabled_abis); while (ret == UNZ_OK) { @@ -2211,21 +2252,27 @@ public: _fix_manifest(p_preset, data, p_flags & (DEBUG_FLAG_DUMB_CLIENT | DEBUG_FLAG_REMOTE_DEBUG)); } - if (file == "resources.arsc") { - if (!use_custom_build) { + if (!use_custom_build) { + if (file == "resources.arsc") { _fix_resources(p_preset, data); } - } - for (int i = 0; i < icon_densities_count; ++i) { - if (launcher_icon_image.is_valid() && !launcher_icon_image->empty()) { - _process_launcher_icons(file, launcher_icon_image, launcher_icons[i], data); - } - if (launcher_adaptive_icon_foreground_image.is_valid() && !launcher_adaptive_icon_foreground_image->empty()) { - _process_launcher_icons(file, launcher_adaptive_icon_foreground_image, launcher_adaptive_icon_foregrounds[i], data); - } - if (launcher_adaptive_icon_background_image.is_valid() && !launcher_adaptive_icon_background_image->empty()) { - _process_launcher_icons(file, launcher_adaptive_icon_background_image, launcher_adaptive_icon_backgrounds[i], data); + for (int i = 0; i < icon_densities_count; ++i) { + if (main_image.is_valid() && !main_image->empty()) { + if (file == launcher_icons[i].export_path) { + _process_launcher_icons(file, main_image, launcher_icons[i].dimensions, data); + } + } + if (foreground.is_valid() && !foreground->empty()) { + if (file == launcher_adaptive_icon_foregrounds[i].export_path) { + _process_launcher_icons(file, foreground, launcher_adaptive_icon_foregrounds[i].dimensions, data); + } + } + if (background.is_valid() && !background->empty()) { + if (file == launcher_adaptive_icon_backgrounds[i].export_path) { + _process_launcher_icons(file, background, launcher_adaptive_icon_backgrounds[i].dimensions, data); + } + } } }