mirror of
https://github.com/Ellpeck/ActuallyAdditions.git
synced 2024-11-26 00:38:35 +01:00
Added search bar
This commit is contained in:
parent
30849fb304
commit
0327ee9a26
7 changed files with 147 additions and 18 deletions
|
@ -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<IBookletChapter> getChaptersForGuiDisplaying(GuiBookletBase gui, String searchBarText);
|
||||
List<IBookletChapter> getChaptersForDisplay(String searchBarText);
|
||||
}
|
||||
|
|
|
@ -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<IBookletChapter> getChaptersForGuiDisplaying(GuiBookletBase gui, String searchBarText){
|
||||
return this.getAllChapters();
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
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(){
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -32,20 +32,24 @@ public class GuiEntry extends GuiBooklet{
|
|||
private final int entryPage;
|
||||
private final IBookletEntry entry;
|
||||
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);
|
||||
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
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 5.3 KiB After Width: | Height: | Size: 5.7 KiB |
Loading…
Reference in a new issue