]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/gui/ControlListSection.java
Setup for 1.16.3 Fabric
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / gui / ControlListSection.java
1 package com.irtimaled.bbor.client.gui;
2
3 import net.minecraft.client.MinecraftClient;
4 import net.minecraft.client.util.math.MatrixStack;
5
6 import java.util.ArrayList;
7 import java.util.List;
8
9 public class ControlListSection extends ControlListEntry implements IControlSet {
10     private static final int TITLE_HEIGHT = 16;
11     private final String title;
12     private final List<AbstractControl> controls = new ArrayList<>();
13     private final MinecraftClient minecraft = MinecraftClient.getInstance();
14     private final int titleHeight;
15     private int height;
16     private IControl focused;
17     private boolean dragging;
18
19     ControlListSection(String title, CreateControl... createControls) {
20         this.title = title;
21         this.titleHeight = title != null ? TITLE_HEIGHT : 0;
22         this.height = titleHeight;
23
24         int columnCount = columnCount();
25         int controlWidth = (ControlList.CONTROLS_WIDTH - ((columnCount - 1) * 4)) / columnCount;
26
27         int column = 0;
28         for (CreateControl createControl : createControls) {
29             AbstractControl control = createControl.create(controlWidth);
30             if (control == null) continue;
31
32             this.controls.add(control);
33             if (column == 0) {
34                 this.height += control.getControlHeight();
35             }
36             column = (column + 1) % columnCount;
37         }
38     }
39
40     private int columnCount() {
41         switch (minecraft.getLanguageManager().getLanguage().getCode()) {
42             case "en_au":
43             case "en_us":
44             case "en_gb":
45                 return 3;
46         }
47         return 2;
48     }
49
50     @Override
51     public void render(MatrixStack matrixStack, int mouseX, int mouseY) {
52         int x = this.getX();
53         int y = this.getY();
54         int top = y;
55         if (this.title != null) {
56             this.minecraft.textRenderer.draw(matrixStack, this.title, x + 4, y + ((TITLE_HEIGHT - this.minecraft.textRenderer.fontHeight) / 1.5f), 16777215);
57             top += titleHeight;
58         }
59
60         int left = 0;
61         int height = 0;
62         for (AbstractControl control : controls) {
63             if (!control.isVisible()) continue;
64
65             control.setX(left + x);
66             control.setY(top);
67             control.render(matrixStack, mouseX, mouseY);
68             if (left == 0) {
69                 height = control.getControlHeight();
70             }
71             left += control.getControlWidth();
72             if (left >= ControlList.CONTROLS_WIDTH) {
73                 left = 0;
74                 top += height;
75             }
76         }
77     }
78
79     @Override
80     public void clearFocus() {
81         IControlSet.super.clearFocus();
82     }
83
84     @Override
85     public int getControlHeight() {
86         return this.height;
87     }
88
89     public int getControlWidth() {
90         return ControlList.CONTROLS_WIDTH;
91     }
92
93     public void filter(String lowerValue) {
94         if (matchesTitle(lowerValue)) lowerValue = "";
95
96         int height = 0;
97         int left = 0;
98         for (AbstractControl entry : controls) {
99             entry.filter(lowerValue);
100             if (entry.isVisible()) {
101                 if (left == 0)
102                     height += entry.getControlHeight();
103                 left += entry.getControlWidth();
104                 if (left >= getControlWidth()) {
105                     left = 0;
106                 }
107             } else if (entry == focused) {
108                 entry.clearFocus();
109                 focused = null;
110             }
111         }
112         this.height = height + titleHeight;
113         this.setVisible(height > 0);
114     }
115
116     private boolean matchesTitle(String lowerValue) {
117         if (this.title == null) return false;
118
119         String lowerString = this.title.toLowerCase();
120         return lowerString.startsWith(lowerValue) ||
121                 lowerString.contains(" " + lowerValue);
122     }
123
124     @Override
125     public List<? extends IControl> controls() {
126         return this.controls;
127     }
128
129     @Override
130     public IControl getFocused() {
131         return this.focused;
132     }
133
134     @Override
135     public void setFocused(IControl focused) {
136         this.focused = focused;
137     }
138
139     @Override
140     public boolean isDragging() {
141         return dragging;
142     }
143
144     @Override
145     public void setDragging(boolean dragging) {
146         this.dragging = dragging;
147     }
148 }