Unravel Engine C++ Reference
Loading...
Searching...
No Matches
thumbnail_manager.cpp
Go to the documentation of this file.
1#include "thumbnail_manager.h"
2
8#include <engine/engine.h>
9#include <engine/events.h>
14
16
21#include <graphics/texture.h>
22
24#include <filesystem/watcher.h>
25
26namespace unravel
27{
28
29namespace
30{
31
32template<typename T>
33auto make_thumbnail(thumbnail_manager::generator& gen, const asset_handle<T>& asset) -> gfx::texture::ptr
34{
35 auto& thumbnail = gen.thumbnails[asset.uid()];
36 auto current_fbo = thumbnail.get();
37
38 if(gen.remaining > 0 && thumbnail.needs_regeneration)
39 {
40 try
41 {
42 auto& scn = gen.get_scene();
43 scn.unload();
44 auto& ctx = engine::context();
45 defaults::create_default_3d_scene_for_asset_preview(ctx, scn, asset, {256, 256});
46
47 delta_t dt(0.016667f);
48
49 auto& rpath = ctx.get_cached<rendering_system>();
50 rpath.on_frame_update(scn, dt);
51 rpath.on_frame_before_render(scn, dt);
52 auto new_fbo = rpath.render_scene(scn, dt);
53 thumbnail.set(new_fbo);
54 }
55 catch(const std::exception& e)
56 {
57 APPLOG_ERROR("{}", e.what());
58 }
59 }
60
61 return current_fbo;
62}
63
64template<typename T>
65auto get_thumbnail_impl(thumbnail_manager::generator& gen,
66 const asset_handle<T>& asset,
67 const asset_handle<gfx::texture>& transparent,
69{
70 if(!asset.is_valid())
71 {
72 return transparent.get();
73 }
74
75 if(!asset.is_ready())
76 {
77 return loading.get();
78 }
79
80 return make_thumbnail(gen, asset);
81}
82
83} // namespace
84
85
86template<>
88{
89 auto thumbnail = get_thumbnail_impl(gen_, asset, thumbnails_.transparent, thumbnails_.loading);
90
91 if(thumbnail)
92 {
93 return thumbnail;
94 }
95
96 return thumbnails_.mesh.get();
97}
98
99template<>
101{
102 auto thumbnail = get_thumbnail_impl(gen_, asset, thumbnails_.transparent, thumbnails_.loading);
103
104 if(thumbnail)
105 {
106 return thumbnail;
107 }
108
109 return thumbnails_.material.get();
110}
111
112
113template<>
115{
116 if(!asset.is_valid())
117 {
118 return thumbnails_.transparent.get();
119 }
120 return !asset.is_ready() ? thumbnails_.loading.get() : thumbnails_.ui_tree.get();
121}
122
123template<>
125{
126 if(!asset.is_valid())
127 {
128 return thumbnails_.transparent.get();
129 }
130 return !asset.is_ready() ? thumbnails_.loading.get() : thumbnails_.style_sheet.get();
131}
132
133template<>
135{
136 if(!asset.is_valid())
137 {
138 return thumbnails_.transparent.get();
139 }
140 return !asset.is_ready() ? thumbnails_.loading.get() : thumbnails_.script.get();
141}
142
143template<>
146{
147 if(!asset.is_valid())
148 {
149 return thumbnails_.transparent.get();
150 }
151 return !asset.is_ready() ? thumbnails_.loading.get() : thumbnails_.physics_material.get();
152}
153
154template<>
156{
157 if(!asset.is_valid())
158 {
159 return thumbnails_.transparent.get();
160 }
161 return !asset.is_ready() ? thumbnails_.loading.get() : thumbnails_.audio_clip.get();
162}
163
164template<>
166{
167 if(!asset.is_valid())
168 {
169 return thumbnails_.transparent.get();
170 }
171 return !asset.is_ready() ? thumbnails_.loading.get() : thumbnails_.font.get();
172}
173
174template<>
176{
177 if(!asset.is_valid())
178 {
179 return thumbnails_.transparent.get();
180 }
181 return !asset.is_ready() ? thumbnails_.loading.get() : thumbnails_.animation.get();
182}
183
184template<>
185auto thumbnail_manager::get_thumbnail<gfx::texture>(const asset_handle<gfx::texture>& asset) -> gfx::texture::ptr
186{
187 if(!asset.is_valid())
188 {
189 return thumbnails_.transparent.get();
190 }
191
192 return !asset.is_ready() ? thumbnails_.loading.get() : asset.get();
193}
194
195template<>
196auto thumbnail_manager::get_thumbnail<gfx::shader>(const asset_handle<gfx::shader>& asset) -> gfx::texture::ptr
197{
198 if(!asset.is_valid())
199 {
200 return thumbnails_.transparent.get();
201 }
202 return !asset.is_ready() ? thumbnails_.loading.get() : thumbnails_.shader.get();
203}
204
205template<>
207{
208 auto thumbnail = get_thumbnail_impl(gen_, asset, thumbnails_.transparent, thumbnails_.loading);
209
210 if(thumbnail)
211 {
212 return thumbnail;
213 }
214
215 return thumbnails_.prefab.get();
216}
217
218template<>
220{
221 if(!asset.is_valid())
222 {
223 return thumbnails_.transparent.get();
224 }
225 return !asset.is_ready() ? thumbnails_.loading.get() : thumbnails_.scene_prefab.get();
226}
227
229{
230 fs::error_code ec;
231 if(fs::is_directory(path, ec))
232 {
233 return thumbnails_.folder.get();
234 }
235
236 return thumbnails_.file.get();
237}
238
240{
241 gen_.thumbnails[uid].needs_regeneration = true;
242}
243void thumbnail_manager::remove_thumbnail(const hpp::uuid& uid)
244{
245 gen_.thumbnails.erase(uid);
246}
247
249{
250 gen_.thumbnails.clear();
251}
252
254{
256
257 if(e.all_of<camera_component>())
258 {
259 icon = gimzmo_icons_.camera;
260 }
261
262 if(e.all_of<light_component>())
263 {
264 const auto& light_comp = e.get<light_component>();
265 const auto& light = light_comp.get_light();
266
267 auto type = [&]() -> asset_handle<gfx::texture>
268 {
269 switch(light.type)
270 {
272 {
273 if(e.all_of<skylight_component>())
274 return gimzmo_icons_.sky_light;
275
276 return gimzmo_icons_.directional_light;
277 }
279 return gimzmo_icons_.point_light;
280 case light_type::spot:
281 return gimzmo_icons_.spot_light;
282 default:
283 return gimzmo_icons_.sky_light;
284 }
285 }();
286
287 icon = type;
288 }
289
290 if(e.all_of<reflection_probe_component>())
291 {
292 icon = gimzmo_icons_.reflection_probe;
293 }
294
295 if(e.all_of<audio_source_component>())
296 {
297 icon = gimzmo_icons_.audio_source;
298 }
299
300 return icon.get();
301}
302
303
305{
306 APPLOG_TRACE("{}::{}", hpp::type_name_str(*this), __func__);
307
308 auto& ev = ctx.get_cached<events>();
309 ev.on_frame_update.connect(sentinel_, this, &thumbnail_manager::on_frame_update);
310
311 auto& am = ctx.get_cached<asset_manager>();
312 thumbnails_.transparent = am.get_asset<gfx::texture>("engine:/data/textures/transparent.png");
313
314 thumbnails_.file = am.get_asset<gfx::texture>("editor:/data/icons/file.png");
315 thumbnails_.folder = am.get_asset<gfx::texture>("editor:/data/icons/folder.png");
316 thumbnails_.folder_empty = am.get_asset<gfx::texture>("editor:/data/icons/folder_empty.png");
317 thumbnails_.loading = am.get_asset<gfx::texture>("editor:/data/icons/loading.png");
318 thumbnails_.font = am.get_asset<gfx::texture>("editor:/data/icons/font.png");
319 thumbnails_.shader = am.get_asset<gfx::texture>("editor:/data/icons/shader.png");
320 thumbnails_.material = am.get_asset<gfx::texture>("editor:/data/icons/material.png");
321 thumbnails_.physics_material = am.get_asset<gfx::texture>("editor:/data/icons/material.png");
322 thumbnails_.mesh = am.get_asset<gfx::texture>("editor:/data/icons/mesh.png");
323 thumbnails_.animation = am.get_asset<gfx::texture>("editor:/data/icons/animation.png");
324 thumbnails_.prefab = am.get_asset<gfx::texture>("editor:/data/icons/prefab.png");
325 thumbnails_.scene_prefab = am.get_asset<gfx::texture>("editor:/data/icons/scene.png");
326 thumbnails_.audio_clip = am.get_asset<gfx::texture>("editor:/data/icons/sound.png");
327 thumbnails_.script = am.get_asset<gfx::texture>("editor:/data/icons/script.png");
328 thumbnails_.ui_tree = am.get_asset<gfx::texture>("editor:/data/icons/rhtml.png");
329 thumbnails_.style_sheet = am.get_asset<gfx::texture>("editor:/data/icons/rcss.png");
330
331 gimzmo_icons_.camera = am.get_asset<gfx::texture>("editor:/data/icons/camera.png");
332 gimzmo_icons_.sky_light = am.get_asset<gfx::texture>("editor:/data/icons/sky_light.png");
333 gimzmo_icons_.directional_light = am.get_asset<gfx::texture>("editor:/data/icons/directional_light.png");
334 gimzmo_icons_.point_light = am.get_asset<gfx::texture>("editor:/data/icons/point_light.png");
335 gimzmo_icons_.spot_light = am.get_asset<gfx::texture>("editor:/data/icons/spot_light.png");
336 gimzmo_icons_.audio_source = am.get_asset<gfx::texture>("editor:/data/icons/audio_source.png");
337 gimzmo_icons_.reflection_probe = am.get_asset<gfx::texture>("editor:/data/icons/reflection_probe.png");
338 return true;
339}
340
342{
343 APPLOG_TRACE("{}::{}", hpp::type_name_str(*this), __func__);
344
345 return true;
346}
347
352
354{
355 if(!thumbnail)
356 {
357 return nullptr;
358 }
359
360 return thumbnail->get_texture();
361}
362
368
370{
371 reset_wait();
372 remaining--;
373 return scenes[remaining];
374}
375
377{
378 if(wait_frames-- <= 0)
379 {
380 for(auto& scn : scenes)
381 {
382 scn.unload();
383 }
384 remaining = scenes.size();
385
386 reset_wait();
387 }
388}
389
391{
392 wait_frames = 1;
393}
394
395} // namespace unravel
manifold_type type
Manages assets, including loading, unloading, and storage.
auto get_asset(const std::string &key, load_flags flags=load_flags::standard) -> asset_handle< T >
Gets an asset by its key.
Class that contains core data for audio sources.
Class that contains core camera data, used for rendering and other purposes.
Class that contains core light data, used for rendering and other purposes.
Class that contains core reflection probe data, used for rendering and other purposes.
Class that contains sky light data.
std::chrono::duration< float > delta_t
const char * icon
#define APPLOG_ERROR(...)
Definition logging.h:20
#define APPLOG_TRACE(...)
Definition logging.h:17
Represents a handle to an asset, providing access and management functions.
auto get(bool wait=true) const -> std::shared_ptr< T >
Gets the shared pointer to the asset.
static void create_default_3d_scene_for_asset_preview(rtti::context &ctx, scene &scn, const asset_handle< T > &asset, const usize32_t &size)
Creates a default 3D scene for asset preview.
static auto context() -> rtti::context &
Definition engine.cpp:115
hpp::event< void(rtti::context &, delta_t)> on_frame_update
Definition events.h:16
Struct representing a light.
Definition light.h:87
light_type type
The type of the light.
Definition light.h:89
Represents a scene in the ACE framework, managing entities and their relationships.
Definition scene.h:21
std::map< hpp::uuid, generated_thumbnail > thumbnails
auto init(rtti::context &ctx) -> bool
auto get_thumbnail(const asset_handle< T > &asset) -> gfx::texture::ptr
auto deinit(rtti::context &ctx) -> bool
auto get_gizmo_icon(entt::handle e) -> gfx::texture::ptr
void on_frame_update(rtti::context &ctx, delta_t)
void remove_thumbnail(const hpp::uuid &uid)
void regenerate_thumbnail(const hpp::uuid &uid)