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