horizontal time cursor

This commit is contained in:
🪞👃🪞 2024-06-21 16:59:13 +03:00
parent 3177e4ab58
commit d9b3bd150e
8 changed files with 341 additions and 249 deletions

View file

@ -0,0 +1,99 @@
use crate::prelude::*;
use super::*;
pub fn draw_horizontal (
s: &Sequencer,
buf: &mut Buffer,
mut area: Rect,
beat: usize
) -> Usually<Rect> {
let ppq = s.timebase.ppq() as u32;
area.x = area.x + 13;
draw_keys_horizontal(s, buf, area)?;
let Rect { x, y, width, .. } = area;
let (time0, time1) = s.time_axis;
let (note0, note1) = s.note_axis;
let bw = Style::default().dim();
let bg = Style::default().on_black();
for step in time0..time1 {
buf.set_string(x + 6 + step, y - 1, &"-", if beat % s.steps == step as usize {
Style::default().yellow().bold().not_dim()
} else {
Style::default()
});
}
for step in time0..time1 {
let time_start = step as u32 * ppq;
let time_end = (step + 1) as u32 * ppq;
if step % s.resolution as u16 == 0 {
buf.set_string(x + 6 + step, y - 1, &format!("{}", step + 1), Style::default());
}
for (_, (_, events)) in s.sequences[s.sequence].range(time_start..time_end).enumerate() {
if events.len() > 0 {
buf.set_string(x + 5 + step as u16, y, "", bw);
}
}
}
let height = 32.max(note1 - note0) / 2;
buf.set_string(x - 13, y + height, format!("{}", "-".repeat((width - 2).into())),
Style::default().dim());
{
let mut x = x - 11;
for (i, [letter, title, value]) in [
["S", &format!("ync"), &format!("<4/4>")],
["Q", &format!("uant"), &format!("<1/{}>", 4 * s.resolution)],
["N", &format!("ote"), &format!("{} ({}-{})",
s.note_axis.0 + s.note_cursor,
s.note_axis.0,
s.note_axis.1 - 1)],
["T", &format!("ime"), &format!("{} ({}-{})",
s.time_axis.0 + s.time_cursor + 1,
s.time_axis.0 + 1,
s.time_axis.1)],
].iter().enumerate() {
buf.set_string(x, y + height + 1, letter, Style::default().bold().yellow().dim());
x = x + 1;
buf.set_string(x, y + height + 1, &title, Style::default().bold().dim());
x = x + title.len() as u16 + 1;
buf.set_string(x, y + height + 1, &value, Style::default().not_dim());
x = x + value.len() as u16;
buf.set_string(x, y + height + 1, " ", Style::default().dim());
x = x + 2;
}
}
buf.set_string(
x + 6 + s.time_cursor,
y + s.note_cursor / 2,
if s.note_cursor % 2 == 0 { "" } else { "" },
Style::default()
);
Ok(Rect {
x: x - 13,
y,
width: time1 - time0 + 19,
height: height + 3
})
}
pub fn draw_keys_horizontal (s: &Sequencer, buf: &mut Buffer, mut area: Rect) -> Usually<Rect> {
let Rect { x, y, .. } = area;
let (note0, note1) = s.note_axis;
let (time0, time1) = s.time_axis;
let bw = Style::default().dim();
let bg = Style::default().on_black();
for i in 0..32.max(note1-note0)/2 {
let y = y + i;
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 + 4, y, &octave, Style::default());
}
}
Ok(area)
}

View file

@ -0,0 +1,27 @@
use crate::prelude::*;
use super::*;
pub const KEY_WHITE: Style = Style {
fg: Some(Color::Gray),
bg: None,
underline_color: None,
add_modifier: ::ratatui::style::Modifier::empty(),
sub_modifier: ::ratatui::style::Modifier::empty(),
};
pub const KEY_BLACK: Style = Style {
fg: Some(Color::Black),
bg: None,
underline_color: None,
add_modifier: ::ratatui::style::Modifier::empty(),
sub_modifier: ::ratatui::style::Modifier::empty(),
};
pub const KEY_STYLE: [Style;12] = [
KEY_WHITE, KEY_BLACK, KEY_WHITE, KEY_BLACK, KEY_WHITE,
KEY_WHITE, KEY_BLACK, KEY_WHITE, KEY_BLACK, KEY_WHITE, KEY_BLACK, KEY_WHITE,
];
pub const KEYS_VERTICAL: [&'static str; 6] = [
"", "", "", "", "", "",
];

