Skip to content

Commit

Permalink
Add VariantResource editor previews
Browse files Browse the repository at this point in the history
Only `Color` is supported for now.

Made sure to initialize via `EditorNode::add_init_callback`, else would crash.
  • Loading branch information
Xrayez committed Oct 31, 2020
1 parent 4e82218 commit 8f3d7d3
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
3 changes: 3 additions & 0 deletions core/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -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")
20 changes: 20 additions & 0 deletions core/register_core_types.cpp
Original file line number Diff line number Diff line change
@@ -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<VariantResourcePreviewGenerator> variant_resource_preview;
variant_resource_preview.instance();
EditorResourcePreview::get_singleton()->add_preview_generator(variant_resource_preview);
}
#endif

void register_core_types() {
ClassDB::register_class<ListNode>();
ClassDB::register_class<LinkedList>();

ClassDB::register_class<VariantResource>();
#ifdef TOOLS_ENABLED
EditorNode::add_init_callback(_variant_resource_preview_init);
#endif

#ifdef GOOST_IMAGE_ENABLED
register_image_types();
#endif
Expand Down
34 changes: 34 additions & 0 deletions core/types/editor/variant_resource_preview.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "variant_resource_preview.h"

bool VariantResourcePreviewGenerator::handles(const String &p_type) const {
return p_type == "VariantResource";
}

Ref<Texture> VariantResourcePreviewGenerator::generate(const Ref<Resource> &p_from, const Size2 &p_size) const {
Ref<VariantResource> var = p_from;
ERR_FAIL_COND_V_MSG(var.is_null(), Ref<VariantResource>(), "Invalid reference to a VariantResource object.");

const Variant &value = var->get(var->get_property_name());

if (value.get_type() == Variant::NIL) {
return Ref<Texture>();
}
Ref<Image> 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<Texture>();
};
}
Ref<ImageTexture> tex;
tex.instance();
tex->create_from_image(image, 0);

return tex;
}
18 changes: 18 additions & 0 deletions core/types/editor/variant_resource_preview.h
Original file line number Diff line number Diff line change
@@ -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<Texture> 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

0 comments on commit 8f3d7d3

Please sign in to comment.