From 89a3543f1811d9d4ecab3bbb9a9529eb11b6424a Mon Sep 17 00:00:00 2001 From: Razmoth <32140579+Razmoth@users.noreply.github.com> Date: Sat, 25 Nov 2023 13:13:41 +0400 Subject: [PATCH] - [GUI] `AssetBrowser` improvments --- AssetStudio.GUI/AssetBrowser.Designer.cs | 40 ++++++---- AssetStudio.GUI/AssetBrowser.cs | 99 ++++++++++++++++++++---- AssetStudio.GUI/AssetBrowser.resx | 62 ++++++++++++++- 3 files changed, 169 insertions(+), 32 deletions(-) diff --git a/AssetStudio.GUI/AssetBrowser.Designer.cs b/AssetStudio.GUI/AssetBrowser.Designer.cs index 6407ab3..780e284 100644 --- a/AssetStudio.GUI/AssetBrowser.Designer.cs +++ b/AssetStudio.GUI/AssetBrowser.Designer.cs @@ -1,4 +1,5 @@ -using System.Windows.Forms; +using System.Configuration; +using System.Windows.Forms; namespace AssetStudio.GUI { @@ -30,37 +31,42 @@ namespace AssetStudio.GUI /// private void InitializeComponent() { - assetListView = new DataGridView(); + assetDataGridView = new DataGridView(); tableLayoutPanel1 = new TableLayoutPanel(); tableLayoutPanel2 = new TableLayoutPanel(); loadAssetMap = new Button(); clear = new Button(); loadSelected = new Button(); searchTextBox = new TextBox(); - ((System.ComponentModel.ISupportInitialize)assetListView).BeginInit(); + ((System.ComponentModel.ISupportInitialize)assetDataGridView).BeginInit(); tableLayoutPanel1.SuspendLayout(); tableLayoutPanel2.SuspendLayout(); + FormClosing += AssetBrowser_FormClosing; SuspendLayout(); // - // assetListView + // assetDataGridView // - assetListView.AllowUserToAddRows = false; - assetListView.AllowUserToDeleteRows = false; - assetListView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; - assetListView.Dock = DockStyle.Fill; - assetListView.Location = new System.Drawing.Point(3, 38); - assetListView.Name = "assetListView"; - assetListView.ReadOnly = true; - assetListView.RowTemplate.Height = 25; - assetListView.Size = new System.Drawing.Size(518, 250); - assetListView.TabIndex = 2; + assetDataGridView.AllowUserToAddRows = false; + assetDataGridView.AllowUserToDeleteRows = false; + assetDataGridView.AllowUserToResizeRows = false; + assetDataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + assetDataGridView.Dock = DockStyle.Fill; + assetDataGridView.Location = new System.Drawing.Point(3, 38); + assetDataGridView.Name = "assetDataGridView"; + assetDataGridView.ReadOnly = true; + assetDataGridView.RowTemplate.Height = 25; + assetDataGridView.Size = new System.Drawing.Size(518, 250); + assetDataGridView.TabIndex = 2; + assetDataGridView.VirtualMode = true; + assetDataGridView.CellValueNeeded += AssetDataGridView_CellValueNeeded; + assetDataGridView.ColumnHeaderMouseClick += AssetListView_ColumnHeaderMouseClick; // // tableLayoutPanel1 // tableLayoutPanel1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; tableLayoutPanel1.ColumnCount = 1; tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F)); - tableLayoutPanel1.Controls.Add(assetListView, 0, 1); + tableLayoutPanel1.Controls.Add(assetDataGridView, 0, 1); tableLayoutPanel1.Controls.Add(tableLayoutPanel2, 0, 0); tableLayoutPanel1.Location = new System.Drawing.Point(12, 12); tableLayoutPanel1.Name = "tableLayoutPanel1"; @@ -142,7 +148,7 @@ namespace AssetStudio.GUI ShowIcon = false; StartPosition = FormStartPosition.CenterScreen; Text = "Asset Browser"; - ((System.ComponentModel.ISupportInitialize)assetListView).EndInit(); + ((System.ComponentModel.ISupportInitialize)assetDataGridView).EndInit(); tableLayoutPanel1.ResumeLayout(false); tableLayoutPanel2.ResumeLayout(false); tableLayoutPanel2.PerformLayout(); @@ -152,7 +158,7 @@ namespace AssetStudio.GUI #endregion - private System.Windows.Forms.DataGridView assetListView; + private System.Windows.Forms.DataGridView assetDataGridView; private TableLayoutPanel tableLayoutPanel1; private TableLayoutPanel tableLayoutPanel2; private Button loadAssetMap; diff --git a/AssetStudio.GUI/AssetBrowser.cs b/AssetStudio.GUI/AssetBrowser.cs index 5b39f06..5678da6 100644 --- a/AssetStudio.GUI/AssetBrowser.cs +++ b/AssetStudio.GUI/AssetBrowser.cs @@ -1,22 +1,28 @@ using System; using System.Collections.Generic; +using System.ComponentModel; +using System.Data; using System.IO; using System.Linq; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Forms; -using AssetStudio; namespace AssetStudio.GUI { partial class AssetBrowser : Form { private readonly MainForm _parent; + + private SortOrder _sortOrder; + private DataGridViewColumn _sortedColumn; + private List _assetEntries; + private List _columnNames; + public AssetBrowser(MainForm form) { InitializeComponent(); _parent = form; - FormClosing += AssetBrowser_FormClosing; } private async void loadAssetMap_Click(object sender, EventArgs e) @@ -29,8 +35,18 @@ namespace AssetStudio.GUI var path = openFileDialog.FileName; Logger.Info($"Loading AssetMap..."); await Task.Run(() => ResourceMap.FromFile(path)); - assetListView.DataSource = ResourceMap.GetEntries(); - assetListView.Columns.GetLastColumn(DataGridViewElementStates.None, DataGridViewElementStates.None).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + + _sortedColumn = null; + _columnNames = typeof(AssetEntry).GetProperties().Select(x => x.Name).ToList(); + _assetEntries = ResourceMap.GetEntries(); + + assetDataGridView.Columns.Clear(); + assetDataGridView.Columns.AddRange(_columnNames.Select(x => new DataGridViewTextBoxColumn() { Name = x, HeaderText = x, SortMode = DataGridViewColumnSortMode.Programmatic }).ToArray()); + assetDataGridView.Columns.GetLastColumn(DataGridViewElementStates.None, DataGridViewElementStates.None).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + + assetDataGridView.Rows.Clear(); + assetDataGridView.RowCount = _assetEntries.Count; + assetDataGridView.Refresh(); } loadAssetMap.Enabled = true; } @@ -39,9 +55,9 @@ namespace AssetStudio.GUI Clear(); Logger.Info($"Cleared !!"); } - private async void loadSelected_Click(object sender, EventArgs e) + private void loadSelected_Click(object sender, EventArgs e) { - var files = assetListView.SelectedRows.Cast().Select(x => x.DataBoundItem as AssetEntry).Select(x => x.Source).ToHashSet(); + var files = assetDataGridView.SelectedRows.Cast().Select(x => x.DataBoundItem as AssetEntry).Select(x => x.Source).ToHashSet(); var missingFiles = files.Where(x => !File.Exists(x)); foreach (var file in missingFiles) { @@ -59,7 +75,6 @@ namespace AssetStudio.GUI if (e.KeyChar == (char)Keys.Enter) { var filters = new Dictionary(); - var names = typeof(AssetEntry).GetProperties().Select(x => x.Name).ToList(); var value = searchTextBox.Text; var options = value.Split(' '); @@ -73,7 +88,7 @@ namespace AssetStudio.GUI continue; } var (name, regex) = (arguments[0], arguments[1]); - if (!names.Contains(name, StringComparer.OrdinalIgnoreCase)) + if (!_columnNames.Contains(name, StringComparer.OrdinalIgnoreCase)) { Logger.Error($"Unknonw argument {name}"); continue; @@ -81,16 +96,72 @@ namespace AssetStudio.GUI filters[name] = new Regex(regex, RegexOptions.IgnoreCase); } - var assets = ResourceMap.GetEntries(); - if (assets.Count != 0) + _assetEntries = ResourceMap.GetEntries().FindAll(x => x.Matches(filters)); + + assetDataGridView.Rows.Clear(); + assetDataGridView.RowCount = _assetEntries.Count; + assetDataGridView.Refresh(); + } + } + private void AssetDataGridView_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e) + { + if (e.RowIndex <= _assetEntries.Count) + { + var assetEntry = _assetEntries[e.RowIndex]; + e.Value = e.ColumnIndex switch { - var regex = new Regex(value, RegexOptions.IgnoreCase); - assetListView.DataSource = assets.FindAll(x => x.Matches(filters)); + 0 => assetEntry.Name, + 1 => assetEntry.Container, + 2 => assetEntry.PathID, + 3 => assetEntry.Source, + 4 => assetEntry.Type, + _ => "" + }; + } + } + private void AssetListView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) + { + if (e.ColumnIndex <= assetDataGridView.Columns.Count) + { + ListSortDirection direction; + var column = assetDataGridView.Columns[e.ColumnIndex]; + + if (_sortedColumn != null) + { + if (_sortedColumn != column) + { + direction = ListSortDirection.Ascending; + _sortedColumn.HeaderCell.SortGlyphDirection = SortOrder.None; + _sortedColumn = column; + } + else + { + direction = _sortOrder == SortOrder.Ascending ? ListSortDirection.Descending : ListSortDirection.Ascending; + } } else { - assetListView.DataSource = assets; + direction = ListSortDirection.Ascending; + _sortedColumn = column; } + + _sortedColumn.HeaderCell.SortGlyphDirection = _sortOrder = direction == ListSortDirection.Ascending ? SortOrder.Ascending : SortOrder.Descending; + + Func keySelector = e.ColumnIndex switch + { + 0 => x => x.Name, + 1 => x => x.Container, + 2 => x => x.PathID, + 3 => x => x.Source, + 4 => x => x.Type, + _ => x => "" + }; + + _assetEntries = direction == ListSortDirection.Ascending ? _assetEntries.OrderBy(keySelector).ToList() : _assetEntries.OrderByDescending(keySelector).ToList(); + + assetDataGridView.Rows.Clear(); + assetDataGridView.RowCount = _assetEntries.Count; + assetDataGridView.Refresh(); } } private void AssetBrowser_FormClosing(object sender, FormClosingEventArgs e) @@ -101,7 +172,7 @@ namespace AssetStudio.GUI public void Clear() { ResourceMap.Clear(); - assetListView.DataSource = Array.Empty(); + assetDataGridView.Rows.Clear(); } } } diff --git a/AssetStudio.GUI/AssetBrowser.resx b/AssetStudio.GUI/AssetBrowser.resx index f298a7b..af32865 100644 --- a/AssetStudio.GUI/AssetBrowser.resx +++ b/AssetStudio.GUI/AssetBrowser.resx @@ -1,4 +1,64 @@ - + + +