Skip to content

Commit 643389a

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

File tree

3 files changed

+40
-21
lines changed

3 files changed

+40
-21
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import net.minecraft.client.gui.GuiGraphics;
44

55
public abstract class Gidget {
6-
protected final int x;
7-
protected final int y;
8-
protected final int width;
9-
protected final int height;
6+
private final int x;
7+
private final int y;
8+
private final int width;
9+
private final int height;
1010

1111
public Gidget(int x, int y, int width, int height) {
1212
this.x = x;

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ public SimpleButton(Component text, int x, int y, Consumer<? super SimpleButton>
2828
public void render(GuiGraphics guiGraphics, int mouseX, int mouseY) {
2929
boolean hovered = this.isInside(mouseX, mouseY);
3030

31-
int backgroundColor = hovered ? ARGB.color(0.7F, 0xFFFFFF) : ARGB.color(0.58F, 0xFFFFFF);
32-
guiGraphics.fill(this.x, this.y, this.x + this.width, this.y + this.height, backgroundColor);
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);
3333

3434
Font font = Minecraft.getInstance().font;
35-
int textWidth = font.width(this.text.getString());
36-
int textColor = hovered ? ARGB.color(1.0F, 0xFFFFA0) : ARGB.color(1.0F, 0xE0E0E0);
37-
guiGraphics.drawString(font, this.text, this.x + ((this.width / 2) - (textWidth / 2)), this.y + ((this.height / 2) - (font.lineHeight / 2)), textColor);
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);
39+
text.render(guiGraphics, mouseX, mouseY);
3840
}
3941

4042
@Override

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

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,31 @@ public class Text extends Gidget implements TextHolder {
99
private final Font font;
1010
private final Component text;
1111
private final int color;
12-
private final boolean centered;
12+
private final Positioned positioned;
1313
private final boolean shadow;
1414

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

2424
@Override
2525
public void render(GuiGraphics guiGraphics, int mouseX, int mouseY) {
26-
int finalX = this.x;
27-
if (this.centered) {
28-
finalX -= this.width / 2;
26+
int finalX = this.x();
27+
if (this.positioned == Positioned.BOTH || this.positioned == Positioned.CENTER_HORIZONTAL) {
28+
finalX -= this.width() / 2;
2929
}
3030

31-
guiGraphics.drawString(this.font, this.text, finalX, this.y, this.color, this.shadow);
31+
int finalY = this.y();
32+
if (this.positioned == Positioned.BOTH || this.positioned == Positioned.CENTER_VERTICAL) {
33+
finalY -= this.height() / 2;
34+
}
35+
36+
guiGraphics.drawString(this.font, this.text, finalX, finalY, this.color, this.shadow);
3237
}
3338

3439
@Override
@@ -40,8 +45,8 @@ public int getColor() {
4045
return this.color;
4146
}
4247

43-
public boolean isCentered() {
44-
return this.centered;
48+
public Positioned getPositioned() {
49+
return this.positioned;
4550
}
4651

4752
public boolean hasShadow() {
@@ -54,7 +59,7 @@ public static class Builder {
5459
private final int y;
5560

5661
private int color = ARGB.white(1.0F);
57-
private boolean centered = false;
62+
private Positioned positioned = Positioned.NONE;
5863
private boolean shadow = true;
5964

6065
public Builder(Component text, int x, int y) {
@@ -77,13 +82,25 @@ public Builder withShadow(boolean shadow) {
7782
return this;
7883
}
7984

85+
public Builder positioned(Positioned positioned) {
86+
this.positioned = positioned;
87+
return this;
88+
}
89+
8090
public Builder centered() {
81-
this.centered = true;
91+
this.positioned = Positioned.CENTER_HORIZONTAL;
8292
return this;
8393
}
8494

8595
public Text build(Font font) {
86-
return new Text(font, this.text, this.x, this.y, this.color, this.centered, this.shadow);
96+
return new Text(font, this.text, this.x, this.y, this.color, this.positioned, this.shadow);
8797
}
8898
}
99+
100+
public enum Positioned {
101+
CENTER_VERTICAL,
102+
CENTER_HORIZONTAL,
103+
BOTH,
104+
NONE
105+
}
89106
}

0 commit comments

Comments
 (0)