Skip to content

Commit

Permalink
Merge pull request #41 from CyberAgentGameEntertainment/feature/sorting
Browse files Browse the repository at this point in the history
Sorting for LayoutViewer
  • Loading branch information
Haruma-K authored Jun 30, 2023
2 parents addd3e3 + 30fd2fe commit 612de09
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 20 deletions.
8 changes: 5 additions & 3 deletions Assets/SmartAddresser/Editor/Core/Models/Layouts/Entry.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using SmartAddresser.Editor.Core.Models.Shared;
using UnityEngine;

Expand All @@ -20,13 +21,14 @@ public sealed class Entry
[NonSerialized] private bool _isErrorTypeDirty = true;
[NonSerialized] private bool _isMessagesDirty = true;

public Entry(string address, string assetPath, string[] labels, string[] versions)
public Entry(string address, string assetPath, IReadOnlyList<string> labels, IReadOnlyList<string> versions)
{
_id = IdentifierFactory.Create();
_address = address;
_assetPath = assetPath;
_labels = labels ?? Array.Empty<string>();
_versions = versions ?? Array.Empty<string>();
// Sort labels and versions for sorting of LayoutViewer.
_labels = labels == null ? Array.Empty<string>() : labels.OrderBy(x => x).ToArray();
_versions = versions == null ? Array.Empty<string>() : versions.OrderBy(x => x).ToArray();
}

public string Id => _id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private static Task<Group> BuildGroupAsync(AddressRule addressRule, LayoutRule l
versions.Add(version);
}

var entry = new Entry(address, assetPath, labels, versions.ToArray());
var entry = new Entry(address, assetPath, labels, versions);
group.Entries.Add(entry);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@ public enum Columns
AssetGroups,
AddressRule
}

private GUIStyle _cellLabelStyle;

private GUIStyle CellLabelStyle
{
get
{
if (_cellLabelStyle == null)
{
_cellLabelStyle = new GUIStyle(EditorStyles.label)
{
wordWrap = false
};
}

return _cellLabelStyle;
}
}

[NonSerialized] private int _currentId;

Expand Down Expand Up @@ -52,7 +70,7 @@ protected override void CellGUI(int columnIndex, Rect cellRect, RowGUIArgs args)
{
case Columns.Groups:
GUI.enabled = addressableGroup != null && !addressableGroup.ReadOnly && item.Rule.Control.Value;
GUI.Label(cellRect, GetText(item, columnIndex));
GUI.Label(cellRect, GetText(item, columnIndex), CellLabelStyle);
break;
case Columns.Control:
GUI.enabled = addressableGroup != null && !addressableGroup.ReadOnly;
Expand All @@ -62,11 +80,11 @@ protected override void CellGUI(int columnIndex, Rect cellRect, RowGUIArgs args)
break;
case Columns.AssetGroups:
GUI.enabled = addressableGroup != null && !addressableGroup.ReadOnly && item.Rule.Control.Value;
GUI.Label(cellRect, GetText(item, columnIndex));
GUI.Label(cellRect, GetText(item, columnIndex), CellLabelStyle);
break;
case Columns.AddressRule:
GUI.enabled = addressableGroup != null && !addressableGroup.ReadOnly && item.Rule.Control.Value;
GUI.Label(cellRect, GetText(item, columnIndex));
GUI.Label(cellRect, GetText(item, columnIndex), CellLabelStyle);
break;
default:
throw new NotImplementedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ public enum Columns
AssetGroups,
LabelRule
}

private GUIStyle _cellLabelStyle;

private GUIStyle CellLabelStyle
{
get
{
if (_cellLabelStyle == null)
{
_cellLabelStyle = new GUIStyle(EditorStyles.label)
{
wordWrap = false
};
}

return _cellLabelStyle;
}
}

[NonSerialized] private int _currentId;

Expand Down Expand Up @@ -62,10 +80,10 @@ protected override void CellGUI(int columnIndex, Rect cellRect, RowGUIArgs args)
base.CellGUI(columnIndex, cellRect, args);
break;
case Columns.AssetGroups:
GUI.Label(cellRect, GetText(item, columnIndex));
GUI.Label(cellRect, GetText(item, columnIndex), CellLabelStyle);
break;
case Columns.LabelRule:
GUI.Label(cellRect, GetText(item, columnIndex));
GUI.Label(cellRect, GetText(item, columnIndex), CellLabelStyle);
break;
default:
throw new NotImplementedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ public enum Columns
AssetGroups,
VersionRule
}

private GUIStyle _cellLabelStyle;

private GUIStyle CellLabelStyle
{
get
{
if (_cellLabelStyle == null)
{
_cellLabelStyle = new GUIStyle(EditorStyles.label)
{
wordWrap = false
};
}

return _cellLabelStyle;
}
}

[NonSerialized] private int _currentId;

Expand Down Expand Up @@ -63,10 +81,10 @@ protected override void CellGUI(int columnIndex, Rect cellRect, RowGUIArgs args)
base.CellGUI(columnIndex, cellRect, args);
break;
case Columns.AssetGroups:
GUI.Label(cellRect, GetText(item, columnIndex));
GUI.Label(cellRect, GetText(item, columnIndex), CellLabelStyle);
break;
case Columns.VersionRule:
GUI.Label(cellRect, GetText(item, columnIndex));
GUI.Label(cellRect, GetText(item, columnIndex), CellLabelStyle);
break;
default:
throw new NotImplementedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,30 @@ public enum Columns
Labels,
Versions
}


