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