-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathToggleButtonWidget.java
More file actions
117 lines (101 loc) · 4.29 KB
/
ToggleButtonWidget.java
File metadata and controls
117 lines (101 loc) · 4.29 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
package gregtech.api.gui.widgets;
import com.google.common.base.Preconditions;
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.LocalisationUtils;
import gregtech.api.util.Position;
import gregtech.api.util.Size;
import gregtech.api.util.function.BooleanConsumer;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import java.util.Arrays;
import java.util.List;
import java.util.function.BooleanSupplier;
public class ToggleButtonWidget extends Widget {
protected TextureArea buttonTexture;
private BooleanSupplier isPressedCondition;
private BooleanConsumer setPressedExecutor;
private String tooltipText;
protected boolean isPressed;
public ToggleButtonWidget(int xPosition, int yPosition, int width, int height, BooleanSupplier isPressedCondition, BooleanConsumer setPressedExecutor) {
this(xPosition, yPosition, width, height, GuiTextures.VANILLA_BUTTON, isPressedCondition, setPressedExecutor);
}
public ToggleButtonWidget(int xPosition, int yPosition, int width, int height, TextureArea buttonTexture,
BooleanSupplier isPressedCondition, BooleanConsumer setPressedExecutor) {
super(new Position(xPosition, yPosition), new Size(width, height));
Preconditions.checkNotNull(buttonTexture, "texture");
this.buttonTexture = buttonTexture;
this.isPressedCondition = isPressedCondition;
this.setPressedExecutor = setPressedExecutor;
}
public ToggleButtonWidget setButtonTexture(TextureArea texture) {
Preconditions.checkNotNull(texture, "texture");
this.buttonTexture = texture;
return this;
}
public ToggleButtonWidget setTooltipText(String tooltipText) {
Preconditions.checkNotNull(tooltipText, "tooltipText");
this.tooltipText = tooltipText;
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, isPressed ? 0.5 : 0.0, 0.5);
} else {
buttonTexture.drawSubArea(pos.x, pos.y, size.width, size.height, 0.0, isPressed ? 0.5 : 0.0, 1.0, 0.5);
}
}
@Override
public void drawInForeground(int mouseX, int mouseY) {
if(isMouseOverElement(mouseX, mouseY) && tooltipText != null) {
String postfix = isPressed ? ".enabled" : ".disabled";
String tooltipHoverString = tooltipText + postfix;
List<String> hoverList = Arrays.asList(LocalisationUtils.format(tooltipHoverString).split("/n"));
drawHoveringText(ItemStack.EMPTY, hoverList, 300, mouseX, mouseY);
}
}
@Override
public void detectAndSendChanges() {
super.detectAndSendChanges();
if (isPressedCondition.getAsBoolean() != isPressed) {
this.isPressed = isPressedCondition.getAsBoolean();
writeUpdateInfo(1, buf -> buf.writeBoolean(isPressed));
}
}
@Override
public void readUpdateInfo(int id, PacketBuffer buffer) {
super.readUpdateInfo(id, buffer);
if (id == 1) {
this.isPressed = buffer.readBoolean();
}
}
@Override
@SideOnly(Side.CLIENT)
public boolean mouseClicked(int mouseX, int mouseY, int button) {
super.mouseClicked(mouseX, mouseY, button);
if (isMouseOverElement(mouseX, mouseY)) {
this.isPressed = !this.isPressed;
writeClientAction(1, buf -> buf.writeBoolean(isPressed));
playButtonClickSound();
return true;
}
return false;
}
@Override
public void handleClientAction(int id, PacketBuffer buffer) {
super.handleClientAction(id, buffer);
if (id == 1) {
this.isPressed = buffer.readBoolean();
setPressedExecutor.apply(isPressed);
}
}
}