Skip to content

Commit

Permalink
Polished spotify support
Browse files Browse the repository at this point in the history
  • Loading branch information
tttsaurus committed Jan 8, 2025
1 parent 4db112b commit c7fd91d
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private static void addArcVertices(BufferBuilder bufferbuilder, float cx, float
}
}

public static void startStencil(float x, float y, float stencilWidth, float stencilHeight, int stencilValue)
public static void startRoundedRectStencil(float x, float y, float stencilWidth, float stencilHeight, int stencilValue, float radius)
{
if (!Minecraft.getMinecraft().getFramebuffer().isStencilEnabled())
Minecraft.getMinecraft().getFramebuffer().enableStencil();
Expand All @@ -160,12 +160,62 @@ public static void startStencil(float x, float y, float stencilWidth, float sten

// mask area
GL11.glStencilMask(0xFF);

int segments = Math.max(3, (int)(radius / 2f));
Tessellator tessellator = Tessellator.getInstance();
BufferBuilder bufferbuilder = tessellator.getBuffer();
bufferbuilder.begin(GL11.GL_POLYGON, DefaultVertexFormats.POSITION);

addArcVertices(bufferbuilder, x + stencilWidth - radius, y + radius, radius, 0, 90, segments);
bufferbuilder.pos(x + stencilWidth, y + radius, 0).endVertex();
bufferbuilder.pos(x + stencilWidth, y + stencilHeight - radius, 0).endVertex();
addArcVertices(bufferbuilder, x + stencilWidth - radius, y + stencilHeight - radius, radius, 90, 180, segments);
bufferbuilder.pos(x + stencilWidth - radius, y + stencilHeight, 0).endVertex();
bufferbuilder.pos(x + radius, y + stencilHeight, 0).endVertex();
addArcVertices(bufferbuilder, x + radius, y + stencilHeight - radius, radius, 180, 270, segments);
bufferbuilder.pos(x, y + stencilHeight - radius, 0).endVertex();
bufferbuilder.pos(x, y + radius, 0).endVertex();
addArcVertices(bufferbuilder, x + radius, y + radius, radius, 270, 360, segments);
bufferbuilder.pos(x + radius, y, 0).endVertex();
bufferbuilder.pos(x + stencilWidth - radius, y, 0).endVertex();

tessellator.draw();

GL11.glStencilMask(0x00);

GlStateManager.depthMask(true);
GlStateManager.colorMask(true, true, true, true);
GL11.glStencilFunc(GL11.GL_EQUAL, stencilValue, 0xFF);
GL11.glStencilOp(GL11.GL_KEEP, GL11.GL_KEEP, GL11.GL_KEEP);
}

public static void startRectStencil(float x, float y, float stencilWidth, float stencilHeight, int stencilValue)
{
if (!Minecraft.getMinecraft().getFramebuffer().isStencilEnabled())
Minecraft.getMinecraft().getFramebuffer().enableStencil();

GlStateManager.disableTexture2D();
GlStateManager.disableCull();

GL11.glEnable(GL11.GL_STENCIL_TEST);
//GL11.glClearStencil(0);
//GL11.glClear(GL11.GL_STENCIL_BUFFER_BIT);

GlStateManager.depthMask(false);
GlStateManager.colorMask(false, false, false, false);
GL11.glStencilFunc(GL11.GL_ALWAYS, stencilValue, 0xFF);
GL11.glStencilOp(GL11.GL_REPLACE, GL11.GL_REPLACE, GL11.GL_REPLACE);

// mask area
GL11.glStencilMask(0xFF);

GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(x, y);
GL11.glVertex2f(x + stencilWidth, y);
GL11.glVertex2f(x + stencilWidth, y + stencilHeight);
GL11.glVertex2f(x, y + stencilHeight);
GL11.glEnd();

GL11.glStencilMask(0x00);

GlStateManager.depthMask(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public String getDefaultIxml()
<Def debug = false>
<VerticalGroup padding = {"top": 10, "left": 10} backgroundStyle = "roundedBoxWithOutline">
<HorizontalGroup padding = {"top": 10, "bottom": 5}>
<UrlImage uid = "albumImage" width = 40 height = 40 padding = {"left": 10, "right": 10}>
<UrlImage uid = "albumImage" rounded = true width = 40 height = 40 padding = {"left": 10, "right": 10}>
</Group>
<HorizontalGroup padding = {"bottom": 5}>
<SlidingText uid = "trackTitle" width = 40 spareWidth = 20 onDemandSliding = true padding = {"left": 10, "right": 10}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public class SpotifyViewModel extends ViewModel<SpotifyView>
@Reactive(targetUid = "progressBar", property = "percentage", initiativeSync = true)
public ReactiveObject<Float> progressBarPercentage = new ReactiveObject<>(){};

private float durationMs = 0;
private float estimatedProgressMs;
private boolean isPlaying = false;

private void refreshTokenIfNeeded()
{
long timeSpan = Duration.between(SpotifyUserInfo.token.start, LocalDateTime.now()).getSeconds();
Expand Down Expand Up @@ -86,6 +90,9 @@ private void refreshTrackInfo()
if (trackPlaying.durationMs != 0)
percentage = ((float)trackPlaying.progressMs) / ((float)trackPlaying.durationMs);
progressBarPercentage.set(percentage);
durationMs = trackPlaying.durationMs;
estimatedProgressMs = trackPlaying.progressMs;
isPlaying = trackPlaying.isPlaying;
}
}
catch (Exception ignored) { }
Expand All @@ -97,6 +104,7 @@ private void refreshTrackInfo()
public void start()
{
activeSetter.invoke(false);
progressBarPercentage.set(0f);

EventCenter.spotifyOverlayEvent.addListener((flag) ->
{
Expand All @@ -118,6 +126,9 @@ public void start()
if (trackPlaying.durationMs != 0)
percentage = ((float)trackPlaying.progressMs) / ((float)trackPlaying.durationMs);
progressBarPercentage.set(percentage);
durationMs = trackPlaying.durationMs;
estimatedProgressMs = trackPlaying.progressMs;
isPlaying = trackPlaying.isPlaying;
}
}
catch (Exception ignored) { }
Expand Down Expand Up @@ -192,5 +203,21 @@ public void onFixedUpdate(double deltaTime)
refreshTrackTimer -= 3f;
refreshTrackInfo();
}

if (progressBarPercentage.get() < 1 && isPlaying)
{
int timeMs = (int)(deltaTime * 1000);
estimatedProgressMs += timeMs;
if (durationMs != 0)
{
if (estimatedProgressMs >= durationMs)
{
estimatedProgressMs = durationMs;
refreshTrackTimer = 2f;
}
float percentage = estimatedProgressMs / durationMs;
progressBarPercentage.set(percentage);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ public void onRenderUpdate(boolean focused)
{
if (rect.width == 0 || rect.height == 0) return;
RenderUtils.renderRoundedRect(rect.x, rect.y, rect.width, rect.height, rect.height / 2f, backgroundColor);
if (rect.width * percentage > rect.height)
RenderUtils.renderRoundedRect(rect.x, rect.y, rect.width * percentage, rect.height, rect.height / 2f, fillerColor);
if (percentage != 0)
{
RenderUtils.startRoundedRectStencil(rect.x, rect.y, rect.width, rect.height, 2, rect.height / 2f);
RenderUtils.renderRect(rect.x, rect.y, rect.width * percentage, rect.height, fillerColor);
RenderUtils.endStencil();
}
RenderUtils.renderRoundedRectOutline(rect.x, rect.y, rect.width, rect.height, rect.height / 2f, 1.0f, outlineColor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void onRenderUpdate(boolean focused)
{
if (!onDemandSliding || needSliding)
{
RenderUtils.startStencil(rect.x, rect.y, rect.width, rect.height, 1);
RenderUtils.startRectStencil(rect.x, rect.y, rect.width, rect.height, 1);

if (forwardSliding)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
import com.tttsaurus.ingameinfo.common.api.gui.style.CallbackInfo;
import com.tttsaurus.ingameinfo.common.api.gui.style.StyleProperty;
import com.tttsaurus.ingameinfo.common.api.gui.style.StylePropertyCallback;
import com.tttsaurus.ingameinfo.common.api.render.RenderUtils;
import com.tttsaurus.ingameinfo.common.impl.render.renderer.UrlImageRenderer;

@RegisterElement
public class UrlImage extends Sized
{
private final UrlImageRenderer urlImageRenderer = new UrlImageRenderer();

@StyleProperty
public boolean rounded;

@StylePropertyCallback
public void urlValidation(String value, CallbackInfo callbackInfo)
{
Expand Down Expand Up @@ -50,6 +54,10 @@ public void onFixedUpdate(double deltaTime)
@Override
public void onRenderUpdate(boolean focused)
{
if (rounded)
RenderUtils.startRoundedRectStencil(rect.x, rect.y, rect.width, rect.height, 1, 3f);
urlImageRenderer.render();
if (rounded)
RenderUtils.endStencil();
}
}

0 comments on commit c7fd91d

Please sign in to comment.