|
| 1 | +package gregtech.api.gui.widgets; |
| 2 | + |
| 3 | +import com.sun.jna.platform.win32.Guid; |
| 4 | +import gregtech.api.gui.GuiTextures; |
| 5 | +import gregtech.api.gui.IRenderContext; |
| 6 | +import gregtech.api.gui.Widget; |
| 7 | +import gregtech.api.gui.resources.TextureArea; |
| 8 | +import gregtech.api.util.GTUtility; |
| 9 | +import gregtech.api.util.Position; |
| 10 | +import gregtech.api.util.Size; |
| 11 | +import net.minecraft.client.resources.I18n; |
| 12 | +import net.minecraft.item.ItemStack; |
| 13 | +import net.minecraft.network.PacketBuffer; |
| 14 | +import net.minecraft.util.IStringSerializable; |
| 15 | +import net.minecraftforge.fml.relauncher.Side; |
| 16 | +import net.minecraftforge.fml.relauncher.SideOnly; |
| 17 | + |
| 18 | +import java.util.Collections; |
| 19 | +import java.util.function.IntSupplier; |
| 20 | +import java.util.function.Supplier; |
| 21 | + |
| 22 | + |
| 23 | +public class DiagnoseWidget extends Widget { |
| 24 | + |
| 25 | + private final String[] issueNames; |
| 26 | + private IntSupplier currentIssue; |
| 27 | + protected String tooltipHoverString; |
| 28 | + protected long hoverStartTime = -1L; |
| 29 | + protected boolean isMouseHovered; |
| 30 | + protected int currentError; |
| 31 | + |
| 32 | + protected TextureArea area; |
| 33 | + private boolean isVisible = true; |
| 34 | + |
| 35 | + public <T extends Enum<T> & IStringSerializable> DiagnoseWidget(int xPosition, int yPosition, int width, int height, Class<T> enumClass, Supplier<T> supplier) { |
| 36 | + super(new Position(xPosition, yPosition), new Size(width, height)); |
| 37 | + T[] enumConstantPool = enumClass.getEnumConstants(); |
| 38 | + this.issueNames = GTUtility.mapToString(enumConstantPool, it -> ((IStringSerializable) it).getName()); |
| 39 | + this.currentIssue = () -> supplier.get().ordinal(); |
| 40 | + setImage(); |
| 41 | + } |
| 42 | + |
| 43 | + public void setTooltipHoverString() { |
| 44 | + this.tooltipHoverString = I18n.format(issueNames[currentError]); |
| 45 | + } |
| 46 | + |
| 47 | + public DiagnoseWidget setImage() { |
| 48 | + if (currentError == 0) { |
| 49 | + this.area = GuiTextures.DIAGNOSE_IDLING; |
| 50 | + } |
| 51 | + if (currentError == 1) { |
| 52 | + this.area = GuiTextures.DIAGNOSE_WORKING; |
| 53 | + } |
| 54 | + else if (currentError > 1) { |
| 55 | + this.area = GuiTextures.DIAGNOSE_ISSUE; |
| 56 | + } |
| 57 | + return this; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void detectAndSendChanges() { |
| 62 | + super.detectAndSendChanges(); |
| 63 | + if (currentIssue.getAsInt() != currentError) { |
| 64 | + this.currentError = currentIssue.getAsInt(); |
| 65 | + writeUpdateInfo(1, buf -> buf.writeVarInt(currentError)); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void readUpdateInfo(int id, PacketBuffer buffer) { |
| 71 | + super.readUpdateInfo(id, buffer); |
| 72 | + if (id == 1) { |
| 73 | + this.currentError = buffer.readVarInt(); |
| 74 | + setImage(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + @SideOnly(Side.CLIENT) |
| 80 | + public void drawInForeground(int mouseX, int mouseY) { |
| 81 | + boolean isHovered = isMouseOverElement(mouseX, mouseY); |
| 82 | + boolean wasHovered = isMouseHovered; |
| 83 | + if (isHovered && !wasHovered) { |
| 84 | + this.isMouseHovered = true; |
| 85 | + this.hoverStartTime = System.currentTimeMillis(); |
| 86 | + } else if (!isHovered && wasHovered) { |
| 87 | + this.isMouseHovered = false; |
| 88 | + this.hoverStartTime = 0L; |
| 89 | + } else if (isHovered) { |
| 90 | + long timeSinceHover = System.currentTimeMillis() - hoverStartTime; |
| 91 | + setTooltipHoverString(); |
| 92 | + if (timeSinceHover > 1000L && tooltipHoverString != null) { |
| 93 | + drawHoveringText(ItemStack.EMPTY, Collections.singletonList(tooltipHoverString), 300, mouseX, mouseY); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + @SideOnly(Side.CLIENT) |
| 100 | + public void drawInBackground(int mouseX, int mouseY, IRenderContext context) { |
| 101 | + if (!this.isVisible || area == null) return; |
| 102 | + Position position = getPosition(); |
| 103 | + Size size = getSize(); |
| 104 | + area.draw(position.x, position.y, size.width, size.height); |
| 105 | + } |
| 106 | +} |
0 commit comments