Small rework of tooltip plugins

This commit is contained in:
kobewi 2023-05-18 17:03:22 +02:00
parent 6101240231
commit 5baebf75f2
5 changed files with 26 additions and 33 deletions

View File

@ -18,20 +18,23 @@
</description> </description>
</method> </method>
<method name="_make_tooltip_for_path" qualifiers="virtual const"> <method name="_make_tooltip_for_path" qualifiers="virtual const">
<return type="Object" /> <return type="Control" />
<param index="0" name="path" type="String" /> <param index="0" name="path" type="String" />
<param index="1" name="metadata" type="Dictionary" /> <param index="1" name="metadata" type="Dictionary" />
<param index="2" name="base" type="Control" />
<description> <description>
Create and return a tooltip that will be displayed when the user hovers resource under given [param path] in filesystem dock. For best results, use [method make_default_tooltip] as a base. Create and return a tooltip that will be displayed when the user hovers a resource under the given [param path] in filesystem dock.
The [param metadata] dictionary is provided by preview generator (see method EditorResourcePreviewGenerator._generate]). The [param metadata] dictionary is provided by preview generator (see method EditorResourcePreviewGenerator._generate]).
[param base] is the base default tooltip, which is a [VBoxContainer] with a file name, type and size labels. If another plugin handled the same file type, [param base] will be output from the previous plugin. For best result, make sure the base tooltip is part of the returned [Control].
[b]Note:[/b] It's unadvised to use [method ResourceLoader.load], especially with heavy resources like models or textures, because it will make the editor unresponsive when creating the tooltip. You can use [method request_thumbnail] if you want to display a preview in your tooltip. [b]Note:[/b] It's unadvised to use [method ResourceLoader.load], especially with heavy resources like models or textures, because it will make the editor unresponsive when creating the tooltip. You can use [method request_thumbnail] if you want to display a preview in your tooltip.
</description> [b]Note:[/b] If you decide to discard the [param base], make sure to call [method Node.queue_free], because it's not freed automatically.
</method> [codeblock]
<method name="make_default_tooltip" qualifiers="static"> func _make_tooltip_for_path(path, metadata, base):
<return type="VBoxContainer" /> var t_rect = TextureRect.new()
<param index="0" name="path" type="String" /> request_thumbnail(path, t_rect)
<description> base.add_child(t_rect) # The TextureRect will appear at the bottom of the tooltip.
Creates a default file tooltip. The tooltip includes file name, file size and [Resource] type if available. return base
[/codeblock]
</description> </description>
</method> </method>
<method name="request_thumbnail" qualifiers="const"> <method name="request_thumbnail" qualifiers="const">

View File

@ -2291,20 +2291,12 @@ Control *FileSystemDock::create_tooltip_for_path(const String &p_path) const {
} }
const String type = ResourceLoader::get_resource_type(p_path); const String type = ResourceLoader::get_resource_type(p_path);
Control *tooltip = nullptr; Control *tooltip = EditorResourceTooltipPlugin::make_default_tooltip(p_path);
for (const Ref<EditorResourceTooltipPlugin> &plugin : tooltip_plugins) { for (const Ref<EditorResourceTooltipPlugin> &plugin : tooltip_plugins) {
if (plugin->handles(type)) { if (plugin->handles(type)) {
tooltip = plugin->make_tooltip_for_path(p_path, EditorResourcePreview::get_singleton()->get_preview_metadata(p_path)); tooltip = plugin->make_tooltip_for_path(p_path, EditorResourcePreview::get_singleton()->get_preview_metadata(p_path), tooltip);
} }
if (tooltip) {
break;
}
}
if (!tooltip) {
tooltip = EditorResourceTooltipPlugin::make_default_tooltip(p_path);
} }
return tooltip; return tooltip;
} }

View File

