Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions HMCL/src/main/java/org/jackhuang/hmcl/game/CapePreview.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* Hello Minecraft! Launcher
* Copyright (C) 2026 huangyuhui <huanghongxun2008@126.com> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.jackhuang.hmcl.game;

import javafx.scene.image.Image;
import javafx.scene.image.PixelReader;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import org.jackhuang.hmcl.auth.yggdrasil.Texture;
import org.jackhuang.hmcl.util.StringUtils;
import org.jetbrains.annotations.NotNullByDefault;
import org.jetbrains.annotations.Nullable;

import static org.jackhuang.hmcl.util.logging.Logger.LOG;

/// Generates the local UI preview of a Minecraft cape from its raw UV texture.
///
/// The raw `capes[].url` texture is a UV unwrap (for example 64x32) and must
/// not be shown as-is. This class downloads and caches that raw texture through
/// [TexturesLoader], then extracts only the cape front face so the launcher
/// renders a correctly proportioned preview.
///
/// The raw texture itself is never modified: the returned image is only ever
/// used for local display.
@NotNullByDefault
public final class CapePreview {

/// Display aspect ratio of a cape front face (10x16).
public static final double ASPECT_RATIO = 10.0 / 16.0;

/// Reference width, in pixels, of the standard 64x32 cape texture.
private static final double BASE_WIDTH = 64.0;

/// UV region of the cape front face in the standard 64x32 layout.
private static final double FRONT_X = 1.0;
private static final double FRONT_Y = 1.0;
private static final double FRONT_WIDTH = 10.0;
private static final double FRONT_HEIGHT = 16.0;

private CapePreview() {
}

/// Downloads and caches the raw cape texture for `url`, then returns the
/// front-face preview image.
///
/// @param url the raw Minecraft cape texture URL
/// @return the cropped preview, or `null` when the texture is unavailable
public static @Nullable Image load(@Nullable String url) {
if (StringUtils.isBlank(url)) {
return null;
}

try {
TexturesLoader.LoadedTexture texture = TexturesLoader.loadTexture(new Texture(url, null));
return extractFrontFace(texture.image());
} catch (Throwable e) {
LOG.warning("Failed to load cape preview: " + url, e);
return null;
}
}

/// Extracts the cape front face from an already decoded raw cape texture.
///
/// The front face is read from the region `(1, 1, 10, 16)` of the standard
/// 64x32 UV layout, scaled by `width / 64` so that higher-resolution
/// textures (128x64, 256x128, ...) are handled proportionally. The
/// rectangle is clamped into the texture bounds, so unknown or irregular
/// sizes degrade gracefully instead of failing.
///
/// @param texture the decoded raw cape texture
/// @return the cropped front-face preview, or `null` when the texture is unreadable
public static @Nullable Image extractFrontFace(@Nullable Image texture) {
if (texture == null) {
return null;
}

int width = (int) texture.getWidth();
int height = (int) texture.getHeight();
int[] region = computeFrontRegion(width, height);
if (region == null) {
return null;
}

int x = region[0];
int y = region[1];
int w = region[2];
int h = region[3];

PixelReader reader = texture.getPixelReader();
if (reader == null) {
return null;
}

WritableImage preview = new WritableImage(w, h);
PixelWriter writer = preview.getPixelWriter();
for (int py = 0; py < h; py++) {
for (int px = 0; px < w; px++) {
writer.setArgb(px, py, reader.getArgb(x + px, y + py));
}
}
return preview;
}

/// Computes the integer pixel rectangle of the cape front face for a texture
/// of the given size.
///
/// @return `{x, y, width, height}`, or `null` when the size cannot be mapped
static int @Nullable [] computeFrontRegion(int width, int height) {
if (width <= 0 || height <= 0) {
return null;
}

double scale = width / BASE_WIDTH;
int x = Math.max(0, Math.min((int) Math.round(FRONT_X * scale), width - 1));
int y = Math.max(0, Math.min((int) Math.round(FRONT_Y * scale), height - 1));
int w = clamp((int) Math.round(FRONT_WIDTH * scale), 1, width - x);
int h = clamp((int) Math.round(FRONT_HEIGHT * scale), 1, height - y);
return new int[]{x, y, w, h};
}

/// Clamps `value` into `[min, max]`.
private static int clamp(int value, int min, int max) {
if (value < min) {
return min;
}
if (value > max) {
return max;
}
return value;
}
}
2 changes: 2 additions & 0 deletions HMCL/src/main/java/org/jackhuang/hmcl/setting/Accounts.java
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,8 @@ public static String localizeErrorMessage(Exception exception) {
return i18n("account.methods.microsoft.error.no_character");
} else if (exception instanceof MicrosoftService.NoXuiException) {
return i18n("account.methods.microsoft.error.add_family");
} else if (exception instanceof MicrosoftService.MinecraftServicesRateLimitException) {
return i18n("account.cape.rate_limited");
} else if (exception instanceof OAuthServer.MicrosoftAuthenticationNotSupportedException) {
return i18n("account.methods.microsoft.snapshot");
} else if (exception instanceof OAuthAccount.WrongAccountException) {
Expand Down
1 change: 1 addition & 0 deletions HMCL/src/main/java/org/jackhuang/hmcl/ui/SVG.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public enum SVG {
ARROW_FORWARD("M16.175 13H4V11H16.175L10.575 5.4 12 4 20 12 12 20 10.575 18.6 16.175 13Z"),
BETA_CIRCLE("M15,10.5C15,11.3 14.3,12 13.5,12C14.3,12 15,12.7 15,13.5V15A2,2 0 0,1 13,17H9V7H13A2,2 0 0,1 15,9V10.5M13,15V13H11V15H13M13,11V9H11V11H13M12,2A10,10 0 0,1 22,12A10,10 0 0,1 12,22A10,10 0 0,1 2,12A10,10 0 0,1 12,2M12,4A8,8 0 0,0 4,12A8,8 0 0,0 12,20A8,8 0 0,0 20,12A8,8 0 0,0 12,4Z"), // Not Material
CANCEL("M8.4 17 12 13.4 15.6 17 17 15.6 13.4 12 17 8.4 15.6 7 12 10.6 8.4 7 7 8.4 10.6 12 7 15.6 8.4 17ZM12 22Q9.925 22 8.1 21.2125T4.925 19.075Q3.575 17.725 2.7875 15.9T2 12Q2 9.925 2.7875 8.1T4.925 4.925Q6.275 3.575 8.1 2.7875T12 2Q14.075 2 15.9 2.7875T19.075 4.925Q20.425 6.275 21.2125 8.1T22 12Q22 14.075 21.2125 15.9T19.075 19.075Q17.725 20.425 15.9 21.2125T12 22ZM12 20Q15.35 20 17.675 17.675T20 12Q20 8.65 17.675 6.325T12 4Q8.65 4 6.325 6.325T4 12Q4 15.35 6.325 17.675T12 20ZM12 12Z"),
CAPE("M7 5h10v3l2 2v9h-4l-3-2-3 2H5v-9l2-2V5Zm2 2v2l-2 1.5V17h2.5l2.5-1.7 2.5 1.7H17v-6.5L15 9V7H9Z"), // Not Material
CHAT("M6 14H14V12H6V14ZM6 11H18V9H6V11ZM6 8H18V6H6V8ZM2 22V4Q2 3.175 2.5875 2.5875T4 2H20Q20.825 2 21.4125 2.5875T22 4V16Q22 16.825 21.4125 17.4125T20 18H6L2 22ZM5.15 16H20V4H4V17.125L5.15 16ZM4 16V4 16Z"),
CHECK("M9.55 18 3.85 12.3 5.275 10.875 9.55 15.15 18.725 5.975 20.15 7.4 9.55 18Z"),
CHECKROOM("M3 20Q2.575 20 2.2875 19.7125T2 19Q2 18.75 2.1 18.5375T2.4 18.2L11 11.75V10Q11 9.575 11.3 9.2875T12.025 9Q12.65 9 13.075 8.55T13.5 7.475Q13.5 6.85 13.0625 6.425T12 6Q11.375 6 10.9375 6.4375T10.5 7.5H8.5Q8.5 6.05 9.525 5.025T12 4Q13.45 4 14.475 5.0125T15.5 7.475Q15.5 8.65 14.8125 9.575T13 10.85V11.75L21.6 18.2Q21.8 18.325 21.9 18.5375T22 19Q22 19.425 21.7125 19.7125T21 20H3ZM6 18H18L12 13.5 6 18Z"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ public AccountListItemSkin(AccountListItem skinnable) {
spinnerUpload.getStyleClass().add("small-spinner-pane");
right.getChildren().add(spinnerUpload);

if (skinnable.getAccount() instanceof MicrosoftAccount microsoftAccount) {
JFXButton btnCape = FXUtils.newToggleButton4(SVG.CAPE);
btnCape.setOnAction(e -> Controllers.dialog(new MicrosoftAccountCapePane(microsoftAccount)));
FXUtils.installFastTooltip(btnCape, i18n("account.cape.manage"));
right.getChildren().add(btnCape);
}

JFXButton btnCopyUUID = FXUtils.newToggleButton4(SVG.CONTENT_COPY);
btnCopyUUID.setOnAction(e -> FXUtils.copyText(skinnable.getAccount().getProfileID().toString()));
FXUtils.installFastTooltip(btnCopyUUID, i18n("account.copy_uuid"));
Expand Down
Loading