Path: blob/master/editor/inspector/editor_resource_tooltip_plugins.cpp
9898 views
/**************************************************************************/1/* editor_resource_tooltip_plugins.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "editor_resource_tooltip_plugins.h"3132#include "editor/editor_node.h"33#include "editor/editor_string_names.h"34#include "editor/file_system/editor_file_system.h"35#include "editor/inspector/editor_resource_preview.h"36#include "editor/themes/editor_scale.h"37#include "scene/gui/box_container.h"38#include "scene/gui/label.h"39#include "scene/gui/texture_rect.h"4041void EditorResourceTooltipPlugin::_thumbnail_ready(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, const Variant &p_udata) {42ObjectID trid = p_udata;43TextureRect *tr = ObjectDB::get_instance<TextureRect>(trid);4445if (!tr) {46return;47}4849tr->set_texture(p_preview);50}5152void EditorResourceTooltipPlugin::_bind_methods() {53ClassDB::bind_method(D_METHOD("_thumbnail_ready"), &EditorResourceTooltipPlugin::_thumbnail_ready);54ClassDB::bind_method(D_METHOD("request_thumbnail", "path", "control"), &EditorResourceTooltipPlugin::request_thumbnail);5556GDVIRTUAL_BIND(_handles, "type");57GDVIRTUAL_BIND(_make_tooltip_for_path, "path", "metadata", "base");58}5960VBoxContainer *EditorResourceTooltipPlugin::make_default_tooltip(const String &p_resource_path) {61VBoxContainer *vb = memnew(VBoxContainer);62vb->add_theme_constant_override("separation", -4 * EDSCALE);63{64Label *label = memnew(Label(p_resource_path.get_file()));65vb->add_child(label);66}6768ResourceUID::ID id = EditorFileSystem::get_singleton()->get_file_uid(p_resource_path);69if (id != ResourceUID::INVALID_ID) {70Label *label = memnew(Label(ResourceUID::get_singleton()->id_to_text(id)));71vb->add_child(label);72}7374{75Ref<FileAccess> f = FileAccess::open(p_resource_path, FileAccess::READ);76if (f.is_valid()) {77Label *label = memnew(Label(vformat(TTR("Size: %s"), String::humanize_size(f->get_length()))));78vb->add_child(label);79} else {80Label *label = memnew(Label(TTR("Invalid file or broken link.")));81label->add_theme_color_override(SceneStringName(font_color), EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("error_color"), EditorStringName(Editor)));82vb->add_child(label);83return vb;84}85}8687if (ResourceLoader::exists(p_resource_path)) {88String type = ResourceLoader::get_resource_type(p_resource_path);89Label *label = memnew(Label(vformat(TTR("Type: %s"), type)));90vb->add_child(label);91}92return vb;93}9495void EditorResourceTooltipPlugin::request_thumbnail(const String &p_path, TextureRect *p_for_control) const {96ERR_FAIL_NULL(p_for_control);97EditorResourcePreview::get_singleton()->queue_resource_preview(p_path, const_cast<EditorResourceTooltipPlugin *>(this), "_thumbnail_ready", p_for_control->get_instance_id());98}99100bool EditorResourceTooltipPlugin::handles(const String &p_resource_type) const {101bool ret = false;102GDVIRTUAL_CALL(_handles, p_resource_type, ret);103return ret;104}105106Control *EditorResourceTooltipPlugin::make_tooltip_for_path(const String &p_resource_path, const Dictionary &p_metadata, Control *p_base) const {107Control *ret = nullptr;108GDVIRTUAL_CALL(_make_tooltip_for_path, p_resource_path, p_metadata, p_base, ret);109return ret;110}111112// EditorTextureTooltipPlugin113114bool EditorTextureTooltipPlugin::handles(const String &p_resource_type) const {115return ClassDB::is_parent_class(p_resource_type, "Texture2D") || ClassDB::is_parent_class(p_resource_type, "Image");116}117118Control *EditorTextureTooltipPlugin::make_tooltip_for_path(const String &p_resource_path, const Dictionary &p_metadata, Control *p_base) const {119HBoxContainer *hb = memnew(HBoxContainer);120VBoxContainer *vb = Object::cast_to<VBoxContainer>(p_base);121DEV_ASSERT(vb);122vb->set_alignment(BoxContainer::ALIGNMENT_CENTER);123124Vector2 dimensions = p_metadata.get("dimensions", Vector2());125Label *label = memnew(Label(vformat(TTR(U"Dimensions: %d × %d"), dimensions.x, dimensions.y)));126vb->add_child(label);127128TextureRect *tr = memnew(TextureRect);129tr->set_v_size_flags(Control::SIZE_SHRINK_CENTER);130hb->add_child(tr);131request_thumbnail(p_resource_path, tr);132133hb->add_child(vb);134return hb;135}136137// EditorAudioStreamTooltipPlugin138139bool EditorAudioStreamTooltipPlugin::handles(const String &p_resource_type) const {140return ClassDB::is_parent_class(p_resource_type, "AudioStream");141}142143Control *EditorAudioStreamTooltipPlugin::make_tooltip_for_path(const String &p_resource_path, const Dictionary &p_metadata, Control *p_base) const {144VBoxContainer *vb = Object::cast_to<VBoxContainer>(p_base);145DEV_ASSERT(vb);146147double length = p_metadata.get("length", 0.0);148if (length >= 60.0) {149vb->add_child(memnew(Label(vformat(TTR("Length: %0dm %0ds"), int(length / 60.0), int(std::fmod(length, 60))))));150} else if (length >= 1.0) {151vb->add_child(memnew(Label(vformat(TTR("Length: %0.1fs"), length))));152} else {153vb->add_child(memnew(Label(vformat(TTR("Length: %0.3fs"), length))));154}155156TextureRect *tr = memnew(TextureRect);157vb->add_child(tr);158request_thumbnail(p_resource_path, tr);159160return vb;161}162163164