Arranger -> Arrangement; ArrangerStandalone -> Arranger

This commit is contained in:
🪞👃🪞 2024-10-06 03:14:24 +03:00
parent 11a66ee415
commit a6b08a3249
5 changed files with 166 additions and 165 deletions

View file

@ -1,7 +1,23 @@
use crate::*; use crate::*;
/// Represents the tracks and scenes of the composition. /// Root level object for standalone `tek_arranger`
pub struct Arranger<E: Engine> { pub struct Arranger<E: Engine> {
/// Controls the JACK transport.
pub transport: Option<Arc<RwLock<TransportToolbar<E>>>>,
/// Contains all the sequencers.
pub arrangement: Arrangement<E>,
/// This allows the sequencer view to be moved or hidden.
pub show_sequencer: Option<tek_core::Direction>,
/// Index of currently focused component
pub focus: usize,
/// Focus target that passes events down to sequencer
pub sequencer_proxy: SequencerProxy<E>,
/// Slot for modal dialog displayed on top of app.
pub modal: Option<Box<dyn ContentComponent<E>>>,
}
/// Represents the tracks and scenes of the composition.
pub struct Arrangement<E: Engine> {
/// Name of arranger /// Name of arranger
pub name: Arc<RwLock<String>>, pub name: Arc<RwLock<String>>,
/// Collection of tracks. /// Collection of tracks.
@ -12,13 +28,11 @@ pub struct Arranger<E: Engine> {
pub selected: ArrangerFocus, pub selected: ArrangerFocus,
/// Display mode of arranger /// Display mode of arranger
pub mode: ArrangerViewMode, pub mode: ArrangerViewMode,
/// Slot for modal dialog displayed on top of app.
pub modal: Option<Box<dyn ContentComponent<E>>>,
/// Whether the arranger is currently focused /// Whether the arranger is currently focused
pub focused: bool pub focused: bool
} }
impl<E: Engine> Arranger<E> { impl<E: Engine> Arrangement<E> {
pub fn new (name: &str) -> Self { pub fn new (name: &str) -> Self {
Self { Self {
name: Arc::new(RwLock::new(name.into())), name: Arc::new(RwLock::new(name.into())),
@ -26,7 +40,6 @@ impl<E: Engine> Arranger<E> {
selected: ArrangerFocus::Clip(0, 0), selected: ArrangerFocus::Clip(0, 0),
scenes: vec![], scenes: vec![],
tracks: vec![], tracks: vec![],
modal: None,
focused: false focused: false
} }
} }
@ -345,7 +358,7 @@ impl ArrangerViewMode {
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
pub struct VerticalArranger<'a, E: Engine>( pub struct VerticalArranger<'a, E: Engine>(
pub &'a Arranger<E>, pub usize pub &'a Arrangement<E>, pub usize
); );
pub struct VerticalArrangerGrid<'a>( pub struct VerticalArrangerGrid<'a>(
pub u16, pub &'a [(usize, usize)], pub &'a [(usize, usize)] pub u16, pub &'a [(usize, usize)], pub &'a [(usize, usize)]
@ -357,7 +370,7 @@ pub struct VerticalArrangerCursor<'a>(
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
pub struct HorizontalArranger<'a, E: Engine>( pub struct HorizontalArranger<'a, E: Engine>(
pub &'a Arranger<E> pub &'a Arrangement<E>
); );
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////

View file

@ -1,6 +1,5 @@
include!("lib.rs"); include!("lib.rs");
use tek_core::clap::{self, Parser}; use tek_core::clap::{self, Parser};
pub fn main () -> Usually<()> { ArrangerCli::parse().run() } pub fn main () -> Usually<()> { ArrangerCli::parse().run() }
/// Parses CLI arguments to the `tek_arranger` invocation. /// Parses CLI arguments to the `tek_arranger` invocation.
@ -27,14 +26,14 @@ impl ArrangerCli {
let jack = JackClient::Inactive( let jack = JackClient::Inactive(
Client::new("tek_arranger", ClientOptions::NO_START_SERVER)?.0 Client::new("tek_arranger", ClientOptions::NO_START_SERVER)?.0
); );
let mut arranger = Arranger::new(""); let mut arrangement = Arrangement::new("");
let jack_transport = jack.transport(); let jack_transport = jack.transport();
let mut transport = TransportToolbar::new(Some(jack_transport)); let mut transport = TransportToolbar::new(Some(jack_transport));
if let Some(name) = self.name.as_ref() { if let Some(name) = self.name.as_ref() {
*arranger.name.write().unwrap() = name.clone(); *arrangement.name.write().unwrap() = name.clone();
} }
for _ in 0..self.tracks { for _ in 0..self.tracks {
let track = arranger.track_add(None)?; let track = arrangement.track_add(None)?;
for _ in 0..self.scenes { for _ in 0..self.scenes {
track.phrases.push( track.phrases.push(
Arc::new(RwLock::new(Phrase::new("", true, PPQ * 4, None))) Arc::new(RwLock::new(Phrase::new("", true, PPQ * 4, None)))
@ -42,7 +41,7 @@ impl ArrangerCli {
} }
} }
for _ in 0..self.scenes { for _ in 0..self.scenes {
let _scene = arranger.scene_add(None)?; let _scene = arrangement.scene_add(None)?;
//for i in 0..self.tracks { //for i in 0..self.tracks {
//scene.clips[i] = Some(i); //scene.clips[i] = Some(i);
//} //}
@ -57,137 +56,14 @@ impl ArrangerCli {
} }
)? )?
); );
Tui::run(Arc::new(RwLock::new(ArrangerStandalone { Tui::run(Arc::new(RwLock::new(Arranger {
transport: self.transport.then_some(transport), transport: self.transport.then_some(transport),
show_sequencer: Some(tek_core::Direction::Down), show_sequencer: Some(tek_core::Direction::Down),
arranger, arrangement,
focus: 0, focus: 0,
sequencer_proxy: SequencerProxy(Default::default(), false) sequencer_proxy: SequencerProxy(Default::default(), false),
modal: None
})))?; })))?;
Ok(()) Ok(())
} }
} }
/// Root level object for standalone `tek_arranger`
struct ArrangerStandalone<E: Engine> {
/// Controls the JACK transport.
transport: Option<Arc<RwLock<TransportToolbar<E>>>>,
/// Contains all the sequencers.
arranger: Arranger<E>,
/// This allows the sequencer view to be moved or hidden.
show_sequencer: Option<tek_core::Direction>,
/// Index of currently focused component
focus: usize,
/// Focus target that passes events down to sequencer
sequencer_proxy: SequencerProxy<E>,
}
/// The standalone arranger consists of transport, clip grid, and sequencer.
impl Content for ArrangerStandalone<Tui> {
type Engine = Tui;
fn content (&self) -> impl Widget<Engine = Tui> {
Layers::new(move|add|{
add(&Stack::down(move|add|{
add(&self.transport)?;
if let (Some(direction), Some(sequencer)) = (
self.show_sequencer,
self.arranger.sequencer(),
) {
let arranger = &self.arranger as &dyn Widget<Engine = Tui>;
let sequencer = sequencer as &dyn Widget<Engine = Tui>;
add(&Split::new(direction, 20, arranger, sequencer.min_y(20)))
} else {
add(&self.arranger)
}
}))?;
if let Some(ref modal) = self.arranger.modal {
add(&Background(COLOR_BG1))?;
add(&Foreground(COLOR_BG2))?;
//add(modal as &dyn Widget<Engine = Tui>)?;
}
Ok(())
})
}
}
/// Handle top-level events in standalone arranger.
impl Handle<Tui> for ArrangerStandalone<Tui> {
fn handle (&mut self, from: &TuiInput) -> Perhaps<bool> {
let focus = self.focus;
let is_first_row = self.arranger.is_first_row();
let is_last_row = self.arranger.is_last_row();
let mut focused_handle = || {
if focus == 2 {
self.arranger.sequencer_mut().handle(from)
} else {
self.focused_mut().handle(from)
}
};
match from.event() {
key!(KeyCode::Char(' ')) => {
if let Some(ref mut transport) = self.transport {
transport.write().unwrap().toggle_play()?;
} else {
return Ok(None)
}
},
key!(KeyCode::Tab) => {
self.focus_next();
},
key!(KeyCode::BackTab) => {
self.focus_prev();
},
key!(KeyCode::Down) => {
if focus == 0 {
self.focus_next();
} else if focus == 1 && is_last_row {
self.focus_next();
} else {
return focused_handle()
}
},
key!(KeyCode::Up) => {
if focus == 1 && is_first_row {
self.focus_prev();
} else {
return focused_handle()
}
},
_ => return focused_handle()
}
Ok(Some(true))
}
}
/// Focusable items in standalone arranger.
impl Focus<3, Tui> for ArrangerStandalone<Tui> {
fn focus (&self) -> usize {
self.focus
}
fn focus_mut (&mut self) -> &mut usize {
&mut self.focus
}
fn focusable (&self) -> [&dyn Focusable<Tui>;3] {
focusables!(self.transport, self.arranger, self.sequencer_proxy)
}
fn focusable_mut (&mut self) -> [&mut dyn Focusable<Tui>;3] {
focusables_mut!(self.transport, self.arranger, self.sequencer_proxy)
}
}
#[derive(Default)]
struct SequencerProxy<E: Engine>(PhantomData<E>, bool);
impl Handle<Tui> for SequencerProxy<Tui> {
fn handle (&mut self, _: &TuiInput) -> Perhaps<bool> { unreachable!() }
}
impl Content for SequencerProxy<Tui> {
type Engine = Tui;
fn content (&self) -> impl Widget<Engine = Tui> { "" }
}
impl Focusable<Tui> for SequencerProxy<Tui> {
fn is_focused (&self) -> bool { self.1 }
fn set_focused (&mut self, focus: bool) { self.1 = focus }
}

View file

@ -1,26 +1,34 @@
use crate::*; use crate::*;
impl Arranger<Tui> { /// The standalone arranger consists of transport, clip grid, and sequencer.
pub fn rename_selected (&mut self) { impl Content for Arranger<Tui> {
self.modal = Some(Box::new(ArrangerRenameModal::new( type Engine = Tui;
self.selected, fn content (&self) -> impl Widget<Engine = Tui> {
&match self.selected { Layers::new(move|add|{
ArrangerFocus::Mix => self.name.clone(), add(&Stack::down(move|add|{
ArrangerFocus::Track(t) => self.tracks[t].name.clone(), add(&self.transport)?;
ArrangerFocus::Scene(s) => self.scenes[s].name.clone(), if let (Some(direction), Some(sequencer)) = (
ArrangerFocus::Clip(t, s) => self.tracks[t].phrases[s].read().unwrap().name.clone(), self.show_sequencer,
self.arrangement.sequencer(),
) {
let arrangement = &self.arrangement as &dyn Widget<Engine = Tui>;
let sequencer = sequencer as &dyn Widget<Engine = Tui>;
add(&Split::new(direction, 20, arrangement, sequencer.min_y(20)))
} else {
add(&self.arrangement)
} }
))); }))?;
} if let Some(ref modal) = self.modal {
} add(&Background(COLOR_BG1))?;
impl Focusable<Tui> for Arranger<Tui> { add(&Foreground(COLOR_BG2))?;
fn is_focused (&self) -> bool { //add(modal as &dyn Widget<Engine = Tui>)?;
self.focused }
} Ok(())
fn set_focused (&mut self, focused: bool) { })
self.focused = focused
} }
} }
/// Handle top-level events in standalone arranger.
impl Handle<Tui> for Arranger<Tui> { impl Handle<Tui> for Arranger<Tui> {
fn handle (&mut self, from: &TuiInput) -> Perhaps<bool> { fn handle (&mut self, from: &TuiInput) -> Perhaps<bool> {
if let Some(modal) = self.modal.as_mut() { if let Some(modal) = self.modal.as_mut() {
@ -30,6 +38,93 @@ impl Handle<Tui> for Arranger<Tui> {
} }
return Ok(result) return Ok(result)
} }
let focus = self.focus;
let is_first_row = self.arrangement.is_first_row();
let is_last_row = self.arrangement.is_last_row();
let mut focused_handle = || {
if focus == 2 {
self.arrangement.sequencer_mut().handle(from)
} else {
self.focused_mut().handle(from)
}
};
match from.event() {
key!(KeyCode::Char(' ')) => {
if let Some(ref mut transport) = self.transport {
transport.write().unwrap().toggle_play()?;
} else {
return Ok(None)
}
},
key!(KeyCode::Tab) => {
self.focus_next();
},
key!(KeyCode::BackTab) => {
self.focus_prev();
},
key!(KeyCode::Down) => {
if focus == 0 {
self.focus_next();
} else if focus == 1 && is_last_row {
self.focus_next();
} else {
return focused_handle()
}
},
key!(KeyCode::Up) => {
if focus == 1 && is_first_row {
self.focus_prev();
} else {
return focused_handle()
}
},
_ => return focused_handle()
}
Ok(Some(true))
}
}
/// Focusable items in standalone arranger.
impl Focus<3, Tui> for Arranger<Tui> {
fn focus (&self) -> usize {
self.focus
}
fn focus_mut (&mut self) -> &mut usize {
&mut self.focus
}
fn focusable (&self) -> [&dyn Focusable<Tui>;3] {
focusables!(self.transport, self.arrangement, self.sequencer_proxy)
}
fn focusable_mut (&mut self) -> [&mut dyn Focusable<Tui>;3] {
focusables_mut!(self.transport, self.arrangement, self.sequencer_proxy)
}
}
impl Arranger<Tui> {
pub fn rename_selected (&mut self) {
self.modal = Some(Box::new(ArrangerRenameModal::new(
self.arrangement.selected,
&match self.arrangement.selected {
ArrangerFocus::Mix => self.arrangement.name.clone(),
ArrangerFocus::Track(t) => self.arrangement.tracks[t].name.clone(),
ArrangerFocus::Scene(s) => self.arrangement.scenes[s].name.clone(),
ArrangerFocus::Clip(t, s) => self.arrangement.tracks[t].phrases[s].read().unwrap().name.clone(),
}
)));
}
}
impl Focusable<Tui> for Arrangement<Tui> {
fn is_focused (&self) -> bool {
self.focused
}
fn set_focused (&mut self, focused: bool) {
self.focused = focused
}
}
impl Handle<Tui> for Arrangement<Tui> {
fn handle (&mut self, from: &TuiInput) -> Perhaps<bool> {
match from.event() { match from.event() {
// mode_switch: switch the display mode // mode_switch: switch the display mode
key!(KeyCode::Char('`')) => { key!(KeyCode::Char('`')) => {
@ -95,9 +190,9 @@ impl Handle<Tui> for Arranger<Tui> {
self.track_add(None)?; self.track_add(None)?;
}, },
// rename: add a new scene // rename: add a new scene
key!(KeyCode::Char('n')) => { //key!(KeyCode::Char('n')) => {
self.rename_selected(); //self.rename_selected();
}, //},
// length: add a new scene // length: add a new scene
key!(KeyCode::Char('l')) => if let Some(phrase) = self.phrase() { key!(KeyCode::Char('l')) => if let Some(phrase) = self.phrase() {
phrase.write().unwrap().toggle_loop() phrase.write().unwrap().toggle_loop()
@ -111,7 +206,8 @@ impl Handle<Tui> for Arranger<Tui> {
Ok(Some(true)) Ok(Some(true))
} }
} }
impl Content for Arranger<Tui> {
impl Content for Arrangement<Tui> {
type Engine = Tui; type Engine = Tui;
fn content (&self) -> impl Widget<Engine = Tui> { fn content (&self) -> impl Widget<Engine = Tui> {
Layers::new(move |add|{ Layers::new(move |add|{
@ -341,7 +437,7 @@ impl<'a> Widget for VerticalArrangerCursor<'a> {
impl<'a> Content for HorizontalArranger<'a, Tui> { impl<'a> Content for HorizontalArranger<'a, Tui> {
type Engine = Tui; type Engine = Tui;
fn content (&self) -> impl Widget<Engine = Tui> { fn content (&self) -> impl Widget<Engine = Tui> {
let Arranger { tracks, focused, selected, scenes, .. } = self.0; let Arrangement { tracks, focused, selected, scenes, .. } = self.0;
let _tracks = tracks.as_slice(); let _tracks = tracks.as_slice();
lay!( lay!(
focused.then_some(Background(Color::Rgb(40, 50, 30))), focused.then_some(Background(Color::Rgb(40, 50, 30))),
@ -510,7 +606,7 @@ impl<'a> Content for HorizontalArranger<'a, Tui> {
CustomWidget::new(|_|{ CustomWidget::new(|_|{
todo!() todo!()
}, |to: &mut TuiOutput|{ }, |to: &mut TuiOutput|{
let Arranger { tracks, scenes, selected, .. } = self.0; let Arrangement { tracks, scenes, selected, .. } = self.0;
let area = to.area(); let area = to.area();
let mut x2 = 0; let mut x2 = 0;
let [x, y, _, height] = area; let [x, y, _, height] = area;

View file

@ -412,3 +412,20 @@ pub fn write_midi_output (writer: &mut MidiWriter, output: &MIDIChunk, frames: u
} }
} }
} }
#[derive(Default)]
pub struct SequencerProxy<E: Engine>(pub PhantomData<E>, pub bool);
impl Handle<Tui> for SequencerProxy<Tui> {
fn handle (&mut self, _: &TuiInput) -> Perhaps<bool> { unreachable!() }
}
impl Content for SequencerProxy<Tui> {
type Engine = Tui;
fn content (&self) -> impl Widget<Engine = Tui> { "" }
}
impl Focusable<Tui> for SequencerProxy<Tui> {
fn is_focused (&self) -> bool { self.1 }
fn set_focused (&mut self, focus: bool) { self.1 = focus }
}

View file

@ -1,8 +1,7 @@
//! Phrase editor. //! Phrase editor.
include!("lib.rs"); include!("lib.rs");
pub fn main () -> Usually<()> { SequencerCli::parse().run() }
use tek_core::clap::{self, Parser}; use tek_core::clap::{self, Parser};
pub fn main () -> Usually<()> { SequencerCli::parse().run() }
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
#[command(version, about, long_about = None)] #[command(version, about, long_about = None)]