diff options
Diffstat (limited to 'editor/plugins/editor_resource_tooltip_plugins.cpp')
-rw-r--r-- | editor/plugins/editor_resource_tooltip_plugins.cpp | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/editor/plugins/editor_resource_tooltip_plugins.cpp b/editor/plugins/editor_resource_tooltip_plugins.cpp new file mode 100644 index 0000000000..26371360e9 --- /dev/null +++ b/editor/plugins/editor_resource_tooltip_plugins.cpp @@ -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; +} |