You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2026-01-06 19:41:11 +00:00
Change preview methods to take Callable
This commit is contained in:
@@ -82,7 +82,7 @@ void EditorResourcePicker::_update_resource() {
|
||||
assign_button->set_tooltip_text(resource_path + TTR("Type:") + " " + class_name);
|
||||
|
||||
// Preview will override the above, so called at the end.
|
||||
EditorResourcePreview::get_singleton()->queue_edited_resource_preview(edited_resource, this, "_update_resource_preview", edited_resource->get_instance_id());
|
||||
EditorResourcePreview::get_singleton()->queue_edited_resource_preview(edited_resource, callable_mp(this, &EditorResourcePicker::_update_resource_preview).bind(edited_resource->get_instance_id()));
|
||||
}
|
||||
} else if (edited_resource.is_valid()) {
|
||||
assign_button->set_tooltip_text(resource_path + TTR("Type:") + " " + edited_resource->get_class());
|
||||
@@ -886,8 +886,6 @@ void EditorResourcePicker::drop_data_fw(const Point2 &p_point, const Variant &p_
|
||||
}
|
||||
|
||||
void EditorResourcePicker::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("_update_resource_preview"), &EditorResourcePicker::_update_resource_preview);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("set_base_type", "base_type"), &EditorResourcePicker::set_base_type);
|
||||
ClassDB::bind_method(D_METHOD("get_base_type"), &EditorResourcePicker::get_base_type);
|
||||
ClassDB::bind_method(D_METHOD("get_allowed_types"), &EditorResourcePicker::get_allowed_types);
|
||||
|
||||
@@ -132,7 +132,7 @@ void EditorResourcePreview::_thread_func(void *ud) {
|
||||
erp->_thread();
|
||||
}
|
||||
|
||||
void EditorResourcePreview::_preview_ready(const String &p_path, int p_hash, const Ref<Texture2D> &p_texture, const Ref<Texture2D> &p_small_texture, ObjectID id, const StringName &p_func, const Variant &p_ud, const Dictionary &p_metadata) {
|
||||
void EditorResourcePreview::_preview_ready(const String &p_path, int p_hash, const Ref<Texture2D> &p_texture, const Ref<Texture2D> &p_small_texture, const Callable &p_callback, const Dictionary &p_metadata) {
|
||||
{
|
||||
MutexLock lock(preview_mutex);
|
||||
|
||||
@@ -155,8 +155,7 @@ void EditorResourcePreview::_preview_ready(const String &p_path, int p_hash, con
|
||||
|
||||
cache[p_path] = item;
|
||||
}
|
||||
|
||||
Callable(id, p_func).call_deferred(p_path, p_texture, p_small_texture, p_ud);
|
||||
p_callback.call_deferred(p_path, p_texture, p_small_texture);
|
||||
}
|
||||
|
||||
void EditorResourcePreview::_generate_preview(Ref<ImageTexture> &r_texture, Ref<ImageTexture> &r_small_texture, const QueueItem &p_item, const String &cache_base, Dictionary &p_metadata) {
|
||||
@@ -285,7 +284,7 @@ void EditorResourcePreview::_iterate() {
|
||||
if (cache.has(item.path)) {
|
||||
Item cached_item = cache[item.path];
|
||||
// Already has it because someone loaded it, just let it know it's ready.
|
||||
_preview_ready(item.path, cached_item.last_hash, cached_item.preview, cached_item.small_preview, item.id, item.function, item.userdata, cached_item.preview_metadata);
|
||||
_preview_ready(item.path, cached_item.last_hash, cached_item.preview, cached_item.small_preview, item.callback, cached_item.preview_metadata);
|
||||
preview_mutex.unlock();
|
||||
return;
|
||||
}
|
||||
@@ -300,7 +299,7 @@ void EditorResourcePreview::_iterate() {
|
||||
if (item.resource.is_valid()) {
|
||||
Dictionary preview_metadata;
|
||||
_generate_preview(texture, small_texture, item, String(), preview_metadata);
|
||||
_preview_ready(item.path, item.resource->hash_edited_version_for_preview(), texture, small_texture, item.id, item.function, item.userdata, preview_metadata);
|
||||
_preview_ready(item.path, item.resource->hash_edited_version_for_preview(), texture, small_texture, item.callback, preview_metadata);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -388,7 +387,7 @@ void EditorResourcePreview::_iterate() {
|
||||
_generate_preview(texture, small_texture, item, cache_base, preview_metadata);
|
||||
}
|
||||
}
|
||||
_preview_ready(item.path, 0, texture, small_texture, item.id, item.function, item.userdata, preview_metadata);
|
||||
_preview_ready(item.path, 0, texture, small_texture, item.callback, preview_metadata);
|
||||
}
|
||||
|
||||
void EditorResourcePreview::_write_preview_cache(Ref<FileAccess> p_file, int p_thumbnail_size, bool p_has_small_texture, uint64_t p_modified_time, const String &p_hash, const Dictionary &p_metadata) {
|
||||
@@ -457,8 +456,12 @@ EditorResourcePreview::PreviewItem EditorResourcePreview::get_resource_preview_i
|
||||
return item;
|
||||
}
|
||||
|
||||
void EditorResourcePreview::queue_edited_resource_preview(const Ref<Resource> &p_res, Object *p_receiver, const StringName &p_receiver_func, const Variant &p_userdata) {
|
||||
void EditorResourcePreview::_queue_edited_resource_preview(const Ref<Resource> &p_res, Object *p_receiver, const StringName &p_receiver_func, const Variant &p_userdata) {
|
||||
ERR_FAIL_NULL(p_receiver);
|
||||
queue_edited_resource_preview(p_res, Callable(p_receiver, p_receiver_func).bind(p_userdata));
|
||||
}
|
||||
|
||||
void EditorResourcePreview::queue_edited_resource_preview(const Ref<Resource> &p_res, const Callable &p_callback) {
|
||||
ERR_FAIL_COND(p_res.is_null());
|
||||
_update_thumbnail_sizes();
|
||||
|
||||
@@ -466,44 +469,46 @@ void EditorResourcePreview::queue_edited_resource_preview(const Ref<Resource> &p
|
||||
MutexLock lock(preview_mutex);
|
||||
|
||||
String path_id = "ID:" + itos(p_res->get_instance_id());
|
||||
HashMap<String, EditorResourcePreview::Item>::Iterator I = cache.find(path_id);
|
||||
|
||||
if (cache.has(path_id) && cache[path_id].last_hash == p_res->hash_edited_version_for_preview()) {
|
||||
p_receiver->call(p_receiver_func, path_id, cache[path_id].preview, cache[path_id].small_preview, p_userdata);
|
||||
if (I && I->value.last_hash == p_res->hash_edited_version_for_preview()) {
|
||||
p_callback.call(path_id, I->value.preview, I->value.small_preview);
|
||||
return;
|
||||
}
|
||||
|
||||
cache.erase(path_id); //erase if exists, since it will be regen
|
||||
if (I) {
|
||||
cache.remove(I); // Erase if exists, since it will be regen.
|
||||
}
|
||||
|
||||
QueueItem item;
|
||||
item.function = p_receiver_func;
|
||||
item.id = p_receiver->get_instance_id();
|
||||
item.resource = p_res;
|
||||
item.path = path_id;
|
||||
item.userdata = p_userdata;
|
||||
|
||||
item.callback = p_callback;
|
||||
queue.push_back(item);
|
||||
}
|
||||
preview_sem.post();
|
||||
}
|
||||
|
||||
void EditorResourcePreview::queue_resource_preview(const String &p_path, Object *p_receiver, const StringName &p_receiver_func, const Variant &p_userdata) {
|
||||
void EditorResourcePreview::_queue_resource_preview(const String &p_path, Object *p_receiver, const StringName &p_receiver_func, const Variant &p_userdata) {
|
||||
ERR_FAIL_NULL(p_receiver);
|
||||
queue_resource_preview(p_path, Callable(p_receiver, p_receiver_func).bind(p_userdata));
|
||||
}
|
||||
|
||||
void EditorResourcePreview::queue_resource_preview(const String &p_path, const Callable &p_callback) {
|
||||
_update_thumbnail_sizes();
|
||||
|
||||
{
|
||||
MutexLock lock(preview_mutex);
|
||||
|
||||
if (cache.has(p_path)) {
|
||||
p_receiver->call(p_receiver_func, p_path, cache[p_path].preview, cache[p_path].small_preview, p_userdata);
|
||||
const Item *cached_item = cache.getptr(p_path);
|
||||
if (cached_item) {
|
||||
p_callback.call(p_path, cached_item->preview, cached_item->small_preview);
|
||||
return;
|
||||
}
|
||||
|
||||
QueueItem item;
|
||||
item.function = p_receiver_func;
|
||||
item.id = p_receiver->get_instance_id();
|
||||
item.path = p_path;
|
||||
item.userdata = p_userdata;
|
||||
|
||||
item.callback = p_callback;
|
||||
queue.push_back(item);
|
||||
}
|
||||
preview_sem.post();
|
||||
@@ -522,8 +527,8 @@ EditorResourcePreview *EditorResourcePreview::get_singleton() {
|
||||
}
|
||||
|
||||
void EditorResourcePreview::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("queue_resource_preview", "path", "receiver", "receiver_func", "userdata"), &EditorResourcePreview::queue_resource_preview);
|
||||
ClassDB::bind_method(D_METHOD("queue_edited_resource_preview", "resource", "receiver", "receiver_func", "userdata"), &EditorResourcePreview::queue_edited_resource_preview);
|
||||
ClassDB::bind_method(D_METHOD("queue_resource_preview", "path", "receiver", "receiver_func", "userdata"), &EditorResourcePreview::_queue_resource_preview);
|
||||
ClassDB::bind_method(D_METHOD("queue_edited_resource_preview", "resource", "receiver", "receiver_func", "userdata"), &EditorResourcePreview::_queue_edited_resource_preview);
|
||||
ClassDB::bind_method(D_METHOD("add_preview_generator", "generator"), &EditorResourcePreview::add_preview_generator);
|
||||
ClassDB::bind_method(D_METHOD("remove_preview_generator", "generator"), &EditorResourcePreview::remove_preview_generator);
|
||||
ClassDB::bind_method(D_METHOD("check_for_invalidation", "path"), &EditorResourcePreview::check_for_invalidation);
|
||||
|
||||
@@ -79,9 +79,7 @@ class EditorResourcePreview : public Node {
|
||||
struct QueueItem {
|
||||
Ref<Resource> resource;
|
||||
String path;
|
||||
ObjectID id;
|
||||
StringName function;
|
||||
Variant userdata;
|
||||
Callable callback;
|
||||
};
|
||||
|
||||
List<QueueItem> queue;
|
||||
@@ -102,7 +100,7 @@ class EditorResourcePreview : public Node {
|
||||
|
||||
HashMap<String, Item> cache;
|
||||
|
||||
void _preview_ready(const String &p_path, int p_hash, const Ref<Texture2D> &p_texture, const Ref<Texture2D> &p_small_texture, ObjectID id, const StringName &p_func, const Variant &p_ud, const Dictionary &p_metadata);
|
||||
void _preview_ready(const String &p_path, int p_hash, const Ref<Texture2D> &p_texture, const Ref<Texture2D> &p_small_texture, const Callable &p_callback, const Dictionary &p_metadata);
|
||||
void _generate_preview(Ref<ImageTexture> &r_texture, Ref<ImageTexture> &r_small_texture, const QueueItem &p_item, const String &cache_base, Dictionary &p_metadata);
|
||||
|
||||
int small_thumbnail_size = -1;
|
||||
@@ -119,6 +117,10 @@ class EditorResourcePreview : public Node {
|
||||
|
||||
void _update_thumbnail_sizes();
|
||||
|
||||
// TODO: These should be deprecated and the new methods exposed instead.
|
||||
void _queue_resource_preview(const String &p_path, Object *p_receiver, const StringName &p_receiver_func, const Variant &p_userdata);
|
||||
void _queue_edited_resource_preview(const Ref<Resource> &p_res, Object *p_receiver, const StringName &p_receiver_func, const Variant &p_userdata);
|
||||
|
||||
protected:
|
||||
void _notification(int p_what);
|
||||
static void _bind_methods();
|
||||
@@ -131,10 +133,8 @@ public:
|
||||
Ref<Texture2D> small_preview;
|
||||
};
|
||||
|
||||
// p_receiver_func callback has signature (String p_path, Ref<Texture2D> p_preview, Ref<Texture2D> p_preview_small, Variant p_userdata)
|
||||
// p_preview will be null if there was an error
|
||||
void queue_resource_preview(const String &p_path, Object *p_receiver, const StringName &p_receiver_func, const Variant &p_userdata);
|
||||
void queue_edited_resource_preview(const Ref<Resource> &p_res, Object *p_receiver, const StringName &p_receiver_func, const Variant &p_userdata);
|
||||
void queue_resource_preview(const String &p_path, const Callable &p_callback);
|
||||
void queue_edited_resource_preview(const Ref<Resource> &p_res, const Callable &p_callback);
|
||||
const Dictionary get_preview_metadata(const String &p_path) const;
|
||||
|
||||
PreviewItem get_resource_preview_if_available(const String &p_path);
|
||||
|
||||
@@ -39,19 +39,14 @@
|
||||
#include "scene/gui/label.h"
|
||||
#include "scene/gui/texture_rect.h"
|
||||
|
||||
void EditorResourceTooltipPlugin::_thumbnail_ready(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, const Variant &p_udata) {
|
||||
ObjectID trid = p_udata;
|
||||
TextureRect *tr = ObjectDB::get_instance<TextureRect>(trid);
|
||||
|
||||
if (!tr) {
|
||||
return;
|
||||
void EditorResourceTooltipPlugin::_thumbnail_ready(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, ObjectID p_trect_id) {
|
||||
TextureRect *tr = ObjectDB::get_instance<TextureRect>(p_trect_id);
|
||||
if (tr) {
|
||||
tr->set_texture(p_preview);
|
||||
}
|
||||
|
||||
tr->set_texture(p_preview);
|
||||
}
|
||||
|
||||
void EditorResourceTooltipPlugin::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("_thumbnail_ready"), &EditorResourceTooltipPlugin::_thumbnail_ready);
|
||||
ClassDB::bind_method(D_METHOD("request_thumbnail", "path", "control"), &EditorResourceTooltipPlugin::request_thumbnail);
|
||||
|
||||
GDVIRTUAL_BIND(_handles, "type");
|
||||
@@ -95,7 +90,7 @@ VBoxContainer *EditorResourceTooltipPlugin::make_default_tooltip(const String &p
|
||||
|
||||
void EditorResourceTooltipPlugin::request_thumbnail(const String &p_path, TextureRect *p_for_control) const {
|
||||
ERR_FAIL_NULL(p_for_control);
|
||||
EditorResourcePreview::get_singleton()->queue_resource_preview(p_path, const_cast<EditorResourceTooltipPlugin *>(this), "_thumbnail_ready", p_for_control->get_instance_id());
|
||||
EditorResourcePreview::get_singleton()->queue_resource_preview(p_path, callable_mp(const_cast<EditorResourceTooltipPlugin *>(this), &EditorResourceTooltipPlugin::_thumbnail_ready).bind(p_for_control->get_instance_id()));
|
||||
}
|
||||
|
||||
bool EditorResourceTooltipPlugin::handles(const String &p_resource_type) const {
|
||||
|
||||
@@ -41,7 +41,7 @@ class VBoxContainer;
|
||||
class EditorResourceTooltipPlugin : public RefCounted {
|
||||
GDCLASS(EditorResourceTooltipPlugin, RefCounted);
|
||||
|
||||
void _thumbnail_ready(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, const Variant &p_udata);
|
||||
void _thumbnail_ready(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, ObjectID p_trect_id);
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
Reference in New Issue
Block a user