| 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_resource_preview.h" |
| 34 | #include "editor/editor_scale.h" |
| 35 | #include "scene/gui/box_container.h" |
| 36 | #include "scene/gui/label.h" |
| 37 | #include "scene/gui/texture_rect.h" |
| 38 | |
| 39 | void EditorResourceTooltipPlugin::_thumbnail_ready(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, const Variant &p_udata) { |
| 40 | ObjectID trid = p_udata; |
| 41 | TextureRect *tr = Object::cast_to<TextureRect>(ObjectDB::get_instance(trid)); |
| 42 | |
| 43 | if (!tr) { |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | tr->set_texture(p_preview); |
| 48 | } |
| 49 | |
| 50 | void EditorResourceTooltipPlugin::_bind_methods() { |
| 51 | ClassDB::bind_method(D_METHOD("_thumbnail_ready" ), &EditorResourceTooltipPlugin::_thumbnail_ready); |
| 52 | ClassDB::bind_method(D_METHOD("request_thumbnail" , "path" , "control" ), &EditorResourceTooltipPlugin::request_thumbnail); |
| 53 | |
| 54 | GDVIRTUAL_BIND(_handles, "type" ); |
| 55 | GDVIRTUAL_BIND(_make_tooltip_for_path, "path" , "metadata" , "base" ); |
| 56 | } |
| 57 | |
| 58 | VBoxContainer *EditorResourceTooltipPlugin::make_default_tooltip(const String &p_resource_path) { |
| 59 | VBoxContainer *vb = memnew(VBoxContainer); |
| 60 | vb->add_theme_constant_override("separation" , -4 * EDSCALE); |
| 61 | { |
| 62 | Label *label = memnew(Label(p_resource_path.get_file())); |
| 63 | vb->add_child(label); |
| 64 | } |
| 65 | |
| 66 | { |
| 67 | Ref<FileAccess> f = FileAccess::open(p_resource_path, FileAccess::READ); |
| 68 | Label *label = memnew(Label(vformat(TTR("Size: %s" ), String::humanize_size(f->get_length())))); |
| 69 | vb->add_child(label); |
| 70 | } |
| 71 | |
| 72 | if (ResourceLoader::exists(p_resource_path)) { |
| 73 | String type = ResourceLoader::get_resource_type(p_resource_path); |
| 74 | Label *label = memnew(Label(vformat(TTR("Type: %s" ), type))); |
| 75 | vb->add_child(label); |
| 76 | } |
| 77 | return vb; |
| 78 | } |
| 79 | |
| 80 | void EditorResourceTooltipPlugin::request_thumbnail(const String &p_path, TextureRect *p_for_control) const { |
| 81 | ERR_FAIL_NULL(p_for_control); |
| 82 | EditorResourcePreview::get_singleton()->queue_resource_preview(p_path, const_cast<EditorResourceTooltipPlugin *>(this), "_thumbnail_ready" , p_for_control->get_instance_id()); |
| 83 | } |
| 84 | |
| 85 | bool EditorResourceTooltipPlugin::handles(const String &p_resource_type) const { |
| 86 | bool ret = false; |
| 87 | GDVIRTUAL_CALL(_handles, p_resource_type, ret); |
| 88 | return ret; |
| 89 | } |
| 90 | |
| 91 | Control *EditorResourceTooltipPlugin::make_tooltip_for_path(const String &p_resource_path, const Dictionary &p_metadata, Control *p_base) const { |
| 92 | Control *ret = nullptr; |
| 93 | GDVIRTUAL_CALL(_make_tooltip_for_path, p_resource_path, p_metadata, p_base, ret); |
| 94 | return ret; |
| 95 | } |
| 96 | |
| 97 | // EditorTextureTooltipPlugin |
| 98 | |
| 99 | bool EditorTextureTooltipPlugin::handles(const String &p_resource_type) const { |
| 100 | return ClassDB::is_parent_class(p_resource_type, "Texture2D" ) || ClassDB::is_parent_class(p_resource_type, "Image" ); |
| 101 | } |
| 102 | |
| 103 | Control *EditorTextureTooltipPlugin::make_tooltip_for_path(const String &p_resource_path, const Dictionary &p_metadata, Control *p_base) const { |
| 104 | HBoxContainer *hb = memnew(HBoxContainer); |
| 105 | VBoxContainer *vb = Object::cast_to<VBoxContainer>(p_base); |
| 106 | vb->set_alignment(BoxContainer::ALIGNMENT_CENTER); |
| 107 | |
| 108 | Vector2 dimensions = p_metadata.get("dimensions" , Vector2()); |
| 109 | Label *label = memnew(Label(vformat(TTR(U"Dimensions: %d × %d" ), dimensions.x, dimensions.y))); |
| 110 | vb->add_child(label); |
| 111 | |
| 112 | TextureRect *tr = memnew(TextureRect); |
| 113 | tr->set_v_size_flags(Control::SIZE_SHRINK_CENTER); |
| 114 | hb->add_child(tr); |
| 115 | request_thumbnail(p_resource_path, tr); |
| 116 | |
| 117 | hb->add_child(vb); |
| 118 | return hb; |
| 119 | } |
| 120 | |