整合SLSUtilities
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
using LunaWolfStudiosEditor.ScriptableSheets.Shared;
|
||||
using System.Linq;
|
||||
using UnityEditor;
|
||||
using UnityEditor.IMGUI.Controls;
|
||||
using UnityEngine;
|
||||
|
||||
namespace LunaWolfStudiosEditor.ScriptableSheets.Layout
|
||||
{
|
||||
public static class ColumnUtility
|
||||
{
|
||||
// Note: Tooltips need to be unique or the table will assume it's the same property.
|
||||
public const string ActionsTooltip = "Object Actions";
|
||||
public const string AssetPathTooltip = "Object Asset Path";
|
||||
public const string GuidColumnTooltip = "Globally Unique Identifier";
|
||||
|
||||
public static MultiColumnHeaderState.Column CreateActionsColumn(string columnName, int width)
|
||||
{
|
||||
return new MultiColumnHeaderState.Column()
|
||||
{
|
||||
allowToggleVisibility = false,
|
||||
autoResize = true,
|
||||
canSort = false,
|
||||
headerContent = new GUIContent(columnName, ActionsTooltip),
|
||||
headerTextAlignment = TextAlignment.Left,
|
||||
minWidth = width,
|
||||
maxWidth = width,
|
||||
sortingArrowAlignment = TextAlignment.Right,
|
||||
};
|
||||
}
|
||||
|
||||
public static MultiColumnHeaderState.Column CreateAssetPathColumn(string columnName)
|
||||
{
|
||||
return new MultiColumnHeaderState.Column()
|
||||
{
|
||||
allowToggleVisibility = true,
|
||||
autoResize = true,
|
||||
canSort = true,
|
||||
headerContent = new GUIContent(columnName, AssetPathTooltip),
|
||||
headerTextAlignment = TextAlignment.Left,
|
||||
minWidth = SheetLayout.PropertyWidth,
|
||||
maxWidth = int.MaxValue,
|
||||
sortedAscending = true,
|
||||
sortingArrowAlignment = TextAlignment.Right,
|
||||
};
|
||||
}
|
||||
|
||||
public static MultiColumnHeaderState.Column CreateGuidColumn(string columnName)
|
||||
{
|
||||
return new MultiColumnHeaderState.Column()
|
||||
{
|
||||
allowToggleVisibility = true,
|
||||
autoResize = true,
|
||||
canSort = true,
|
||||
headerContent = new GUIContent(columnName, GuidColumnTooltip),
|
||||
headerTextAlignment = TextAlignment.Left,
|
||||
minWidth = SheetLayout.GuidPropertyWidth,
|
||||
maxWidth = int.MaxValue,
|
||||
sortedAscending = true,
|
||||
sortingArrowAlignment = TextAlignment.Right,
|
||||
};
|
||||
}
|
||||
|
||||
public static PropertyColumn CreatePropertyColumn(SerializedProperty serializedProperty, bool isScriptableObject, HeaderFormat headerFormat, string labelPrefix = "")
|
||||
{
|
||||
string formattedName;
|
||||
switch (headerFormat)
|
||||
{
|
||||
case HeaderFormat.Default:
|
||||
formattedName = serializedProperty.displayName;
|
||||
break;
|
||||
|
||||
case HeaderFormat.Friendly:
|
||||
formattedName = serializedProperty.FriendlyPropertyPath();
|
||||
break;
|
||||
|
||||
case HeaderFormat.Advanced:
|
||||
formattedName = serializedProperty.propertyPath;
|
||||
break;
|
||||
|
||||
default:
|
||||
Debug.LogWarning($"Unsupported {nameof(HeaderFormat)} {headerFormat}. Using {nameof(HeaderFormat.Default)} format.");
|
||||
formattedName = serializedProperty.displayName;
|
||||
break;
|
||||
}
|
||||
var columnName = $"{labelPrefix}{formattedName}";
|
||||
var propertyType = serializedProperty.GetSheetsPropertyType(isScriptableObject);
|
||||
var minWidth = SheetLayout.PropertyWidth;
|
||||
var propertyPath = serializedProperty.propertyPath;
|
||||
switch (propertyType)
|
||||
{
|
||||
case SerializedPropertyType.Integer:
|
||||
case SerializedPropertyType.Boolean:
|
||||
case SerializedPropertyType.Float:
|
||||
case SerializedPropertyType.Color:
|
||||
case SerializedPropertyType.ArraySize:
|
||||
case SerializedPropertyType.Character:
|
||||
minWidth = SheetLayout.PropertyWidthSmall;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return new PropertyColumn()
|
||||
{
|
||||
allowToggleVisibility = true,
|
||||
autoResize = true,
|
||||
canSort = true,
|
||||
headerContent = new GUIContent(columnName, propertyPath),
|
||||
headerTextAlignment = TextAlignment.Left,
|
||||
minWidth = minWidth,
|
||||
maxWidth = int.MaxValue,
|
||||
sortedAscending = true,
|
||||
sortingArrowAlignment = TextAlignment.Right,
|
||||
property = serializedProperty.Copy(),
|
||||
};
|
||||
}
|
||||
|
||||
public static string GetColumnIndexLabel(bool showIndex, int index)
|
||||
{
|
||||
return showIndex ? $"{index} " : string.Empty;
|
||||
}
|
||||
|
||||
public static int[] GetClampedColumns(this MultiColumnHeaderState.Column[] columns, int maxColumns)
|
||||
{
|
||||
return Enumerable.Range(0, Mathf.Min(columns.Length, maxColumns)).ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dd4296a446a420745b7d516dc765e918
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 284559
|
||||
packageName: Scriptable Sheets
|
||||
packageVersion: 1.8.0
|
||||
assetPath: Packages/com.lunawolfstudios.scriptablesheets/Editor/Modules/Layout/Utilities/ColumnUtility.cs
|
||||
uploadId: 823456
|
||||
@@ -0,0 +1,86 @@
|
||||
using LunaWolfStudiosEditor.ScriptableSheets.Comparables;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEditor;
|
||||
using UnityEditor.IMGUI.Controls;
|
||||
using UnityEngine;
|
||||
|
||||
namespace LunaWolfStudiosEditor.ScriptableSheets.Layout
|
||||
{
|
||||
public static class MultiColumnHeaderUtility
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns a list of sorted objects based on the sorting properties of the multi column header.
|
||||
/// </summary>
|
||||
public static List<TObject> GetSorted<TObject>(this MultiColumnHeader multiColumnHeader, List<TObject> unsorted) where TObject : Object
|
||||
{
|
||||
var sortedColumnIndex = multiColumnHeader.sortedColumnIndex;
|
||||
if (sortedColumnIndex >= 0 && unsorted.Count > 1)
|
||||
{
|
||||
var column = multiColumnHeader.GetColumn(sortedColumnIndex);
|
||||
var isAscending = column.sortedAscending;
|
||||
if (column is PropertyColumn propertyColumn)
|
||||
{
|
||||
var propertyPath = propertyColumn.property.propertyPath;
|
||||
if (!string.IsNullOrEmpty(propertyPath))
|
||||
{
|
||||
// Use Alphanumeric sorting algorithm for strings.
|
||||
if (propertyColumn.property.propertyType == SerializedPropertyType.String)
|
||||
{
|
||||
var alphanumComparer = new AlphanumComparer();
|
||||
var ordered = isAscending ? unsorted.OrderBy(u => ComparableUtility.GetPropertyComparable(u, propertyPath) as string, alphanumComparer) : unsorted.OrderByDescending(u => ComparableUtility.GetPropertyComparable(u, propertyPath) as string, alphanumComparer);
|
||||
return ordered.ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
var ordered = isAscending ? unsorted.OrderBy(u => ComparableUtility.GetPropertyComparable(u, propertyPath)) : unsorted.OrderByDescending(u => ComparableUtility.GetPropertyComparable(u, propertyPath));
|
||||
return ordered.ToList();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning($"Cannot sort column {sortedColumnIndex} property path is null or empty.");
|
||||
}
|
||||
}
|
||||
else if (column.headerContent.tooltip == ColumnUtility.AssetPathTooltip)
|
||||
{
|
||||
var ordered = isAscending ? unsorted.OrderBy(u => ComparableUtility.GetAssetPathComparable(u)) : unsorted.OrderByDescending(u => ComparableUtility.GetAssetPathComparable(u));
|
||||
return ordered.ToList();
|
||||
}
|
||||
else if (column.headerContent.tooltip == ColumnUtility.GuidColumnTooltip)
|
||||
{
|
||||
var ordered = isAscending ? unsorted.OrderBy(u => ComparableUtility.GetGUIDComparable(u)) : unsorted.OrderByDescending(u => ComparableUtility.GetGUIDComparable(u));
|
||||
return ordered.ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning($"Cannot sort column {sortedColumnIndex} it is not a valid {nameof(PropertyColumn)}.");
|
||||
}
|
||||
}
|
||||
return unsorted.ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resizes the MultiColumnHeader to the min width of all its columns.
|
||||
/// </summary>
|
||||
public static void ResizeToMinWidth(this MultiColumnHeader multiColumnHeader)
|
||||
{
|
||||
foreach (var column in multiColumnHeader.state.columns)
|
||||
{
|
||||
column.width = column.minWidth;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resizes the MultiColumnHeader to the fit the header content.
|
||||
/// </summary>
|
||||
public static void ResizeToHeaderWidth(this MultiColumnHeader multiColumnHeader, int padding = 0)
|
||||
{
|
||||
foreach (var column in multiColumnHeader.state.columns)
|
||||
{
|
||||
var headerWidth = EditorStyles.label.CalcSize(column.headerContent).x + padding;
|
||||
column.width = Mathf.Clamp(headerWidth, column.minWidth, column.maxWidth);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 34d037c93f354814a87609647ad3215c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 284559
|
||||
packageName: Scriptable Sheets
|
||||
packageVersion: 1.8.0
|
||||
assetPath: Packages/com.lunawolfstudios.scriptablesheets/Editor/Modules/Layout/Utilities/MultiColumnHeaderUtility.cs
|
||||
uploadId: 823456
|
||||
Reference in New Issue
Block a user