-
-
Notifications
You must be signed in to change notification settings - Fork 21.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
337 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<class name="EditorResourceTooltipPlugin" inherits="RefCounted" version="4.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd"> | ||
<brief_description> | ||
A plugin that advanced tooltip for its handled resource type. | ||
</brief_description> | ||
<description> | ||
Resource tooltip plugins are used by [FileSystemDock] to generate customized tooltips for specific resources. E.g. tooltip for a [Texture2D] displays a bigger preview and the texture's dimensions. | ||
A plugin must be first registered with [method FileSystemDock.add_resource_tooltip_plugin]. When the user hovers a resource in filesystem dock which is handled by the plugin, [method _make_tooltip_for_path] is called to create the tooltip. It works similarly to [method Control._make_custom_tooltip]. | ||
</description> | ||
<tutorials> | ||
</tutorials> | ||
<methods> | ||
<method name="_handles" qualifiers="virtual const"> | ||
<return type="bool" /> | ||
<param index="0" name="type" type="String" /> | ||
<description> | ||
Return [code]true[/code] if the plugin is going to handle the given [Resource] [param type]. | ||
</description> | ||
</method> | ||
<method name="_make_tooltip_for_path" qualifiers="virtual const"> | ||
<return type="Object" /> | ||
<param index="0" name="path" type="String" /> | ||
<param index="1" name="metadata" type="Dictionary" /> | ||
<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. | ||
The [param metadata] dictionary is provided by preview generator (see method EditorResourcePreviewGenerator._generate]). | ||
[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> | ||
</method> | ||
<method name="make_default_tooltip" qualifiers="static"> | ||
<return type="VBoxContainer" /> | ||
<param index="0" name="path" type="String" /> | ||
<description> | ||
Creates a default file tooltip. The tooltip includes file name, file size and [Resource] type if available. | ||
</description> | ||
</method> | ||
<method name="request_thumbnail" qualifiers="const"> | ||
<return type="void" /> | ||
<param index="0" name="path" type="String" /> | ||
<param index="1" name="control" type="TextureRect" /> | ||
<description> | ||
Requests a thumbnail for the given [TextureRect]. The thumbnail is created asynchronously by [EditorResourcePreview] and automatically set when available. | ||
</description> | ||
</method> | ||
</methods> | ||
</class> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/**************************************************************************/ | ||
/* editor_resource_tooltip_plugins.cpp */ | ||
/**************************************************************************/ | ||
/* This file is part of: */ | ||
/* GODOT ENGINE */ | ||
/* https://godotengine.org */ | ||
/**************************************************************************/ | ||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | ||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ | ||
/* */ | ||
/* Permission is hereby granted, free of charge, to any person obtaining */ | ||
/* a copy of this software and associated documentation files (the */ | ||
/* "Software"), to deal in the Software without restriction, including */ | ||
/* without limitation the rights to use, copy, modify, merge, publish, */ | ||
/* distribute, sublicense, and/or sell copies of the Software, and to */ | ||
/* permit persons to whom the Software is furnished to do so, subject to */ | ||
/* the following conditions: */ | ||
/* */ | ||
/* The above copyright notice and this permission notice shall be */ | ||
/* included in all copies or substantial portions of the Software. */ | ||
/* */ | ||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ | ||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ | ||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | ||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ | ||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ | ||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ | ||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ | ||
/**************************************************************************/ | ||
|
||
#include "editor_resource_tooltip_plugins.h" | ||
|
||
#include "editor/editor_resource_preview.h" | ||
#include "editor/editor_scale.h" | ||
#include "scene/gui/box_container.h" | ||
#include "scene/gui/control.h" | ||
#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 = Object::cast_to<TextureRect>(ObjectDB::get_instance(trid)); | ||
|
||
if (!tr) { | ||
return; | ||
} | ||
|
||
tr->set_texture(p_preview); | ||
} | ||
|
||
void EditorResourceTooltipPlugin::_bind_methods() { | ||
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); | ||
|
||
GDVIRTUAL_BIND(_handles, "type"); | ||
GDVIRTUAL_BIND(_make_tooltip_for_path, "path", "metadata"); | ||
} | ||
|
||
VBoxContainer *EditorResourceTooltipPlugin::make_default_tooltip(const String &p_resource_path) { | ||
VBoxContainer *vb = memnew(VBoxContainer); | ||
vb->add_theme_constant_override("separation", -4 * EDSCALE); | ||
{ | ||
Label *label = memnew(Label(p_resource_path.get_file())); | ||
vb->add_child(label); | ||
} | ||
|
||
{ | ||
Ref<FileAccess> f = FileAccess::open(p_resource_path, FileAccess::READ); | ||
Label *label = memnew(Label(vformat(TTR("Size: %s"), String::humanize_size(f->get_length())))); | ||
vb->add_child(label); | ||
} | ||
|
||
if (ResourceLoader::exists(p_resource_path)) { | ||
String type = ResourceLoader::get_resource_type(p_resource_path); | ||
Label *label = memnew(Label(vformat(TTR("Type: %s"), type))); | ||
vb->add_child(label); | ||
} | ||
return vb; | ||
} | ||
|
||
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()); | ||
} | ||
|
||
bool EditorResourceTooltipPlugin::handles(const String &p_resource_type) const { | ||
bool ret = false; | ||
GDVIRTUAL_CALL(_handles, p_resource_type, ret); | ||
return ret; | ||
} | ||
|
||
Control *EditorResourceTooltipPlugin::make_tooltip_for_path(const String &p_resource_path, const Dictionary &p_metadata) const { | ||
Object *ret = nullptr; | ||
GDVIRTUAL_CALL(_make_tooltip_for_path, p_resource_path, p_metadata, ret); | ||
return Object::cast_to<Control>(ret); | ||
} | ||
|
||
// EditorTextureTooltipPlugin | ||
|
||
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"); | ||
} | ||
|
||
Control *EditorTextureTooltipPlugin::make_tooltip_for_path(const String &p_resource_path, const Dictionary &p_metadata) const { | ||
HBoxContainer *hb = memnew(HBoxContainer); | ||
VBoxContainer *vb = EditorResourceTooltipPlugin::make_default_tooltip(p_resource_path); | ||
vb->set_alignment(BoxContainer::ALIGNMENT_CENTER); | ||
|
||
Vector2 dimensions = p_metadata.get("dimensions", Vector2()); | ||
Label *label = memnew(Label(vformat(TTR(U"Dimensions: %d × %d"), dimensions.x, dimensions.y))); | ||
vb->add_child(label); | ||
|
||
TextureRect *tr = memnew(TextureRect); | ||
tr->set_v_size_flags(Control::SIZE_SHRINK_CENTER); | ||
hb->add_child(tr); | ||
request_thumbnail(p_resource_path, tr); | ||
|
||
hb->add_child(vb); | ||
return hb; | ||
} |
Oops, something went wrong.