update UIElements

pull/464/head
何冠峰 2025-02-05 17:26:19 +08:00
parent 698d81a433
commit 7e1dc7ef24
9 changed files with 219 additions and 9 deletions

View File

@ -20,7 +20,7 @@ namespace YooAsset.Editor
/// <summary>
/// 单元列最小宽度
/// </summary>
public int MinWidth = 30;
public int MinWidth = 50;
/// <summary>
/// 单元列最大宽度
@ -52,7 +52,17 @@ namespace YooAsset.Editor
{
HeaderTitle = headerTitle;
Width = width;
MinWidth = width;
MaxWidth = width;
}
public ReportHeader(string headerTitle, int width, int minWidth, int maxWidth)
{
HeaderTitle = headerTitle;
Width = width;
MinWidth = minWidth;
MaxWidth = maxWidth;
}
public ReportHeader SetMinWidth(int value)
{
MinWidth = value;

View File

@ -61,5 +61,11 @@ namespace YooAsset.Editor
HeaderTitles.Add(reportHeader);
return reportHeader;
}
public ReportHeader AddHeader(string headerTitle, int width, int minWidth, int maxWidth)
{
var reportHeader = new ReportHeader(headerTitle, width, minWidth, maxWidth);
HeaderTitles.Add(reportHeader);
return reportHeader;
}
}
}

View File

@ -17,7 +17,7 @@ namespace YooAsset.Editor
{
}
#else
public new class UxmlFactory : UxmlFactory<PanelSplitView, TwoPaneSplitView.UxmlTraits>
public new class UxmlFactory : UxmlFactory<PanelSplitView, UxmlTraits>
{
}
#endif

View File

@ -0,0 +1,111 @@
#if UNITY_2019_4_OR_NEWER
using System;
using UnityEditor;
using UnityEngine;
using UnityEditor.UIElements;
using UnityEngine.UIElements;
namespace YooAsset.Editor
{
public class ResizeHandle : VisualElement
{
/// <summary>
/// Instantiates a TableView using data from a UXML file.
/// </summary>
public new class UxmlFactory : UxmlFactory<ResizeHandle, UxmlTraits>
{
}
private bool _isResizing = false;
private float _initialWidth;
private Vector2 _initialMousePos;
/// <summary>
/// 控制的UI元素
/// </summary>
public VisualElement ControlTarget { get; set; }
/// <summary>
/// 控制元素的最小宽度
/// </summary>
public int ControlMinWidth { get; set; }
/// <summary>
/// 控制元素的最大宽度
/// </summary>
public int ControlMaxWidth { get; set; }
/// <summary>
/// 尺寸发生变化
/// </summary>
public Action<float> ResizeChanged { get; set; }
public ResizeHandle()
{
int defaultWidth = 5;
this.style.width = defaultWidth;
this.style.minWidth = defaultWidth;
this.style.maxWidth = defaultWidth;
this.style.opacity = 0;
this.style.cursor = UIElementsCursor.CreateCursor(MouseCursor.ResizeHorizontal);
this.RegisterCallback<MouseDownEvent>(OnMouseDown);
this.RegisterCallback<MouseMoveEvent>(OnMouseMove);
this.RegisterCallback<MouseUpEvent>(OnMouseUp);
}
public ResizeHandle(int handleWidth, VisualElement controlTarget, int controlMinWidth, int controlMaxWidth)
{
ControlTarget = controlTarget;
ControlMinWidth = controlMinWidth;
ControlMaxWidth = controlMaxWidth;
this.style.width = handleWidth;
this.style.minWidth = handleWidth;
this.style.maxWidth = handleWidth;
this.style.opacity = 0;
this.style.cursor = UIElementsCursor.CreateCursor(MouseCursor.ResizeHorizontal);
this.RegisterCallback<MouseDownEvent>(OnMouseDown);
this.RegisterCallback<MouseMoveEvent>(OnMouseMove);
this.RegisterCallback<MouseUpEvent>(OnMouseUp);
}
private void OnMouseDown(MouseDownEvent evt)
{
// 鼠标左键按下
if (ControlTarget != null && evt.button == 0)
{
_isResizing = true;
_initialWidth = ControlTarget.resolvedStyle.width;
_initialMousePos = evt.mousePosition;
this.CaptureMouse();
}
}
private void OnMouseMove(MouseMoveEvent evt)
{
if (ControlTarget != null && _isResizing)
{
// 计算鼠标移动距离
float deltaX = evt.mousePosition.x - _initialMousePos.x;
// 更新控制元素尺寸
float newWidth = _initialWidth + deltaX;
float width = Mathf.Clamp(newWidth, ControlMinWidth, ControlMaxWidth);
ControlTarget.style.width = width;
ControlTarget.style.minWidth = width;
ControlTarget.style.maxWidth = width;
ResizeChanged?.Invoke(width);
}
}
private void OnMouseUp(MouseUpEvent evt)
{
// 鼠标左键释放
if (ControlTarget != null && evt.button == 0)
{
_isResizing = false;
this.ReleaseMouse();
}
}
}
}
#endif

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: adb565f9b5e9fb3428e54d0cfd58676d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,5 +1,6 @@
#if UNITY_2019_4_OR_NEWER
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEditor.UIElements;
@ -14,6 +15,11 @@ namespace YooAsset.Editor
/// </summary>
internal int ColumnIndex;
/// <summary>
/// 单元元素集合
/// </summary>
internal List<VisualElement> CellElements = new List<VisualElement>(1000);
/// <summary>
/// UI元素名称
/// </summary>

