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

106 lines
3.8 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
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
2016-07-21 01:49:01 +02:00
*/
package de.ellpeck.actuallyadditions.mod.inventory.gui;
2021-03-01 20:55:50 +01:00
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import de.ellpeck.actuallyadditions.mod.tile.CustomEnergyStorage;
2016-07-21 01:49:01 +02:00
import de.ellpeck.actuallyadditions.mod.util.AssetUtil;
import de.ellpeck.actuallyadditions.mod.util.StringUtil;
2016-07-21 01:49:01 +02:00
import net.minecraft.client.Minecraft;
2021-03-01 20:55:50 +01:00
import net.minecraft.client.gui.AbstractGui;
import net.minecraft.client.resources.I18n;
2021-03-01 20:55:50 +01:00
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fml.client.gui.GuiUtils;
2021-02-26 22:15:48 +01:00
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
2016-07-21 01:49:01 +02:00
2021-02-26 22:15:48 +01:00
@OnlyIn(Dist.CLIENT)
2021-03-01 20:55:50 +01:00
public class EnergyDisplay extends AbstractGui {
2016-07-21 01:49:01 +02:00
private CustomEnergyStorage rfReference;
private int x;
private int y;
private boolean outline;
private boolean drawTextNextTo;
2016-07-21 01:49:01 +02:00
2019-05-02 09:10:29 +02:00
public EnergyDisplay(int x, int y, CustomEnergyStorage rfReference, boolean outline, boolean drawTextNextTo) {
this.setData(x, y, rfReference, outline, drawTextNextTo);
}
2019-05-02 09:10:29 +02:00
public EnergyDisplay(int x, int y, CustomEnergyStorage rfReference) {
this(x, y, rfReference, false, false);
}
2019-05-02 09:10:29 +02:00
public void setData(int x, int y, CustomEnergyStorage 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;
2016-07-21 01:49:01 +02:00
}
2021-03-01 20:55:50 +01:00
public void draw(MatrixStack matrices) {
2021-02-26 22:15:48 +01:00
Minecraft mc = Minecraft.getInstance();
2016-07-21 01:49:01 +02:00
mc.getTextureManager().bindTexture(AssetUtil.GUI_INVENTORY_LOCATION);
int barX = this.x;
int barY = this.y;
2019-05-02 09:10:29 +02:00
if (this.outline) {
this.blit(matrices, this.x, this.y, 52, 163, 26, 93);
2016-07-21 01:49:01 +02:00
barX += 4;
barY += 4;
}
this.blit(matrices, barX, barY, 18, 171, 18, 85);
2016-07-21 01:49:01 +02:00
2019-05-02 09:10:29 +02:00
if (this.rfReference.getEnergyStored() > 0) {
int i = this.rfReference.getEnergyStored() * 83 / this.rfReference.getMaxEnergyStored();
2016-11-26 15:09:15 +01:00
2019-05-02 09:10:29 +02:00
float[] color = AssetUtil.getWheelColor(mc.world.getTotalWorldTime() % 256);
2021-03-01 20:55:50 +01:00
RenderSystem.color4f(color[0] / 255F, color[1] / 255F, color[2] / 255F);
this.blit(matrices, barX + 1, barY + 84 - i, 36, 172, 16, i);
2021-03-01 20:55:50 +01:00
RenderSystem.color4f(1F, 1F, 1F);
2016-07-21 01:49:01 +02:00
}
2019-05-02 09:10:29 +02:00
if (this.drawTextNextTo) {
this.drawString(mc.fontRenderer, this.getOverlayText(), barX + 25, barY + 78, StringUtil.DECIMAL_COLOR_WHITE);
}
2016-07-21 01:49:01 +02:00
}
2021-03-01 20:55:50 +01:00
public void render(MatrixStack matrices, int mouseX, int mouseY) {
2019-05-02 09:10:29 +02:00
if (this.isMouseOver(mouseX, mouseY)) {
2021-02-26 22:15:48 +01:00
Minecraft mc = Minecraft.getInstance();
2019-02-27 19:53:05 +01:00
List<String> text = new ArrayList<>();
text.add(this.getOverlayText());
GuiUtils.drawHoveringText(text, mouseX, mouseY, mc.displayWidth, mc.displayHeight, -1, mc.fontRenderer);
2016-07-21 01:49:01 +02:00
}
}
2019-05-02 09:10:29 +02:00
private boolean isMouseOver(int mouseX, int mouseY) {
2021-02-26 22:15:48 +01:00
return mouseX >= this.x && mouseY >= this.y && mouseX < this.x + (this.outline
? 26
: 18) && mouseY < this.y + (this.outline
? 93
: 85);
}
2019-05-02 09:10:29 +02:00
private String getOverlayText() {
NumberFormat format = NumberFormat.getInstance();
return String.format("%s/%s %s", format.format(this.rfReference.getEnergyStored()), format.format(this.rfReference.getMaxEnergyStored()), I18n.format("actuallyadditions.cflong"));
}
2016-07-21 01:49:01 +02:00
}