From 185500c0177f0f55380c37d14529a05e9ccdc158 Mon Sep 17 00:00:00 2001 From: yueh Date: Tue, 30 Sep 2014 15:50:11 +0200 Subject: [PATCH 1/2] Adds filter by machine name to interface terminals --- .../implementations/GuiInterfaceTerminal.java | 181 +++++++++++------- .../textures/guis/interfaceterminal.png | Bin 3356 -> 1928 bytes 2 files changed, 114 insertions(+), 67 deletions(-) diff --git a/src/main/java/appeng/client/gui/implementations/GuiInterfaceTerminal.java b/src/main/java/appeng/client/gui/implementations/GuiInterfaceTerminal.java index e5f675c64..4cb8a8607 100644 --- a/src/main/java/appeng/client/gui/implementations/GuiInterfaceTerminal.java +++ b/src/main/java/appeng/client/gui/implementations/GuiInterfaceTerminal.java @@ -14,6 +14,7 @@ import org.lwjgl.opengl.GL11; import appeng.client.gui.AEBaseGui; import appeng.client.gui.widgets.GuiScrollbar; +import appeng.client.gui.widgets.MEGuiTextField; import appeng.client.me.ClientDCInternalInv; import appeng.client.me.SlotDisconnected; import appeng.container.implementations.ContainerInterfaceTerminal; @@ -25,26 +26,32 @@ import com.google.common.collect.HashMultimap; public class GuiInterfaceTerminal extends AEBaseGui { + private static final int LINES_ON_PAGE = 6; + + // TODO: copied from GuiMEMonitorable. It looks not changed, maybe unneeded? + int offsetX = 9; + final HashMap byId = new HashMap(); final HashMultimap byName = HashMultimap.create(); final ArrayList names = new ArrayList(); ArrayList lines = new ArrayList(); + LinkedList dcSlots = new LinkedList(); + private MEGuiTextField searchField; - private int getTotalRows() + private int getMaxRows() { return names.size() + byId.size();// unique names, and each inv row. } - public GuiInterfaceTerminal(InventoryPlayer inventoryPlayer, PartMonitor te) { + public GuiInterfaceTerminal(InventoryPlayer inventoryPlayer, PartMonitor te) + { super( new ContainerInterfaceTerminal( inventoryPlayer, te ) ); myScrollBar = new GuiScrollbar(); xSize = 195; ySize = 222; } - LinkedList dcSlots = new LinkedList(); - @Override public void initGui() { @@ -52,6 +59,45 @@ public class GuiInterfaceTerminal extends AEBaseGui myScrollBar.setLeft( 175 ); myScrollBar.setHeight( 106 ); myScrollBar.setTop( 18 ); + + searchField = new MEGuiTextField( fontRendererObj, this.guiLeft + Math.max( 107, offsetX ), this.guiTop + 6, 64, fontRendererObj.FONT_HEIGHT ); + searchField.setEnableBackgroundDrawing( false ); + searchField.setMaxStringLength( 25 ); + searchField.setTextColor( 0xFFFFFF ); + searchField.setVisible( true ); + } + + @Override + protected void mouseClicked(int xCoord, int yCoord, int btn) + { + searchField.mouseClicked( xCoord, yCoord, btn ); + + if ( btn == 1 && searchField.isMouseIn( xCoord, yCoord ) ) + { + searchField.setText( "" ); + refreshList(); + } + + super.mouseClicked( xCoord, yCoord, btn ); + } + + @Override + protected void keyTyped(char character, int key) + { + if ( !this.checkHotbarKeys( key ) ) + { + if ( character == ' ' && this.searchField.getText().length() == 0 ) + return; + + if ( searchField.textboxKeyTyped( character, key ) ) + { + refreshList(); + } + else + { + super.keyTyped( character, key ); + } + } } @Override @@ -62,24 +108,23 @@ public class GuiInterfaceTerminal extends AEBaseGui int offset = 17; int ex = myScrollBar.getCurrentScroll(); - int linesOnPage = 6; - for (int x = 0; x < linesOnPage; x++) + for (int x = 0; x < LINES_ON_PAGE && ex + x < lines.size(); x++) { - if ( ex + x < lines.size() ) + Object lineObj = lines.get( ex + x ); + if ( lineObj instanceof ClientDCInternalInv ) { - Object lineObj = lines.get( ex + x ); - if ( lineObj instanceof ClientDCInternalInv ) - { - ClientDCInternalInv inv = (ClientDCInternalInv) lineObj; + ClientDCInternalInv inv = (ClientDCInternalInv) lineObj; - GL11.glColor4f( 1, 1, 1, 1 ); - for (int z = 0; z < inv.inv.getSizeInventory(); z++) - this.drawTexturedModalRect( offsetX + z * 18 + 7, offsetY + offset, 7, 139, 18, 18 ); - } + GL11.glColor4f( 1, 1, 1, 1 ); + for (int z = 0; z < inv.inv.getSizeInventory(); z++) + this.drawTexturedModalRect( offsetX + z * 18 + 7, offsetY + offset, 7, 139, 18, 18 ); } offset += 18; } + + if ( searchField != null ) + searchField.drawTextBox(); } @Override @@ -89,10 +134,7 @@ public class GuiInterfaceTerminal extends AEBaseGui fontRendererObj.drawString( GuiText.inventory.getLocal(), 8, ySize - 96 + 3, 4210752 ); int offset = 17; - - // for (String name : lines) int ex = myScrollBar.getCurrentScroll(); - int linesOnPage = 6; Iterator o = inventorySlots.inventorySlots.iterator(); while (o.hasNext()) @@ -101,33 +143,30 @@ public class GuiInterfaceTerminal extends AEBaseGui o.remove(); } - for (int x = 0; x < linesOnPage; x++) + for (int x = 0; x < LINES_ON_PAGE && ex + x < lines.size(); x++) { - if ( ex + x < lines.size() ) + Object lineObj = lines.get( ex + x ); + if ( lineObj instanceof ClientDCInternalInv ) { - Object lineObj = lines.get( ex + x ); - if ( lineObj instanceof ClientDCInternalInv ) + ClientDCInternalInv inv = (ClientDCInternalInv) lineObj; + for (int z = 0; z < inv.inv.getSizeInventory(); z++) { - ClientDCInternalInv inv = (ClientDCInternalInv) lineObj; - for (int z = 0; z < inv.inv.getSizeInventory(); z++) - { - inventorySlots.inventorySlots.add( new SlotDisconnected( inv, z, z * 18 + 8, 1 + offset ) ); - } + inventorySlots.inventorySlots.add( new SlotDisconnected( inv, z, z * 18 + 8, 1 + offset ) ); } - else if ( lineObj instanceof String ) - { - String name = (String) lineObj; - int rows = byName.get( name ).size(); - if ( rows > 1 ) - name = name + " (" + rows + ")"; - - while (name.length() > 2 && fontRendererObj.getStringWidth( name ) > 155) - name = name.substring( 0, name.length() - 1 ); - - fontRendererObj.drawString( name, 10, 6 + offset, 4210752 ); - } - offset += 18; } + else if ( lineObj instanceof String ) + { + String name = (String) lineObj; + int rows = byName.get( name ).size(); + if ( rows > 1 ) + name = name + " (" + rows + ")"; + + while (name.length() > 2 && fontRendererObj.getStringWidth( name ) > 155) + name = name.substring( 0, name.length() - 1 ); + + fontRendererObj.drawString( name, 10, 6 + offset, 4210752 ); + } + offset += 18; } } @@ -168,36 +207,44 @@ public class GuiInterfaceTerminal extends AEBaseGui if ( refreshList ) { refreshList = false; - - byName.clear(); - for (ClientDCInternalInv o : byId.values()) - byName.put( o.getName(), o ); - - names.clear(); - names.addAll( byName.keySet() ); - - Collections.sort( names ); - - lines = new ArrayList( getTotalRows() ); - for (String n : names) - { - lines.add( n ); - - ArrayList clientInventories = new ArrayList(); - clientInventories.addAll( byName.get( n ) ); - - Collections.sort( clientInventories ); - - for (ClientDCInternalInv i : clientInventories) - { - lines.add( i ); - } - } - - myScrollBar.setRange( 0, getTotalRows() - 6, 2 ); + refreshList(); } } + private void refreshList() + { + byName.clear(); + String searchFilterLowerCase = searchField.getText().toLowerCase(); + String itemNameLowerCase; + + for (ClientDCInternalInv entry : byId.values()) + { + itemNameLowerCase = entry.getName().toLowerCase(); + if ( !searchFilterLowerCase.equals( "" ) && !itemNameLowerCase.contains( searchFilterLowerCase ) ) + continue; + byName.put( entry.getName(), entry ); + } + + names.clear(); + names.addAll( byName.keySet() ); + + Collections.sort( names ); + + lines = new ArrayList( getMaxRows() ); + for (String n : names) + { + lines.add( n ); + + ArrayList clientInventories = new ArrayList(); + clientInventories.addAll( byName.get( n ) ); + + Collections.sort( clientInventories ); + lines.addAll( clientInventories ); + } + + myScrollBar.setRange( 0, lines.size() - LINES_ON_PAGE, 2 ); + } + private ClientDCInternalInv getById(long id, long sortBy, String string) { ClientDCInternalInv o = byId.get( id ); diff --git a/src/main/resources/assets/appliedenergistics2/textures/guis/interfaceterminal.png b/src/main/resources/assets/appliedenergistics2/textures/guis/interfaceterminal.png index d4c4dc132e387774d882366d3b617e5fef9b3254..9f6e68f501d3614ce782febca6befa179b2f5047 100644 GIT binary patch literal 1928 zcmeAS@N?(olHy`uVBq!ia0y~yU}OMc4rT@hhU_&FAq)%*Ea{HEjtmSN`?>!lvNA9* zGX(gAxH2#>n46o2hK6=`cON@;?7@Qv|NsAI7zLvtFfu|w`Nlt6P(~>U@(cbyvH?Tz zcXm|<1_sUokH}&M2EJV&%qY6w>=FY5gG7mIL`iUdT1k0gQ7S`0VrE{6US4X6f{C7i zo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EG%H&f0zQvNbWBfAO;M&EUNz44ZGd$E|EvduCi!rB=OaI$MI-?-SZzFC3|H^FN>B z|Me2XnKb#zrE;@ooPY6h=9__a#IG90_}+PTRlmSKi%UOe&sZ~6)$!M#%`aa5oC$IL z`seS%8_q8n5#d2FQeJ#!tud|o_2MNsrYq-K{%83RI*kP_neC5fv}+y?>4RW+5Y8#* z#Xnqq@pAEUaLRvH`b?gm_f7}FT>Zb0;aYs*{!_o41JDxVeD;J|#`r|n^Tt(GVS_w- zQ6cyU7BqJ82f`OG@1JJ}F1@bLthX^KP-aUoTmL+t;e)LK16o0P=X~WKwgfYK8Sx!+ zpT2G1XEd=Q<$>F@_42n)rJPG_ye&ut@N@r@>GA*AN;}KGia8%EW?*1o@O1TaS?83{ F1OVq5_&fjr From 79837cf35f40ba73d08a677451ce98cdf5db4d81 Mon Sep 17 00:00:00 2001 From: yueh Date: Tue, 30 Sep 2014 17:52:28 +0200 Subject: [PATCH 2/2] Adds filter by pattern to the interface terminals Some code cleanup --- .../implementations/GuiInterfaceTerminal.java | 73 ++++++++++++++----- 1 file changed, 53 insertions(+), 20 deletions(-) diff --git a/src/main/java/appeng/client/gui/implementations/GuiInterfaceTerminal.java b/src/main/java/appeng/client/gui/implementations/GuiInterfaceTerminal.java index 4cb8a8607..1220a5f74 100644 --- a/src/main/java/appeng/client/gui/implementations/GuiInterfaceTerminal.java +++ b/src/main/java/appeng/client/gui/implementations/GuiInterfaceTerminal.java @@ -4,8 +4,8 @@ import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; -import java.util.LinkedList; +import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; @@ -31,22 +31,19 @@ public class GuiInterfaceTerminal extends AEBaseGui // TODO: copied from GuiMEMonitorable. It looks not changed, maybe unneeded? int offsetX = 9; - final HashMap byId = new HashMap(); - final HashMultimap byName = HashMultimap.create(); - final ArrayList names = new ArrayList(); + private final HashMap byId = new HashMap(); + private final HashMultimap byName = HashMultimap.create(); + private final ArrayList names = new ArrayList(); + private final ArrayList lines = new ArrayList(); + private final EntityPlayer player; - ArrayList lines = new ArrayList(); - LinkedList dcSlots = new LinkedList(); + private boolean refreshList = false; private MEGuiTextField searchField; - private int getMaxRows() - { - return names.size() + byId.size();// unique names, and each inv row. - } - public GuiInterfaceTerminal(InventoryPlayer inventoryPlayer, PartMonitor te) { super( new ContainerInterfaceTerminal( inventoryPlayer, te ) ); + this.player = inventoryPlayer.player; myScrollBar = new GuiScrollbar(); xSize = 195; ySize = 222; @@ -56,6 +53,7 @@ public class GuiInterfaceTerminal extends AEBaseGui public void initGui() { super.initGui(); + myScrollBar.setLeft( 175 ); myScrollBar.setHeight( 106 ); myScrollBar.setTop( 18 ); @@ -65,6 +63,7 @@ public class GuiInterfaceTerminal extends AEBaseGui searchField.setMaxStringLength( 25 ); searchField.setTextColor( 0xFFFFFF ); searchField.setVisible( true ); + searchField.setFocused( true ); } @Override @@ -170,8 +169,6 @@ public class GuiInterfaceTerminal extends AEBaseGui } } - boolean refreshList = false; - public void postUpdate(NBTTagCompound in) { if ( in.getBoolean( "clear" ) ) @@ -211,18 +208,42 @@ public class GuiInterfaceTerminal extends AEBaseGui } } + /** + * rebuilds the list of interfaces. + * + * Respects a search term if present (ignores case) and adding only matching patterns. + */ private void refreshList() { byName.clear(); - String searchFilterLowerCase = searchField.getText().toLowerCase(); - String itemNameLowerCase; + + final String searchFilterLowerCase = searchField.getText().toLowerCase(); for (ClientDCInternalInv entry : byId.values()) { - itemNameLowerCase = entry.getName().toLowerCase(); - if ( !searchFilterLowerCase.equals( "" ) && !itemNameLowerCase.contains( searchFilterLowerCase ) ) - continue; - byName.put( entry.getName(), entry ); + // Shortcut to skip any filter if search term is ""/empty + boolean found = searchFilterLowerCase.isEmpty(); + + // Search if the current inventory holds a pattern containing the search term. + if ( !found && !searchFilterLowerCase.equals( "" ) ) + { + for (ItemStack itemStack : entry.inv) + { + if ( itemStack != null ) + { + String tooltipLowerCase = String.valueOf( itemStack.getTooltip( player, false ) ).toLowerCase(); + if ( tooltipLowerCase.contains( searchFilterLowerCase ) ) + { + found = true; + break; + } + } + } + } + + // if found, filter skipped or machine name matching the search term, add it + if ( found || entry.getName().toLowerCase().contains( searchFilterLowerCase ) ) + byName.put( entry.getName(), entry ); } names.clear(); @@ -230,7 +251,9 @@ public class GuiInterfaceTerminal extends AEBaseGui Collections.sort( names ); - lines = new ArrayList( getMaxRows() ); + lines.clear(); + lines.ensureCapacity( getMaxRows() ); + for (String n : names) { lines.add( n ); @@ -245,6 +268,16 @@ public class GuiInterfaceTerminal extends AEBaseGui myScrollBar.setRange( 0, lines.size() - LINES_ON_PAGE, 2 ); } + /** + * The max amount of unique names and each inv row. Not affected by the filtering. + * + * @return + */ + private int getMaxRows() + { + return names.size() + byId.size(); + } + private ClientDCInternalInv getById(long id, long sortBy, String string) { ClientDCInternalInv o = byId.get( id );