// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
using System;
using System.Collections.Generic;
using FlaxEditor.Content;
using FlaxEngine;
using FlaxEngine.GUI;
namespace FlaxEditor.GUI.Drag
{
///
/// Assets collection drag handler.
///
public sealed class DragAssets : DragAssets
{
///
/// Initializes a new instance of the class.
///
/// The validation function
public DragAssets(Func validateFunction)
: base(validateFunction)
{
}
}
///
/// Helper class for handling drag and drop.
///
///
public class DragAssets : DragHelper where U : DragEventArgs
{
///
/// The default prefix for drag data used for .
///
public const string DragPrefix = DragItems.DragPrefix;
///
/// Creates a new DragHelper
///
/// The validation function
public DragAssets(Func validateFunction)
: base(validateFunction)
{
}
///
/// Gets the drag data (finds asset item).
///
/// The asset.
/// The data.
public DragData ToDragData(Asset asset) => GetDragData(asset);
///
public override DragData ToDragData(AssetItem item) => GetDragData(item);
///
public override DragData ToDragData(IEnumerable items) => GetDragData(items);
///
/// Gets the drag data.
///
/// The asset.
/// The data.
public static DragData GetDragData(Asset asset)
{
return DragItems.GetDragData(Editor.Instance.ContentDatabase.Find(asset.ID));
}
///
/// Gets the drag data.
///
/// The item.
/// The data.
public static DragData GetDragData(AssetItem item)
{
return DragItems.GetDragData(item);
}
///
/// Gets the drag data.
///
/// The items.
/// The data.
public static DragData GetDragData(IEnumerable items)
{
return DragItems.GetDragData(items);
}
///
/// Tries to parse the drag data to extract collection.
///
/// The data.
/// Gathered objects or empty array if cannot get any valid.
public override IEnumerable FromDragData(DragData data)
{
if (data is DragDataText dataText)
{
if (dataText.Text.StartsWith(DragPrefix))
{
// Remove prefix and parse spitted names
var paths = dataText.Text.Remove(0, DragPrefix.Length).Split('\n');
var results = new List(paths.Length);
for (int i = 0; i < paths.Length; i++)
{
// Find element
if (Editor.Instance.ContentDatabase.Find(paths[i]) is AssetItem obj)
results.Add(obj);
}
return results.ToArray();
}
}
return new AssetItem[0];
}
}
}