@ -33,7 +33,6 @@
#include "editor/editor_resource_preview.h" #include "editor/editor_resource_preview.h"
#include "editor/editor_scale.h" #include "editor/editor_scale.h"
#include "scene/gui/box_container.h" #include "scene/gui/box_container.h"
#include "scene/gui/control.h"
#include "scene/gui/label.h" #include "scene/gui/label.h"
#include "scene/gui/texture_rect.h" #include "scene/gui/texture_rect.h"
@ -50,12 +49,10 @@ void EditorResourceTooltipPlugin::_thumbnail_ready(const String &p_path, const R
void EditorResourceTooltipPlugin::_bind_methods() { void EditorResourceTooltipPlugin::_bind_methods() {
ClassDB::bind_method(D_METHOD("_thumbnail_ready"), &EditorResourceTooltipPlugin::_thumbnail_ready); ClassDB::bind_method(D_METHOD("_thumbnail_ready"), &EditorResourceTooltipPlugin::_thumbnail_ready);
ClassDB::bind_static_method("EditorResourceTooltipPlugin", D_METHOD("make_default_tooltip", "path"), &EditorResourceTooltipPlugin::make_default_tooltip);
ClassDB::bind_method(D_METHOD("request_thumbnail", "path", "control"), &EditorResourceTooltipPlugin::request_thumbnail); ClassDB::bind_method(D_METHOD("request_thumbnail", "path", "control"), &EditorResourceTooltipPlugin::request_thumbnail);
GDVIRTUAL_BIND(_handles, "type"); GDVIRTUAL_BIND(_handles, "type");
GDVIRTUAL_BIND(_make_tooltip_for_path, "path", "metadata"); GDVIRTUAL_BIND(_make_tooltip_for_path, "path", "metadata", "base");
} }
VBoxContainer *EditorResourceTooltipPlugin::make_default_tooltip(const String &p_resource_path) { VBoxContainer *EditorResourceTooltipPlugin::make_default_tooltip(const String &p_resource_path) {
@ -91,10 +88,10 @@ bool EditorResourceTooltipPlugin::handles(const String &p_resource_type) const {
return ret; return ret;
} }
Control *EditorResourceTooltipPlugin::make_tooltip_for_path(const String &p_resource_path, const Dictionary &p_metadata) const { Control *EditorResourceTooltipPlugin::make_tooltip_for_path(const String &p_resource_path, const Dictionary &p_metadata, Control *p_base) const {
Object *ret = nullptr; Control *ret = nullptr;
GDVIRTUAL_CALL(_make_tooltip_for_path, p_resource_path, p_metadata, ret); GDVIRTUAL_CALL(_make_tooltip_for_path, p_resource_path, p_metadata, p_base, ret);
return Object::cast_to<Control>(ret); return ret;
} }
// EditorTextureTooltipPlugin // EditorTextureTooltipPlugin
@ -103,9 +100,9 @@ bool EditorTextureTooltipPlugin::handles(const String &p_resource_type) const {
return ClassDB::is_parent_class(p_resource_type, "Texture2D") || ClassDB::is_parent_class(p_resource_type, "Image"); return ClassDB::is_parent_class(p_resource_type, "Texture2D") || ClassDB::is_parent_class(p_resource_type, "Image");
} }
Control *EditorTextureTooltipPlugin::make_tooltip_for_path(const String &p_resource_path, const Dictionary &p_metadata) const { Control *EditorTextureTooltipPlugin::make_tooltip_for_path(const String &p_resource_path, const Dictionary &p_metadata, Control *p_base) const {
HBoxContainer *hb = memnew(HBoxContainer); HBoxContainer *hb = memnew(HBoxContainer);
VBoxContainer *vb = EditorResourceTooltipPlugin::make_default_tooltip(p_resource_path); VBoxContainer *vb = Object::cast_to<VBoxContainer>(p_base);
vb->set_alignment(BoxContainer::ALIGNMENT_CENTER); vb->set_alignment(BoxContainer::ALIGNMENT_CENTER);
Vector2 dimensions = p_metadata.get("dimensions", Vector2()); Vector2 dimensions = p_metadata.get("dimensions", Vector2());

View File

@ -34,6 +34,7 @@
#include "core/object/gdvirtual.gen.inc" #include "core/object/gdvirtual.gen.inc"
#include "core/object/ref_counted.h" #include "core/object/ref_counted.h"
#include "core/object/script_language.h" #include "core/object/script_language.h"
#include <scene/gui/control.h>
class Control; class Control;
class Texture2D; class Texture2D;
@ -49,14 +50,14 @@ protected:
static void _bind_methods(); static void _bind_methods();
GDVIRTUAL1RC(bool, _handles, String) GDVIRTUAL1RC(bool, _handles, String)
GDVIRTUAL2RC(Object *, _make_tooltip_for_path, String, Dictionary) GDVIRTUAL3RC(Control *, _make_tooltip_for_path, String, Dictionary, Control *)
public: public:
static VBoxContainer *make_default_tooltip(const String &p_resource_path); static VBoxContainer *make_default_tooltip(const String &p_resource_path);
void request_thumbnail(const String &p_path, TextureRect *p_for_control) const; void request_thumbnail(const String &p_path, TextureRect *p_for_control) const;
virtual bool handles(const String &p_resource_type) const; virtual bool handles(const String &p_resource_type) const;
virtual Control *make_tooltip_for_path(const String &p_resource_path, const Dictionary &p_metadata) const; virtual Control *make_tooltip_for_path(const String &p_resource_path, const Dictionary &p_metadata, Control *p_base) const;
}; };
class EditorTextureTooltipPlugin : public EditorResourceTooltipPlugin { class EditorTextureTooltipPlugin : public EditorResourceTooltipPlugin {
@ -64,7 +65,7 @@ class EditorTextureTooltipPlugin : public EditorResourceTooltipPlugin {
public: public:
virtual bool handles(const String &p_resource_type) const override; virtual bool handles(const String &p_resource_type) const override;
virtual Control *make_tooltip_for_path(const String &p_resource_path, const Dictionary &p_metadata) const override; virtual Control *make_tooltip_for_path(const String &p_resource_path, const Dictionary &p_metadata, Control *p_base) const override;
}; };
#endif // EDITOR_RESOURCE_TOOLTIP_PLUGINS_H #endif // EDITOR_RESOURCE_TOOLTIP_PLUGINS_H

View File

@ -132,8 +132,8 @@ void register_editor_types() {
GDREGISTER_CLASS(EditorNode3DGizmo); GDREGISTER_CLASS(EditorNode3DGizmo);
GDREGISTER_CLASS(EditorNode3DGizmoPlugin); GDREGISTER_CLASS(EditorNode3DGizmoPlugin);
GDREGISTER_ABSTRACT_CLASS(EditorResourcePreview); GDREGISTER_ABSTRACT_CLASS(EditorResourcePreview);
GDREGISTER_ABSTRACT_CLASS(EditorResourceTooltipPlugin);
GDREGISTER_CLASS(EditorResourcePreviewGenerator); GDREGISTER_CLASS(EditorResourcePreviewGenerator);
GDREGISTER_CLASS(EditorResourceTooltipPlugin);
GDREGISTER_ABSTRACT_CLASS(EditorFileSystem); GDREGISTER_ABSTRACT_CLASS(EditorFileSystem);
GDREGISTER_CLASS(EditorFileSystemDirectory); GDREGISTER_CLASS(EditorFileSystemDirectory);
GDREGISTER_CLASS(EditorVCSInterface); GDREGISTER_CLASS(EditorVCSInterface);