Skip to content

Commit 977242e

Browse files
committed
Adjust positioning of Text && Use Text in SimpleButton
1 parent 643389a commit 977242e

File tree

5 files changed

+71
-30
lines changed

5 files changed

+71
-30
lines changed

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

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

3-
import btw.lowercase.optiboxes.screen.widget.Clickable;
43
import btw.lowercase.optiboxes.screen.widget.Gidget;
54
import net.minecraft.client.gui.GuiGraphics;
65
import net.minecraft.client.gui.screens.Screen;
@@ -37,14 +36,27 @@ public void render(@NotNull GuiGraphics guiGraphics, int mouseX, int mouseY, flo
3736
@Override
3837
public boolean mouseClicked(@NotNull MouseButtonEvent event, boolean isDoubleClick) {
3938
for (Gidget gidget : this.gidgets) {
40-
if (gidget instanceof Clickable clickable && gidget.isInside(event.x(), event.y())) {
41-
clickable.click(event.x(), event.y());
39+
if (gidget.isInside(event.x(), event.y())) {
40+
gidget.onMouseClicked(event.x(), event.y());
4241
}
4342
}
4443

4544
return super.mouseClicked(event, isDoubleClick);
4645
}
4746

47+
@Override
48+
public void mouseMoved(double mouseX, double mouseY) {
49+
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+
}
55+
}
56+
57+
super.mouseMoved(mouseX, mouseY);
58+
}
59+
4860
@Override
4961
public void onClose() {
5062
this.minecraft.setScreen(this.parent);

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

Lines changed: 0 additions & 5 deletions
This file was deleted.

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

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

33
import net.minecraft.client.gui.GuiGraphics;
4+
import net.minecraft.util.ARGB;
45

56
public abstract class Gidget {
67
private final int x;
78
private final int y;
89
private final int width;
910
private final int height;
11+
private boolean hovered = false;
1012

1113
public Gidget(int x, int y, int width, int height) {
1214
this.x = x;
@@ -17,6 +19,22 @@ public Gidget(int x, int y, int width, int height) {
1719

1820
public abstract void render(GuiGraphics guiGraphics, int mouseX, int mouseY);
1921

22+
public void renderBackground(GuiGraphics guiGraphics) {
23+
final int backgroundColor = this.hovered ? ARGB.white(0.7F) : ARGB.white(0.58F);
24+
guiGraphics.fill(this.x(), this.y(), this.x() + this.width(), this.y() + this.height(), backgroundColor);
25+
}
26+
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) {
32+
this.hovered = this.isInside(mouseX, mouseY);
33+
}
34+
35+
public void onMouseClicked(double mouseX, double mouseY) {
36+
}
37+
2038
public boolean isInside(double x, double y) {
2139
return (x >= this.x && x <= this.x + this.width) && (y >= this.y && y <= this.y + this.height);
2240
}
@@ -36,4 +54,8 @@ public int width() {
3654
public int height() {
3755
return this.height;
3856
}
57+
58+
public boolean hovered() {
59+
return this.hovered;
60+
}
3961
}

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

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package btw.lowercase.optiboxes.screen.widget;
22

33
import net.minecraft.client.Minecraft;
4-
import net.minecraft.client.gui.Font;
54
import net.minecraft.client.gui.GuiGraphics;
65
import net.minecraft.network.chat.Component;
76
import net.minecraft.resources.Identifier;
@@ -11,42 +10,36 @@
1110
import java.util.Map;
1211
import java.util.function.Consumer;
1312

14-
public class SimpleButton extends Gidget implements Clickable, TextHolder {
13+
public class SimpleButton extends Gidget implements TextHolder {
1514
public static final int DEFAULT_WIDTH = 200;
1615
public static final int DEFAULT_HEIGHT = 20;
1716

18-
private final Component text;
17+
private final Text text;
1918
private final Consumer<? super SimpleButton> onClick;
2019

2120
public SimpleButton(Component text, int x, int y, Consumer<? super SimpleButton> onClick) {
2221
super(x, y, DEFAULT_WIDTH, DEFAULT_HEIGHT);
23-
this.text = text;
22+
this.text = new Text.Builder(text, this.x() + (this.width() / 2), this.y() + (this.height() / 2))
23+
.positioned(Text.Positioned.BOTH)
24+
.build(Minecraft.getInstance().font);
2425
this.onClick = onClick;
2526
}
2627

2728
@Override
2829
public void render(GuiGraphics guiGraphics, int mouseX, int mouseY) {
29-
boolean hovered = this.isInside(mouseX, mouseY);
30-
31-
int backgroundColor = hovered ? ARGB.white(0.7F) : ARGB.white(0.58F);
32-
guiGraphics.fill(this.x(), this.y(), this.x() + this.width(), this.y() + this.height(), backgroundColor);
33-
34-
Font font = Minecraft.getInstance().font;
35-
Text text = new Text.Builder(this.text, this.x() + (this.width() / 2), this.y() + (this.height() / 2))
36-
.withColor(hovered ? ARGB.color(1.0F, 0xFFFFA0) : ARGB.color(1.0F, 0xE0E0E0))
37-
.positioned(Text.Positioned.BOTH)
38-
.build(font);
30+
this.renderBackground(guiGraphics);
31+
text.setColor(this.hovered() ? ARGB.color(1.0F, 0xFFFFA0) : ARGB.color(1.0F, 0xE0E0E0));
3932
text.render(guiGraphics, mouseX, mouseY);
4033
}
4134

4235
@Override
43-
public void click(double mouseX, double mouseY) {
36+
public void onMouseClicked(double mouseX, double mouseY) {
4437
this.onClick.accept(this);
4538
}
4639

4740
@Override
4841
public Component getText() {
49-
return this.text;
42+
return this.text.getText();
5043
}
5144

5245
public static class Storage extends SimpleButton {

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@
88
public class Text extends Gidget implements TextHolder {
99
private final Font font;
1010
private final Component text;
11-
private final int color;
12-
private final Positioned positioned;
13-
private final boolean shadow;
11+
private Positioned positioned;
12+
private boolean shadow;
13+
private int color;
1414

15-
public Text(Font font, Component text, int x, int y, int color, Positioned positioned, boolean shadow) {
15+
public Text(Font font, Component text, int x, int y, Positioned positioned, boolean shadow, int color) {
1616
super(x, y, font.width(text.getString()), font.lineHeight);
1717
this.font = font;
1818
this.text = text;
19-
this.color = color;
2019
this.positioned = positioned;
2120
this.shadow = shadow;
21+
this.color = color;
2222
}
2323

2424
@Override
@@ -36,19 +36,38 @@ public void render(GuiGraphics guiGraphics, int mouseX, int mouseY) {
3636
guiGraphics.drawString(this.font, this.text, finalX, finalY, this.color, this.shadow);
3737
}
3838

39+
public Builder builder() {
40+
return new Builder(this.text, this.x(), this.y())
41+
.withColor(this.color)
42+
.positioned(this.positioned)
43+
.withShadow(this.shadow);
44+
}
45+
3946
@Override
4047
public Component getText() {
4148
return this.text;
4249
}
4350

51+
public void setColor(int color) {
52+
this.color = color;
53+
}
54+
4455
public int getColor() {
4556
return this.color;
4657
}
4758

59+
public void setPositioned(Positioned positioned) {
60+
this.positioned = positioned;
61+
}
62+
4863
public Positioned getPositioned() {
4964
return this.positioned;
5065
}
5166

67+
public void setShadow(boolean shadow) {
68+
this.shadow = shadow;
69+
}
70+
5271
public boolean hasShadow() {
5372
return this.shadow;
5473
}
@@ -93,7 +112,7 @@ public Builder centered() {
93112
}
94113

95114
public Text build(Font font) {
96-
return new Text(font, this.text, this.x, this.y, this.color, this.positioned, this.shadow);
115+
return new Text(font, this.text, this.x, this.y, this.positioned, this.shadow, this.color);
97116
}
98117
}
99118

0 commit comments

Comments
 (0)