2
0

editor_resource_tooltip_plugins.cpp 6.6 KB

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