/
ArrowWidget.java
214 lines (171 loc) · 7.42 KB
/
ArrowWidget.java
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package slimeknights.tmechworks.client.gui.components;
import com.google.common.collect.ImmutableList;
import com.mojang.blaze3d.platform.GlStateManager;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.widget.Widget;
import net.minecraft.client.resources.I18n;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.client.config.GuiUtils;
import javax.vecmath.Vector2f;
import java.util.Arrays;
import java.util.function.Consumer;
public class ArrowWidget extends Widget {
public static final ResourceLocation ARROW_WIDGET = new ResourceLocation("tmechworks", "textures/gui/arrows.png");
public static final String[] LABELS_DEFAULT = {
"tmechworks:widget.arrow.down",
"tmechworks:widget.arrow.up",
"tmechworks:widget.arrow.front",
"tmechworks:widget.arrow.back",
"tmechworks:widget.arrow.left",
"tmechworks:widget.arrow.right",
"tmechworks:widget.arrow.high",
"tmechworks:widget.arrow.middle",
"tmechworks:widget.arrow.low"
};
private static final int ARROW_SIZE = 20;
private static final int ARROW_SMALL_SIZE = 6;
private static final int ARROW_ROWS = 2;
private String[] labels;
private ArrowState[] states = new ArrowState[Arrow.values().length];
private Arrow hoveredArrow;
private int screenW, screenH;
private IArrowPressed onClick;
public ArrowWidget(int x, int y, int screenW, int screenH, IArrowPressed onClick) {
this(x, y, screenW, screenH, false, onClick);
}
public ArrowWidget(int x, int y, int screenW, int screenH, boolean drawAdditionalArrows, IArrowPressed onClick) {
super(x, y, "");
setLabels(LABELS_DEFAULT);
Arrays.fill(states, ArrowState.ENABLED);
if (!drawAdditionalArrows) {
for (int i = Arrow.MID_UP.ordinal(); i < Arrow.values().length; i++) {
states[i] = ArrowState.NO_DRAW;
}
}
this.screenW = screenW;
this.screenH = screenH;
this.onClick = onClick;
}
public ArrowWidget setLabels(String[] labels) {
if (labels.length < Arrow.values().length) {
labels = new String[Arrow.values().length];
Arrays.fill(this.labels, "");
for (int i = 0; i < this.labels.length; i++) {
if (i < labels.length)
this.labels[i] = labels[i];
}
} else {
this.labels = labels;
}
return this;
}
public ArrowState getState(Arrow arrow) {
return states[arrow.ordinal()];
}
public ArrowWidget setState(Arrow arrow, ArrowState state) {
states[arrow.ordinal()] = state;
return this;
}
public Arrow getHoveredArrow() {
return hoveredArrow;
}
@Override
public void renderButton(int mouseX, int mouseY, float partialTicks) {
Minecraft mc = Minecraft.getInstance();
Arrow.SMALL_DOWN.position.y = ARROW_SIZE + 8;
GlStateManager.pushMatrix();
GlStateManager.translatef(x, y, 0F);
GlStateManager.color4f(1.0F, 1.0F, 1.0F, 1.0F);
mc.getTextureManager().bindTexture(ARROW_WIDGET);
hoveredArrow = null;
boolean canHover = true;
for (int i = 0; i < Arrow.values().length; i++) {
Arrow arrow = Arrow.values()[i];
ArrowState state = states[i];
if (state == ArrowState.NO_DRAW)
continue;
if (state != ArrowState.DISABLED && state != ArrowState.SELECTED) {
if (canHover && hoveredArrow == null && arrow.isHovered(x, y, mouseX, mouseY)) {
states[i] = ArrowState.HOVER;
hoveredArrow = arrow;
canHover = false;
} else {
states[i] = ArrowState.ENABLED;
}
} else if (arrow.isHovered(x, y, mouseX, mouseY)) {
canHover = false;
}
}
for (int i = 0; i < Arrow.values().length; i++) {
Arrow arrow = Arrow.values()[i];
ArrowState state = states[i];
if (state == ArrowState.NO_DRAW)
continue;
int indexX = arrow.indexX * arrow.w + arrow.subX * arrow.subW;
int indexY = (arrow.indexY + state.ordinal() * ARROW_ROWS) * arrow.h + arrow.subY * arrow.subH;
blit((int) arrow.position.getX(), (int) arrow.position.getY(), indexX, indexY, arrow.subW, arrow.subH);
}
GlStateManager.popMatrix();
if (hoveredArrow == null)
return;
if (labels != null && states[hoveredArrow.ordinal()] == ArrowState.HOVER && !labels[hoveredArrow.ordinal()].trim().isEmpty()) {
GuiUtils.drawHoveringText(ImmutableList.of(I18n.format(labels[hoveredArrow.ordinal()])), mouseX, mouseY, screenW, screenH, 100, Minecraft.getInstance().fontRenderer);
}
}
public boolean mouseClicked(double p_mouseClicked_1_, double p_mouseClicked_3_, int p_mouseClicked_5_) {
if (hoveredArrow != null) {
this.playDownSound(Minecraft.getInstance().getSoundHandler());
onClick(p_mouseClicked_1_, p_mouseClicked_3_);
return true;
}
return false;
}
@Override
public void onClick(double p_onClick_1_, double p_onClick_3_) {
super.onClick(p_onClick_1_, p_onClick_3_);
onClick.onPress(this, hoveredArrow);
}
public enum Arrow {
MID_DOWN(2, 0, 0, 0, new Vector2f(24 - ARROW_SIZE / 2 - 1, 48 - ARROW_SIZE + 1)),
MID_UP(2, 0, 0, 0, new Vector2f(24 - ARROW_SIZE / 2 - 1, ARROW_SIZE - ARROW_SMALL_SIZE - 1)),
UP(0, 0, new Vector2f(24 - ARROW_SIZE / 2, 0)),
DOWN(1, 0, new Vector2f(24 - ARROW_SIZE / 2, 48 - ARROW_SIZE)),
LEFT(0, 1, new Vector2f(0, 24 - ARROW_SIZE / 2)),
RIGHT(1, 1, new Vector2f(48 - ARROW_SIZE, 24 - ARROW_SIZE / 2)),
SMALL_UP(2, 0, 1, 0, new Vector2f(50, ARROW_SIZE - 5)),
SMALL_MID(2, 0, 0, 1, new Vector2f(50, ARROW_SIZE + 1)),
SMALL_DOWN(2, 0, 1, 1, new Vector2f(50, 48 - ARROW_SIZE + 1));
public final int indexX, indexY, w, h, subX, subY, subW, subH;
public final Vector2f position;
Arrow(int indexX, int indexY, Vector2f pos) {
this(indexX, indexY, ARROW_SIZE, ARROW_SIZE, 0, 0, ARROW_SIZE, ARROW_SIZE, pos);
}
Arrow(int indexX, int indexY, int subX, int subY, Vector2f pos) {
this(indexX, indexY, ARROW_SIZE, ARROW_SIZE, subX, subY, ARROW_SMALL_SIZE, ARROW_SMALL_SIZE, pos);
}
Arrow(int indexX, int indexY, int w, int h, int subX, int subY, int subW, int subH, Vector2f pos) {
this.indexX = indexX;
this.indexY = indexY;
this.w = w;
this.h = h;
this.subX = subX;
this.subY = subY;
this.subW = subW;
this.subH = subH;
this.position = pos;
}
public boolean isHovered(int xPosition, int yPosition, int mouseX, int mouseY) {
return mouseX > xPosition + position.getX() && mouseX < xPosition + position.getX() + subW && mouseY > yPosition + position.getY() && mouseY < yPosition + position.getY() + subH;
}
}
public enum ArrowState {
ENABLED,
DISABLED,
HOVER,
SELECTED,
NO_DRAW
}
public interface IArrowPressed {
void onPress(ArrowWidget widget, Arrow arrow);
}
}