View File

@ -127,15 +127,37 @@ namespace YooAsset.Editor
toolbarBtn.name = column.ElementName;
toolbarBtn.text = column.HeaderTitle;
toolbarBtn.style.unityTextAlign = column.ColumnStyle.TxtAnchor;
toolbarBtn.style.flexGrow = column.ColumnStyle.Stretchable ? 1f : 0f;
toolbarBtn.style.flexGrow = 0;
toolbarBtn.style.width = column.ColumnStyle.Width;
toolbarBtn.style.minWidth = column.ColumnStyle.MinWidth;
toolbarBtn.style.maxWidth = column.ColumnStyle.MaxWidth;
toolbarBtn.style.minWidth = column.ColumnStyle.Width;
toolbarBtn.style.maxWidth = column.ColumnStyle.Width;
toolbarBtn.clickable.clickedWithEventInfo += OnClickTableHead;
SetCellElementStyle(toolbarBtn);
_toolbar.Add(toolbarBtn);
_columns.Add(column);
// 可伸缩控制柄
if (column.ColumnStyle.Stretchable)
{
int handleWidth = 3;
int minWidth = (int)column.ColumnStyle.MinWidth.value;
int maxWidth = (int)column.ColumnStyle.MaxWidth.value;
var resizeHandle = new ResizeHandle(handleWidth, toolbarBtn, minWidth, maxWidth);
resizeHandle.ResizeChanged += (float value) =>
{
float width = Mathf.Clamp(value, column.ColumnStyle.MinWidth.value, column.ColumnStyle.MaxWidth.value);
column.ColumnStyle.Width = width;
foreach (var element in column.CellElements)
{
element.style.width = width;
element.style.minWidth = width;
element.style.maxWidth = width;
}
};
_toolbar.Add(resizeHandle);
}
// 计算索引值
column.ColumnIndex = _columns.Count - 1;
@ -293,8 +315,10 @@ namespace YooAsset.Editor
{
var cellElement = colum.MakeCell.Invoke();
cellElement.name = colum.ElementName;
cellElement.style.flexGrow = 0f;
SetCellElementStyle(cellElement);
listViewElement.Add(cellElement);
colum.CellElements.Add(cellElement);
}
return listViewElement;
}
@ -314,12 +338,13 @@ namespace YooAsset.Editor
StyleLength defaultStyle = new StyleLength(1f);
element.style.paddingTop = defaultStyle;
element.style.paddingBottom = defaultStyle;
element.style.paddingLeft = defaultStyle;
element.style.paddingRight = defaultStyle;
element.style.marginTop = defaultStyle;
element.style.marginBottom = defaultStyle;
element.style.marginLeft = defaultStyle;
element.style.marginRight = defaultStyle;
element.style.paddingLeft = 1;
element.style.paddingRight = 1;
element.style.marginLeft = 0;
element.style.marginRight = 0;
}
}
}

View File

@ -0,0 +1,30 @@
#if UNITY_2019_4_OR_NEWER
using System.Reflection;
using UnityEditor;
namespace YooAsset.Editor
{
public static class UIElementsCursor
{
private static PropertyInfo _defaultCursorId;
private static PropertyInfo DefaultCursorId
{
get
{
if (_defaultCursorId != null)
return _defaultCursorId;
_defaultCursorId = typeof(UnityEngine.UIElements.Cursor).GetProperty("defaultCursorId", BindingFlags.NonPublic | BindingFlags.Instance);
return _defaultCursorId;
}
}
public static UnityEngine.UIElements.Cursor CreateCursor(MouseCursor cursorType)
{
var ret = (object)new UnityEngine.UIElements.Cursor();
DefaultCursorId.SetValue(ret, (int)cursorType);
return (UnityEngine.UIElements.Cursor)ret;
}
}
}
#endif

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 35d99a1727f773b46bc6a7bb1a2be970
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: