ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/booklet/gui/GuiBooklet.java

331 lines
11 KiB
Java
Raw Normal View History

/*
* This file ("GuiBooklet.java") is part of the Actually Additions mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://ellpeck.de/actaddlicense
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
* © 2015-2016 Ellpeck
*/
package de.ellpeck.actuallyadditions.mod.booklet.gui;
2016-11-12 12:26:36 +01:00
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
2016-11-10 21:07:15 +01:00
import de.ellpeck.actuallyadditions.api.booklet.internal.GuiBookletBase;
import de.ellpeck.actuallyadditions.mod.booklet.button.BookmarkButton;
import de.ellpeck.actuallyadditions.mod.data.PlayerData;
import de.ellpeck.actuallyadditions.mod.data.PlayerData.PlayerSave;
2016-11-11 18:55:32 +01:00
import de.ellpeck.actuallyadditions.mod.inventory.gui.TexturedButton;
2016-11-12 15:09:30 +01:00
import de.ellpeck.actuallyadditions.mod.network.PacketHandlerHelper;
import de.ellpeck.actuallyadditions.mod.util.AssetUtil;
import de.ellpeck.actuallyadditions.mod.util.StringUtil;
2016-11-10 21:07:15 +01:00
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
2016-11-12 12:26:36 +01:00
import net.minecraft.client.gui.GuiTextField;
2016-11-11 16:37:45 +01:00
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.util.ResourceLocation;
2016-11-12 12:26:36 +01:00
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.apache.commons.lang3.ArrayUtils;
import org.lwjgl.input.Keyboard;
2016-11-12 15:09:30 +01:00
import org.lwjgl.input.Mouse;
import java.io.IOException;
2016-11-12 15:09:30 +01:00
import java.util.Arrays;
import java.util.List;
@SideOnly(Side.CLIENT)
public abstract class GuiBooklet extends GuiBookletBase{
public static final int BUTTONS_PER_PAGE = 12;
public static final ResourceLocation RES_LOC_GUI = AssetUtil.getBookletGuiLocation("guiBooklet");
public static final ResourceLocation RES_LOC_GADGETS = AssetUtil.getBookletGuiLocation("guiBookletGadgets");
protected final BookmarkButton[] bookmarkButtons = new BookmarkButton[12];
public GuiScreen previousScreen;
2016-11-11 21:07:18 +01:00
public GuiBookletBase parentPage;
2016-11-12 12:26:36 +01:00
public GuiTextField searchField;
protected int xSize;
protected int ySize;
protected int guiLeft;
protected int guiTop;
private GuiButton buttonLeft;
private GuiButton buttonRight;
private GuiButton buttonBack;
2016-11-10 21:07:15 +01:00
public GuiBooklet(GuiScreen previousScreen, GuiBookletBase parentPage){
this.previousScreen = previousScreen;
this.parentPage = parentPage;
this.xSize = 281;
this.ySize = 180;
}
@Override
public void initGui(){
super.initGui();
this.guiLeft = (this.width-this.xSize)/2;
this.guiTop = (this.height-this.ySize)/2;
2016-11-11 18:55:32 +01:00
if(this.hasPageLeftButton()){
2016-11-12 15:09:30 +01:00
List<String> hoverText = Arrays.asList(TextFormatting.GOLD+"Previous Page", TextFormatting.ITALIC+"Or scroll up");
this.buttonLeft = new TexturedButton(RES_LOC_GADGETS, -2000, this.guiLeft-12, this.guiTop+this.ySize-8, 18, 54, 18, 10, hoverText);
2016-11-11 18:55:32 +01:00
this.buttonList.add(this.buttonLeft);
}
if(this.hasPageRightButton()){
2016-11-12 15:09:30 +01:00
List<String> hoverText = Arrays.asList(TextFormatting.GOLD+"Next Page", TextFormatting.ITALIC+"Or scroll down");
this.buttonRight = new TexturedButton(RES_LOC_GADGETS, -2001, this.guiLeft+this.xSize-6, this.guiTop+this.ySize-8, 0, 54, 18, 10, hoverText);
2016-11-11 18:55:32 +01:00
this.buttonList.add(this.buttonRight);
}
if(this.hasBackButton()){
2016-11-12 15:09:30 +01:00
List<String> hoverText = Arrays.asList(TextFormatting.GOLD+"Go Back", TextFormatting.ITALIC+"Or right-click", TextFormatting.ITALIC.toString()+TextFormatting.GRAY+"Hold Shift for Main Page");
this.buttonBack = new TexturedButton(RES_LOC_GADGETS, -2002, this.guiLeft-15, this.guiTop-3, 36, 54, 18, 10, hoverText);
2016-11-11 18:55:32 +01:00
this.buttonList.add(this.buttonBack);
}
2016-11-12 12:26:36 +01:00
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);
}
if(this.hasBookmarkButtons()){
PlayerSave data = PlayerData.getDataFromPlayer(this.mc.thePlayer);
2016-11-12 15:09:30 +01:00
int xStart = this.guiLeft+this.xSize/2-16*this.bookmarkButtons.length/2;
for(int i = 0; i < this.bookmarkButtons.length; i++){
2016-11-12 15:09:30 +01:00
this.bookmarkButtons[i] = new BookmarkButton(1337+i, xStart+i*16, this.guiTop+this.ySize, this);
this.buttonList.add(this.bookmarkButtons[i]);
if(data.bookmarks[i] != null){
this.bookmarkButtons[i].assignedPage = data.bookmarks[i];
}
}
}
}
@Override
public void onGuiClosed(){
super.onGuiClosed();
2016-11-15 19:28:51 +01:00
//Don't cache the parent GUI, otherwise it opens again when you close the cached book!
this.previousScreen = null;
PlayerSave data = PlayerData.getDataFromPlayer(this.mc.thePlayer);
2016-11-12 15:09:30 +01:00
boolean change = false;
for(int i = 0; i < this.bookmarkButtons.length; i++){
2016-11-12 15:09:30 +01:00
if(data.bookmarks[i] != this.bookmarkButtons[i].assignedPage){
data.bookmarks[i] = this.bookmarkButtons[i].assignedPage;
change = true;
}
}
data.lastOpenBooklet = this;
2016-11-12 15:09:30 +01:00
if(change){
PacketHandlerHelper.sendPlayerDataPacket(this.mc.thePlayer, true, false);
}
}
@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks){
this.drawScreenPre(mouseX, mouseY, partialTicks);
super.drawScreen(mouseX, mouseY, partialTicks);
this.drawScreenPost(mouseX, mouseY, partialTicks);
}
public void drawScreenPre(int mouseX, int mouseY, float partialTicks){
2016-11-11 16:37:45 +01:00
GlStateManager.color(1F, 1F, 1F);
this.mc.getTextureManager().bindTexture(RES_LOC_GUI);
drawModalRectWithCustomSizedTexture(this.guiLeft, this.guiTop, 0, 0, this.xSize, this.ySize, 512, 512);
2016-11-12 12:26:36 +01:00
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);
}
}
2016-11-12 12:26:36 +01:00
public void drawScreenPost(int mouseX, int mouseY, float partialTicks){
2016-11-12 15:09:30 +01:00
for(GuiButton button : this.buttonList){
if(button instanceof BookmarkButton){
((BookmarkButton)button).drawHover(mouseX, mouseY);
}
else if(button instanceof TexturedButton){
((TexturedButton)button).drawHover(mouseX, mouseY);
}
}
}
2016-11-12 12:26:36 +01:00
@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);
}
2016-11-12 15:09:30 +01:00
if(mouseButton == 1 && this.hasBackButton()){
this.onBackButtonPressed();
}
}
@Override
public void handleMouseInput() throws IOException{
int wheel = Mouse.getEventDWheel();
if(wheel != 0){
if(wheel < 0){
if(this.hasPageRightButton()){
this.onPageRightButtonPressed();
}
}
else if(wheel > 0){
if(this.hasPageLeftButton()){
this.onPageLeftButtonPressed();
}
}
}
super.handleMouseInput();
2016-11-12 12:26:36 +01:00
}
@Override
public void updateScreen(){
super.updateScreen();
if(this.hasSearchBar()){
this.searchField.updateCursorCounter();
}
}
@Override
public boolean doesGuiPauseGame(){
return false;
}
2016-11-11 18:55:32 +01:00
public boolean hasPageLeftButton(){
return false;
}
public void onPageLeftButtonPressed(){
}
public boolean hasPageRightButton(){
return false;
}
public void onPageRightButtonPressed(){
}
public boolean hasBackButton(){
return false;
}
public void onBackButtonPressed(){
2016-11-12 15:09:30 +01:00
this.mc.displayGuiScreen(new GuiMainPage(this.previousScreen));
2016-11-11 18:55:32 +01:00
}
2016-11-12 12:26:36 +01:00
public boolean hasSearchBar(){
return true;
}
public boolean hasBookmarkButtons(){
return true;
}
2016-11-12 12:26:36 +01:00
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));
}
2016-11-11 18:55:32 +01:00
@Override
protected void actionPerformed(GuiButton button) throws IOException{
if(this.hasPageLeftButton() && button == this.buttonLeft){
this.onPageLeftButtonPressed();
}
else if(this.hasPageRightButton() && button == this.buttonRight){
this.onPageRightButtonPressed();
}
else if(this.hasBackButton() && button == this.buttonBack){
this.onBackButtonPressed();
}
else if(this.hasBookmarkButtons() && button instanceof BookmarkButton){
int index = ArrayUtils.indexOf(this.bookmarkButtons, button);
if(index >= 0){
this.bookmarkButtons[index].onPressed();
}
}
2016-11-11 18:55:32 +01:00
else{
super.actionPerformed(button);
}
}
@Override
2016-11-12 12:26:36 +01:00
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()))){
2016-11-10 21:07:15 +01:00
this.mc.displayGuiScreen(this.previousScreen);
}
2016-11-12 12:26:36 +01:00
else if(this.hasSearchBar() & this.searchField.isFocused()){
2016-11-12 15:09:30 +01:00
String lastText = this.searchField.getText();
2016-11-12 12:26:36 +01:00
this.searchField.textboxKeyTyped(typedChar, key);
2016-11-12 15:09:30 +01:00
if(!lastText.equals(this.searchField.getText())){
this.onSearchBarChanged(this.searchField.getText());
}
2016-11-12 12:26:36 +01:00
}
else{
2016-11-12 12:26:36 +01:00
super.keyTyped(typedChar, key);
}
}
@Override
public void renderScaledAsciiString(String text, int x, int y, int color, boolean shadow, float scale){
StringUtil.renderScaledAsciiString(this.fontRendererObj, text, x, y, color, shadow, scale);
}
@Override
public void renderSplitScaledAsciiString(String text, int x, int y, int color, boolean shadow, float scale, int length){
StringUtil.renderSplitScaledAsciiString(this.fontRendererObj, text, x, y, color, shadow, scale, length);
}
2016-11-10 21:07:15 +01:00
@Override
public List<GuiButton> getButtonList(){
return this.buttonList;
}
2016-11-10 22:06:58 +01:00
@Override
public int getGuiLeft(){
return this.guiLeft;
}
@Override
public int getGuiTop(){
return this.guiTop;
}
@Override
public int getSizeX(){
return this.xSize;
}
@Override
public int getSizeY(){
return this.ySize;
}
}