Skip to content

Commit 4998c9a

Browse files
committed
Colored button for boolean settings
1 parent 0609c53 commit 4998c9a

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

src/client/java/com/hanprogramer/androids/client/screen/android/SettingsTab.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.hanprogramer.androids.client.screen.android;
22

33
import com.hanprogramer.androids.client.screen.base.BaseScreenTab;
4+
import com.hanprogramer.androids.client.screen.widgets.ColoredButtonWidget;
45
import com.hanprogramer.androids.entities.android.AndroidScriptProperty;
56
import net.minecraft.client.MinecraftClient;
67
import net.minecraft.client.gl.RenderPipelines;
@@ -36,9 +37,9 @@ public SettingsEntry(Text title, String entryType, String id, Object value) {
3637
this.value = value;
3738
if (Objects.equals(entryType, AndroidScriptProperty.TYPE_BOOLEAN)) {
3839
var bool = (Boolean) value;
39-
button = ButtonWidget.builder(bool ? Text.of("ON") : Text.of("OFF"), button1 -> {
40+
button = new ColoredButtonWidget(0,0,0,0, bool ? Text.of("ON") : Text.of("OFF"), button1 -> {
4041
System.out.println("Clicked");
41-
}).build();
42+
}, bool? Colors.GREEN : Colors.RED);
4243
}
4344
else if (Objects.equals(entryType, AndroidScriptProperty.TYPE_BLOCKPOS)) {
4445
var val = (BlockPos) value;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.hanprogramer.androids.client.screen.widgets;
2+
3+
import net.minecraft.client.MinecraftClient;
4+
import net.minecraft.client.gl.RenderPipelines;
5+
import net.minecraft.client.gui.DrawContext;
6+
import net.minecraft.client.gui.screen.ButtonTextures;
7+
import net.minecraft.client.gui.widget.ButtonWidget;
8+
import net.minecraft.text.Text;
9+
import net.minecraft.util.Identifier;
10+
import net.minecraft.util.math.ColorHelper;
11+
12+
import java.util.function.Supplier;
13+
14+
public class ColoredButtonWidget extends ButtonWidget {
15+
private static final ButtonTextures TEXTURES = new ButtonTextures(Identifier.ofVanilla("widget/button"), Identifier.ofVanilla("widget/button_disabled"), Identifier.ofVanilla("widget/button_highlighted"));
16+
private final int textColor;
17+
18+
public ColoredButtonWidget(int x, int y, int width, int height, Text message, PressAction onPress, int textColor) {
19+
super(x, y, width, height, message, onPress, Supplier::get);
20+
this.textColor = textColor;
21+
}
22+
23+
@Override
24+
protected void renderWidget(DrawContext context, int mouseX, int mouseY, float deltaTicks) {
25+
MinecraftClient minecraftClient = MinecraftClient.getInstance();
26+
context.drawGuiTexture(RenderPipelines.GUI_TEXTURED, TEXTURES.get(this.active, this.isSelected()), this.getX(), this.getY(), this.getWidth(), this.getHeight(), ColorHelper.getWhite(this.alpha));
27+
int i = ColorHelper.withAlpha(this.alpha, textColor);
28+
this.drawMessage(context, minecraftClient.textRenderer, i);
29+
context.drawCenteredTextWithShadow(minecraftClient.textRenderer, getMessage(),
30+
this.getX() + this.width / 2,
31+
this.getY() + (this.height - 8) / 2,
32+
textColor);
33+
}
34+
}

0 commit comments

Comments
 (0)