grid -> arranger

This commit is contained in:
🪞👃🪞 2024-07-08 19:50:18 +03:00
parent d99a08bcf7
commit f1f812d0fb
5 changed files with 131 additions and 191 deletions

382
src/view/arranger.rs Normal file
View file

@ -0,0 +1,382 @@
use crate::core::*;
use crate::model::*;
use crate::view::*;
pub struct ArrangerViewVertical<'a> {
pub buf: &'a mut Buffer,
pub area: Rect,
pub scenes: &'a[Scene],
pub tracks: &'a[Track],
pub cursor: &'a(usize, usize),
pub focused: bool,
pub entered: bool,
}
impl<'a> ArrangerViewVertical<'a> {
pub fn size (&self) -> (u16, u16) {
unimplemented!();
}
pub fn draw (&mut self) -> Usually<Rect> {
self.area.height = self.scenes.len() as u16 + 3;
let Rect { x, y, width, height } = self.area;
//let style = Some(Style::default().green().dim());
fill_bg(&mut self.buf, self.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(self.buf, Rect { x, y, width, height });
//lozenge_left(self.buf, x, y, height, style);
//lozenge_right(self.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 {
fill_bg(
&mut self.buf,
Rect { x: x2, y, width: *w, height },
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(
&mut self.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.scenes(x2+1, y2+2);
} else if i < columns.len() {
self.clips(x2+1, y2+2, i - 1);
};
let style = Some(highlight(self.focused, focus_column).bold());
if i > 0 {
"".blit(self.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(self.buf, x2+2, y2, style);
x2 = x2 + w + 3;
}
fill_bg(
&mut self.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(self.buf, x + 2, y + height - 1, Some(Style::default().dim()));
}
Ok(self.area)
}
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 scenes (&mut self, x: u16, y: u16) -> u16 {
let mut index = 0usize;
loop {
if index >= self.scenes.len() {
break
}
if y + index as u16 >= self.area.height {
break
}
self.scene(x, y + index as u16, index);
index = index + 1;
}
longest_scene_name(&self.scenes)
}
fn scene (&mut self, 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(self.buf, x, y, style);
scene.name.blit(self.buf, x + 2, y, style);
}
}
fn clips (&mut self, x: u16, y: u16, track: usize) -> u16 {
let mut index = 0;
let mut width = 10;
loop {
if index >= self.scenes.len() {
break
}
if y + index as u16 >= self.area.height {
break
}
width = 10.max(self.clip(x, y, track, index));
index = index + 1;
}
width
}
fn clip (&mut self, 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(self.buf, x, y + index, style);
label.len() as u16
} else {
0u16
}
}
fn separator_v (&mut self, x: u16, hi: bool) {
let style = Some(highlight(self.focused, hi));
for y in self.area.y+1..self.area.y+self.area.height-1 {
"".blit(self.buf, x, y, style);
}
}
}
pub struct ArrangerViewHorizontal<'a> {
pub buf: &'a mut Buffer,
pub area: Rect,
pub scenes: &'a[Scene],
pub tracks: &'a[Track],
pub cursor: &'a(usize, usize),
pub focused: bool,
pub entered: bool,
}
impl<'a> ArrangerViewHorizontal<'a> {
pub fn 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)
}
pub fn draw (&mut self) -> Usually<Rect> {
self.area.height = self.tracks.len() as u16 * 2 + 2;
//let style = Some(Style::default().green().dim());
let Rect { x, y, width, height } = self.area;
fill_bg(&mut self.buf, self.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(self.buf, Rect { x, y, width, height });
QuarterV(Style::default().green().dim()).draw(self.buf, Rect { x, y, width, height });
//lozenge_left(self.buf, x, y, height, style);
//lozenge_right(self.buf, x + width - 1, y, height, style);
}
let mut x2 = 0;
self.draw_tracks(&mut x2);
x2 = x2 + 1;
self.draw_scenes(&mut x2);
if self.focused {
HELP.blit(self.buf, x + 2, y + height - 1, Some(Style::default().dim()));
}
Ok(self.area)
}
fn draw_tracks (&mut self, x2: &mut u16) {
let style = Some(Style::default().bold().not_dim().white());
let Rect { x, y, height, .. } = self.area;
"Mix".blit(self.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(self.buf, x + 1, y2, style);
"RDM".blit(self.buf, x + 10, y2, Some(Style::default().dim()));
" 0.0dB".blit(self.buf, x + 15, y2, Some(Style::default().dim()));
*x2 = (*x2).max(label.len() as u16 + 13);
}
"".blit(self.buf, x + *x2, y, style);
for y in y+1..y+height-1 {
"".blit(self.buf, x + *x2, y, style);
}
//"╵".blit(self.buf, x + x2, y+height-1, style);
}
fn draw_scenes (&mut self, x2: &mut u16) {
let Rect { x, y, height, .. } = self.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(self.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(self.buf, x + *x2, y2, None);
x3 = x3.max(label.len() as u16)
}
}
}
*x2 = *x2 + x3;
"".blit(self.buf, x + *x2, y, sep);
for y in y+1..y+height-1 {
"".blit(self.buf, x + *x2, y, sep);
}
"".blit(self.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)
//}