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

366 lines
13 KiB
Java
Raw Normal View History

package de.ellpeck.actuallyadditions.common.booklet.gui;
2019-05-02 09:10:29 +02:00
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang3.ArrayUtils;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
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.common.ActuallyAdditions;
import de.ellpeck.actuallyadditions.common.booklet.button.BookmarkButton;
import de.ellpeck.actuallyadditions.common.booklet.button.TrialsButton;
import de.ellpeck.actuallyadditions.common.config.values.ConfigIntValues;
import de.ellpeck.actuallyadditions.common.data.PlayerData;
import de.ellpeck.actuallyadditions.common.data.PlayerData.PlayerSave;
import de.ellpeck.actuallyadditions.common.inventory.gui.TexturedButton;
import de.ellpeck.actuallyadditions.common.network.PacketHandlerHelper;
import de.ellpeck.actuallyadditions.common.util.AssetUtil;
import de.ellpeck.actuallyadditions.common.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;
@SideOnly(Side.CLIENT)
2019-05-02 09:10:29 +02:00
public abstract class GuiBooklet extends GuiBookletBase {
public static final int BUTTONS_PER_PAGE = 12;
2016-11-19 23:12:22 +01:00
public static final ResourceLocation RES_LOC_GUI = AssetUtil.getBookletGuiLocation("gui_booklet");
public static final ResourceLocation RES_LOC_GADGETS = AssetUtil.getBookletGuiLocation("gui_booklet_gadgets");
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;
2017-02-18 00:54:58 +01:00
private GuiButton buttonTrials;
private float smallFontSize;
private float mediumFontSize;
private float largeFontSize;
2019-05-02 09:10:29 +02:00
public GuiBooklet(GuiScreen previousScreen, GuiBookletBase parentPage) {
2016-11-10 21:07:15 +01:00
this.previousScreen = previousScreen;
this.parentPage = parentPage;
this.xSize = 281;
this.ySize = 180;
}
2019-05-02 09:10:29 +02:00
private static float getFontSize(String lang, ConfigIntValues config, float defaultValue) {
int conf = config.getValue();
2019-05-02 09:10:29 +02:00
if (conf <= 0) {
try {
return Float.parseFloat(StringUtil.localize("booklet." + ActuallyAdditions.MODID + ".fontSize." + lang));
} catch (Exception e) {
return defaultValue;
}
2019-05-02 09:10:29 +02:00
} else {
return conf / 100F;
}
}
@Override
2019-05-02 09:10:29 +02:00
public void initGui() {
super.initGui();
2019-05-02 09:10:29 +02:00
this.guiLeft = (this.width - this.xSize) / 2;
this.guiTop = (this.height - this.ySize) / 2;
2016-11-11 18:55:32 +01:00
this.smallFontSize = getFontSize("small", ConfigIntValues.FONT_SIZE_SMALL, 0.5F);
this.mediumFontSize = getFontSize("medium", ConfigIntValues.FONT_SIZE_MEDIUM, 0.75F);
this.largeFontSize = getFontSize("large", ConfigIntValues.FONT_SIZE_LARGE, 0.8F);
2019-05-02 09:10:29 +02:00
if (this.hasPageLeftButton()) {
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);
}
2019-05-02 09:10:29 +02:00
if (this.hasPageRightButton()) {
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);
}
2019-05-02 09:10:29 +02:00
if (this.hasBackButton()) {
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
2019-05-02 09:10:29 +02:00
if (this.hasSearchBar()) {
this.searchField = new GuiTextField(-420, this.fontRenderer, this.guiLeft + this.xSize + 2, this.guiTop + this.ySize - 40 + 2, 64, 12);
2016-11-12 12:26:36 +01:00
this.searchField.setMaxStringLength(50);
this.searchField.setEnableBackgroundDrawing(false);
}
2019-05-02 09:10:29 +02:00
if (this.hasBookmarkButtons()) {
2016-11-26 21:32:27 +01:00
PlayerSave data = PlayerData.getDataFromPlayer(this.mc.player);
2016-11-12 15:09:30 +01:00
2019-05-02 09:10:29 +02:00
int xStart = this.guiLeft + this.xSize / 2 - 16 * this.bookmarkButtons.length / 2;
for (int i = 0; i < this.bookmarkButtons.length; i++) {
this.bookmarkButtons[i] = new BookmarkButton(1337 + i, xStart + i * 16, this.guiTop + this.ySize, this);
this.buttonList.add(this.bookmarkButtons[i]);
2019-05-02 09:10:29 +02:00
if (data.bookmarks[i] != null) {
this.bookmarkButtons[i].assignedPage = data.bookmarks[i];
}
}
}
2017-02-18 00:54:58 +01:00
this.buttonTrials = new TrialsButton(this);
this.buttonList.add(this.buttonTrials);
}
@Override
2019-05-02 09:10:29 +02:00
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;
2019-05-02 09:10:29 +02:00
if (this.mc.player == null) return;
2016-11-26 21:32:27 +01:00
PlayerSave data = PlayerData.getDataFromPlayer(this.mc.player);
2017-02-18 00:54:58 +01:00
data.lastOpenBooklet = this;
2016-11-12 15:09:30 +01:00
boolean change = false;
2019-05-02 09:10:29 +02:00
for (int i = 0; i < this.bookmarkButtons.length; i++) {
if (data.bookmarks[i] != this.bookmarkButtons[i].assignedPage) {
2016-11-12 15:09:30 +01:00
data.bookmarks[i] = this.bookmarkButtons[i].assignedPage;
change = true;
}
}
2016-11-12 15:09:30 +01:00
2019-05-02 09:10:29 +02:00
if (change) {
PacketHandlerHelper.sendPlayerDataToServer(true, 0);
2016-11-12 15:09:30 +01:00
}
}
@Override
2019-05-02 09:10:29 +02:00
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
this.drawScreenPre(mouseX, mouseY, partialTicks);
super.drawScreen(mouseX, mouseY, partialTicks);
this.drawScreenPost(mouseX, mouseY, partialTicks);
}
2019-05-02 09:10:29 +02:00
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);
2019-05-02 09:10:29 +02:00
if (this.hasSearchBar()) {
2016-11-12 12:26:36 +01:00
this.mc.getTextureManager().bindTexture(RES_LOC_GADGETS);
2019-05-02 09:10:29 +02:00
this.drawTexturedModalRect(this.guiLeft + this.xSize, this.guiTop + this.ySize - 40, 188, 0, 68, 14);
2016-11-12 12:26:36 +01:00
boolean unicodeBefore = this.fontRenderer.getUnicodeFlag();
this.fontRenderer.setUnicodeFlag(true);
2016-11-12 12:26:36 +01:00
2019-05-02 09:10:29 +02:00
if (!this.searchField.isFocused() && (this.searchField.getText() == null || this.searchField.getText().isEmpty())) {
this.fontRenderer.drawString(TextFormatting.ITALIC + StringUtil.localize("info." + ActuallyAdditions.MODID + ".booklet.searchField"), this.guiLeft + this.xSize + 2, this.guiTop + this.ySize - 40 + 2, 0xFFFFFF, false);
2016-11-12 12:26:36 +01:00
}
this.searchField.drawTextBox();
this.fontRenderer.setUnicodeFlag(unicodeBefore);
2016-11-12 12:26:36 +01:00
}
}
2016-11-12 12:26:36 +01:00
2019-05-02 09:10:29 +02:00
public void drawScreenPost(int mouseX, int mouseY, float partialTicks) {
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
2019-05-02 09:10:29 +02:00
protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException {
2016-11-12 12:26:36 +01:00
super.mouseClicked(mouseX, mouseY, mouseButton);
2019-05-02 09:10:29 +02:00
if (this.hasSearchBar()) {
2016-11-12 12:26:36 +01:00
this.searchField.mouseClicked(mouseX, mouseY, mouseButton);
}
2016-11-12 15:09:30 +01:00
2019-05-02 09:10:29 +02:00
if (mouseButton == 1 && this.hasBackButton()) {
2016-11-12 15:09:30 +01:00
this.onBackButtonPressed();
}
}
@Override
2019-05-02 09:10:29 +02:00
public void handleMouseInput() throws IOException {
2016-11-12 15:09:30 +01:00
int wheel = Mouse.getEventDWheel();
2019-05-02 09:10:29 +02:00
if (wheel != 0) {
if (wheel < 0) {
if (this.hasPageRightButton()) {
2016-11-12 15:09:30 +01:00
this.onPageRightButtonPressed();
}
2019-05-02 09:10:29 +02:00
} else if (wheel > 0) {
if (this.hasPageLeftButton()) {
2016-11-12 15:09:30 +01:00
this.onPageLeftButtonPressed();
}
}
}
super.handleMouseInput();
2016-11-12 12:26:36 +01:00
}
@Override
2019-05-02 09:10:29 +02:00
public void updateScreen() {
2016-11-12 12:26:36 +01:00
super.updateScreen();
2019-05-02 09:10:29 +02:00
if (this.hasSearchBar()) {
2016-11-12 12:26:36 +01:00
this.searchField.updateCursorCounter();
}
}
@Override
2019-05-02 09:10:29 +02:00
public boolean doesGuiPauseGame() {
return false;
}
2019-05-02 09:10:29 +02:00
public boolean hasPageLeftButton() {
2016-11-11 18:55:32 +01:00
return false;
}
2019-05-02 09:10:29 +02:00
public void onPageLeftButtonPressed() {
2016-11-11 18:55:32 +01:00
}
2019-05-02 09:10:29 +02:00
public boolean hasPageRightButton() {
2016-11-11 18:55:32 +01:00
return false;
}
2019-05-02 09:10:29 +02:00
public void onPageRightButtonPressed() {
2016-11-11 18:55:32 +01:00
}
2019-05-02 09:10:29 +02:00
public boolean areTrialsOpened() {
2017-02-18 00:54:58 +01:00
return false;
}
2019-05-02 09:10:29 +02:00
public boolean hasBackButton() {
2016-11-11 18:55:32 +01:00
return false;
}
2019-05-02 09:10:29 +02:00
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
}
2019-05-02 09:10:29 +02:00
public boolean hasSearchBar() {
2016-11-12 12:26:36 +01:00
return true;
}
2019-05-02 09:10:29 +02:00
public boolean hasBookmarkButtons() {
return true;
}
@Override
2019-05-02 09:10:29 +02:00
public float getSmallFontSize() {
return this.smallFontSize;
}
@Override
2019-05-02 09:10:29 +02:00
public float getMediumFontSize() {
return this.mediumFontSize;
}
@Override
2019-05-02 09:10:29 +02:00
public float getLargeFontSize() {
return this.largeFontSize;
}
2019-05-02 09:10:29 +02:00
public void onSearchBarChanged(String searchBarText) {
2016-11-12 12:26:36 +01:00
GuiBookletBase parent = !(this instanceof GuiEntry) ? this : this.parentPage;
2017-02-18 00:54:58 +01:00
this.mc.displayGuiScreen(new GuiEntry(this.previousScreen, parent, ActuallyAdditionsAPI.entryAllAndSearch, 0, searchBarText, true));
2016-11-12 12:26:36 +01:00
}
2016-11-11 18:55:32 +01:00
@Override
2019-05-02 09:10:29 +02:00
protected void actionPerformed(GuiButton button) throws IOException {
if (this.hasPageLeftButton() && button == this.buttonLeft) {
2016-11-11 18:55:32 +01:00
this.onPageLeftButtonPressed();
2019-05-02 09:10:29 +02:00
} else if (this.hasPageRightButton() && button == this.buttonRight) {
2016-11-11 18:55:32 +01:00
this.onPageRightButtonPressed();
2019-05-02 09:10:29 +02:00
} else if (this.hasBackButton() && button == this.buttonBack) {
2016-11-11 18:55:32 +01:00
this.onBackButtonPressed();
}
2019-05-02 09:10:29 +02:00
if (button == this.buttonTrials) {
2017-02-18 00:54:58 +01:00
this.mc.displayGuiScreen(new GuiEntry(this.previousScreen, this, ActuallyAdditionsAPI.entryTrials, 0, "", false));
2019-05-02 09:10:29 +02:00
} else if (this.hasBookmarkButtons() && button instanceof BookmarkButton) {
int index = ArrayUtils.indexOf(this.bookmarkButtons, button);
2019-05-02 09:10:29 +02:00
if (index >= 0) {
this.bookmarkButtons[index].onPressed();
}
2019-05-02 09:10:29 +02:00
} else {
2016-11-11 18:55:32 +01:00
super.actionPerformed(button);
}
}
@Override
2019-05-02 09:10:29 +02: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);
2019-05-02 09:10:29 +02: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
2019-05-02 09:10:29 +02:00
if (!lastText.equals(this.searchField.getText())) {
2016-11-12 15:09:30 +01:00
this.onSearchBarChanged(this.searchField.getText());
}
2019-05-02 09:10:29 +02:00
} else {
2016-11-12 12:26:36 +01:00
super.keyTyped(typedChar, key);
}
}
@Override
2019-05-02 09:10:29 +02:00
public void renderScaledAsciiString(String text, int x, int y, int color, boolean shadow, float scale) {
StringUtil.renderScaledAsciiString(this.fontRenderer, text, x, y, color, shadow, scale);
}
@Override
2019-05-02 09:10:29 +02:00
public void renderSplitScaledAsciiString(String text, int x, int y, int color, boolean shadow, float scale, int length) {
StringUtil.renderSplitScaledAsciiString(this.fontRenderer, text, x, y, color, shadow, scale, length);
}
2016-11-10 21:07:15 +01:00
@Override
2019-05-02 09:10:29 +02:00
public List<GuiButton> getButtonList() {
2016-11-10 21:07:15 +01:00
return this.buttonList;
}
2016-11-10 22:06:58 +01:00
@Override
2019-05-02 09:10:29 +02:00
public int getGuiLeft() {
2016-11-10 22:06:58 +01:00
return this.guiLeft;
}
@Override
2019-05-02 09:10:29 +02:00
public int getGuiTop() {
2016-11-10 22:06:58 +01:00
return this.guiTop;
}
@Override
2019-05-02 09:10:29 +02:00
public int getSizeX() {
2016-11-10 22:06:58 +01:00
return this.xSize;
}
@Override
2019-05-02 09:10:29 +02:00
public int getSizeY() {
2016-11-10 22:06:58 +01:00
return this.ySize;
}
}