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