Skip to content

Commit 6435eb9

Browse files
Merge branch 'feat/add-alpha-slider'
2 parents fa038dd + 6b5ce1d commit 6435eb9

File tree

14 files changed

+122
-58
lines changed

14 files changed

+122
-58
lines changed

build.gradle

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
plugins {
22
id 'java-library'
3-
id 'fabric-loom' version '1.10-SNAPSHOT' apply(false)
3+
id 'fabric-loom' version '1.11-SNAPSHOT' apply(false)
44
id 'net.neoforged.moddev' version '2.0.105' apply(false)
55
id "me.modmuss50.mod-publish-plugin" version "0.8.4"
66
}
@@ -77,6 +77,15 @@ subprojects {
7777
filesMatching(["META-INF/neoforge.mods.toml", "fabric.mod.json", "META-INF/mods.toml"]) {
7878
expand replaceProperties
7979
}
80+
81+
// This is less than ideal, we'll need to look into this.
82+
if (project.name == "fabric" || project.name == "neoforge") {
83+
tasks.named('processResources') {
84+
from(project(":common").file("src/main/resources")) {
85+
into "."
86+
}
87+
}
88+
}
8089
}
8190

8291
publishing {
@@ -135,4 +144,4 @@ publishMods {
135144
file = project.provider { project(":neoforge").tasks.jar }.flatMap { it.archiveFile }
136145
additionalFiles.from project.provider { project(":fabric").tasks.remapJar }.flatMap { it.archiveFile }
137146
}
138-
}
147+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ public void run() {
4949
continue;
5050

5151
for (var target : ScanController.INSTANCE.scanStore.activeScanTargets()) {
52-
if (target.type().matches(level, pos, state, fluidState)) {
53-
renderQueue.add(new OutlineRenderTarget(pos.getX(), pos.getY(), pos.getZ(), target.color()));
52+
if (target.matches(level, pos, state, fluidState)) {
53+
renderQueue.add(new OutlineRenderTarget(pos.getX(), pos.getY(), pos.getZ(), target.colorInt()));
5454
}
5555
}
5656
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ public static void renderBlocks(PoseStack poseStack) {
8282
if (holder == null) {
8383
BufferBuilder bufferBuilder = Tesselator.getInstance().begin(LINES_NO_DEPTH.getVertexFormatMode(), LINES_NO_DEPTH.getVertexFormat());
8484

85-
var opacity = 1F;
86-
8785
// More concurrent modification exceptions can happen here, so we clone the list
8886
var blockPropsClone = new ArrayList<>(blocksWithProps);
8987

@@ -95,9 +93,13 @@ public static void renderBlocks(PoseStack poseStack) {
9593
final float size = 1.0f;
9694
final int x = blockProps.x(), y = blockProps.y(), z = blockProps.z();
9795

96+
final float alpha = ((blockProps.color() >> 24) & 0xff) / 255f;
9897
final float red = (blockProps.color() >> 16 & 0xff) / 255f;
9998
final float green = (blockProps.color() >> 8 & 0xff) / 255f;
10099
final float blue = (blockProps.color() & 0xff) / 255f;
100+
101+
// Use the alpha from the color, or default to 1.0 if alpha is 0
102+
final float opacity = alpha > 0 ? alpha : 1.0f;
101103

102104
ShapeRenderer.renderLineBox(poseStack, bufferBuilder, x, y, z, x + size, y + size, z + size, red, green, blue, opacity);
103105
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
import net.minecraft.world.level.block.state.BlockState;
1212
import org.jetbrains.annotations.NotNull;
1313
import pro.mikey.xray.XRay;
14-
import pro.mikey.xray.core.scanner.ActiveScanTarget;
1514
import pro.mikey.xray.core.scanner.ScanStore;
15+
import pro.mikey.xray.core.scanner.ScanType;
1616

1717
import java.util.*;
1818
import java.util.concurrent.ExecutorService;
@@ -223,12 +223,12 @@ public static void onBlockChange(Level level, BlockPos pos, BlockState state) {
223223
// Otherwise, do we have scantarget in the active list of things to find?
224224
var noMatchesFound = true;
225225

226-
Set<ActiveScanTarget> activeScanTargets = ScanController.INSTANCE.scanStore.activeScanTargets();
226+
Set<ScanType> activeScanTargets = ScanController.INSTANCE.scanStore.activeScanTargets();
227227
for (var scanType : activeScanTargets) {
228-
if (scanType.type().matches(level, pos, state, state.getFluidState())) {
228+
if (scanType.matches(level, pos, state, state.getFluidState())) {
229229
// We need to tell the render system to refresh. We should manually add this black to the renderlist
230230
// We're actively looking at this chunk so let's inject this block
231-
outlineRenderTargets.add(new OutlineRenderTarget(pos.getX(), pos.getY(), pos.getZ(), scanType.color()));
231+
outlineRenderTargets.add(new OutlineRenderTarget(pos.getX(), pos.getY(), pos.getZ(), scanType.colorInt()));
232232

233233
// Tell the VBO to refresh for this chunk
234234
OutlineRender.refreshVBOForChunk(chunkPos);

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

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

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class ScanStore {
5454
private final List<Category> categories = new ArrayList<>();
5555

5656
// In memory holder of only the enabled scan targets
57-
private final Set<ActiveScanTarget> activeScanTargets = new HashSet<>();
57+
private final Set<ScanType> activeScanTargets = new HashSet<>();
5858

5959
public ScanStore() {}
6060

@@ -183,8 +183,7 @@ public void updateActiveTargets() {
183183
continue; // Skip disabled scan types
184184
}
185185

186-
var target = new ActiveScanTarget(scanType, scanType.colorInt);
187-
this.activeScanTargets.add(target);
186+
this.activeScanTargets.add(scanType);
188187
}
189188
}
190189
}
@@ -193,7 +192,7 @@ public List<Category> categories() {
193192
return Collections.unmodifiableList(categories);
194193
}
195194

196-
public Set<ActiveScanTarget> activeScanTargets() {
195+
public Set<ScanType> activeScanTargets() {
197196
return activeScanTargets;
198197
}
199198

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

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,29 @@ public void updateColor(String newColor) {
9090
public void updateColor(int newColorInt) {
9191
this.colorInt = newColorInt;
9292

93-
// Convert RGB int to rgb(r, g, b) format
93+
// Extract ARGB components
94+
int a = (newColorInt >> 24) & 0xFF;
9495
int r = (newColorInt >> 16) & 0xFF;
9596
int g = (newColorInt >> 8) & 0xFF;
9697
int b = newColorInt & 0xFF;
9798

9899
// What type are we?
99-
if (this.color.startsWith("rgb(")) {
100-
this.color = String.format("rgb(%d, %d, %d)", r, g, b);
100+
if (this.color.startsWith("rgba(")) {
101+
this.color = String.format("rgba(%d, %d, %d, %d)", r, g, b, a);
102+
} else if (this.color.startsWith("rgb(")) {
103+
// If alpha is not 255, convert to rgba format
104+
if (a != 255) {
105+
this.color = String.format("rgba(%d, %d, %d, %d)", r, g, b, a);
106+
} else {
107+
this.color = String.format("rgb(%d, %d, %d)", r, g, b);
108+
}
101109
} else if (this.color.startsWith("#")) {
102-
// Convert to hex format
103-
this.color = String.format("#%02X%02X%02X", r, g, b);
110+
// Convert to hex format (with alpha if not fully opaque)
111+
if (a != 255) {
112+
this.color = String.format("#%02X%02X%02X%02X", a, r, g, b);
113+
} else {
114+
this.color = String.format("#%02X%02X%02X", r, g, b);
115+
}
104116
} else if (this.color.startsWith("hsl(")) {
105117
// Convert to HSL format
106118
var hsl = Color.RGBtoHSB(r, g, b, null);
@@ -109,38 +121,55 @@ public void updateColor(int newColorInt) {
109121
} else if (this.color.startsWith("0x")) {
110122
this.color = String.format("#%06X", newColorInt & 0xFFFFFF);
111123
} else {
112-
throw new IllegalArgumentException("Unsupported color format: " + this.color);
124+
// Default to rgba format for unknown formats
125+
this.color = String.format("rgba(%d, %d, %d, %d)", r, g, b, a);
113126
}
114127
}
115128

116129
/**
117-
* Custom method for reading hex, rgb, hsl, and other color formats
130+
* Custom method for reading hex, rgb, rgba, hsl, and other color formats
118131
* @param color String representation of the color
119-
* rgb(255, 0, 0), hsl(120, 100%, 50%), #FF0000, 0xFF0000
132+
* rgb(255, 0, 0), rgba(255, 0, 0, 128), hsl(120, 100%, 50%), #FF0000, #FF0000FF, 0xFF0000
120133
* are the supported formats at the moment
121-
* @return int representation of the color
134+
* @return int representation of the color (ARGB format)
122135
*/
123136
public static int parseColor(String color) {
124137
if (color.startsWith("#")) {
125-
// Only support RRGGBB format, no alpha channel
126-
if (color.length() != 7) {
138+
// Support both RRGGBB and AARRGGBB formats
139+
if (color.length() == 7) {
140+
// RRGGBB format - add full alpha
141+
return 0xFF000000 | Integer.parseInt(color.substring(1), 16);
142+
} else if (color.length() == 9) {
143+
// AARRGGBB format
144+
return (int)Long.parseLong(color.substring(1), 16);
145+
} else {
127146
throw new IllegalArgumentException("Invalid hex color format: " + color);
128147
}
148+
}
129149

130-
// Hex color
131-
return Integer.parseInt(color.substring(1), 16);
150+
if (color.startsWith("rgba(") && color.endsWith(")")) {
151+
// rgba(255, 0, 0, 128)
152+
String[] parts = color.substring(5, color.length() - 1).split(",");
153+
if (parts.length != 4) {
154+
throw new IllegalArgumentException("Invalid RGBA color format: " + color);
155+
}
156+
int r = Integer.parseInt(parts[0].trim());
157+
int g = Integer.parseInt(parts[1].trim());
158+
int b = Integer.parseInt(parts[2].trim());
159+
int a = Integer.parseInt(parts[3].trim());
160+
return (a << 24) | (r << 16) | (g << 8) | b;
132161
}
133162

134163
if (color.startsWith("rgb(") && color.endsWith(")")) {
135-
// rgb(255, 0, 0)
164+
// rgb(255, 0, 0) - default to full alpha
136165
String[] parts = color.substring(4, color.length() - 1).split(",");
137166
if (parts.length != 3) {
138167
throw new IllegalArgumentException("Invalid RGB color format: " + color);
139168
}
140169
int r = Integer.parseInt(parts[0].trim());
141170
int g = Integer.parseInt(parts[1].trim());
142171
int b = Integer.parseInt(parts[2].trim());
143-
return (r << 16) | (g << 8) | b;
172+
return 0xFF000000 | (r << 16) | (g << 8) | b;
144173
}
145174

146175
if (color.startsWith("hsl(") && color.endsWith(")")) {
@@ -177,12 +206,12 @@ public static int parseColor(String color) {
177206
int gi = Math.round((g + m) * 255);
178207
int bi = Math.round((b + m) * 255);
179208

180-
return (ri << 16) | (gi << 8) | bi;
209+
return 0xFF000000 | (ri << 16) | (gi << 8) | bi;
181210
}
182211

183212
if (color.startsWith("0x")) {
184-
// 0xFF0000 format
185-
return Integer.parseInt(color.substring(2), 16);
213+
// 0xFF0000 format - add full alpha
214+
return 0xFF000000 | Integer.parseInt(color.substring(2), 16);
186215
}
187216

188217
throw new IllegalArgumentException("Unsupported color format: " + color);

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

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import net.minecraft.client.gui.components.SpriteIconButton;
88
import net.minecraft.client.gui.layouts.GridLayout;
99
import net.minecraft.client.gui.layouts.Layout;
10+
import net.minecraft.client.renderer.RenderPipelines;
1011
import net.minecraft.client.resources.language.I18n;
1112
import net.minecraft.network.chat.Component;
1213
import net.minecraft.resources.ResourceLocation;
@@ -27,12 +28,14 @@
2728

2829
public class ScanConfigureScreen extends GuiBase {
2930
private static final ResourceLocation TRASH_ICON = XRay.assetLocation("gui/trash.png");
31+
private static final ResourceLocation TRANSPARENT_BACKGROUND = XRay.assetLocation("gui/transparent_background.png");
3032

3133
private EditBox oreName;
3234

3335
private SliderWidget redSlider;
3436
private SliderWidget greenSlider;
3537
private SliderWidget blueSlider;
38+
private SliderWidget alphaSlider;
3639

3740
private final Block selectBlock;
3841
private final ItemStack icon;
@@ -97,18 +100,25 @@ public void init() {
97100
layout.arrangeElements();
98101
layout.visitWidgets(this::addRenderableWidget);
99102

100-
int defaultColor = 0x00A8FF; // Default color (Blue)
103+
int defaultColor = 0xFF00A8FF; // Default color (Blue with full alpha)
101104
if (editingType != null) {
105+
// Use the existing color with its alpha value
102106
defaultColor = editingType.colorInt;
107+
// Only add alpha if the color doesn't have it (checking if top byte is 0)
108+
if ((defaultColor & 0xFF000000) == 0) {
109+
defaultColor = defaultColor | 0xFF000000;
110+
}
103111
}
104112

113+
double alpha = ((defaultColor >> 24) & 0xFF) / 255.0;
105114
double red = (defaultColor >> 16 & 0xFF) / 255.0;
106115
double green = (defaultColor >> 8 & 0xFF) / 255.0;
107116
double blue = (defaultColor & 0xFF) / 255.0;
108117

109-
addRenderableWidget(redSlider = new SliderWidget(getWidth() / 2 - 100, getHeight() / 2 + 7, 202, 20, "xray.color.red", red));
110-
addRenderableWidget(greenSlider = new SliderWidget(getWidth() / 2 - 100, getHeight() / 2 + 30, 202, 20, "xray.color.green", green));
111-
addRenderableWidget(blueSlider = new SliderWidget(getWidth() / 2 - 100, getHeight() / 2 + 53,202, 20, "xray.color.blue", blue));
118+
addRenderableWidget(redSlider = new SliderWidget(getWidth() / 2 - 100, getHeight() / 2 - 16, 202, 20, "xray.color.red", red));
119+
addRenderableWidget(greenSlider = new SliderWidget(getWidth() / 2 - 100, getHeight() / 2 + 7, 202, 20, "xray.color.green", green));
120+
addRenderableWidget(blueSlider = new SliderWidget(getWidth() / 2 - 100, getHeight() / 2 + 30, 202, 20, "xray.color.blue", blue));
121+
addRenderableWidget(alphaSlider = new SliderWidget(getWidth() / 2 - 100, getHeight() / 2 + 53, 202, 20, "xray.color.alpha", alpha));
112122

113123
oreName = new EditBox(minecraft.font, getWidth() / 2 - 100, getHeight() / 2 - 70, 202, 20, Component.empty());
114124
if (editingType != null) {
@@ -125,7 +135,8 @@ private void editBlock() {
125135
throw new IllegalStateException("Editing type is not set");
126136
}
127137

128-
int color = (int) (redSlider.getValue() * 255) << 16
138+
int color = (int) (alphaSlider.getValue() * 255) << 24
139+
| (int) (redSlider.getValue() * 255) << 16
129140
| (int) (greenSlider.getValue() * 255) << 8
130141
| (int) (blueSlider.getValue() * 255);
131142

@@ -156,8 +167,8 @@ private void addBlock() {
156167
scanStore.addEntry(new BlockScanType(
157168
selectBlock,
158169
oreName.getValue(),
159-
// Save as RGB by default
160-
"rgb(" + (int) (redSlider.getValue() * 255) + ", " + (int) (greenSlider.getValue() * 255) + ", " + (int) (blueSlider.getValue() * 255) + ")",
170+
// Save as RGBA by default
171+
"rgba(" + (int) (redSlider.getValue() * 255) + ", " + (int) (greenSlider.getValue() * 255) + ", " + (int) (blueSlider.getValue() * 255) + ", " + (int) (alphaSlider.getValue() * 255) + ")",
161172
scanStore.getNextOrder()
162173
));
163174

@@ -174,8 +185,11 @@ public void tick() {
174185
public void renderExtra(GuiGraphics graphics, int x, int y, float partialTicks) {
175186
graphics.drawString(font, selectBlock.getName().getString(), getWidth() / 2 - 100, getHeight() / 2 - 90, 0xffffffff);
176187

177-
int color = (255 << 24) | ((int) (this.redSlider.getValue() * 255) << 16) | ((int) (this.greenSlider.getValue() * 255) << 8) | (int) (this.blueSlider.getValue() * 255);
178-
graphics.fill(this.getWidth() / 2 - 100, this.getHeight() / 2 - 45, (this.getWidth() / 2 + 2) + 100, (this.getHeight() / 2 - 45) + 45, color);
188+
// blit render the TRANSPARENT_BACKGROUND texture, a 16x16 checkerboard pattern that should tile to fit the fill area
189+
graphics.blit(RenderPipelines.GUI_TEXTURED, TRANSPARENT_BACKGROUND, this.getWidth() / 2 - 100, this.getHeight() / 2 - 45, 0, 0, 202, 24, 16, 16, 0x80FFFFFF);
190+
191+
int color = ((int) (this.alphaSlider.getValue() * 255) << 24) | ((int) (this.redSlider.getValue() * 255) << 16) | ((int) (this.greenSlider.getValue() * 255) << 8) | (int) (this.blueSlider.getValue() * 255);
192+
graphics.fill(this.getWidth() / 2 - 100, this.getHeight() / 2 - 45, (this.getWidth() / 2 + 2) + 100, (this.getHeight() / 2 - 45) + 24, color);
179193

180194
oreName.render(graphics, x, y, partialTicks);
181195

common/src/main/resources/assets/xray/lang/en_us.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"xray.color.red": "Red: %s",
1717
"xray.color.green": "Green: %s",
1818
"xray.color.blue": "Blue: %s",
19+
"xray.color.alpha": "Alpha: %s",
1920

2021
"xray.input.gui": "GUI Name",
2122
"xray.input.add": "Add Block",

common/src/main/resources/assets/xray/lang/fr_ca.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
"xray.single.delete": "Supprimer",
77
"xray.single.save": "Sauvegarder",
88

9-
"xray.color.red": "Rouge",
10-
"xray.color.green": "Vert",
11-
"xray.color.blue": "Bleu",
9+
"xray.color.red": "Rouge: %s",
10+
"xray.color.green": "Vert: %s",
11+
"xray.color.blue": "Bleu: %s",
12+
"xray.color.alpha": "Alpha: %s",
1213

1314
"xray.input.gui": "Nom GUI",
1415
"xray.input.add": "Ajouter un minerai",

0 commit comments

Comments
 (0)