Skip to content

Commit cc3ee08

Browse files
committed
Add ScrollableList
1 parent 977242e commit cc3ee08

File tree

6 files changed

+84
-17
lines changed

6 files changed

+84
-17
lines changed

src/main/java/btw/lowercase/optiboxes/screen/DebugScreen.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,7 @@ public boolean mouseClicked(@NotNull MouseButtonEvent event, boolean isDoubleCli
4747
@Override
4848
public void mouseMoved(double mouseX, double mouseY) {
4949
for (Gidget gidget : this.gidgets) {
50-
if (gidget.isInside(mouseX, mouseY)) {
51-
gidget.onMouseEnter(mouseX, mouseY);
52-
} else if (gidget.hovered()) {
53-
gidget.onMouseLeave(mouseX, mouseY);
54-
}
50+
gidget.onMouseMove(mouseX, mouseY);
5551
}
5652

5753
super.mouseMoved(mouseX, mouseY);

src/main/java/btw/lowercase/optiboxes/screen/SkyLayerListScreen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ protected void init() {
2929
this.gidgets.add(new SimpleButton(
3030
Component.literal(index + " - " + skyLayer.source()),
3131
(this.width / 2) - (SimpleButton.DEFAULT_WIDTH / 2),
32-
36 + ((SimpleButton.DEFAULT_HEIGHT + 8) * index),
32+
36 + ((SimpleButton.DEFAULT_HEIGHT + SimpleButton.DEFAULT_PADDING) * index),
3333
(button) -> this.minecraft.setScreen(new SkyLayerInfoScreen(this, skyLayer, cidx))
3434
));
3535
index++;

src/main/java/btw/lowercase/optiboxes/screen/SkyboxListScreen.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
package btw.lowercase.optiboxes.screen;
22

3+
import btw.lowercase.optiboxes.screen.widget.Gidget;
4+
import btw.lowercase.optiboxes.screen.widget.ScrollableList;
35
import btw.lowercase.optiboxes.screen.widget.SimpleButton;
46
import btw.lowercase.optiboxes.screen.widget.Text;
57
import btw.lowercase.optiboxes.skybox.OptiFineSkybox;
68
import net.minecraft.client.gui.screens.Screen;
79
import net.minecraft.network.chat.CommonComponents;
810
import net.minecraft.network.chat.Component;
911

12+
import java.util.ArrayList;
1013
import java.util.List;
1114

1215
public class SkyboxListScreen extends DebugScreen {
1316
private final List<OptiFineSkybox> skyboxes;
17+
private ScrollableList scrollableList;
1418

1519
public SkyboxListScreen(Screen parent, List<OptiFineSkybox> skyboxes) {
1620
super(Component.literal(skyboxes.isEmpty() ? "No skies enabled..." : skyboxes.size() + " Total Active Skyboxes"), parent);
@@ -24,16 +28,18 @@ protected void init() {
2428
// TODO: isActive | button color
2529
this.gidgets.add(new Text.Builder(this.title, this.width / 2, 12).centered().build(this.font));
2630

31+
List<Gidget> scrollableListGidgets = new ArrayList<>();
2732
int index = 0;
2833
for (OptiFineSkybox skybox : this.skyboxes) {
29-
this.gidgets.add(new SimpleButton(
34+
scrollableListGidgets.add(new SimpleButton(
3035
Component.literal(skybox.getWorldResourceKey().identifier().toString()),
3136
(this.width / 2) - (SimpleButton.DEFAULT_WIDTH / 2),
32-
30 + ((SimpleButton.DEFAULT_HEIGHT + 8) * index),
37+
30 + ((SimpleButton.DEFAULT_HEIGHT + SimpleButton.DEFAULT_PADDING) * index),
3338
(button) -> this.minecraft.setScreen(new SkyLayerListScreen(this, skybox))
3439
));
3540
index++;
3641
}
42+
this.gidgets.add(new ScrollableList(scrollableListGidgets, 0, 16, this.width, this.height - 32));
3743

3844
this.gidgets.add(new SimpleButton(
3945
CommonComponents.GUI_DONE,

src/main/java/btw/lowercase/optiboxes/screen/widget/Gidget.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import net.minecraft.util.ARGB;
55

66
public abstract class Gidget {
7-
private final int x;
8-
private final int y;
9-
private final int width;
10-
private final int height;
7+
private int x;
8+
private int y;
9+
private int width;
10+
private int height;
1111
private boolean hovered = false;
1212

1313
public Gidget(int x, int y, int width, int height) {
@@ -24,11 +24,7 @@ public void renderBackground(GuiGraphics guiGraphics) {
2424
guiGraphics.fill(this.x(), this.y(), this.x() + this.width(), this.y() + this.height(), backgroundColor);
2525
}
2626

27-
public void onMouseEnter(double mouseX, double mouseY) {
28-
this.hovered = this.isInside(mouseX, mouseY);
29-
}
30-
31-
public void onMouseLeave(double mouseX, double mouseY) {
27+
public void onMouseMove(double mouseX, double mouseY) {
3228
this.hovered = this.isInside(mouseX, mouseY);
3329
}
3430

@@ -39,6 +35,16 @@ public boolean isInside(double x, double y) {
3935
return (x >= this.x && x <= this.x + this.width) && (y >= this.y && y <= this.y + this.height);
4036
}
4137

38+
public void reposition(int x, int y) {
39+
this.x = x;
40+
this.y = y;
41+
}
42+
43+
public void resize(int width, int height) {
44+
this.width = width;
45+
this.height = height;
46+
}
47+
4248
public int x() {
4349
return this.x;
4450
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package btw.lowercase.optiboxes.screen.widget;
2+
3+
import com.mojang.blaze3d.systems.RenderSystem;
4+
import net.minecraft.client.gui.GuiGraphics;
5+
6+
import java.util.List;
7+
8+
public class ScrollableList extends Gidget {
9+
private final List<Gidget> gidgets;
10+
11+
public ScrollableList(List<Gidget> gidgets, int x, int y, int width, int height) {
12+
super(x, y, width, height);
13+
this.gidgets = gidgets;
14+
for (Gidget gidget : this.gidgets) {
15+
gidget.reposition(this.x() + gidget.x(), this.y() + gidget.y());
16+
}
17+
}
18+
19+
@Override
20+
public void render(GuiGraphics guiGraphics, int mouseX, int mouseY) {
21+
this.renderBackground(guiGraphics);
22+
RenderSystem.enableScissorForRenderTypeDraws(this.x(), this.y(), this.width(), this.height());
23+
for (Gidget gidget : this.gidgets) {
24+
gidget.render(guiGraphics, mouseX, mouseY);
25+
}
26+
RenderSystem.disableScissorForRenderTypeDraws();
27+
}
28+
29+
@Override
30+
public void renderBackground(GuiGraphics guiGraphics) {
31+
32+
}
33+
34+
@Override
35+
public void onMouseMove(double mouseX, double mouseY) {
36+
super.onMouseMove(mouseX, mouseY);
37+
for (Gidget gidget : this.gidgets) {
38+
gidget.onMouseMove(mouseX, mouseY);
39+
}
40+
}
41+
42+
@Override
43+
public void onMouseClicked(double mouseX, double mouseY) {
44+
super.onMouseClicked(mouseX, mouseY);
45+
for (Gidget gidget : this.gidgets) {
46+
if (gidget.isInside(mouseX, mouseY)) {
47+
gidget.onMouseClicked(mouseX, mouseY);
48+
}
49+
}
50+
}
51+
}

src/main/java/btw/lowercase/optiboxes/screen/widget/SimpleButton.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
public class SimpleButton extends Gidget implements TextHolder {
1414
public static final int DEFAULT_WIDTH = 200;
1515
public static final int DEFAULT_HEIGHT = 20;
16+
public static final int DEFAULT_PADDING = 8;
1617

1718
private final Text text;
1819
private final Consumer<? super SimpleButton> onClick;
@@ -22,6 +23,7 @@ public SimpleButton(Component text, int x, int y, Consumer<? super SimpleButton>
2223
this.text = new Text.Builder(text, this.x() + (this.width() / 2), this.y() + (this.height() / 2))
2324
.positioned(Text.Positioned.BOTH)
2425
.build(Minecraft.getInstance().font);
26+
this.resize(Math.max(this.text.width() + DEFAULT_PADDING, DEFAULT_WIDTH), this.height());
2527
this.onClick = onClick;
2628
}
2729

@@ -37,6 +39,12 @@ public void onMouseClicked(double mouseX, double mouseY) {
3739
this.onClick.accept(this);
3840
}
3941

42+
@Override
43+
public void reposition(int x, int y) {
44+
super.reposition(x, y);
45+
this.text.reposition(this.x() + (this.width() / 2), this.y() + (this.height() / 2));
46+
}
47+
4048
@Override
4149
public Component getText() {
4250
return this.text.getText();

0 commit comments

Comments
 (0)