Skip to content

Commit 49480ca

Browse files
committed
Visualize tempo changes
1 parent f88159f commit 49480ca

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,19 +147,17 @@ public void delete() {
147147

148148
public void render(final Matrix4fStack positionMatrix) {
149149
final int height = ThinGL.getWindowFramebufferHeight();
150-
151150
this.renderedNotes = 0;
152151
this.updatePianoKeyPositions();
153152

154153
this.drawNotes(positionMatrix);
154+
GlobalObjects.GLOBAL_BATCH.draw();
155155

156156
positionMatrix.pushMatrix();
157157
positionMatrix.translate(0, height - (int) (height / PIANO_HEIGHT_DIVIDER), 0);
158158
this.drawPiano(positionMatrix);
159159
positionMatrix.popMatrix();
160-
161160
this.drawDebugText(positionMatrix);
162-
163161
GlobalObjects.GLOBAL_BATCH.draw();
164162
}
165163

@@ -169,11 +167,10 @@ private void drawNotes(final Matrix4fStack positionMatrix) {
169167
final float whiteKeyWidth = (float) width / WHITE_PIANO_KEY_COUNT;
170168
final float blackKeyWidth = whiteKeyWidth * BLACK_KEY_WIDTH_RATIO;
171169
final float noteSize = 16 * Math.max(1, width / 960);
172-
final float noteSquishFactor = 1F;
173170

174171
final Song song = this.songPlayer.getSong();
175-
final int tickWindow = MathUtils.ceilInt(height / (noteSize / noteSquishFactor));
176-
final int currentTick = this.songPlayer.getTick();
172+
final int tickWindow = MathUtils.ceilInt(height / noteSize);
173+
final int currentTick = this.songPlayer.getTick() - 1; // SongPlayer advances the tick immediately after playing the previous one
177174
final int endTick = currentTick + tickWindow;
178175
final float ticksPerSecond = this.songPlayer.getCurrentTicksPerSecond();
179176
final long lastTickTime = this.songPlayer.getLastTickTime();
@@ -182,7 +179,8 @@ private void drawNotes(final Matrix4fStack positionMatrix) {
182179
final float tickProgress = !paused ? MathUtils.clamp(timeSinceLastTick / (1_000_000_000F / ticksPerSecond), 0F, 1F) : 0F;
183180

184181
Renderer2D.INSTANCE.beginGlobalBuffering();
185-
for (int tick = endTick; tick >= currentTick; tick--) {
182+
for (int tick = endTick; tick >= currentTick - 1; tick--) {
183+
final float y = height - (tick - currentTick + 1 - tickProgress) * noteSize;
186184
for (Note note : song.getNotes().getOrEmpty(tick)) {
187185
final int nbsKey = note.getNbsKey();
188186
if (nbsKey < 0 || nbsKey >= this.pianoKeyPositions.length) {
@@ -195,7 +193,6 @@ private void drawNotes(final Matrix4fStack positionMatrix) {
195193
x += blackKeyWidth / 2F;
196194
}
197195
x -= noteSize / 2F;
198-
final float y = height - (tick - currentTick + 1 - tickProgress) * noteSize / noteSquishFactor;
199196

200197
final float alpha = MathUtils.clamp(note.getVolume(), 0.25F, 1F);
201198
if (note.getInstrument() instanceof MinecraftInstrument instrument) {
@@ -221,6 +218,18 @@ private void drawNotes(final Matrix4fStack positionMatrix) {
221218
}
222219
this.renderedNotes++;
223220
}
221+
if (song.getTempoEvents().get(tick) != 0) {
222+
final float bottomY = y + noteSize;
223+
final float tps = song.getTempoEvents().get(tick);
224+
final String tempoString = "Tempo: " + String.format("%.2f", tps) + " t/s";
225+
226+
this.textRenderer.setGlobalScale(ThinGL.getWindowFramebufferWidth() / 2000F);
227+
final float textHeight = this.textRenderer.calculateHeight(tempoString);
228+
this.textRenderer.renderString(positionMatrix, GlobalObjects.GLOBAL_BATCH, tempoString, 10, bottomY - textHeight - 2, 0, Color.WHITE);
229+
this.textRenderer.setGlobalScale(1F);
230+
231+
Renderer2D.INSTANCE.filledRectangle(positionMatrix, 0, bottomY, width, bottomY + 1, Color.WHITE.withAlpha(100));
232+
}
224233
}
225234
Renderer2D.INSTANCE.endBuffering();
226235
}
@@ -296,6 +305,9 @@ private void drawDebugText(final Matrix4fStack positionMatrix) {
296305
this.textRenderer.renderString(positionMatrix, GlobalObjects.GLOBAL_BATCH, "Position: " + currentPosition + " / " + this.songPlayer.getSong().getHumanReadableLength(), 5, textY, 0, Color.WHITE);
297306
textY += this.textRenderer.getPaddedHeight();
298307

308+
this.textRenderer.renderString(positionMatrix, GlobalObjects.GLOBAL_BATCH, "Tempo: " + String.format("%.2f", this.songPlayer.getCurrentTicksPerSecond())+ " t/s", 5, textY, 0, Color.WHITE);
309+
textY += this.textRenderer.getPaddedHeight();
310+
299311
if (this.songPlayer.getSoundSystem() != null) {
300312
this.textRenderer.renderString(positionMatrix, GlobalObjects.GLOBAL_BATCH, this.songPlayer.getSoundSystem().getStatusLine(), 5, textY, 0, Color.WHITE);
301313
textY += this.textRenderer.getPaddedHeight();

0 commit comments

Comments
 (0)