Improved rounded rectangles #59

This commit is contained in:
2026-03-25 22:18:16 +01:00
parent fc1500abbd
commit b81a4dda98
3 changed files with 46 additions and 13 deletions
@@ -1,9 +1,14 @@
package org.saturnclient.ui; package org.saturnclient.ui;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import org.saturnclient.common.ref.asset.IdentifierRef; import org.saturnclient.common.ref.asset.IdentifierRef;
import org.saturnclient.common.ref.asset.SpriteRef; import org.saturnclient.common.ref.asset.SpriteRef;
import org.saturnclient.common.ref.game.ItemStackRef; import org.saturnclient.common.ref.game.ItemStackRef;
import org.saturnclient.common.ref.render.MatrixStackRef; import org.saturnclient.common.ref.render.MatrixStackRef;
import org.saturnclient.ui.resources.SvgTexture;
public interface RenderScope { public interface RenderScope {
@@ -24,7 +29,25 @@ public interface RenderScope {
// ---------------------------- // ----------------------------
void drawRectangle(int x, int y, int width, int height, int color); void drawRectangle(int x, int y, int width, int height, int color);
void drawRoundedRectangle(int x, int y, int width, int height, int radius, int color); default void drawRoundedRectangle(int x, int y, int width, int height, int radius, int color) {
if (color == 0)
return;
radius = Math.min(radius, Math.min(width, height));
String svg = String.format(
"<svg xmlns='http://www.w3.org/2000/svg' width='%d' height='%d'>" +
"<rect width='%d' height='%d' rx='%d' ry='%d' fill='white'/>" +
"</svg>",
width, height,
width, height,
radius, radius);
InputStream svgStream = new ByteArrayInputStream(svg.getBytes(StandardCharsets.UTF_8));
drawTexture(SvgTexture.getSvg(svgStream,
IdentifierRef.ofSaturn("textures/gui/tmp_rect/rounded_" + width + "_" + height + "_" + radius),
width * 3, height * 3), x, y, 0, 0, width, height, color);
}
void drawBorder(int x, int y, int width, int height, int color); void drawBorder(int x, int y, int width, int height, int color);
@@ -24,7 +24,7 @@ public class ImageTexture extends Element {
int renderHeight = (int) (height * client.getWindow().getFramebufferHeight() int renderHeight = (int) (height * client.getWindow().getFramebufferHeight()
/ client.getWindow().getHeight()); / client.getWindow().getHeight());
IdentifierRef pngId = SvgTexture.getSvg(client, sprite, renderWidth * 2, renderHeight * 2); IdentifierRef pngId = SvgTexture.getSvg(sprite, renderWidth * 2, renderHeight * 2);
this.sprite = pngId; this.sprite = pngId;
if (pngId == null) { if (pngId == null) {
@@ -12,7 +12,6 @@ import com.kitfox.svg.SVGUniverse;
import org.saturnclient.common.provider.Providers; import org.saturnclient.common.provider.Providers;
import org.saturnclient.common.ref.asset.IdentifierRef; import org.saturnclient.common.ref.asset.IdentifierRef;
import org.saturnclient.common.ref.game.MinecraftClientRef;
public class SvgTexture { public class SvgTexture {
@@ -50,22 +49,33 @@ public class SvgTexture {
return image; return image;
} }
public static IdentifierRef getSvg(MinecraftClientRef client, IdentifierRef svgImage, int width, int height) { public static IdentifierRef getSvg(InputStream stream, IdentifierRef output, int width, int height) {
IdentifierRef id = IdentifierRef if (svgCache.contains(output)) {
.of(svgImage.toString().replaceAll("\\.svg$", (width + "_" + height).toString() + ".png")); return output;
if (svgCache.contains(id)) {
return id;
} }
try (InputStream svgStream = client.getResource(svgImage)) { try {
BufferedImage image = renderSvg(stream, width, height);
Providers.saturn.registerBufferedImageTexture(output, image);
svgCache.add(output);
} catch (Exception e) {
e.printStackTrace();
return null;
}
BufferedImage image = renderSvg(svgStream, width, height); return output;
}
Providers.saturn.registerBufferedImageTexture(id, image); public static IdentifierRef getSvg(IdentifierRef svgImage, int width, int height) {
IdentifierRef output = IdentifierRef
.of(svgImage.toString().replaceAll("\\.svg$", (width + "_" + height).toString() + ".png"));
return id; if (svgCache.contains(output)) {
return output;
}
try (InputStream svgStream = Providers.saturn.getClient().getResource(svgImage)) {
return getSvg(svgStream, output, width, height);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return null; return null;