-
-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathRadialMenuScreen.java
More file actions
669 lines (559 loc) · 23.2 KB
/
RadialMenuScreen.java
File metadata and controls
669 lines (559 loc) · 23.2 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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
package dev.isxander.controlify.gui.screen;
import dev.isxander.controlify.Controlify;
import dev.isxander.controlify.api.bind.RadialIcon;
import dev.isxander.controlify.bindings.ControlifyBindings;
import dev.isxander.controlify.api.bind.InputBinding;
import dev.isxander.controlify.controller.ControllerEntity;
import dev.isxander.controlify.controller.haptic.HapticEffects;
//import dev.isxander.controlify.gui.layout.AnchorPoint;
//import dev.isxander.controlify.gui.layout.PositionedComponent;
import dev.isxander.controlify.screenop.ComponentProcessor;
import dev.isxander.controlify.screenop.ScreenControllerEventListener;
import dev.isxander.controlify.screenop.ScreenProcessor;
import dev.isxander.controlify.screenop.ScreenProcessorProvider;
import dev.isxander.controlify.sound.ControlifyClientSounds;
import dev.isxander.controlify.utils.CUtil;
import dev.isxander.controlify.utils.animation.api.Animation;
import dev.isxander.controlify.utils.animation.api.EasingFunction;
import dev.isxander.controlify.utils.render.Blit;
import dev.isxander.controlify.utils.render.CGuiPose;
import dev.isxander.controlify.virtualmouse.VirtualMouseBehaviour;
import net.minecraft.client.gui.ComponentPath;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.MultiLineLabel;
import net.minecraft.client.gui.components.Renderable;
import net.minecraft.client.gui.components.events.ContainerEventHandler;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.gui.narration.NarratableEntry;
import net.minecraft.client.gui.narration.NarratedElementType;
import net.minecraft.client.gui.narration.NarrationElementOutput;
import net.minecraft.client.gui.navigation.FocusNavigationEvent;
import net.minecraft.client.gui.navigation.ScreenRectangle;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.resources.sounds.SimpleSoundInstance;
//import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.util.Mth;
import org.jetbrains.annotations.Nullable;
//? if neoforge {
/*import net.minecraft.client.KeyMapping;
import org.lwjgl.glfw.GLFW;
import net.neoforged.neoforge.client.event.InputEvent;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.bus.api.EventPriority;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.api.distmarker.Dist;
//? if >= 1.21.9 {
//import net.minecraft.client.input.KeyEvent;
//?}
//? if < 1.21.4 {
//import dev.isxander.controlify.mixins.feature.bind.KeyMappingAccessor;
//?}
*///?}
import java.util.ArrayList;
import java.util.List;
//import java.util.Optional;
//? if neoforge {
/*//? if < 1.21.8 {
@EventBusSubscriber(modid = "controlify", bus = EventBusSubscriber.Bus.GAME, value = Dist.CLIENT)
//?} else {
//@EventBusSubscriber(modid = "controlify", value = Dist.CLIENT)
//?}
*///?}
public class RadialMenuScreen extends Screen implements ScreenControllerEventListener, ScreenProcessorProvider {
public static final ResourceLocation EMPTY_ACTION = CUtil.rl("empty_action");
private final ControllerEntity controller;
private final @Nullable EditMode editMode;
private final Screen parent;
private final Component text;
private final RadialItem[] items;
private final RadialButton[] buttons;
private float radialRadius;
private final InputBinding openBind;
private int selectedButton = -1;
private int idleTicks;
private final int idleTicksTimeout;
private boolean isEditing;
private ActionSelectList actionSelectList;
private final Processor processor = new Processor(this);
public RadialMenuScreen(ControllerEntity controller, InputBinding openBind, RadialItem[] items, Component text, @Nullable EditMode editMode, Screen parent) {
super(text);
this.text = text;
this.controller = controller;
this.items = items;
this.buttons = new RadialButton[items.length];
this.editMode = editMode;
this.parent = parent;
this.idleTicksTimeout = controller.input().orElseThrow().confObj().radialButtonFocusTimeoutTicks;
this.openBind = openBind;
}
@Override
protected void init() {
int centerX = this.width / 2;
int centerY = this.height / 2;
// get diameter of enclosing circle of a radial button
// w = 16, h = 16
// r = sqrt(w^2 + h^2) / 2
float buttonRadius = (float) Math.sqrt(32 * 32 + 32 * 32) + 8;
// add the amount of radii together to create the circumference of the circle they all fit around
float circumference = buttonRadius * items.length;
// c = 2 * pi * r
radialRadius = Math.max(circumference / Mth.TWO_PI, 43);
Animation animation = Animation.of(5)
.easing(EasingFunction.EASE_OUT_QUAD);
for (int i = 0; i < items.length; i++) {
float angle = Mth.TWO_PI * i / items.length - (90 * Mth.DEG_TO_RAD);
float x = centerX + Mth.cos(angle) * radialRadius;
float y = centerY + Mth.sin(angle) * radialRadius;
RadialButton button = buttons[i] = new RadialButton(items[i], centerX - 16, centerY - 16);
animation
.consumerF(button::setX, centerX - 16, x - 16)
.consumerF(button::setY, centerY - 16, y - 16);
addRenderableWidget(button);
}
animation.play();
// if (editMode != null) {
// var exitGuide = addRenderableWidget(new PositionedComponent<>(
// new GuideActionRenderer(
// new GuideAction(
// ControlifyBindings.GUI_BACK.on(controller),
// obj -> Optional.of(CommonComponents.GUI_DONE)
// ),
// false,
// true
// ),
// AnchorPoint.BOTTOM_CENTER,
// 0, -10,
// AnchorPoint.BOTTOM_CENTER
// ));
//
// exitGuide.getComponent().updateName(null);
// exitGuide.updatePosition(width, height);
// }
}
//? if neoforge {
/*private static KeyMapping keyToPress = null;
private static boolean setKeysDownWithRadialEvent = true;
@SubscribeEvent(priority = EventPriority.HIGHEST)
public static void onKeyInputBefore(InputEvent.Key event) {
if (keyToPress != null) {
switch (event.getAction()) {
case GLFW.GLFW_PRESS:
if (setKeysDownWithRadialEvent)
keyToPress.setDown(true);
break;
case GLFW.GLFW_RELEASE:
if (setKeysDownWithRadialEvent)
keyToPress.setDown(false);
keyToPress = null;
break;
}
}
}
@SubscribeEvent(priority = EventPriority.LOWEST)
public static void onKeyInputAfter(InputEvent.Key event) {
if (keyToPress != null) {
NeoForge.EVENT_BUS.post(
new InputEvent.Key(
//? if >= 1.21.9 {
new KeyEvent(keyToPress.getKey().getValue(), 0, 0),
GLFW.GLFW_RELEASE
//?} else {
//keyToPress.getKey().getValue(),
//0,
//GLFW.GLFW_RELEASE,
//0
//?}
)
);
}
}
*///?}
@Override
public void onControllerInput(ControllerEntity controller) {
if (this.controller != controller) return;
if (editMode == null) {
//? if neoforge {
/*if (ControlifyBindings.RADIAL_SEND_KEY.on(controller).justPressed()) {
onClose();
if (selectedButton != -1) {
@Nullable KeyMapping km =
//? if >= 1.21.4 {
KeyMapping
//?} else {
//KeyMappingAccessor.getAll()
//?}
.get(controller.input().orElseThrow().confObj().radialActions[selectedButton].getPath());
keyToPress = km;
setKeysDownWithRadialEvent = controller.input().orElseThrow().confObj().setKeysDownWithRadialEvent;
if (km != null) {
NeoForge.EVENT_BUS.post(
new InputEvent.Key(
//? if >= 1.21.9 {
new KeyEvent(km.getKey().getValue(), 0, 0),
GLFW.GLFW_PRESS
//?} else {
//km.getKey().getValue(),
//0,
//GLFW.GLFW_PRESS,
//0
//?}
)
);
playClickSound();
}
}
}
*///?}
if (!openBind.digitalNow()) {
if (selectedButton != -1 && buttons[selectedButton].invoke()) {
playClickSound();
}
onClose();
}
}
if (editMode != null && ControlifyBindings.GUI_BACK.on(controller).justPressed()) {
playClickSound();
onClose();
}
if (!isEditing) {
float x = ControlifyBindings.RADIAL_AXIS_RIGHT.on(controller).analogueNow()
- ControlifyBindings.RADIAL_AXIS_LEFT.on(controller).analogueNow();
float y = ControlifyBindings.RADIAL_AXIS_DOWN.on(controller).analogueNow()
- ControlifyBindings.RADIAL_AXIS_UP.on(controller).analogueNow();
float threshold = controller.input().orElseThrow().config().config().buttonActivationThreshold;
if (Math.abs(x) >= threshold || Math.abs(y) >= threshold) {
float angle = Mth.wrapDegrees(Mth.RAD_TO_DEG * (float) Mth.atan2(y, x) - 90f) + 180f;
float each = 360f / buttons.length;
int newSelected = Mth.floor((angle + each / 2f) / each) % buttons.length;
if (newSelected != selectedButton) {
selectedButton = newSelected;
minecraft.getSoundManager().play(SimpleSoundInstance.forUI(ControlifyClientSounds.SCREEN_FOCUS_CHANGE.get(), 1f));
controller.hdHaptics().ifPresent(haptics -> haptics.playHaptic(HapticEffects.NAVIGATE));
}
for (int i = 0; i < buttons.length; i++) {
boolean selected = i == selectedButton;
buttons[i].setFocused(selected);
if (selected) {
this.setFocused(buttons[i]);
}
}
idleTicks = 0;
} else if (editMode == null) {
idleTicks++;
if (idleTicks >= idleTicksTimeout && selectedButton != -1) {
selectedButton = -1;
for (RadialButton button : buttons) {
button.setFocused(false);
}
controller.hdHaptics().ifPresent(haptics -> haptics.playHaptic(HapticEffects.NAVIGATE));
}
}
}
}
@Override
public void render(GuiGraphics graphics, int mouseX, int mouseY, float delta) {
super.render(graphics, mouseX, mouseY, delta);
if (editMode == null) {
graphics.drawCenteredString(
font,
text,
width / 2,
height - 39,
-1
);
}
}
@Override
public void renderBackground(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
if (editMode != null) {
super.renderBackground(guiGraphics, mouseX, mouseY, partialTick);
}
}
private void playClickSound() {
minecraft.getSoundManager().play(SimpleSoundInstance.forUI(SoundEvents.UI_BUTTON_CLICK, 1f));
}
private void finishEditing() {
isEditing = false;
removeWidget(actionSelectList);
this.setFocused(null);
actionSelectList = null;
}
@Override
public void onClose() {
Controlify.instance().config().saveIfDirty();
minecraft.setScreen(parent);
}
@Override
public boolean isPauseScreen() {
return editMode != null;
}
@Override
public ScreenProcessor<?> screenProcessor() {
return this.processor;
}
public interface RadialItem {
Component name();
RadialIcon icon();
boolean playAction();
}
public interface EditMode {
void setRadialItem(int index, RadialItem item);
List<RadialItem> getEditCandidates();
}
public class RadialButton implements Renderable, GuiEventListener, NarratableEntry, ComponentProcessor {
public static final ResourceLocation TEXTURE = CUtil.rl("textures/gui/radial-buttons.png");
private int x, y;
private float translateX, translateY;
private boolean focused;
private RadialItem item;
private MultiLineLabel name;
private RadialButton(RadialItem item, float x, float y) {
this.setX(x);
this.setY(y);
this.setAction(item);
}
@Override
public void render(GuiGraphics graphics, int mouseX, int mouseY, float delta) {
var pose = CGuiPose.ofPush(graphics);
pose.translate(x + translateX, y + translateY);
pose.push();
pose.scale(2, 2);
Blit.tex(
graphics,
TEXTURE,
0, 0,
focused ? 16 : 0, 0,
16, 16,
32, 16
);
pose.pop();
if (editMode == null || !focused) {
pose.push();
pose.translate(4, 4);
pose.scale(1.5f, 1.5f);
this.item.icon().draw(graphics, 0, 0, delta);
pose.pop();
} else {
Component bind = ControlifyBindings.GUI_PRESS.on(controller).inputGlyph();
graphics.drawString(font, bind, 16 - font.width(bind) / 2, 16 - font.lineHeight / 2, -1);
}
pose.pop();
if (focused) {
//? if >=1.21.9 {
name.render(graphics, MultiLineLabel.Align.CENTER, width / 2, height / 2 - font.lineHeight / 2 - ((name.getLineCount() - 1) * font.lineHeight), font.lineHeight, false, -1);
//?} else {
/*name.renderCentered(graphics, width / 2, height / 2 - font.lineHeight / 2 - ((name.getLineCount() - 1) * font.lineHeight / 2));
*///?}
}
}
public boolean invoke() {
return this.item.playAction();
}
public void setAction(RadialItem item) {
this.item = item;
this.name = MultiLineLabel.create(font, item.name(), (int)(radialRadius * 2 - 32));
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setX(float x) {
this.x = (int) x;
this.translateX = x - this.x;
}
public void setY(float y) {
this.y = (int) y;
this.translateY = y - this.y;
}
@Override
public boolean isFocused() {
return focused;
}
@Override
public void setFocused(boolean focused) {
this.focused = focused;
}
@Override
public boolean overrideControllerButtons(ScreenProcessor<?> screen, ControllerEntity controller) {
if (editMode != null && controller == RadialMenuScreen.this.controller && ControlifyBindings.GUI_PRESS.on(controller).justPressed()) {
RadialButton button = buttons[selectedButton];
int x = button.x < width / 2 ? button.x - 110 : button.x + 42;
actionSelectList = new ActionSelectList(selectedButton, x, button.y, 100, 80);
addRenderableWidget(actionSelectList);
RadialMenuScreen.this.setFocused(actionSelectList);
isEditing = true;
return true;
}
return false;
}
@Override
public NarrationPriority narrationPriority() {
return isFocused() ? NarrationPriority.FOCUSED : NarrationPriority.NONE;
}
@Override
public void updateNarration(NarrationElementOutput builder) {
builder.add(NarratedElementType.TITLE, item.name());
}
@Override
public ScreenRectangle getRectangle() {
return new ScreenRectangle(x, y, 32, 32);
}
}
public class ActionSelectList implements Renderable, ContainerEventHandler, NarratableEntry, ComponentProcessor {
private final int radialIndex;
private final int x, y;
private final int width, height;
private final int itemHeight = 10;
private int scrollOffset;
private boolean focused;
private ActionEntry focusedEntry;
private final List<ActionEntry> children = new ArrayList<>();
public ActionSelectList(int index, int x, int y, int width, int height) {
this.radialIndex = index;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
for (RadialItem item : editMode.getEditCandidates()) {
children.add(new ActionEntry(item));
}
RadialItem item = items[radialIndex];
children.stream()
.filter(action -> action.item.equals(item))
.findAny()
.ifPresent(this::setFocused);
}
@Override
public void render(GuiGraphics graphics, int mouseX, int mouseY, float delta) {
graphics.fill(x, y, x + width, y + height, 0x80000000);
graphics.enableScissor(x, y, x + width, y + height);
int y = this.y - scrollOffset;
for (ActionEntry child : children) {
child.render(graphics, x, y, width, itemHeight, mouseX, mouseY, delta);
y += itemHeight;
}
graphics.disableScissor();
graphics./*? if >=1.21.9 {*/submitOutline/*?} else {*//*renderOutline*//*?}*/(x - 1, this.y - 1, width + 2, height + 2, 0x80ffffff);
}
@Override
public boolean overrideControllerButtons(ScreenProcessor<?> screen, ControllerEntity controller) {
if (controller == RadialMenuScreen.this.controller) {
if (ControlifyBindings.GUI_BACK.on(controller).justPressed()) {
finishEditing();
return true;
}
}
return false;
}
@Override
public List<ActionEntry> children() {
return children;
}
@Override
public boolean isDragging() {
return false;
}
@Override
public void setDragging(boolean dragging) {
}
@Nullable
@Override
public ActionEntry getFocused() {
return focusedEntry;
}
@Override
public void setFocused(@Nullable GuiEventListener child) {
ActionEntry focus = (ActionEntry) child;
this.focusedEntry = focus;
if (focus != null) {
int index = children().indexOf(child);
if (index != -1) {
int focusY = index * itemHeight - scrollOffset;
if (focusY < 0)
scrollOffset = Mth.clamp(index * itemHeight, 0, children().size() * itemHeight - height);
else if (focusY + itemHeight > height)
scrollOffset = Mth.clamp(index * itemHeight + itemHeight - height, 0, children().size() * itemHeight - height);
}
}
}
@Override
public void setFocused(boolean focused) {
this.focused = focused;
}
@Override
public boolean isFocused() {
return focused;
}
@Override
public NarrationPriority narrationPriority() {
return focused ? NarrationPriority.FOCUSED : NarrationPriority.NONE;
}
@Override
public void updateNarration(NarrationElementOutput builder) {
if (getFocused() != null) {
builder.add(NarratedElementType.TITLE, getFocused().item.name());
}
}
public class ActionEntry implements GuiEventListener, ComponentProcessor {
private int x, y;
private boolean focused;
private final RadialItem item;
public ActionEntry(RadialItem item) {
this.item = item;
}
public void render(GuiGraphics graphics, int x, int y, int width, int itemHeight, int mouseX, int mouseY, float delta) {
this.x = x;
this.y = y;
if (focused)
graphics.fill(x, y, x + width, y + itemHeight, 0xff000000);
graphics.drawString(RadialMenuScreen.this.font, item.name(), x + 2, y + 1, focused ? -1 : 0xffa6a6a6);
}
@Override
public void setFocused(boolean focused) {
this.focused = focused;
}
@Override
public boolean isFocused() {
return focused;
}
@Nullable
@Override
public ComponentPath nextFocusPath(FocusNavigationEvent event) {
return !focused ? ComponentPath.leaf(this) : null;
}
@Override
public ScreenRectangle getRectangle() {
return new ScreenRectangle(x, y, width, itemHeight);
}
@Override
public boolean overrideControllerButtons(ScreenProcessor<?> screen, ControllerEntity controller) {
if (controller == RadialMenuScreen.this.controller) {
if (ControlifyBindings.GUI_PRESS.on(controller).justPressed()) {
editMode.setRadialItem(radialIndex, item);
Controlify.instance().config().setDirty();
buttons[radialIndex].setAction(item);
playClickSound();
finishEditing();
return true;
}
}
return false;
}
}
}
public static class Processor extends ScreenProcessor<RadialMenuScreen> {
public Processor(RadialMenuScreen screen) {
super(screen);
}
@Override
public VirtualMouseBehaviour virtualMouseBehaviour() {
return VirtualMouseBehaviour.DISABLED;
}
}
}