wip: show single-device chain in launcher

This commit is contained in:
🪞👃🪞 2024-06-24 22:55:24 +03:00
parent 1f194dafd8
commit 2f52e97c58
7 changed files with 315 additions and 239 deletions

View file

@ -7,11 +7,21 @@ pub struct Launcher {
chains: Vec<DynamicDevice<Chain>>,
rows: usize,
show_help: bool,
view: LauncherView,
}
pub enum LauncherView {
Tracks,
Sequencer,
Chains
}
impl Launcher {
pub fn new (name: &str, timebase: &Arc<Timebase>) -> Result<DynamicDevice<Self>, Box<dyn Error>> {
pub fn new (
name: &str,
timebase: &Arc<Timebase>,
) -> Result<DynamicDevice<Self>, Box<dyn Error>> {
Ok(DynamicDevice::new(render, handle, process, Self {
name: name.into(),
view: LauncherView::Tracks,
timebase: timebase.clone(),
cursor: (0, 0),
rows: 8,
@ -22,7 +32,18 @@ impl Launcher {
Sequencer::new("Lead", timebase)?,
],
chains: vec![
// TODO
Chain::new("Chain#0000", vec![
Box::new(Plugin::new("Plugin#000")?),
])?,
Chain::new("Chain#0000", vec![
Box::new(Plugin::new("Plugin#001")?),
])?,
Chain::new("Chain#0000", vec![
Box::new(Plugin::new("Plugin#002")?),
])?,
Chain::new("Chain#0000", vec![
Box::new(Plugin::new("Plugin#003")?),
])?,
],
show_help: true
}))
@ -72,38 +93,52 @@ impl DevicePorts for Launcher {}
pub fn process (_: &mut Launcher, _: &Client, _: &ProcessScope) -> Control {
Control::Continue
}
macro_rules! set {
($buf:expr, $style:expr, $x: expr, $y: expr, $fmt: literal $(, $val:expr)*) => {
$buf.set_string($x, $y, &format!($fmt $(, $val)*), $style)
}
}
trait Blit {
fn blit (&self, buf: &mut Buffer, x: u16, y: u16, style: Option<Style>);
}
impl<T: AsRef<str>> Blit for T {
fn blit (&self, buf: &mut Buffer, x: u16, y: u16, style: Option<Style>) {
buf.set_string(x, y, self.as_ref(), style.unwrap_or(Style::default()))
}
}
pub fn render (state: &Launcher, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
let Rect { x, y, width, height } = area;
let separator = format!("{}", "-".repeat((width - 2).into()));
let scenes = draw_launcher_scenes(state, buf, x, y);
let track_area = Rect { x: 0, y: 0, width, height: 22 };
let seq_area = Rect { x: 0, y: 23, width, height: 18 };
let chain_area = Rect { x: 0, y: 40, width, height: 22 };
let separator = format!("{}", "-".repeat((width - 2).into()));
let scenes = draw_scenes(state, buf, x, y);
separator.blit(buf, x, y + 2, Some(Style::default().dim()));
separator.blit(buf, x, y + 4, Some(Style::default().dim()));
separator.blit(buf, x, y + 40, Some(Style::default().dim()));
let (w, mut highlight) = draw_launcher_tracks(state, buf, x, y);
let (w, mut highlight) = draw_tracks(state, buf, x, y);
if state.col() == 0 {
highlight = Some(scenes);
}
draw_crossings(state, buf, x + w - 2, y);
draw_sequencer(state, buf, x, y + 21, width, height - 22)?;
draw_box(buf, area);
draw_launcher_highlight(state, buf, &highlight);
draw_highlight(state, buf, &highlight);
let style = Some(Style::default().green().not_dim());
crate::device::chain::draw_as_row(
&*state.chains[0].state(), buf, chain_area, Some(Style::default().green().dim())
)?;
match state.view {
LauncherView::Tracks => draw_box_styled(buf, track_area, style),
LauncherView::Sequencer => draw_box_styled(buf, seq_area, style),
LauncherView::Chains => draw_box_styled(buf, chain_area, style),
};
if state.show_help {
let style = Some(Style::default().bold().white().not_dim().on_black().italic());
//" [Tab] ".blit(buf, 1, match state.view {
//LauncherView::Tracks => 21,
//LauncherView::Sequencer => 40,
//LauncherView::Chains => 0,
//}, style);
//" [Shift-Tab] ".blit(buf, 1, match state.view {
//LauncherView::Tracks => 40,
//LauncherView::Sequencer => 0,
//LauncherView::Chains => 21,
//}, style);
let hide = "[F1] Toggle help ";
hide.blit(buf, width - hide.len() as u16, height - 1, style);
}
Ok(area)
}
fn draw_launcher_scenes (
fn draw_scenes (
state: &Launcher, buf: &mut Buffer, x: u16, y: u16,
) -> Rect {
let style = Style::default().not_dim().bold();
@ -128,8 +163,7 @@ fn draw_launcher_scenes (
}
Rect { x, y, width, height }
}
fn draw_launcher_tracks (
fn draw_tracks (
state: &Launcher, buf: &mut Buffer, x: u16, y: u16
) -> (u16, Option<Rect>) {
let mut w = 15;
@ -137,7 +171,7 @@ fn draw_launcher_tracks (
for (i, track) in state.tracks.iter().enumerate() {
let track = track.state();
draw_crossings(state, buf, x + w - 2, y);
let width = draw_launcher_track(state, buf, x + w, y, i as u16, &track);
let width = draw_track(state, buf, x + w, y, i as u16, &track);
if i + 1 == state.col() {
highlight = Some(Rect { x: x + w - 2, y, width: width + 1, height: 22 });
}
@ -145,8 +179,7 @@ fn draw_launcher_tracks (
}
(w, highlight)
}
fn draw_launcher_track (
fn draw_track (
state: &Launcher, buf: &mut Buffer, x: u16, y: u16, i: u16, track: &Sequencer
) -> u16 {
let mut width = track.name.len() as u16 + 3;
@ -190,7 +223,6 @@ fn draw_launcher_track (
width
}
fn draw_crossings (state: &Launcher, buf: &mut Buffer, x: u16, y: u16) {
"".blit(buf, x, y + 0, None);
"".blit(buf, x, y + 1, Some(Style::default().dim()));
@ -198,7 +230,6 @@ fn draw_crossings (state: &Launcher, buf: &mut Buffer, x: u16, y: u16) {
"".blit(buf, x, y + 3, Some(Style::default().dim()));
"".blit(buf, x, y + 4, Some(Style::default().dim()));
}
fn draw_sequencer (
state: &Launcher, buf: &mut Buffer, x: u16, y: u16, width: u16, height: u16
) -> Usually<()> {
@ -212,9 +243,9 @@ fn draw_sequencer (
}
Ok(())
}
fn draw_launcher_highlight (
state: &Launcher, buf: &mut Buffer, highlight: &Option<Rect>) {
fn draw_highlight (
state: &Launcher, buf: &mut Buffer, highlight: &Option<Rect>
) {
if let Some(area) = highlight {
draw_box_styled(buf, *area, Some(Style::default().green().dim()));
}
@ -224,12 +255,14 @@ pub fn handle (state: &mut Launcher, event: &AppEvent) -> Usually<bool> {
handle_keymap(state, event, KEYMAP)
}
pub const KEYMAP: &'static [KeyBinding<Launcher>] = keymap!(Launcher {
[Up, NONE, "cursor_up", "move cursor up", cursor_up],
[Down, NONE, "cursor_down", "move cursor down", cursor_down],
[Left, NONE, "cursor_left", "move cursor left", cursor_left],
[Right, NONE, "cursor_right", "move cursor right", cursor_right],
[Char('r'), NONE, "rename", "rename current element", rename],
[F(1), NONE, "toggle_help", "toggle help", toggle_help],
[Up, NONE, "cursor_up", "move cursor up", cursor_up],
[Down, NONE, "cursor_down", "move cursor down", cursor_down],
[Left, NONE, "cursor_left", "move cursor left", cursor_left],
[Right, NONE, "cursor_right", "move cursor right", cursor_right],
[Char('r'), NONE, "rename", "rename current element", rename],
[F(1), NONE, "toggle_help", "toggle help", toggle_help],
[Tab, SHIFT, "focus_prev", "focus previous area", focus_prev],
[Tab, NONE, "focus_next", "focus next area", focus_next],
});
fn rename (_: &mut Launcher) -> Usually<bool> {
Ok(true)
@ -254,3 +287,19 @@ fn toggle_help (state: &mut Launcher) -> Usually<bool> {
state.show_help = !state.show_help;
Ok(true)
}
fn focus_next (state: &mut Launcher) -> Usually<bool> {
state.view = match state.view {
LauncherView::Tracks => LauncherView::Sequencer,
LauncherView::Sequencer => LauncherView::Chains,
LauncherView::Chains => LauncherView::Tracks,
};
Ok(true)
}
fn focus_prev (state: &mut Launcher) -> Usually<bool> {
state.view = match state.view {
LauncherView::Tracks => LauncherView::Chains,
LauncherView::Chains => LauncherView::Sequencer,
LauncherView::Sequencer => LauncherView::Tracks,
};
Ok(true)
}