From b731f18615719fa4efd7790933a0ff517dbafa6a Mon Sep 17 00:00:00 2001 From: Relintai Date: Tue, 22 Oct 2019 04:22:32 +0200 Subject: [PATCH] Work on an image resource class, and a custom importer. --- .../editor_plugin_packer_image_resource.cpp | 22 +++++++ .../editor_plugin_packer_image_resource.h | 25 ++++++++ texture_resource/packer_image_resource.cpp | 44 ++++++++++++++ texture_resource/packer_image_resource.h | 30 ++++++++++ .../packer_image_resource_importer.cpp | 59 +++++++++++++++++++ .../packer_image_resource_importer.h | 36 +++++++++++ 6 files changed, 216 insertions(+) create mode 100644 texture_resource/editor_plugin_packer_image_resource.cpp create mode 100644 texture_resource/editor_plugin_packer_image_resource.h create mode 100644 texture_resource/packer_image_resource.cpp create mode 100644 texture_resource/packer_image_resource.h create mode 100644 texture_resource/packer_image_resource_importer.cpp create mode 100644 texture_resource/packer_image_resource_importer.h diff --git a/texture_resource/editor_plugin_packer_image_resource.cpp b/texture_resource/editor_plugin_packer_image_resource.cpp new file mode 100644 index 0000000..1fa0aee --- /dev/null +++ b/texture_resource/editor_plugin_packer_image_resource.cpp @@ -0,0 +1,22 @@ +#include "editor_plugin_packer_image_resource.h" + +void EditorPluginPackerImageResource::_notification(int p_what) { + switch (p_what) { + case NOTIFICATION_ENTER_TREE: + _importer.instance(); + + add_import_plugin(_importer); + + break; + case NOTIFICATION_EXIT_TREE: + remove_import_plugin(_importer); + + _importer.unref(); + + break; + } +} + +EditorPluginPackerImageResource::EditorPluginPackerImageResource(EditorNode *node) { + _node = node; +} diff --git a/texture_resource/editor_plugin_packer_image_resource.h b/texture_resource/editor_plugin_packer_image_resource.h new file mode 100644 index 0000000..e557bdb --- /dev/null +++ b/texture_resource/editor_plugin_packer_image_resource.h @@ -0,0 +1,25 @@ + +#ifndef EDITOR_PLUGIN_PACKER_IMAGE_RESOURCE_H +#define EDITOR_PLUGIN_PACKER_IMAGE_RESOURCE_H + +#include "editor/editor_plugin.h" +#include "core/ustring.h" + +#include "packer_image_resource_importer.h" + +class EditorPluginPackerImageResource : public EditorPlugin { + + GDCLASS(EditorPluginPackerImageResource, EditorPlugin); + +public: + EditorPluginPackerImageResource(EditorNode *node); + +protected: + void _notification(int p_what); + +private: + EditorNode *_node; + Ref _importer; +}; + +#endif diff --git a/texture_resource/packer_image_resource.cpp b/texture_resource/packer_image_resource.cpp new file mode 100644 index 0000000..4ea1176 --- /dev/null +++ b/texture_resource/packer_image_resource.cpp @@ -0,0 +1,44 @@ +#include "packer_image_resource.h" + +void PackerImageResource::set_data(const Ref &p_image) { + ERR_FAIL_COND(p_image.is_null()); + + _image = p_image; +} + +void PackerImageResource::_resource_path_changed() { + + String path = get_path(); +} + +Ref PackerImageResource::get_data() const { + return _image; +} + +int PackerImageResource::get_width() const { + return w; +} + +int PackerImageResource::get_height() const { + return h; +} + +void PackerImageResource::_set_data(Dictionary p_data) { + + Ref img = p_data["image"]; + ERR_FAIL_COND(!img.is_valid()); + uint32_t flags = p_data["flags"]; +}; + +void PackerImageResource::_bind_methods() { + + ClassDB::bind_method(D_METHOD("set_data", "image"), &PackerImageResource::set_data); +} + +PackerImageResource::PackerImageResource() { + w = h = 0; +} + +PackerImageResource::~PackerImageResource() { + _image.unref(); +} diff --git a/texture_resource/packer_image_resource.h b/texture_resource/packer_image_resource.h new file mode 100644 index 0000000..7d1c610 --- /dev/null +++ b/texture_resource/packer_image_resource.h @@ -0,0 +1,30 @@ +#ifndef PACKER_IMAGE_REOURCE_H +#define PACKER_IMAGE_REOURCE_H + +#include "core/image.h" +#include "scene/resources/texture.h" + +class PackerImageResource : public Texture { + + GDCLASS(PackerImageResource, Texture); + + RES_BASE_EXTENSION("tres"); + +public: + void set_data(const Ref &p_image); + Ref get_data() const; + + int get_width() const; + int get_height() const; + + ImageTexture(); + ~ImageTexture(); + +protected: + static void _bind_methods(); + +private: + Ref _image; +}; + +#endif diff --git a/texture_resource/packer_image_resource_importer.cpp b/texture_resource/packer_image_resource_importer.cpp new file mode 100644 index 0000000..5e35e33 --- /dev/null +++ b/texture_resource/packer_image_resource_importer.cpp @@ -0,0 +1,59 @@ +#include "editor_import_collada_mdr.h" + +String PackerImageResourceImporter::get_importer_name() const { + return "packer_image_resource"; +} + +String PackerImageResourceImporter::get_visible_name() const { + return "Packer Image Resource"; +} + +void PackerImageResourceImporter::get_recognized_extensions(List *p_extensions) const { + ImageLoader::get_recognized_extensions(p_extensions); +} + +String PackerImageResourceImporter::get_save_extension() const { + return "tres"; +} + +String PackerImageResourceImporter::get_resource_type() const { + return "PackerImageResource"; +} + +float PackerImageResourceImporter::get_priority() const { + return 1.0; +} + +int PackerImageResourceImporter::get_preset_count() const { + return 0; +} + +String PackerImageResourceImporter::get_preset_name(int p_idx) const { + return ""; +} + +void PackerImageResourceImporter::get_import_options(List *r_options, int p_preset) const { +} + +bool PackerImageResourceImporter::get_option_visibility(const String &p_option, const Map &p_options) const { + return true; +} + +Error PackerImageResourceImporter::import(const String &p_source_file, const String &p_save_path, const Map &p_options, List *r_platform_variants, List *r_gen_files, Variant *r_metadata) { + Ref image; + image.instance(); + Error err = ImageLoader::load_image(p_source_file, image, NULL, hdr_as_srgb, scale); + + if (err != OK) + return err; + + +} + +PackerImageResourceImporter::PackerImageResourceImporter() { + _importer.instance(); +} + +PackerImageResourceImporter::~PackerImageResourceImporter() { + _importer.unref(); +} diff --git a/texture_resource/packer_image_resource_importer.h b/texture_resource/packer_image_resource_importer.h new file mode 100644 index 0000000..689ca17 --- /dev/null +++ b/texture_resource/packer_image_resource_importer.h @@ -0,0 +1,36 @@ + +#ifndef EDITOR_IMPORT_COLLADA_MDR +#define EDITOR_IMPORT_COLLADA_MDR + +#include "editor/import/editor_import_plugin.h" +#include "core/ustring.h" + + +class PackerImageResourceImporter : public EditorImportPlugin { + + GDCLASS(PackerImageResourceImporter, EditorImportPlugin); + +public: + virtual String get_importer_name() const; + virtual String get_visible_name() const; + virtual void get_recognized_extensions(List *p_extensions) const; + virtual String get_save_extension() const; + virtual String get_resource_type() const; + virtual float get_priority() const; + + virtual int get_preset_count() const; + virtual String get_preset_name(int p_idx) const; + + virtual void get_import_options(List *r_options, int p_preset = 0) const; + virtual bool get_option_visibility(const String &p_option, const Map &p_options) const; + + virtual Error import(const String &p_source_file, const String &p_save_path, const Map &p_options, List *r_platform_variants, List *r_gen_files = NULL, Variant *r_metadata = NULL); + + PackerImageResourceImporter(); + ~PackerImageResourceImporter(); + +private: + Ref _importer; +}; + +#endif