]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/gui/ListScreen.java
c8add06855ed3d3604e7d61363b7fdec311d9425
[BoundingBoxOutlineReloaded.git] / src / main / java / com / irtimaled / bbor / client / gui / ListScreen.java
1 package com.irtimaled.bbor.client.gui;
2
3 import com.irtimaled.bbor.client.interop.ClientInterop;
4 import net.minecraft.client.gui.IGuiEventListener;
5 import net.minecraft.client.gui.screen.Screen;
6 import net.minecraft.client.resources.I18n;
7 import net.minecraft.util.text.StringTextComponent;
8
9 public abstract class ListScreen extends Screen {
10     private final Screen lastScreen;
11
12     private AbstractButton doneButton;
13     private ControlList controlList;
14     private SearchField searchField;
15
16     ListScreen(Screen lastScreen) {
17         super(new StringTextComponent("Bounding Box Outline Reloaded"));
18         this.lastScreen = lastScreen;
19     }
20
21     ListScreen() {
22         this(null);
23     }
24
25     protected void onDoneClicked() {
26         ClientInterop.displayScreen(lastScreen);
27     }
28
29     @Override
30     protected void init() {
31         this.controlList = this.buildList(48, this.height - 28);
32         this.searchField = new SearchField(this.font, this.width / 2 - 100, 22, 200, 20, this.controlList);
33         this.doneButton = new AbstractButton(this.width / 2 - 100, this.height - 24, 200, I18n.format("gui.done")) {
34             @Override
35             public void onPressed() {
36                 onDoneClicked();
37             }
38         };
39
40         this.children.add(this.searchField);
41         this.children.add(this.controlList);
42         this.children.add(this.doneButton);
43     }
44
45     protected abstract ControlList buildList(int top, int bottom);
46
47     @Override
48     public void render(int mouseX, int mouseY, float unknown) {
49         render(mouseX, mouseY);
50     }
51
52     protected void render(int mouseX, int mouseY) {
53         this.controlList.render(mouseX, mouseY);
54
55         this.drawCenteredString(this.font, this.title.getUnformattedComponentText(), this.width / 2, 8, 16777215);
56         this.searchField.render(mouseX, mouseY);
57         this.doneButton.render(mouseX, mouseY);
58     }
59
60     @Override
61     public void tick() {
62         this.searchField.tick();
63     }
64
65     @Override
66     public boolean keyPressed(int key, int scanCode, int modifiers) {
67         return super.keyPressed(key, scanCode, modifiers) || this.searchField.keyPressed(key, scanCode, modifiers);
68     }
69
70     @Override
71     public boolean charTyped(char character, int modifiers) {
72         return this.searchField.charTyped(character, modifiers);
73     }
74
75     @Override
76     public boolean mouseScrolled(double mouseX, double mouseY, double scrollAmount) {
77         return this.controlList.mouseScrolled(mouseX, mouseY, scrollAmount);
78     }
79
80     @Override
81     public void removed() {
82         this.controlList.close();
83     }
84
85     protected void setCanExit(boolean canExit) {
86         this.doneButton.active = canExit;
87     }
88
89     @Override
90     public boolean mouseClicked(double mouseX, double mouseY, int button) {
91         for (IGuiEventListener control : this.children()) {
92             if (control.mouseClicked(mouseX, mouseY, button)) {
93                 IGuiEventListener focused = getFocused();
94                 if (focused instanceof IFocusableControl && focused != control) {
95                     ((IFocusableControl) focused).clearFocus();
96                 }
97                 this.setFocused(control);
98                 if (button == 0) this.setDragging(true);
99                 return true;
100             }
101         }
102         return false;
103     }
104 }