Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for disabling per-filetype ImageFormatLoader import #99666

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion core/io/image_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ Error ImageFormatLoaderExtension::load_image(Ref<Image> p_image, Ref<FileAccess>
return err;
}

bool ImageFormatLoaderExtension::should_import() const {
bool import = true;
GDVIRTUAL_CALL(_should_import, import);
return import;
}

void ImageFormatLoaderExtension::get_recognized_extensions(List<String> *p_extension) const {
PackedStringArray ext;
if (GDVIRTUAL_CALL(_get_recognized_extensions, ext)) {
Expand All @@ -76,6 +82,7 @@ void ImageFormatLoaderExtension::remove_format_loader() {
void ImageFormatLoaderExtension::_bind_methods() {
GDVIRTUAL_BIND(_get_recognized_extensions);
GDVIRTUAL_BIND(_load_image, "image", "fileaccess", "flags", "scale");
GDVIRTUAL_BIND(_should_import);
ClassDB::bind_method(D_METHOD("add_format_loader"), &ImageFormatLoaderExtension::add_format_loader);
ClassDB::bind_method(D_METHOD("remove_format_loader"), &ImageFormatLoaderExtension::remove_format_loader);
}
Expand Down Expand Up @@ -110,8 +117,11 @@ Error ImageLoader::load_image(const String &p_file, Ref<Image> p_image, Ref<File
return ERR_FILE_UNRECOGNIZED;
}

void ImageLoader::get_recognized_extensions(List<String> *p_extensions) {
void ImageLoader::get_recognized_extensions(List<String> *p_extensions, bool p_is_import) {
for (int i = 0; i < loader.size(); i++) {
if (p_is_import && !loader[i]->should_import()) {
continue;
}
loader[i]->get_recognized_extensions(p_extensions);
}
}
Expand Down
5 changes: 4 additions & 1 deletion core/io/image_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class ImageFormatLoader : public RefCounted {

virtual Error load_image(Ref<Image> p_image, Ref<FileAccess> p_fileaccess, BitField<ImageFormatLoader::LoaderFlags> p_flags = FLAG_NONE, float p_scale = 1.0) = 0;
virtual void get_recognized_extensions(List<String> *p_extensions) const = 0;
virtual bool should_import() const { return true; }
bool recognize(const String &p_extension) const;

public:
Expand All @@ -76,13 +77,15 @@ class ImageFormatLoaderExtension : public ImageFormatLoader {

public:
virtual Error load_image(Ref<Image> p_image, Ref<FileAccess> p_fileaccess, BitField<ImageFormatLoader::LoaderFlags> p_flags = FLAG_NONE, float p_scale = 1.0) override;
virtual bool should_import() const override;
virtual void get_recognized_extensions(List<String> *p_extensions) const override;

void add_format_loader();
void remove_format_loader();

GDVIRTUAL0RC(PackedStringArray, _get_recognized_extensions);
GDVIRTUAL4R(Error, _load_image, Ref<Image>, Ref<FileAccess>, BitField<ImageFormatLoader::LoaderFlags>, float);
GDVIRTUAL0RC(bool, _should_import);
};

class ImageLoader {
Expand All @@ -92,7 +95,7 @@ class ImageLoader {
protected:
public:
static Error load_image(const String &p_file, Ref<Image> p_image, Ref<FileAccess> p_custom = Ref<FileAccess>(), BitField<ImageFormatLoader::LoaderFlags> p_flags = ImageFormatLoader::FLAG_NONE, float p_scale = 1.0);
static void get_recognized_extensions(List<String> *p_extensions);
static void get_recognized_extensions(List<String> *p_extensions, bool p_is_import = true);
static Ref<ImageFormatLoader> recognize(const String &p_extension);

static void add_image_format_loader(Ref<ImageFormatLoader> p_loader);
Expand Down
6 changes: 6 additions & 0 deletions doc/classes/ImageFormatLoaderExtension.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
Loads the content of [param fileaccess] into the provided [param image].
</description>
</method>
<method name="_should_import" qualifiers="virtual const">
<return type="bool" />
<description>
Returns [code]true[/code] if image file importing should be performed for this image format's extensions, else returns [code]false[/code].
</description>
</method>
<method name="add_format_loader">
<return type="void" />
<description>
Expand Down
2 changes: 1 addition & 1 deletion editor/plugins/particles_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ Particles2DEditorPlugin::Particles2DEditorPlugin() {
file = memnew(EditorFileDialog);

List<String> ext;
ImageLoader::get_recognized_extensions(&ext);
ImageLoader::get_recognized_extensions(&ext, false);
for (const String &E : ext) {
file->add_filter("*." + E, E.to_upper());
}
Expand Down