From 3a591577ad61364acb1a0f3c310762f545884253 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Wed, 22 Jan 2025 21:46:21 -0600 Subject: [PATCH 1/2] Add drag drop to `FilePathEditor` --- .../CustomEditors/Editors/AssetRefEditor.cs | 92 ++++++++++++++++++- 1 file changed, 91 insertions(+), 1 deletion(-) diff --git a/Source/Editor/CustomEditors/Editors/AssetRefEditor.cs b/Source/Editor/CustomEditors/Editors/AssetRefEditor.cs index 83fa1f129..f22d4daae 100644 --- a/Source/Editor/CustomEditors/Editors/AssetRefEditor.cs +++ b/Source/Editor/CustomEditors/Editors/AssetRefEditor.cs @@ -4,6 +4,7 @@ using System; using System.Linq; using FlaxEditor.Content; using FlaxEditor.GUI; +using FlaxEditor.GUI.Drag; using FlaxEditor.Scripting; using FlaxEngine; using FlaxEngine.GUI; @@ -156,6 +157,17 @@ namespace FlaxEditor.CustomEditors.Editors private Rectangle DropdownRect => new Rectangle(Width - DropdownIconSize - DropdownIconMargin, DropdownIconMargin, DropdownIconSize, DropdownIconSize); public Action ShowPicker; + public Action OnAssetDropped; + + private DragItems _dragItems; + private DragHandlers _dragHandlers; + private bool _hasValidDragOver; + private Func _validate; + + public void SetValidationMethod(Func validate) + { + _validate = validate; + } public override void Draw() { @@ -164,6 +176,14 @@ namespace FlaxEditor.CustomEditors.Editors var style = FlaxEngine.GUI.Style.Current; var dropdownRect = DropdownRect; Render2D.DrawSprite(style.ArrowDown, dropdownRect, Enabled ? (DropdownRect.Contains(PointFromWindow(RootWindow.MousePosition)) ? style.BorderSelected : style.Foreground) : style.ForegroundDisabled); + + // Check if drag is over + if (IsDragOver && _hasValidDragOver) + { + var bounds = new Rectangle(Float2.Zero, Size); + Render2D.FillRectangle(bounds, style.Selection); + Render2D.DrawRectangle(bounds, style.SelectionBorder); + } } public override bool OnMouseDown(Float2 location, MouseButton button) @@ -207,6 +227,68 @@ namespace FlaxEditor.CustomEditors.Editors return result; } } + + private DragDropEffect DragEffect => _hasValidDragOver ? DragDropEffect.Move : DragDropEffect.None; + + /// + public override DragDropEffect OnDragEnter(ref Float2 location, DragData data) + { + base.OnDragEnter(ref location, data); + + // Ensure to have valid drag helpers (uses lazy init) + if (_dragItems == null) + _dragItems = new DragItems(ValidateDragAsset); + if (_dragHandlers == null) + { + _dragHandlers = new DragHandlers + { + _dragItems, + }; + } + + _hasValidDragOver = _dragHandlers.OnDragEnter(data) != DragDropEffect.None; + + + return DragEffect; + } + + private bool ValidateDragAsset(ContentItem contentItem) + { + // Load or get asset + return _validate?.Invoke(contentItem) ?? false; + } + + /// + public override DragDropEffect OnDragMove(ref Float2 location, DragData data) + { + base.OnDragMove(ref location, data); + + return DragEffect; + } + + /// + public override void OnDragLeave() + { + _hasValidDragOver = false; + _dragHandlers.OnDragLeave(); + + base.OnDragLeave(); + } + + /// + public override DragDropEffect OnDragDrop(ref Float2 location, DragData data) + { + var result = DragEffect; + + base.OnDragDrop(ref location, data); + + if (_dragItems.HasValidDrag) + { + OnAssetDropped(_dragItems.Objects[0]); + } + + return result; + } } private TextBoxWithPicker _textBox; @@ -221,13 +303,21 @@ namespace FlaxEditor.CustomEditors.Editors { if (HasDifferentTypes) return; + + _validator = new AssetPickerValidator(ScriptType.Null); _textBox = layout.Custom().CustomControl; _textBox.ShowPicker = OnShowPicker; + _textBox.OnAssetDropped = OnAssetDropped; _textBox.EditEnd += OnEditEnd; - _validator = new AssetPickerValidator(ScriptType.Null); + _textBox.SetValidationMethod(_validator.IsValid); AssetRefEditor.ApplyAssetReferenceAttribute(Values, out _, _validator); } + private void OnAssetDropped(ContentItem item) + { + SetPickerPath(item); + } + private void OnShowPicker() { if (_validator.AssetType != ScriptType.Null) From 030befdcaa8b80d06a047794d0602f7a75365a57 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Wed, 22 Jan 2025 21:47:28 -0600 Subject: [PATCH 2/2] Rename dropped method. --- Source/Editor/CustomEditors/Editors/AssetRefEditor.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/Editor/CustomEditors/Editors/AssetRefEditor.cs b/Source/Editor/CustomEditors/Editors/AssetRefEditor.cs index f22d4daae..231a4de8a 100644 --- a/Source/Editor/CustomEditors/Editors/AssetRefEditor.cs +++ b/Source/Editor/CustomEditors/Editors/AssetRefEditor.cs @@ -307,13 +307,13 @@ namespace FlaxEditor.CustomEditors.Editors _validator = new AssetPickerValidator(ScriptType.Null); _textBox = layout.Custom().CustomControl; _textBox.ShowPicker = OnShowPicker; - _textBox.OnAssetDropped = OnAssetDropped; + _textBox.OnAssetDropped = OnItemDropped; _textBox.EditEnd += OnEditEnd; _textBox.SetValidationMethod(_validator.IsValid); AssetRefEditor.ApplyAssetReferenceAttribute(Values, out _, _validator); } - private void OnAssetDropped(ContentItem item) + private void OnItemDropped(ContentItem item) { SetPickerPath(item); }