Skip to content

Commit aceff2f

Browse files
committed
Animate keys when they are pressed down
1 parent 3ed4995 commit aceff2f

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/main/java/net/raphimc/noteblocktool/frames/visualizer/DropRenderer.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ public class DropRenderer {
5353
private static final float BLACK_KEY_WIDTH_RATIO = 0.6F;
5454
private static final float BLACK_KEY_HEIGHT_RATIO = 0.6F;
5555
private static final float PIANO_HEIGHT_DIVIDER = 7F;
56+
private static final int KEY_LINE_HEIGHT = 2;
57+
private static final int KEY_LINE_OFFSET = 25;
58+
private static final int KEY_PRESS_DEPTH = 20;
59+
private static final long KEY_ANIMATION_DURATION = 250_000_000L;
5660

5761
private final SoundSystemSongPlayer songPlayer;
5862
private Font robotoFont;
@@ -207,7 +211,12 @@ private void drawNotes(final Matrix4fStack positionMatrix) {
207211
Renderer2D.INSTANCE.filledRect(positionMatrix, x, y, x + noteSize, y + noteSize, Color.WHITE.withAlphaF(alpha));
208212
}
209213
if (tick == currentTick) {
210-
this.pianoKeyLastPlayed[nbsKey] = System.nanoTime();
214+
final long currentTime = System.nanoTime();
215+
if (currentTime - this.pianoKeyLastPlayed[nbsKey] < currentTime + KEY_ANIMATION_DURATION / 2) {
216+
this.pianoKeyLastPlayed[nbsKey] = currentTime - KEY_ANIMATION_DURATION / 2;
217+
} else {
218+
this.pianoKeyLastPlayed[nbsKey] = System.nanoTime();
219+
}
211220
}
212221
this.renderedNotes++;
213222
}
@@ -226,20 +235,22 @@ private void drawPiano(final Matrix4fStack positionMatrix) {
226235

227236
for (int nbsKey = 0; nbsKey < this.pianoKeyPositions.length; nbsKey++) {
228237
final float x = this.pianoKeyPositions[nbsKey];
238+
final float progress = this.pianoKeyLastColors[nbsKey] != null ? MathUtils.clamp((System.nanoTime() - this.pianoKeyLastPlayed[nbsKey]) / (float) KEY_ANIMATION_DURATION, 0F, 1F) : 1F;
239+
final float pressOffset = KEY_PRESS_DEPTH - KEY_PRESS_DEPTH * (progress < 0.5F ? 1F - progress : progress);
229240
if (!this.isBlackKey(nbsKey)) {
230-
Renderer2D.INSTANCE.filledRect(positionMatrix, x + 1, 0, x + whiteKeyWidth - 1, height, Color.WHITE);
241+
Renderer2D.INSTANCE.filledRect(positionMatrix, x + 1, pressOffset, x + whiteKeyWidth - 1, height, Color.WHITE);
231242
if (this.pianoKeyLastColors[nbsKey] != null) {
232-
final float alpha = 1F - MathUtils.clamp((System.nanoTime() - this.pianoKeyLastPlayed[nbsKey]) / 500_000_000F, 0F, 1F);
233-
Renderer2D.INSTANCE.filledRect(positionMatrix, x + 1, 0, x + whiteKeyWidth - 1, height, this.pianoKeyLastColors[nbsKey].withAlpha(Math.round(200 * alpha)));
243+
Renderer2D.INSTANCE.filledRect(positionMatrix, x + 1, pressOffset, x + whiteKeyWidth - 1, height, this.pianoKeyLastColors[nbsKey].withAlpha(Math.round(200 * (1 - progress))));
234244
}
245+
Renderer2D.INSTANCE.filledRect(positionMatrix, x, height - KEY_LINE_OFFSET + pressOffset, x + whiteKeyWidth, height - KEY_LINE_OFFSET - KEY_LINE_HEIGHT + pressOffset, Color.GRAY);
235246
} else {
236247
positionMatrix.pushMatrix();
237248
positionMatrix.translate(0, 0, 1);
238-
Renderer2D.INSTANCE.filledRect(positionMatrix, x, 0, x + blackKeyWidth, height * BLACK_KEY_HEIGHT_RATIO, Color.BLACK);
249+
Renderer2D.INSTANCE.filledRect(positionMatrix, x, pressOffset, x + blackKeyWidth, height * BLACK_KEY_HEIGHT_RATIO, Color.BLACK);
239250
if (this.pianoKeyLastColors[nbsKey] != null) {
240-
final float alpha = 1F - MathUtils.clamp((System.nanoTime() - this.pianoKeyLastPlayed[nbsKey]) / 500_000_000F, 0F, 1F);
241-
Renderer2D.INSTANCE.filledRect(positionMatrix, x, 0, x + blackKeyWidth, height * BLACK_KEY_HEIGHT_RATIO, this.pianoKeyLastColors[nbsKey].withAlpha(Math.round(200 * alpha)));
251+
Renderer2D.INSTANCE.filledRect(positionMatrix, x, pressOffset, x + blackKeyWidth, height * BLACK_KEY_HEIGHT_RATIO, this.pianoKeyLastColors[nbsKey].withAlpha(Math.round(200 * (1 - progress))));
242252
}
253+
Renderer2D.INSTANCE.filledRect(positionMatrix, x, height * BLACK_KEY_HEIGHT_RATIO - KEY_LINE_OFFSET + pressOffset, x + blackKeyWidth, height * BLACK_KEY_HEIGHT_RATIO - KEY_LINE_OFFSET - KEY_LINE_HEIGHT + pressOffset, Color.GRAY);
243254
positionMatrix.popMatrix();
244255
}
245256
}

0 commit comments

Comments
 (0)