// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved. using System; using System.Collections.Generic; using FlaxEngine; using FlaxEngine.GUI; namespace FlaxEditor.GUI.Drag { /// /// The drag events helper object. /// [HideInEditor] public abstract class DragHelper { /// /// Gets a value indicating whether this instance has valid drag. /// public abstract bool HasValidDrag { get; } /// /// Gets the drag effect. /// public abstract DragDropEffect Effect { get; } /// /// Called when drag enters. /// /// The data. /// True if handled. public abstract bool OnDragEnter(DragData data); /// /// Called when drag leaves. /// public abstract void OnDragLeave(); /// /// Called when drag drops. /// public abstract void OnDragDrop(); /// /// Called when drag drops. /// /// The instance containing the event data. public abstract void OnDragDrop(DragEventArgs dragEventArgs); } /// /// Base class for drag and drop operation helpers. /// /// Type of the objects to collect from drag data. /// Type of the drag-drop event. public abstract class DragHelper : DragHelper where U : DragEventArgs { /// /// The objects gathered. /// public readonly List Objects = new List(); /// /// Gets a value indicating whether this instance has valid drag data. /// public sealed override bool HasValidDrag => Objects.Count > 0; /// /// Gets the current drag effect. /// public override DragDropEffect Effect => HasValidDrag ? DragDropEffect.Move : DragDropEffect.None; /// /// The validation function /// protected Func ValidateFunction { get; set; } /// /// Creates a new DragHelper /// /// The validation function protected DragHelper(Func validateFunction) { ValidateFunction = validateFunction; } /// /// Gets the drag data. /// /// An item. /// The data. public abstract DragData ToDragData(T item); /// /// Gets the drag data. /// /// The items. /// The data. public abstract DragData ToDragData(IEnumerable items); /// /// Tries to parse the drag data. /// /// The data. /// Gathered objects or empty IEnumerable if cannot get any valid. public abstract IEnumerable FromDragData(DragData data); /// /// Handler drag drop event. /// /// The drag event arguments. /// The item. public virtual void DragDrop(U dragEventArgs, IEnumerable item) { } /// /// Invalids the drag data. /// public void InvalidDrag() { Objects.Clear(); } /// /// Called when drag enters. /// /// The data. /// True if drag event is valid and can be performed, otherwise false. public sealed override bool OnDragEnter(DragData data) { if (data == null || ValidateFunction == null) throw new ArgumentNullException(); Objects.Clear(); var items = FromDragData(data); foreach (var item in items) { if (ValidateFunction(item)) Objects.Add(item); } return HasValidDrag; } /// /// Called when drag leaves. /// public override void OnDragLeave() { Objects.Clear(); } /// /// Called when drag drops. /// public sealed override void OnDragDrop() { if (HasValidDrag) DragDrop(null, Objects); Objects.Clear(); } /// /// Called when drag drops. /// /// Arguments public sealed override void OnDragDrop(DragEventArgs dragEventArgs) { if (HasValidDrag) DragDrop(dragEventArgs as U, Objects); Objects.Clear(); } } }