From cc2b59d7728b367b97f09cada7f6667f30ed911e Mon Sep 17 00:00:00 2001 From: unspeaker Date: Tue, 2 Jul 2024 21:30:08 +0300 Subject: [PATCH] refactor transport into render-only struct --- src/device/launcher/mod.rs | 33 ++++++++++++++++++++------------- src/device/mod.rs | 2 +- src/device/sequencer/mod.rs | 16 ++++++++-------- src/device/transport.rs | 22 ++++++++++++++++++++++ 4 files changed, 51 insertions(+), 22 deletions(-) diff --git a/src/device/launcher/mod.rs b/src/device/launcher/mod.rs index a19b6e31..0d2da7c2 100644 --- a/src/device/launcher/mod.rs +++ b/src/device/launcher/mod.rs @@ -180,22 +180,29 @@ pub fn process (state: &mut Launcher, _: &Client, _: &ProcessScope) -> Control { pub fn render (state: &Launcher, buf: &mut Buffer, mut area: Rect) -> Usually { //area.width = 80; // DOS mode //area.height = 25; - let Rect { x, y, width, height } = area; - { - use crate::device::transport::*; - draw_play_stop(buf, x + 1, y, &state.playing); - draw_rec(buf, x + 12, y, state.sequencer().map(|s|s.recording).unwrap_or(false)); - draw_dub(buf, x + 19, y, state.sequencer().map(|s|s.overdub).unwrap_or(false)); - draw_mon(buf, x + 26, y, state.sequencer().map(|s|s.monitoring).unwrap_or(false)); - draw_bpm(buf, x + 33, y, state.timebase.bpm() as usize); - draw_timer(buf, x + width - 1, y, &state.timebase, state.current_frame); - } - let mut y = y + 1; + let Rect { x, mut y, width, height } = area; + + y = y + crate::device::Transport { + timebase: &state.timebase, + playing: state.playing, + record: state.sequencer().map(|s|s.recording).unwrap_or(false), + overdub: state.sequencer().map(|s|s.overdub).unwrap_or(false), + monitor: state.sequencer().map(|s|s.monitoring).unwrap_or(false), + frame: state.current_frame + }.render(buf, area)?.height; + y = y + LauncherGrid::new( state, buf, Rect { x, y, width, height: height/3 }, state.view.is_tracks() ).draw()?.height; - y = y + draw_section_chains(state, buf, Rect { x, y, width, height: height/3 })?.height; - y = y + draw_section_sequencer(state, buf, Rect { x, y, width, height: height - y })?.height; + + y = y + draw_section_chains( + state, buf, Rect { x, y, width, height: height/3 } + )?.height; + + y = y + draw_section_sequencer( + state, buf, Rect { x, y, width, height: height - y } + )?.height; + area.height = y; if state.show_help { let style = Some(Style::default().bold().white().not_dim().on_black().italic()); diff --git a/src/device/mod.rs b/src/device/mod.rs index 8113ff4c..b829d327 100644 --- a/src/device/mod.rs +++ b/src/device/mod.rs @@ -4,4 +4,4 @@ mod looper; pub use self::looper::Looper; mod mixer; pub use self::mixer::Mixer; mod sequencer; pub use self::sequencer::{Sequencer, Phrase}; mod track; pub use self::track::Track; -mod transport; +mod transport; pub use self::transport::Transport; diff --git a/src/device/sequencer/mod.rs b/src/device/sequencer/mod.rs index eb7de416..c97c9dc3 100644 --- a/src/device/sequencer/mod.rs +++ b/src/device/sequencer/mod.rs @@ -157,19 +157,19 @@ fn render (s: &Sequencer, buf: &mut Buffer, area: Rect) -> Usually { pub fn draw_header (s: &Sequencer, buf: &mut Buffer, area: Rect) -> Usually { let Rect { x, y, width, .. } = area; let style = Style::default().gray(); - crate::device::transport::draw_play_stop(buf, x + 2, y + 1, &s.playing); + crate::device::Transport { + timebase: &s.timebase, + playing: s.playing, + record: s.recording, + overdub: s.overdub, + monitor: s.monitoring, + frame: 0 + }.render(buf, area)?; let separator = format!("├{}┤", "-".repeat((width - 2).into())); separator.blit(buf, x, y + 2, Some(style.dim())); - crate::device::transport::draw_rec(buf, x + 13, y + 1, s.recording); - crate::device::transport::draw_dub(buf, x + 20, y + 1, s.overdub); - crate::device::transport::draw_mon(buf, x + 27, y + 1, s.monitoring); let _ = draw_clips(s, buf, area)?; Ok(Rect { x, y, width, height: 3 }) } -pub fn draw_timer (buf: &mut Buffer, x: u16, y: u16, timer: &str) { - let style = Some(Style::default().gray().bold().not_dim()); - timer.blit(buf, x - timer.len() as u16, y, style); -} pub fn draw_clips (s: &Sequencer, buf: &mut Buffer, area: Rect) -> Usually { let Rect { x, y, .. } = area; let style = Style::default().gray(); diff --git a/src/device/transport.rs b/src/device/transport.rs index d213944c..8d5b8930 100644 --- a/src/device/transport.rs +++ b/src/device/transport.rs @@ -1,6 +1,28 @@ use crate::core::*; use crate::layout::*; +pub struct Transport<'a> { + pub timebase: &'a Arc, + pub playing: TransportState, + pub record: bool, + pub overdub: bool, + pub monitor: bool, + pub frame: usize, +} + +impl<'a> Render for Transport<'a> { + fn render (&self, buf: &mut Buffer, area: Rect) -> Usually { + let Rect { x, y, width, .. } = area; + draw_play_stop(buf, x + 1, y, &self.playing); + draw_rec(buf, x + 12, y, self.record); + draw_dub(buf, x + 19, y, self.overdub); + draw_mon(buf, x + 26, y, self.monitor); + draw_bpm(buf, x + 33, y, self.timebase.bpm() as usize); + draw_timer(buf, x + width - 1, y, &self.timebase, self.frame); + Ok(Rect { x, y, width, height: 1 }) + } +} + pub fn draw_timer (buf: &mut Buffer, x: u16, y: u16, timebase: &Arc, frame: usize) { let ppq = timebase.ppq() as usize;