From 1fce8d8a91afa5ae739377990c32239ffbe11c3a Mon Sep 17 00:00:00 2001 From: kobewi Date: Fri, 12 May 2023 22:39:05 +0200 Subject: [PATCH] Add tooltip plugin for AudioStream --- editor/filesystem_dock.cpp | 1 + editor/plugins/editor_preview_plugins.cpp | 2 ++ .../editor_resource_tooltip_plugins.cpp | 27 +++++++++++++++++++ .../plugins/editor_resource_tooltip_plugins.h | 11 ++++++-- 4 files changed, 39 insertions(+), 2 deletions(-) diff --git a/editor/filesystem_dock.cpp b/editor/filesystem_dock.cpp index 2ed547a970c..0472025aed4 100644 --- a/editor/filesystem_dock.cpp +++ b/editor/filesystem_dock.cpp @@ -4128,6 +4128,7 @@ FileSystemDock::FileSystemDock() { ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &FileSystemDock::_project_settings_changed)); add_resource_tooltip_plugin(memnew(EditorTextureTooltipPlugin)); + add_resource_tooltip_plugin(memnew(EditorAudioStreamTooltipPlugin)); } FileSystemDock::~FileSystemDock() { diff --git a/editor/plugins/editor_preview_plugins.cpp b/editor/plugins/editor_preview_plugins.cpp index 70213b280ca..9fb4d86fa21 100644 --- a/editor/plugins/editor_preview_plugins.cpp +++ b/editor/plugins/editor_preview_plugins.cpp @@ -678,6 +678,8 @@ Ref EditorAudioStreamPreviewPlugin::generate(const Ref &p_f } } + p_metadata["length"] = stream->get_length(); + //post_process_preview(img); Ref image = Image::create_from_data(w, h, false, Image::FORMAT_RGB8, img); diff --git a/editor/plugins/editor_resource_tooltip_plugins.cpp b/editor/plugins/editor_resource_tooltip_plugins.cpp index fab8ee9f59c..dfeb59214c2 100644 --- a/editor/plugins/editor_resource_tooltip_plugins.cpp +++ b/editor/plugins/editor_resource_tooltip_plugins.cpp @@ -103,6 +103,7 @@ bool EditorTextureTooltipPlugin::handles(const String &p_resource_type) const { Control *EditorTextureTooltipPlugin::make_tooltip_for_path(const String &p_resource_path, const Dictionary &p_metadata, Control *p_base) const { HBoxContainer *hb = memnew(HBoxContainer); VBoxContainer *vb = Object::cast_to(p_base); + DEV_ASSERT(vb); vb->set_alignment(BoxContainer::ALIGNMENT_CENTER); Vector2 dimensions = p_metadata.get("dimensions", Vector2()); @@ -117,3 +118,29 @@ Control *EditorTextureTooltipPlugin::make_tooltip_for_path(const String &p_resou hb->add_child(vb); return hb; } + +// EditorAudioStreamTooltipPlugin + +bool EditorAudioStreamTooltipPlugin::handles(const String &p_resource_type) const { + return ClassDB::is_parent_class(p_resource_type, "AudioStream"); +} + +Control *EditorAudioStreamTooltipPlugin::make_tooltip_for_path(const String &p_resource_path, const Dictionary &p_metadata, Control *p_base) const { + VBoxContainer *vb = Object::cast_to(p_base); + DEV_ASSERT(vb); + + double length = p_metadata.get("length", 0.0); + if (length >= 60.0) { + vb->add_child(memnew(Label(vformat(TTR("Length: %0dm %0ds"), int(length / 60.0), int(fmod(length, 60)))))); + } else if (length >= 1.0) { + vb->add_child(memnew(Label(vformat(TTR("Length: %0.1fs"), length)))); + } else { + vb->add_child(memnew(Label(vformat(TTR("Length: %0.3fs"), length)))); + } + + TextureRect *tr = memnew(TextureRect); + vb->add_child(tr); + request_thumbnail(p_resource_path, tr); + + return vb; +} diff --git a/editor/plugins/editor_resource_tooltip_plugins.h b/editor/plugins/editor_resource_tooltip_plugins.h index e3a27de0bb8..43be8fd8e8f 100644 --- a/editor/plugins/editor_resource_tooltip_plugins.h +++ b/editor/plugins/editor_resource_tooltip_plugins.h @@ -33,9 +33,8 @@ #include "core/object/gdvirtual.gen.inc" #include "core/object/ref_counted.h" -#include +#include "scene/gui/control.h" -class Control; class Texture2D; class TextureRect; class VBoxContainer; @@ -67,4 +66,12 @@ public: virtual Control *make_tooltip_for_path(const String &p_resource_path, const Dictionary &p_metadata, Control *p_base) const override; }; +class EditorAudioStreamTooltipPlugin : public EditorResourceTooltipPlugin { + GDCLASS(EditorAudioStreamTooltipPlugin, EditorResourceTooltipPlugin); + +public: + virtual bool handles(const String &p_resource_type) const override; + virtual Control *make_tooltip_for_path(const String &p_resource_path, const Dictionary &p_metadata, Control *p_base) const override; +}; + #endif // EDITOR_RESOURCE_TOOLTIP_PLUGINS_H