1 package com.irtimaled.bbor.client.gui;
3 import com.irtimaled.bbor.client.renderers.RenderHelper;
4 import com.irtimaled.bbor.client.renderers.Renderer;
5 import net.minecraft.client.util.math.MatrixStack;
7 public class SelectableControlList extends ControlList {
8 private final int listRight;
10 private int selectedElement;
11 private boolean isFocused;
13 SelectableControlList(int width, int height, int top, int bottom) {
14 super(width, height, top, bottom);
15 this.listRight = this.listLeft + CONTROLS_WIDTH;
16 this.selectedElement = -1;
20 public void filter(String lowerValue) {
21 super.filter(lowerValue);
22 if (selectedElement >= 0) {
23 if (selectNextVisibleElement(true, selectedElement) ||
24 selectNextVisibleElement(true, 0)) return;
30 public boolean keyPressed(int key, int scanCode, int modifiers) {
31 if (key != 264 && key != 265 && key != 257) return false;
34 if (selectedElement >= 0) {
35 getSelectedEntry().done();
41 boolean moveForward = key == 264;
42 if (selectedElement >= 0) {
43 int newIndex = selectedElement + (moveForward ? 1 : 0);
44 if (selectNextVisibleElement(moveForward, newIndex)) return true;
46 if (selectNextVisibleElement(moveForward, moveForward ? 0 : entries.size())) return true;
48 this.selectedElement = -1;
52 private boolean selectNextVisibleElement(boolean moveForward, int index) {
53 return ListHelper.findNextMatch(entries, index, moveForward, ControlListEntry::isVisible,
54 entry -> this.selectedElement = entry.index);
57 ControlListEntry getSelectedEntry() {
58 return this.selectedElement >= 0 && this.selectedElement < this.entries.size() ?
59 this.entries.get(this.selectedElement) :
63 void setSelectedEntry(ControlListEntry entry) {
65 this.selectedElement = entry.index;
67 this.selectedElement = -1;
72 public boolean changeFocus(boolean moveForward) {
73 if (contentHeight == PADDING) return false;
75 isFocused = !isFocused;
76 if (getSelectedEntry() == null && this.entries.size() > 0) {
77 setSelectedEntry(this.entries.get(0));
83 protected void drawEntry(MatrixStack matrixStack, int mouseX, int mouseY, int top, ControlListEntry entry, int height) {
84 if (this.selectedElement == entry.index) {
85 RenderHelper.disableTexture();
86 RenderHelper.resetShader();
87 int color = this.isFocused ? 255 : 128;
90 .setColor(color, color, color)
91 .addPoint((double) this.listLeft - 2, (double) (top + height) - 2, 0.0D)
92 .addPoint((double) this.listRight + 2, (double) (top + height) - 2, 0.0D)
93 .addPoint((double) this.listRight + 2, top - 2, 0.0D)
94 .addPoint((double) this.listLeft - 2, top - 2, 0.0D)
96 .addPoint(this.listLeft - 1, (double) (top + height) - 3, 0.0D)
97 .addPoint(this.listRight + 1, (double) (top + height) - 3, 0.0D)
98 .addPoint(this.listRight + 1, top - 1, 0.0D)
99 .addPoint(this.listLeft - 1, top - 1, 0.0D)
101 RenderHelper.enableTexture();
103 super.drawEntry(matrixStack, mouseX, mouseY, top, entry, height);
107 public void clearFocus() {
108 this.isFocused = false;