// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
using System;
using FlaxEngine;
using FlaxEngine.GUI;
namespace FlaxEditor.Content
{
///
/// Asset item object.
///
///
[HideInEditor]
public abstract class AssetItem : ContentItem
{
///
/// Gets the asset unique identifier.
///
public Guid ID { get; protected set; }
///
/// Gets the asset type identifier.
///
public string TypeName { get; }
///
/// Returns true if asset is now loaded.
///
public bool IsLoaded => FlaxEngine.Content.GetAsset(ID)?.IsLoaded ?? false;
///
/// Initializes a new instance of the class.
///
/// The asset path.
/// The asset type name.
/// The asset identifier.
protected AssetItem(string path, string typeName, ref Guid id)
: base(path)
{
TypeName = typeName;
ID = id;
}
private sealed class TooltipDoubleClickHook : Control
{
public AssetItem Item;
public TooltipDoubleClickHook()
{
AnchorPreset = AnchorPresets.StretchAll;
Offsets = Margin.Zero;
}
public override bool OnMouseDoubleClick(Float2 location, MouseButton button)
{
return Item.OnMouseDoubleClick(Item.PointFromScreen(PointToScreen(location)), button);
}
}
///
public override void OnTooltipShown(Tooltip tooltip)
{
base.OnTooltipShown(tooltip);
// Inject the hook control for the double-click event (if user double-clicks on tooltip over the asset item it will open that item - helps on small screens)
var hook = tooltip.GetChild();
if (hook == null)
hook = tooltip.AddChild();
hook.Item = this;
}
///
public override ContentItemType ItemType => ContentItemType.Asset;
///
public override string TypeDescription
{
get
{
// Translate asset type name
var typeName = TypeName;
string[] typeNamespaces = typeName.Split('.');
if (typeNamespaces.Length != 0 && typeNamespaces.Length != 0)
{
typeName = Utilities.Utils.GetPropertyNameUI(typeNamespaces[typeNamespaces.Length - 1]);
}
return typeName;
}
}
///
/// Loads the asset.
///
/// The asset object.
public Asset LoadAsync()
{
return FlaxEngine.Content.LoadAsync(ID);
}
///
/// Reloads the asset (if it's loaded).
///
public void Reload()
{
var asset = FlaxEngine.Content.GetAsset(ID);
if (asset != null && asset.IsLoaded)
{
asset.Reload();
}
}
///
/// Determines whether asset is of the specified type (included inheritance checks).
///
/// The type to check.
/// true if asset is of the specified type (including inherited types); otherwise, false.
public bool IsOfType()
{
return IsOfType(typeof(T));
}
///
/// Determines whether asset is of the specified type (included inheritance checks).
///
/// The type to check.
/// true if asset is of the specified type (including inherited types); otherwise, false.
public virtual bool IsOfType(Type type)
{
return false;
}
///
/// Called when user dags this item into editor viewport or scene tree node.
///
/// The editor context (eg. editor viewport or scene tree node).
/// True if item can be dropped in, otherwise false.
public virtual bool OnEditorDrag(object context)
{
return false;
}
///
/// Called when user drops the item into editor viewport or scene tree node.
///
/// The editor context (eg. editor viewport or scene tree node).
/// The spawned object.
public virtual Actor OnEditorDrop(object context)
{
throw new NotSupportedException($"Asset {GetType()} doesn't support dropping into viewport.");
}
///
protected override bool DrawShadow => true;
///
public override ContentItem Find(Guid id)
{
return id == ID ? this : null;
}
///
public override string ToString()
{
return Path + ":" + ID;
}
}
}