Skip to content

Commit

Permalink
Optimize interface terminal gui (#205)
Browse files Browse the repository at this point in the history
Co-authored-by: slprime <[email protected]>
  • Loading branch information
slprime and slprime authored Apr 18, 2024
1 parent 8dbeee2 commit b9b4211
Showing 1 changed file with 62 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -460,19 +460,20 @@ private int drawSection(InterfaceWirelessSection section, int viewY, int relMous
GL11.glTranslatef(0.0f, 0.0f, -(ITEM_STACK_OVERLAY_Z + ITEM_STACK_Z + STEP_Z));
GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

Iterator<InerfaceWirelessEntry> visible = section.getVisible();
Iterator<InterfaceWirelessEntry> visible = section.getVisible();
while (visible.hasNext()) {
if (viewY < viewHeight) {
InterfaceWirelessEntry entry = visible.next();
if (viewY + renderY + entry.rows * 18 + 1 > 0 && viewY + renderY < viewHeight) {
renderY += drawEntry(
visible.next(),
entry,
viewY + InterfaceWirelessSection.TITLE_HEIGHT + renderY,
title,
relMouseX,
relMouseY);
} else {
InerfaceWirelessEntry entry = visible.next();
entry.dispY = -9999;
entry.optionsButton.yPosition = -1;
renderY += entry.rows * 18 + 1;
}
}
bindTexture(BACKGROUND);
Expand Down Expand Up @@ -516,7 +517,7 @@ private int drawSection(InterfaceWirelessSection section, int viewY, int relMous
*
* @param viewY the gui coordinate z
*/
private int drawEntry(InerfaceWirelessEntry entry, int viewY, int titleBottom, int relMouseX, int relMouseY) {
private int drawEntry(InterfaceWirelessEntry entry, int viewY, int titleBottom, int relMouseX, int relMouseY) {
bindTexture(BACKGROUND);
Tessellator.instance.startDrawingQuads();
int relY = 0;
Expand Down Expand Up @@ -607,7 +608,7 @@ private int drawEntry(InerfaceWirelessEntry entry, int viewY, int titleBottom, i
aeRenderItem.zLevel = 0.0f;
RenderHelper.disableStandardItemLighting();
if (!tooltip) {
if (entry.brokenRecipes[slotIdx]) {
if (entry.slotIsBroken(slotIdx)) {
GL11.glTranslatef(0.0f, 0.0f, SLOT_Z - ITEM_STACK_OVERLAY_Z);
drawRect(0, 0, 16, 16, GuiColors.ItemSlotOverlayInvalid.getColor());
} else if (entry.filteredRecipes[slotIdx]) {
Expand Down Expand Up @@ -834,7 +835,7 @@ public void postUpdate(List<PacketInterfaceTerminalUpdate.PacketEntry> updates,
private void parsePacketCmd(PacketInterfaceTerminalUpdate.PacketEntry cmd) {
long id = cmd.entryId;
if (cmd instanceof PacketInterfaceTerminalUpdate.PacketAdd addCmd) {
InerfaceWirelessEntry entry = new InerfaceWirelessEntry(
InterfaceWirelessEntry entry = new InterfaceWirelessEntry(
id,
addCmd.name,
addCmd.rows,
Expand All @@ -845,7 +846,7 @@ private void parsePacketCmd(PacketInterfaceTerminalUpdate.PacketEntry cmd) {
} else if (cmd instanceof PacketInterfaceTerminalUpdate.PacketRemove) {
masterList.removeEntry(id);
} else if (cmd instanceof PacketInterfaceTerminalUpdate.PacketOverwrite owCmd) {
InerfaceWirelessEntry entry = masterList.list.get(id);
InterfaceWirelessEntry entry = masterList.list.get(id);

if (entry == null) {
return;
Expand All @@ -864,7 +865,7 @@ private void parsePacketCmd(PacketInterfaceTerminalUpdate.PacketEntry cmd) {
}
masterList.isDirty = true;
} else if (cmd instanceof PacketInterfaceTerminalUpdate.PacketRename renameCmd) {
InerfaceWirelessEntry entry = masterList.list.get(id);
InterfaceWirelessEntry entry = masterList.list.get(id);

if (entry != null) {
if (StatCollector.canTranslate(renameCmd.newName)) {
Expand Down Expand Up @@ -959,12 +960,12 @@ public void setTextFieldValue(final String displayName, final int mousex, final
*/
private class InterfaceWirelessList {

private final Map<Long, InerfaceWirelessEntry> list = new HashMap<>();
private final Map<Long, InterfaceWirelessEntry> list = new HashMap<>();
private final Map<String, InterfaceWirelessSection> sections = new TreeMap<>();
private final List<InterfaceWirelessSection> visibleSections = new ArrayList<>();
private boolean isDirty;
private int height;
private InerfaceWirelessEntry hoveredEntry;
private InterfaceWirelessEntry hoveredEntry;

InterfaceWirelessList() {
this.isDirty = true;
Expand Down Expand Up @@ -1041,7 +1042,7 @@ private boolean scrollNextSection(boolean up) {
return result;
}

public void addEntry(InerfaceWirelessEntry entry) {
public void addEntry(InterfaceWirelessEntry entry) {
InterfaceWirelessSection section = sections.get(entry.dispName);

if (section == null) {
Expand All @@ -1054,7 +1055,7 @@ public void addEntry(InerfaceWirelessEntry entry) {
}

public void removeEntry(long id) {
InerfaceWirelessEntry entry = list.remove(id);
InterfaceWirelessEntry entry = list.remove(id);

if (entry != null) {
entry.section.removeEntry(entry);
Expand Down Expand Up @@ -1096,8 +1097,8 @@ private class InterfaceWirelessSection {
public static final int TITLE_HEIGHT = 12;

String name;
List<InerfaceWirelessEntry> entries = new ArrayList<>();
Set<InerfaceWirelessEntry> visibleEntries = new TreeSet<>(Comparator.comparing(e -> {
List<InterfaceWirelessEntry> entries = new ArrayList<>();
Set<InterfaceWirelessEntry> visibleEntries = new TreeSet<>(Comparator.comparing(e -> {
if (e.dispRep != null) {
return e.dispRep.getDisplayName() + e.id;
} else {
Expand Down Expand Up @@ -1128,7 +1129,7 @@ private void update() {
height = 0;
} else {
height = TITLE_HEIGHT;
for (InerfaceWirelessEntry entry : visibleEntries) {
for (InterfaceWirelessEntry entry : visibleEntries) {
height += entry.guiHeight;
}
}
Expand All @@ -1140,7 +1141,7 @@ public void refreshVisible() {
String input = GuiInterfaceWireless.this.searchFieldInputs.getText().toLowerCase();
String output = GuiInterfaceWireless.this.searchFieldOutputs.getText().toLowerCase();

for (InerfaceWirelessEntry entry : entries) {
for (InterfaceWirelessEntry entry : entries) {
var moleAss = AEApi.instance().definitions().blocks().molecularAssembler().maybeStack(1);
entry.dispY = -9999;
if (onlyMolecularAssemblers
Expand All @@ -1151,7 +1152,7 @@ public void refreshVisible() {
&& entry.numItems == entry.rows * entry.rowSize) {
continue;
}
if (onlyBrokenRecipes && entry.numBrokenRecipes == 0) {
if (onlyBrokenRecipes && !entry.hasBrokenSlot()) {
continue;
}
// Find search terms
Expand Down Expand Up @@ -1179,27 +1180,27 @@ && itemStackMatchesSearchTerm(stack, output, false)) {
}
}

public void addEntry(InerfaceWirelessEntry entry) {
public void addEntry(InterfaceWirelessEntry entry) {
this.entries.add(entry);
entry.section = this;
this.isDirty = true;
}

public void removeEntry(InerfaceWirelessEntry entry) {
public void removeEntry(InterfaceWirelessEntry entry) {
this.entries.remove(entry);
entry.section = null;
this.isDirty = true;
}

public Iterator<InerfaceWirelessEntry> getVisible() {
public Iterator<InterfaceWirelessEntry> getVisible() {
if (isDirty) {
update();
}
return visibleEntries.iterator();
}

public boolean mouseClicked(int relMouseX, int relMouseY, int btn) {
Iterator<InerfaceWirelessEntry> it = getVisible();
Iterator<InterfaceWirelessEntry> it = getVisible();
boolean ret = false;

while (it.hasNext() && !ret) {
Expand All @@ -1213,7 +1214,7 @@ public boolean mouseClicked(int relMouseX, int relMouseY, int btn) {
/**
* This class keeps track of an entry and its widgets.
*/
private class InerfaceWirelessEntry {
private class InterfaceWirelessEntry {

String dispName;
AppEngInternalInventory inv;
Expand All @@ -1231,14 +1232,13 @@ private class InerfaceWirelessEntry {
int guiHeight;
int dispY = -9999;
boolean online;
int numBrokenRecipes;
boolean[] brokenRecipes;
private Boolean[] brokenRecipes;
int numItems = 0;
/** Should recipe be filtered out/grayed out? */
boolean[] filteredRecipes;
private int hoveredSlotIdx = -1;

InerfaceWirelessEntry(long id, String name, int rows, int rowSize, boolean online) {
InterfaceWirelessEntry(long id, String name, int rows, int rowSize, boolean online) {
this.id = id;
if (StatCollector.canTranslate(name)) {
this.dispName = StatCollector.translateToLocal(name);
Expand All @@ -1259,11 +1259,11 @@ private class InerfaceWirelessEntry {
this.renameButton = new GuiFCImgButton(2, 0, "EDIT", "YES");
this.renameButton.setHalfSize(true);
this.guiHeight = 18 * rows + 1;
this.brokenRecipes = new boolean[rows * rowSize];
this.brokenRecipes = new Boolean[rows * rowSize];
this.filteredRecipes = new boolean[rows * rowSize];
}

InerfaceWirelessEntry setLocation(int x, int y, int z, int dim, int side) {
InterfaceWirelessEntry setLocation(int x, int y, int z, int dim, int side) {
this.x = x;
this.y = y;
this.z = z;
Expand All @@ -1273,7 +1273,7 @@ InerfaceWirelessEntry setLocation(int x, int y, int z, int dim, int side) {
return this;
}

InerfaceWirelessEntry setIcons(ItemStack selfRep, ItemStack dispRep) {
InterfaceWirelessEntry setIcons(ItemStack selfRep, ItemStack dispRep) {
// Kotlin would make this pretty easy :(
this.selfRep = selfRep;
this.dispRep = dispRep;
Expand All @@ -1284,7 +1284,7 @@ InerfaceWirelessEntry setIcons(ItemStack selfRep, ItemStack dispRep) {
public void fullItemUpdate(NBTTagList items, int newSize) {
inv = new AppEngInternalInventory(null, newSize);
rows = newSize / rowSize;
brokenRecipes = new boolean[newSize];
brokenRecipes = new Boolean[newSize];
numItems = 0;

for (int i = 0; i < inv.getSizeInventory(); ++i) {
Expand All @@ -1293,7 +1293,7 @@ public void fullItemUpdate(NBTTagList items, int newSize) {
this.guiHeight = 18 * rows + 4;
}

InerfaceWirelessEntry setItems(NBTTagList items) {
InterfaceWirelessEntry setItems(NBTTagList items) {
assert items.tagCount() == inv.getSizeInventory();

for (int i = 0; i < items.tagCount(); ++i) {
Expand All @@ -1309,21 +1309,46 @@ public void partialItemUpdate(NBTTagList items, int[] validIndices) {
}

private void setItemInSlot(ItemStack stack, int idx) {
final int oldBroke = brokenRecipes[idx] ? 1 : 0;
final int newBroke = recipeIsBroken(stack) ? 1 : 0;
final int oldHasItem = inv.getStackInSlot(idx) != null ? 1 : 0;
final int newHasItem = stack != null ? 1 : 0;

// Update broken recipe count
numBrokenRecipes += newBroke - oldBroke;
brokenRecipes[idx] = newBroke == 1;
inv.setInventorySlotContents(idx, stack);
assert numBrokenRecipes >= 0;
// Update item count
numItems += newHasItem - oldHasItem;
assert numItems >= 0;
}

public boolean hasBrokenSlot() {
boolean existsUnknown = false;

for (int idx = 0; idx < brokenRecipes.length; idx++) {
if (brokenRecipes[idx] == null) {
existsUnknown = true;
} else if (brokenRecipes[idx] == true) {
return true;
}
}

if (existsUnknown) {
for (int idx = 0; idx < brokenRecipes.length; idx++) {
if (slotIsBroken(idx)) {
return true;
}
}
}

return false;
}

public boolean slotIsBroken(int idx) {

if (brokenRecipes[idx] == null) {
brokenRecipes[idx] = recipeIsBroken(inv.getStackInSlot(idx));
}

return brokenRecipes[idx];
}

public AppEngInternalInventory getInventory() {
return inv;
}
Expand Down

0 comments on commit b9b4211

Please sign in to comment.