mostly pointless refactors

This commit is contained in:
🪞👃🪞 2024-09-30 10:46:50 +03:00
parent a27b39e595
commit d42512fc76

View file

@ -521,51 +521,53 @@ impl<'a> Content for VerticalArranger<'a, Tui> {
}; };
//let height = rows.last().map(|(w,y)|(y+w)/ppq).unwrap_or(16); //let height = rows.last().map(|(w,y)|(y+w)/ppq).unwrap_or(16);
let tracks: &[Sequencer<Tui>] = state.tracks.as_ref(); let tracks: &[Sequencer<Tui>] = state.tracks.as_ref();
let scenes = state.scenes.as_ref(); let scenes: &[Scene] = state.scenes.as_ref();
let offset = 4 + scene_name_max_len(scenes) as u16; let offset = 4 + scene_name_max_len(scenes) as u16;
Layers::new(move |add|{ Layers::new(move |add|{
let rows = rows.as_ref(); let rows: &[(usize, usize)] = rows.as_ref();
let cols = cols.as_ref(); let cols: &[(usize, usize)] = cols.as_ref();
add(&VerticalArrangerGrid(offset, &rows, &cols))?; let track_titles = row!((track, (w, _)) in tracks.iter().zip(cols) =>
add(&VerticalArrangerCursor(state.focused, state.selected, offset, &cols, &rows))?;
let track_titles = row!((track, (w, _)) in tracks.iter().zip(cols) => {
(&track.name.read().unwrap().as_str() as &dyn Widget<Engine = Tui>) (&track.name.read().unwrap().as_str() as &dyn Widget<Engine = Tui>)
.bg(COLOR_BG1).min_xy(*w as u16, 2) .min_xy(*w as u16, 2).push_x(offset));
}.push_x(offset)); let scene_name = |scene, playing: bool, height|row!(
let track_clips = col!((scene, (pulses, _)) in scenes.iter().zip(rows) => { if playing { "" } else { " " },
(scene as &Scene).name.read().unwrap().as_str(),
).fixed_xy(offset.saturating_sub(1), height);
let scene_clip = |scene, track: usize, w: u16, h: u16|Layers::new(move |add|{
let mut color = Color::Rgb(40, 50, 30);
match (tracks.get(track), (scene as &Scene).clips.get(track)) {
(Some(track), Some(Some(clip))) => match track.phrases.get(*clip) {
Some(phrase) => {
let name = &(phrase as &Arc<RwLock<Phrase>>).read().unwrap().name;
let name = name.read().unwrap();
let name = format!("{clip:02} {}", name);
add(&name.as_str().push_x(1))?;
if (track as &Sequencer<_>).sequence == Some(*clip) {
color = COLOR_PLAYING
} else {
color = COLOR_BG1
};
},
_ => {}
},
_ => {}
};
add(&Background(color))
}).fixed_xy(w, h);
let tracks_clips = col!((scene, (pulses, _)) in scenes.iter().zip(rows) => {
let height = 1.max((pulses / 96) as u16); let height = 1.max((pulses / 96) as u16);
let playing = scene.is_playing(tracks); let playing = scene.is_playing(tracks);
Stack::right(move |add| { Stack::right(move |add| {
add(&Stack::right(|add|{ add(&scene_name(scene, playing, height))?;
add(&if playing { "" } else { " " })?;
add(&scene.name.read().unwrap().as_str())
}).fixed_xy(offset.saturating_sub(1), height))?;
for (track, (w, _x)) in cols.iter().enumerate() { for (track, (w, _x)) in cols.iter().enumerate() {
add(&Layers::new(move |add|{ add(&scene_clip(scene, track, *w as u16, height))?;
let mut color = Color::Rgb(40, 50, 30);
if let (Some(track), Some(Some(clip))) = (
tracks.get(track),
scene.clips.get(track),
) {
if let Some(phrase) = track.phrases.get(*clip) {
add(&format!(
"{clip:02} {}",
phrase.read().unwrap().name.read().unwrap()
).as_str().push_x(1))?;
color = if track.sequence == Some(*clip) {
COLOR_PLAYING
} else {
COLOR_BG1
};
}
}
add(&Background(color))
}).fixed_xy(*w as u16, height))?;
} }
Ok(()) Ok(())
}).fixed_y(height) }).fixed_y(height)
}); });
add(&col!(track_titles, track_clips))?; add(&VerticalArrangerGrid(offset, &rows, &cols))?;
add(&VerticalArrangerCursor(state.focused, state.selected, offset, &cols, &rows))?;
add(&col!(track_titles, tracks_clips))?;
Ok(()) Ok(())
}) })
.bg(Color::Rgb(28, 35, 25)) .bg(Color::Rgb(28, 35, 25))
@ -686,7 +688,6 @@ impl<'a> Widget for VerticalArrangerCursor<'a> {
to.fill_bg(scene_area, Color::Rgb(40, 50, 30)); to.fill_bg(scene_area, Color::Rgb(40, 50, 30));
} }
} }
//Ok(Some(area))
Ok(()) Ok(())
} }
} }
@ -1286,8 +1287,8 @@ impl<E: Engine> Sequencer<E> {
let width = usize::MAX.min(phrase.read().unwrap().length); let width = usize::MAX.min(phrase.read().unwrap().length);
let mut buffer = BigBuffer::new(width, 64); let mut buffer = BigBuffer::new(width, 64);
let phrase = phrase.read().unwrap(); let phrase = phrase.read().unwrap();
fill_seq_bg(&mut buffer, phrase.length, self.ppq)?; Self::fill_seq_bg(&mut buffer, phrase.length, self.ppq)?;
fill_seq_fg(&mut buffer, &phrase)?; Self::fill_seq_fg(&mut buffer, &phrase)?;
self.buffer = buffer; self.buffer = buffer;
} else { } else {
self.buffer = Default::default(); self.buffer = Default::default();
@ -1295,6 +1296,83 @@ impl<E: Engine> Sequencer<E> {
Ok(()) Ok(())
} }
fn fill_seq_bg (buf: &mut BigBuffer, length: usize, ppq: usize) -> Usually<()> {
for x in 0..buf.width {
if x as usize >= length {
break
}
let style = Style::default();
buf.get_mut(x, 0).map(|cell|{
cell.set_char('-');
cell.set_style(style);
});
for y in 0 .. buf.height {
buf.get_mut(x, y).map(|cell|{
cell.set_char(if ppq == 0 {
'·'
} else if x % (4 * ppq) == 0 {
'│'
} else if x % ppq == 0 {
'╎'
} else {
'·'
});
cell.set_fg(Color::Gray);
cell.modifier = Modifier::DIM;
});
}
}
Ok(())
}
fn fill_seq_fg (buf: &mut BigBuffer, phrase: &Phrase) -> Usually<()> {
let mut notes_on = [false;128];
for x in 0..buf.width {
if x as usize >= phrase.length {
break
}
if let Some(notes) = phrase.notes.get(x as usize) {
if phrase.percussive {
for note in notes {
match note {
MidiMessage::NoteOn { key, .. } =>
notes_on[key.as_int() as usize] = true,
_ => {}
}
}
} else {
for note in notes {
match note {
MidiMessage::NoteOn { key, .. } =>
notes_on[key.as_int() as usize] = true,
MidiMessage::NoteOff { key, .. } =>
notes_on[key.as_int() as usize] = false,
_ => {}
}
}
}
for y in 0..buf.height/2 {
if y >= 64 {
break
}
if let Some(block) = half_block(
notes_on[y as usize * 2],
notes_on[y as usize * 2 + 1],
) {
buf.get_mut(x, y).map(|cell|{
cell.set_char(block);
cell.set_fg(Color::White);
});
}
}
if phrase.percussive {
notes_on.fill(false);
}
}
}
Ok(())
}
pub(crate) fn style_focus (&self) -> Option<Style> { pub(crate) fn style_focus (&self) -> Option<Style> {
Some(if self.focused { Some(if self.focused {
Style::default().green().not_dim() Style::default().green().not_dim()
@ -1493,115 +1571,18 @@ impl Handle<Tui> for Sequencer<Tui> {
} }
} }
fn nth_octave (index: u16) -> &'static str { const NTH_OCTAVE: [&'static str;11] = [
match index { "-1", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
0 => "-1", ];
1 => "0",
2 => "1",
3 => "2",
4 => "3",
5 => "4",
6 => "5",
7 => "6",
8 => "7",
9 => "8",
10 => "9",
_ => unreachable!()
}
}
fn key_colors (index: u16) -> (Color, Color) { const KEY_COLORS: [(Color, Color);6] = [
match index % 6 { (Color::Rgb(255, 255, 255), Color::Rgb(0, 0, 0)),
0 => (Color::Rgb(255, 255, 255), Color::Rgb(0, 0, 0)), (Color::Rgb(255, 255, 255), Color::Rgb(0, 0, 0)),
1 => (Color::Rgb(255, 255, 255), Color::Rgb(0, 0, 0)), (Color::Rgb(255, 255, 255), Color::Rgb(255, 255, 255)),
2 => (Color::Rgb(255, 255, 255), Color::Rgb(255, 255, 255)), (Color::Rgb(0, 0, 0), Color::Rgb(255, 255, 255)),
3 => (Color::Rgb(0, 0, 0), Color::Rgb(255, 255, 255)), (Color::Rgb(0, 0, 0), Color::Rgb(255, 255, 255)),
4 => (Color::Rgb(0, 0, 0), Color::Rgb(255, 255, 255)), (Color::Rgb(0, 0, 0), Color::Rgb(255, 255, 255)),
5 => (Color::Rgb(0, 0, 0), Color::Rgb(255, 255, 255)), ];
_ => unreachable!()
}
}
fn fill_seq_bg (buf: &mut BigBuffer, length: usize, ppq: usize) -> Usually<()> {
for x in 0..buf.width {
if x as usize >= length {
break
}
let style = Style::default();
buf.get_mut(x, 0).map(|cell|{
cell.set_char('-');
cell.set_style(style);
});
for y in 0 .. buf.height {
buf.get_mut(x, y).map(|cell|{
cell.set_char(char_seq_bg(ppq, x as u16));
cell.set_fg(Color::Gray);
cell.modifier = Modifier::DIM;
});
}
}
Ok(())
}
fn char_seq_bg (ppq: usize, x: u16) -> char {
if ppq == 0 {
'·'
} else if x % (4 * ppq as u16) == 0 {
'│'
} else if x % ppq as u16 == 0 {
'╎'
} else {
'·'
}
}
fn fill_seq_fg (buf: &mut BigBuffer, phrase: &Phrase) -> Usually<()> {
let mut notes_on = [false;128];
for x in 0..buf.width {
if x as usize >= phrase.length {
break
}
if let Some(notes) = phrase.notes.get(x as usize) {
if phrase.percussive {
for note in notes {
match note {
MidiMessage::NoteOn { key, .. } =>
notes_on[key.as_int() as usize] = true,
_ => {}
}
}
} else {
for note in notes {
match note {
MidiMessage::NoteOn { key, .. } =>
notes_on[key.as_int() as usize] = true,
MidiMessage::NoteOff { key, .. } =>
notes_on[key.as_int() as usize] = false,
_ => {}
}
}
}
for y in 0..buf.height/2 {
if y >= 64 {
break
}
if let Some(block) = half_block(
notes_on[y as usize * 2],
notes_on[y as usize * 2 + 1],
) {
buf.get_mut(x, y).map(|cell|{
cell.set_char(block);
cell.set_fg(Color::White);
});
}
}
if phrase.percussive {
notes_on.fill(false);
}
}
}
Ok(())
}
pub(crate) fn keys_vert () -> Buffer { pub(crate) fn keys_vert () -> Buffer {
let area = [0, 0, 5, 64]; let area = [0, 0, 5, 64];
@ -1613,7 +1594,7 @@ pub(crate) fn keys_vert () -> Buffer {
match x { match x {
0 => { 0 => {
cell.set_char('▀'); cell.set_char('▀');
let (fg, bg) = key_colors(6 - y % 6); let (fg, bg) = KEY_COLORS[((6 - y % 6) % 6) as usize];
cell.set_fg(fg); cell.set_fg(fg);
cell.set_bg(bg); cell.set_bg(bg);
}, },
@ -1622,12 +1603,8 @@ pub(crate) fn keys_vert () -> Buffer {
cell.set_fg(Color::White); cell.set_fg(Color::White);
cell.set_bg(Color::White); cell.set_bg(Color::White);
}, },
2 => if y % 6 == 0 { 2 => if y % 6 == 0 { cell.set_char('C'); },
cell.set_char('C'); 3 => if y % 6 == 0 { cell.set_symbol(NTH_OCTAVE[(y / 6) as usize]); },
},
3 => if y % 6 == 0 {
cell.set_symbol(nth_octave(y / 6));
},
_ => {} _ => {}
} }
}); });
@ -2002,7 +1979,7 @@ impl Content for TransportToolbar<Tui> {
type Engine = Tui; type Engine = Tui;
fn content (&self) -> impl Widget<Engine = Tui> { fn content (&self) -> impl Widget<Engine = Tui> {
let focus_wrap = |focused, component|Layers::new(move |add|{ let focus_wrap = |focused, component|Layers::new(move |add|{
if focused { add(&CORNERS)?; add(&Background(COLOR_BG1))?; } if focused { add(&CORNERS)?; add(&Background(Color::Rgb(60, 70, 50)))?; }
add(component) add(component)
}); });
row! { row! {