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

370 lines
13 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
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
*/
package de.ellpeck.actuallyadditions.mod.booklet.gui;
2021-04-19 21:20:23 +02:00
import com.mojang.blaze3d.matrix.MatrixStack;
2021-02-27 22:59:18 +01:00
import com.mojang.blaze3d.platform.GlStateManager;
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;
2018-05-10 11:38:58 +02:00
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
import de.ellpeck.actuallyadditions.mod.booklet.button.BookmarkButton;
2017-02-18 00:54:58 +01:00
import de.ellpeck.actuallyadditions.mod.booklet.button.TrialsButton;
import de.ellpeck.actuallyadditions.mod.config.values.ConfigIntValues;
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;
2021-04-19 21:20:23 +02:00
import net.minecraft.client.gui.IGuiEventListener;
2021-02-27 22:59:18 +01:00
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.client.gui.widget.button.Button;
import net.minecraft.util.ResourceLocation;
2021-04-19 21:20:23 +02:00
import net.minecraft.util.text.StringTextComponent;
2016-11-12 12:26:36 +01:00
import net.minecraft.util.text.TextFormatting;
2021-02-27 22:59:18 +01:00
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import java.util.Arrays;
import java.util.List;
2021-04-19 21:20:23 +02:00
import java.util.stream.Collectors;
2021-02-26 22:15:48 +01:00
@OnlyIn(Dist.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];
2021-02-27 22:59:18 +01:00
public Screen previousScreen;
2016-11-11 21:07:18 +01:00
public GuiBookletBase parentPage;
2021-02-27 22:59:18 +01:00
public TextFieldWidget searchField;
protected int xSize;
protected int ySize;
protected int guiLeft;
protected int guiTop;
2021-02-27 22:59:18 +01:00
private Button buttonLeft;
private Button buttonRight;
private Button buttonBack;
2021-02-27 22:59:18 +01:00
private Button buttonTrials;
2017-02-18 00:54:58 +01:00
private float smallFontSize;
private float mediumFontSize;
private float largeFontSize;
2021-02-27 22:59:18 +01:00
public GuiBooklet(Screen previousScreen, GuiBookletBase parentPage) {
2021-04-19 21:20:23 +02:00
super(StringTextComponent.EMPTY);
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
2021-02-27 22:59:18 +01:00
public void init() {
super.init();
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");
2021-04-19 21:20:23 +02:00
this.buttonLeft = new TexturedButton(RES_LOC_GADGETS, this.guiLeft - 12, this.guiTop + this.ySize - 8, 18, 54, 18, 10, hoverText, btn -> this.onPageLeftButtonPressed());
2021-02-27 22:59:18 +01:00
this.addButton(this.buttonLeft);
2016-11-11 18:55:32 +01:00
}
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");
2021-04-19 21:20:23 +02:00
this.buttonRight = new TexturedButton(RES_LOC_GADGETS, this.guiLeft + this.xSize - 6, this.guiTop + this.ySize - 8, 0, 54, 18, 10, hoverText, btn -> this.onPageRightButtonPressed());
2021-02-27 22:59:18 +01:00
this.addButton(this.buttonRight);
2016-11-11 18:55:32 +01:00
}
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");
2021-04-19 21:20:23 +02:00
this.buttonBack = new TexturedButton(RES_LOC_GADGETS, this.guiLeft - 15, this.guiTop - 3, 36, 54, 18, 10, hoverText, btn -> this.onBackButtonPressed());
2021-02-27 22:59:18 +01:00
this.addButton(this.buttonBack);
2016-11-11 18:55:32 +01:00
}
2016-11-12 12:26:36 +01:00
2019-05-02 09:10:29 +02:00
if (this.hasSearchBar()) {
2021-04-19 21:20:23 +02:00
this.searchField = new TextFieldWidget(this.font, this.guiLeft + this.xSize + 2, this.guiTop + this.ySize - 40 + 2, 64, 12, StringTextComponent.EMPTY);
this.searchField.setMaxLength(50);
this.searchField.setBordered(false);
2021-04-19 21:20:23 +02:00
this.children.add(this.searchField);
2016-11-12 12:26:36 +01:00
}
2019-05-02 09:10:29 +02:00
if (this.hasBookmarkButtons()) {
2021-04-19 21:20:23 +02:00
PlayerSave data = PlayerData.getDataFromPlayer(this.getMinecraft().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++) {
2021-04-19 21:20:23 +02:00
this.bookmarkButtons[i] = new BookmarkButton(xStart + i * 16, this.guiTop + this.ySize, this);
this.addButton(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, btn -> this.getMinecraft().setScreen(new GuiEntry(this.previousScreen, this, ActuallyAdditionsAPI.entryTrials, 0, "", false)));
this.addButton(this.buttonTrials);
}
@Override
public void removed() {
super.removed();
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;
2021-04-19 21:20:23 +02:00
if (this.getMinecraft().player == null) {
2021-02-27 22:59:18 +01:00
return;
}
2021-04-19 21:20:23 +02:00
PlayerSave data = PlayerData.getDataFromPlayer(this.getMinecraft().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
2021-04-19 21:20:23 +02:00
public void render(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
this.drawScreenPre(matrixStack, mouseX, mouseY, partialTicks);
super.render(matrixStack, mouseX, mouseY, partialTicks);
this.drawScreenPost(matrixStack, mouseX, mouseY, partialTicks);
}
2021-04-19 21:20:23 +02:00
public void drawScreenPre(MatrixStack matrices, int mouseX, int mouseY, float partialTicks) {
GlStateManager._color4f(1F, 1F, 1F, 1F);
this.getMinecraft().getTextureManager().bind(RES_LOC_GUI);
2021-04-19 21:20:23 +02:00
blit(matrices, this.guiLeft, this.guiTop, 0, 0, this.xSize, this.ySize, 512, 512);
2019-05-02 09:10:29 +02:00
if (this.hasSearchBar()) {
this.getMinecraft().getTextureManager().bind(RES_LOC_GADGETS);
this.blit(matrices, this.guiLeft + this.xSize, this.guiTop + this.ySize - 40, 188, 0, 68, 14);
2016-11-12 12:26:36 +01:00
2021-04-19 21:20:23 +02:00
// boolean unicodeBefore = this.font.getUnicodeFlag();
// this.font.setUnicodeFlag(true);
2016-11-12 12:26:36 +01:00
if (!this.searchField.isFocused() && (this.searchField.getValue() == null || this.searchField.getValue().isEmpty())) {
this.font.draw(matrices, TextFormatting.ITALIC + StringUtil.localize("info." + ActuallyAdditions.MODID + ".booklet.searchField"), this.guiLeft + this.xSize + 2, this.guiTop + this.ySize - 40 + 2, 0xFFFFFF);
2016-11-12 12:26:36 +01:00
}
2021-04-19 21:20:23 +02:00
this.searchField.render(matrices, mouseX, mouseY, partialTicks);
2016-11-12 12:26:36 +01:00
2021-04-19 21:20:23 +02:00
// this.font.setUnicodeFlag(unicodeBefore);
2016-11-12 12:26:36 +01:00
}
}
2016-11-12 12:26:36 +01:00
2021-04-19 21:20:23 +02:00
public void drawScreenPost(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
for (IGuiEventListener button : this.children) {
2019-05-02 09:10:29 +02:00
if (button instanceof BookmarkButton) {
2021-04-19 21:20:23 +02:00
((BookmarkButton) button).drawHover(matrixStack, mouseX, mouseY);
2019-05-02 09:10:29 +02:00
} else if (button instanceof TexturedButton) {
2021-04-19 21:20:23 +02:00
((TexturedButton) button).drawHover(matrixStack, mouseX, mouseY);
}
}
}
2016-11-12 12:26:36 +01:00
@Override
2021-04-19 21:20:23 +02:00
public boolean mouseClicked(double mouseX, double mouseY, int button) {
return super.mouseClicked(mouseX, mouseY, button);
2016-11-12 15:09:30 +01:00
}
2021-04-19 21:20:23 +02:00
// TODO: Ensure replacement works
2016-11-12 15:09:30 +01:00
@Override
2021-04-19 21:20:23 +02:00
public boolean mouseScrolled(double mouseX, double mouseY, double delta) {
if (delta < 0) {
if (this.hasPageRightButton()) {
this.onPageRightButtonPressed();
}
} else if (delta > 0) {
if (this.hasPageLeftButton()) {
this.onPageLeftButtonPressed();
2016-11-12 15:09:30 +01:00
}
}
2021-04-19 21:20:23 +02:00
return super.mouseScrolled(mouseX, mouseY, delta);
}
// @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
2021-04-19 21:20:23 +02:00
@Override
public void tick() {
2019-05-02 09:10:29 +02:00
if (this.hasSearchBar()) {
2021-04-19 21:20:23 +02:00
this.searchField.tick();
2016-11-12 12:26:36 +01:00
}
}
@Override
2021-04-19 21:20:23 +02:00
public boolean isPauseScreen() {
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() {
this.getMinecraft().setScreen(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;
}
2021-04-19 21:20:23 +02:00
// TODO: Check if not being used
2019-05-02 09:10:29 +02:00
public void onSearchBarChanged(String searchBarText) {
2021-02-27 22:59:18 +01:00
GuiBookletBase parent = !(this instanceof GuiEntry)
? this
: this.parentPage;
this.getMinecraft().setScreen(new GuiEntry(this.previousScreen, parent, ActuallyAdditionsAPI.entryAllAndSearch, 0, searchBarText, true));
2021-04-19 21:20:23 +02:00
}
// TODO: ensure typing still works
// @Override
// 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()) {
// String lastText = this.searchField.getText();
//
// this.searchField.textboxKeyTyped(typedChar, key);
//
// if (!lastText.equals(this.searchField.getText())) {
// this.onSearchBarChanged(this.searchField.getText());
// }
// } else {
// 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) {
2021-04-19 21:20:23 +02:00
StringUtil.renderScaledAsciiString(this.font, 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) {
2021-04-19 21:20:23 +02:00
StringUtil.renderSplitScaledAsciiString(this.font, text, x, y, color, shadow, scale, length);
}
2016-11-10 21:07:15 +01:00
@Override
2021-04-19 21:20:23 +02:00
public List<IGuiEventListener> getButtonList() {
return this.children.stream().filter(e -> e instanceof Button).collect(Collectors.toList());
2016-11-10 21:07:15 +01:00
}
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;
}
}