Files
TinkersConstruct/src/main/java/tconstruct/tools/gui/BattlesignGui.java
T
2023-09-24 22:57:55 +02:00

170 lines
5.3 KiB
Java

package tconstruct.tools.gui;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ChatAllowedCharacters;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.GL11;
import tconstruct.TConstruct;
import tconstruct.library.TConstructRegistry;
import tconstruct.tools.logic.BattlesignLogic;
import tconstruct.util.network.SignDataPacket;
public class BattlesignGui extends GuiScreen {
private final BattlesignLogic battlesign;
private float bgColR = 1F;
private float bgColG = 1F;
private float bgColB = 1F;
private static final ResourceLocation background = new ResourceLocation("tinker:textures/gui/battlesignText.png");
private final String[] text = { "", "", "", "", "" };
int currentLine = 0;
public BattlesignGui(BattlesignLogic logic) {
this.battlesign = logic;
ItemStack stack = logic.getEquipmentItem();
if (stack != null) {
NBTTagCompound tag = stack.getTagCompound().getCompoundTag("InfiTool");
if (tag != null) {
int head = tag.getInteger("Head");
int bgCol = TConstructRegistry.getMaterial(head).primaryColor();
bgColR = (float) (bgCol >> 16 & 255) / 255.0F;
bgColG = (float) (bgCol >> 8 & 255) / 255.0F;
bgColB = (float) (bgCol & 255) / 255.0F;
}
}
}
@Override
public void initGui() {
buttonList.clear();
super.initGui();
Keyboard.enableRepeatEvents(true);
int k = (this.width - 100) / 2;
int l = (this.height - 110) / 2;
buttonList.add(new GuiButton(0, k, l + 100, 100, 20, "Done"));
}
@Override
public void drawScreen(int mouseX, int mouseY, float something) {
Tessellator t = Tessellator.instance;
GL11.glColor4f(bgColR, bgColG, bgColB, 1F);
this.mc.getTextureManager().bindTexture(background);
int k = (this.width - 100) / 2;
int l = (this.height - 103) / 2;
this.drawTexturedModalRect(k, l, 0, 0, 100, 103);
super.drawScreen(mouseX, mouseY, something);
GL11.glPushMatrix();
float lum = calcLuminance(bgColR, bgColG, bgColB);
for (int i = 0; i < text.length; i++) {
final EnumChatFormatting chatFormatting = lum >= 35F ? EnumChatFormatting.BLACK
: lum >= 31F ? EnumChatFormatting.GRAY : EnumChatFormatting.WHITE;
fontRendererObj.drawString(
chatFormatting + (i == currentLine ? "> " : "")
+ text[i]
+ (i == currentLine ? " " + EnumChatFormatting.RESET + chatFormatting + "<" : ""),
k - fontRendererObj.getStringWidth(
(i == currentLine ? "> " : "") + text[i] + (i == currentLine ? " <" : "")) / 2 + 51,
l + 4 + 10 * i,
0);
}
GL11.glPopMatrix();
}
@Override
protected void keyTyped(char c, int i) {
super.keyTyped(c, i);
if (fontRendererObj.getStringWidth(text[currentLine]) < 90 && ChatAllowedCharacters.isAllowedCharacter(c)) {
if (Keyboard.isKeyDown(56) && c == 'f'
&& (text[currentLine].length() == 0
|| text[currentLine].charAt(text[currentLine].length() - 1) != '\u00A7')) {
text[currentLine] += "\u00A7";
} else {
text[currentLine] += c;
}
} else {
switch (i) {
case 14:
if (text[currentLine].length() > 0) {
text[currentLine] = text[currentLine].substring(0, text[currentLine].length() - 1);
}
break;
case 28:
case 208:
moveLine(1);
break;
case 200:
moveLine(-1);
break;
}
}
}
private void moveLine(int i) {
if (i < 0) {
if (currentLine <= 0) {
currentLine = text.length - 1;
} else {
currentLine--;
}
} else {
if (currentLine >= text.length - 1) {
currentLine = 0;
} else {
currentLine++;
}
}
}
@Override
public void onGuiClosed() {
super.onGuiClosed();
Keyboard.enableRepeatEvents(false);
TConstruct.packetPipeline.sendToServer(
new SignDataPacket(
battlesign.getWorldObj().provider.dimensionId,
battlesign.xCoord,
battlesign.yCoord,
battlesign.zCoord,
text));
}
private float calcLuminance(float r, float g, float b) {
return (r * 255 * 0.299f + g * 255 * 0.587f + b * 255 * 0.114f) / 3;
}
@Override
protected void actionPerformed(GuiButton button) {
if (button.id == 0) {
this.mc.displayGuiScreen(null);
this.mc.setIngameFocus();
}
}
}