Skip to content

Commit

Permalink
Fix image aspect ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
zbx1425 committed Mar 1, 2025
1 parent 34b56bd commit 551f2ad
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public class ImageDownload {

private static final Map<String, ImageState> 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);
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
}

Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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.*;
Expand Down Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 551f2ad

Please sign in to comment.