]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/blob - src/main/java/com/irtimaled/bbor/client/gui/ListScreen.java
447b9cb12c219eb28430f163c2158e79cb584f91
[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.Versions;
4 import com.irtimaled.bbor.client.interop.ClientInterop;
5 import com.mojang.blaze3d.systems.RenderSystem;
6 import net.minecraft.client.gui.Element;
7 import net.minecraft.client.gui.screen.Screen;
8 import net.minecraft.client.gui.widget.ButtonWidget;
9 import net.minecraft.client.util.math.MatrixStack;
10 import net.minecraft.text.LiteralText;
11 import net.minecraft.text.TranslatableText;
12
13 import java.util.List;
14
15 public abstract class ListScreen extends Screen {
16     private final Screen lastScreen;
17     private static final String version = Versions.build;
18
19     private ButtonWidget doneButton;
20     private ControlList controlList;
21     private SearchField searchField;
22
23     ListScreen(Screen lastScreen) {
24         super(new LiteralText("Bounding Box Outline Reloaded"));
25         this.lastScreen = lastScreen;
26     }
27
28     ListScreen() {
29         this(null);
30     }
31
32     protected void onDoneClicked() {
33         ClientInterop.displayScreen(lastScreen);
34     }
35
36     @Override
37     protected void init() {
38         this.controlList = this.buildList(48, this.height - 28);
39         this.searchField = new SearchField(this.textRenderer, this.width / 2 - 100, 22, 200, 20, this.controlList);
40         this.doneButton = new ButtonWidget(this.width / 2 - 100, this.height - 24, 200, 20, new TranslatableText("gui.done"), buttonWidget -> onDoneClicked());
41
42         this.addDrawableChild(this.searchField);
43         ((List<Element>) this.children()).add(this.controlList);
44         this.addDrawableChild(this.doneButton);
45     }
46
47     protected abstract ControlList buildList(int top, int bottom);
48
49     @Override
50     public void render(MatrixStack matrixStack, int mouseX, int mouseY, float unknown) {
51         render(matrixStack, mouseX, mouseY);
52     }
53
54     protected void render(MatrixStack matrixStack, int mouseX, int mouseY) {
55         RenderSystem.assertOnRenderThread();
56         this.renderBackground(matrixStack);
57         this.controlList.render(matrixStack, mouseX, mouseY);
58
59         this.drawCenteredText(matrixStack, this.textRenderer, this.title.asString(), this.width / 2, 8, 16777215);
60         this.searchField.render(matrixStack, mouseX, mouseY);
61         this.doneButton.render(matrixStack, mouseX, mouseY, 0f);
62
63         int left = this.width - this.textRenderer.getWidth(version) - 2;
64         int top = this.height - 10;
65         this.drawStringWithShadow(matrixStack, this.textRenderer, version, left, top, -10658467);
66     }
67
68     @Override
69     public void tick() {
70         this.searchField.tick();
71     }
72
73     @Override
74     public boolean keyPressed(int key, int scanCode, int modifiers) {
75         return super.keyPressed(key, scanCode, modifiers) || this.searchField.keyPressed(key, scanCode, modifiers);
76     }
77
78     @Override
79     public boolean charTyped(char character, int modifiers) {
80         return this.searchField.charTyped(character, modifiers);
81     }
82
83     @Override
84     public boolean mouseScrolled(double mouseX, double mouseY, double scrollAmount) {
85         return this.controlList.mouseScrolled(mouseX, mouseY, scrollAmount);
86     }
87
88     @Override
89     public void removed() {
90         this.controlList.close();
91     }
92
93     protected void setCanExit(boolean canExit) {
94         this.doneButton.active = canExit;
95     }
96
97     @Override
98     public boolean mouseClicked(double mouseX, double mouseY, int button) {
99         for (Element control : this.children()) {
100             if (control.mouseClicked(mouseX, mouseY, button)) {
101                 Element focused = getFocused();
102                 if (focused instanceof IFocusableControl && focused != control) {
103                     ((IFocusableControl) focused).clearFocus();
104                 }
105                 this.setFocused(control);
106                 if (button == 0) this.setDragging(true);
107                 return true;
108             }
109         }
110         return false;
111     }
112 }