diff --git a/src/main/java/de/ellpeck/actuallyadditions/api/booklet/IBookletEntry.java b/src/main/java/de/ellpeck/actuallyadditions/api/booklet/IBookletEntry.java index ae60daa37..ae345e8a2 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/api/booklet/IBookletEntry.java +++ b/src/main/java/de/ellpeck/actuallyadditions/api/booklet/IBookletEntry.java @@ -10,7 +10,6 @@ package de.ellpeck.actuallyadditions.api.booklet; -import de.ellpeck.actuallyadditions.api.booklet.internal.GuiBookletBase; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; @@ -29,5 +28,5 @@ public interface IBookletEntry{ void addChapter(IBookletChapter chapter); @SideOnly(Side.CLIENT) - List getChaptersForGuiDisplaying(GuiBookletBase gui, String searchBarText); + List getChaptersForDisplay(String searchBarText); } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/booklet/entry/BookletEntry.java b/src/main/java/de/ellpeck/actuallyadditions/mod/booklet/entry/BookletEntry.java index 591f675f2..74e5f3a89 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/booklet/entry/BookletEntry.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/booklet/entry/BookletEntry.java @@ -13,13 +13,18 @@ package de.ellpeck.actuallyadditions.mod.booklet.entry; import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI; import de.ellpeck.actuallyadditions.api.booklet.IBookletChapter; import de.ellpeck.actuallyadditions.api.booklet.IBookletEntry; +import de.ellpeck.actuallyadditions.api.booklet.IBookletPage; import de.ellpeck.actuallyadditions.api.booklet.internal.GuiBookletBase; import de.ellpeck.actuallyadditions.mod.util.ModUtil; import de.ellpeck.actuallyadditions.mod.util.StringUtil; +import net.minecraft.client.Minecraft; +import net.minecraft.item.ItemStack; import net.minecraft.util.text.TextFormatting; +import net.minecraftforge.fluids.FluidStack; import java.util.ArrayList; import java.util.List; +import java.util.Locale; public class BookletEntry implements IBookletEntry{ @@ -60,8 +65,64 @@ public class BookletEntry implements IBookletEntry{ } @Override - public List getChaptersForGuiDisplaying(GuiBookletBase gui, String searchBarText){ - return this.getAllChapters(); + public List getChaptersForDisplay(String searchBarText){ + if(searchBarText != null && !searchBarText.isEmpty()){ + String search = searchBarText.toLowerCase(Locale.ROOT); + + List fittingChapters = new ArrayList(); + for(IBookletChapter chapter : this.getAllChapters()){ + if(chapter.getLocalizedName().toLowerCase(Locale.ROOT).contains(search)){ + fittingChapters.add(chapter); + } + else{ + for(IBookletPage page : chapter.getAllPages()){ + if(fitsFilter(page, search)){ + fittingChapters.add(chapter); + break; + } + } + } + } + + return fittingChapters; + } + else{ + return this.getAllChapters(); + } + } + + private static boolean fitsFilter(IBookletPage page, String searchBarText){ + Minecraft mc = Minecraft.getMinecraft(); + + List items = new ArrayList(); + page.getItemStacksForPage(items); + if(!items.isEmpty()){ + for(ItemStack stack : items){ + if(stack != null){ + List tooltip = stack.getTooltip(mc.thePlayer, mc.gameSettings.advancedItemTooltips); + for(String strg : tooltip){ + if(strg != null && strg.toLowerCase(Locale.ROOT).contains(searchBarText)){ + return true; + } + } + } + } + } + + List fluids = new ArrayList(); + page.getFluidStacksForPage(fluids); + if(!fluids.isEmpty()){ + for(FluidStack stack : fluids){ + if(stack != null){ + String strg = stack.getLocalizedName(); + if(strg != null && strg.toLowerCase(Locale.ROOT).contains(searchBarText)){ + return true; + } + } + } + } + + return false; } public BookletEntry setImportant(){ diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/booklet/gui/GuiBooklet.java b/src/main/java/de/ellpeck/actuallyadditions/mod/booklet/gui/GuiBooklet.java index 4a19654e0..e502be321 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/booklet/gui/GuiBooklet.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/booklet/gui/GuiBooklet.java @@ -10,14 +10,17 @@ package de.ellpeck.actuallyadditions.mod.booklet.gui; +import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI; import de.ellpeck.actuallyadditions.api.booklet.internal.GuiBookletBase; import de.ellpeck.actuallyadditions.mod.inventory.gui.TexturedButton; import de.ellpeck.actuallyadditions.mod.util.AssetUtil; import de.ellpeck.actuallyadditions.mod.util.StringUtil; import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.GuiScreen; +import net.minecraft.client.gui.GuiTextField; import net.minecraft.client.renderer.GlStateManager; import net.minecraft.util.ResourceLocation; +import net.minecraft.util.text.TextFormatting; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; import org.lwjgl.input.Keyboard; @@ -39,6 +42,8 @@ public abstract class GuiBooklet extends GuiBookletBase{ private GuiButton buttonRight; private GuiButton buttonBack; + public GuiTextField searchField; + protected int xSize; protected int ySize; protected int guiLeft; @@ -73,6 +78,12 @@ public abstract class GuiBooklet extends GuiBookletBase{ this.buttonBack = new TexturedButton(RES_LOC_GADGETS, -2002, this.guiLeft-15, this.guiTop-3, 36, 54, 18, 10); this.buttonList.add(this.buttonBack); } + + if(this.hasSearchBar()){ + this.searchField = new GuiTextField(-420, this.fontRendererObj, this.guiLeft+this.xSize+2, this.guiTop+this.ySize-40+2, 64, 12); + this.searchField.setMaxStringLength(50); + this.searchField.setEnableBackgroundDrawing(false); + } } @Override @@ -81,9 +92,43 @@ public abstract class GuiBooklet extends GuiBookletBase{ this.mc.getTextureManager().bindTexture(RES_LOC_GUI); drawModalRectWithCustomSizedTexture(this.guiLeft, this.guiTop, 0, 0, this.xSize, this.ySize, 512, 512); + if(this.hasSearchBar()){ + this.mc.getTextureManager().bindTexture(RES_LOC_GADGETS); + this.drawTexturedModalRect(this.guiLeft+this.xSize, this.guiTop+this.ySize-40, 188, 0, 68, 14); + + boolean unicodeBefore = this.fontRendererObj.getUnicodeFlag(); + this.fontRendererObj.setUnicodeFlag(true); + + if(!this.searchField.isFocused() && (this.searchField.getText() == null || this.searchField.getText().isEmpty())){ + this.fontRendererObj.drawString(TextFormatting.ITALIC+"Click to search...", this.guiLeft+this.xSize+2, this.guiTop+this.ySize-40+2, 0xFFFFFF, false); + } + + this.searchField.drawTextBox(); + + this.fontRendererObj.setUnicodeFlag(unicodeBefore); + } + super.drawScreen(mouseX, mouseY, partialTicks); } + @Override + protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException{ + super.mouseClicked(mouseX, mouseY, mouseButton); + + if(this.hasSearchBar()){ + this.searchField.mouseClicked(mouseX, mouseY, mouseButton); + } + } + + @Override + public void updateScreen(){ + super.updateScreen(); + + if(this.hasSearchBar()){ + this.searchField.updateCursorCounter(); + } + } + @Override public boolean doesGuiPauseGame(){ return false; @@ -113,6 +158,15 @@ public abstract class GuiBooklet extends GuiBookletBase{ } + public boolean hasSearchBar(){ + return true; + } + + public void onSearchBarChanged(String searchBarText){ + GuiBookletBase parent = !(this instanceof GuiEntry) ? this : this.parentPage; + this.mc.displayGuiScreen(new GuiEntry(this.previousScreen, parent, ActuallyAdditionsAPI.allAndSearch, 0, searchBarText, true)); + } + @Override protected void actionPerformed(GuiButton button) throws IOException{ if(this.hasPageLeftButton() && button == this.buttonLeft){ @@ -130,12 +184,16 @@ public abstract class GuiBooklet extends GuiBookletBase{ } @Override - protected void keyTyped(char typedChar, int keyCode) throws IOException{ - if(this.previousScreen != null && keyCode == Keyboard.KEY_ESCAPE){ + protected void keyTyped(char typedChar, int key) throws IOException{ + if(key == Keyboard.KEY_ESCAPE || (key == this.mc.gameSettings.keyBindInventory.getKeyCode() && (!this.hasSearchBar() || !this.searchField.isFocused()))){ this.mc.displayGuiScreen(this.previousScreen); } + else if(this.hasSearchBar() & this.searchField.isFocused()){ + this.searchField.textboxKeyTyped(typedChar, key); + this.onSearchBarChanged(this.searchField.getText()); + } else{ - super.keyTyped(typedChar, keyCode); + super.keyTyped(typedChar, key); } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/booklet/gui/GuiEntry.java b/src/main/java/de/ellpeck/actuallyadditions/mod/booklet/gui/GuiEntry.java index 9a731f480..5ac48ce75 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/booklet/gui/GuiEntry.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/booklet/gui/GuiEntry.java @@ -32,20 +32,24 @@ public class GuiEntry extends GuiBooklet{ private final int entryPage; private final IBookletEntry entry; private final List chapters; + private final String searchText; + private final boolean focusSearch; - public GuiEntry(GuiScreen previousScreen, GuiBookletBase parentPage, IBookletEntry entry, int entryPage){ + public GuiEntry(GuiScreen previousScreen, GuiBookletBase parentPage, IBookletEntry entry, int entryPage, String search, boolean focusSearch){ super(previousScreen, parentPage); this.entryPage = entryPage; this.entry = entry; - this.chapters = entry.getChaptersForGuiDisplaying(this, null /*TODO Insert search bar text here*/); + this.searchText = search; + this.focusSearch = focusSearch; + this.chapters = entry.getChaptersForDisplay(search); } - public GuiEntry(GuiScreen previousScreen, GuiBookletBase parentPage, IBookletEntry entry, IBookletChapter chapterForPageCalc){ - this(previousScreen, parentPage, entry, calcEntryPage(entry, chapterForPageCalc)); + public GuiEntry(GuiScreen previousScreen, GuiBookletBase parentPage, IBookletEntry entry, IBookletChapter chapterForPageCalc, String search, boolean focusSearch){ + this(previousScreen, parentPage, entry, calcEntryPage(entry, chapterForPageCalc, search), search, focusSearch); } - private static int calcEntryPage(IBookletEntry entry, IBookletChapter chapterForPageCalc){ - int index = entry.getAllChapters().indexOf(chapterForPageCalc); + private static int calcEntryPage(IBookletEntry entry, IBookletChapter chapterForPageCalc, String search){ + int index = entry.getChaptersForDisplay(search).indexOf(chapterForPageCalc); return index/(BUTTONS_PER_PAGE*2); } @@ -53,6 +57,13 @@ public class GuiEntry extends GuiBooklet{ public void initGui(){ super.initGui(); + if(this.hasSearchBar() && this.searchText != null){ + this.searchField.setText(this.searchText); + if(this.focusSearch){ + this.searchField.setFocused(true); + } + } + int idOffset = this.entryPage*(BUTTONS_PER_PAGE*2); for(int x = 0; x < 2; x++){ for(int y = 0; y < BUTTONS_PER_PAGE; y++){ @@ -100,7 +111,7 @@ public class GuiEntry extends GuiBooklet{ @Override public void onPageLeftButtonPressed(){ - this.mc.displayGuiScreen(new GuiEntry(this.previousScreen, this.parentPage, this.entry, this.entryPage-1)); + this.mc.displayGuiScreen(new GuiEntry(this.previousScreen, this.parentPage, this.entry, this.entryPage-1, this.searchText, this.searchField.isFocused())); } @Override @@ -108,7 +119,7 @@ public class GuiEntry extends GuiBooklet{ if(!this.chapters.isEmpty()){ IBookletChapter lastChap = this.chapters.get(this.chapters.size()-1); if(lastChap != null){ - int lastPage = calcEntryPage(this.entry, lastChap); + int lastPage = calcEntryPage(this.entry, lastChap, this.searchText); return this.entryPage < lastPage; } } @@ -117,7 +128,7 @@ public class GuiEntry extends GuiBooklet{ @Override public void onPageRightButtonPressed(){ - this.mc.displayGuiScreen(new GuiEntry(this.previousScreen, this.parentPage, this.entry, this.entryPage+1)); + this.mc.displayGuiScreen(new GuiEntry(this.previousScreen, this.parentPage, this.entry, this.entryPage+1, this.searchText, this.searchField.isFocused())); } @Override diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/booklet/gui/GuiMainPage.java b/src/main/java/de/ellpeck/actuallyadditions/mod/booklet/gui/GuiMainPage.java index 315622cef..2676c7735 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/booklet/gui/GuiMainPage.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/booklet/gui/GuiMainPage.java @@ -49,7 +49,7 @@ public class GuiMainPage extends GuiBooklet{ if(ActuallyAdditionsAPI.BOOKLET_ENTRIES.size() > button.id){ IBookletEntry entry = ActuallyAdditionsAPI.BOOKLET_ENTRIES.get(button.id); if(entry != null){ - this.mc.displayGuiScreen(new GuiEntry(this.previousScreen, this, entry, 0)); + this.mc.displayGuiScreen(new GuiEntry(this.previousScreen, this, entry, 0, "", false)); } } } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/booklet/misc/BookletUtils.java b/src/main/java/de/ellpeck/actuallyadditions/mod/booklet/misc/BookletUtils.java index 83bab9043..bc299d41d 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/booklet/misc/BookletUtils.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/booklet/misc/BookletUtils.java @@ -46,7 +46,7 @@ public final class BookletUtils{ GuiMainPage mainPage = new GuiMainPage(previousScreen); IBookletChapter chapter = page.getChapter(); - GuiEntry entry = new GuiEntry(previousScreen, mainPage, chapter.getEntry(), chapter); + GuiEntry entry = new GuiEntry(previousScreen, mainPage, chapter.getEntry(), chapter, "", false); return createPageGui(previousScreen, entry, page); } diff --git a/src/main/resources/assets/actuallyadditions/textures/gui/booklet/guiBookletGadgets.png b/src/main/resources/assets/actuallyadditions/textures/gui/booklet/guiBookletGadgets.png index 0bfed6f38..a5842423f 100644 Binary files a/src/main/resources/assets/actuallyadditions/textures/gui/booklet/guiBookletGadgets.png and b/src/main/resources/assets/actuallyadditions/textures/gui/booklet/guiBookletGadgets.png differ