1 package com.irtimaled.bbor.client.gui;
3 import com.irtimaled.bbor.client.renderers.RenderHelper;
4 import com.irtimaled.bbor.client.renderers.Renderer;
6 public class SelectableControlList extends ControlList {
7 private final int listRight;
9 private int selectedElement;
10 private boolean isFocused;
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;
19 public void filter(String lowerValue) {
20 super.filter(lowerValue);
21 if (selectedElement >= 0) {
22 if (selectNextVisibleElement(true, selectedElement) ||
23 selectNextVisibleElement(true, 0)) return;
29 public boolean keyPressed(int key, int scanCode, int modifiers) {
30 if (key != 264 && key != 265 && key != 257) return false;
33 if (selectedElement >= 0) {
34 getSelectedEntry().done();
40 boolean moveForward = key == 264;
41 if (selectedElement >= 0) {
42 int newIndex = selectedElement + (moveForward ? 1 : 0);
43 if (selectNextVisibleElement(moveForward, newIndex)) return true;
45 if (selectNextVisibleElement(moveForward, moveForward ? 0 : entries.size())) return true;
47 this.selectedElement = -1;
51 private boolean selectNextVisibleElement(boolean moveForward, int index) {
52 return ListHelper.findNextMatch(entries, index, moveForward, ControlListEntry::isVisible,
53 entry -> this.selectedElement = entry.index);
56 ControlListEntry getSelectedEntry() {
57 return this.selectedElement >= 0 && this.selectedElement < this.entries.size() ?
58 this.entries.get(this.selectedElement) :
62 void setSelectedEntry(ControlListEntry entry) {
64 this.selectedElement = entry.index;
66 this.selectedElement = -1;
71 public boolean changeFocus(boolean moveForward) {
72 if (contentHeight == PADDING) return false;
74 isFocused = !isFocused;
75 if (getSelectedEntry() == null && this.entries.size() > 0) {
76 setSelectedEntry(this.entries.get(0));
82 protected void drawEntry(int mouseX, int mouseY, int top, ControlListEntry entry, int height) {
83 if (this.selectedElement == entry.index) {
84 RenderHelper.disableTexture();
85 int color = this.isFocused ? 255 : 128;
86 Renderer.startTextured()
88 .setColor(color, color, color)
89 .addPoint((double) this.listLeft - 2, (double) (top + height) - 2, 0.0D, 0.0D, 1.0D)
90 .addPoint((double) this.listRight + 2, (double) (top + height) - 2, 0.0D, 1.0D, 1.0D)
91 .addPoint((double) this.listRight + 2, top - 2, 0.0D, 1.0D, 0.0D)
92 .addPoint((double) this.listLeft - 2, top - 2, 0.0D, 0.0D, 0.0D)
94 .addPoint(this.listLeft - 1, (double) (top + height) - 3, 0.0D, 0.0D, 1.0D)
95 .addPoint(this.listRight + 1, (double) (top + height) - 3, 0.0D, 1.0D, 1.0D)
96 .addPoint(this.listRight + 1, top - 1, 0.0D, 1.0D, 0.0D)
97 .addPoint(this.listLeft - 1, top - 1, 0.0D, 0.0D, 0.0D)
99 RenderHelper.enableTexture();
101 super.drawEntry(mouseX, mouseY, top, entry, height);
105 public void clearFocus() {
106 this.isFocused = false;