refactor: reuse horizontal::draw

This commit is contained in:
🪞👃🪞 2024-07-01 22:12:25 +03:00
parent 91832e0072
commit 939ffe3630
6 changed files with 130 additions and 117 deletions

View file

@ -2,47 +2,39 @@ use crate::core::*;
use super::*;
pub fn draw (
s: &Sequencer,
buf: &mut Buffer,
mut area: Rect,
buf: &mut Buffer,
area: Rect,
phrase: Option<&Phrase>,
ppq: usize,
time: usize,
time0: usize,
timeZ: usize,
note: usize,
note0: usize,
style: Option<Style>,
) -> Usually<Rect> {
area.x = area.x + 13;
let Rect { x, y, width, height } = area;
keys(buf, area, s.note_start)?;
timer(buf, x+6, y-1, s.time_start, s.time_start + area.width as usize, 0);
if let Some(phrase) = s.phrase() {
lanes(buf, x, y,
phrase,
s.timebase.ppq() as usize,
s.time_zoom,
s.time_start,
s.time_start + area.width as usize,
s.note_start,
s.note_start + area.height as usize,
);
let now = 0;
let notes = &[];
keys(buf, area, note0, notes)?;
timer(buf, area, time0, now);
if let Some(phrase) = phrase {
lanes(buf, area, phrase, ppq, timeZ, time0, note0);
}
cursor(
buf,
x,
y,
Style::default().green().not_dim(),
s.time_cursor,
s.note_cursor
);
footer(s, buf, x, y, width, height);
Ok(Rect { x: x - 13, y, width, height })
let style = style.unwrap_or_else(||{Style::default().green().not_dim()});
cursor(buf, area, style, time, note);
//footer(buf, area, note0, note, time0, time, timeZ);
Ok(area)
}
pub fn timer (
buf: &mut Buffer,
x: u16,
y: u16,
area: Rect,
time0: usize,
time1: usize,
now: usize
) {
for step in time0..time1 {
buf.set_string(x + step as u16, y, &"-", if step == now {
let x = area.x + 5;
for step in time0..(time0+area.width as usize).saturating_sub(5) {
buf.set_string(x + step as u16, area.y, &"-", if step == now {
Style::default().yellow().bold().not_dim()
} else {
Style::default()
@ -51,20 +43,23 @@ pub fn timer (
}
pub fn keys (
buf: &mut Buffer, area: Rect, note_start: usize
buf: &mut Buffer,
area: Rect,
note0: usize,
notes: &[bool],
) -> Usually<Rect> {
let bw = Style::default().dim();
let Rect { x, y, width, height } = area;
let h = height.saturating_sub(2);
for i in 0..h {
let y = y + i;
let y = y + i + 1;
let key = KEYS_VERTICAL[(i % 6) as usize];
key.blit(buf, x + 1, y, Some(bw));
"".blit(buf, x + 2, y, Some(bw));
"·".repeat(width.saturating_sub(6) as usize).blit(buf, x + 5, y, Some(bw.black()));
"|---".repeat(width.saturating_sub(6) as usize).blit(buf, x + 5, y, Some(bw.black()));
//buf.set_string(x + 3, y, &format!("{i}"), Style::default());
if i % 6 == 0 {
let octave = format!("C{}", ((note_start - i as usize) / 6) as i8 - 4);
let octave = format!("C{}", ((note0 - i as usize) / 6) as i8 - 4);
buf.set_string(x + 3, y, &octave, Style::default());
}
}
@ -73,27 +68,27 @@ pub fn keys (
pub fn lanes (
buf: &mut Buffer,
x: u16,
y: u16,
area: Rect,
phrase: &Phrase,
ppq: usize,
time_zoom: usize,
time0: usize,
time1: usize,
note0: usize,
note1: usize,
) {
let bg = Style::default();
let Rect { x, y, width, height } = area;
let time1 = time0 + width as usize;
let note1 = note0 + height as usize;
let bg = Style::default();
let (bw, wh) = (bg.dim(), bg.white());
for step in time0..time1 {
let x = x as usize + 5 + step;
let (a, b) = ((step + 0) * ppq / time_zoom, (step + 1) * ppq / time_zoom,);
if step % 4 == 0 {
"|".blit(buf, x as u16, y - 1, Some(Style::default().dim()));
"|".blit(buf, x as u16, y, Some(Style::default().dim()));
}
if step % (time_zoom * 4) == 0 {
format!("{}", step / time_zoom / 4 + 1)
.blit(buf, x as u16, y - 1, Some(Style::default().bold().not_dim()));
.blit(buf, x as u16, y, Some(Style::default().bold().not_dim()));
}
let h = ((note1-note0)/2).saturating_sub(y as usize);
for k in 0..h {
@ -112,14 +107,29 @@ pub fn lanes (
}
}
pub fn cursor (buf: &mut Buffer, x: u16, y: u16, style: Style, time: usize, note: usize) {
let x = x + 5 + time as u16;
let y = y + note as u16 / 2;
pub fn cursor (
buf: &mut Buffer,
area: Rect,
style: Style,
time: usize,
note: usize
) {
let x = area.x + 5 + time as u16;
let y = area.y + 1 + note as u16 / 2;
let c = if note % 2 == 0 { "" } else { "" };
c.blit(buf, x, y, Some(style));
}
pub fn footer (s: &Sequencer, buf: &mut Buffer, mut x: u16, y: u16, width: u16, height: u16) {
pub fn footer (
buf: &mut Buffer,
area: Rect,
note0: usize,
note: usize,
time0: usize,
time: usize,
timeZ: usize,
) {
let Rect { mut x, y, width, height } = area;
buf.set_string(x, y + height, format!("{}", "-".repeat((width - 2).into())),
Style::default().dim());
buf.set_string(x, y + height + 2, format!("{}", "-".repeat((width - 2).into())),
@ -128,15 +138,9 @@ pub fn footer (s: &Sequencer, buf: &mut Buffer, mut x: u16, y: u16, width: u16,
{
for (_, [letter, title, value]) in [
["S", &format!("ync"), &format!("<4/4>")],
["Q", &format!("uant"), &format!("<1/{}>", 4 * s.time_zoom)],
["N", &format!("ote"), &format!("{} ({}-{})",
s.note_start + s.note_cursor,
s.note_start,
"X")],
["T", &format!("ime"), &format!("{} ({}-{})",
s.time_start + s.time_cursor + 1,
s.time_start + 1,
"X")],
["Q", &format!("uant"), &format!("<1/{}>", 4 * timeZ)],
["N", &format!("ote"), &format!("{} ({}-{})", note0 + note, note0, "X")],
["T", &format!("ime"), &format!("{} ({}-{})", time0 + time, time0 + 1, "X")],
].iter().enumerate() {
buf.set_string(x, y + height + 1, letter, Style::default().bold().yellow().dim());
x = x + 1;