wip: component refactor

This commit is contained in:
🪞👃🪞 2024-06-21 12:13:11 +03:00
parent 5082bf9fdf
commit c18aa2cbbd
14 changed files with 374 additions and 272 deletions

View file

@ -76,7 +76,7 @@ impl Sequencer {
output_port: client.register_port("out", MidiOut::default())?,
output_connect: vec![],
mode: SequencerView::Vertical,
mode: SequencerView::Horizontal,
note_axis: (36, 68),
note_cursor: 0,
time_axis: (0, 64),
@ -167,7 +167,7 @@ fn process_out (s: &mut Sequencer, scope: &ProcessScope) {
}
}
fn render (s: &Sequencer, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
fn render (s: &Sequencer, buf: &mut Buffer, mut area: Rect) -> Usually<Rect> {
let Rect { x, y, width, .. } = area;
let (time0, time1) = s.time_axis;
let (note0, note1) = s.note_axis;
@ -199,7 +199,7 @@ fn render (s: &Sequencer, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
x,
y,
width: header.width.max(piano.width),
height: header.height + piano.height + 3
height: header.height + piano.height
}))
}
@ -210,42 +210,55 @@ fn draw_header (s: &Sequencer, buf: &mut Buffer, area: Rect, beat: usize) -> Usu
let steps = s.steps % s.resolution;
let Rect { x, y, .. } = area;
let style = Style::default().gray();
buf.set_string(x + 1, y + 1,
&format!("{rep}.{step:2} / {reps}.{steps}"),
style);
buf.set_string(x + 2, y + 1,
&format!("{}", &s.name),
style.white().bold());
buf.set_string(x + 1, y + 2,
&format!(" ▶ PLAY │ ⏹ STOP │ │"),
&format!("{rep}.{step:2} / {reps}.{steps}"),
style);
buf.set_string(x + 2, y + 2,
&format!("▶ PLAY"), if s.playing {
Style::default().green()
} else {
Style::default().dim()
});
buf.set_string(x + 24, y + 2,
&format!("⏹ STOP"),
style);
//buf.set_string(x + 2, y + 2,
//&format!("▶ PLAY"), if s.playing {
//Style::default().green()
//} else {
//Style::default().dim()
//});
buf.set_string(x + 10, y + 2,
&format!("⏺ REC"), if s.recording {
Style::default().red()
} else {
Style::default().dim()
});
buf.set_string(x + 32, y + 2,
buf.set_string(x + 17, y + 2,
&format!("⏺ DUB"), if s.overdub {
Style::default().yellow()
} else {
Style::default().dim()
});
Ok(Rect { x, y, width: 39, height: 4 })
let clips = draw_clips(s, buf, area)?;
Ok(Rect { x, y, width: area.width, height: 4 })
}
fn draw_clips (s: &Sequencer, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
let Rect { x, y, .. } = area;
let style = Style::default().gray();
buf.set_string(x + 2, y + 4, &format!("{}", &s.name), style.white().bold());
buf.set_string(x + 2, y + 6, &format!("{}", &s.name), style.dim());
buf.set_string(x + 2, y + 8, &format!("{}", &s.name), style.dim());
buf.set_string(x + 2, y + 10, &format!("{}", &s.name), style.dim());
buf.set_string(x + 2, y + 12, &format!("{}", &s.name), style.dim());
buf.set_string(x + 2, y + 14, &format!("{}", &s.name), style.dim());
buf.set_string(x + 2, y + 16, &format!("{}", &s.name), style.dim());
buf.set_string(x + 2, y + 18, &format!("{}", &s.name), style.dim());
Ok(Rect { x, y, width: 14, height: 14 })
}
const KEYS_VERTICAL: [&'static str; 6] = [
"", "", "", "", "", "",
];
fn draw_vertical (s: &Sequencer, buf: &mut Buffer, area: Rect, beat: usize) -> Usually<Rect> {
fn draw_vertical (s: &Sequencer, buf: &mut Buffer, mut area: Rect, beat: usize) -> Usually<Rect> {
let ppq = s.timebase.ppq() as u32;
area.x = area.x + 13;
let Rect { x, y, .. } = area;
let (time0, time1) = s.time_axis;
let (note0, note1) = s.note_axis;
@ -369,7 +382,8 @@ const KEY_HORIZONTAL_STYLE: [Style;12] = [
KEY_WHITE, KEY_BLACK, KEY_WHITE, KEY_BLACK, KEY_WHITE, KEY_BLACK, KEY_WHITE,
];
fn draw_horizontal (s: &Sequencer, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
fn draw_horizontal (s: &Sequencer, buf: &mut Buffer, mut area: Rect) -> Usually<Rect> {
area.x = area.x + 13;
let Rect { x, y, .. } = area;
let (time0, time1) = s.time_axis;
let (note0, note1) = s.note_axis;
@ -377,9 +391,9 @@ fn draw_horizontal (s: &Sequencer, buf: &mut Buffer, area: Rect) -> Usually<Rect
let bg = Style::default().on_black();
for i in 0..32.max(note1-note0)/2 {
let y = y + i;
buf.set_string(x + 1, y, KEYS_VERTICAL[(i % 6) as usize], bw);
buf.set_string(x + 2, y, "", bw);
buf.set_string(x + 5, y, &" ".repeat((time1 - time0) as usize), bg);
buf.set_string(x + 2, y, KEYS_VERTICAL[(i % 6) as usize], bw);
buf.set_string(x + 3, y, "", bw);
buf.set_string(x + 6, y, &"·".repeat((time1 - time0) as usize), bg.dim());
if i % 6 == 0 {
let octave = format!("C{}", ((note1 - i) / 6) as i8 - 4);
buf.set_string(x+5, y, &octave, Style::default());
@ -395,7 +409,7 @@ fn draw_horizontal (s: &Sequencer, buf: &mut Buffer, area: Rect) -> Usually<Rect
}
}
let height = 32.max(note1 - note0) / 2;
buf.set_string(x + 2, y + height + 1, format!(
buf.set_string(x + 2, y + height, format!(
" Q 1/{} | N {} ({}-{}) | T {} ({}-{})",
4 * s.resolution,
s.note_axis.0 + s.note_cursor,
@ -411,7 +425,12 @@ fn draw_horizontal (s: &Sequencer, buf: &mut Buffer, area: Rect) -> Usually<Rect
if s.note_cursor % 2 == 0 { "" } else { "" },
Style::default()
);
Ok(Rect { x, y, width: time1 - time0 + 6, height})
Ok(Rect {
x: x - 13,
y,
width: time1 - time0 + 19,
height: height + 2
})
}
pub fn handle (s: &mut Sequencer, event: &AppEvent) -> Usually<bool> {
@ -495,7 +514,7 @@ fn cursor_up (s: &mut Sequencer) -> Usually<bool> {
match s.mode {
SequencerView::Vertical => time_cursor_dec(s),
SequencerView::Horizontal => note_cursor_dec(s),
_ => unimplemented!()
_ => Ok(false)
};
Ok(true)
}
@ -503,7 +522,7 @@ fn cursor_down (s: &mut Sequencer) -> Usually<bool> {
match s.mode {
SequencerView::Vertical => time_cursor_inc(s),
SequencerView::Horizontal => note_cursor_inc(s),
_ => unimplemented!()
_ => Ok(false)
};
Ok(true)
}
@ -511,7 +530,7 @@ fn cursor_left (s: &mut Sequencer) -> Usually<bool> {
match s.mode {
SequencerView::Vertical => note_cursor_dec(s),
SequencerView::Horizontal => time_cursor_dec(s),
_ => unimplemented!()
_ => Ok(false)
};
Ok(true)
}
@ -519,7 +538,7 @@ fn cursor_right (s: &mut Sequencer) -> Usually<bool> {
match s.mode {
SequencerView::Vertical => note_cursor_inc(s),
SequencerView::Horizontal => time_cursor_inc(s),
_ => unimplemented!()
_ => Ok(false)
};
Ok(true)
}
@ -539,7 +558,8 @@ impl SequencerView {
fn next (&self) -> Self {
match self {
Self::Horizontal => Self::Vertical,
Self::Vertical => Self::Horizontal,
Self::Vertical => Self::Tiny,
Self::Tiny => Self::Horizontal,
_ => self.clone()
}
}