Skip to content

Commit 23ec3f9

Browse files
committed
Improved keyboard design
1 parent 7164619 commit 23ec3f9

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

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

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,33 +230,46 @@ private void drawPiano(final Matrix4fStack positionMatrix) {
230230
final float blackKeyWidth = whiteKeyWidth * BLACK_KEY_WIDTH_RATIO;
231231

232232
Renderer2D.INSTANCE.beginGlobalBuffering();
233-
Renderer2D.INSTANCE.filledRect(positionMatrix, 0, 0, width, height, Color.BLACK);
234233

235234
final float keyLineOffset = height / KEY_LINE_OFFSET_RATIO;
236235
for (int nbsKey = 0; nbsKey < this.pianoKeyPositions.length; nbsKey++) {
237236
final float x = this.pianoKeyPositions[nbsKey];
238237
final float progress = this.pianoKeyLastColors[nbsKey] != null ? MathUtils.clamp((System.nanoTime() - this.pianoKeyLastPlayed[nbsKey]) / (float) KEY_ANIMATION_DURATION, 0F, 1F) : 1F;
239238
final float colorProgress = progress < 0.5F ? 0 : (progress - 0.5F) * 2;
240239
final float pressOffset = height / KEY_PRESS_DEPTH_RATIO - height / KEY_PRESS_DEPTH_RATIO * (progress < 0.5F ? 1F - progress : progress);
240+
final String noteName = this.getNoteName(nbsKey);
241241
if (!this.isBlackKey(nbsKey)) {
242+
Renderer2D.INSTANCE.filledRect(positionMatrix, x, pressOffset, x + 1, height, Color.BLACK);
243+
Renderer2D.INSTANCE.filledRect(positionMatrix, x + whiteKeyWidth - 1, pressOffset, x + whiteKeyWidth, height, Color.BLACK);
242244
Renderer2D.INSTANCE.filledRect(positionMatrix, x + 1, pressOffset, x + whiteKeyWidth - 1, height, Color.WHITE);
243245
if (this.pianoKeyLastColors[nbsKey] != null) {
244246
Renderer2D.INSTANCE.filledRect(positionMatrix, x + 1, pressOffset, x + whiteKeyWidth - 1, height, this.pianoKeyLastColors[nbsKey].withAlpha(Math.round(PRESSED_KEY_COLOR_ALPHA * (1 - colorProgress))));
245247
}
246248
Renderer2D.INSTANCE.filledRect(positionMatrix, x, height - keyLineOffset + pressOffset, x + whiteKeyWidth, height - keyLineOffset - KEY_LINE_HEIGHT + pressOffset, Color.GRAY);
249+
250+
this.textRenderer.setGlobalScale(ThinGL.getWindowFramebufferWidth() / 2745F); // 0,7
251+
final float nameWidth = this.textRenderer.calculateWidth(noteName);
252+
this.textRenderer.renderString(positionMatrix, GlobalObjects.GLOBAL_BATCH, this.getNoteName(nbsKey), x + whiteKeyWidth / 2 - nameWidth / 2, height - keyLineOffset - KEY_LINE_HEIGHT + pressOffset - this.textRenderer.getPaddedHeight(), 0, Color.BLACK);
253+
this.textRenderer.setGlobalScale(1F);
247254
} else {
248255
positionMatrix.pushMatrix();
249256
positionMatrix.translate(0, 0, 1);
250-
Renderer2D.INSTANCE.filledRect(positionMatrix, x, pressOffset, x + blackKeyWidth, height * BLACK_KEY_HEIGHT_RATIO, Color.BLACK);
257+
258+
Renderer2D.INSTANCE.filledRect(positionMatrix, x, pressOffset - height / KEY_PRESS_DEPTH_RATIO / 2, x + blackKeyWidth, height * BLACK_KEY_HEIGHT_RATIO, Color.BLACK);
251259
if (this.pianoKeyLastColors[nbsKey] != null) {
252-
Renderer2D.INSTANCE.filledRect(positionMatrix, x, pressOffset, x + blackKeyWidth, height * BLACK_KEY_HEIGHT_RATIO, this.pianoKeyLastColors[nbsKey].withAlpha(Math.round(PRESSED_KEY_COLOR_ALPHA * (1 - colorProgress))));
260+
Renderer2D.INSTANCE.filledRect(positionMatrix, x, pressOffset - height / KEY_PRESS_DEPTH_RATIO / 2, x + blackKeyWidth, height * BLACK_KEY_HEIGHT_RATIO, this.pianoKeyLastColors[nbsKey].withAlpha(Math.round(PRESSED_KEY_COLOR_ALPHA * (1 - colorProgress))));
253261
}
254262
Renderer2D.INSTANCE.filledRect(positionMatrix, x, height * BLACK_KEY_HEIGHT_RATIO - keyLineOffset + pressOffset, x + blackKeyWidth, height * BLACK_KEY_HEIGHT_RATIO - keyLineOffset - KEY_LINE_HEIGHT + pressOffset, Color.GRAY);
263+
264+
this.textRenderer.setGlobalScale(ThinGL.getWindowFramebufferWidth() / 4000F); // 0,48
265+
final float nameWidth = this.textRenderer.calculateWidth(noteName);
266+
this.textRenderer.renderString(positionMatrix, GlobalObjects.GLOBAL_BATCH, this.getNoteName(nbsKey), x + blackKeyWidth / 2 - nameWidth / 2, height * BLACK_KEY_HEIGHT_RATIO - keyLineOffset - KEY_LINE_HEIGHT + pressOffset - this.textRenderer.getPaddedHeight(), 0, Color.WHITE);
267+
this.textRenderer.setGlobalScale(1F);
268+
255269
positionMatrix.popMatrix();
256270
}
257271
}
258272

259-
Renderer2D.INSTANCE.filledRect(positionMatrix, 0, -1, width, 1, Color.RED);
260273
Renderer2D.INSTANCE.endBuffering();
261274
}
262275

@@ -298,6 +311,27 @@ private boolean isBlackKey(final int nbsKey) {
298311
return noteInOctave == 1 || noteInOctave == 3 || noteInOctave == 6 || noteInOctave == 8 || noteInOctave == 10;
299312
}
300313

314+
private String getNoteName(final int nbsKey) {
315+
final int noteInOctave = nbsKey % 12;
316+
String noteName = switch (noteInOctave) {
317+
case 0 -> "A";
318+
case 1 -> "A#";
319+
case 2 -> "B";
320+
case 3 -> "C";
321+
case 4 -> "C#";
322+
case 5 -> "D";
323+
case 6 -> "D#";
324+
case 7 -> "E";
325+
case 8 -> "F";
326+
case 9 -> "F#";
327+
case 10 -> "G";
328+
case 11 -> "G#";
329+
default -> throw new IllegalStateException("Unexpected value: " + noteInOctave);
330+
};
331+
noteName += (nbsKey + NbsDefinitions.NBS_LOWEST_MIDI_KEY) / 12 - 1;
332+
return noteName;
333+
}
334+
301335
private void updatePianoKeyPositions() {
302336
Arrays.fill(this.pianoKeyPositions, 0F);
303337
final int width = ThinGL.getWindowFramebufferWidth();

0 commit comments

Comments
 (0)