// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
using System;
using System.Collections.Generic;
using FlaxEditor.Content;
using FlaxEngine.GUI;
namespace FlaxEditor.GUI.Drag
{
///
/// Drag content items handler.
///
public sealed class DragItems : DragItems
{
///
/// Initializes a new instance of the class.
///
/// The validation function
public DragItems(Func validateFunction)
: base(validateFunction)
{
}
}
///
/// Helper class for handling drag and drop.
///
///
public class DragItems : DragHelper where U : DragEventArgs
{
///
/// The default prefix for drag data used for .
///
public const string DragPrefix = "ASSET!?";
///
/// Creates a new DragHelper
///
/// The validation function
public DragItems(Func validateFunction)
: base(validateFunction)
{
}
///
/// Gets the drag data for the given file.
///
/// The path.
/// The data.
public DragData ToDragData(string path) => GetDragData(path);
///
public override DragData ToDragData(ContentItem item) => GetDragData(item);
///
public override DragData ToDragData(IEnumerable items) => GetDragData(items);
///
/// Gets the drag data.
///
/// The path.
/// The data.
public static DragData GetDragData(string path)
{
if (path == null)
throw new ArgumentNullException();
return new DragDataText(DragPrefix + path);
}
///
/// Gets the drag data.
///
/// The item.
/// The data.
public static DragDataText GetDragData(ContentItem item)
{
if (item == null)
throw new ArgumentNullException();
return new DragDataText(DragPrefix + item.Path);
}
///
/// Gets the drag data.
///
/// The items.
/// The data.
public static DragData GetDragData(IEnumerable items)
{
if (items == null)
throw new ArgumentNullException();
string text = DragPrefix;
foreach (var item in items)
text += item.Path + '\n';
return new DragDataText(text);
}
///
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
var obj = Editor.Instance.ContentDatabase.Find(paths[i]);
// Check it
if (obj != null)
results.Add(obj);
}
return results.ToArray();
}
}
return new ContentItem[0];
}
}
}