Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/inspector/editor_resource_tooltip_plugins.cpp
9898 views
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, const Variant &p_udata) {
43
ObjectID trid = p_udata;
44
TextureRect *tr = ObjectDB::get_instance<TextureRect>(trid);
45
46
if (!tr) {
47
return;
48
}
49
50
tr->set_texture(p_preview);
51
}
52
53
void EditorResourceTooltipPlugin::_bind_methods() {
54
ClassDB::bind_method(D_METHOD("_thumbnail_ready"), &EditorResourceTooltipPlugin::_thumbnail_ready);
55
ClassDB::bind_method(D_METHOD("request_thumbnail", "path", "control"), &EditorResourceTooltipPlugin::request_thumbnail);
56
57
GDVIRTUAL_BIND(_handles, "type");
58
GDVIRTUAL_BIND(_make_tooltip_for_path, "path", "metadata", "base");
59
}
60
61
VBoxContainer *EditorResourceTooltipPlugin::make_default_tooltip(const String &p_resource_path) {
62
VBoxContainer *vb = memnew(VBoxContainer);
63
vb->add_theme_constant_override("separation", -4 * EDSCALE);
64
{
65
Label *label = memnew(Label(p_resource_path.get_file()));
66
vb->add_child(label);
67
}
68
69
ResourceUID::ID id = EditorFileSystem::get_singleton()->get_file_uid(p_resource_path);
70
if (id != ResourceUID::INVALID_ID) {
71
Label *label = memnew(Label(ResourceUID::get_singleton()->id_to_text(id)));
72
vb->add_child(label);
73
}
74
75
{
76
Ref<FileAccess> f = FileAccess::open(p_resource_path, FileAccess::READ);
77
if (f.is_valid()) {
78
Label *label = memnew(Label(vformat(TTR("Size: %s"), String::humanize_size(f->get_length()))));
79
vb->add_child(label);
80
} else {
81
Label *label = memnew(Label(TTR("Invalid file or broken link.")));
82
label->add_theme_color_override(SceneStringName(font_color), EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
83
vb->add_child(label);
84
return vb;
85
}
86
}
87
88
if (ResourceLoader::exists(p_resource_path)) {
89
String type = ResourceLoader::get_resource_type(p_resource_path);
90
Label *label = memnew(Label(vformat(TTR("Type: %s"), type)));
91
vb->add_child(label);
92
}
93
return vb;
94
}
95
96
void EditorResourceTooltipPlugin::request_thumbnail(const String &p_path, TextureRect *p_for_control) const {
97
ERR_FAIL_NULL(p_for_control);
98
EditorResourcePreview::get_singleton()->queue_resource_preview(p_path, const_cast<EditorResourceTooltipPlugin *>(this), "_thumbnail_ready", p_for_control->get_instance_id());
99
}
100
101
bool EditorResourceTooltipPlugin::handles(const String &p_resource_type) const {
102
bool ret = false;
103
GDVIRTUAL_CALL(_handles, p_resource_type, ret);
104
return ret;
105
}
106
107
Control *EditorResourceTooltipPlugin::make_tooltip_for_path(const String &p_resource_path, const Dictionary &p_metadata, Control *p_base) const {
108
Control *ret = nullptr;
109
GDVIRTUAL_CALL(_make_tooltip_for_path, p_resource_path, p_metadata, p_base, ret);
110
return ret;
111
}
112
113
// EditorTextureTooltipPlugin
114
115
bool EditorTextureTooltipPlugin::handles(const String &p_resource_type) const {
116
return ClassDB::is_parent_class(p_resource_type, "Texture2D") || ClassDB::is_parent_class(p_resource_type, "Image");
117
}
118
119
Control *EditorTextureTooltipPlugin::make_tooltip_for_path(const String &p_resource_path, const Dictionary &p_metadata, Control *p_base) const {
120
HBoxContainer *hb = memnew(HBoxContainer);
121
VBoxContainer *vb = Object::cast_to<VBoxContainer>(p_base);
122
DEV_ASSERT(vb);
123
vb->set_alignment(BoxContainer::ALIGNMENT_CENTER);
124
125
Vector2 dimensions = p_metadata.get("dimensions", Vector2());
126
Label *label = memnew(Label(vformat(TTR(U"Dimensions: %d × %d"), dimensions.x, dimensions.y)));
127
vb->add_child(label);
128
129
TextureRect *tr = memnew(TextureRect);
130
tr->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
131
hb->add_child(tr);
132
request_thumbnail(p_resource_path, tr);
133
134
hb->add_child(vb);
135
return hb;
136
}
137
138
// EditorAudioStreamTooltipPlugin
139
140
bool EditorAudioStreamTooltipPlugin::handles(const String &p_resource_type) const {
141
return ClassDB::is_parent_class(p_resource_type, "AudioStream");
142
}
143
144
Control *EditorAudioStreamTooltipPlugin::make_tooltip_for_path(const String &p_resource_path, const Dictionary &p_metadata, Control *p_base) const {
145
VBoxContainer *vb = Object::cast_to<VBoxContainer>(p_base);
146
DEV_ASSERT(vb);
147
148
double length = p_metadata.get("length", 0.0);
149
if (length >= 60.0) {
150
vb->add_child(memnew(Label(vformat(TTR("Length: %0dm %0ds"), int(length / 60.0), int(std::fmod(length, 60))))));
151
} else if (length >= 1.0) {
152
vb->add_child(memnew(Label(vformat(TTR("Length: %0.1fs"), length))));
153
} else {
154
vb->add_child(memnew(Label(vformat(TTR("Length: %0.3fs"), length))));
155
}
156
157
TextureRect *tr = memnew(TextureRect);
158
vb->add_child(tr);
159
request_thumbnail(p_resource_path, tr);
160
161
return vb;
162
}
163
164