ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/inventory/gui/EnergyDisplay.java

118 lines
4.4 KiB
Java
Raw Normal View History

2016-07-21 01:49:01 +02:00
/*
* This file ("EnergyDisplay.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.inventory.gui;
import cofh.api.energy.EnergyStorage;
import de.ellpeck.actuallyadditions.mod.data.PlayerData;
2016-08-09 20:56:09 +02:00
import de.ellpeck.actuallyadditions.mod.network.PacketHandlerHelper;
2016-07-21 01:49:01 +02:00
import de.ellpeck.actuallyadditions.mod.util.AssetUtil;
2016-07-21 20:04:10 +02:00
import de.ellpeck.actuallyadditions.mod.util.ModUtil;
import de.ellpeck.actuallyadditions.mod.util.StringUtil;
2016-07-21 01:49:01 +02:00
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.text.TextFormatting;
2016-07-21 01:49:01 +02:00
import net.minecraftforge.fml.client.config.GuiUtils;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
2016-07-21 01:49:01 +02:00
public class EnergyDisplay extends Gui{
private EnergyStorage rfReference;
private int x;
private int y;
private boolean outline;
private boolean drawTextNextTo;
private boolean displayTesla;
2016-07-21 01:49:01 +02:00
public EnergyDisplay(int x, int y, EnergyStorage rfReference, boolean outline, boolean drawTextNextTo){
this.setData(x, y, rfReference, outline, drawTextNextTo);
}
public EnergyDisplay(int x, int y, EnergyStorage rfReference){
this(x, y, rfReference, false, false);
}
public void setData(int x, int y, EnergyStorage rfReference, boolean outline, boolean drawTextNextTo){
2016-07-21 01:49:01 +02:00
this.x = x;
this.y = y;
this.rfReference = rfReference;
this.outline = outline;
this.drawTextNextTo = drawTextNextTo;
this.displayTesla = PlayerData.getDataFromPlayer(Minecraft.getMinecraft().thePlayer).displayTesla;
2016-07-21 01:49:01 +02:00
}
public void draw(){
Minecraft mc = Minecraft.getMinecraft();
mc.getTextureManager().bindTexture(AssetUtil.GUI_INVENTORY_LOCATION);
int barX = this.x;
int barY = this.y;
int vOffset = this.displayTesla ? 85 : 0;
2016-07-21 01:49:01 +02:00
if(this.outline){
this.drawTexturedModalRect(this.x, this.y, 52, 163, 26, 93);
barX += 4;
barY += 4;
}
this.drawTexturedModalRect(barX, barY, 18, 171-vOffset, 18, 85);
2016-07-21 01:49:01 +02:00
if(this.rfReference.getEnergyStored() > 0){
int i = this.rfReference.getEnergyStored()*83/this.rfReference.getMaxEnergyStored();
this.drawTexturedModalRect(barX+1, barY+84-i, 36, 172-vOffset, 16, i);
2016-07-21 01:49:01 +02:00
}
if(this.drawTextNextTo){
this.drawString(mc.fontRendererObj, this.getOverlayText(), barX+25, barY+78, StringUtil.DECIMAL_COLOR_WHITE);
}
2016-07-21 01:49:01 +02:00
}
public void drawOverlay(int mouseX, int mouseY){
if(this.isMouseOver(mouseX, mouseY)){
2016-07-21 01:49:01 +02:00
Minecraft mc = Minecraft.getMinecraft();
List<String> text = new ArrayList<String>();
text.add(this.getOverlayText());
text.add("");
2016-07-21 20:04:10 +02:00
text.add(TextFormatting.GRAY+""+TextFormatting.ITALIC+StringUtil.localize("info."+ModUtil.MOD_ID+".energy.to"+(this.displayTesla ? "RF" : "T")));
text.add(TextFormatting.DARK_GRAY+""+TextFormatting.ITALIC+StringUtil.localize("info."+ModUtil.MOD_ID+".energy.disclaimer"));
GuiUtils.drawHoveringText(text, mouseX, mouseY, mc.displayWidth, mc.displayHeight, -1, mc.fontRendererObj);
2016-07-21 01:49:01 +02:00
}
}
public void onMouseClick(int mouseX, int mouseY, int mouseButton){
if(mouseButton == 0 && this.isMouseOver(mouseX, mouseY)){
this.changeDisplayMode();
}
}
private boolean isMouseOver(int mouseX, int mouseY){
return mouseX >= this.x && mouseY >= this.y && mouseX < this.x+(this.outline ? 26 : 18) && mouseY < this.y+(this.outline ? 93 : 85);
}
private String getOverlayText(){
NumberFormat format = NumberFormat.getInstance();
return format.format(this.rfReference.getEnergyStored())+"/"+format.format(this.rfReference.getMaxEnergyStored())+(this.displayTesla ? " T" : " RF");
}
private void changeDisplayMode(){
2016-07-21 20:04:10 +02:00
this.displayTesla = !this.displayTesla;
EntityPlayer player = Minecraft.getMinecraft().thePlayer;
PlayerData.getDataFromPlayer(player).displayTesla = this.displayTesla;
PacketHandlerHelper.sendPlayerDataPacket(player, true, false);
}
2016-07-21 01:49:01 +02:00
}