-
Notifications
You must be signed in to change notification settings - Fork 318
Expand file tree
/
Copy pathAbstractScrollList.java
More file actions
297 lines (236 loc) · 8.87 KB
/
Copy pathAbstractScrollList.java
File metadata and controls
297 lines (236 loc) · 8.87 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
package net.vulkanmod.config.gui;
import com.mojang.blaze3d.opengl.GlStateManager;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.input.MouseButtonEvent;
import net.minecraft.util.Mth;
import net.vulkanmod.config.gui.render.GuiRenderer;
import net.vulkanmod.config.gui.widget.VAbstractWidget;
import net.vulkanmod.vulkan.util.ColorUtil;
import org.jetbrains.annotations.Nullable;
import org.jspecify.annotations.NonNull;
import java.util.List;
public abstract class AbstractScrollList extends GuiElement {
protected final List<Entry> children = new ObjectArrayList<>();
protected boolean scrolling = false;
protected float scrollAmount = 0.0f;
protected int itemWidth;
protected int totalItemHeight;
protected int itemMargin;
protected int listLength = 0;
protected Entry focused;
protected AbstractScrollList() {
}
protected void addEntry(Entry entry) {
this.children.add(entry);
this.listLength += entry.getTotalHeight();
}
public void clearEntries() {
this.listLength = 0;
this.children.clear();
}
protected void updateScrollingState(double mouseX, int button) {
this.scrolling = button == 0 && mouseX >= (double) this.getScrollbarPosition() && mouseX < (double) (this.getScrollbarPosition() + 6);
}
protected float getScrollAmount() {
return scrollAmount;
}
public void setScrollAmount(double d) {
this.scrollAmount = (float) Mth.clamp(d, 0.0, this.getMaxScroll());
}
protected int getItemCount() {
return this.children.size();
}
protected GuiEventListener getFocused() {
return focused;
}
protected void setFocused(Entry focussed) {
this.focused = focussed;
}
@Override
public boolean mouseClicked(MouseButtonEvent event, boolean bl) {
this.updateScrollingState(event.x(), event.button());
if (this.isMouseOver(event.x(), event.y())) {
Entry entry = this.getEntryAtPos(event.x(), event.y());
if (entry != null && entry.mouseClicked(event, bl)) {
setFocused(entry);
entry.setFocused(true);
return true;
}
return event.button() == 0;
}
return false;
}
@Override
public boolean mouseReleased(MouseButtonEvent event) {
if (this.isValidClickButton(event.button())) {
Entry entry = this.getEntryAtPos(event.x(), event.y());
if (entry != null) {
if (entry.mouseReleased(event)) {
entry.setFocused(false);
setFocused(null);
return true;
}
}
}
return false;
}
@Override
public boolean mouseDragged(MouseButtonEvent event, double deltaX, double deltaY) {
if (event.button() != 0) {
return false;
}
if (this.getFocused() != null) {
return this.getFocused().mouseDragged(event, deltaX, deltaY);
}
if (!this.scrolling) {
return false;
}
double maxScroll = this.getMaxScroll();
if (event.y() < this.y) {
this.setScrollAmount(0.0);
} else if (event.y() > this.getBottom()) {
this.setScrollAmount(maxScroll);
} else if (maxScroll > 0.0) {
double barHeight = (double) this.height * this.height / this.getTotalLength();
double scrollFactor = Math.max(1.0, maxScroll / (this.height - barHeight));
this.setScrollAmount(this.getScrollAmount() + deltaY * scrollFactor);
}
return true;
}
public boolean mouseScrolled(double mouseX, double mouseY, double xScroll, double yScroll) {
this.setScrollAmount(this.getScrollAmount() - yScroll * (double) this.totalItemHeight / 2.0);
return true;
}
public int getMaxScroll() {
return Math.max(0, this.getTotalLength() - (this.height));
}
protected int getTotalLength() {
return this.listLength;
}
public int getBottom() {
return this.y + this.height;
}
@Nullable
protected Entry getEntryAtPos(double x, double y) {
int x0 = this.x;
if (x > this.getScrollbarPosition() || x < (double) x0)
return null;
for (var entry : this.children) {
VAbstractWidget widget = entry.widget;
if (widget != null && y >= widget.y && y <= widget.y + widget.height) {
return entry;
}
}
return null;
}
@Override
public void updateState(double mX, double mY) {
if (this.focused != null)
return;
super.updateState(mX, mY);
}
public void renderWidget(int mouseX, int mouseY) {
GuiRenderer.enableScissor(x, y, x + width, y + height);
this.renderList(mouseX, mouseY);
GuiRenderer.disableScissor();
// Scroll bar
int maxScroll = this.getMaxScroll();
if (maxScroll > 0) {
GlStateManager._enableBlend();
int height = this.getHeight();
int totalLength = this.getTotalLength();
int barHeight = (int) ((float) (height * height) / totalLength);
barHeight = Mth.clamp(barHeight, 32, height - 8);
int scrollAmount = (int) this.getScrollAmount();
int barY = scrollAmount * (height - barHeight) / maxScroll + this.getY();
barY = Math.max(barY, this.getY());
int scrollbarPosition = this.getScrollbarPosition();
int thickness = 3;
int backgroundColor = ColorUtil.ARGB.pack(0.8f, 0.8f, 0.8f, 0.2f);
GuiRenderer.fill(scrollbarPosition, this.getY(), scrollbarPosition + thickness, this.getY() + height, backgroundColor);
int barColor = ColorUtil.ARGB.pack(0.3f, 0.0f, 0.0f, 0.6f);
GuiRenderer.fill(scrollbarPosition, barY, scrollbarPosition + thickness, barY + barHeight, barColor);
}
}
protected int getScrollbarPosition() {
return this.x + this.width;
}
public VAbstractWidget getHoveredWidget(double mouseX, double mouseY) {
if (this.focused != null)
return focused.widget;
if (!this.isMouseOver(mouseX, mouseY))
return null;
for (Entry entry : this.children) {
var widget = entry.widget;
if (widget == null || !widget.isMouseOver(mouseX, mouseY))
continue;
return widget;
}
return null;
}
protected void renderList(int mouseX, int mouseY) {
int itemCount = this.getItemCount();
int rowTop = this.y - (int) this.getScrollAmount();
for (int j = 0; j < itemCount; ++j) {
Entry entry = this.getEntry(j);
if (rowTop + entry.getTotalHeight() >= this.y && rowTop <= (this.y + this.height)) {
boolean updateState = this.focused == null;
entry.render(rowTop, mouseX, mouseY, updateState, this.x);
}
rowTop += entry.getTotalHeight();
}
}
protected Entry getEntry(int j) {
return this.children.get(j);
}
protected boolean isValidClickButton(int i) {
return i == 0;
}
protected static class Entry implements GuiEventListener {
final VAbstractWidget widget;
final int margin;
protected Entry(VAbstractWidget widget, int margin) {
this.widget = widget;
this.margin = margin;
}
public void render(int y, int mouseX, int mouseY, boolean updateState, int listX) {
if (widget == null)
return;
widget.y = y;
if (updateState)
widget.updateState(mouseX, mouseY);
widget.render(mouseX, mouseY);
}
public int getTotalHeight() {
if (widget != null)
return widget.height + margin;
else
return margin;
}
@Override
public boolean mouseClicked(@NonNull MouseButtonEvent event, boolean bl) {
if (widget == null) return false;
return widget.mouseClicked(event, bl);
}
@Override
public boolean mouseReleased(@NonNull MouseButtonEvent event) {
if (widget == null) return false;
return widget.mouseReleased(event);
}
@Override
public boolean mouseDragged(@NonNull MouseButtonEvent event, double deltaX, double deltaY) {
if (widget == null) return false;
return widget.mouseDragged(event, deltaX, deltaY);
}
@Override
public boolean isFocused() {
return false;
}
@Override
public void setFocused(boolean bl) {
if (widget != null)
widget.setFocused(bl);
}
}
}