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