From 9ad4ca486b229aa59da9a4fac0e3e44a02e8cbe2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Korczy=C5=84ski?= Date: Tue, 2 Jan 2024 17:02:32 +0000 Subject: [PATCH] [Loot Editor] Loot can be sorted by pressing the column --- .../Editor/ViewModels/LootEditorViewModel.cs | 43 ++++++++++++++++++- .../Editor/Views/LootEditorView.axaml.cs | 14 +++++- .../Models/MySqlCreatureTemplateWrath.cs | 2 +- 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/Modules/WDE.LootEditor/Editor/ViewModels/LootEditorViewModel.cs b/Modules/WDE.LootEditor/Editor/ViewModels/LootEditorViewModel.cs index 17b4acad6..20d838523 100644 --- a/Modules/WDE.LootEditor/Editor/ViewModels/LootEditorViewModel.cs +++ b/Modules/WDE.LootEditor/Editor/ViewModels/LootEditorViewModel.cs @@ -1121,12 +1121,51 @@ private bool VerifyDuplicateKeys() public ISolutionItem SolutionItem => ((ISolutionItem?)perDbSolutionItem ?? perEntitySolutionItem)!; - public void SortElements() + public void SortElements(int sortByCellIndex, bool ascending) { using var bulk = HistoryHandler.WithinBulk("Sort loot"); foreach (var group in Loots) { - var copy = group.LootItems.OrderBy(x => x.GroupId.Value) + var source = group.LootItems; + if (source.Count == 0) + continue; + + var firstRow = source[0]; + IOrderedEnumerable ordered; + if (firstRow.CellsList[sortByCellIndex] is LootItemParameterCellLong) + { + if (ascending) + ordered = source.OrderBy(x => ((LootItemParameterCellLong)x.CellsList[sortByCellIndex]).Value); + else + ordered = source.OrderByDescending(x => ((LootItemParameterCellLong)x.CellsList[sortByCellIndex]).Value); + } + else if (firstRow.CellsList[sortByCellIndex] is ItemNameStringCell) + { + if (ascending) + ordered = source.OrderBy(x => ((ItemNameStringCell)x.CellsList[sortByCellIndex]).StringValue); + else + ordered = source.OrderByDescending(x => ((ItemNameStringCell)x.CellsList[sortByCellIndex]).StringValue); + } + else if (firstRow.CellsList[sortByCellIndex] is LootItemParameterCell) + { + if (ascending) + ordered = source.OrderBy(x => ((LootItemParameterCell)x.CellsList[sortByCellIndex]).Value); + else + ordered = source.OrderByDescending(x => ((LootItemParameterCell)x.CellsList[sortByCellIndex]).Value); + } + else if (firstRow.CellsList[sortByCellIndex] is ActionCell) + { + continue; + } + else + { + messageBoxService.SimpleDialog("Error", "Non critical error - can't sort by this column", + "This is an internal error, please report it. Can't sort the loot, because " + + LootColumns[sortByCellIndex].Header + " is not sortable"); + continue; + } + + var copy = ordered .ThenBy(x => x.ItemOrCurrencyId.Value) .ToList(); group.LootItems.RemoveAll(); diff --git a/Modules/WDE.LootEditor/Editor/Views/LootEditorView.axaml.cs b/Modules/WDE.LootEditor/Editor/Views/LootEditorView.axaml.cs index f1e21392f..609987bb9 100644 --- a/Modules/WDE.LootEditor/Editor/Views/LootEditorView.axaml.cs +++ b/Modules/WDE.LootEditor/Editor/Views/LootEditorView.axaml.cs @@ -22,11 +22,23 @@ private void VeryFastTableView_OnValueUpdateRequest(string text) (DataContext as LootEditorViewModel)!.UpdateSelectedCells(text); } + private int lastSortByColumnIndex = -1; + private bool lastAscending = true; + private void VeryFastTableView_OnColumnPressed(object? sender, ColumnPressedEventArgs e) { if (DataContext is LootEditorViewModel vm) { - vm.SortElements(); + if (lastSortByColumnIndex != e.ColumnIndex) + { + lastSortByColumnIndex = e.ColumnIndex; + lastAscending = true; + } + else + { + lastAscending = !lastAscending; + } + vm.SortElements(e.ColumnIndex, lastAscending); } } } \ No newline at end of file diff --git a/WoWDatabaseEditor.Common/WDE.TrinityMySqlDatabase/Models/MySqlCreatureTemplateWrath.cs b/WoWDatabaseEditor.Common/WDE.TrinityMySqlDatabase/Models/MySqlCreatureTemplateWrath.cs index 4d36f2676..639e50fdf 100644 --- a/WoWDatabaseEditor.Common/WDE.TrinityMySqlDatabase/Models/MySqlCreatureTemplateWrath.cs +++ b/WoWDatabaseEditor.Common/WDE.TrinityMySqlDatabase/Models/MySqlCreatureTemplateWrath.cs @@ -225,7 +225,7 @@ public class MySqlCreatureTemplateMaster : ICreatureTemplate [Column(Name = "RequiredExpansion")] public short RequiredExpansion { get; set; } - [Column(Name = "rank")] + [Column(Name = "Classification")] public byte Rank { get; set; } [Column(Name = "unit_class")]