Skip to content

Commit

Permalink
[Loot Editor] Loot can be sorted by pressing the column
Browse files Browse the repository at this point in the history
  • Loading branch information
BAndysc committed Jan 2, 2024
1 parent 8c8e7ee commit 9ad4ca4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
43 changes: 41 additions & 2 deletions Modules/WDE.LootEditor/Editor/ViewModels/LootEditorViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LootItemViewModel> 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<float>)
{
if (ascending)
ordered = source.OrderBy(x => ((LootItemParameterCell<float>)x.CellsList[sortByCellIndex]).Value);
else
ordered = source.OrderByDescending(x => ((LootItemParameterCell<float>)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();
Expand Down
14 changes: 13 additions & 1 deletion Modules/WDE.LootEditor/Editor/Views/LootEditorView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down

0 comments on commit 9ad4ca4

Please sign in to comment.