-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathSliderWidget.java
More file actions
176 lines (149 loc) · 5.97 KB
/
SliderWidget.java
File metadata and controls
176 lines (149 loc) · 5.97 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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.TextureArea;
import gregtech.api.util.LocalisationUtils;
import gregtech.api.util.Position;
import gregtech.api.util.Size;
import gregtech.api.util.function.FloatConsumer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.math.MathHelper;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.function.BiFunction;
public class SliderWidget extends Widget {
public static final BiFunction<String, Float, String> DEFAULT_TEXT_SUPPLIER = (name, value) -> LocalisationUtils.format(name, value.intValue());
private int sliderWidth = 8;
private TextureArea backgroundArea = GuiTextures.SLIDER_BACKGROUND;
private TextureArea sliderIcon = GuiTextures.SLIDER_ICON;
private BiFunction<String, Float, String> textSupplier = DEFAULT_TEXT_SUPPLIER;
private int textColor = 0xFFFFFF;
private final float min;
private final float max;
private final String name;
private final FloatConsumer responder;
private boolean isPositionSent;
private String displayString;
private float sliderPosition;
public boolean isMouseDown;
public SliderWidget(String name, int xPosition, int yPosition, int width, int height, float min, float max, float currentValue, FloatConsumer responder) {
super(new Position(xPosition, yPosition), new Size(width, height));
Preconditions.checkNotNull(responder, "responder");
Preconditions.checkNotNull(name, "name");
this.min = min;
this.max = max;
this.name = name;
this.responder = responder;
this.sliderPosition = (currentValue - min) / (max - min);
}
public SliderWidget setSliderIcon(@Nonnull TextureArea sliderIcon) {
Preconditions.checkNotNull(sliderIcon, "sliderIcon");
this.sliderIcon = sliderIcon;
return this;
}
public SliderWidget setBackground(@Nullable TextureArea background) {
this.backgroundArea = background;
return this;
}
public SliderWidget setSliderWidth(int sliderWidth) {
this.sliderWidth = sliderWidth;
return this;
}
public SliderWidget setTextColor(int textColor) {
this.textColor = textColor;
return this;
}
@Override
public void detectAndSendChanges() {
if (!isPositionSent) {
writeUpdateInfo(1, buffer -> buffer.writeFloat(sliderPosition));
this.isPositionSent = true;
}
}
public float getSliderValue() {
return this.min + (this.max - this.min) * this.sliderPosition;
}
protected String getDisplayString() {
return textSupplier.apply(name, getSliderValue());
}
@Override
public void drawInBackground(int mouseX, int mouseY, IRenderContext context) {
Position pos = getPosition();
Size size = getSize();
if (backgroundArea != null) {
backgroundArea.draw(pos.x, pos.y, size.width, size.height);
}
if(displayString == null) {
this.displayString = getDisplayString();
}
sliderIcon.draw(pos.x + (int) (this.sliderPosition * (float) (size.width - 8)), pos.y, sliderWidth, size.height);
FontRenderer fontRenderer = Minecraft.getMinecraft().fontRenderer;
fontRenderer.drawString(displayString,
pos.x + size.width / 2 - fontRenderer.getStringWidth(displayString) / 2,
pos.y + size.height / 2 - fontRenderer.FONT_HEIGHT / 2, textColor);
GlStateManager.color(1.0f, 1.0f, 1.0f);
}
@Override
public boolean mouseDragged(int mouseX, int mouseY, int button, long timeDragged) {
if (this.isMouseDown) {
Position pos = getPosition();
Size size = getSize();
this.sliderPosition = (float) (mouseX - (pos.x + 4)) / (float) (size.width - 8);
if (this.sliderPosition < 0.0F) {
this.sliderPosition = 0.0F;
}
if (this.sliderPosition > 1.0F) {
this.sliderPosition = 1.0F;
}
this.displayString = this.getDisplayString();
writeClientAction(1, buffer -> buffer.writeFloat(sliderPosition));
return true;
}
return false;
}
@Override
public boolean mouseClicked(int mouseX, int mouseY, int button) {
if (isMouseOverElement(mouseX, mouseY)) {
Position pos = getPosition();
Size size = getSize();
this.sliderPosition = (float) (mouseX - (pos.x + 4)) / (float) (size.width - 8);
if (this.sliderPosition < 0.0F) {
this.sliderPosition = 0.0F;
}
if (this.sliderPosition > 1.0F) {
this.sliderPosition = 1.0F;
}
this.displayString = this.getDisplayString();
writeClientAction(1, buffer -> buffer.writeFloat(sliderPosition));
this.isMouseDown = true;
return true;
}
return false;
}
@Override
public boolean mouseReleased(int mouseX, int mouseY, int button) {
this.isMouseDown = false;
return false;
}
@Override
public void handleClientAction(int id, PacketBuffer buffer) {
if (id == 1) {
this.sliderPosition = buffer.readFloat();
this.sliderPosition = MathHelper.clamp(sliderPosition, 0.0f, 1.0f);
this.responder.apply(getSliderValue());
}
}
@Override
public void readUpdateInfo(int id, PacketBuffer buffer) {
if (id == 1) {
this.sliderPosition = buffer.readFloat();
this.sliderPosition = MathHelper.clamp(sliderPosition, 0.0f, 1.0f);
this.displayString = getDisplayString();
}
}
}