// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
using System;
using FlaxEngine;
namespace FlaxEditor.Content.Thumbnails
{
///
/// Contains information about asset thumbnail rendering.
///
[HideInEditor]
public class ThumbnailRequest
{
///
/// The request state types.
///
public enum States
{
///
/// The initial state.
///
Created,
///
/// Request has been prepared for the rendering but still may wait for resources to load fully.
///
Prepared,
///
/// The thumbnail has been rendered. Request can be finalized.
///
Rendered,
///
/// The finalized state.
///
Disposed,
};
///
/// Gets the state.
///
public States State { get; private set; } = States.Created;
///
/// The item.
///
public readonly AssetItem Item;
///
/// The proxy object for the asset item.
///
public readonly AssetProxy Proxy;
///
/// The asset reference. May be null if not cached yet.
///
public Asset Asset;
///
/// The custom tag object used by the thumbnails rendering pipeline. Can be used to store the data related to the thumbnail rendering by the asset proxy.
///
public object Tag;
///
/// Determines whether thumbnail can be drawn for the item.
///
public bool IsReady => State == States.Prepared && Asset && Asset.IsLoaded && Proxy.CanDrawThumbnail(this);
///
/// Initializes a new instance of the class.
///
/// The item.
/// The proxy.
public ThumbnailRequest(AssetItem item, AssetProxy proxy)
{
Item = item;
Proxy = proxy;
}
///
/// Prepares this request.
///
public void Prepare()
{
if (State != States.Created)
throw new InvalidOperationException();
// Prepare
Asset = FlaxEngine.Content.LoadAsync(Item.Path);
Proxy.OnThumbnailDrawPrepare(this);
State = States.Prepared;
}
///
/// Finishes the rendering and updates the item thumbnail.
///
/// The icon.
public void FinishRender(ref SpriteHandle icon)
{
if (State != States.Prepared)
throw new InvalidOperationException();
Item.Thumbnail = icon;
State = States.Rendered;
}
///
/// Finalizes this request.
///
public void Dispose()
{
if (State == States.Disposed)
throw new InvalidOperationException();
if (State != States.Created)
{
// Cleanup
Proxy.OnThumbnailDrawCleanup(this);
Asset = null;
}
Tag = null;
State = States.Disposed;
}
}
}