-
-
Notifications
You must be signed in to change notification settings - Fork 509
/
GuiRadialSelector.java
371 lines (338 loc) · 16.3 KB
/
GuiRadialSelector.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package mekanism.client.gui;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.Tesselator;
import com.mojang.blaze3d.vertex.VertexFormat.Mode;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import java.util.function.Supplier;
import mekanism.api.math.MathUtils;
import mekanism.api.radial.RadialData;
import mekanism.api.radial.mode.INestedRadialMode;
import mekanism.api.radial.mode.IRadialMode;
import mekanism.api.text.EnumColor;
import mekanism.client.render.MekanismRenderer;
import mekanism.client.render.lib.ScrollIncrementer;
import mekanism.common.Mekanism;
import mekanism.common.MekanismLang;
import mekanism.common.config.MekanismConfig;
import mekanism.common.lib.radial.IGenericRadialModeItem;
import mekanism.common.network.to_server.PacketRadialModeChange;
import mekanism.common.util.MekanismUtils;
import mekanism.common.util.MekanismUtils.ResourceType;
import mekanism.common.util.StatUtils;
import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.joml.Matrix4f;
//TODO: Do we Automatically want to go through nested things if there is only one option and potentially not even allow opening it?
// For now no as it might be confusing to people what the menu is relating to especially on the Meka-Tool but it is worth thinking more about
public class GuiRadialSelector extends Screen {
private static final float DRAWS = 300;
private static final float INNER = 40, OUTER = 100;
private static final float MIDDLE_DISTANCE = (INNER + OUTER) / 2F;
private static final float SELECT_RADIUS = 10, SELECT_RADIUS_WITH_PARENT = 20;
private final ScrollIncrementer scrollIncrementer = new ScrollIncrementer(true);
private final Deque<RadialData<?>> parents = new ArrayDeque<>();
private final Supplier<Player> playerSupplier;
private final EquipmentSlot slot;
@NotNull
private RadialData<?> radialData;
private IRadialMode selection = null;
private boolean overBackButton = false;
private boolean updateOnClose = true;
public GuiRadialSelector(EquipmentSlot slot, @NotNull RadialData<?> radialData, Supplier<Player> playerSupplier) {
super(MekanismLang.RADIAL_SCREEN.translate());
this.slot = slot;
this.radialData = radialData;
this.playerSupplier = playerSupplier;
}
@Override
public void render(@NotNull GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
// center of screen
float centerX = guiGraphics.guiWidth() / 2F;
float centerY = guiGraphics.guiHeight() / 2F;
render(guiGraphics, mouseX, mouseY, centerX, centerY, radialData);
}
private <MODE extends IRadialMode> void render(@NotNull GuiGraphics guiGraphics, int mouseX, int mouseY, float centerX, float centerY, RadialData<MODE> radialData) {
// Calculate number of available modes to switch between
List<MODE> modes = radialData.getModes();
int activeModes = modes.size();
if (activeModes == 0) {
//If for some reason none are available try going up a level first, and if that fails close the screen
RadialData<?> parent = parents.pollLast();
if (parent == null) {
onClose();
} else {
this.radialData = parent;
}
return;
}
float angleSize = 360F / activeModes;
PoseStack pose = guiGraphics.pose();
pose.pushPose();
pose.translate(centerX, centerY, 0);
RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();
// draw base
// Note: While there might be slightly better performance only drawing part of the Torus given
// other bits may be drawn by hovering or current selection, it is not practical to do so due
// to floating point precision causing some values to have gaps in the torus, and also the light
// colors occasionally being harder to see without the added back layer torus
guiGraphics.setColor(0.3F, 0.3F, 0.3F, 0.5F);
drawTorus(guiGraphics, 0, 360);
MODE current = getCurrent(radialData);
if (current == null) {
//See if the radial data has a default fallback to go to
current = radialData.getDefaultMode(modes);
}
// Draw segments
//Calculate the proper section to highlight as green based on the radial data in case indexing can be optimized or
// some pieces are actually disabled
int section = radialData.indexNullable(modes, current);
if (current != null && section != -1) {
// draw current selected if any is selected
EnumColor color = current.color();
if (color == null) {
guiGraphics.setColor(0.4F, 0.4F, 0.4F, 0.7F);
} else {
MekanismRenderer.color(guiGraphics, color, 0.3F);
}
float startAngle = -90F + 360F * (-0.5F + section) / activeModes;
drawTorus(guiGraphics, startAngle, angleSize);
}
// Draw current hovered selection and selection highlighter
double xDiff = mouseX - centerX;
double yDiff = mouseY - centerY;
double distanceFromCenter = Mth.length(xDiff, yDiff);
if (distanceFromCenter > (parents.isEmpty() ? SELECT_RADIUS : SELECT_RADIUS_WITH_PARENT)) {
// draw mouse selection highlight
float angle = (float) (Mth.RAD_TO_DEG * Mth.atan2(yDiff, xDiff));
float modeSize = 180F / activeModes;
guiGraphics.setColor(0.8F, 0.8F, 0.8F, 0.3F);
drawTorus(guiGraphics, angle - modeSize, angleSize);
float selectionAngle = StatUtils.wrapDegrees(angle + modeSize + 90F);
int selectionDrawnPos = (int) (selectionAngle * (activeModes / 360F));
selection = modes.get(selectionDrawnPos);
// draw hovered selection
guiGraphics.setColor(0.6F, 0.6F, 0.6F, 0.7F);
drawTorus(guiGraphics, -90F + 360F * (-0.5F + selectionDrawnPos) / activeModes, angleSize);
} else {
selection = null;
}
record PositionedText(float x, float y, Component text) {}
List<PositionedText> textToDraw = new ArrayList<>(parents.isEmpty() ? activeModes : activeModes + 1);
//Draw back button if needed
if (!parents.isEmpty()) {
overBackButton = distanceFromCenter <= SELECT_RADIUS_WITH_PARENT;
if (overBackButton) {
guiGraphics.setColor(0.8F, 0.8F, 0.8F, 0.3F);
} else {
guiGraphics.setColor(0.3F, 0.3F, 0.3F, 0.5F);
}
drawTorus(guiGraphics, 0, 360, 0, SELECT_RADIUS_WITH_PARENT);
MekanismRenderer.resetColor(guiGraphics);
// draw icon
guiGraphics.blit(MekanismUtils.getResource(ResourceType.GUI_RADIAL, "back.png"), -12, -18, 24, 24, 0, 0, 18, 18, 18, 18);
textToDraw.add(new PositionedText(0, 0, MekanismLang.BACK.translate()));
} else {
overBackButton = false;
}
MekanismRenderer.resetColor(guiGraphics);
// Icons
int position = 0;
for (MODE mode : modes) {
float degrees = 270 + 360 * ((float) position++ / activeModes);
float angle = Mth.DEG_TO_RAD * degrees;
float x = Mth.cos(angle) * MIDDLE_DISTANCE;
float y = Mth.sin(angle) * MIDDLE_DISTANCE;
// draw icon
guiGraphics.blit(mode.icon(), Math.round(x - 12), Math.round(y - 20), 24, 24, 0, 0, 18, 18, 18, 18);
// queue label
textToDraw.add(new PositionedText(x, y, mode.sliceName()));
}
// Labels (has to be separate from icons or the icons occasionally will get extra artifacts for some reason)
boolean whiteRadialText = MekanismConfig.client.whiteRadialText.get();
for (PositionedText toDraw : textToDraw) {
pose.pushPose();
pose.translate(toDraw.x, toDraw.y, 0);
pose.scale(0.6F, 0.6F, 0.6F);
Component text = toDraw.text;
if (whiteRadialText) {
text = text.copy().withStyle(ChatFormatting.RESET);
}
GuiUtils.drawString(guiGraphics, font, text, -font.width(text) / 2F, 8, 0xCCFFFFFF, true);
pose.popPose();
}
MekanismRenderer.resetColor(guiGraphics);
pose.popPose();
}
@Override
public void removed() {
if (updateOnClose) {
updateSelection(radialData);
}
super.removed();
}
@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
// handle & ignore all key events
return true;
}
@Override
public boolean mouseScrolled(double mouseX, double mouseY, double delta) {
return delta != 0 && mouseScrolled(radialData, scrollIncrementer.scroll(delta)) || super.mouseScrolled(mouseX, mouseY, delta);
}
private <MODE extends IRadialMode> boolean mouseScrolled(RadialData<MODE> radialData, int shift) {
if (shift == 0) {
return true;
}
List<MODE> modes = radialData.getModes();
if (!modes.isEmpty()) {
//Should always be true but handle just in case
MODE current = getCurrent(radialData);
int index = radialData.indexNullable(modes, current);
if (index != -1) {//Skip if we couldn't find a selected one (such as for the nested radials)
selection = MathUtils.getByIndexMod(modes, index + shift);
updateSelection(radialData);
return true;
}
}
return false;
}
@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
updateSelection(radialData);
return true;
}
@Override
public boolean isPauseScreen() {
return false;
}
private void drawTorus(GuiGraphics guiGraphics, float startAngle, float sizeAngle) {
drawTorus(guiGraphics, startAngle, sizeAngle, INNER, OUTER);
}
private void drawTorus(GuiGraphics guiGraphics, float startAngle, float sizeAngle, float inner, float outer) {
RenderSystem.setShader(GameRenderer::getPositionShader);
Tesselator tesselator = Tesselator.getInstance();
BufferBuilder vertexBuffer = tesselator.getBuilder();
vertexBuffer.begin(Mode.TRIANGLE_STRIP, DefaultVertexFormat.POSITION);
Matrix4f matrix4f = guiGraphics.pose().last().pose();
float draws = DRAWS * (sizeAngle / 360F);
for (int i = 0; i <= draws; i++) {
float degrees = startAngle + (i / DRAWS) * 360;
float angle = Mth.DEG_TO_RAD * degrees;
float cos = Mth.cos(angle);
float sin = Mth.sin(angle);
vertexBuffer.vertex(matrix4f, outer * cos, outer * sin, 0).endVertex();
vertexBuffer.vertex(matrix4f, inner * cos, inner * sin, 0).endVertex();
}
tesselator.end();
}
@Nullable
private <MODE extends IRadialMode> MODE getCurrent(RadialData<MODE> radialData) {
Player player = playerSupplier.get();
if (player != null) {
ItemStack stack = player.getItemBySlot(slot);
if (stack.getItem() instanceof IGenericRadialModeItem item) {
return item.getMode(stack, radialData);
}
}
return null;
}
private <MODE extends IRadialMode> void updateSelection(final RadialData<MODE> radialData) {
//Only update it if we have a selection, and it is different from the current value
if (selection != null && playerSupplier.get() != null) {
if (selection instanceof INestedRadialMode nested && nested.hasNestedData()) {
parents.push(radialData);
//noinspection ConstantConditions: not null, validated by hasNestedData
this.radialData = nested.nestedData();
//Reset immediately rather than next render frame
selection = null;
} else if (!selection.equals(getCurrent(radialData))) {
//Only bother syncing if we don't already have that mode selected
List<ResourceLocation> path = new ArrayList<>(parents.size());
RadialData<?> previousParent = null;
for (RadialData<?> parent : parents) {
if (previousParent != null) {
path.add(parent.getIdentifier());
}
previousParent = parent;
}
if (previousParent != null) {
path.add(radialData.getIdentifier());
}
int networkRepresentation = radialData.tryGetNetworkRepresentation(selection);
if (networkRepresentation != -1) {
//TODO: If we ever add a radial type where the network representation may be negative,
// re-evaluate how we do this type validation
Mekanism.packetHandler().sendToServer(new PacketRadialModeChange(slot, path, networkRepresentation));
}
}
} else if (overBackButton) {
//Reset immediately rather than next render frame
overBackButton = false;
RadialData<?> parent = parents.pollLast();
if (parent != null) {
this.radialData = parent;
}
}
}
public boolean hasMatchingData(EquipmentSlot slot, RadialData<?> data) {
if (this.slot == slot) {
RadialData<?> firstData = parents.peekFirst();
//If there is an initial root parent compare it, otherwise we are currently at the root so compare to our current one
return firstData == null ? radialData.equals(data) : firstData.equals(data);
}
return false;
}
@SuppressWarnings("ConstantConditions")//not null, validated by hasNestedData
public void tryInheritCurrentPath(@Nullable Screen screen) {
if (screen instanceof GuiRadialSelector old) {
//Try to calculate the expected sub radial if we are going from radial selector to radial selector
// as if they both have the same path odds are only some minor option changed, so we might as well
// go to the same sub one
RadialData<?> previousParent = null;
for (RadialData<?> parent : old.parents) {
if (previousParent != null && radialData.getIdentifier().equals(previousParent.getIdentifier())) {
INestedRadialMode nestedMode = radialData.fromIdentifier(parent.getIdentifier());
if (nestedMode == null || !nestedMode.hasNestedData()) {
//Can't go any deeper end
return;
}
//Update the current radial depth
parents.push(radialData);
radialData = nestedMode.nestedData();
}
previousParent = parent;
}
if (previousParent != null && radialData.getIdentifier().equals(previousParent.getIdentifier())) {
INestedRadialMode nestedMode = radialData.fromIdentifier(old.radialData.getIdentifier());
if (nestedMode != null && nestedMode.hasNestedData()) {
//Update the current radial depth
parents.push(radialData);
radialData = nestedMode.nestedData();
// and mark the previous one to not change values when closing
// (as it isn't "fully" closing, just changing views)
old.updateOnClose = false;
}
}
}
}
public boolean shouldHideCrosshair() {
return !parents.isEmpty();
}
}