// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
using System;
using System.Collections.Generic;
using FlaxEditor.SceneGraph;
using FlaxEngine;
using FlaxEngine.GUI;
namespace FlaxEditor.GUI.Drag
{
///
/// Actors references collection drag handler.
///
public sealed class DragActors : DragActors
{
///
/// Initializes a new instance of the class.
///
/// The validation function
public DragActors(Func validateFunction)
: base(validateFunction)
{
}
}
///
/// Helper class for handling drag and drop.
///
///
///
public class DragActors : DragHelper where U : DragEventArgs
{
///
/// The default prefix for drag data used for .
///
public const string DragPrefix = "ACTOR!?";
///
/// Creates a new DragHelper
///
/// The validation function
public DragActors(Func validateFunction)
: base(validateFunction)
{
}
///
/// Gets the drag data.
///
/// The actor.
/// The data.
public DragData ToDragData(Actor actor) => GetDragData(actor);
///
public override DragData ToDragData(ActorNode item) => GetDragData(item);
///
public override DragData ToDragData(IEnumerable items) => GetDragData(items);
///
/// Gets the drag data.
///
/// The actor.
/// The data.
public static DragData GetDragData(Actor actor)
{
if (actor == null)
throw new ArgumentNullException();
return new DragDataText(DragPrefix + actor.ID.ToString("N"));
}
///
/// Gets the drag data.
///
/// The item.
/// The data.
public static DragData GetDragData(ActorNode item)
{
if (item == null)
throw new ArgumentNullException();
return new DragDataText(DragPrefix + item.ID.ToString("N"));
}
///
/// 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.ID.ToString("N") + '\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 ids = dataText.Text.Remove(0, DragPrefix.Length).Split('\n');
var results = new List(ids.Length);
for (int i = 0; i < ids.Length; i++)
{
// Find element
if (Guid.TryParse(ids[i], out Guid id))
{
var obj = Editor.Instance.Scene.GetActorNode(id);
// Check it
if (obj != null)
results.Add(obj);
}
}
return results.ToArray();
}
}
return new ActorNode[0];
}
}
}