// 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
{
///
/// Script items drag handler.
///
public sealed class DragScriptItems : DragScriptItems
{
///
/// Initializes a new instance of the class.
///
/// The validation function
public DragScriptItems(Func validateFunction)
: base(validateFunction)
{
}
}
///
/// Helper class for handling drag and drop.
///
///
public class DragScriptItems : 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 DragScriptItems(Func validateFunction)
: base(validateFunction)
{
}
///
public override DragData ToDragData(ScriptItem item) => GetDragData(item);
///
public override DragData ToDragData(IEnumerable items) => GetDragData(items);
///
/// Gets the drag data.
///
/// The item.
/// The data.
public static DragData GetDragData(ScriptItem 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.
///
/// The data.
///
/// Gathered objects or empty IEnumerable 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
var obj = Editor.Instance.ContentDatabase.FindScript(paths[i]);
// Check it
if (obj != null)
results.Add(obj);
}
return results.ToArray();
}
}
return new ScriptItem[0];
}
}
}