-
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathLabelController.java
More file actions
186 lines (154 loc) · 6.41 KB
/
Copy pathLabelController.java
File metadata and controls
186 lines (154 loc) · 6.41 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
package dev.isxander.yacl3.gui.controllers;
import com.mojang.blaze3d.platform.cursor.CursorTypes;
import dev.isxander.yacl3.api.Controller;
import dev.isxander.yacl3.api.Option;
import dev.isxander.yacl3.api.utils.Dimension;
import dev.isxander.yacl3.gui.AbstractWidget;
import dev.isxander.yacl3.gui.YACLScreen;
import dev.isxander.yacl3.gui.utils.GuiUtils;
import net.minecraft.client.gui.ActiveTextCollector;
import net.minecraft.client.gui.ComponentPath;
import net.minecraft.client.gui.GuiGraphicsExtractor;
import net.minecraft.client.gui.components.MultiLineLabel;
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.input.MouseButtonEvent;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.Style;
import net.minecraft.util.FormattedCharSequence;
import org.jetbrains.annotations.Nullable;
import org.jspecify.annotations.NonNull;
import java.util.List;
/**
* Simply renders some text as a label.
*/
public record LabelController(Option<Component> option) implements Controller<Component> {
/**
* Constructs a label controller
*
* @param option bound option
*/
public LabelController {
}
/**
* {@inheritDoc}
*/
@Override
public Option<Component> option() {
return option;
}
@Override
public Component formatValue() {
return option().pendingValue();
}
@Override
public AbstractWidget provideWidget(YACLScreen screen, Dimension<Integer> widgetDimension) {
return new LabelControllerElement(screen, widgetDimension);
}
public class LabelControllerElement extends AbstractWidget {
private List<FormattedCharSequence> wrappedText;
protected MultiLineLabel wrappedTooltip;
protected boolean focused;
protected final YACLScreen screen;
public LabelControllerElement(YACLScreen screen, Dimension<Integer> dim) {
super(dim);
this.screen = screen;
option().addListener((opt, pending) -> updateTooltip());
updateTooltip();
updateText();
}
@Override
public void extractRenderState(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float a) {
updateText();
int y = getDimension().y();
ActiveTextCollector textCollector = graphics.textRenderer(GuiGraphicsExtractor.HoveredTextEffects.TOOLTIP_AND_CURSOR);
Style fallbackStyle = Style.EMPTY.withColor(option().available() ? -1 : 0xFFA0A0A0);
for (FormattedCharSequence text : wrappedText) {
textCollector.accept(
getDimension().x() + getXPadding(),
y + getYPadding(),
GuiUtils.applyFallbackStyle(text, fallbackStyle)
);
y += textRenderer.lineHeight;
}
if (isFocused()) {
graphics.fill(getDimension().x() - 1, getDimension().y() - 1, getDimension().xLimit() + 1, getDimension().y(), -1);
graphics.fill(getDimension().x() - 1, getDimension().y() - 1, getDimension().x(), getDimension().yLimit() + 1, -1);
graphics.fill(getDimension().x() - 1, getDimension().yLimit(), getDimension().xLimit() + 1, getDimension().yLimit() + 1, -1);
graphics.fill(getDimension().xLimit(), getDimension().y() - 1, getDimension().xLimit() + 1, getDimension().yLimit() + 1, -1);
}
graphics.pose().pushMatrix();
if (isMouseOver(mouseX, mouseY)) {
Style style = getStyle(mouseX, mouseY);
if (style != null && style.getClickEvent() != null) {
graphics.requestCursor(CursorTypes.POINTING_HAND);
}
}
graphics.pose().popMatrix();
}
@Override
public boolean mouseClicked(@NonNull MouseButtonEvent event, boolean doubleClick) {
if (!isMouseOver(event.x(), event.y()))
return false;
Style style = getStyle((int) event.x(), (int) event.y());
if (style == null)
return false;
// TODO: reimplement
return false;
}
@Nullable
protected Style getStyle(int mouseX, int mouseY) {
if (!getDimension().isPointInside(mouseX, mouseY))
return null;
int x = mouseX - getDimension().x() - getXPadding();
int y = mouseY - getDimension().y() - getYPadding();
int line = y / textRenderer.lineHeight;
if (x < 0 || x > getDimension().xLimit()) return null;
if (y < 0 || y > getDimension().yLimit()) return null;
if (line < 0 || line >= wrappedText.size()) return null;
// TODO reimplement
return null;
}
private int getXPadding() {
return 4;
}
private int getYPadding() {
return 3;
}
private void updateText() {
wrappedText = textRenderer.split(formatValue(), getDimension().width() - getXPadding() * 2);
setDimension(getDimension().withHeight(wrappedText.size() * textRenderer.lineHeight + getYPadding() * 2));
}
private void updateTooltip() {
this.wrappedTooltip = MultiLineLabel.create(textRenderer, option().tooltip(), screen.width / 3 * 2 - 10);
}
@Override
public boolean matchesSearch(String query) {
return formatValue().getString().toLowerCase().contains(query.toLowerCase());
}
@Nullable
@Override
public ComponentPath nextFocusPath(FocusNavigationEvent focusNavigationEvent) {
if (!option().available())
return null;
return !this.isFocused() ? ComponentPath.leaf(this) : null;
}
@Override
public boolean isFocused() {
return focused;
}
@Override
public void setFocused(boolean focused) {
this.focused = focused;
}
@Override
public void updateNarration(NarrationElementOutput builder) {
builder.add(NarratedElementType.TITLE, formatValue());
}
@Override
public NarrationPriority narrationPriority() {
return NarrationPriority.FOCUSED;
}
}
}