From 551f2adb6cbf8f7f9a16541585f4b5dc4f6cfa7b Mon Sep 17 00:00:00 2001 From: zbx1425 Date: Sat, 1 Mar 2025 20:23:30 +0800 Subject: [PATCH] Fix image aspect ratio --- .github/workflows/build.yml | 2 +- .../data/network/ImageDownload.java | 48 ++++++++++++++----- .../worldcomment/gui/CommentListScreen.java | 21 ++++---- .../worldcomment/gui/WidgetCommentEntry.java | 3 +- 4 files changed, 49 insertions(+), 25 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c0ca0e1..dfdc869 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -32,7 +32,7 @@ jobs: properties: | buildVersion=${{ matrix.minecraft }} - name: Capture release artifacts - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4S with: name: Fabric and Forge Artifacts path: build/ diff --git a/common/src/main/java/cn/zbx1425/worldcomment/data/network/ImageDownload.java b/common/src/main/java/cn/zbx1425/worldcomment/data/network/ImageDownload.java index e7eeee8..10feca7 100644 --- a/common/src/main/java/cn/zbx1425/worldcomment/data/network/ImageDownload.java +++ b/common/src/main/java/cn/zbx1425/worldcomment/data/network/ImageDownload.java @@ -30,7 +30,8 @@ public class ImageDownload { private static final Map images = new HashMap<>(); - public static AbstractTexture getTexture(ThumbImage image, boolean thumb) { + public static ImageState getTexture(ThumbImage image, boolean thumb) { + if (image.url.isEmpty()) return ImageState.BLANK; String targetUrl = (thumb && !image.thumbUrl.isEmpty()) ? image.thumbUrl : image.url; synchronized (images) { if (images.containsKey(targetUrl)) return queryTexture(targetUrl); @@ -111,10 +112,14 @@ private static void applyImageData(String url, byte[] pngOrJpgImageData) { buffer.rewind(); Minecraft.getInstance().execute(() -> { try { - DynamicTexture dynamicTexture = new DynamicTexture(NativeImage.read(buffer)); + NativeImage pixels = NativeImage.read(buffer); + DynamicTexture dynamicTexture = new DynamicTexture(pixels); synchronized (images) { - if (!images.containsKey(url)) return; - images.get(url).texture = dynamicTexture; + ImageState sink = images.get(url); + if (sink == null) return; + sink.texture = dynamicTexture; + sink.width = pixels.getWidth(); + sink.height = pixels.getHeight(); } } catch (Throwable ex) { Main.LOGGER.warn("Cannot store image " + url, ex); @@ -128,16 +133,11 @@ private static void applyImageData(String url, byte[] pngOrJpgImageData) { }); } - private static AbstractTexture queryTexture(String url) { + private static ImageState queryTexture(String url) { synchronized (images) { ImageState state = images.get(url); state.onQuery(); - if (state.texture != null) return state.texture; - TextureManager textureManager = Minecraft.getInstance().getTextureManager(); - if (state.failed) return textureManager.getTexture( - Main.id("textures/gui/placeholder-failed.png")); - return textureManager.getTexture( - Main.id("textures/gui/placeholder-loading.png")); + return state; } } @@ -157,14 +157,38 @@ public static void purgeUnused() { } } - private static class ImageState { + public static class ImageState { public DynamicTexture texture; + public int width = 16, height = 9; public boolean failed; + public boolean blank; public long lastQueryTime; + private ImageState(boolean blank) { + this.blank = blank; + } + + public ImageState() { + this(false); + } + public void onQuery() { lastQueryTime = System.currentTimeMillis(); } + + public AbstractTexture getFriendlyTexture(TextureManager textureManager) { + if (failed) { + return textureManager.getTexture(Main.id("textures/gui/placeholder-failed.png")); + } else if (texture != null) { + return texture; + } else if (blank) { + return textureManager.getTexture(Main.id("textures/gui/placeholder-blank.png")); + } else { + return textureManager.getTexture(Main.id("textures/gui/placeholder-loading.png")); + } + } + + public static final ImageState BLANK = new ImageState(true); } } diff --git a/common/src/main/java/cn/zbx1425/worldcomment/gui/CommentListScreen.java b/common/src/main/java/cn/zbx1425/worldcomment/gui/CommentListScreen.java index 44e3831..4bc9b54 100644 --- a/common/src/main/java/cn/zbx1425/worldcomment/gui/CommentListScreen.java +++ b/common/src/main/java/cn/zbx1425/worldcomment/gui/CommentListScreen.java @@ -1,6 +1,5 @@ package cn.zbx1425.worldcomment.gui; -import cn.zbx1425.worldcomment.Main; import cn.zbx1425.worldcomment.data.CommentEntry; import cn.zbx1425.worldcomment.data.ServerWorldData; import cn.zbx1425.worldcomment.data.client.ClientWorldData; @@ -17,7 +16,6 @@ import net.minecraft.client.renderer.GameRenderer; import net.minecraft.core.BlockPos; import net.minecraft.network.chat.Component; -import net.minecraft.resources.ResourceLocation; import net.minecraft.util.Mth; #if MC_VERSION >= "11903" import org.joml.Matrix4f; #else import com.mojang.math.Matrix4f; #endif import java.util.*; @@ -137,23 +135,24 @@ public void render(#if MC_VERSION >= "12000" GuiGraphics #else PoseStack #endif if (subScreen == 3) { CommentEntry comment = commentForDetail; - int picWidth = width - 100 - 20 - 20; - int picHeight = picWidth * 9 / 16; - int x1 = 100 + 10, x2 = 100 + 10 + picWidth; - int y1 = 30 + 10, y2 = 30 + 10 + picHeight; + int maxPicWidth = width - 100 - 20 - 20; + int maxPicHeight = height - 30 - 20 - 20; int shadowColor = 0xFF000000; int shadowOffset = 3; + ImageDownload.ImageState imageToDraw = ImageDownload.getTexture(comment.image, false); + int picWidth = Math.min(maxPicWidth, maxPicHeight * imageToDraw.width / imageToDraw.height); + int picHeight = picWidth * imageToDraw.height / imageToDraw.width; + int x1 = 100 + 10, x2 = 100 + 10 + picWidth; + int y1 = 30 + 10, y2 = 30 + 10 + picHeight; guiGraphics.fill( (int) (x1 + shadowOffset), (int) (y1 + shadowOffset), (int) (x2 + shadowOffset), (int) (y2 + shadowOffset), shadowColor ); - if (!comment.image.url.isEmpty()) { - RenderSystem.setShaderTexture(0, ImageDownload.getTexture(comment.image, false).getId()); - } else { - RenderSystem.setShaderTexture(0, Main.id("textures/gui/placeholder-blank.png")); - } + + RenderSystem.setShaderTexture(0, imageToDraw.getFriendlyTexture(minecraft.getTextureManager()).getId()); + RenderSystem.setShader(GameRenderer::getPositionTexShader); Matrix4f matrix4f = guiGraphics.pose().last().pose(); #if MC_VERSION >= "12100" diff --git a/common/src/main/java/cn/zbx1425/worldcomment/gui/WidgetCommentEntry.java b/common/src/main/java/cn/zbx1425/worldcomment/gui/WidgetCommentEntry.java index 0ab3c04..a2f71ef 100644 --- a/common/src/main/java/cn/zbx1425/worldcomment/gui/WidgetCommentEntry.java +++ b/common/src/main/java/cn/zbx1425/worldcomment/gui/WidgetCommentEntry.java @@ -88,7 +88,8 @@ guiGraphics, getX(), getY(), getWidth(), getHeight(), } if (!comment.image.url.isEmpty() && showImage) { - RenderSystem.setShaderTexture(0, ImageDownload.getTexture(comment.image, true).getId()); + ImageDownload.ImageState imageToDraw = ImageDownload.getTexture(comment.image, true); + RenderSystem.setShaderTexture(0, imageToDraw.getFriendlyTexture(Minecraft.getInstance().getTextureManager()).getId()); RenderSystem.setShader(GameRenderer::getPositionTexShader); Matrix4f matrix4f = guiGraphics.pose().last().pose(); #if MC_VERSION >= "12100"