View file

@ -0,0 +1,106 @@
use crate::prelude::*;
use super::*;
pub 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;
draw_keys_vertical(s, buf, area, beat);
let Rect { x, y, .. } = area;
let (time0, time1) = s.time_axis;
let (note0, note1) = s.note_axis;
let bw = Style::default().dim().on_black();
let bg = Style::default().on_black();
for step in time0..time1 {
let y = y - time0 + step / 2;
let step = step as usize;
//buf.set_string(x + 5, y, &" ".repeat(32.max(note1-note0)as usize), bg);
if step % s.resolution == 0 {
buf.set_string(x + 2, y, &format!("{:2} ", step + 1), Style::default());
}
for k in note0..note1 {
let key = ::midly::num::u7::from_int_lossy(k as u8);
if step % 2 == 0 {
let (a, b, c) = (
(step + 0) as u32 * ppq / s.resolution as u32,
(step + 1) as u32 * ppq / s.resolution as u32,
(step + 2) as u32 * ppq / s.resolution as u32,
);
let (character, style) = match (
contains_note_on(&s.sequences[s.sequence], key, a, b),
contains_note_on(&s.sequences[s.sequence], key, b, c),
) {
(true, true) => ("", bg),
(true, false) => ("", bg),
(false, true) => ("", bg),
(false, false) => ("·", bw),
};
buf.set_string(x + 5 + k - note0, y, character, style);
}
}
if beat % s.steps == step as usize {
buf.set_string(x + 4, y, if beat % 2 == 0 { "" } else { "" }, Style::default().yellow());
for key in note0..note1 {
let _color = if s.notes_on[key as usize] {
Style::default().red()
} else {
KEY_STYLE[key as usize % 12]
};
}
}
}
let height = (time1-time0)/2;
buf.set_string(x + 2, y + height + 1, format!(
"Q 1/{} | N {} ({}-{}) | T {} ({}-{})",
4 * s.resolution,
s.note_axis.0 + s.note_cursor,
s.note_axis.0,
s.note_axis.1 - 1,
s.time_axis.0 + s.time_cursor + 1,
s.time_axis.0 + 1,
s.time_axis.1,
), Style::default().dim());
buf.set_string(
x + 5 + s.note_cursor,
y + s.time_cursor / 2,
if s.time_cursor % 2 == 0 { "" } else { "" },
Style::default()
);
Ok(Rect { x, y, width: area.width, height: height + 1 })
}
fn draw_keys_vertical (s: &Sequencer, buf: &mut Buffer, mut area: Rect, beat: usize) {
let ppq = s.timebase.ppq() as u32;
let Rect { x, y, .. } = area;
let (note0, note1) = s.note_axis;
for key in note0..note1 {
let x = x + 5 + key - note0;
if key % 12 == 0 {
let octave = format!("C{}", (key / 12) as i8 - 4);
buf.set_string(x, y, &octave, Style::default());
}
let mut color = KEY_STYLE[key as usize % 12];
let mut is_on = s.notes_on[key as usize];
let step = beat % s.steps;
let (a, b, c) = (
(step + 0) as u32 * ppq / s.resolution as u32,
(step + 1) as u32 * ppq / s.resolution as u32,
(step + 2) as u32 * ppq / s.resolution as u32,
);
let key = ::midly::num::u7::from(key as u8);
is_on = is_on || contains_note_on(&s.sequences[s.sequence], key, a, b);
is_on = is_on || contains_note_on(&s.sequences[s.sequence], key, b, c);
if is_on {
color = Style::default().red();
}
buf.set_string(x, y - 1, &format!(""), color);
}
}