]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blobdiff - src/main/java/com/irtimaled/bbor/client/gui/ControlListSection.java
Fully support keyboard nav in gui
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / gui / ControlListSection.java
index 4135bc9beab6dce8040a04a0cc1b48a71671972a..dba872b9402869a8b4489d0fbaee5f8f9b809ea4 100644 (file)
@@ -5,14 +5,15 @@ import net.minecraft.client.Minecraft;
 import java.util.ArrayList;
 import java.util.List;
 
-public class ControlListSection extends ControlListEntry {
+public class ControlListSection extends ControlListEntry implements IControlSet {
     private static final int TITLE_HEIGHT = 16;
-    private static final int CONTROLS_WIDTH = 310;
     private final String title;
-    private final List<IControl> controls = new ArrayList<>();
+    private final List<AbstractControl> controls = new ArrayList<>();
     private final Minecraft minecraft = Minecraft.getInstance();
     private final int titleHeight;
     private int height;
+    private IControl focused;
+    private boolean dragging;
 
     ControlListSection(String title, CreateControl... createControls) {
         this.title = title;
@@ -20,12 +21,12 @@ public class ControlListSection extends ControlListEntry {
         this.height = titleHeight;
 
         int columnCount = columnCount();
-        int controlWidth = (CONTROLS_WIDTH - ((columnCount - 1) * 4)) / columnCount;
+        int controlWidth = (ControlList.CONTROLS_WIDTH - ((columnCount - 1) * 4)) / columnCount;
 
         int column = 0;
         for (CreateControl createControl : createControls) {
-            IControl control = createControl.create(0, controlWidth);
-            if(control == null) continue;
+            AbstractControl control = createControl.create(controlWidth);
+            if (control == null) continue;
 
             this.controls.add(control);
             if (column == 0) {
@@ -36,10 +37,11 @@ public class ControlListSection extends ControlListEntry {
     }
 
     private int columnCount() {
-        switch (minecraft.getLanguageManager().getCurrentLanguage().getCode()){
+        switch (minecraft.getLanguageManager().getCurrentLanguage().getCode()) {
             case "en_au":
             case "en_us":
-            case "en_gb": return 3;
+            case "en_gb":
+                return 3;
         }
         return 2;
     }
@@ -49,24 +51,24 @@ public class ControlListSection extends ControlListEntry {
         int x = this.getX();
         int y = this.getY();
         int top = y;
-        if(this.title != null) {
+        if (this.title != null) {
             this.minecraft.fontRenderer.drawString(this.title, x + 4, y + ((TITLE_HEIGHT - this.minecraft.fontRenderer.FONT_HEIGHT) / 1.5f), 16777215);
             top += titleHeight;
         }
 
         int left = 0;
         int height = 0;
-        for(IControl control : controls) {
-            if(!control.getVisible()) continue;
+        for (AbstractControl control : controls) {
+            if (!control.isVisible()) continue;
 
             control.setX(left + x);
             control.setY(top);
             control.render(mouseX, mouseY);
-            if(left == 0) {
+            if (left == 0) {
                 height = control.getControlHeight();
             }
             left += control.getControlWidth();
-            if(left >= CONTROLS_WIDTH) {
+            if (left >= ControlList.CONTROLS_WIDTH) {
                 left = 0;
                 top += height;
             }
@@ -74,49 +76,36 @@ public class ControlListSection extends ControlListEntry {
     }
 
     @Override
-    public int getControlHeight() {
-        return this.height;
-    }
-
-    @Override
-    public int getControlWidth() {
-        return CONTROLS_WIDTH;
+    public void clearFocus() {
+        IControlSet.super.clearFocus();
     }
 
     @Override
-    public boolean mouseClicked(double mouseX, double mouseY, int button) {
-        for (IControl control : controls) {
-            if (control.getVisible() && control.mouseClicked(mouseX, mouseY, button)) {
-                return true;
-            }
-        }
-        return false;
+    public int getControlHeight() {
+        return this.height;
     }
 
-    @Override
-    public boolean mouseReleased(double mouseX, double mouseY, int button) {
-        boolean result = false;
-        for (IControl control : controls) {
-            if (control.mouseReleased(mouseX, mouseY, button)) result = true;
-        }
-        return result;
+    public int getControlWidth() {
+        return ControlList.CONTROLS_WIDTH;
     }
 
-    @Override
     public void filter(String lowerValue) {
-        if(matchesTitle(lowerValue)) lowerValue = "";
+        if (matchesTitle(lowerValue)) lowerValue = "";
 
         int height = 0;
         int left = 0;
-        for (IControl entry : controls) {
+        for (AbstractControl entry : controls) {
             entry.filter(lowerValue);
-            if (entry.getVisible()) {
+            if (entry.isVisible()) {
                 if (left == 0)
                     height += entry.getControlHeight();
                 left += entry.getControlWidth();
                 if (left >= getControlWidth()) {
                     left = 0;
                 }
+            } else if (entry == focused) {
+                entry.clearFocus();
+                focused = null;
             }
         }
         this.height = height + titleHeight;
@@ -130,4 +119,29 @@ public class ControlListSection extends ControlListEntry {
         return lowerString.startsWith(lowerValue) ||
                 lowerString.contains(" " + lowerValue);
     }
+
+    @Override
+    public List<? extends IControl> controls() {
+        return this.controls;
+    }
+
+    @Override
+    public IControl getFocused() {
+        return this.focused;
+    }
+
+    @Override
+    public void setFocused(IControl focused) {
+        this.focused = focused;
+    }
+
+    @Override
+    public boolean isDragging() {
+        return dragging;
+    }
+
+    @Override
+    public void setDragging(boolean dragging) {
+        this.dragging = dragging;
+    }
 }