-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathModularPanel.java
More file actions
665 lines (601 loc) · 23.1 KB
/
ModularPanel.java
File metadata and controls
665 lines (601 loc) · 23.1 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
package com.cleanroommc.modularui.screen;
import com.cleanroommc.modularui.api.IPanelHandler;
import com.cleanroommc.modularui.api.ITheme;
import com.cleanroommc.modularui.api.layout.IViewport;
import com.cleanroommc.modularui.api.layout.IViewportStack;
import com.cleanroommc.modularui.api.widget.IFocusedWidget;
import com.cleanroommc.modularui.api.widget.IWidget;
import com.cleanroommc.modularui.api.widget.Interactable;
import com.cleanroommc.modularui.screen.viewport.GuiContext;
import com.cleanroommc.modularui.screen.viewport.GuiViewportStack;
import com.cleanroommc.modularui.screen.viewport.LocatedWidget;
import com.cleanroommc.modularui.theme.WidgetTheme;
import com.cleanroommc.modularui.utils.*;
import com.cleanroommc.modularui.value.sync.PanelSyncHandler;
import com.cleanroommc.modularui.widget.ParentWidget;
import com.cleanroommc.modularui.widget.sizer.Area;
import com.cleanroommc.modularui.widgets.SlotGroupWidget;
import net.minecraft.client.Minecraft;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.MustBeInvokedByOverriders;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Objects;
import java.util.function.BooleanSupplier;
import java.util.function.Function;
import java.util.function.IntSupplier;
import java.util.function.Supplier;
/**
* This class is like a window in windows. It can hold any amount of widgets. It may also be draggable.
* To open another panel on top of the main panel you must use {@link IPanelHandler#simple(ModularPanel, SecondaryPanel.IPanelBuilder)}
* or {@link com.cleanroommc.modularui.value.sync.PanelSyncManager#panel(String, ModularPanel, PanelSyncHandler.IPanelBuilder) PanelSyncManager#panel(String, ModularPanel, PanelSyncHandler.IPanelBuilder)}
* if the panel should be synced.
*/
public class ModularPanel extends ParentWidget<ModularPanel> implements IViewport {
public static ModularPanel defaultPanel(@NotNull String name) {
return defaultPanel(name, 176, 166);
}
public static ModularPanel defaultPanel(@NotNull String name, int width, int height) {
return new ModularPanel(name).size(width, height);
}
private static final int tapTime = 200;
@NotNull
private final String name;
private ModularScreen screen;
private IPanelHandler panelHandler;
private State state = State.IDLE;
private boolean cantDisposeNow = false;
private final ObjectList<LocatedWidget> hovering = ObjectList.create();
private final ObjectList<Interactable> acceptedInteractions = ObjectList.create();
private boolean isMouseButtonHeld = false, isKeyHeld = false;
@Nullable
private LocatedWidget lastPressed;
private long timePressed;
private int lastMouseButton;
private Animator animator;
private float scale = 1f;
private float alpha = 1f;
public ModularPanel(@NotNull String name) {
this.name = Objects.requireNonNull(name, "A panels name must not be null and should be unique!");
align(Alignment.Center);
}
@Override
public @NotNull ModularPanel getPanel() {
return this;
}
@Override
public Area getParentArea() {
return getScreen().getScreenArea();
}
@Override
public void onInit() {
getScreen().registerFrameUpdateListener(this, this::findHoveredWidgets, false);
}
/**
* @return true if this panel is currently open on a screen
*/
public boolean isOpen() {
return this.state == State.OPEN;
}
/**
* If this panel is open it will be closed.
* If animating is enabled and an animation is already playing this method will do nothing.
*
* @param animate true if the closing animation should play first.
*/
public void closeIfOpen(boolean animate) {
if (!animate || !shouldAnimate()) {
this.screen.getPanelManager().closePanel(this);
return;
}
if (isOpen() && !isOpening() && !isClosing()) {
if (isMainPanel()) {
// if this is the main panel, start closing animation for all panels
for (ModularPanel panel : getScreen().getPanelManager().getOpenPanels()) {
if (!panel.isMainPanel()) {
panel.closeIfOpen(true);
}
}
}
getAnimator().setEndCallback(val -> this.screen.getPanelManager().closePanel(this)).backward();
}
}
public void animateClose() {
closeIfOpen(true);
}
void setPanelHandler(IPanelHandler panelHandler) {
this.panelHandler = panelHandler;
}
@Override
public boolean hasParent() {
return false;
}
@Override
public WidgetTheme getWidgetThemeInternal(ITheme theme) {
return theme.getPanelTheme();
}
@Override
public void transform(IViewportStack stack) {
super.transform(stack);
// apply scaling for animation
if (getScale() != 1f) {
float x = getArea().w() / 2f;
float y = getArea().h() / 2f;
stack.translate(x, y);
stack.scale(getScale(), getScale());
stack.translate(-x, -y);
}
}
@Override
public void getWidgetsAt(IViewportStack stack, HoveredWidgetList widgets, int x, int y) {
if (hasChildren()) {
IViewport.getChildrenAt(this, stack, widgets, x, y);
}
}
@Override
public void getSelfAt(IViewportStack stack, HoveredWidgetList widgets, int x, int y) {
if (isInside(stack, x, y)) {
widgets.add(this, stack.peek());
}
}
private void findHoveredWidgets() {
this.hovering.clear();
this.hovering.trim();
if (!isEnabled()) {
return;
}
HoveredWidgetList widgetList = new HoveredWidgetList(this.hovering);
getContext().reset();
GuiViewportStack stack = new GuiViewportStack();
stack.pushViewport(null, getScreen().getScreenArea());
stack.pushViewport(this, getArea());
transform(stack);
getSelfAt(stack, widgetList, getContext().getAbsMouseX(), getContext().getAbsMouseY());
transformChildren(stack);
getWidgetsAt(stack, widgetList, getContext().getAbsMouseX(), getContext().getAbsMouseY());
stack.popViewport(this);
stack.popViewport(null);
}
@MustBeInvokedByOverriders
public void onOpen(ModularScreen screen) {
this.screen = screen;
getArea().z(1);
this.scale = 1f;
this.alpha = 1f;
initialise(this);
if (shouldAnimate()) {
this.scale = 0.75f;
this.alpha = 0f;
getAnimator().setEndCallback(value -> {
this.scale = 1f;
this.alpha = 1f;
}).forward();
}
this.state = State.OPEN;
}
void reopen() {
if (this.state != State.CLOSED) throw new IllegalStateException();
this.state = State.OPEN;
}
@MustBeInvokedByOverriders
public void onClose() {
this.state = State.CLOSED;
if (this.panelHandler != null) {
this.panelHandler.closePanelInternal();
}
}
@MustBeInvokedByOverriders
@Override
public void dispose() {
if (this.state == State.DISPOSED) return;
if (this.state != State.CLOSED && this.state != State.WAIT_DISPOSING) {
throw new IllegalStateException("Panel must be closed before disposing!");
}
if (this.cantDisposeNow) {
this.state = State.WAIT_DISPOSING;
return;
}
getContext().getJeiSettings().removeJeiExclusionArea(this);
super.dispose();
this.screen = null;
this.state = State.DISPOSED;
}
/**
* Wraps a function so it can be called safely. This is needed in methods where the panel can be closed and disposed, but doing
* so will result in unexpected errors. This wrapper stops the disposal until the function has been fully executed.
* The return value of the function is then returned.
*
* @param runnable function to be called safely
* @param <T> return type
* @return return value of function
*/
public final <T> T doSafe(Supplier<T> runnable) {
if (this.state == State.DISPOSED) return null;
// make sure the screen is also not disposed
return getScreen().getPanelManager().doSafe(() -> {
this.cantDisposeNow = true;
T t = runnable.get();
this.cantDisposeNow = false;
if (this.state == State.WAIT_DISPOSING) {
this.state = State.CLOSED;
dispose();
}
return t;
});
}
public final boolean doSafeBool(BooleanSupplier runnable) {
return Objects.requireNonNull(doSafe(runnable::getAsBoolean));
}
public final int doSafeInt(IntSupplier runnable) {
return Objects.requireNonNull(doSafe(runnable::getAsInt));
}
@ApiStatus.OverrideOnly
public boolean onMousePressed(int mouseButton) {
return doSafeBool(() -> {
LocatedWidget pressed = LocatedWidget.EMPTY;
boolean result = false;
if (this.hovering.isEmpty()) {
if (closeOnOutOfBoundsClick()) {
animateClose();
result = true;
}
} else {
loop:
for (LocatedWidget widget : this.hovering) {
widget.applyMatrix(getContext());
if (widget.getElement() instanceof Interactable interactable) {
switch (interactable.onMousePressed(mouseButton)) {
case IGNORE:
break;
case ACCEPT: {
if (!this.isKeyHeld && !this.isMouseButtonHeld) {
this.acceptedInteractions.add(interactable);
}
pressed = widget;
// result = false;
break;
}
case STOP: {
pressed = LocatedWidget.EMPTY;
result = true;
widget.unapplyMatrix(getContext());
break loop;
}
case SUCCESS: {
if (!this.isKeyHeld && !this.isMouseButtonHeld) {
this.acceptedInteractions.add(interactable);
}
pressed = widget;
result = true;
widget.unapplyMatrix(getContext());
break loop;
}
}
}
if (getContext().onHoveredClick(mouseButton, widget)) {
pressed = LocatedWidget.EMPTY;
result = true;
widget.unapplyMatrix(getContext());
break;
}
widget.unapplyMatrix(getContext());
}
}
if (result && pressed.getElement() instanceof IFocusedWidget) {
getContext().focus(pressed);
} else {
getContext().removeFocus();
}
if (!this.isKeyHeld && !this.isMouseButtonHeld) {
this.lastPressed = pressed;
if (this.lastPressed.getElement() != null) {
this.timePressed = Minecraft.getSystemTime();
}
this.lastMouseButton = mouseButton;
this.isMouseButtonHeld = true;
}
return result;
});
}
@ApiStatus.OverrideOnly
public boolean onMouseRelease(int mouseButton) {
return isEnabled() && doSafeBool(() -> {
if (interactFocused(widget -> widget.onMouseRelease(mouseButton), false)) {
return true;
}
boolean result = false;
boolean tryTap = mouseButton == this.lastMouseButton && Minecraft.getSystemTime() - this.timePressed < tapTime;
for (LocatedWidget widget : this.hovering) {
if (widget.getElement() instanceof Interactable interactable) {
widget.applyMatrix(getContext());
if (interactable.onMouseRelease(mouseButton)) {
result = true;
widget.applyMatrix(getContext());
break;
}
if (tryTap && this.acceptedInteractions.remove(interactable)) {
Interactable.Result tabResult = interactable.onMouseTapped(mouseButton);
tryTap = switch (tabResult) {
case SUCCESS, STOP -> false;
default -> true;
};
}
widget.unapplyMatrix(getContext());
}
}
this.acceptedInteractions.clear();
this.lastMouseButton = -1;
this.timePressed = 0;
this.isMouseButtonHeld = false;
return result;
});
}
@ApiStatus.OverrideOnly
public boolean onKeyPressed(char typedChar, int keyCode) {
return doSafeBool(() -> {
switch (interactFocused(widget -> widget.onKeyPressed(typedChar, keyCode), Interactable.Result.IGNORE)) {
case STOP:
case SUCCESS:
if (!this.isKeyHeld && !this.isMouseButtonHeld) {
this.lastPressed = getContext().getFocusedWidget();
if (this.lastPressed != null) {
this.timePressed = Minecraft.getSystemTime();
}
this.lastMouseButton = keyCode;
this.isKeyHeld = true;
}
return true;
}
LocatedWidget pressed = null;
boolean result = false;
loop:
for (LocatedWidget widget : this.hovering) {
if (widget.getElement() instanceof Interactable interactable) {
widget.applyMatrix(getContext());
switch (interactable.onKeyPressed(typedChar, keyCode)) {
case IGNORE:
break;
case ACCEPT: {
if (!this.isKeyHeld && !this.isMouseButtonHeld) {
this.acceptedInteractions.add(interactable);
}
pressed = widget;
// result = false;
break;
}
case STOP: {
pressed = null;
result = true;
widget.unapplyMatrix(getContext());
break loop;
}
case SUCCESS: {
if (!this.isKeyHeld && !this.isMouseButtonHeld) {
this.acceptedInteractions.add(interactable);
}
pressed = widget;
result = true;
widget.unapplyMatrix(getContext());
break loop;
}
}
}
}
if (!this.isKeyHeld && !this.isMouseButtonHeld) {
this.lastPressed = pressed;
if (this.lastPressed != null) {
this.timePressed = Minecraft.getSystemTime();
}
this.lastMouseButton = keyCode;
this.isKeyHeld = true;
}
return result;
});
}
@ApiStatus.OverrideOnly
public boolean onKeyRelease(char typedChar, int keyCode) {
return doSafeBool(() -> {
if (interactFocused(widget -> widget.onKeyRelease(typedChar, keyCode), false)) {
return true;
}
boolean result = false;
boolean tryTap = keyCode == this.lastMouseButton && Minecraft.getSystemTime() - this.timePressed < tapTime;
for (LocatedWidget widget : this.hovering) {
if (widget.getElement() instanceof Interactable interactable) {
widget.applyMatrix(getContext());
if (interactable.onKeyRelease(typedChar, keyCode)) {
result = true;
widget.unapplyMatrix(getContext());
break;
}
if (tryTap && this.acceptedInteractions.remove(interactable)) {
Interactable.Result tabResult = interactable.onKeyTapped(typedChar, keyCode);
tryTap = switch (tabResult) {
case SUCCESS, STOP -> false;
default -> true;
};
}
widget.unapplyMatrix(getContext());
}
}
this.acceptedInteractions.clear();
this.lastMouseButton = -1;
this.timePressed = 0;
this.isKeyHeld = false;
return result;
});
}
@ApiStatus.OverrideOnly
public boolean onMouseScroll(ModularScreen.UpOrDown scrollDirection, int amount) {
return doSafeBool(() -> {
if (interactFocused(widget -> widget.onMouseScroll(scrollDirection, amount), false)) {
return true;
}
if (this.hovering.isEmpty()) return false;
for (LocatedWidget widget : this.hovering) {
if (widget.getElement() instanceof Interactable interactable) {
widget.applyMatrix(getContext());
boolean result = interactable.onMouseScroll(scrollDirection, amount);
widget.unapplyMatrix(getContext());
if (result) return true;
}
}
return true;
});
}
@ApiStatus.OverrideOnly
public boolean onMouseDrag(int mouseButton, long timeSinceClick) {
return doSafeBool(() -> {
if (this.isMouseButtonHeld &&
mouseButton == this.lastMouseButton &&
this.lastPressed != null &&
this.lastPressed.getElement() instanceof Interactable interactable) {
this.lastPressed.applyMatrix(getContext());
interactable.onMouseDrag(mouseButton, timeSinceClick);
this.lastPressed.unapplyMatrix(getContext());
return true;
}
return false;
});
}
@SuppressWarnings("unchecked")
private <T, W extends IWidget & IFocusedWidget & Interactable> T interactFocused(Function<W, T> function, T defaultValue) {
LocatedWidget focused = this.getContext().getFocusedWidget();
T result = defaultValue;
if (focused.getElement() instanceof Interactable interactable) {
focused.applyMatrix(getContext());
result = function.apply((W) interactable);
focused.unapplyMatrix(getContext());
}
return result;
}
/**
* @return if this panel can be dragged. Never works on the main panel.
*/
public boolean isDraggable() {
return getScreen().getMainPanel() != this;
}
/**
* @return if panels below this can still be interacted with.
*/
public boolean disablePanelsBelow() {
return false;
}
/**
* @return if this panel should be closed if outside of this panel is clicked.
*/
public boolean closeOnOutOfBoundsClick() {
return false;
}
public @NotNull String getName() {
return this.name;
}
@Override
public ModularScreen getScreen() {
if (!isValid()) {
throw new IllegalStateException();
}
return this.screen;
}
@NotNull
public ObjectList<LocatedWidget> getHovering() {
return this.hovering;
}
@Nullable
public IWidget getTopHovering() {
LocatedWidget lw = getTopHoveringLocated(false);
return lw == null ? null : lw.getElement();
}
@Nullable
public LocatedWidget getTopHoveringLocated(boolean debug) {
for (LocatedWidget widget : this.hovering) {
if (debug || widget.getElement().canHover()) {
return widget;
}
}
return null;
}
@Override
public int getDefaultHeight() {
return 166;
}
@Override
public int getDefaultWidth() {
return 176;
}
final void setPanelGuiContext(@NotNull GuiContext context) {
setContext(context);
context.getJeiSettings().addJeiExclusionArea(this);
}
public boolean isOpening() {
return this.animator != null && this.animator.isRunningForwards();
}
public boolean isClosing() {
return this.animator != null && this.animator.isRunningBackwards();
}
public float getScale() {
return this.scale;
}
public float getAlpha() {
return this.alpha;
}
public final boolean isMainPanel() {
return getScreen().getMainPanel() == this;
}
@ApiStatus.Internal
public void setSyncHandler(@Nullable PanelSyncHandler syncHandler) {
super.setSyncHandler(syncHandler);
setPanelHandler(syncHandler);
}
@NotNull
protected Animator getAnimator() {
if (this.animator == null) {
this.animator = new Animator(getScreen().getCurrentTheme().getOpenCloseAnimationOverride(), Interpolation.QUINT_OUT)
.setValueBounds(0.0f, 1.0f)
.setCallback(val -> {
this.alpha = (float) val;
this.scale = (float) val * 0.25f + 0.75f;
});
}
return this.animator;
}
public boolean shouldAnimate() {
return getScreen().getCurrentTheme().getOpenCloseAnimationOverride() > 0;
}
public ModularPanel bindPlayerInventory() {
return child(SlotGroupWidget.playerInventory());
}
public ModularPanel bindPlayerInventory(int bottom) {
return child(SlotGroupWidget.playerInventory(bottom));
}
@Override
public String toString() {
return super.toString() + "#" + getName();
}
public State getState() {
return this.state;
}
public enum State {
/**
* Initial state of any panel
*/
IDLE,
/**
* State after the panel opened
*/
OPEN,
/**
* State after panel closed
*/
CLOSED,
/**
* State after panel disposed.
* Panel can still be reopened in this state.
*/
DISPOSED,
/**
* Panel is closed and is waiting to be disposed.
*/
WAIT_DISPOSING
}
}