editor_resource_tooltip_plugins.cpp 6.3 KB

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