#if UNITY_2019_4_OR_NEWER
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEditor.UIElements;
using UnityEngine.UIElements;
namespace YooAsset.Editor
{
internal class ReporterBundleListViewer
{
private enum ESortMode
{
BundleName,
BundleSize,
BundleTags
}
private VisualTreeAsset _visualAsset;
private TemplateContainer _root;
private ToolbarButton _topBar1;
private ToolbarButton _topBar2;
private ToolbarButton _topBar3;
private ToolbarButton _topBar5;
private ToolbarButton _bottomBar1;
private ListView _bundleListView;
private ListView _includeListView;
private BuildReport _buildReport;
private string _reportFilePath;
private string _searchKeyWord;
private ESortMode _sortMode = ESortMode.BundleName;
private bool _descendingSort = false;
///
/// 初始化页面
///
public void InitViewer()
{
// 加载布局文件
_visualAsset = UxmlLoader.LoadWindowUXML();
if (_visualAsset == null)
return;
_root = _visualAsset.CloneTree();
_root.style.flexGrow = 1f;
// 顶部按钮栏
_topBar1 = _root.Q("TopBar1");
_topBar2 = _root.Q("TopBar2");
_topBar3 = _root.Q("TopBar3");
_topBar5 = _root.Q("TopBar5");
_topBar1.clicked += TopBar1_clicked;
_topBar2.clicked += TopBar2_clicked;
_topBar3.clicked += TopBar3_clicked;
_topBar5.clicked += TopBar4_clicked;
// 底部按钮栏
_bottomBar1 = _root.Q("BottomBar1");
// 资源包列表
_bundleListView = _root.Q("TopListView");
_bundleListView.makeItem = MakeBundleListViewItem;
_bundleListView.bindItem = BindBundleListViewItem;
#if UNITY_2022_3_OR_NEWER
_bundleListView.selectionChanged += BundleListView_onSelectionChange;
#elif UNITY_2020_1_OR_NEWER
_bundleListView.onSelectionChange += BundleListView_onSelectionChange;
#else
_bundleListView.onSelectionChanged += BundleListView_onSelectionChange;
#endif
// 包含列表
_includeListView = _root.Q("BottomListView");
_includeListView.makeItem = MakeIncludeListViewItem;
_includeListView.bindItem = BindIncludeListViewItem;
#if UNITY_2020_3_OR_NEWER
SplitView.Adjuster(_root);
#endif
}
///
/// 填充页面数据
///
public void FillViewData(BuildReport buildReport, string reprotFilePath, string searchKeyWord)
{
_buildReport = buildReport;
_reportFilePath = reprotFilePath;
_searchKeyWord = searchKeyWord;
RefreshView();
}
private void RefreshView()
{
_bundleListView.Clear();
_bundleListView.ClearSelection();
_bundleListView.itemsSource = FilterAndSortViewItems();
_bundleListView.Rebuild();
RefreshSortingSymbol();
}
private List FilterAndSortViewItems()
{
List result = new List(_buildReport.BundleInfos.Count);
// 过滤列表
foreach (var bundleInfo in _buildReport.BundleInfos)
{
if (string.IsNullOrEmpty(_searchKeyWord) == false)
{
if (bundleInfo.BundleName.Contains(_searchKeyWord) == false)
continue;
}
result.Add(bundleInfo);
}
// 排序列表
if (_sortMode == ESortMode.BundleName)
{
if (_descendingSort)
return result.OrderByDescending(a => a.BundleName).ToList();
else
return result.OrderBy(a => a.BundleName).ToList();
}
else if (_sortMode == ESortMode.BundleSize)
{
if (_descendingSort)
return result.OrderByDescending(a => a.FileSize).ToList();
else
return result.OrderBy(a => a.FileSize).ToList();
}
else if (_sortMode == ESortMode.BundleTags)
{
if (_descendingSort)
return result.OrderByDescending(a => a.GetTagsString()).ToList();
else
return result.OrderBy(a => a.GetTagsString()).ToList();
}
else
{
throw new System.NotImplementedException();
}
}
private void RefreshSortingSymbol()
{
// 刷新符号
_topBar1.text = $"Bundle Name ({_bundleListView.itemsSource.Count})";
_topBar2.text = "Size";
_topBar3.text = "Hash";
_topBar5.text = "Tags";
if (_sortMode == ESortMode.BundleName)
{
if (_descendingSort)
_topBar1.text = $"Bundle Name ({_bundleListView.itemsSource.Count}) ↓";
else
_topBar1.text = $"Bundle Name ({_bundleListView.itemsSource.Count}) ↑";
}
else if (_sortMode == ESortMode.BundleSize)
{
if (_descendingSort)
_topBar2.text = "Size ↓";
else
_topBar2.text = "Size ↑";
}
else if (_sortMode == ESortMode.BundleTags)
{
if (_descendingSort)
_topBar5.text = "Tags ↓";
else
_topBar5.text = "Tags ↑";
}
else
{
throw new System.NotImplementedException();
}
}
///
/// 挂接到父类页面上
///
public void AttachParent(VisualElement parent)
{
parent.Add(_root);
}
///
/// 从父类页面脱离开
///
public void DetachParent()
{
_root.RemoveFromHierarchy();
}
// 顶部列表相关
private VisualElement MakeBundleListViewItem()
{
VisualElement element = new VisualElement();
element.style.flexDirection = FlexDirection.Row;
{
var label = new Label();
label.name = "Label1";
label.style.unityTextAlign = TextAnchor.MiddleLeft;
label.style.marginLeft = 3f;
label.style.flexGrow = 1f;
label.style.width = 280;
element.Add(label);
}
{
var label = new Label();
label.name = "Label2";
label.style.unityTextAlign = TextAnchor.MiddleLeft;
label.style.marginLeft = 3f;
//label.style.flexGrow = 1f;
label.style.width = 100;
element.Add(label);
}
{
var label = new Label();
label.name = "Label3";
label.style.unityTextAlign = TextAnchor.MiddleLeft;
label.style.marginLeft = 3f;
//label.style.flexGrow = 1f;
label.style.width = 280;
element.Add(label);
}
{
var label = new Label();
label.name = "Label5";
label.style.unityTextAlign = TextAnchor.MiddleLeft;
label.style.marginLeft = 3f;
//label.style.flexGrow = 1f;
label.style.width = 150;
element.Add(label);
}
{
var label = new Label();
label.name = "Label6";
label.style.unityTextAlign = TextAnchor.MiddleLeft;
label.style.marginLeft = 3f;
label.style.flexGrow = 1f;
label.style.width = 80;
element.Add(label);
}
return element;
}
private void BindBundleListViewItem(VisualElement element, int index)
{
var sourceData = _bundleListView.itemsSource as List;
var bundleInfo = sourceData[index];
// Bundle Name
var label1 = element.Q