// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved. using System.Collections.Generic; using System.IO; using FlaxEditor.Content; using FlaxEditor.GUI.Drag; using FlaxEngine; using FlaxEngine.GUI; namespace FlaxEditor.Surface { public partial class VisjectSurface { private readonly DragAssets _dragAssets; private readonly DragNames _dragParameters; /// /// The custom drag drop event arguments. /// /// public class DragDropEventArgs : DragEventArgs { /// /// The surface location. /// public Float2 SurfaceLocation; } /// /// Drag and drop handlers. /// public readonly DragHandlers DragHandlers = new DragHandlers(); /// public override DragDropEffect OnDragEnter(ref Float2 location, DragData data) { var result = base.OnDragEnter(ref location, data); if (result != DragDropEffect.None) return result; return DragHandlers.OnDragEnter(data); } /// public override DragDropEffect OnDragMove(ref Float2 location, DragData data) { var result = base.OnDragMove(ref location, data); if (result != DragDropEffect.None) return result; return DragHandlers.Effect; } /// public override void OnDragLeave() { DragHandlers.OnDragLeave(); base.OnDragLeave(); } /// public override DragDropEffect OnDragDrop(ref Float2 location, DragData data) { var result = base.OnDragDrop(ref location, data); if (result != DragDropEffect.None) return result; var args = new DragDropEventArgs { SurfaceLocation = _rootControl.PointFromParent(ref location) }; // Drag assets if (_dragAssets.HasValidDrag) { result = _dragAssets.Effect; // Process items HandleDragDropAssets(_dragAssets.Objects, args); } // Drag parameters else if (_dragParameters.HasValidDrag) { result = _dragParameters.Effect; // Process items HandleDragDropParameters(_dragParameters.Objects, args); } DragHandlers.OnDragDrop(args); return result; } /// /// Validates the asset items drag operation. /// /// The asset item. /// True if can drag that item, otherwise false. protected virtual bool ValidateDragItem(AssetItem assetItem) { return false; } /// /// Validates the parameter drag operation. /// /// Name of the parameter. /// True if can drag that parameter, otherwise false. protected virtual bool ValidateDragParameter(string parameterName) { return GetParameter(parameterName) != null; } /// /// Handles the drag drop assets action. /// /// The objects. /// The drag drop arguments data. protected virtual void HandleDragDropAssets(List objects, DragDropEventArgs args) { } /// /// Handles the drag drop surface parameters action. /// /// The objects. /// The drag drop arguments data. protected virtual void HandleDragDropParameters(List objects, DragDropEventArgs args) { var arch = GetParameterGetterNodeArchetype(out var groupId); for (int i = 0; i < objects.Count; i++) { var parameter = GetParameter(objects[i]); if (parameter == null) throw new InvalidDataException(); var values = (object[])arch.DefaultValues.Clone(); values[0] = parameter.ID; var node = Context.SpawnNode(groupId, arch.TypeID, args.SurfaceLocation, values); if (node != null) args.SurfaceLocation.X += node.Width + 10; } } /// /// Gets the parameter getter node archetype to use. /// /// The group ID. /// The node archetype. protected virtual NodeArchetype GetParameterGetterNodeArchetype(out ushort groupId) { groupId = 6; return Archetypes.Parameters.Nodes[0]; } } }