Skip to content

Commit

Permalink
Sort the creative inventory:
Browse files Browse the repository at this point in the history
Item groups like glass and crystals are now at the bottom.
Items at the top are now sorted.
Fixes #341
  • Loading branch information
IntegratedQuantum committed May 2, 2024
1 parent 3419034 commit a575eda
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/gui/windows/creative_inventory.zig
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,26 @@ pub fn trySwappingItems(index: usize, destination: *ItemStack) void {
destination.amount = destination.item.?.stackSize();
}

fn lessThan(_: void, lhs: Item, rhs: Item) bool {
if(lhs == .baseItem and rhs == .baseItem) {
const lhsFolders = std.mem.count(u8, lhs.baseItem.id, "/");
const rhsFolders = std.mem.count(u8, rhs.baseItem.id, "/");
if(lhsFolders < rhsFolders) return true;
if(lhsFolders > rhsFolders) return false;
return std.ascii.lessThanIgnoreCase(lhs.baseItem.id, rhs.baseItem.id);
} else {
if(lhs == .baseItem) return true;
return false;
}
}

pub fn onOpen() void {
items = main.List(Item).init(main.globalAllocator);
var itemIterator = main.items.iterator();
while(itemIterator.next()) |item| {
items.append(Item{.baseItem = item.*});
}
std.mem.sort(Item, items.items, {}, lessThan);

const list = VerticalList.init(.{padding, padding + 16}, 140, 0);
var i: u32 = 0;
Expand Down

0 comments on commit a575eda

Please sign in to comment.