tek/src/view/arranger.rs

495 lines
18 KiB
Rust

use crate::core::*;
use crate::model::*;
use crate::view::*;
pub struct ArrangerView<'a> {
scenes: &'a[Scene],
tracks: &'a[Track],
cursor: (usize, usize),
focused: bool,
entered: bool,
vertical: bool,
}
const HELP: &'static str = "[C-t] Add track [C-a] Add scene [v] Show/hide track";
impl<'a> ArrangerView<'a> {
pub fn new (app: &'a App, vertical: bool) -> Self {
Self {
focused: app.section == AppSection::Arranger,
entered: app.entered,
scenes: &app.scenes,
tracks: &app.tracks,
cursor: (app.track_cursor, app.scene_cursor),
vertical
}
}
fn track_names (&self) -> Vec<&'a str> {
let mut track_names = vec!["Mix"];
for track in self.tracks.iter() {
track_names.push(track.name.as_str());
}
track_names
}
fn track_widths (&self) -> Vec<u16> {
self.track_names()
.iter()
.map(|name|(name.len() as u16).max(10) + 3)
.collect()
}
fn bg_color_lo (&self) -> Color {
if self.focused && self.entered {
Color::Rgb(25, 60, 15)
} else if self.focused {
Color::Rgb(20, 45, 5)
} else {
Color::Reset
}
}
fn bg_color_hi (&self) -> Color {
if self.focused && self.entered {
Color::Rgb(30, 90, 25)
} else if self.focused {
Color::Rgb(25, 60, 15)
} else {
Color::Reset
}
}
}
impl<'a> Render for ArrangerView<'a> {
fn render (&self, buf: &mut Buffer, mut area: Rect) -> Usually<Rect> {
area.height = area.height.min(if self.vertical {
self.scenes.len() as u16 + 3
} else {
self.tracks.len() as u16 * 2
});
fill_bg(buf, area, self.bg_color_lo());
area = if self.vertical {
self.draw_vertical(buf, area)
} else {
self.draw_horizontal(buf, area)
}?;
if self.focused {
//HELP.blit(buf, area.x + 2, area.y + area.height - 1, Some(Style::default().dim()))?;
if self.entered {
QuarterV(Style::default().green().dim()).draw(buf, area)?;
}
}
Ok(area)
}
}
impl<'a> ArrangerView<'a> {
fn vertical_scenes (&self, buf: &mut Buffer, x: u16, y: u16, height: u16) -> Usually<u16> {
let mut index = 0usize;
loop {
if index >= self.scenes.len() {
break
}
if y + index as u16 >= height {
break
}
self.vertical_scene(buf, x, y + index as u16, index)?;
index = index + 1;
}
Ok(self.scenes
.iter()
.map(|x|&x.name)
.fold(0, |x,y|x.max(y.len() as u16)))
}
fn vertical_scene (&self, buf: &mut Buffer, x: u16, y: u16, index: usize) -> Usually<()> {
if let Some(scene) = self.scenes.get(index) {
let style = Some(highlight(
self.focused,
(0 == self.cursor.0) && (index + 1 == self.cursor.1)
).bold());
"".blit(buf, x, y, style)?;
scene.name.blit(buf, x + 2, y, style)?;
}
Ok(())
}
fn vertical_clips (&self, buf: &mut Buffer, x: u16, y: u16, height: u16, track: usize) -> Usually<u16> {
let mut index = 0;
let mut width = 10;
loop {
if index >= self.scenes.len() {
break
}
if y + index as u16 >= height {
break
}
width = 10.max(self.vertical_clip(buf, x, y, track, index)?);
index = index + 1;
}
Ok(width)
}
fn vertical_clip (&self, buf: &mut Buffer, x: u16, y: u16, track: usize, index: usize) -> Usually<u16> {
Ok(if let Some(scene) = self.scenes.get(index) {
let hi = (track + 1 == self.cursor.0) &&
(index + 1 == self.cursor.1);
let style = Some(highlight(self.focused, hi));
let clip = scene.clips.get(track);
let index = index as u16;
let label = if let Some(Some(clip)) = clip {
if let Some(phrase) = self.tracks[track].phrases.get(*clip) {
format!("{} {}", if self.tracks[track].sequence == Some(*clip) {
""
} else {
" "
}, phrase.name)
} else {
format!(" ??? ")
}
} else {
format!(" ·········")
};
label.blit(buf, x, y + index, style)?;
label.len() as u16
} else {
0u16
})
}
fn draw_vertical (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
let Rect { x, y, width, height } = area;
let mut x2 = x;
let bg_color = self.bg_color_hi();
for (i, w) in self.track_widths().iter().enumerate() {
let focus_column = i == self.cursor.0;
if x2 >= x + width {
break
}
if focus_column {
let bg_area = Rect { x: x2, y, width: *w, height };
fill_bg(buf, bg_area, bg_color);
}
x2 = x2 + w;
}
let (mut x2, y2) = (x, y);
let columns = self.track_names();
for (i, title) in columns.iter().enumerate() {
let focus_column = i == self.cursor.0;
if x2 >= x + width {
break
}
let w = (title.len() as u16).max(10);
let bg_area = Rect { x: x2, y: if self.cursor.1 == 0 {
y2
} else {
y2 + 1 + self.cursor.1 as u16
}, width: w + 3, height: 1 };
fill_bg(buf, bg_area, bg_color);
if i == 0 {
self.vertical_scenes(buf, x2+1, y2+2, area.height)?;
} else if i < columns.len() {
self.vertical_clips(buf, x2+1, y2+2, area.height, i - 1)?;
};
let style = Some(highlight(self.focused, focus_column).bold());
if i > 0 {
"".blit(buf, x2, y2, Some(if self.tracks[i-1].recording {
Style::default().red()
} else if self.tracks[i-1].monitoring {
Style::default().yellow()
} else {
Style::default().black()
}))?;
}
title.blit(buf, x2+2, y2, style)?;
x2 = x2 + w + 3;
}
let bg_area = Rect { x: x2, y: if self.cursor.1 == 0 {
y2
} else {
y2 + 1 + self.cursor.1 as u16
}, width: width - x2, height: 1 };
fill_bg(buf, bg_area, bg_color);
Ok(area)
}
fn draw_vertical_2 (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
Split::right([
// Scene list
&|buf: &mut Buffer, mut area: Rect|Ok(Rect::default()),
// Track columns
&|buf: &mut Buffer, mut area: Rect|Ok(Rect::default()),
&|buf: &mut Buffer, mut area: Rect|Ok(Rect::default()),
&|buf: &mut Buffer, mut area: Rect|Ok(Rect::default()),
]).render(buf, area)
}
}
impl<'a> ArrangerView<'a> {
fn draw_horizontal (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
let style1 = Some(Style::default().bold().not_dim());
let style2 = Some(Style::default().not_dim());
let style3 = Some(Style::default().yellow().bold().not_dim());
let style4 = Some(Style::default().dim());
let style5 = Some(Style::default().white().bold().not_dim());
Split::right([
// Track name
&|buf: &mut Buffer, mut area: Rect|{
area.x = area.x + 1;
let mut width = 0;
for y in 0..area.height {
if y == 0 {
"MIX".blit(buf, area.x + 1, area.y + y, style1)?;
} else if y % 2 == 1 {
let index = y as usize / 2;
if let Some(track) = self.tracks.get(index) {
width = width.max(
track.name.blit(buf, area.x + 1, area.y + y, style5)?.width
);
if self.cursor.0 > 0 {
if index == self.cursor.0 - 1 {
"".blit(buf, area.x, area.y + y, style3)?;
}
}
} else {
area.height = y;
break
}
}
}
area.width = width + 1;
Ok(area)
},
&|buf: &mut Buffer, mut area: Rect|{
area.x = area.x + 1;
for y in 0..area.height {
if y == 0 {
" MON ".blit(buf, area.x, area.y + y, style2)?;
} else if y % 2 == 1 {
let index = y as usize / 2;
if let Some(track) = self.tracks.get(index) {
" MON ".blit(buf, area.x, area.y + y, if track.monitoring {
Some(Style::default().not_dim().green().bold())
} else {
style4
})?;
} else {
area.height = y;
break
}
}
}
area.width = 4;
Ok(area)
},
&|buf: &mut Buffer, mut area: Rect|{
area.x = area.x + 1;
for y in 0..area.height {
if y == 0 {
" REC ".blit(buf, area.x, area.y + y, style2)?;
} else if y % 2 == 1 {
let index = y as usize / 2;
if let Some(track) = self.tracks.get(index) {
" REC ".blit(buf, area.x, area.y + y, if track.recording {
Some(Style::default().not_dim().red().bold())
} else {
style4
})?;
} else {
area.height = y;
break
}
}
}
area.width = 4;
Ok(area)
},
&|buf: &mut Buffer, mut area: Rect|{
area.x = area.x + 1;
for y in 0..area.height {
if y == 0 {
" OVR ".blit(buf, area.x, area.y + y, style2)?;
} else if y % 2 == 1 {
let index = y as usize / 2;
if let Some(track) = self.tracks.get(index) {
" OVR ".blit(buf, area.x, area.y + y, if track.overdub {
Some(Style::default().not_dim().yellow().bold())
} else {
style4
})?;
} else {
area.height = y;
break
}
}
}
area.width = 4;
Ok(area)
},
&|buf: &mut Buffer, mut area: Rect|{
area.x = area.x + 1;
for y in 0..area.height {
if y == 0 {
" DEL ".blit(buf, area.x, area.y + y, style2)?;
} else if y % 2 == 1 {
let index = y as usize / 2;
if let Some(track) = self.tracks.get(index) {
" DEL ".blit(buf, area.x, area.y + y, style4)?;
} else {
area.height = y;
break
}
}
}
area.width = 4;
Ok(area)
},
&|buf: &mut Buffer, mut area: Rect|{
area.x = area.x + 1;
for y in 0..area.height {
if y == 0 {
" GAIN ".blit(buf, area.x, area.y + y, style2)?;
} else if y % 2 == 1 {
let index = y as usize / 2;
if let Some(track) = self.tracks.get(index) {
" +0.0 ".blit(buf, area.x, area.y + y, style4)?;
} else {
area.height = y;
break
}
}
}
area.width = 7;
Ok(area)
},
// Scene columns
&|buf: &mut Buffer, area: Rect|{
let mut x2 = 0;
let Rect { x, y, height, .. } = area;
for (i, scene) in self.scenes.iter().enumerate() {
let active_scene = self.cursor.1 > 0 && self.cursor.1 - 1 == i;
let sep = Some(if active_scene {
Style::default().yellow().not_dim()
} else {
Style::default().dim()
});
"".blit(buf, x + x2, y, sep)?;
for y in y+1..y+height-1 {
"".blit(buf, x + x2, y, sep)?;
}
"".blit(buf, x + x2, y+height-1, sep)?;
let mut x3 = scene.name.len() as u16;
scene.name.blit(buf, x + x2, y, Some(Style::default().bold().not_dim()))?;
for (i, clip) in scene.clips.iter().enumerate() {
let active_track = self.cursor.0 > 0 && self.cursor.0 - 1 == i;
if let Some(clip) = clip {
let y2 = y + 1 + i as u16 * 2;
let label = format!("{}", if let Some(phrase) = self.tracks[i].phrases.get(*clip) {
&phrase.name
} else {
"...."
});
label.blit(buf, x + x2, y2, Some(if active_track && active_scene {
Style::default().not_dim().yellow().bold()
} else {
Style::default().not_dim()
}))?;
x3 = x3.max(label.len() as u16)
}
}
x2 = x2 + x3;
x2 = x2 + 1;
}
Ok(Rect { x, y, height, width: x2 })
},
]).render(buf, area)
}
}
fn highlight (focused: bool, highlight: bool) -> Style {
if highlight {
if focused {
Style::default().yellow().not_dim()
} else {
Style::default().yellow().dim()
}
} else {
Style::default()
}
}
//use crate::core::*;
//use crate::view::*;
//use crate::model::*;
//pub fn render (state: &Mixer, buf: &mut Buffer, mut area: Rect)
//-> Usually<Rect>
//{
//if area.height < 2 {
//return Ok(area)
//}
//area.x = area.width.saturating_sub(80) / 2;
//area.width = area.width.min(80);
//area.height = state.tracks.len() as u16 + 2;
//draw_box(buf, area);
//let x = area.x + 1;
//let y = area.y + 1;
//let _h = area.height - 2;
//for (i, track) in state.tracks.iter().enumerate() {
////buf.set_string(
////x, y + index as u16,
////&track.name, Style::default().bold().not_dim()
////);
//for (j, (column, field)) in [
//(0, format!(" {:10} ", track.name)),
//(12, format!(" {:.1}dB ", track.gain)),
//(22, format!(" [ ] ")),
//(30, format!(" C ")),
//(35, format!(" {:.1}dB ", track.level)),
//(45, format!(" [ ] ")),
//(51, format!(" {:7} ", track.route)),
//].into_iter().enumerate() {
//buf.set_string(
//x + column as u16,
//y + i as u16,
//field,
//if state.selected_track == i && state.selected_column == j {
//Style::default().white().bold().not_dim()
//} else {
//Style::default().not_dim()
//}
//);
////stdout.queue(move_to(column, row))?;
////if state.selected_track == i && state.selected_column == j {
////stdout.queue(PrintStyledContent(field.to_string().bold().reverse()))?;
////} else {
////stdout.queue(PrintStyledContent(field.to_string().bold()))?;
////}
////fn render_meters (
////state: &mut Mixer,
////stdout: &mut Stdout,
////offset: Rect
////) -> Result<(), Box<dyn Error>> {
////let move_to = |col, row| crossterm::cursor::MoveTo(offset.0 + col, offset.1 + row);
////for (i, track) in state.tracks.iter().enumerate() {
////let row = (i + 1) as u16;
////stdout
////.queue(move_to(10, row))?.queue(PrintStyledContent("▁".green()))?
////.queue(move_to(20, row))?.queue(PrintStyledContent("▁".green()))?
////.queue(move_to(28, row))?.queue(PrintStyledContent("▁".green()))?
////.queue(move_to(43, row))?.queue(PrintStyledContent("▁".green()))?;
////}
////Ok(())
////}
//}
//}
//Ok(area)
//}