tek/src/view/arranger.rs
2024-07-11 17:38:52 +03:00

382 lines
13 KiB
Rust

use crate::core::*;
use crate::model::*;
use crate::view::*;
pub struct ArrangerView<'a> {
pub scenes: &'a[Scene],
pub tracks: &'a[Track],
pub cursor: (usize, usize),
pub focused: bool,
pub entered: bool,
pub vertical: bool,
}
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()
}
}
impl<'a> Render for ArrangerView<'a> {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
if self.vertical {
self.draw_vertical(buf, area)
} else {
self.draw_horizontal(buf, area)
}
}
}
impl<'a> ArrangerView<'a> {
fn draw_vertical (&self, buf: &mut Buffer, mut area: Rect) -> Usually<Rect> {
area.height = self.scenes.len() as u16 + 3;
let Rect { x, y, width, height } = area;
//let style = Some(Style::default().green().dim());
fill_bg(buf, area, if self.focused && self.entered {
Color::Rgb(25, 60, 15)
} else if self.focused {
Color::Rgb(20, 45, 5)
} else {
Color::Reset
});
if self.focused && self.entered {
QuarterV(Style::default().green().dim()).draw(buf, Rect { x, y, width, height })?;
//lozenge_left(buf, x, y, height, style);
//lozenge_right(buf, x + width - 1, y, height, style);
}
let bg_color = if self.focused && self.entered {
Color::Rgb(30, 90, 25)
} else if self.focused {
Color::Rgb(25, 60, 15)
} else {
Color::Rgb(20, 45, 5)
};
let mut x2 = x;
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);
fill_bg(
buf,
Rect { x: x2, y: if self.cursor.1 == 0 {
y2
} else {
y2 + 1 + self.cursor.1 as u16
}, width: w + 3, height: 1 },
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().green()
} else {
Style::default().black()
}));
}
title.blit(buf, x2+2, y2, style);
x2 = x2 + w + 3;
}
fill_bg(
buf,
Rect { x: x2, y: if self.cursor.1 == 0 {
y2
} else {
y2 + 1 + self.cursor.1 as u16
}, width: width - x2, height: 1 },
bg_color
);
if self.focused {
HELP.blit(buf, x + 2, y + height - 1, Some(Style::default().dim()));
}
Ok(area)
}
fn vertical_scenes (&self, buf: &mut Buffer, x: u16, y: u16, height: u16) -> 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;
}
longest_scene_name(&self.scenes)
}
fn vertical_scene (&self, buf: &mut Buffer, x: u16, y: u16, index: usize) {
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);
}
}
fn vertical_clips (&self, buf: &mut Buffer, x: u16, y: u16, height: u16, track: usize) -> 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;
}
width
}
fn vertical_clip (&self, buf: &mut Buffer, x: u16, y: u16, track: usize, index: usize) -> u16 {
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
}
}
}
impl<'a> ArrangerView<'a> {
fn draw_horizontal (&self, buf: &mut Buffer, mut area: Rect) -> Usually<Rect> {
area.height = self.tracks.len() as u16 * 2 + 2;
//let style = Some(Style::default().green().dim());
let Rect { x, y, width, height } = area;
fill_bg(buf, area, if self.focused && self.entered {
Color::Rgb(25, 60, 15)
} else if self.focused {
Color::Rgb(20, 45, 5)
} else {
Color::Reset
});
if self.focused && self.entered {
//RailV::draw(buf, Rect { x, y, width, height });
QuarterV(Style::default().green().dim()).draw(buf, Rect { x, y, width, height });
//lozenge_left(buf, x, y, height, style);
//lozenge_right(buf, x + width - 1, y, height, style);
}
let mut x2 = 0;
self.horizontal_tracks(buf, area, &mut x2);
x2 = x2 + 1;
self.horizontal_scenes(buf, area, &mut x2);
if self.focused {
HELP.blit(buf, x + 2, y + height - 1, Some(Style::default().dim()));
}
Ok(area)
}
fn horizontal_size (&self) -> (u16, u16) {
let w = (4 + longest_track_name(&self.tracks) + self.scenes.len() as u16 * 10) as u16;
let h = (2 + self.tracks.len()) as u16;
(w, h)
}
fn horizontal_tracks (&self, buf: &mut Buffer, area: Rect, x2: &mut u16) {
let style = Some(Style::default().bold().not_dim().white());
let Rect { x, y, height, .. } = area;
"Mix".blit(buf, x + 1, y, style);
let style = Some(Style::default().bold());
for (i, track) in self.tracks.iter().enumerate() {
let y2 = y + 1 + i as u16 * 2;
let label = format!(" {:8}", &track.name);
label.blit(buf, x + 1, y2, style);
"RDM".blit(buf, x + 10, y2, Some(Style::default().dim()));
" 0.0dB".blit(buf, x + 15, y2, Some(Style::default().dim()));
*x2 = (*x2).max(label.len() as u16 + 13);
}
"".blit(buf, x + *x2, y, style);
for y in y+1..y+height-1 {
"".blit(buf, x + *x2, y, style);
}
//"╵".blit(buf, x + x2, y+height-1, style);
}
fn horizontal_scenes (&self, buf: &mut Buffer, area: Rect, x2: &mut u16) {
let Rect { x, y, height, .. } = area;
let bold = Some(Style::default().bold());
let sep = Some(Style::default().dim());
for scene in self.scenes.iter() {
let mut x3 = scene.name.len() as u16;
scene.name.blit(buf, x + *x2, y, bold);
for (i, clip) in scene.clips.iter().enumerate() {
if let Some(clip) = clip {
if let Some(phrase) = self.tracks[i].phrases.get(*clip) {
let y2 = y + 1 + i as u16 * 2;
let label = format!("{}", &phrase.name);
label.blit(buf, x + *x2, y2, None);
x3 = x3.max(label.len() as u16)
}
}
}
*x2 = *x2 + x3;
"".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);
*x2 = *x2 + 1;
}
}
}
const HELP: &'static str = "[C-t] Add track [C-a] Add scene [v] Show/hide track";
fn highlight (focused: bool, highlight: bool) -> Style {
if highlight {
if focused {
Style::default().green().not_dim()
} else {
Style::default().green().dim()
}
} else {
Style::default()
}
}
fn longest_track_name (tracks: &[Track]) -> u16 {
let mut w = 3u16;
for track in tracks.iter() {
w = w.max(track.name.len() as u16);
}
w
}
fn longest_scene_name (scenes: &[Scene]) -> u16 {
let mut w = 3u16;
for scene in scenes.iter() {
w = w.max(scene.name.len() as u16);
}
w
}
//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)
//}