Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added note labels on rendered waterfall notes. #231

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion neothesia-core/src/render/guidelines.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{sync::Arc, time::Duration};

use crate::{
render::{QuadInstance, QuadPipeline},
render::{QuadInstance, QuadPipeline, TextInstance, TextPipeline}, // Move text types to same level as Quad types
utils::Point,
};

Expand All @@ -13,6 +13,7 @@ pub struct GuidelineRenderer {
horizontal_guidelines: bool,

cache: Vec<QuadInstance>,
text_cache: Vec<TextInstance>,
measures: Arc<[Duration]>,
}

Expand All @@ -30,18 +31,21 @@ impl GuidelineRenderer {
vertical_guidelines,
horizontal_guidelines,
cache: Vec::new(),
text_cache: Vec::new(),
measures,
}
}

pub fn set_pos(&mut self, pos: Point<f32>) {
self.pos = pos;
self.cache.clear();
self.text_cache.clear();
}

pub fn set_layout(&mut self, layout: piano_layout::KeyboardLayout) {
self.layout = layout;
self.cache.clear();
self.text_cache.clear();
}

/// Reupload instances to GPU
Expand Down Expand Up @@ -74,6 +78,42 @@ impl GuidelineRenderer {
color,
border_radius: [0.0, 0.0, 0.0, 0.0],
});

// Add text instance for note label
self.text_cache.push(TextInstance {
position: [x, y],
text: key.note_id().to_string(),
color: [1.0, 1.0, 1.0, 1.0],
scale: 1.0,
});
}
}

// Helper method to convert key index to note name
fn get_note_name(key_index: usize) -> &'static str {
const NOTE_NAMES: [&str; 12] = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"];
NOTE_NAMES[key_index % 12]
}

#[profiling::function]
fn update_vertical_guidelines(&mut self, quads: &mut QuadPipeline, layer: usize) {
for key in self.layout.keys.iter() {
let x = self.pos.x + key.x();
let y = self.pos.y;

quads.instances(layer).push(QuadInstance {
position: [x, y],
size: [2.0, self.layout.height], // Use height field directly
color: [0.2, 0.2, 0.2, 0.5],
border_radius: [0.0, 0.0, 0.0, 0.0],
});

self.text_cache.push(TextInstance {
position: [x + 2.0, y + 2.0],
text: Self::get_note_name(key.note_id() as usize).to_string(),
color: [1.0, 1.0, 1.0, 1.0],
scale: 0.8,
});
}
}

Expand Down Expand Up @@ -113,6 +153,7 @@ impl GuidelineRenderer {
pub fn update(
&mut self,
quads: &mut QuadPipeline,
texts: &mut TextPipeline,
layer: usize,
animation_speed: f32,
time: f32,
Expand All @@ -128,5 +169,10 @@ impl GuidelineRenderer {
for quad in self.cache.iter() {
quads.instances(layer).push(*quad);
}

for text in self.text_cache.iter() {
texts.instances(layer).push(*text);
}
}
}

2 changes: 2 additions & 0 deletions note_names.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub fn get_note_name(midi_note: u8) -> String {
const NOTE_NAMES: [&str;
Loading