// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
using System;
using System.Collections.Generic;
using FlaxEngine.GUI;
namespace FlaxEditor.GUI.Drag
{
///
/// Generic items names collection drag handler.
///
public sealed class DragNames : DragNames
{
///
/// Initializes a new instance of the class.
///
/// The drag data prefix.
/// The validation function.
public DragNames(string prefix, Func validateFunction)
: base(prefix, validateFunction)
{
}
}
///
/// Helper class for handling generic items names drag and drop.
///
public class DragNames : DragHelper where U : DragEventArgs
{
///
/// Gets the drag data prefix.
///
public string DragPrefix { get; }
///
/// Creates a new DragHelper
///
/// The drag data prefix.
/// The validation function.
public DragNames(string prefix, Func validateFunction)
: base(validateFunction)
{
DragPrefix = prefix;
}
///
public override DragData ToDragData(string track) => GetDragData(DragPrefix, track);
///
public override DragData ToDragData(IEnumerable tracks) => GetDragData(DragPrefix, tracks);
///
/// Gets the drag data.
///
/// The drag dat prefix.
/// The name.
/// The data.
public static DragData GetDragData(string dragPrefix, string name)
{
if (name == null)
throw new ArgumentNullException();
return new DragDataText(dragPrefix + name);
}
///
/// Gets the drag data.
///
/// The drag dat prefix.
/// The names.
/// The data.
public static DragData GetDragData(string dragPrefix, IEnumerable names)
{
if (names == null)
throw new ArgumentNullException();
string text = dragPrefix;
foreach (var item in names)
text += item + '\n';
return new DragDataText(text);
}
///
/// Tries to parse the drag data to extract generic items names 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 split names
return dataText.Text.Remove(0, DragPrefix.Length).Split('\n');
}
}
return new string[0];
}
}
}