forked from ThinkingStudios/ObsidianUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpruceScreen.java
More file actions
126 lines (107 loc) · 3.7 KB
/
Copy pathSpruceScreen.java
File metadata and controls
126 lines (107 loc) · 3.7 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
/*
* Copyright © 2020~2024 LambdAurora <email@lambdaurora.dev>
* Copyright © 2024 ThinkingStudio
*
* This file is part of ObsidianUI.
*
* Licensed under the MIT license. For more information,
* see the LICENSE file.
*/
package org.thinkingstudio.obsidianui.screen;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.Drawable;
import net.minecraft.client.gui.Element;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.Text;
import org.lwjgl.glfw.GLFW;
import org.thinkingstudio.obsidianui.SprucePositioned;
import org.thinkingstudio.obsidianui.Tooltip;
import org.thinkingstudio.obsidianui.navigation.NavigationDirection;
import org.thinkingstudio.obsidianui.widget.SpruceElement;
import org.thinkingstudio.obsidianui.widget.SpruceWidget;
import java.util.function.BooleanSupplier;
import java.util.function.Supplier;
/**
* Represents a screen.
*
* @author LambdAurora
* @version 3.3.0
* @since 2.0.0
*/
public abstract class SpruceScreen extends Screen implements SprucePositioned, SpruceElement {
protected double scaleFactor;
protected SpruceScreen(Text title) {
super(title);
}
@Override
public void setFocused(Element focused) {
var old = this.getFocused();
if (old == focused) return;
if (old instanceof SpruceWidget)
old.setFocused(false);
super.setFocused(focused);
if (focused instanceof SpruceWidget)
focused.setFocused(true);
}
@Override
protected void init() {
this.scaleFactor = this.client.getWindow().getScaleFactor();
}
/* Input */
@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
return NavigationDirection.fromKey(keyCode, Screen.hasShiftDown())
.map(dir -> this.onNavigation(dir, keyCode == GLFW.GLFW_KEY_TAB))
.orElseGet(() -> super.keyPressed(keyCode, scanCode, modifiers));
}
/* Navigation */
@Override
public boolean onNavigation(NavigationDirection direction, boolean tab) {
if (this.requiresCursor()) return false;
var focused = this.getFocused();
boolean isNonNull = focused != null;
if (!isNonNull || !this.tryNavigating(focused, direction, tab)) {
var children = this.children();
int i = children.indexOf(focused);
int next;
if (isNonNull && i >= 0) next = i + (direction.isLookingForward() ? 1 : 0);
else if (direction.isLookingForward()) next = 0;
else next = children.size();
var iterator = children.listIterator(next);
BooleanSupplier hasNext = direction.isLookingForward() ? iterator::hasNext : iterator::hasPrevious;
Supplier<Element> nextGetter = direction.isLookingForward() ? iterator::next : iterator::previous;
Element nextElement;
do {
if (!hasNext.getAsBoolean()) {
this.setFocused(null);
return false;
}
nextElement = nextGetter.get();
} while (!this.tryNavigating(nextElement, direction, tab));
this.setFocused(nextElement);
}
return true;
}
private boolean tryNavigating(Element element, NavigationDirection direction, boolean tab) {
if (element instanceof SpruceElement) {
return ((SpruceElement) element).onNavigation(direction, tab);
}
element.setFocused(direction.isLookingForward());
return true;
}
/* Render */
@Override
public void render(DrawContext drawContext, int mouseX, int mouseY, float delta) {
this.renderWidgets(drawContext, mouseX, mouseY, delta);
this.renderTitle(drawContext, mouseX, mouseY, delta);
Tooltip.renderAll(drawContext);
}
public void renderTitle(DrawContext drawContext, int mouseX, int mouseY, float delta) {
}
public void renderWidgets(DrawContext drawContext, int mouseX, int mouseY, float delta) {
for (var element : this.children()) {
if (element instanceof Drawable drawable)
drawable.render(drawContext, mouseX, mouseY, delta);
}
}
}