-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathCycleButtonWidget.java
More file actions
161 lines (143 loc) · 6.5 KB
/
CycleButtonWidget.java
File metadata and controls
161 lines (143 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package gregtech.api.gui.widgets;
import gregtech.api.gui.GuiTextures;
import gregtech.api.gui.IRenderContext;
import gregtech.api.gui.Widget;
import gregtech.api.gui.resources.SizedTextureArea;
import gregtech.api.gui.resources.TextureArea;
import gregtech.api.util.GTUtility;
import gregtech.api.util.LocalisationUtils;
import gregtech.api.util.Position;
import gregtech.api.util.Size;
import gregtech.api.util.function.BooleanConsumer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.IStringSerializable;
import net.minecraft.util.math.MathHelper;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import java.util.Arrays;
import java.util.List;
import java.util.function.*;
public class CycleButtonWidget extends Widget {
protected TextureArea buttonTexture = GuiTextures.VANILLA_BUTTON.getSubArea(0.0, 0.0, 1.0, 0.5);
private String[] optionNames;
private int textColor = 0xFFFFFF;
private IntSupplier currentOptionSupplier;
private IntConsumer setOptionExecutor;
private final int RIGHT_MOUSE = 1;
protected int currentOption;
protected String tooltipHoverString;
protected long hoverStartTime = -1L;
protected boolean isMouseHovered;
public CycleButtonWidget(int xPosition, int yPosition, int width, int height, String[] optionNames, IntSupplier currentOptionSupplier, IntConsumer setOptionExecutor) {
super(new Position(xPosition, yPosition), new Size(width, height));
this.optionNames = optionNames;
this.currentOptionSupplier = currentOptionSupplier;
this.setOptionExecutor = setOptionExecutor;
}
public <T extends Enum<T> & IStringSerializable> CycleButtonWidget(int xPosition, int yPosition, int width, int height, Class<T> enumClass, Supplier<T> supplier, Consumer<T> updater) {
super(new Position(xPosition, yPosition), new Size(width, height));
T[] enumConstantPool = enumClass.getEnumConstants();
this.optionNames = GTUtility.mapToString(enumConstantPool, it -> ((IStringSerializable) it).getName());
this.currentOptionSupplier = () -> supplier.get().ordinal();
this.setOptionExecutor = (newIndex) -> updater.accept(enumConstantPool[newIndex]);
}
public CycleButtonWidget(int xPosition, int yPosition, int width, int height, BooleanSupplier supplier, BooleanConsumer updater, String... optionNames) {
super(new Position(xPosition, yPosition), new Size(width, height));
this.optionNames = optionNames;
this.currentOptionSupplier = () -> supplier.getAsBoolean() ? 1 : 0;
this.setOptionExecutor = (value) -> updater.apply(value >= 1);
}
public CycleButtonWidget setTooltipHoverString(String hoverString) {
this.tooltipHoverString = hoverString;
return this;
}
public CycleButtonWidget setButtonTexture(TextureArea texture) {
this.buttonTexture = texture;
return this;
}
public CycleButtonWidget setTextColor(int textColor) {
this.textColor = textColor;
return this;
}
@Override
@SideOnly(Side.CLIENT)
public void drawInBackground(int mouseX, int mouseY, IRenderContext context) {
Position pos = getPosition();
Size size = getSize();
if (buttonTexture instanceof SizedTextureArea) {
((SizedTextureArea) buttonTexture).drawHorizontalCutSubArea(pos.x, pos.y, size.width, size.height, 0.0, 1.0);
} else {
buttonTexture.drawSubArea(pos.x, pos.y, size.width, size.height, 0.0, 0.0, 1.0, 1.0);
}
FontRenderer fontRenderer = Minecraft.getMinecraft().fontRenderer;
String text = LocalisationUtils.format(optionNames[currentOption]);
fontRenderer.drawString(text,
pos.x + size.width / 2 - fontRenderer.getStringWidth(text) / 2,
pos.y + size.height / 2 - fontRenderer.FONT_HEIGHT / 2 + 1, textColor);
GlStateManager.color(1.0f, 1.0f, 1.0f);
}
@Override
public void drawInForeground(int mouseX, int mouseY) {
boolean isHovered = isMouseOverElement(mouseX, mouseY);
boolean wasHovered = isMouseHovered;
if (isHovered && !wasHovered) {
this.isMouseHovered = true;
this.hoverStartTime = System.currentTimeMillis();
} else if (!isHovered && wasHovered) {
this.isMouseHovered = false;
this.hoverStartTime = 0L;
} else if (isHovered) {
long timeSinceHover = System.currentTimeMillis() - hoverStartTime;
if (timeSinceHover > 1000L && tooltipHoverString != null) {
List<String> hoverList = Arrays.asList(LocalisationUtils.format(tooltipHoverString).split("/n"));
drawHoveringText(ItemStack.EMPTY, hoverList, 300, mouseX, mouseY);
}
}
}
@Override
public void detectAndSendChanges() {
super.detectAndSendChanges();
if (currentOptionSupplier.getAsInt() != currentOption) {
this.currentOption = currentOptionSupplier.getAsInt();
writeUpdateInfo(1, buf -> buf.writeVarInt(currentOption));
}
}
@Override
public void readUpdateInfo(int id, PacketBuffer buffer) {
super.readUpdateInfo(id, buffer);
if (id == 1) {
this.currentOption = buffer.readVarInt();
}
}
@Override
@SideOnly(Side.CLIENT)
public boolean mouseClicked(int mouseX, int mouseY, int button) {
super.mouseClicked(mouseX, mouseY, button);
if (isMouseOverElement(mouseX, mouseY)) {
//Allow only the RMB to reverse cycle
if(button == RIGHT_MOUSE) {
//Wrap from the first option to the last if needed
this.currentOption = currentOption == 0 ? optionNames.length - 1 : currentOption - 1;
}
else {
this.currentOption = (currentOption + 1) % optionNames.length;
}
writeClientAction(1, buf -> buf.writeVarInt(currentOption));
playButtonClickSound();
return true;
}
return false;
}
@Override
public void handleClientAction(int id, PacketBuffer buffer) {
super.handleClientAction(id, buffer);
if (id == 1) {
this.currentOption = MathHelper.clamp(buffer.readVarInt(), 0, optionNames.length);
setOptionExecutor.accept(currentOption);
}
}
}