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

@ -67,6 +67,9 @@ pub fn render (state: &Chain, buf: &mut Buffer, area: Rect)
}
impl Focus for Chain {
fn unfocus (&mut self) {
self.focused = false
}
fn focused (&self) -> Option<&Box<dyn Device>> {
match self.focused {
true => self.items.get(self.focus),
@ -80,29 +83,36 @@ impl Focus for Chain {
}
}
fn handle_focus (&mut self, event: &FocusEvent) -> Usually<bool> {
match event {
Ok(match event {
FocusEvent::Backward => {
if self.focus == 0 {
self.focus = self.items.len();
}
self.focus = self.focus - 1;
true
},
FocusEvent::Forward => {
self.focus = self.focus + 1;
if self.focus >= self.items.len() {
self.focus = 0;
}
true
},
FocusEvent::Inward => {
self.focused = true;
self.items[self.focus].handle(&AppEvent::Focus)?;
true
},
FocusEvent::Outward => {
self.focused = false;
self.items[self.focus].handle(&AppEvent::Blur)?;
if self.focused {
self.focused = false;
self.items[self.focus].handle(&AppEvent::Blur)?;
true
} else {
false
}
},
};
Ok(true)
})
}
}

View file

@ -6,7 +6,7 @@ pub struct Launcher {
impl Launcher {
pub fn new (name: &str) -> Result<DynamicDevice<Self>, Box<dyn Error>> {
Ok(DynamicDevice::new(render, handle, Self {
Ok(DynamicDevice::new(render, handle, process, Self {
name: name.into(),
}))
}
@ -16,10 +16,23 @@ pub fn process (_: &mut Launcher, _: &Client, _: &ProcessScope) -> Control {
Control::Continue
}
pub fn render (_: &Launcher, _: &mut Buffer, _: Rect) -> Usually<Rect> {
Ok(Rect::default())
pub fn render (_: &Launcher, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
for i in 1..=8 {
buf.set_string(area.x + 2 + (i-1) * 10, area.y, format!("Track#{i} | "), Style::default())
}
for i in 0..=7 {
for j in 0..=7 {
buf.set_string(area.x + 2 + i * 10, area.y + 2 + j, format!("······· | "), Style::default().dim())
}
}
Ok(draw_box(buf, Rect {
x: area.x,
y: area.y - 1,
width: area.width,
height: 12
}))
}
pub fn handle (_: &mut Launcher, _: &AppEvent) -> Result<(), Box<dyn Error>> {
Ok(())
pub fn handle (_: &mut Launcher, _: &AppEvent) -> Usually<bool> {
Ok(false)
}

View file

@ -7,7 +7,8 @@ mod vst3;
pub struct Plugin {
name: String,
path: String,
plugin: Option<PluginKind>
plugin: Option<PluginKind>,
parameter_offset: usize,
}
enum PluginKind {
@ -24,6 +25,7 @@ impl Plugin {
name: name.into(),
path: HELM.into(),
plugin: None,
parameter_offset: 0,
});
device.state.lock().unwrap().plugin = Some(self::lv2::plug_in(HELM)?);
Ok(device)
@ -40,13 +42,13 @@ pub fn render (state: &Plugin, buf: &mut Buffer, Rect { x, y, .. }: Rect)
-> Usually<Rect>
{
let style = Style::default().gray();
buf.set_string(x + 1, y + 1, &format!(" {}", state.name), style.white().bold());
buf.set_string(x + 1, y + 1, &format!(" {}", state.name), style.white().bold());
buf.set_string(x + 13, y + 1, &format!("│ ...{}...", &HELM[13..30]), style.not_dim());
buf.set_string(x + 0, y + 2, &format!("├--------------------------------------┤"), style.dim());
buf.set_string(x + 0, y + 2, &format!("├--------------------------------------┤"), style.dim());
match &state.plugin {
Some(PluginKind::LV2(ports, instance)) => {
let mut height = 3;
for (i, port) in ports.iter().enumerate() {
for (i, port) in ports.iter().skip(state.parameter_offset).enumerate() {
if i >= 10 {
break
}
@ -56,7 +58,7 @@ pub fn render (state: &Plugin, buf: &mut Buffer, Rect { x, y, .. }: Rect)
), Style::default());
height = height + 1;
}
Ok(draw_box(buf, Rect { x, y, width: 40, height }))
Ok(draw_box(buf, Rect { x, y, width: 40, height: height.max(10) }))
},
_ => {
buf.set_string(x + 1, y + 3, &format!(" Parameter 1 0.0"), style);
@ -68,6 +70,17 @@ pub fn render (state: &Plugin, buf: &mut Buffer, Rect { x, y, .. }: Rect)
}
}
pub fn handle (_: &mut Plugin, _: &AppEvent) -> Usually<bool> {
Ok(false)
pub fn handle (s: &mut Plugin, event: &AppEvent) -> Usually<bool> {
handle_keymap(s, event, keymap!(Plugin {
[Up, NONE, "cursor_up", "move cursor up",
|s: &mut Plugin|{
s.parameter_offset = s.parameter_offset.saturating_sub(1);
Ok(true)
}],
[Down, NONE, "cursor_down", "move cursor down",
|s: &mut Plugin|{
s.parameter_offset = s.parameter_offset + 1;
Ok(true)
}]
}))
}

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()
}
}

View file

@ -72,12 +72,12 @@ pub fn render (state: &Transport, buf: &mut Buffer, mut area: Rect)
"STOP",
"REC",
"DUB",
"0.0.00",
"0:00.000",
&format!("BPM {:03}.{:03}",
state.timebase.tempo() / 1000,
state.timebase.tempo() % 1000,
)
),
"0.0+00",
"0:00.000",
].iter() {
buf.set_string(area.x + x, area.y + 1, button, label);
x = x + button.len() as u16 + 1;