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