diff --git a/core/SCsub b/core/SCsub index 31f9a49e..d0a847f2 100644 --- a/core/SCsub +++ b/core/SCsub @@ -10,3 +10,6 @@ if env["goost_math_enabled"]: env_goost.add_source_files(env.modules_sources, "*.cpp") env_goost.add_source_files(env.modules_sources, "types/*.cpp") + +if env["tools"]: + env_goost.add_source_files(env.modules_sources, "types/editor/*.cpp") diff --git a/core/register_core_types.cpp b/core/register_core_types.cpp index ef74dff4..d73ff0ef 100644 --- a/core/register_core_types.cpp +++ b/core/register_core_types.cpp @@ -1,17 +1,37 @@ #include "register_core_types.h" +#include "core/engine.h" + #include "image/register_image_types.h" #include "math/register_math_types.h" #include "types/list.h" #include "types/variant_resource.h" +#ifdef TOOLS_ENABLED +#include "editor/editor_node.h" +#include "editor/editor_resource_preview.h" +#include "types/editor/variant_resource_preview.h" +#endif + namespace goost { +#ifdef TOOLS_ENABLED +static void _variant_resource_preview_init() { + Ref variant_resource_preview; + variant_resource_preview.instance(); + EditorResourcePreview::get_singleton()->add_preview_generator(variant_resource_preview); +} +#endif + void register_core_types() { ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class(); +#ifdef TOOLS_ENABLED + EditorNode::add_init_callback(_variant_resource_preview_init); +#endif + #ifdef GOOST_IMAGE_ENABLED register_image_types(); #endif diff --git a/core/types/editor/variant_resource_preview.cpp b/core/types/editor/variant_resource_preview.cpp new file mode 100644 index 00000000..a5c8c30d --- /dev/null +++ b/core/types/editor/variant_resource_preview.cpp @@ -0,0 +1,34 @@ +#include "variant_resource_preview.h" + +bool VariantResourcePreviewGenerator::handles(const String &p_type) const { + return p_type == "VariantResource"; +} + +Ref VariantResourcePreviewGenerator::generate(const Ref &p_from, const Size2 &p_size) const { + Ref var = p_from; + ERR_FAIL_COND_V_MSG(var.is_null(), Ref(), "Invalid reference to a VariantResource object."); + + const Variant &value = var->get(var->get_property_name()); + + if (value.get_type() == Variant::NIL) { + return Ref(); + } + Ref image; + image.instance(); + image->create(p_size.x, p_size.y, false, Image::FORMAT_RGBA8); + + switch (value.get_type()) { + case Variant::COLOR: { + Color color = value; + image->fill(color); + } break; + default: { + return Ref(); + }; + } + Ref tex; + tex.instance(); + tex->create_from_image(image, 0); + + return tex; +} diff --git a/core/types/editor/variant_resource_preview.h b/core/types/editor/variant_resource_preview.h new file mode 100644 index 00000000..5bacb326 --- /dev/null +++ b/core/types/editor/variant_resource_preview.h @@ -0,0 +1,18 @@ +#ifndef VARIANT_RESOURCE_PREVIEW_H +#define VARIANT_RESOURCE_PREVIEW_H + +#include "editor/editor_resource_preview.h" +#include "../variant_resource.h" + +class VariantResourcePreviewGenerator : public EditorResourcePreviewGenerator { + GDCLASS(VariantResourcePreviewGenerator, EditorResourcePreviewGenerator); + +public: + virtual bool handles(const String &p_type) const; + virtual Ref generate(const RES &p_from, const Size2 &p_size) const; + + virtual bool generate_small_preview_automatically() const { return true; }; + virtual bool can_generate_small_preview() const { return true; }; +}; + +#endif // VARIANT_RESOURCE_PREVIEW_H