Skip to content

Commit

Permalink
Fix song album texture handling
Browse files Browse the repository at this point in the history
  • Loading branch information
KabanFriends committed Jun 14, 2024
1 parent d9ddbd4 commit 7617087
Showing 1 changed file with 80 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ public class SongInfoOverlay extends Overlay {
private static SongInfoOverlay instance;

private final TextureManager textureManager;
private final ScrollingText songTitleText;

private DynamicTexture albumArtTexture;
private ScrollingText songTitleText;
private boolean renderAlbumArt;
private boolean expanded;
private boolean muted;

Expand Down Expand Up @@ -122,7 +122,7 @@ public void render(GuiGraphics graphics, int mouseX, int mouseY) {
RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, 1.0f);

if (!GRConfig.<Boolean>getValue("hideAlbumArt")) {
graphics.blit(albumArtTexture == null ? ALBUM_ART_PLACEHOLDER_LOCATION : ALBUM_ART_LOCATION, x + ART_LEFT_PADDING, y + ART_TOP_PADDING, 0f, 0f, ART_SIZE, ART_SIZE, ART_SIZE, ART_SIZE);
graphics.blit(shouldRenderAlbumArt() ? ALBUM_ART_LOCATION : ALBUM_ART_PLACEHOLDER_LOCATION, x + ART_LEFT_PADDING, y + ART_TOP_PADDING, 0f, 0f, ART_SIZE, ART_SIZE, ART_SIZE, ART_SIZE);
}

poseStack.pushPose();
Expand Down Expand Up @@ -180,10 +180,10 @@ public void render(GuiGraphics graphics, int mouseX, int mouseY) {
long played = System.currentTimeMillis() / 1000L - SongHandler.getInstance().getSongStart();
if (played > duration) played = duration;

graphics.drawString(CraftGR.MC.font, getTimer((int) played), x + ART_LEFT_PADDING, y + ART_TOP_PADDING + ART_SIZE + ART_TIMER_SPACE_HEIGHT, COLOR_WHITE);
graphics.drawString(CraftGR.MC.font, formatTime((int) played), x + ART_LEFT_PADDING, y + ART_TOP_PADDING + ART_SIZE + ART_TIMER_SPACE_HEIGHT, COLOR_WHITE);

int timerWidth = font.width(getTimer((int) duration));
graphics.drawString(CraftGR.MC.font, getTimer((int) duration), x + (int) width - timerWidth - TIMER_RIGHT_PADDING, y + ART_TOP_PADDING + ART_SIZE + ART_TIMER_SPACE_HEIGHT, COLOR_WHITE);
int timerWidth = font.width(formatTime((int) duration));
graphics.drawString(CraftGR.MC.font, formatTime((int) duration), x + (int) width - timerWidth - TIMER_RIGHT_PADDING, y + ART_TOP_PADDING + ART_SIZE + ART_TIMER_SPACE_HEIGHT, COLOR_WHITE);

RenderUtil.fill(poseStack, x, y + ART_TOP_PADDING + ART_SIZE + ART_BOTTOM_PADDING, x + (float) played / duration * width, y + height, GRConfig.<Color>getValue("overlayBarColor").getRGB() + 0xFF000000, 0.6f);
RenderUtil.fill(poseStack, x + (float) played / duration * width, y + ART_TOP_PADDING + ART_SIZE + ART_BOTTOM_PADDING, x + width, y + height, GRConfig.<Color>getValue("overlayBgColor").getRGB() + 0xFF000000, 0.6f);
Expand Down Expand Up @@ -261,70 +261,6 @@ public boolean mouseClick(int mouseX, int mouseY) {
return true;
}

private int getOverlayX(OverlayPosition position, float width) {
float scale = GRConfig.getValue("overlayScale");
float offset = 10 / scale;
int x = (int)(CraftGR.MC.getWindow().getWidth() / scale - width - offset);

if (position == OverlayPosition.TOP_RIGHT || position == OverlayPosition.BOTTOM_RIGHT) {
return x;
} else {
return (int)offset;
}
}

private int getOverlayY(OverlayPosition position, float height) {
float scale = GRConfig.getValue("overlayScale");
float offset = 10 / scale;
int y = (int)(CraftGR.MC.getWindow().getHeight() / scale - height - offset);

if (position == OverlayPosition.BOTTOM_LEFT || position == OverlayPosition.BOTTOM_RIGHT) {
return y;
} else {
return (int)offset;
}
}

private float[] getOverlaySize() {
int albumArtWidth;
if (GRConfig.getValue("hideAlbumArt")) albumArtWidth = -ART_LEFT_PADDING;
else albumArtWidth = ART_SIZE;

float width;
float height = ART_TOP_PADDING + ART_SIZE + ART_BOTTOM_PADDING + PROGRESS_BAR_HEIGHT;

if (expanded) {
int maxWidth = getMaxTextWidth();

if (GRConfig.<Integer>getValue("overlayWidth") > maxWidth) {
maxWidth = GRConfig.getValue("overlayWidth");
}

width = ART_LEFT_PADDING + albumArtWidth + ART_INFO_SPACE_WIDTH + maxWidth * 2 + INFO_RIGHT_PADDING;
} else {
width = ART_LEFT_PADDING + albumArtWidth + ART_INFO_SPACE_WIDTH + GRConfig.<Integer>getValue("overlayWidth") * 2 + INFO_RIGHT_PADDING;
}

return new float[]{width, height};
}

private int getMaxTextWidth() {
Song song = SongHandler.getInstance().getCurrentSong();
Font font = CraftGR.MC.font;

if (song == null || song.isIntermission()) {
return font.width(songTitleText.getText());
}

return NumberUtils.max(
font.width(songTitleText.getText()),
font.width("(" + song.year + ")"),
font.width(song.artist),
font.width(song.album),
font.width(song.circle)
);
}

public void updateSongTitle() {
songTitleText.resetScroll();

Expand Down Expand Up @@ -355,7 +291,7 @@ public void updateScrollWidth() {
}

public void updateAlbumArtTexture() {
disposeAlbumArtTexture();
renderAlbumArt = false;

Song song = SongHandler.getInstance().getCurrentSong();
if (song == null || song.albumArt == null || song.albumArt.isEmpty()) {
Expand All @@ -378,23 +314,16 @@ public void updateAlbumArtTexture() {
ThreadLocals.PNG_INFO_BYPASS_VALIDATION.set(true);
NativeImage image = NativeImage.read(stream);

if (albumArtTexture == null) {
albumArtTexture = new DynamicTexture(image);
} else {
albumArtTexture.setPixels(image);
albumArtTexture.upload();
}

// OptiFine compatibility: RenderSystem only works in the main thread
CraftGR.MC.execute(() -> {
textureManager.register(ALBUM_ART_LOCATION, albumArtTexture);
textureManager.register(ALBUM_ART_LOCATION, new DynamicTexture(image));
renderAlbumArt = true;
});
}
break;
} catch (Exception e) {
CraftGR.log(Level.ERROR, "Error while creating album art texture! (" + url + ")");
e.printStackTrace();
disposeAlbumArtTexture();
textureManager.release(ALBUM_ART_LOCATION);
} finally {
ThreadLocals.PNG_INFO_BYPASS_VALIDATION.remove();
}
Expand All @@ -409,14 +338,6 @@ public void updateAlbumArtTexture() {
} while (tries < ALBUM_ART_FETCH_TRIES);
}

private void disposeAlbumArtTexture() {
if (albumArtTexture != null) {
textureManager.release(ALBUM_ART_LOCATION);
albumArtTexture.close();
albumArtTexture = null;
}
}

public InputStream resizeImage(InputStream input) throws IOException {
try (input) {
Image image = ImageIO.read(input);
Expand Down Expand Up @@ -445,7 +366,76 @@ public enum OverlayVisibility {
CHAT
}

private static String getTimer(int time) {
private int getOverlayX(OverlayPosition position, float width) {
float scale = GRConfig.getValue("overlayScale");
float offset = 10 / scale;
int x = (int)(CraftGR.MC.getWindow().getWidth() / scale - width - offset);

if (position == OverlayPosition.TOP_RIGHT || position == OverlayPosition.BOTTOM_RIGHT) {
return x;
} else {
return (int)offset;
}
}

private int getOverlayY(OverlayPosition position, float height) {
float scale = GRConfig.getValue("overlayScale");
float offset = 10 / scale;
int y = (int)(CraftGR.MC.getWindow().getHeight() / scale - height - offset);

if (position == OverlayPosition.BOTTOM_LEFT || position == OverlayPosition.BOTTOM_RIGHT) {
return y;
} else {
return (int)offset;
}
}

private float[] getOverlaySize() {
int albumArtWidth;
if (GRConfig.getValue("hideAlbumArt")) albumArtWidth = -ART_LEFT_PADDING;
else albumArtWidth = ART_SIZE;

float width;
float height = ART_TOP_PADDING + ART_SIZE + ART_BOTTOM_PADDING + PROGRESS_BAR_HEIGHT;

if (expanded) {
int maxWidth = getMaxTextWidth();

if (GRConfig.<Integer>getValue("overlayWidth") > maxWidth) {
maxWidth = GRConfig.getValue("overlayWidth");
}

width = ART_LEFT_PADDING + albumArtWidth + ART_INFO_SPACE_WIDTH + maxWidth * 2 + INFO_RIGHT_PADDING;
} else {
width = ART_LEFT_PADDING + albumArtWidth + ART_INFO_SPACE_WIDTH + GRConfig.<Integer>getValue("overlayWidth") * 2 + INFO_RIGHT_PADDING;
}

return new float[]{width, height};
}

private int getMaxTextWidth() {
Song song = SongHandler.getInstance().getCurrentSong();
Font font = CraftGR.MC.font;

if (song == null || song.isIntermission()) {
return font.width(songTitleText.getText());
}

return NumberUtils.max(
font.width(songTitleText.getText()),
font.width("(" + song.year + ")"),
font.width(song.artist),
font.width(song.album),
font.width(song.circle)
);
}

@SuppressWarnings("ConstantConditions")
private boolean shouldRenderAlbumArt() {
return renderAlbumArt && textureManager.getTexture(ALBUM_ART_LOCATION, null) != null;
}

private static String formatTime(int time) {
int minutes = time / 60;
int seconds = time % 60;

Expand Down

0 comments on commit 7617087

Please sign in to comment.