editor_resource_preview.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*************************************************************************/
  2. /* editor_resource_preview.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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. #ifndef EDITORRESOURCEPREVIEW_H
  31. #define EDITORRESOURCEPREVIEW_H
  32. #include "core/os/semaphore.h"
  33. #include "core/os/thread.h"
  34. #include "scene/main/node.h"
  35. #include "scene/resources/texture.h"
  36. /* make previews for:
  37. *packdscene
  38. *wav
  39. *image
  40. *mesh
  41. -font
  42. *script
  43. *material
  44. -shader
  45. -shader graph?
  46. -navigation mesh
  47. -collision?
  48. -occluder polygon
  49. -navigation polygon
  50. -tileset
  51. -curve and curve2D
  52. */
  53. class EditorResourcePreviewGenerator : public Reference {
  54. GDCLASS(EditorResourcePreviewGenerator, Reference);
  55. protected:
  56. static void _bind_methods();
  57. public:
  58. virtual bool handles(const String &p_type) const;
  59. virtual Ref<Texture> generate(const RES &p_from, const Size2 p_size) const;
  60. virtual Ref<Texture> generate_from_path(const String &p_path, const Size2 p_size) const;
  61. virtual bool should_generate_small_preview() const;
  62. EditorResourcePreviewGenerator();
  63. };
  64. class EditorResourcePreview : public Node {
  65. GDCLASS(EditorResourcePreview, Node);
  66. static EditorResourcePreview *singleton;
  67. struct QueueItem {
  68. Ref<Resource> resource;
  69. String path;
  70. ObjectID id;
  71. StringName function;
  72. Variant userdata;
  73. };
  74. List<QueueItem> queue;
  75. Mutex *preview_mutex;
  76. Semaphore *preview_sem;
  77. Thread *thread;
  78. bool exit;
  79. struct Item {
  80. Ref<Texture> preview;
  81. Ref<Texture> small_preview;
  82. int order;
  83. uint32_t last_hash;
  84. uint64_t modified_time;
  85. };
  86. int order;
  87. Map<String, Item> cache;
  88. void _preview_ready(const String &p_str, const Ref<Texture> &p_texture, const Ref<Texture> &p_small_texture, ObjectID id, const StringName &p_func, const Variant &p_ud);
  89. void _generate_preview(Ref<ImageTexture> &r_texture, Ref<ImageTexture> &r_small_texture, const QueueItem &p_item, const String &cache_base);
  90. static void _thread_func(void *ud);
  91. void _thread();
  92. Vector<Ref<EditorResourcePreviewGenerator> > preview_generators;
  93. protected:
  94. static void _bind_methods();
  95. public:
  96. static EditorResourcePreview *get_singleton();
  97. //callback function is callback(String p_path,Ref<Texture> preview,Variant udata) preview null if could not load
  98. void queue_resource_preview(const String &p_path, Object *p_receiver, const StringName &p_receiver_func, const Variant &p_userdata);
  99. void queue_edited_resource_preview(const Ref<Resource> &p_res, Object *p_receiver, const StringName &p_receiver_func, const Variant &p_userdata);
  100. void add_preview_generator(const Ref<EditorResourcePreviewGenerator> &p_generator);
  101. void remove_preview_generator(const Ref<EditorResourcePreviewGenerator> &p_generator);
  102. void check_for_invalidation(const String &p_path);
  103. EditorResourcePreview();
  104. ~EditorResourcePreview();
  105. };
  106. #endif // EDITORRESOURCEPREVIEW_H