group modules and scripts

This commit is contained in:
🪞👃🪞 2025-03-04 19:02:48 +02:00
parent 93a14a3040
commit 8794b2e05b
32 changed files with 34 additions and 26 deletions

View file

@ -0,0 +1,388 @@
use crate::*;
impl Tek {
// FIXME: The memoized arranger is too complex.
// Just render a slice of the arranger for now,
// starting from tracks[scroll_offset] and ending
// at the last track that fits fully.
/// Blit the currently visible section of the arranger view buffer to the output.
///
/// If the arranger is larger than the available display area,
/// the scrollbars determine the portion that will be shown.
///
/// This function is called on every frame, but is relatively cheap
/// as the rendering logic of [redraw_arranger] is only invoked on
/// changes to the content.
pub fn view_arranger (&self) -> impl Content<TuiOut> + use<'_> {
Fill::xy(ThunkRender::new(move|to: &mut TuiOut|{
let [x0, y0, w, h] = to.area().xywh();
let source = self.arranger.read().unwrap();
for (source_x, target_x) in (x0..x0+w).enumerate() {
for (source_y, target_y) in (y0..y0+h).enumerate() {
let target_pos = Position::from((target_x, target_y));
if let Some(target) = to.buffer.cell_mut(target_pos) {
target.set_bg(Color::Rgb(128,0,0));
let source_pos = Position::from((source_x as u16, source_y as u16));
if let Some(source) = source.cell(source_pos) {
*target = source.clone();
target.set_bg(Color::Rgb(0,128,0));
}
}
}
}
}))
}
/// Draw the full arranger to the arranger view buffer.
///
/// This should happen on changes to the arrangement contents,
/// i.e. not on scroll. Scrolling just determines which part
/// of the arranger buffer to blit in [view_arranger].
pub fn redraw_arranger (&self) {
let width = self.w_tracks();
let height = self.h_scenes() + self.h_inputs() + self.h_outputs();
let buffer = Buffer::empty(ratatui::prelude::Rect { x: 0, y: 0, width, height });
let mut output = TuiOut { buffer, area: [0, 0, width, height] };
let layout = Bsp::s(self.view_inputs(),
Bsp::s(self.view_tracks(),
Bsp::n(self.view_outputs(),
self.view_scenes())));
Content::render(&layout, &mut output);
*self.arranger.write().unwrap() = output.buffer;
}
/// Display the current scene scroll state.
fn scene_scrollbar (&self) -> impl Content<TuiOut> + use<'_> {
let offset = self.scene_scroll;
let length = self.h_tracks_area() as usize;
let total = self.h_scenes() as usize;
Fill::y(Fixed::x(1, ScrollbarV { offset, length, total }))
}
/// Display the current track scroll state.
fn track_scrollbar (&self) -> impl Content<TuiOut> + use<'_> {
let offset = self.track_scroll;
let length = self.w_tracks_area() as usize;
let total = self.w_tracks() as usize;
Fill::x(Fixed::y(1, ScrollbarH { offset, length, total }))
}
/// Render something centered for each track.
fn per_track <'a, T: Content<TuiOut> + 'a> (
&'a self, f: impl Fn(usize, &'a Track)->T + Send + Sync + 'a
) -> impl Content<TuiOut> + 'a {
self.per_track_top(move|index, track|Fill::y(Align::y(f(index, track))))
}
/// Render something top-aligned for each track.
fn per_track_top <'a, T: Content<TuiOut> + 'a> (
&'a self, f: impl Fn(usize, &'a Track)->T + Send + Sync + 'a
) -> impl Content<TuiOut> + 'a {
let width = self.w_tracks_area();
let filter = move|(t, track, x1, x2)|if x2 as u16 >= width {
None
} else {
Some((t, track, x1, x2))
};
let tracks = move||self.tracks_sizes().map_while(filter);
Align::x(Tui::bg(Reset, Map::new(tracks, move|(index, track, x1, x2), _|{
let width = (x2 - x1) as u16;
map_east(x1 as u16, width, Fixed::x(width, Tui::fg_bg(
track.color.lightest.rgb,
track.color.base.rgb,
f(index, track))))
})))
}
pub fn view_tracks (&self) -> impl Content<TuiOut> + use<'_> {
let w = (self.size.w() as u16).saturating_sub(2 * self.w_sidebar());
let s = self.w_sidebar() as u16;
let data = (self.selected.track().unwrap_or(0), self.tracks().len());
let editing = self.is_editing();
self.fmtd.write().unwrap().trks.update(Some(data), rewrite!(buf, "{}/{}", data.0, data.1));
row(w, 1, s, button_3("t", "track", self.fmtd.read().unwrap().trks.view.clone(), editing),
self.per_track(|t, track|track_header(t, track, self.selected().track() == Some(t))),
button_2("T", "add track", editing))
}
pub fn view_scenes (&self) -> impl Content<TuiOut> + use<'_> {
let editing = self.is_editing();
let s = self.w_sidebar() as u16;
let w = self.w_tracks_area();
let w_full = self.w();
let h = self.h_scenes();
let h_area = self.h_tracks_area();
let selected_track = self.selected().track();
let selected_scene = self.selected().scene();
let track_scroll = self.track_scrollbar();
let scene_scroll = self.scene_scrollbar();
Tui::bg(Reset, Bsp::s(track_scroll, Bsp::e(scene_scroll, Fixed::y(h_area, row(w, h, s,
Map::new(
move||self.scenes_with_colors(editing, h_area),
move|(s, scene, y1, y2, prev): SceneWithColor, _|self.view_scene_name(
w_full, (1 + y2 - y1) as u16, y1 as u16, s, scene, prev)),
self.per_track(move|t, track|Map::new(
move||self.scenes_with_track_colors(editing, h_area, t),
move|(s, scene, y1, y2, prev): SceneWithColor, _|self.view_scene_clip(
w, (1 + y2 - y1) as u16, y1 as u16,
scene, prev, s, t, editing, selected_track == Some(t), selected_scene))),
() )))))
}
fn view_scene_name (
&self,
width: u16,
height: u16,
offset: u16,
index: usize,
scene: &Scene,
prev: Option<ItemPalette>,
) -> impl Content<TuiOut> + use<'_> {
Fill::x(map_south(offset, height, Fixed::y(height, scene_cell(
index == self.scenes.len().saturating_sub(1),
self.selected().scene(),
true,
index,
&scene.color,
prev,
Some(scene.name.clone()),
"",
scene.color.lightest.rgb
))))
}
fn view_scene_clip (
&self,
width: u16,
height: u16,
offset: u16,
scene: &Scene,
prev: Option<ItemPalette>,
scene_index: usize,
track_index: usize,
editing: bool,
same_track: bool,
selected_scene: Option<usize>
) -> impl Content<TuiOut> + use<'_> {
let (name, fg, bg) = if let Some(clip) = &scene.clips[track_index] {
let clip = clip.read().unwrap();
(Some(clip.name.clone()), clip.color.lightest.rgb, clip.color)
} else {
(None, Tui::g(96), ItemPalette::G[32])
};
let active = editing && same_track && selected_scene == Some(scene_index);
let edit = |x|Bsp::b(x, When(active, &self.editor));
map_south(offset, height, edit(Fixed::y(height, scene_cell(
scene_index == self.scenes.len().saturating_sub(1),
self.selected().scene(),
same_track,
scene_index,
&bg,
prev,
name,
"",
fg
))))
}
pub fn view_scene_add (&self) -> impl Content<TuiOut> + use<'_> {
let editing = self.is_editing();
let data = (self.selected().scene().unwrap_or(0), self.scenes().len());
self.fmtd.write().unwrap().scns.update(Some(data), rewrite!(buf, "({}/{})", data.0, data.1));
button_3("S", "add scene", self.fmtd.read().unwrap().scns.view.clone(), editing)
}
pub fn view_outputs (&self) -> impl Content<TuiOut> + use<'_> {
let editing = self.is_editing();
let w = self.w_tracks_area();
let s = self.w_sidebar() as u16;
let fg = Tui::g(224);
let nexts = self.per_track_top(|t, track|Either(
track.player.next_clip.is_some(),
Thunk::new(||Tui::bg(Reset, format!("{:?}",
track.player.next_clip.as_ref()
.map(|(moment, clip)|clip.as_ref()
.map(|clip|clip.read().unwrap().name.clone()))
.flatten().as_ref()))),
Thunk::new(||Tui::bg(Reset, " ------ "))));
let nexts = row_top(w, 2, s, Align::ne("Next:"), nexts, ());
let froms = self.per_track_top(|_, _|Tui::bg(Reset, Align::c(Bsp::s(" ------ ", OctaveVertical::default(),))));
let froms = row_top(w, 2, s, Align::ne("From:"), froms, ());
let ports = row_top(w, 1, s,
button_3("o", "midi outs", format!("{}", self.midi_outs.len()), editing),
self.per_track_top(move|t, track|{
let mute = false;
let solo = false;
let mute = if mute { White } else { track.color.darkest.rgb };
let solo = if solo { White } else { track.color.darkest.rgb };
let bg = if self.selected().track() == Some(t) {
track.color.light.rgb
} else {
track.color.base.rgb
};
let bg2 = if t > 0 { self.tracks()[t].color.base.rgb } else { Reset };
wrap(bg, fg, Tui::bold(true, Fill::x(Bsp::e(
Tui::fg_bg(mute, bg, "Play "),
Tui::fg_bg(solo, bg, "Solo ")))))}),
button_2("O", "add midi out", editing));
let routes = row_top(w, self.h_outputs() - 1, s,
io_ports(fg, Tui::g(32), ||self.outputs_sizes()),
self.per_track_top(move|t, track|io_conns(
track.color.dark.rgb, track.color.darker.rgb, ||self.outputs_sizes())), ());
Align::n(Bsp::s(Bsp::s(nexts, froms), Bsp::s(ports, routes)))
}
pub fn view_inputs (&self) -> impl Content<TuiOut> + use<'_> {
let editing = self.is_editing();
let w = (self.size.w() as u16).saturating_sub(2 * self.w_sidebar());
let s = self.w_sidebar() as u16;
let fg = Tui::g(224);
let routes = row_top(w, self.h_inputs() - 1, s,
io_ports(fg, Tui::g(32), ||self.inputs_sizes()),
self.per_track_top(move|t, track|io_conns(
track.color.dark.rgb,
track.color.darker.rgb,
||self.inputs_sizes()
)), ());
let ports = row_top(w, 1, s,
button_3("i", "midi ins", format!("{}", self.midi_ins.len()), editing),
self.per_track_top(move|t, track|{
let rec = track.player.recording;
let mon = track.player.monitoring;
let rec = if rec { White } else { track.color.darkest.rgb };
let mon = if mon { White } else { track.color.darkest.rgb };
let bg = if self.selected().track() == Some(t) {
track.color.light.rgb
} else {
track.color.base.rgb
};
let bg2 = if t > 0 { self.tracks()[t - 1].color.base.rgb } else { Reset };
wrap(bg, fg, Tui::bold(true, Fill::x(Bsp::e(
Tui::fg_bg(rec, bg, "Rec "),
Tui::fg_bg(mon, bg, "Mon ")))))
}),
button_2("I", "add midi in", editing));
Bsp::s(
Bsp::s(routes, ports),
row_top(w, 2, s,
Bsp::s(Align::e("Input:"), Align::e("Into:")),
self.per_track_top(|_, _|Tui::bg(Reset, Align::c(Bsp::s(
OctaveVertical::default(),
" ------ ")))), ())
)
}
}
fn scene_cell <'a> (
is_last: bool,
selected_scene: Option<usize>,
same_track: bool,
scene: usize,
color: &ItemPalette,
prev: Option<ItemPalette>,
name: Option<Arc<str>>,
icon: &'a str,
fg: Color,
) -> impl Content<TuiOut> + use<'a> {
Phat {
width: 0,
height: 0,
content: Fill::x(Align::w(Tui::bold(true, Bsp::e(icon, name)))),
colors: Tek::colors(
color,
prev,
same_track && selected_scene == Some(scene),
same_track && scene > 0 && selected_scene == Some(scene - 1),
is_last
)
}
}
fn track_header <'a> (t: usize, track: &'a Track, active: bool) -> impl Content<TuiOut> + use<'a> {
let name = &track.name;
let fg = track.color.lightest.rgb;
let bg = if active { track.color.light.rgb } else { track.color.base.rgb };
let bg2 = Reset;//if t > 0 { self.tracks()[t - 1].color.base.rgb } else { Reset };
let bfg = if active { Rgb(255,255,255) } else { Rgb(0,0,0) };
wrap(bg, fg, Tui::bold(true, Fill::x(Align::nw(name))))
}
fn io_ports <'a, T: PortsSizes<'a>> (
fg: Color,
bg: Color,
iter: impl Fn()->T + Send + Sync + 'a
) -> impl Content<TuiOut> + 'a {
Map::new(iter,
move|(index, name, connections, y, y2), _|map_south(y as u16, (y2-y) as u16, Bsp::s(
Fill::x(Tui::bold(true, Tui::fg_bg(fg, bg, Align::w(Bsp::e(" 󰣲 ", name))))),
Map::new(||connections.iter(), move|connect, index|map_south(index as u16, 1,
Fill::x(Align::w(Tui::bold(false, Tui::fg_bg(fg, bg,
&connect.info)))))))))
}
fn io_conns <'a, T: PortsSizes<'a>> (
fg: Color,
bg: Color,
iter: impl Fn()->T + Send + Sync + 'a
) -> impl Content<TuiOut> + 'a {
Map::new(iter,
move|(index, name, connections, y, y2), _|map_south(y as u16, (y2-y) as u16, Bsp::s(
Fill::x(Tui::bold(true, wrap(bg, fg, Fill::x(Align::w("▞▞▞▞ ▞▞▞▞"))))),
Map::new(||connections.iter(), move|connect, index|map_south(index as u16, 1,
Fill::x(Align::w(Tui::bold(false, wrap(bg, fg, Fill::x(""))))))))))
}
#[cfg(test)] mod test {
use super::*;
#[test] fn test_view_arranger () {
let mut output = TuiOut::default();
output.area[2] = 9;
output.area[3] = 9;
let mut app = Tek::default();
app.editor = Some(Default::default());
app.scenes_add(5);
app.tracks_add(5, Some(5), &[], &[]);
Content::render(&io_ports(Reset, Reset, ||app.inputs_sizes()), &mut output);
Content::render(&io_conns(Reset, Reset, ||app.outputs_sizes()), &mut output);
Content::render(&app.per_track(|_, _|()), &mut output);
Content::render(&app.per_track_top(|_, _|()), &mut output);
app.redraw_arranger();
Content::render(&app.view_arranger(), &mut output);
Content::render(&app.view_inputs(), &mut output);
Content::render(&app.view_outputs(), &mut output);
Content::render(&app.view_scenes(), &mut output);
Content::render(
&app.view_scene_name(0, 0, 0, 0, &Default::default(), None),
&mut output);
Content::render(
&app.view_scene_clip(0, 0, 0, &{
let mut scene: Scene = Default::default();
scene.clips.push(Some(Default::default()));
scene
}, None, 0, 0, false, false, None),
&mut output);
Content::render(
&track_header(0, &Default::default(), true),
&mut output);
Content::render(&scene_cell(
false,
None,
false,
0,
&Default::default(),
None,
None,
&"",
Default::default(),
), &mut output);
}
}