Path: blob/master/editor/inspector/editor_resource_tooltip_plugins.cpp
20873 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, ObjectID p_trect_id) {42TextureRect *tr = ObjectDB::get_instance<TextureRect>(p_trect_id);43if (tr) {44tr->set_texture(p_preview);45}46}4748void EditorResourceTooltipPlugin::_bind_methods() {49ClassDB::bind_method(D_METHOD("request_thumbnail", "path", "control"), &EditorResourceTooltipPlugin::request_thumbnail);5051GDVIRTUAL_BIND(_handles, "type");52GDVIRTUAL_BIND(_make_tooltip_for_path, "path", "metadata", "base");53}5455VBoxContainer *EditorResourceTooltipPlugin::make_default_tooltip(const String &p_resource_path) {56VBoxContainer *vb = memnew(VBoxContainer);57vb->add_theme_constant_override("separation", -4 * EDSCALE);58{59Label *label = memnew(Label(p_resource_path.get_file()));60vb->add_child(label);61}6263ResourceUID::ID id = EditorFileSystem::get_singleton()->get_file_uid(p_resource_path);64if (id != ResourceUID::INVALID_ID) {65Label *label = memnew(Label(ResourceUID::get_singleton()->id_to_text(id)));66vb->add_child(label);67}6869{70Ref<FileAccess> f = FileAccess::open(p_resource_path, FileAccess::READ);71if (f.is_valid()) {72Label *label = memnew(Label(vformat(TTR("Size: %s"), String::humanize_size(f->get_length()))));73vb->add_child(label);74} else {75Label *label = memnew(Label(TTR("Invalid file or broken link.")));76label->add_theme_color_override(SceneStringName(font_color), EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("error_color"), EditorStringName(Editor)));77vb->add_child(label);78return vb;79}80}8182if (ResourceLoader::exists(p_resource_path)) {83String type = ResourceLoader::get_resource_type(p_resource_path);84Label *label = memnew(Label(vformat(TTR("Type: %s"), type)));85vb->add_child(label);86}8788Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);89if (da->is_link(p_resource_path)) {90Label *link = memnew(Label(vformat(TTR("Link to: %s"), da->read_link(p_resource_path))));91vb->add_child(link);92}93return vb;94}9596void EditorResourceTooltipPlugin::request_thumbnail(const String &p_path, TextureRect *p_for_control) const {97ERR_FAIL_NULL(p_for_control);98EditorResourcePreview::get_singleton()->queue_resource_preview(p_path, callable_mp(const_cast<EditorResourceTooltipPlugin *>(this), &EditorResourceTooltipPlugin::_thumbnail_ready).bind(p_for_control->get_instance_id()));99}100101bool EditorResourceTooltipPlugin::handles(const String &p_resource_type) const {102bool ret = false;103GDVIRTUAL_CALL(_handles, p_resource_type, ret);104return ret;105}106107Control *EditorResourceTooltipPlugin::make_tooltip_for_path(const String &p_resource_path, const Dictionary &p_metadata, Control *p_base) const {108Control *ret = nullptr;109GDVIRTUAL_CALL(_make_tooltip_for_path, p_resource_path, p_metadata, p_base, ret);110return ret;111}112113// EditorTextureTooltipPlugin114115bool EditorTextureTooltipPlugin::handles(const String &p_resource_type) const {116return ClassDB::is_parent_class(p_resource_type, "Texture2D") || ClassDB::is_parent_class(p_resource_type, "Image");117}118119Control *EditorTextureTooltipPlugin::make_tooltip_for_path(const String &p_resource_path, const Dictionary &p_metadata, Control *p_base) const {120HBoxContainer *hb = memnew(HBoxContainer);121VBoxContainer *vb = Object::cast_to<VBoxContainer>(p_base);122DEV_ASSERT(vb);123vb->set_alignment(BoxContainer::ALIGNMENT_CENTER);124125Vector2 dimensions = p_metadata.get("dimensions", Vector2());126Label *label = memnew(Label(vformat(TTR(U"Dimensions: %d × %d"), dimensions.x, dimensions.y)));127vb->add_child(label);128129TextureRect *tr = memnew(TextureRect);130tr->set_v_size_flags(Control::SIZE_SHRINK_CENTER);131hb->add_child(tr);132request_thumbnail(p_resource_path, tr);133134hb->add_child(vb);135return hb;136}137138// EditorAudioStreamTooltipPlugin139140bool EditorAudioStreamTooltipPlugin::handles(const String &p_resource_type) const {141return ClassDB::is_parent_class(p_resource_type, "AudioStream");142}143144Control *EditorAudioStreamTooltipPlugin::make_tooltip_for_path(const String &p_resource_path, const Dictionary &p_metadata, Control *p_base) const {145VBoxContainer *vb = Object::cast_to<VBoxContainer>(p_base);146DEV_ASSERT(vb);147148double length = p_metadata.get("length", 0.0);149if (length >= 60.0) {150vb->add_child(memnew(Label(vformat(TTR("Length: %0dm %0ds"), int(length / 60.0), int(std::fmod(length, 60))))));151} else if (length >= 1.0) {152vb->add_child(memnew(Label(vformat(TTR("Length: %0.1fs"), length))));153} else {154vb->add_child(memnew(Label(vformat(TTR("Length: %0.3fs"), length))));155}156157TextureRect *tr = memnew(TextureRect);158vb->add_child(tr);159request_thumbnail(p_resource_path, tr);160161return vb;162}163164165