mirror of
https://github.com/GTNewHorizons/EnderIO.git
synced 2026-11-26 15:03:07 +00:00
sorting for inventory panel
This commit is contained in:
1 parent
7742233460
commit
f2473362c1
5 files changed
+219
-14
No files matched your search
@@ -144,7 +144,17 @@ public final class IconEIO {
|
||||
public static final IconEIO SUN = new IconEIO(160, 240);
|
||||
public static final IconEIO RAIN = new IconEIO(176, 240);
|
||||
public static final IconEIO THUNDER = new IconEIO(192, 240);
|
||||
|
||||
|
||||
public static final IconEIO SORT_DIR_DOWN = new IconEIO( 0, 176);
|
||||
public static final IconEIO SORT_DIR_UP = new IconEIO(16, 176);
|
||||
|
||||
public static final IconEIO SORT_NAME_DOWN = new IconEIO(208, 240, SORT_DIR_DOWN);
|
||||
public static final IconEIO SORT_NAME_UP = new IconEIO(208, 240, SORT_DIR_UP);
|
||||
public static final IconEIO SORT_SIZE_DOWN = new IconEIO(224, 240, SORT_DIR_DOWN);
|
||||
public static final IconEIO SORT_SIZE_UP = new IconEIO(224, 240, SORT_DIR_UP);
|
||||
public static final IconEIO SORT_MOD_DOWN = new IconEIO(240, 240, SORT_DIR_DOWN);
|
||||
public static final IconEIO SORT_MOD_UP = new IconEIO(240, 240, SORT_DIR_UP);
|
||||
|
||||
// Texture size is actually 512 but everything is aligned to a 256 grid
|
||||
private static final int TEX_SIZE = 256;
|
||||
private static final double PIX_SIZE = 1d / TEX_SIZE;
|
||||
@@ -155,24 +165,34 @@ public final class IconEIO {
|
||||
public final double maxV;
|
||||
public final double width;
|
||||
public final double height;
|
||||
public final IconEIO overlay;
|
||||
|
||||
public static final ResourceLocation TEXTURE = new ResourceLocation("enderio:textures/gui/widgetsv2.png");
|
||||
|
||||
public IconEIO(int x, int y) {
|
||||
this(x, y, 16, 16);
|
||||
this(x, y, null);
|
||||
}
|
||||
|
||||
public IconEIO(int x, int y, IconEIO overlay) {
|
||||
this(x, y, 16, 16, overlay);
|
||||
}
|
||||
|
||||
public IconEIO(int x, int y, int width, int height) {
|
||||
this(width, height, (float) (PIX_SIZE * x), (float) (PIX_SIZE * (x + width)), (float) (PIX_SIZE * y), (float) (PIX_SIZE * (y + height)));
|
||||
this(x, y, width, height, null);
|
||||
}
|
||||
|
||||
public IconEIO(double width, double height, double minU, double maxU, double minV, double maxV) {
|
||||
public IconEIO(int x, int y, int width, int height, IconEIO overlay) {
|
||||
this(width, height, (float) (PIX_SIZE * x), (float) (PIX_SIZE * (x + width)), (float) (PIX_SIZE * y), (float) (PIX_SIZE * (y + height)), overlay);
|
||||
}
|
||||
|
||||
public IconEIO(double width, double height, double minU, double maxU, double minV, double maxV, IconEIO overlay) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.minU = minU;
|
||||
this.maxU = maxU;
|
||||
this.minV = minV;
|
||||
this.maxV = maxV;
|
||||
this.overlay = overlay;
|
||||
}
|
||||
|
||||
public void renderIcon(double x, double y) {
|
||||
@@ -206,6 +226,9 @@ public final class IconEIO {
|
||||
tessellator.addVertexWithUV(x + width, y + 0, zLevel, maxU, minV);
|
||||
tessellator.addVertexWithUV(x, y + 0, zLevel, minU, minV);
|
||||
}
|
||||
if(overlay != null) {
|
||||
overlay.renderIcon(x, y, width, height, zLevel, false, flipY);
|
||||
}
|
||||
if(doDraw) {
|
||||
tessellator.draw();
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package crazypants.enderio.machine.invpanel;
|
||||
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import crazypants.enderio.gui.IconButtonEIO;
|
||||
import crazypants.enderio.gui.IconEIO;
|
||||
import crazypants.enderio.machine.gui.GuiMachineBase;
|
||||
import crazypants.gui.GhostSlot;
|
||||
@@ -9,6 +10,7 @@ import crazypants.render.RenderUtil;
|
||||
import java.awt.Rectangle;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.FontRenderer;
|
||||
import net.minecraft.client.gui.GuiButton;
|
||||
import net.minecraft.client.renderer.Tessellator;
|
||||
import net.minecraft.inventory.Container;
|
||||
import net.minecraft.item.ItemStack;
|
||||
@@ -21,9 +23,13 @@ public class GuiInventoryPanel extends GuiMachineBase<TileInventoryPanel> {
|
||||
private static final Rectangle btnScrollDown = new Rectangle(216, 109, 16, 8);
|
||||
private static final Rectangle thumbArea = new Rectangle(216, 35, 16, 74);
|
||||
|
||||
private static final int ID_SORT = 9876;
|
||||
|
||||
private static final int GHOST_COLUMNS = 6;
|
||||
private static final int GHOST_ROWS = 5;
|
||||
|
||||
private final IconButtonEIO btnSort;
|
||||
|
||||
private boolean scrollUpPressed;
|
||||
private boolean scrollDownPressed;
|
||||
private int scrollPos;
|
||||
@@ -44,6 +50,22 @@ public class GuiInventoryPanel extends GuiMachineBase<TileInventoryPanel> {
|
||||
ghostSlots.add(slot);
|
||||
}
|
||||
}
|
||||
|
||||
btnSort = new IconButtonEIO(this, ID_SORT, 216, 7, getSortOrderIcon());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initGui() {
|
||||
super.initGui();
|
||||
btnSort.onGuiInit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(GuiButton b) {
|
||||
super.actionPerformed(b);
|
||||
if(b.id == ID_SORT) {
|
||||
toggleSortOrder();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -79,7 +101,7 @@ public class GuiInventoryPanel extends GuiMachineBase<TileInventoryPanel> {
|
||||
|
||||
tes.draw();
|
||||
|
||||
if(getDatabase().sortClientItems()) {
|
||||
if(getDatabase().sortItems()) {
|
||||
updateGhostSlots();
|
||||
}
|
||||
|
||||
@@ -104,6 +126,29 @@ public class GuiInventoryPanel extends GuiMachineBase<TileInventoryPanel> {
|
||||
return (InventoryDatabaseClient) getTileEntity().getDatabaseClient();
|
||||
}
|
||||
|
||||
private IconEIO getSortOrderIcon() {
|
||||
InventoryDatabaseClient db = getDatabase();
|
||||
SortOrder order = db.getSortOrder();
|
||||
boolean invert = db.isSortOrderInverted();
|
||||
switch (order) {
|
||||
case NAME: return invert ? IconEIO.SORT_NAME_UP : IconEIO.SORT_NAME_DOWN;
|
||||
case COUNT: return invert ? IconEIO.SORT_SIZE_UP : IconEIO.SORT_SIZE_DOWN;
|
||||
case MOD: return invert ? IconEIO.SORT_MOD_UP : IconEIO.SORT_MOD_DOWN;
|
||||
default: return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void toggleSortOrder() {
|
||||
InventoryDatabaseClient db = getDatabase();
|
||||
SortOrder order = db.getSortOrder();
|
||||
if(db.isSortOrderInverted()) {
|
||||
SortOrder[] values = SortOrder.values();
|
||||
order = values[(order.ordinal()+1) % values.length];
|
||||
}
|
||||
db.setSortOrder(order, !db.isSortOrderInverted());
|
||||
btnSort.setIcon(getSortOrderIcon());
|
||||
}
|
||||
|
||||
private void doScroll() {
|
||||
scrollMax = Math.max(0, (getDatabase().getNumEntries()+GHOST_COLUMNS-1) / GHOST_COLUMNS - GHOST_ROWS);
|
||||
if(scrollUpPressed && scrollPos > 0) {
|
||||
|
||||
@@ -1,11 +1,17 @@
|
||||
package crazypants.enderio.machine.invpanel;
|
||||
|
||||
import cpw.mods.fml.common.registry.GameRegistry;
|
||||
import crazypants.enderio.network.CompressedDataInput;
|
||||
import crazypants.enderio.network.PacketHandler;
|
||||
import java.io.IOException;
|
||||
import java.text.Collator;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.CompressedStreamTools;
|
||||
@@ -16,8 +22,11 @@ public class InventoryDatabaseClient extends InventoryDatabase<InventoryDatabase
|
||||
private final TileInventoryPanel te;
|
||||
private final ArrayList<ItemEntry> clientItems;
|
||||
private final HashSet<Integer> requestedItems;
|
||||
|
||||
private boolean clientListNeedsSorting;
|
||||
|
||||
private SortOrder order = SortOrder.NAME;
|
||||
private boolean invertSortOrder;
|
||||
private boolean needsSorting;
|
||||
private Collator collator;
|
||||
|
||||
public InventoryDatabaseClient(TileInventoryPanel te) {
|
||||
this.te = te;
|
||||
@@ -60,7 +69,7 @@ public class InventoryDatabaseClient extends InventoryDatabase<InventoryDatabase
|
||||
int count = cdi.readVariable();
|
||||
setItemCount(entry, count);
|
||||
}
|
||||
clientListNeedsSorting = true;
|
||||
needsSorting = true;
|
||||
} finally {
|
||||
cdi.close();
|
||||
}
|
||||
@@ -123,7 +132,7 @@ public class InventoryDatabaseClient extends InventoryDatabase<InventoryDatabase
|
||||
if(missingItems != null) {
|
||||
PacketHandler.INSTANCE.sendToServer(new PacketRequestMissingItems(te, missingItems));
|
||||
}
|
||||
clientListNeedsSorting = true;
|
||||
needsSorting = true;
|
||||
} finally {
|
||||
cdi.close();
|
||||
}
|
||||
@@ -149,10 +158,46 @@ public class InventoryDatabaseClient extends InventoryDatabase<InventoryDatabase
|
||||
return list;
|
||||
}
|
||||
|
||||
public boolean sortClientItems() {
|
||||
boolean res = clientListNeedsSorting;
|
||||
clientListNeedsSorting = false;
|
||||
return res;
|
||||
private Collator getCollator() {
|
||||
if(collator == null) {
|
||||
String langCode = Minecraft.getMinecraft().getLanguageManager().getCurrentLanguage().getLanguageCode();
|
||||
Locale locale = Locale.forLanguageTag(langCode);
|
||||
collator = Collator.getInstance(locale);
|
||||
}
|
||||
return collator;
|
||||
}
|
||||
|
||||
public void setSortOrder(SortOrder order, boolean invert) {
|
||||
if(this.order != order || this.invertSortOrder != invert) {
|
||||
this.order = order;
|
||||
this.invertSortOrder = invert;
|
||||
this.needsSorting = true;
|
||||
}
|
||||
}
|
||||
|
||||
public SortOrder getSortOrder() {
|
||||
return order;
|
||||
}
|
||||
|
||||
public boolean isSortOrderInverted() {
|
||||
return invertSortOrder;
|
||||
}
|
||||
|
||||
public boolean sortItems() {
|
||||
if(needsSorting) {
|
||||
Comparator<ItemEntry> cmp;
|
||||
switch (order) {
|
||||
case NAME: cmp = new NameComparator(getCollator()); break;
|
||||
case MOD: cmp = new ModComparator(getCollator()); break;
|
||||
default: cmp = CountComparator.INSTANCE;
|
||||
}
|
||||
if(invertSortOrder) {
|
||||
cmp = Collections.reverseOrder(cmp);
|
||||
}
|
||||
Collections.sort(clientItems, cmp);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public int getNumEntries() {
|
||||
@@ -173,6 +218,8 @@ public class InventoryDatabaseClient extends InventoryDatabase<InventoryDatabase
|
||||
}
|
||||
|
||||
public static class ItemEntry extends ItemEntryBase {
|
||||
String name;
|
||||
String modId;
|
||||
int count;
|
||||
|
||||
public ItemEntry(int dbID, int hash, int itemID, int meta, NBTTagCompound nbt) {
|
||||
@@ -183,10 +230,93 @@ public class InventoryDatabaseClient extends InventoryDatabase<InventoryDatabase
|
||||
return count;
|
||||
}
|
||||
|
||||
public Item getItem() {
|
||||
return Item.getItemById(itemID);
|
||||
}
|
||||
|
||||
public ItemStack makeItemStack() {
|
||||
ItemStack stack = new ItemStack(Item.getItemById(itemID), count, meta);
|
||||
ItemStack stack = new ItemStack(getItem(), count, meta);
|
||||
stack.stackTagCompound = nbt;
|
||||
return stack;
|
||||
}
|
||||
|
||||
public String getUnlocName() {
|
||||
if(name == null) {
|
||||
findUnlocName();
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
private void findUnlocName() {
|
||||
ItemStack stack = makeItemStack();
|
||||
try {
|
||||
name = stack.getDisplayName();
|
||||
if(name == null || name.isEmpty()) {
|
||||
name = stack.getItem().getUnlocalizedName();
|
||||
if(name == null || name.isEmpty()) {
|
||||
name = stack.getItem().getClass().getName();
|
||||
}
|
||||
}
|
||||
} catch(Throwable ex) {
|
||||
name = "Exception: " + ex.getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
public String getModId() {
|
||||
if(modId == null) {
|
||||
findModId();
|
||||
}
|
||||
return modId;
|
||||
}
|
||||
|
||||
private void findModId() {
|
||||
Item item = getItem();
|
||||
GameRegistry.UniqueIdentifier id = GameRegistry.findUniqueIdentifierFor(item);
|
||||
if(id != null && id.modId != null) {
|
||||
modId = id.modId;
|
||||
} else {
|
||||
modId = "Unknown";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static class NameComparator implements Comparator<ItemEntry> {
|
||||
protected final Collator collator;
|
||||
|
||||
NameComparator(Collator collator) {
|
||||
this.collator = collator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compare(ItemEntry a, ItemEntry b) {
|
||||
String nameA = a.getUnlocName();
|
||||
String nameB = b.getUnlocName();
|
||||
return collator.compare(nameA, nameB);
|
||||
}
|
||||
}
|
||||
|
||||
static class ModComparator extends NameComparator {
|
||||
ModComparator(Collator collator) {
|
||||
super(collator);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compare(ItemEntry a, ItemEntry b) {
|
||||
String modIdA = a.getModId();
|
||||
String modIdB = b.getModId();
|
||||
int res = collator.compare(modIdA, modIdB);
|
||||
if(res == 0) {
|
||||
res = super.compare(a, b);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
static class CountComparator implements Comparator<ItemEntry> {
|
||||
public static final CountComparator INSTANCE = new CountComparator();
|
||||
@Override
|
||||
public int compare(ItemEntry a, ItemEntry b) {
|
||||
return a.count - b.count;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package crazypants.enderio.machine.invpanel;
|
||||
|
||||
public enum SortOrder {
|
||||
NAME,
|
||||
MOD,
|
||||
COUNT
|
||||
}
|
||||
Reference in new issue
Block a user