aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorDoKM <54663875+DoKM@users.noreply.github.com>2021-10-02 08:58:12 +0200
committerGitHub <noreply@github.com>2021-10-02 08:58:12 +0200
commit0aa7768a4c8c3119644f58bd166935ff0d29bef0 (patch)
tree0dfa3a1be66696ad0baf284259238ee51a32e9dc /src/main/java
parent7e985dc228a0998cf35474b96c2244e16c8ca7a5 (diff)
parent215e4a84802b9e02078ff0938cb1651bda673bc3 (diff)
downloadNotEnoughUpdates-0aa7768a4c8c3119644f58bd166935ff0d29bef0.tar.gz
NotEnoughUpdates-0aa7768a4c8c3119644f58bd166935ff0d29bef0.tar.bz2
NotEnoughUpdates-0aa7768a4c8c3119644f58bd166935ff0d29bef0.zip
Merge pull request #31 from Lulonaut/master
Tab completion for ah search overlay
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/io/github/moulberry/notenoughupdates/overlays/AuctionSearchOverlay.java104
1 files changed, 93 insertions, 11 deletions
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/overlays/AuctionSearchOverlay.java b/src/main/java/io/github/moulberry/notenoughupdates/overlays/AuctionSearchOverlay.java
index 816a6c63..016e638b 100644
--- a/src/main/java/io/github/moulberry/notenoughupdates/overlays/AuctionSearchOverlay.java
+++ b/src/main/java/io/github/moulberry/notenoughupdates/overlays/AuctionSearchOverlay.java
@@ -32,6 +32,7 @@ import java.util.concurrent.atomic.AtomicInteger;
public class AuctionSearchOverlay {
private static final ResourceLocation SEARCH_OVERLAY_TEXTURE = new ResourceLocation("notenoughupdates:auc_search/ah_search_overlay.png");
+ private static final ResourceLocation SEARCH_OVERLAY_TEXTURE_TAB_COMPLETED = new ResourceLocation("notenoughupdates:auc_search/ah_search_overlay_tab_completed.png");
private static final ResourceLocation STAR = new ResourceLocation("notenoughupdates:auc_search/star.png");
private static final ResourceLocation STAR_BOARD = new ResourceLocation("notenoughupdates:auc_search/star_board.png");
@@ -40,6 +41,8 @@ public class AuctionSearchOverlay {
private static String searchString = "";
private static String searchStringExtra = "";
private static Splitter SPACE_SPLITTER = Splitter.on(" ").omitEmptyStrings().trimResults();
+ private static boolean tabCompleted = false;
+ private static int tabCompletionIndex = -1;
private static int selectedStars = 0;
private static boolean atLeast = true;
@@ -146,24 +149,31 @@ public class AuctionSearchOverlay {
int num = 0;
synchronized(autocompletedItems) {
- for(String str : autocompletedItems) {
+ String[] autoCompletedItemsArray = autocompletedItems.toArray(new String[0]);
+ for (int i = 0; i < autocompletedItems.size(); i++) {
+ String str = autoCompletedItemsArray[i];
JsonObject obj = NotEnoughUpdates.INSTANCE.manager.getItemInformation().get(str);
- if(obj != null) {
+ if (obj != null) {
ItemStack stack = NotEnoughUpdates.INSTANCE.manager.jsonToStack(obj);
//Gui.drawRect(width/2-96, height/4+30+num*22, width/2+96, height/4+30+num*22+20, 0xff505050);
+ if (i == tabCompletionIndex) {
+ Minecraft.getMinecraft().getTextureManager().bindTexture(SEARCH_OVERLAY_TEXTURE_TAB_COMPLETED);
+ GlStateManager.color(1, 1, 1, 1);
+ Utils.drawTexturedRect(width / 2 - 96 + 1, topY + 30 + num * 22 + 1, 193, 21, 0 / 512f, 193 / 512f, 0, 21 / 256f, GL11.GL_NEAREST);
+ } else {
+ Minecraft.getMinecraft().getTextureManager().bindTexture(SEARCH_OVERLAY_TEXTURE);
+ GlStateManager.color(1, 1, 1, 1);
+ Utils.drawTexturedRect(width / 2 - 96 + 1, topY + 30 + num * 22 + 1, 193, 21, 214 / 512f, 407 / 512f, 0, 21 / 256f, GL11.GL_NEAREST);
- Minecraft.getMinecraft().getTextureManager().bindTexture(SEARCH_OVERLAY_TEXTURE);
- GlStateManager.color(1, 1, 1, 1);
- Utils.drawTexturedRect(width/2-96+1, topY+30+num*22+1, 193, 21, 214/512f, 407/512f, 0, 21/256f, GL11.GL_NEAREST);
-
+ }
String itemName = Utils.trimIgnoreColour(stack.getDisplayName().replaceAll("\\[.+]", ""));
- if(itemName.contains("Enchanted Book") && str.contains(";")) {
+ if (itemName.contains("Enchanted Book") && str.contains(";")) {
String[] lore = NotEnoughUpdates.INSTANCE.manager.getLoreFromNBT(stack.getTagCompound());
itemName = lore[0].trim();
}
Minecraft.getMinecraft().fontRendererObj.drawString(Minecraft.getMinecraft().fontRendererObj.trimStringToWidth(itemName, 165),
- width/2-74, topY+35+num*22+1, 0xdddddd, true);
+ width / 2 - 74, topY + 35 + num * 22 + 1, 0xdddddd, true);
GlStateManager.enableDepth();
Utils.drawItemStack(stack, width/2-94+2, topY+32+num*22+1);
@@ -273,15 +283,35 @@ public class AuctionSearchOverlay {
private static ExecutorService searchES = Executors.newSingleThreadExecutor();
private static AtomicInteger searchId = new AtomicInteger(0);
+ private static String getItemIdAtIndex(int i) {
+ if (!autocompletedItems.isEmpty()) {
+ if ((i > autocompletedItems.size() - 1) || i < 0 || i > 4) {
+ return "";
+ }
+ String searchString = autocompletedItems.toArray()[i].toString();
+ JsonObject repoObject = NotEnoughUpdates.INSTANCE.manager.getItemInformation().get(searchString);
+ String displayname = repoObject.get("displayname").getAsString();
+ if (displayname.contains("Enchanted Book")) {
+ String lore = repoObject.get("lore").getAsJsonArray().get(0).getAsString();
+ String name = lore.substring(0, lore.lastIndexOf(" "));
+ return Utils.cleanColour(name);
+ } else {
+ return Utils.cleanColour(displayname);
+ }
+ } else {
+ return null;
+ }
+ }
+
public static void search() {
final int thisSearchId = searchId.incrementAndGet();
searchES.submit(() -> {
- if(thisSearchId != searchId.get()) return;
+ if (thisSearchId != searchId.get()) return;
- List<String> title = new ArrayList<>(NotEnoughUpdates.INSTANCE.manager.search("title:"+searchString.trim()));
+ List<String> title = new ArrayList<>(NotEnoughUpdates.INSTANCE.manager.search("title:" + searchString.trim()));
- if(thisSearchId != searchId.get()) return;
+ if (thisSearchId != searchId.get()) return;
if(!searchString.trim().contains(" ")) {
StringBuilder sb = new StringBuilder();
@@ -334,7 +364,59 @@ public class AuctionSearchOverlay {
} else if(Keyboard.getEventKey() == Keyboard.KEY_RETURN) {
searchStringExtra = "";
close();
+ } else if(Keyboard.getEventKey() == Keyboard.KEY_TAB) {
+ //autocomplete to first item in the list
+ tabCompleted = true;
+ String id = getItemIdAtIndex(0);
+ if (id == null) {
+ tabCompleted = false;
+ textField.setFocus(true);
+ textField.setText(searchString);
+ } else {
+ tabCompletionIndex = 0;
+ searchString = id;
+ }
} else if(Keyboard.getEventKeyState()) {
+ if (tabCompleted) {
+ String id;
+ switch (Keyboard.getEventKey()) {
+ case Keyboard.KEY_DOWN:
+ id = getItemIdAtIndex(tabCompletionIndex + 1);
+ if (id == null) {
+ textField.setFocus(true);
+ textField.setText(searchString);
+ tabCompleted = false;
+ tabCompletionIndex = -1;
+ } else if (id.equals("")) {
+ //At the end of the autocompletion List, do nothing
+ return;
+ } else {
+ searchString = id;
+ tabCompletionIndex += 1;
+ return;
+ }
+ break;
+ case Keyboard.KEY_UP:
+ id = getItemIdAtIndex(tabCompletionIndex - 1);
+ if (id == null) {
+ textField.setFocus(true);
+ textField.setText(searchString);
+ tabCompleted = false;
+ tabCompletionIndex = -1;
+ } else if (id.equals("")) {
+ //At the end of the autocompletion List, do nothing
+ return;
+ } else {
+ searchString = id;
+ tabCompletionIndex -= 1;
+ return;
+ }
+ break;
+ default:
+ tabCompletionIndex = -1;
+ tabCompleted = false;
+ }
+ }
textField.setFocus(true);
textField.setText(searchString);
textField.keyTyped(Keyboard.getEventCharacter(), Keyboard.getEventKey());