Sort button attempt 2 - #3194
Crepestrom wants to merge 87 commits into
Conversation
Wunka
left a comment
There was a problem hiding this comment.
I still think that std.sort is probably way nicer and easier. Here is an example (not tested) based on your current code (but of course not completly mapped and I with an added getTags method for the Item struct):
pub fn sortItems(source: ClientInventory, ignoredSlotCount: usize) void {
compressItems(source);
const ctx: SortContext = .{.inv = source};
std.sort.insertionContext(ignoredSlotCount, source.super._items.len, ctx);
}
const SortContext = struct {
inv: ClientInventory,
pub fn lessThan(ctx: @This(), a: usize, b: usize) bool {
const itemA = ctx.inv.getItem(a);
const itemB = ctx.inv.getItem(b);
if(itemA == .null) return false;
if(itemB == .null) return true;
const itemATags = itemA.getTags().?;
const itemBTags = itemB.getTags().?;
for(0..@min(itemATags.len, itemBTags.len)) |i| {
if(itemATags[i] == itemBTags[i]) continue;
return std.mem.lessThan(u8, itemATags[i].getName(), itemBTags[i].getName());
}
if(itemATags.len != itemBTags.len) return itemATags.len < itemBTags.len;
return std.mem.lessThan(u8, itemA.id().?, itemB.id().?);
}
pub fn swap(ctx: @This(), a: usize, b: usize) void {
main.sync.client.executeCommand(.{.depositOrSwap = .{
.dest = .{.inv = ctx.inv.super, .slot = @intCast(a)},
.source = .{.inv = ctx.inv.super, .slot = @intCast(b)},
}});
}
};|
also fixes #3195 |
I realized how to turn tags into numbers so I’m gonna try this |
|
is ready for rereview |
Wunka
left a comment
There was a problem hiding this comment.
Nice. Now the code looks much better
|
I don't see any changes since my last review. Did you forget to push? |
|
testing finished |
| } | ||
| if ((ctx.inv.getAmount(a) > ctx.inv.getAmount(b)) and std.mem.eql(u8, itemA.id().?, itemB.id().?)) return true; | ||
|
|
||
| return std.mem.lessThan(u8, itemA.id().?, itemB.id().?); |
There was a problem hiding this comment.
They should still sort by id first (you did accound for that with items, but not tools).
There was a problem hiding this comment.
sorts by id first now
| .disabled = options.disabled, | ||
| .hideBackground = options.hideBackground, | ||
| }; | ||
| if (self.hideBackground) self.child.mutSize().* = self.size; |
There was a problem hiding this comment.
oops forgot to remove that line
| if (std.mem.lessThan(u8, itemA.id().?, itemB.id().?)) return true; | ||
| if (!std.mem.lessThan(u8, itemA.id().?, itemB.id().?) and !std.mem.eql(u8, itemA.id().?, itemB.id().?)) return false; |
There was a problem hiding this comment.
on the second line the first argument is already proven to be false (because of the first line). So either do:
| if (std.mem.lessThan(u8, itemA.id().?, itemB.id().?)) return true; | |
| if (!std.mem.lessThan(u8, itemA.id().?, itemB.id().?) and !std.mem.eql(u8, itemA.id().?, itemB.id().?)) return false; | |
| if (std.mem.lessThan(u8, itemA.id().?, itemB.id().?)) return true; | |
| if (!std.mem.eql(u8, itemA.id().?, itemB.id().?)) return false; |
or
| if (std.mem.lessThan(u8, itemA.id().?, itemB.id().?)) return true; | |
| if (!std.mem.lessThan(u8, itemA.id().?, itemB.id().?) and !std.mem.eql(u8, itemA.id().?, itemB.id().?)) return false; | |
| if (!std.mem.eql(u8, itemA.id().?, itemB.id().?)) return std.mem.lessThan(u8, itemA.id().?, itemB.id().?): |
There was a problem hiding this comment.
applied suggesion
| if (std.mem.lessThan(u8, itemA.id().?, itemB.id().?)) return true; | ||
| if (!std.mem.lessThan(u8, itemA.id().?, itemB.id().?) and !std.mem.eql(u8, itemA.id().?, itemB.id().?)) return false; |
There was a problem hiding this comment.
this is already checked a few lines above
|
thanks for your help wunka |
| pub fn initIcon(pos: Vec2f, iconSize: Vec2f, iconTexture: Texture, options: Options) *Button { | ||
| const icon = Icon.init(undefined, iconSize, iconTexture); | ||
| pub fn initIcon(pos: Vec2f, givenIconSize: Vec2f, iconTexture: Texture, options: Options) *Button { | ||
| const calculatedIconSize: Vec2f = if (options.hideBackground) givenIconSize + @as(Vec2f, @splat(3*border)) else givenIconSize; |
There was a problem hiding this comment.
This looks wrong, if there is no background, then it should not add any border around the supplied size.
There was a problem hiding this comment.
nope getting rid of the border just makes the button smaller or the icon smaller than it needs to be
| if (!std.mem.eql(u8, itemA.id().?, itemB.id().?)) return false; | ||
| if ((itemA == .proceduralItem) and (itemB == .proceduralItem)) { | ||
| const itemADurabilityPercent: f32 = @as(f32, @floatFromInt(itemA.proceduralItem.durability))/itemA.proceduralItem.getProperty(.maxDurability); | ||
| const itemBDurabilityPercent: f32 = @as(f32, @floatFromInt(itemB.proceduralItem.durability))/itemB.proceduralItem.getProperty(.maxDurability); |
There was a problem hiding this comment.
I think durability should be the last property to consider (→after dps), otherwise items with the identical recipe will not be sorted next to each other.
Co-authored-by: IntegratedQuantum <43880493+IntegratedQuantum@users.noreply.github.com>
…nto Sort-Button-2



Sort button now functions as normal
Before sorting it will compress all items into their max stacks and push them all to the lowest index getting rid of any holes
Then the sort button will first seperate items into procederal items and nonprocedural items
then they will be sorted by tag
items that share the same first tag will be internally sorted by their second tag and so on and so forth
procedural items are sorted by durability
Also allows buttons to be hidden
if a button is hidden the icon within it is extended to the size of the whole button (its scaled up).