]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/gui/ControlList.java
Setup for 1.16.3 Fabric
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / gui / ControlList.java
1 package com.irtimaled.bbor.client.gui;
2
3 import com.irtimaled.bbor.client.renderers.RenderHelper;
4 import com.irtimaled.bbor.client.renderers.Renderer;
5 import com.irtimaled.bbor.common.MathHelper;
6 import net.minecraft.client.MinecraftClient;
7 import net.minecraft.client.gui.DrawableHelper;
8 import net.minecraft.client.util.math.MatrixStack;
9
10 import java.util.ArrayList;
11 import java.util.List;
12
13 public class ControlList extends DrawableHelper implements IControlSet {
14     public static final int CONTROLS_WIDTH = 310;
15     protected static final int PADDING = 8;
16
17     protected final int listLeft;
18     protected final List<ControlListEntry> entries = new ArrayList<>();
19     private final int scrollBarLeft;
20     private final int listHeight;
21     private final MinecraftClient minecraft;
22     private final int width;
23     private final int height;
24     private final int top;
25     private final int bottom;
26
27     protected int contentHeight = PADDING;
28     private double amountScrolled;
29     private boolean clickedScrollbar;
30     private boolean transparentBackground;
31     private IControl focused;
32     private boolean isDragging;
33
34     ControlList(int width, int height, int top, int bottom) {
35         this.minecraft = MinecraftClient.getInstance();
36         this.width = width;
37         this.scrollBarLeft = width - 6;
38         this.height = height;
39         this.top = top;
40         this.bottom = bottom;
41         this.listHeight = bottom - top;
42         this.listLeft = width / 2 - CONTROLS_WIDTH / 2;
43     }
44
45     void add(ControlListEntry entry) {
46         entry.index = entries.size();
47         addEntry(entry);
48     }
49
50     private void addEntry(ControlListEntry entry) {
51         this.entries.add(entry);
52         this.contentHeight += entry.getControlHeight();
53     }
54
55     public void filter(String lowerValue) {
56         int height = 0;
57
58         for (ControlListEntry entry : entries) {
59             entry.filter(lowerValue);
60             if (entry.isVisible()) {
61                 height += entry.getControlHeight();
62             } else if (entry == focused) {
63                 focused = null;
64             }
65         }
66         this.contentHeight = height + PADDING;
67     }
68
69     void close() {
70         this.entries.forEach(ControlListEntry::close);
71     }
72
73     @Override
74     public boolean mouseClicked(double mouseX, double mouseY, int button) {
75         this.clickedScrollbar = button == 0 && mouseX >= (double) this.scrollBarLeft;
76         return isMouseOver(mouseX, mouseY) &&
77                 (IControlSet.super.mouseClicked(mouseX, mouseY, button) ||
78                         this.clickedScrollbar);
79     }
80
81     @Override
82     public boolean isMouseOver(double mouseX, double mouseY) {
83         return mouseY >= (double) this.top && mouseY <= (double) this.bottom;
84     }
85
86     @Override
87     public boolean changeFocus(boolean moveForward) {
88         boolean newControlFocused = IControlSet.super.changeFocus(moveForward);
89         if (newControlFocused) {
90             this.ensureVisible((ControlListEntry) this.getFocused());
91         }
92
93         return newControlFocused;
94     }
95
96     private void ensureVisible(ControlListEntry control) {
97         int controlTop = control.getControlTop();
98         int controlHeight = control.getControlHeight();
99         int distanceAboveTop = this.top - controlTop;
100         if (distanceAboveTop > 0) {
101             this.amountScrolled -= Math.max(controlHeight, distanceAboveTop + PADDING);
102             return;
103         }
104
105         int distanceBelowBottom = controlTop + controlHeight - this.bottom;
106         if (distanceBelowBottom > 0) {
107             this.amountScrolled += Math.max(controlHeight, distanceBelowBottom + PADDING);
108         }
109     }
110
111     @Override
112     public boolean mouseDragged(double mouseX, double mouseY, int button, double p_mouseDragged_6_, double p_mouseDragged_8_) {
113         if (IControlSet.super.mouseDragged(mouseX, mouseY, button, p_mouseDragged_6_, p_mouseDragged_8_)) {
114             return true;
115         }
116         if (button == 0 && this.clickedScrollbar) {
117             if (mouseY < (double) this.top) {
118                 this.amountScrolled = 0.0D;
119             } else if (mouseY > (double) this.bottom) {
120                 this.amountScrolled = this.getMaxScroll();
121             } else {
122                 double maxScroll = this.getMaxScroll();
123                 if (maxScroll < 1.0D) {
124                     maxScroll = 1.0D;
125                 }
126
127                 double amountScrolled = maxScroll / (double) (this.listHeight - getScrollBarHeight());
128                 if (amountScrolled < 1.0D) {
129                     amountScrolled = 1.0D;
130                 }
131
132                 this.amountScrolled += p_mouseDragged_8_ * amountScrolled;
133             }
134
135             return true;
136         }
137         return false;
138     }
139
140     private int getMaxScroll() {
141         return Math.max(0, this.contentHeight - (this.listHeight - 4));
142     }
143
144     private int getScrollBarHeight() {
145         return MathHelper.clamp((int) ((float) (this.listHeight * this.listHeight) / (float) this.contentHeight),
146                 32,
147                 this.listHeight - PADDING);
148     }
149
150     @Override
151     public boolean mouseScrolled(double mouseX, double mouseY, double scrollAmount) {
152         this.amountScrolled -= scrollAmount * 10;
153         return true;
154     }
155
156     public void render(MatrixStack matrixStack, int mouseX, int mouseY) {
157         this.amountScrolled = MathHelper.clamp(this.amountScrolled, 0.0D, this.getMaxScroll());
158
159         RenderHelper.disableLighting();
160         RenderHelper.disableFog();
161         if (!transparentBackground) drawListBackground();
162
163         int listTop = this.top + PADDING - (int) this.amountScrolled;
164
165         drawEntries(matrixStack, mouseX, mouseY, listTop);
166
167         RenderHelper.enableDepthTest();
168         RenderHelper.depthFuncAlways();
169
170         this.overlayBackground(0, this.top);
171         this.overlayBackground(this.bottom, this.height);
172         RenderHelper.depthFuncLessEqual();
173         RenderHelper.disableDepthTest();
174         RenderHelper.enableBlend();
175         RenderHelper.blendFuncGui();
176         RenderHelper.disableAlphaTest();
177         RenderHelper.shadeModelSmooth();
178         RenderHelper.disableTexture();
179         drawOverlayShadows();
180
181         int maxScroll = this.getMaxScroll();
182         if (maxScroll > 0) {
183             drawScrollBar(maxScroll);
184         }
185
186         RenderHelper.enableTexture();
187         RenderHelper.shadeModelFlat();
188         RenderHelper.enableAlphaTest();
189         RenderHelper.disableBlend();
190     }
191
192     private void drawListBackground() {
193         this.minecraft.getTextureManager().bindTexture(DrawableHelper.OPTIONS_BACKGROUND_TEXTURE);
194         Renderer.startTextured()
195                 .setColor(32, 32, 32)
196                 .setAlpha(255)
197                 .addPoint(0, this.bottom, 0.0D, (float) 0 / 32.0F, (float) (this.bottom + (int) this.amountScrolled) / 32.0F)
198                 .addPoint(this.width, this.bottom, 0.0D, (float) this.width / 32.0F, (float) (this.bottom + (int) this.amountScrolled) / 32.0F)
199                 .addPoint(this.width, this.top, 0.0D, (float) this.width / 32.0F, (float) (this.top + (int) this.amountScrolled) / 32.0F)
200                 .addPoint(0, this.top, 0.0D, (float) 0 / 32.0F, (float) (this.top + (int) this.amountScrolled) / 32.0F)
201                 .render();
202     }
203
204     private void drawEntries(MatrixStack matrixStack, int mouseX, int mouseY, int top) {
205         for (ControlListEntry entry : this.entries) {
206             if (!entry.isVisible()) continue;
207
208             entry.setX(this.listLeft);
209             entry.setY(top);
210
211             int height = entry.getControlHeight();
212             int bottom = top + height;
213             if(top <= this.bottom && bottom >= this.top) {
214                 drawEntry(matrixStack, mouseX, mouseY, top, entry, height);
215             }
216             top = bottom;
217         }
218     }
219
220     protected void drawEntry(MatrixStack matrixStack, int mouseX, int mouseY, int top, ControlListEntry entry, int height) {
221         entry.render(matrixStack, mouseX, mouseY);
222     }
223
224     private void overlayBackground(int top, int bottom) {
225         this.minecraft.getTextureManager().bindTexture(DrawableHelper.OPTIONS_BACKGROUND_TEXTURE);
226         Renderer.startTextured()
227                 .setColor(64, 64, 64)
228                 .setAlpha(255)
229                 .addPoint(0, bottom, -100.0D, 0.0D, (float) bottom / 32.0F)
230                 .addPoint(this.width, bottom, -100.0D, (float) this.width / 32.0F, (float) bottom / 32.0F)
231                 .addPoint(this.width, top, -100.0D, (float) this.width / 32.0F, (float) top / 32.0F)
232                 .addPoint(0, top, -100.0D, 0.0D, (float) top / 32.0F)
233                 .render();
234     }
235
236     private void drawScrollBar(int maxScroll) {
237         int scrollBarHeight = this.getScrollBarHeight();
238         int scrollBarTop = (int) this.amountScrolled * (this.listHeight - scrollBarHeight) / maxScroll + this.top;
239         if (scrollBarTop < this.top) {
240             scrollBarTop = this.top;
241         }
242
243         Renderer.startTextured()
244                 .setAlpha(255)
245                 .addPoint(this.scrollBarLeft, this.bottom, 0.0D, 0.0D, 1.0D)
246                 .addPoint(this.width, this.bottom, 0.0D, 1.0D, 1.0D)
247                 .addPoint(this.width, this.top, 0.0D, 1.0D, 0.0D)
248                 .addPoint(this.scrollBarLeft, this.top, 0.0D, 0.0D, 0.0D)
249                 .render();
250
251         Renderer.startTextured()
252                 .setColor(128, 128, 128)
253                 .setAlpha(255)
254                 .addPoint(this.scrollBarLeft, scrollBarTop + scrollBarHeight, 0.0D, 0.0D, 1.0D)
255                 .addPoint(this.width, scrollBarTop + scrollBarHeight, 0.0D, 1.0D, 1.0D)
256                 .addPoint(this.width, scrollBarTop, 0.0D, 1.0D, 0.0D)
257                 .addPoint(this.scrollBarLeft, scrollBarTop, 0.0D, 0.0D, 0.0D)
258                 .render();
259
260         Renderer.startTextured()
261                 .setColor(192, 192, 192)
262                 .setAlpha(255)
263                 .addPoint(this.scrollBarLeft, scrollBarTop + scrollBarHeight - 1, 0.0D, 0.0D, 1.0D)
264                 .addPoint(this.width - 1, scrollBarTop + scrollBarHeight - 1, 0.0D, 1.0D, 1.0D)
265                 .addPoint(this.width - 1, scrollBarTop, 0.0D, 1.0D, 0.0D)
266                 .addPoint(this.scrollBarLeft, scrollBarTop, 0.0D, 0.0D, 0.0D)
267                 .render();
268     }
269
270     private void drawOverlayShadows() {
271         Renderer.startTextured()
272                 .addPoint(0, this.top + 4, 0.0D, 0.0D, 1.0D)
273                 .addPoint(this.width, this.top + 4, 0.0D, 1.0D, 1.0D)
274                 .setAlpha(255)
275                 .addPoint(this.width, this.top, 0.0D, 1.0D, 0.0D)
276                 .addPoint(0, this.top, 0.0D, 0.0D, 0.0D)
277                 .render();
278         Renderer.startTextured()
279                 .addPoint(this.width, this.bottom - 4, 0.0D, 1.0D, 0.0D)
280                 .addPoint(0, this.bottom - 4, 0.0D, 0.0D, 0.0D)
281                 .setAlpha(255)
282                 .addPoint(0, this.bottom, 0.0D, 0.0D, 1.0D)
283                 .addPoint(this.width, this.bottom, 0.0D, 1.0D, 1.0D)
284                 .render();
285     }
286
287     ControlList section(String title, CreateControl... createControls) {
288         this.add(new ControlListSection(title, createControls));
289         return this;
290     }
291
292     void setTransparentBackground() {
293         this.transparentBackground = true;
294     }
295
296     @Override
297     public List<? extends IControl> controls() {
298         return entries;
299     }
300
301     @Override
302     public IControl getFocused() {
303         return focused;
304     }
305
306     @Override
307     public void setFocused(IControl control) {
308         this.focused = control;
309     }
310
311     @Override
312     public boolean isDragging() {
313         return isDragging;
314     }
315
316     @Override
317     public void setDragging(boolean dragging) {
318         this.isDragging = dragging;
319     }
320 }