refactor: view constructors from App

This commit is contained in:
🪞👃🪞 2024-07-08 21:19:53 +03:00
parent 1e3d96e64e
commit b01863dfdc
5 changed files with 88 additions and 102 deletions

View file

@ -35,7 +35,7 @@ pub fn main () -> Usually<()> {
// Init view
app.track_cursor = 1;
app.scene_cursor = 1;
app.note_start = 12;
app.note_start = 2;
app.time_zoom = 12;
app.quant = 24;

View file

@ -18,7 +18,7 @@ use crate::{render, App, AppSection, core::*};
render!(App |self, buf, area| {
let Rect { x, y, width, height } = area;
let transport = self.draw_transport(buf, area)?;
let transport = TransportView::new(self).render(buf, area)?;
let y = y + transport.height;
let arranger = if self.arranger_mode {
self.draw_arranger_horizontal(buf, Rect {
@ -38,7 +38,7 @@ render!(App |self, buf, area| {
}
let y = y + arranger.height;
if self.track_cursor > 0 {
let chain = self.draw_chain(buf, Rect {
let chain = ChainView::new(&self, false).render(buf, Rect {
x, y: y + height - height / 3 - 1, width, height: height / 3
})?;
if self.section == AppSection::Chain {
@ -48,7 +48,7 @@ render!(App |self, buf, area| {
Style::default().green().dim()
}).draw(buf, Rect { width, ..chain })
}
let phrase = self.draw_phrase(buf, Rect {
let phrase = SequencerView::new(&self).render(buf, Rect {
x, y, width, height: height - height / 3
})?;
if self.section == AppSection::Sequencer {
@ -88,17 +88,6 @@ impl App {
}
Ok(area)
}
fn draw_transport (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
TransportView {
timebase: &self.timebase,
playing: *self.playing.as_ref().unwrap_or(&TransportState::Stopped),
monitor: self.track().map(|t|t.1.monitoring).unwrap_or(false),
record: self.track().map(|t|t.1.recording).unwrap_or(false),
overdub: self.track().map(|t|t.1.overdub).unwrap_or(false),
frame: self.playhead,
quant: self.quant,
}.render(buf, area)
}
fn draw_arranger_horizontal (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
ArrangerViewHorizontal {
buf,
@ -121,37 +110,4 @@ impl App {
cursor: &(self.track_cursor, self.scene_cursor),
}.draw()
}
fn draw_chain (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
ChainView {
focused: self.section == AppSection::Chain,
track: self.tracks.get(self.track_cursor - 1),
vertical: false,
}.render(buf, area)
}
fn draw_phrase (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
let track = self.tracks.get(self.track_cursor - 1);
let phrase = self.phrase();
let seq_area = SequencerView {
phrase,
focused: self.section == AppSection::Sequencer,
ppq: self.timebase.ppq() as usize,
now: self.timebase.frame_to_pulse(self.playhead as f64) as usize,
time_cursor: self.time_cursor,
time_start: self.time_start,
time_zoom: self.time_zoom,
note_cursor: self.note_cursor,
note_start: self.note_start,
notes_in: if let Some(track) = track { &track.notes_in } else { &[false;128] },
notes_out: if let Some(track) = track { &track.notes_out } else { &[false;128] },
}.render(buf, area)?;
if let Some(track) = track {
if phrase.is_none() && self.section == AppSection::Sequencer {
let label = format!("[ENTER] Create new clip: {}", track.name);
let x = area.x + seq_area.width / 2 - (label.len() / 2) as u16;
let y = area.y + seq_area.height / 2;
label.blit(buf, x, y, Some(Style::default().white()));
}
}
Ok(seq_area)
}
}

View file

@ -8,6 +8,16 @@ pub struct ChainView<'a> {
pub vertical: bool,
}
impl<'a> ChainView<'a> {
pub fn new (app: &'a App, vertical: bool) -> Self {
Self {
focused: app.section == AppSection::Chain,
track: app.tracks.get(app.track_cursor - 1),
vertical
}
}
}
impl<'a> Render for ChainView<'a> {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
let style = Some(if self.focused {

View file

@ -28,44 +28,75 @@ pub struct SequencerView<'a> {
pub notes_out: &'a [bool; 128],
}
impl<'a> SequencerView<'a> {
pub fn new (app: &'a App) -> Self {
let track = app.tracks.get(app.track_cursor - 1);
let phrase = app.phrase();
Self {
phrase,
focused: app.section == AppSection::Sequencer,
ppq: app.timebase.ppq() as usize,
now: app.timebase.frame_to_pulse(app.playhead as f64) as usize,
time_cursor: app.time_cursor,
time_start: app.time_start,
time_zoom: app.time_zoom,
note_cursor: app.note_cursor,
note_start: app.note_start,
notes_in: if let Some(track) = track { &track.notes_in } else { &[false;128] },
notes_out: if let Some(track) = track { &track.notes_out } else { &[false;128] },
}
}
}
impl<'a> Render for SequencerView<'a> {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
let bg = if self.focused { Color::Rgb(20, 45, 5) } else { Color::Reset };
fill_bg(buf, area, bg);
self.draw_horizontal(buf, area)?;
self.horizontal_draw(buf, area)?;
Ok(area)
}
}
impl<'a> SequencerView<'a> {
fn draw_horizontal (&self, buf: &mut Buffer, area: Rect) -> Usually<()> {
let style = Some(if self.focused {
fn style_focus (&self) -> Option<Style> {
Some(if self.focused {
Style::default().green().not_dim()
} else {
Style::default().green().dim()
});
self::horizontal::keys(buf, area, self.note_start, self.notes_in, self.notes_out)?;
let quant = ppq_to_name(self.time_zoom);
quant.blit(
buf,
area.x + area.width - 1 - quant.len() as u16,
area.y + area.height - 2,
style
);
if let Some(phrase) = self.phrase {
self::horizontal::timer(buf, area, self.time_start, self.time_zoom, self.now % phrase.length);
self::horizontal::lanes(buf, area, phrase, self.ppq, self.time_zoom, self.time_start, self.note_start);
}
self::horizontal::cursor(buf, area, style.unwrap(), self.time_cursor, self.note_cursor);
Ok(())
})
}
}
mod horizontal {
use crate::core::*;
use super::*;
impl<'a> SequencerView<'a> {
fn horizontal_draw (&self, buf: &mut Buffer, area: Rect) -> Usually<()> {
self.horizontal_keys(buf, area)?;
self.horizontal_quant(buf, area);
if let Some(phrase) = self.phrase {
self.horizontal_timer(buf, area, phrase);
self.horizontal_lanes(buf, area, phrase);
}
self.horizontal_cursor(buf, area);
Ok(())
}
pub fn timer (buf: &mut Buffer, area: Rect, time0: usize, time_z: usize, now: usize) {
fn horizontal_quant (&self, buf: &mut Buffer, area: Rect) {
let quant = ppq_to_name(self.time_zoom);
let quant_x = area.x + area.width - 1 - quant.len() as u16;
let quant_y = area.y + area.height - 2;
quant.blit(buf, quant_x, quant_y, self.style_focus());
}
fn horizontal_cursor (&self, buf: &mut Buffer, area: Rect) {
let (time, note) = (self.time_cursor, self.note_cursor);
let x = area.x + 5 + time as u16;
let y = area.y + 1 + note as u16 / 2;
let c = if note % 2 == 0 { "" } else { "" };
c.blit(buf, x, y, self.style_focus());
}
fn horizontal_timer (&self, buf: &mut Buffer, area: Rect, phrase: &Phrase) {
let (time0, time_z, now) =
(self.time_start, self.time_zoom, self.now % phrase.length);
let Rect { x, width, .. } = area;
let offset = 5;
for x in x+offset..x+width-offset {
@ -80,13 +111,8 @@ mod horizontal {
}
}
pub fn keys (
buf: &mut Buffer,
area: Rect,
note0: usize,
notes_in: &[bool;128],
notes_out: &[bool;128],
) -> Usually<Rect> {
fn horizontal_keys (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
let (note0, notes_in, notes_out) = (self.note_start, self.notes_in, self.notes_out);
let dim = Style::default().not_dim();
let red = Style::default().red();
let yellow = Style::default().yellow();
@ -112,7 +138,7 @@ mod horizontal {
let Rect { x, y, width, height } = area;
let height = height.min(128);
let h = height.saturating_sub(2);
let h = height.saturating_sub(3);
let index_to_color = |index: usize, default: Color|
if notes_in[index] && notes_out[index] {
Color::Yellow
@ -208,15 +234,9 @@ mod horizontal {
Ok(area)
}
pub fn lanes (
buf: &mut Buffer,
area: Rect,
phrase: &Phrase,
ppq: usize,
time_z: usize,
time0: usize,
note0: usize,
) {
fn horizontal_lanes (&self, buf: &mut Buffer, area: Rect, phrase: &Phrase) {
let (ppq, time_z, time0, note0) =
(self.ppq, self.time_zoom, self.time_start, self.note_start);
let bg = Style::default();
let (bw, wh) = (bg.dim(), bg.white().not_dim());
@ -295,22 +315,8 @@ mod horizontal {
}
}
}
pub fn cursor (
buf: &mut Buffer,
area: Rect,
style: Style,
time: usize,
note: usize
) {
let x = area.x + 5 + time as u16;
let y = area.y + 1 + note as u16 / 2;
let c = if note % 2 == 0 { "" } else { "" };
c.blit(buf, x, y, Some(style));
}
}
//pub fn footer (
//buf: &mut Buffer,
//area: Rect,

View file

@ -1,4 +1,4 @@
use crate::core::*;
use crate::{core::*, model::App};
pub struct TransportView<'a> {
pub timebase: &'a Arc<Timebase>,
@ -10,6 +10,20 @@ pub struct TransportView<'a> {
pub quant: usize,
}
impl<'a> TransportView<'a> {
pub fn new (app: &'a App) -> Self {
Self {
timebase: &app.timebase,
playing: *app.playing.as_ref().unwrap_or(&TransportState::Stopped),
monitor: app.track().map(|t|t.1.monitoring).unwrap_or(false),
record: app.track().map(|t|t.1.recording).unwrap_or(false),
overdub: app.track().map(|t|t.1.overdub).unwrap_or(false),
frame: app.playhead,
quant: app.quant,
}
}
}
impl<'a> Render for TransportView<'a> {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
fill_bg(buf, area, Color::Rgb(20, 45, 5));