private static readonly int[] DefaultSkipSortingDepths = { 0 };

private readonly Texture2D _badgeBackgroundTexture;
private readonly Texture2D _failedTexture;
private readonly Texture2D _successTexture;
private readonly Texture2D _warningTexture;
private GUIStyle _cellLabelStyle;

private GUIStyle CellLabelStyle
{
get
{
if (_cellLabelStyle == null)
{
_cellLabelStyle = new GUIStyle(EditorStyles.label)
{
wordWrap = false
};
}

return _cellLabelStyle;
}
}

[NonSerialized] private int _currentId;

Expand Down Expand Up @@ -98,10 +117,10 @@ protected override void CellGUI(int columnIndex, Rect cellRect, RowGUIArgs args)
statusIconRect.width = statusIconRect.height;
labelRect.xMin += statusIconRect.width + 2.0f;
GUI.DrawTexture(statusIconRect, errorTypeIcon);
GUI.Label(labelRect, GetTextForDisplay(item, columnIndex));
GUI.Label(labelRect, GetTextForDisplay(item, columnIndex), CellLabelStyle);
break;
case Columns.AssetPath:
GUI.Label(cellRect, GetTextForDisplay(item, columnIndex));
GUI.Label(cellRect, GetTextForDisplay(item, columnIndex), CellLabelStyle);
break;
case Columns.Labels:
DrawBadges(entryItem.Entry.Labels, new Vector2(cellRect.x, cellRect.y + 1),
Expand Down Expand Up @@ -142,7 +161,7 @@ private void DrawBadges(IEnumerable<string> texts, Vector2 startPos, float heigh
GUI.DrawTexture(textureRect, _badgeBackgroundTexture, ScaleMode.StretchToFill, true, 0, Color.white,
Vector4.zero, cornerRadius);
var labelRect = new Rect(pos.x + 6, pos.y, labelWidth, height);
GUI.Label(labelRect, text);
GUI.Label(labelRect, text, CellLabelStyle);

pos.x += textureRect.width;
pos.x += 3; // space between badges
Expand Down Expand Up @@ -183,6 +202,12 @@ protected override string GetTextForSearch(TreeViewItem item, int columnIndex)

}

protected override IEnumerable<int> GetSkipSortingDepths()
{
// Exclude GroupItem from sorting targets.
return DefaultSkipSortingDepths;
}

protected override bool CanMultiSelect(TreeViewItem item)
{
return false;
Expand Down Expand Up @@ -287,7 +312,7 @@ public MultiColumnHeaderState.Column[] GetColumnStates()
sortedAscending = oldGroupNameAddressColumn?.sortedAscending ?? true,
headerContent = new GUIContent("Group Name / Address"),
headerTextAlignment = TextAlignment.Center,
canSort = false,
canSort = true,
minWidth = 50,
autoResize = false,
allowToggleVisibility = false
Expand All @@ -298,7 +323,7 @@ public MultiColumnHeaderState.Column[] GetColumnStates()
sortedAscending = oldAssetPathColumn?.sortedAscending ?? true,
headerContent = new GUIContent("Asset Path"),
headerTextAlignment = TextAlignment.Center,
canSort = false,
canSort = true,
minWidth = 50,
autoResize = false,
allowToggleVisibility = false
Expand All @@ -309,7 +334,7 @@ public MultiColumnHeaderState.Column[] GetColumnStates()
sortedAscending = oldLabelsColumn?.sortedAscending ?? true,
headerContent = new GUIContent("Labels"),
headerTextAlignment = TextAlignment.Center,
canSort = false,
canSort = true,
minWidth = 20,
autoResize = false,
allowToggleVisibility = false
Expand All @@ -320,7 +345,7 @@ public MultiColumnHeaderState.Column[] GetColumnStates()
sortedAscending = oldVersionsColumn?.sortedAscending ?? true,
headerContent = new GUIContent("Versions"),
headerTextAlignment = TextAlignment.Center,
canSort = false,
canSort = true,
minWidth = 20,
autoResize = false,
allowToggleVisibility = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,11 @@ private void SortHierarchical(IList<TreeViewItem> children, int keyColumnIndex,
{
if (children == null) return;

var orderedChildren = OrderItems(children, keyColumnIndex, ascending).ToList();
var depth = children[0].depth;
var isSkipSortingTarget = GetSkipSortingDepths().Contains(depth);
var orderedChildren = isSkipSortingTarget
? new List<TreeViewItem>(children)
: OrderItems(children, keyColumnIndex, ascending).ToList();

children.Clear();
foreach (var orderedChild in orderedChildren) children.Add(orderedChild);
Expand All @@ -319,5 +323,10 @@ private void SortHierarchical(IList<TreeViewItem> children, int keyColumnIndex,
if (child != null)
SortHierarchical(child.children, keyColumnIndex, ascending);
}

protected virtual IEnumerable<int> GetSkipSortingDepths()
{
return Array.Empty<int>();
}
}
}

0 comments on commit 612de09

Please sign in to comment.