summaryrefslogtreecommitdiff
path: root/tools/editor/editor_resource_preview.h
blob: 13c3d51313c56d8e4e7a5cb71e936b23c934bbc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#ifndef EDITORRESOURCEPREVIEW_H
#define EDITORRESOURCEPREVIEW_H

#include "scene/main/node.h"
#include "os/semaphore.h"
#include "os/thread.h"
#include "scene/resources/texture.h"

/* make previews for:
*packdscene
*wav
*image
*mesh
-font
*script
*material
-shader
-shader graph?
-navigation mesh
-collision?
-occluder polygon
-navigation polygon
-tileset
-curve and curve2D
*/


class EditorResourcePreviewGenerator : public Reference {

	OBJ_TYPE(EditorResourcePreviewGenerator,Reference );

public:

	virtual bool handles(const String& p_type) const=0;
	virtual Ref<Texture> generate(const RES& p_from)=0;
	virtual Ref<Texture> generate_from_path(const String& p_path);

	EditorResourcePreviewGenerator();
};


class EditorResourcePreview : public Node {

	OBJ_TYPE(EditorResourcePreview,Node);


	static EditorResourcePreview* singleton;

	struct QueueItem {
		Ref<Resource> resource;
		String path;
		ObjectID id;
		StringName function;
		Variant userdata;
	};

	List<QueueItem> queue;

	Mutex *preview_mutex;
	Semaphore *preview_sem;
	Thread *thread;
	bool exit;

	struct Item {
		Ref<Texture> preview;
		int order;
		uint32_t last_hash;
	};

	int order;

	Map<String,Item> cache;

	void _preview_ready(const String& p_str,const Ref<Texture>& p_texture, ObjectID id, const StringName &p_func, const Variant &p_ud);
	Ref<Texture> _generate_preview(const QueueItem& p_item, const String &cache_base);

	static void _thread_func(void *ud);
	void _thread();

	Vector<Ref<EditorResourcePreviewGenerator> > preview_generators;
protected:

	static void _bind_methods();
public:

	static EditorResourcePreview* get_singleton();

	//callback funtion is callback(String p_path,Ref<Texture> preview,Variant udata) preview null if could not load
	void queue_resource_preview(const String& p_res, Object* p_receiver, const StringName& p_receiver_func, const Variant& p_userdata);
	void queue_edited_resource_preview(const Ref<Resource>& p_path, Object* p_receiver, const StringName& p_receiver_func, const Variant& p_userdata);

	void add_preview_generator(const Ref<EditorResourcePreviewGenerator>& p_generator);

	EditorResourcePreview();
	~EditorResourcePreview();
};

#endif // EDITORRESOURCEPREVIEW_H