Skip to content

Commit 415e3ba

Browse files
fix: #314 no remove button regression
1 parent 28f22bb commit 415e3ba

File tree

5 files changed

+128
-14
lines changed

5 files changed

+128
-14
lines changed

common/src/main/java/pro/mikey/xray/core/OutlineRender.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class OutlineRender {
3636
private static final Set<ChunkPos> chunksToRefresh = Collections.synchronizedSet(new HashSet<>());
3737

3838
public static RenderPipeline LINES_NO_DEPTH = RenderPipeline.builder(RenderPipelines.MATRICES_FOG_SNIPPET, RenderPipelines.GLOBALS_SNIPPET)
39-
.withLocation("pipeline/xray_lines")
39+
.withLocation(XRay.id("pipeline/xray_lines"))
4040
.withVertexShader("core/rendertype_lines")
4141
.withFragmentShader(ResourceLocation.fromNamespaceAndPath(XRay.MOD_ID, "frag/constant_color"))
4242
.withBlend(BlendFunction.TRANSLUCENT)

common/src/main/java/pro/mikey/xray/core/scanner/ScanStore.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,18 @@ public void addEntry(ScanType type) {
102102
this.save();
103103
}
104104

105+
// TODO: Support categories once the GUI can support it.
106+
public void removeEntry(ScanType type) {
107+
for (Category category : this.categories) {
108+
if (category.entries.remove(type)) {
109+
this.save();
110+
return; // Exit after removing the entry
111+
}
112+
}
113+
114+
LOGGER.warn("Scan type not found in any category: {}", type);
115+
}
116+
105117
public int getNextOrder() {
106118
var firstCategory = this.categories.stream().findFirst();
107119
if (firstCategory.isEmpty()) {

common/src/main/java/pro/mikey/xray/screens/ScanConfigureScreen.java

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,30 @@
44
import net.minecraft.client.gui.GuiGraphics;
55
import net.minecraft.client.gui.components.Button;
66
import net.minecraft.client.gui.components.EditBox;
7+
import net.minecraft.client.gui.components.SpriteIconButton;
8+
import net.minecraft.client.gui.layouts.GridLayout;
9+
import net.minecraft.client.gui.layouts.Layout;
710
import net.minecraft.client.resources.language.I18n;
811
import net.minecraft.network.chat.Component;
12+
import net.minecraft.resources.ResourceLocation;
913
import net.minecraft.world.item.ItemStack;
1014
import net.minecraft.world.level.block.Block;
1115
import org.jetbrains.annotations.Nullable;
16+
import pro.mikey.xray.XRay;
1217
import pro.mikey.xray.core.ScanController;
1318
import pro.mikey.xray.core.scanner.BlockScanType;
1419
import pro.mikey.xray.core.scanner.ScanStore;
1520
import pro.mikey.xray.core.scanner.ScanType;
1621
import pro.mikey.xray.screens.helpers.GuiBase;
22+
import pro.mikey.xray.screens.helpers.ImageButton;
1723
import pro.mikey.xray.screens.helpers.SliderWidget;
1824

1925
import java.util.Objects;
2026
import java.util.function.Supplier;
2127

2228
public class ScanConfigureScreen extends GuiBase {
29+
private static final ResourceLocation TRASH_ICON = XRay.assetLocation("gui/trash.png");
30+
2331
private EditBox oreName;
2432

2533
private SliderWidget redSlider;
@@ -60,22 +68,35 @@ public ScanConfigureScreen(ScanType editingType, Supplier<GuiBase> previousScree
6068
@Override
6169
public void init() {
6270
// Called when the gui should be (re)created
63-
addRenderableWidget(Button.builder(Component.translatable(editingType != null ? "xray.title.edit" : "xray.single.add"), b -> {
64-
if (editingType != null) {
65-
editBlock();
66-
} else {
67-
addBlock();
68-
}
69-
})
70-
.pos(getWidth() / 2 - 100, getHeight() / 2 + 85)
71-
.size(128, 20)
71+
GridLayout layout = new GridLayout();
72+
layout.columnSpacing(3);
73+
layout.setPosition(getWidth() / 2 - 100, getHeight() / 2 + 85);
74+
GridLayout.RowHelper rowHelper = layout.createRowHelper(3);
75+
76+
rowHelper.addChild(ImageButton.builder(b -> {
77+
removeBlock();
78+
})
79+
.image(XRay.assetLocation("gui/trash.png"), 16, 16)
80+
.size(20, 20)
7281
.build());
7382

74-
addRenderableWidget(Button.builder(Component.translatable("xray.single.cancel"), b -> Minecraft.getInstance().setScreen(this.previousScreenCallback.get()))
75-
.pos(getWidth() / 2 + 30, getHeight() / 2 + 85)
76-
.size(72, 20)
83+
rowHelper.addChild(Button.builder(Component.translatable("xray.single.cancel"), b -> Minecraft.getInstance().setScreen(this.previousScreenCallback.get()))
84+
.size(60, 20)
7785
.build());
7886

87+
rowHelper.addChild(Button.builder(Component.translatable(editingType != null ? "xray.title.edit" : "xray.single.add"), b -> {
88+
if (editingType != null) {
89+
editBlock();
90+
} else {
91+
addBlock();
92+
}
93+
})
94+
.size(117, 20)
95+
.build());
96+
97+
layout.arrangeElements();
98+
layout.visitWidgets(this::addRenderableWidget);
99+
79100
int defaultColor = 0x00A8FF; // Default color (Blue)
80101
if (editingType != null) {
81102
defaultColor = editingType.colorInt;
@@ -115,6 +136,17 @@ private void editBlock() {
115136
minecraft.setScreen(this.previousScreenCallback.get());
116137
}
117138

139+
private void removeBlock() {
140+
if (editingType == null) {
141+
throw new IllegalStateException("Editing type is not set");
142+
}
143+
144+
ScanStore scanStore = ScanController.INSTANCE.scanStore;
145+
scanStore.removeEntry(editingType);
146+
ScanController.INSTANCE.requestBlockFinder(true);
147+
minecraft.setScreen(this.previousScreenCallback.get());
148+
}
149+
118150
private void addBlock() {
119151
if (editingType != null) {
120152
throw new IllegalStateException("Editing type is already set");
@@ -140,7 +172,7 @@ public void tick() {
140172

141173
@Override
142174
public void renderExtra(GuiGraphics graphics, int x, int y, float partialTicks) {
143-
graphics.drawString(font, selectBlock.getName().getString(), getWidth() / 2 - 100, getHeight() / 2 - 90, 0xffffff);
175+
graphics.drawString(font, selectBlock.getName().getString(), getWidth() / 2 - 100, getHeight() / 2 - 90, 0xffffffff);
144176

145177
int color = (255 << 24) | ((int) (this.redSlider.getValue() * 255) << 16) | ((int) (this.greenSlider.getValue() * 255) << 8) | (int) (this.blueSlider.getValue() * 255);
146178
graphics.fill(this.getWidth() / 2 - 100, this.getHeight() / 2 - 45, (this.getWidth() / 2 + 2) + 100, (this.getHeight() / 2 - 45) + 45, color);
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package pro.mikey.xray.screens.helpers;
2+
3+
import net.minecraft.client.gui.GuiGraphics;
4+
import net.minecraft.client.gui.components.Button;
5+
import net.minecraft.client.renderer.RenderPipelines;
6+
import net.minecraft.network.chat.Component;
7+
import net.minecraft.resources.ResourceLocation;
8+
9+
public class ImageButton extends Button {
10+
protected final ResourceLocation image;
11+
protected final int imageWidth;
12+
protected final int imageHeight;
13+
14+
ImageButton(int width, int height, int imageWidth, int imageHeight, ResourceLocation resourceLocation, Button.OnPress onPress) {
15+
super(0, 0, width, height, Component.empty(), onPress, DEFAULT_NARRATION);
16+
this.imageWidth = imageWidth;
17+
this.imageHeight = imageHeight;
18+
this.image = resourceLocation;
19+
}
20+
21+
public void renderWidget(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTicks) {
22+
super.renderWidget(guiGraphics, mouseX, mouseY, partialTicks);
23+
int k = this.getX() + this.getWidth() / 2 - this.imageWidth / 2;
24+
int l = this.getY() + this.getHeight() / 2 - this.imageHeight / 2;
25+
guiGraphics.blit(RenderPipelines.GUI_TEXTURED, this.image, k, l, 0f, 0f, this.imageWidth, this.imageHeight, this.imageWidth, this.imageHeight, 0xFFFFFFFF);
26+
}
27+
28+
public static ImageButton.Builder builder(Button.OnPress onPress) {
29+
return new ImageButton.Builder(onPress);
30+
}
31+
32+
public static class Builder {
33+
private final Button.OnPress onPress;
34+
private int width = 20;
35+
private int height = 20;
36+
private ResourceLocation image;
37+
private int imageWidth;
38+
private int imageHeight;
39+
40+
public Builder(Button.OnPress onPress) {
41+
this.onPress = onPress;
42+
}
43+
44+
public ImageButton.Builder width(int i) {
45+
this.width = i;
46+
return this;
47+
}
48+
49+
public ImageButton.Builder size(int i, int j) {
50+
this.width = i;
51+
this.height = j;
52+
return this;
53+
}
54+
55+
public ImageButton.Builder image(ResourceLocation resourceLocation, int i, int j) {
56+
this.image = resourceLocation;
57+
this.imageWidth = i;
58+
this.imageHeight = j;
59+
return this;
60+
}
61+
62+
public ImageButton build() {
63+
if (this.image == null) {
64+
throw new IllegalStateException("Sprite not set");
65+
} else {
66+
return new ImageButton(this.width, this.height, this.imageWidth, this.imageHeight, this.image, this.onPress);
67+
}
68+
}
69+
}
70+
}
381 Bytes
Loading

0 commit comments

Comments
 (0)