]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/gui/SelectableControlList.java
Fully support keyboard nav in gui
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / gui / SelectableControlList.java
1 package com.irtimaled.bbor.client.gui;
2
3 import com.irtimaled.bbor.client.renderers.Renderer;
4 import org.lwjgl.opengl.GL11;
5
6 public class SelectableControlList extends ControlList {
7     private final int listRight;
8
9     private int selectedElement;
10     private boolean isFocused;
11
12     SelectableControlList(int width, int height, int top, int bottom) {
13         super(width, height, top, bottom);
14         this.listRight = this.listLeft + CONTROLS_WIDTH;
15         this.selectedElement = -1;
16     }
17
18     @Override
19     public void filter(String lowerValue) {
20         super.filter(lowerValue);
21         if (selectedElement >= 0) {
22             if (selectNextVisibleElement(true, selectedElement) ||
23                     selectNextVisibleElement(true, 0)) return;
24             selectedElement = -1;
25         }
26     }
27
28     @Override
29     public boolean keyPressed(int key, int scanCode, int modifiers) {
30         if (key != 264 && key != 265 && key != 257) return false;
31
32         if (key == 257) {
33             if (selectedElement >= 0) {
34                 getSelectedEntry().done();
35                 return true;
36             }
37             return false;
38         }
39
40         boolean moveForward = key == 264;
41         if (selectedElement >= 0) {
42             int newIndex = selectedElement + (moveForward ? 1 : 0);
43             if (selectNextVisibleElement(moveForward, newIndex)) return true;
44         }
45         if (selectNextVisibleElement(moveForward, moveForward ? 0 : entries.size())) return true;
46
47         this.selectedElement = -1;
48         return false;
49     }
50
51     private boolean selectNextVisibleElement(boolean moveForward, int index) {
52         return ListHelper.findNextMatch(entries, index, moveForward, ControlListEntry::isVisible,
53                 entry -> this.selectedElement = entry.index);
54     }
55
56     ControlListEntry getSelectedEntry() {
57         return this.selectedElement >= 0 && this.selectedElement < this.entries.size() ?
58                 this.entries.get(this.selectedElement) :
59                 null;
60     }
61
62     void setSelectedEntry(ControlListEntry entry) {
63         if (entry != null) {
64             this.selectedElement = entry.index;
65         } else {
66             this.selectedElement = -1;
67         }
68     }
69
70     @Override
71     public boolean changeFocus(boolean moveForward) {
72         if (contentHeight == PADDING) return false;
73
74         isFocused = !isFocused;
75         if (getSelectedEntry() == null && this.entries.size() > 0) {
76             setSelectedEntry(this.entries.get(0));
77         }
78         return isFocused;
79     }
80
81     @Override
82     protected void drawEntry(int mouseX, int mouseY, int top, ControlListEntry entry, int height) {
83         if (this.selectedElement == entry.index) {
84             GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
85             GL11.glDisable(GL11.GL_TEXTURE_2D);
86             int color = this.isFocused ? 255 : 128;
87             Renderer.startTextured()
88                     .setAlpha(255)
89                     .setColor(color, color, color)
90                     .addPoint((double) this.listLeft - 2, (double) (top + height) - 2, 0.0D, 0.0D, 1.0D)
91                     .addPoint((double) this.listRight + 2, (double) (top + height) - 2, 0.0D, 1.0D, 1.0D)
92                     .addPoint((double) this.listRight + 2, top - 2, 0.0D, 1.0D, 0.0D)
93                     .addPoint((double) this.listLeft - 2, top - 2, 0.0D, 0.0D, 0.0D)
94                     .setColor(0, 0, 0)
95                     .addPoint(this.listLeft - 1, (double) (top + height) - 3, 0.0D, 0.0D, 1.0D)
96                     .addPoint(this.listRight + 1, (double) (top + height) - 3, 0.0D, 1.0D, 1.0D)
97                     .addPoint(this.listRight + 1, top - 1, 0.0D, 1.0D, 0.0D)
98                     .addPoint(this.listLeft - 1, top - 1, 0.0D, 0.0D, 0.0D)
99                     .render();
100             GL11.glEnable(GL11.GL_TEXTURE_2D);
101         }
102         super.drawEntry(mouseX, mouseY, top, entry, height);
103     }
104
105     @Override
106     public void clearFocus() {
107         this.isFocused = false;
108     }
109 }