Skip to content

Commit cd8d02c

Browse files
committed
Create base widget class
1 parent d9e653e commit cd8d02c

File tree

5 files changed

+98
-61
lines changed

5 files changed

+98
-61
lines changed

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

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

33
import btw.lowercase.optiboxes.OptiBoxesClient;
4+
import btw.lowercase.optiboxes.screen.widget.Gidget;
5+
import btw.lowercase.optiboxes.screen.widget.SimpleButton;
46
import btw.lowercase.optiboxes.skybox.OptiFineSkybox;
57
import net.minecraft.client.gui.GuiGraphics;
68
import net.minecraft.client.gui.components.Button;
@@ -18,33 +20,33 @@
1820
public class OptiboxesDebugScreen extends Screen {
1921
private final Screen parent;
2022
private final List<OptiFineSkybox> skyboxes;
21-
private final List<SimpleButton> buttons;
23+
private final List<Gidget> gidgets;
2224

2325
private static final Identifier SKYBOX_LOCATION = OptiBoxesClient.locationOrNull("skybox");
2426

2527
public OptiboxesDebugScreen(Screen parent, List<OptiFineSkybox> skyboxes) {
2628
super(Component.empty());
2729
this.parent = parent;
2830
this.skyboxes = skyboxes;
29-
this.buttons = new ArrayList<>();
31+
this.gidgets = new ArrayList<>();
3032
}
3133

3234
@Override
3335
protected void init() {
3436
int index = 0;
35-
this.buttons.clear();
37+
this.gidgets.clear();
3638
this.addRenderableWidget(Button.builder(CommonComponents.GUI_DONE, (button) -> this.onClose())
3739
.pos((this.width / 2) - (Button.DEFAULT_WIDTH / 2), this.height - Button.DEFAULT_HEIGHT - 4)
3840
.build());
3941
for (OptiFineSkybox skybox : this.skyboxes) {
4042
SimpleButton.DataHolder<OptiFineSkybox> button = new SimpleButton.DataHolder<>(
4143
Component.literal(skybox.getWorldResourceKey().identifier().toString()),
42-
(this.width / 2) - (SimpleButton.WIDTH / 2),
43-
30 + ((SimpleButton.HEIGHT + 8) * index),
44+
(this.width / 2) - (SimpleButton.DEFAULT_WIDTH / 2),
45+
30 + ((SimpleButton.DEFAULT_HEIGHT + 8) * index),
4446
this::buttonClicked
4547
);
4648
button.put(SKYBOX_LOCATION, skybox);
47-
this.buttons.add(button);
49+
this.gidgets.add(button);
4850
index++;
4951
}
5052
}
@@ -53,15 +55,17 @@ protected void init() {
5355
public void render(@NotNull GuiGraphics guiGraphics, int mouseX, int mouseY, float delta) {
5456
super.render(guiGraphics, mouseX, mouseY, delta);
5557
guiGraphics.drawCenteredString(this.font, this.skyboxes.isEmpty() ? "No skies enabled..." : this.skyboxes.size() + " Total Active Skyboxes", this.width / 2, 12, ARGB.white(1.0F));
56-
for (SimpleButton button : this.buttons) {
57-
button.render(guiGraphics, mouseX, mouseY);
58+
for (Gidget gidget : this.gidgets) {
59+
if (gidget instanceof SimpleButton button) {
60+
button.render(guiGraphics, mouseX, mouseY);
61+
}
5862
}
5963
}
6064

6165
@Override
6266
public boolean mouseClicked(@NotNull MouseButtonEvent event, boolean isDoubleClick) {
63-
for (SimpleButton button : this.buttons) {
64-
if (button.isInside(event.x(), event.y())) {
67+
for (Gidget gidget : this.gidgets) {
68+
if (gidget instanceof SimpleButton button && button.isInside(event.x(), event.y())) {
6569
button.onClick().accept(button);
6670
}
6771
}

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
public class SkyLayerInfoScreen extends Screen {
1313
private final Screen parent;
1414
private final OptiFineSkyLayer skyLayer;
15+
private final int index;
1516

16-
public SkyLayerInfoScreen(Screen parent, OptiFineSkyLayer skyLayer) {
17+
public SkyLayerInfoScreen(Screen parent, OptiFineSkyLayer skyLayer, int index) {
1718
super(Component.empty());
1819
this.parent = parent;
1920
this.skyLayer = skyLayer;
21+
this.index = index;
2022
}
2123

2224
@Override
@@ -29,24 +31,22 @@ protected void init() {
2931
@Override
3032
public void render(@NotNull GuiGraphics guiGraphics, int mouseX, int mouseY, float delta) {
3133
super.render(guiGraphics, mouseX, mouseY, delta);
32-
guiGraphics.drawCenteredString(this.font, "TODO", this.width / 2, 12, ARGB.white(1.0F));
34+
guiGraphics.drawCenteredString(this.font, Component.literal(this.index + " - " + this.skyLayer.source().toString()), this.width / 2, 12, ARGB.white(1.0F));
35+
// context.getSource().sendFeedback(Component.literal(" Rotate: " + skyLayer.rotate()));
36+
// context.getSource().sendFeedback(Component.literal(" Axis: " + skyLayer.axis()));
37+
// context.getSource().sendFeedback(Component.literal(" Blend: " + skyLayer.blend()));
38+
// context.getSource().sendFeedback(Component.literal(" Speed: " + skyLayer.speed()));
39+
// context.getSource().sendFeedback(Component.literal(" Transition: " + skyLayer.transition()));
40+
// context.getSource().sendFeedback(Component.literal(" Fade: " + skyLayer.fade()));
41+
// context.getSource().sendFeedback(Component.literal(" Loop: " + skyLayer.loop()));
42+
// context.getSource().sendFeedback(Component.literal(" Include Biome: " + skyLayer.biomeInclusion()));
43+
// context.getSource().sendFeedback(Component.literal(" Biomes: " + skyLayer.biomes()));
44+
// context.getSource().sendFeedback(Component.literal(" Heights: " + skyLayer.heights()));
45+
// context.getSource().sendFeedback(Component.literal(" Weather Conditions: " + skyLayer.weatherConditions()));
3346
}
3447

3548
@Override
3649
public void onClose() {
3750
this.minecraft.setScreen(this.parent);
3851
}
39-
}
40-
41-
// context.getSource().sendFeedback(Component.literal(" #" + index + " (" + skyLayer.source() + ")"));
42-
// context.getSource().sendFeedback(Component.literal(" Rotate: " + skyLayer.rotate()));
43-
// context.getSource().sendFeedback(Component.literal(" Axis: " + skyLayer.axis()));
44-
// context.getSource().sendFeedback(Component.literal(" Blend: " + skyLayer.blend()));
45-
// context.getSource().sendFeedback(Component.literal(" Speed: " + skyLayer.speed()));
46-
// context.getSource().sendFeedback(Component.literal(" Transition: " + skyLayer.transition()));
47-
// context.getSource().sendFeedback(Component.literal(" Fade: " + skyLayer.fade()));
48-
// context.getSource().sendFeedback(Component.literal(" Loop: " + skyLayer.loop()));
49-
// context.getSource().sendFeedback(Component.literal(" Include Biome: " + skyLayer.biomeInclusion()));
50-
// context.getSource().sendFeedback(Component.literal(" Biomes: " + skyLayer.biomes()));
51-
// context.getSource().sendFeedback(Component.literal(" Heights: " + skyLayer.heights()));
52-
// context.getSource().sendFeedback(Component.literal(" Weather Conditions: " + skyLayer.weatherConditions()));
52+
}

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

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

33
import btw.lowercase.optiboxes.OptiBoxesClient;
4+
import btw.lowercase.optiboxes.screen.widget.Gidget;
5+
import btw.lowercase.optiboxes.screen.widget.SimpleButton;
46
import btw.lowercase.optiboxes.skybox.OptiFineSkyLayer;
57
import btw.lowercase.optiboxes.skybox.OptiFineSkybox;
68
import net.minecraft.client.gui.GuiGraphics;
@@ -19,33 +21,35 @@
1921
public class SkyboxInfoScreen extends Screen {
2022
private final Screen parent;
2123
private final OptiFineSkybox skybox;
22-
private final List<SimpleButton> buttons;
24+
private final List<Gidget> gidgets;
2325

2426
private static final Identifier SKYLAYER_LOCATION = OptiBoxesClient.locationOrNull("skylayer");
27+
private static final Identifier INDEX_LOCATION = OptiBoxesClient.locationOrNull("index");
2528

2629
public SkyboxInfoScreen(Screen parent, OptiFineSkybox skybox) {
2730
super(Component.empty());
2831
this.parent = parent;
2932
this.skybox = skybox;
30-
this.buttons = new ArrayList<>();
33+
this.gidgets = new ArrayList<>();
3134
}
3235

3336
@Override
3437
protected void init() {
3538
int index = 0;
36-
this.buttons.clear();
39+
this.gidgets.clear();
3740
this.addRenderableWidget(Button.builder(CommonComponents.GUI_BACK, (button) -> this.onClose())
3841
.pos((this.width / 2) - (Button.DEFAULT_WIDTH / 2), this.height - Button.DEFAULT_HEIGHT - 4)
3942
.build());
4043
for (OptiFineSkyLayer layer : this.skybox.getLayers()) {
41-
SimpleButton.DataHolder<OptiFineSkyLayer> button = new SimpleButton.DataHolder<>(
42-
Component.literal(index + " - " + layer.source().getPath()),
43-
(this.width / 2) - (SimpleButton.WIDTH / 2),
44-
36 + ((SimpleButton.HEIGHT + 8) * index),
44+
SimpleButton.DataHolder<Object> button = new SimpleButton.DataHolder<>(
45+
Component.literal(index + " - " + layer.source()),
46+
(this.width / 2) - (SimpleButton.DEFAULT_WIDTH / 2),
47+
36 + ((SimpleButton.DEFAULT_HEIGHT + 8) * index),
4548
this::buttonClicked
4649
);
4750
button.put(SKYLAYER_LOCATION, layer);
48-
this.buttons.add(button);
51+
button.put(INDEX_LOCATION, index);
52+
this.gidgets.add(button);
4953
index++;
5054
}
5155
}
@@ -56,15 +60,17 @@ public void render(@NotNull GuiGraphics guiGraphics, int mouseX, int mouseY, flo
5660
guiGraphics.drawCenteredString(this.font, this.skybox.getWorldResourceKey().identifier().toString(), this.width / 2, 12, ARGB.white(1.0F));
5761
guiGraphics.drawCenteredString(this.font, this.skybox.getLayers().size() + " layers", this.width / 2, 12 + this.font.lineHeight, ARGB.white(1.0F));
5862
// TODO: isActive | parent button color
59-
for (SimpleButton button : this.buttons) {
60-
button.render(guiGraphics, mouseX, mouseY);
63+
for (Gidget gidget : this.gidgets) {
64+
if (gidget instanceof SimpleButton button) {
65+
button.render(guiGraphics, mouseX, mouseY);
66+
}
6167
}
6268
}
6369

6470
@Override
6571
public boolean mouseClicked(@NotNull MouseButtonEvent event, boolean isDoubleClick) {
66-
for (SimpleButton button : this.buttons) {
67-
if (button.isInside(event.x(), event.y())) {
72+
for (Gidget gidget : this.gidgets) {
73+
if (gidget instanceof SimpleButton button && button.isInside(event.x(), event.y())) {
6874
button.onClick().accept(button);
6975
}
7076
}
@@ -79,7 +85,9 @@ public void onClose() {
7985

8086
private void buttonClicked(SimpleButton button) {
8187
if (button instanceof SimpleButton.DataHolder<?> holder) {
82-
this.minecraft.setScreen(new SkyLayerInfoScreen(this, (OptiFineSkyLayer) holder.get(SKYLAYER_LOCATION)));
88+
OptiFineSkyLayer skyLayer = (OptiFineSkyLayer) holder.get(SKYLAYER_LOCATION);
89+
int index = (Integer) holder.get(INDEX_LOCATION);
90+
this.minecraft.setScreen(new SkyLayerInfoScreen(this, skyLayer, index));
8391
}
8492
}
8593
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package btw.lowercase.optiboxes.screen.widget;
2+
3+
import net.minecraft.client.gui.GuiGraphics;
4+
5+
public abstract class Gidget {
6+
protected final int x;
7+
protected final int y;
8+
protected final int width;
9+
protected final int height;
10+
11+
public Gidget(int x, int y, int width, int height) {
12+
this.x = x;
13+
this.y = y;
14+
this.width = width;
15+
this.height = height;
16+
}
17+
18+
public abstract void render(GuiGraphics guiGraphics, int mouseX, int mouseY);
19+
20+
public boolean isInside(double x, double y) {
21+
return (x >= this.x && x <= this.x + this.width) && (y >= this.y && y <= this.y + this.height);
22+
}
23+
24+
public int x() {
25+
return this.x;
26+
}
27+
28+
public int y() {
29+
return this.y;
30+
}
31+
32+
public int width() {
33+
return this.width;
34+
}
35+
36+
public int height() {
37+
return this.height;
38+
}
39+
}

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

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

33
import net.minecraft.client.Minecraft;
44
import net.minecraft.client.gui.Font;
@@ -10,47 +10,33 @@
1010
import java.util.Map;
1111
import java.util.function.Consumer;
1212

13-
public class SimpleButton {
14-
public static final int WIDTH = 200;
15-
public static final int HEIGHT = 20;
13+
public class SimpleButton extends Gidget {
14+
public static final int DEFAULT_WIDTH = 200;
15+
public static final int DEFAULT_HEIGHT = 20;
1616

1717
private final Component text;
18-
private final int x;
19-
private final int y;
2018
private final Consumer<SimpleButton> onClick;
2119

2220
public SimpleButton(Component text, int x, int y, Consumer<SimpleButton> onClick) {
21+
super(x, y, DEFAULT_WIDTH, DEFAULT_HEIGHT);
2322
this.text = text;
24-
this.x = x;
25-
this.y = y;
2623
this.onClick = onClick;
2724
}
2825

26+
@Override
2927
public void render(GuiGraphics guiGraphics, int mouseX, int mouseY) {
3028
boolean hovered = this.isInside(mouseX, mouseY);
31-
guiGraphics.fill(this.x, this.y, this.x + WIDTH, this.y + HEIGHT, hovered ? 0xFF00FF00 : 0xFF00FFFF);
29+
guiGraphics.fill(this.x, this.y, this.x + this.width, this.y + this.height, hovered ? 0xFF00FF00 : 0xFF00FFFF);
3230

3331
Font font = Minecraft.getInstance().font;
3432
int textWidth = font.width(this.text.getString());
35-
guiGraphics.drawString(font, this.text, this.x + ((WIDTH / 2) - (textWidth / 2)), this.y + ((HEIGHT / 2) - (font.lineHeight / 2)), 0xFFFFFFFF);
36-
}
37-
38-
public boolean isInside(double x, double y) {
39-
return (x >= this.x && x <= this.x + WIDTH) && (y >= this.y && y <= this.y + HEIGHT);
33+
guiGraphics.drawString(font, this.text, this.x + ((this.width / 2) - (textWidth / 2)), this.y + ((this.height / 2) - (font.lineHeight / 2)), 0xFFFFFFFF);
4034
}
4135

4236
public Component text() {
4337
return this.text;
4438
}
4539

46-
public int x() {
47-
return this.x;
48-
}
49-
50-
public int y() {
51-
return this.y;
52-
}
53-
5440
public Consumer<SimpleButton> onClick() {
5541
return this.onClick;
5642
}

0 commit comments

Comments
 (0)