1 package com.irtimaled.bbor.client.gui;
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;
9 import java.util.ArrayList;
10 import java.util.List;
12 public class ControlList extends AbstractGui implements IControlSet {
13 public static final int CONTROLS_WIDTH = 310;
14 protected static final int PADDING = 8;
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;
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;
33 ControlList(int width, int height, int top, int bottom) {
34 this.minecraft = Minecraft.getInstance();
36 this.scrollBarLeft = width - 6;
40 this.listHeight = bottom - top;
41 this.listLeft = width / 2 - CONTROLS_WIDTH / 2;
44 void add(ControlListEntry entry) {
45 entry.index = entries.size();
49 private void addEntry(ControlListEntry entry) {
50 this.entries.add(entry);
51 this.contentHeight += entry.getControlHeight();
54 public void filter(String lowerValue) {
57 for (ControlListEntry entry : entries) {
58 entry.filter(lowerValue);
59 if (entry.isVisible()) {
60 height += entry.getControlHeight();
61 } else if (entry == focused) {
65 this.contentHeight = height + PADDING;
69 this.entries.forEach(ControlListEntry::close);
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);
81 public boolean isMouseOver(double mouseX, double mouseY) {
82 return mouseY >= (double) this.top && mouseY <= (double) this.bottom;
86 public boolean changeFocus(boolean moveForward) {
87 boolean newControlFocused = IControlSet.super.changeFocus(moveForward);
88 if (newControlFocused) {
89 this.ensureVisible((ControlListEntry) this.getFocused());
92 return newControlFocused;
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);
104 int distanceBelowBottom = controlTop + controlHeight - this.bottom;
105 if (distanceBelowBottom > 0) {
106 this.amountScrolled += Math.max(controlHeight, distanceBelowBottom + PADDING);
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_)) {
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();
121 double maxScroll = this.getMaxScroll();
122 if (maxScroll < 1.0D) {
126 double amountScrolled = maxScroll / (double) (this.listHeight - getScrollBarHeight());
127 if (amountScrolled < 1.0D) {
128 amountScrolled = 1.0D;
131 this.amountScrolled += p_mouseDragged_8_ * amountScrolled;
139 private int getMaxScroll() {
140 return Math.max(0, this.contentHeight - (this.listHeight - 4));
143 private int getScrollBarHeight() {
144 return MathHelper.clamp((int) ((float) (this.listHeight * this.listHeight) / (float) this.contentHeight),
146 this.listHeight - PADDING);
150 public boolean mouseScrolled(double mouseX, double mouseY, double scrollAmount) {
151 this.amountScrolled -= scrollAmount * 10;
155 public void render(int mouseX, int mouseY) {
156 this.amountScrolled = MathHelper.clamp(this.amountScrolled, 0.0D, this.getMaxScroll());
158 RenderHelper.disableLighting();
159 RenderHelper.disableFog();
160 if (!transparentBackground) drawListBackground();
162 int listTop = this.top + PADDING - (int) this.amountScrolled;
164 drawEntries(mouseX, mouseY, listTop);
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();
176 int maxScroll = this.getMaxScroll();
178 drawScrollBar(maxScroll);
181 RenderHelper.enableTexture();
182 RenderHelper.shadeModelFlat();
183 RenderHelper.enableAlphaTest();
184 RenderHelper.disableBlend();
187 private void drawListBackground() {
188 this.minecraft.getTextureManager().bindTexture(AbstractGui.BACKGROUND_LOCATION);
189 Renderer.startTextured()
190 .setColor(32, 32, 32)
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)
199 private void drawEntries(int mouseX, int mouseY, int top) {
200 for (ControlListEntry entry : this.entries) {
201 if (!entry.isVisible()) continue;
203 entry.setX(this.listLeft);
206 int height = entry.getControlHeight();
207 drawEntry(mouseX, mouseY, top, entry, height);
212 protected void drawEntry(int mouseX, int mouseY, int top, ControlListEntry entry, int height) {
213 entry.render(mouseX, mouseY);
216 private void overlayBackground(int top, int bottom) {
217 this.minecraft.getTextureManager().bindTexture(AbstractGui.BACKGROUND_LOCATION);
218 Renderer.startTextured()
219 .setColor(64, 64, 64)
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)
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;
235 Renderer.startTextured()
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)
243 Renderer.startTextured()
244 .setColor(128, 128, 128)
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)
252 Renderer.startTextured()
253 .setColor(192, 192, 192)
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)
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)
267 .addPoint(this.width, this.top, 0.0D, 1.0D, 0.0D)
268 .addPoint(0, this.top, 0.0D, 0.0D, 0.0D)
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)
274 .addPoint(0, this.bottom, 0.0D, 0.0D, 1.0D)
275 .addPoint(this.width, this.bottom, 0.0D, 1.0D, 1.0D)
279 ControlList section(String title, CreateControl... createControls) {
280 this.add(new ControlListSection(title, createControls));
284 void setTransparentBackground() {
285 this.transparentBackground = true;
289 public List<? extends IControl> controls() {
294 public IControl getFocused() {
299 public void setFocused(IControl control) {
300 this.focused = control;
304 public boolean isDragging() {
309 public void setDragging(boolean dragging) {
310 this.isDragging = dragging;