Added search bar

This commit is contained in:
Ellpeck 2016-11-12 12:26:36 +01:00
parent 30849fb304
commit 0327ee9a26
7 changed files with 147 additions and 18 deletions

View file

@ -10,7 +10,6 @@
package de.ellpeck.actuallyadditions.api.booklet; 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.Side;
import net.minecraftforge.fml.relauncher.SideOnly; import net.minecraftforge.fml.relauncher.SideOnly;
@ -29,5 +28,5 @@ public interface IBookletEntry{
void addChapter(IBookletChapter chapter); void addChapter(IBookletChapter chapter);
@SideOnly(Side.CLIENT) @SideOnly(Side.CLIENT)
List<IBookletChapter> getChaptersForGuiDisplaying(GuiBookletBase gui, String searchBarText); List<IBookletChapter> getChaptersForDisplay(String searchBarText);
} }

View file

@ -13,13 +13,18 @@ package de.ellpeck.actuallyadditions.mod.booklet.entry;
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI; import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
import de.ellpeck.actuallyadditions.api.booklet.IBookletChapter; import de.ellpeck.actuallyadditions.api.booklet.IBookletChapter;
import de.ellpeck.actuallyadditions.api.booklet.IBookletEntry; 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.api.booklet.internal.GuiBookletBase;
import de.ellpeck.actuallyadditions.mod.util.ModUtil; import de.ellpeck.actuallyadditions.mod.util.ModUtil;
import de.ellpeck.actuallyadditions.mod.util.StringUtil; 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.minecraft.util.text.TextFormatting;
import net.minecraftforge.fluids.FluidStack;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale;
public class BookletEntry implements IBookletEntry{ public class BookletEntry implements IBookletEntry{
@ -60,9 +65,65 @@ public class BookletEntry implements IBookletEntry{
} }
@Override @Override
public List<IBookletChapter> getChaptersForGuiDisplaying(GuiBookletBase gui, String searchBarText){ public List<IBookletChapter> getChaptersForDisplay(String searchBarText){
if(searchBarText != null && !searchBarText.isEmpty()){
String search = searchBarText.toLowerCase(Locale.ROOT);
List<IBookletChapter> fittingChapters = new ArrayList<IBookletChapter>();
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(); return this.getAllChapters();
} }
}
private static boolean fitsFilter(IBookletPage page, String searchBarText){
Minecraft mc = Minecraft.getMinecraft();
List<ItemStack> items = new ArrayList<ItemStack>();
page.getItemStacksForPage(items);
if(!items.isEmpty()){
for(ItemStack stack : items){
if(stack != null){
List<String> tooltip = stack.getTooltip(mc.thePlayer, mc.gameSettings.advancedItemTooltips);
for(String strg : tooltip){
if(strg != null && strg.toLowerCase(Locale.ROOT).contains(searchBarText)){
return true;
}
}
}
}
}
List<FluidStack> fluids = new ArrayList<FluidStack>();
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(){ public BookletEntry setImportant(){
this.color = TextFormatting.DARK_GREEN; this.color = TextFormatting.DARK_GREEN;

View file

@ -10,14 +10,17 @@
package de.ellpeck.actuallyadditions.mod.booklet.gui; 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.api.booklet.internal.GuiBookletBase;
import de.ellpeck.actuallyadditions.mod.inventory.gui.TexturedButton; import de.ellpeck.actuallyadditions.mod.inventory.gui.TexturedButton;
import de.ellpeck.actuallyadditions.mod.util.AssetUtil; import de.ellpeck.actuallyadditions.mod.util.AssetUtil;
import de.ellpeck.actuallyadditions.mod.util.StringUtil; import de.ellpeck.actuallyadditions.mod.util.StringUtil;
import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen; import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.GuiTextField;
import net.minecraft.client.renderer.GlStateManager; import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly; import net.minecraftforge.fml.relauncher.SideOnly;
import org.lwjgl.input.Keyboard; import org.lwjgl.input.Keyboard;
@ -39,6 +42,8 @@ public abstract class GuiBooklet extends GuiBookletBase{
private GuiButton buttonRight; private GuiButton buttonRight;
private GuiButton buttonBack; private GuiButton buttonBack;
public GuiTextField searchField;
protected int xSize; protected int xSize;
protected int ySize; protected int ySize;
protected int guiLeft; 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.buttonBack = new TexturedButton(RES_LOC_GADGETS, -2002, this.guiLeft-15, this.guiTop-3, 36, 54, 18, 10);
this.buttonList.add(this.buttonBack); 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 @Override
@ -81,9 +92,43 @@ public abstract class GuiBooklet extends GuiBookletBase{
this.mc.getTextureManager().bindTexture(RES_LOC_GUI); this.mc.getTextureManager().bindTexture(RES_LOC_GUI);
drawModalRectWithCustomSizedTexture(this.guiLeft, this.guiTop, 0, 0, this.xSize, this.ySize, 512, 512); 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); 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 @Override
public boolean doesGuiPauseGame(){ public boolean doesGuiPauseGame(){
return false; 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 @Override
protected void actionPerformed(GuiButton button) throws IOException{ protected void actionPerformed(GuiButton button) throws IOException{
if(this.hasPageLeftButton() && button == this.buttonLeft){ if(this.hasPageLeftButton() && button == this.buttonLeft){
@ -130,12 +184,16 @@ public abstract class GuiBooklet extends GuiBookletBase{
} }
@Override @Override
protected void keyTyped(char typedChar, int keyCode) throws IOException{ protected void keyTyped(char typedChar, int key) throws IOException{
if(this.previousScreen != null && keyCode == Keyboard.KEY_ESCAPE){ if(key == Keyboard.KEY_ESCAPE || (key == this.mc.gameSettings.keyBindInventory.getKeyCode() && (!this.hasSearchBar() || !this.searchField.isFocused()))){
this.mc.displayGuiScreen(this.previousScreen); this.mc.displayGuiScreen(this.previousScreen);
} }
else if(this.hasSearchBar() & this.searchField.isFocused()){
this.searchField.textboxKeyTyped(typedChar, key);
this.onSearchBarChanged(this.searchField.getText());
}
else{ else{
super.keyTyped(typedChar, keyCode); super.keyTyped(typedChar, key);
} }
} }

View file

@ -32,20 +32,24 @@ public class GuiEntry extends GuiBooklet{
private final int entryPage; private final int entryPage;
private final IBookletEntry entry; private final IBookletEntry entry;
private final List<IBookletChapter> chapters; private final List<IBookletChapter> 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); super(previousScreen, parentPage);
this.entryPage = entryPage; this.entryPage = entryPage;
this.entry = entry; 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){ public GuiEntry(GuiScreen previousScreen, GuiBookletBase parentPage, IBookletEntry entry, IBookletChapter chapterForPageCalc, String search, boolean focusSearch){
this(previousScreen, parentPage, entry, calcEntryPage(entry, chapterForPageCalc)); this(previousScreen, parentPage, entry, calcEntryPage(entry, chapterForPageCalc, search), search, focusSearch);
} }
private static int calcEntryPage(IBookletEntry entry, IBookletChapter chapterForPageCalc){ private static int calcEntryPage(IBookletEntry entry, IBookletChapter chapterForPageCalc, String search){
int index = entry.getAllChapters().indexOf(chapterForPageCalc); int index = entry.getChaptersForDisplay(search).indexOf(chapterForPageCalc);
return index/(BUTTONS_PER_PAGE*2); return index/(BUTTONS_PER_PAGE*2);
} }
@ -53,6 +57,13 @@ public class GuiEntry extends GuiBooklet{
public void initGui(){ public void initGui(){
super.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); int idOffset = this.entryPage*(BUTTONS_PER_PAGE*2);
for(int x = 0; x < 2; x++){ for(int x = 0; x < 2; x++){
for(int y = 0; y < BUTTONS_PER_PAGE; y++){ for(int y = 0; y < BUTTONS_PER_PAGE; y++){
@ -100,7 +111,7 @@ public class GuiEntry extends GuiBooklet{
@Override @Override
public void onPageLeftButtonPressed(){ 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 @Override
@ -108,7 +119,7 @@ public class GuiEntry extends GuiBooklet{
if(!this.chapters.isEmpty()){ if(!this.chapters.isEmpty()){
IBookletChapter lastChap = this.chapters.get(this.chapters.size()-1); IBookletChapter lastChap = this.chapters.get(this.chapters.size()-1);
if(lastChap != null){ if(lastChap != null){
int lastPage = calcEntryPage(this.entry, lastChap); int lastPage = calcEntryPage(this.entry, lastChap, this.searchText);
return this.entryPage < lastPage; return this.entryPage < lastPage;
} }
} }
@ -117,7 +128,7 @@ public class GuiEntry extends GuiBooklet{
@Override @Override
public void onPageRightButtonPressed(){ 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 @Override

View file

@ -49,7 +49,7 @@ public class GuiMainPage extends GuiBooklet{
if(ActuallyAdditionsAPI.BOOKLET_ENTRIES.size() > button.id){ if(ActuallyAdditionsAPI.BOOKLET_ENTRIES.size() > button.id){
IBookletEntry entry = ActuallyAdditionsAPI.BOOKLET_ENTRIES.get(button.id); IBookletEntry entry = ActuallyAdditionsAPI.BOOKLET_ENTRIES.get(button.id);
if(entry != null){ if(entry != null){
this.mc.displayGuiScreen(new GuiEntry(this.previousScreen, this, entry, 0)); this.mc.displayGuiScreen(new GuiEntry(this.previousScreen, this, entry, 0, "", false));
} }
} }
} }

View file

@ -46,7 +46,7 @@ public final class BookletUtils{
GuiMainPage mainPage = new GuiMainPage(previousScreen); GuiMainPage mainPage = new GuiMainPage(previousScreen);
IBookletChapter chapter = page.getChapter(); 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); return createPageGui(previousScreen, entry, page);
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.3 KiB

After

Width:  |  Height:  |  Size: 5.7 KiB