#if UNITY_EDITOR using UnityEngine; using UnityEngine.UIElements; using System.Collections.Generic; namespace GSpawn { public abstract class GridViewItem : VisualElement where TData : IUIItemStateProvider { public delegate void DragBeginHandler(GridViewItem item); public delegate void DragEnterHandler(GridViewItem item); public delegate void DragLeaveHandler(GridViewItem item); public delegate void DragPerformHandler(); public DragBeginHandler dragBegin; public DragEnterHandler dragEnter; public DragLeaveHandler dragLeave; public DragPerformHandler dragPerform; protected Label _displayNameLabel; private bool _visible = true; private IGridView _gridView; private ItemDragState _dragState = ItemDragState.AtRest; private HashSet _dragInitiators = new HashSet(); private HashSet _tags = new HashSet(); public TData data { get; set; } public bool itemVisible { get { return _visible; } } public int numTags { get { return _tags.Count; } } public abstract string displayName { get; } public abstract PluginGuid guid { get; } public abstract Vector2 imageSize { get; set; } public bool selected { get { return data.uiSelected; } } public CopyPasteMode copyPasteMode { get { return data.uiCopyPasteMode; } } public GridViewItem() { style.flexDirection = FlexDirection.Column; style.flexWrap = Wrap.NoWrap; style.setBorderWidth(1.0f); style.setBorderColor(Color.black); _dragInitiators.Add(this); RegisterCallback(onDragEnter); RegisterCallback(onDragLeave); RegisterCallback(onDragPerform); RegisterCallback(onDragExit); RegisterCallback(onMouseDown); RegisterCallback(onMouseUp); RegisterCallback(onMouseMove); } public void initialize(IGridView gridView, TData data) { if (_gridView != null) return; _gridView = gridView; this.data = data; } public void buildUI() { onBuildUI(); refreshUI(); } public void refreshUI() { if (GSpawn.active == null) return; _displayNameLabel.text = displayName; updateBkColor(); onRefreshUI(); } public void addTag(string tag) { _tags.Add(tag); } public bool hasTag(string tag) { return _tags.Contains(tag); } public bool removeTag(string tag) { return _tags.Remove(tag); } public void setSelected(bool selected) { data.uiSelected = selected; updateBkColor(); onSelectedStateChanged(selected); } public bool setCopyPasteMode(CopyPasteMode copyPasteMode) { if (copyPasteMode != CopyPasteMode.None && !canBeCopyPasteSource()) return false; data.uiCopyPasteMode = copyPasteMode; applyCopyPasteModeStates(); return true; } public void setVisible(bool visible) { if (_visible == visible) return; _visible = visible; this.setDisplayVisible(itemVisible); } public virtual bool canBeCopyPasteSource() { return false; } protected virtual void onRefreshUI() { } protected abstract void onBuildUI(); protected virtual void onSelectedStateChanged(bool selected) { } protected virtual void applyCopyPasteModeStates() { } protected void addDragAndDropInitiator(VisualElement initiator) { _dragInitiators.Add(initiator); } private void updateBkColor() { style.backgroundColor = selected ? UIValues.selectedListItemColor : UIValues.unselectedListItemColor; } private void onMouseDown(MouseDownEvent e) { if (e.button == (int)MouseButton.LeftMouse && _dragInitiators.Contains((VisualElement)e.target)) { _dragState = ItemDragState.Ready; } } private void onMouseUp(MouseUpEvent e) { _dragState = ItemDragState.AtRest; } private void onMouseMove(MouseMoveEvent e) { if (_dragState == ItemDragState.Ready) { if (Event.current.type == EventType.MouseDrag) { PluginDragAndDrop.beginDrag(PluginDragAndDropTitles.gridViewItem, _gridView.dragAndDropInitiatorId, _gridView.dragAndDropData); if (dragBegin != null) dragBegin(this); } _dragState = ItemDragState.Dragging; } } private bool _calledOnDragEnter = false; private void onDragEnter(DragEnterEvent e) { _calledOnDragEnter = true; if (dragEnter != null) dragEnter(this); } private void onDragLeave(DragLeaveEvent e) { _calledOnDragEnter = false; updateBkColor(); if (dragLeave != null) dragLeave(this); } private void onDragPerform(DragPerformEvent e) { if (!_calledOnDragEnter) return; _calledOnDragEnter = false; updateBkColor(); if (dragPerform != null) dragPerform(); } private void onDragExit(DragExitedEvent e) { _calledOnDragEnter = false; PluginDragAndDrop.endDrag(); } } } #endif