mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-08 04:36:45 +01:00
wip: refactor pt.6, fixed tek_api
This commit is contained in:
parent
5df08409e5
commit
869d92110d
29 changed files with 1678 additions and 1679 deletions
232
crates/tek_tui/src/tui_pool.rs
Normal file
232
crates/tek_tui/src/tui_pool.rs
Normal file
|
|
@ -0,0 +1,232 @@
|
|||
use crate::*;
|
||||
|
||||
pub struct PhrasePoolView<E: Engine> {
|
||||
_engine: PhantomData<E>,
|
||||
state: PhrasePool,
|
||||
/// Scroll offset
|
||||
pub scroll: usize,
|
||||
/// Mode switch
|
||||
pub mode: Option<PhrasePoolMode>,
|
||||
/// Whether this widget is focused
|
||||
pub focused: bool,
|
||||
/// Whether this widget is entered
|
||||
pub entered: bool,
|
||||
}
|
||||
|
||||
/// Modes for phrase pool
|
||||
pub enum PhrasePoolMode {
|
||||
/// Renaming a pattern
|
||||
Rename(usize, String),
|
||||
/// Editing the length of a pattern
|
||||
Length(usize, usize, PhraseLengthFocus),
|
||||
}
|
||||
|
||||
/// Displays and edits phrase length.
|
||||
pub struct PhraseLength<E: Engine> {
|
||||
_engine: PhantomData<E>,
|
||||
/// Pulses per beat (quaver)
|
||||
pub ppq: usize,
|
||||
/// Beats per bar
|
||||
pub bpb: usize,
|
||||
/// Length of phrase in pulses
|
||||
pub pulses: usize,
|
||||
/// Selected subdivision
|
||||
pub focus: Option<PhraseLengthFocus>,
|
||||
}
|
||||
|
||||
impl<E: Engine> PhraseLength<E> {
|
||||
pub fn new (pulses: usize, focus: Option<PhraseLengthFocus>) -> Self {
|
||||
Self { _engine: Default::default(), ppq: PPQ, bpb: 4, pulses, focus }
|
||||
}
|
||||
pub fn bars (&self) -> usize { self.pulses / (self.bpb * self.ppq) }
|
||||
pub fn beats (&self) -> usize { (self.pulses % (self.bpb * self.ppq)) / self.ppq }
|
||||
pub fn ticks (&self) -> usize { self.pulses % self.ppq }
|
||||
pub fn bars_string (&self) -> String { format!("{}", self.bars()) }
|
||||
pub fn beats_string (&self) -> String { format!("{}", self.beats()) }
|
||||
pub fn ticks_string (&self) -> String { format!("{:>02}", self.ticks()) }
|
||||
}
|
||||
|
||||
/// Focused field of `PhraseLength`
|
||||
#[derive(Copy, Clone)] pub enum PhraseLengthFocus {
|
||||
/// Editing the number of bars
|
||||
Bar,
|
||||
/// Editing the number of beats
|
||||
Beat,
|
||||
/// Editing the number of ticks
|
||||
Tick,
|
||||
}
|
||||
|
||||
impl PhraseLengthFocus {
|
||||
pub fn next (&mut self) {
|
||||
*self = match self {
|
||||
Self::Bar => Self::Beat,
|
||||
Self::Beat => Self::Tick,
|
||||
Self::Tick => Self::Bar,
|
||||
}
|
||||
}
|
||||
pub fn prev (&mut self) {
|
||||
*self = match self {
|
||||
Self::Bar => Self::Tick,
|
||||
Self::Beat => Self::Bar,
|
||||
Self::Tick => Self::Beat,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Engine> PhrasePool<E> {
|
||||
pub fn new () -> Self {
|
||||
Self {
|
||||
_engine: Default::default(),
|
||||
scroll: 0,
|
||||
phrase: 0,
|
||||
phrases: vec![Arc::new(RwLock::new(Phrase::default()))],
|
||||
mode: None,
|
||||
focused: false,
|
||||
entered: false,
|
||||
}
|
||||
}
|
||||
pub fn len (&self) -> usize { self.phrases.len() }
|
||||
pub fn phrase (&self) -> &Arc<RwLock<Phrase>> { &self.phrases[self.phrase] }
|
||||
pub fn select_prev (&mut self) { self.phrase = self.index_before(self.phrase) }
|
||||
pub fn select_next (&mut self) { self.phrase = self.index_after(self.phrase) }
|
||||
pub fn index_before (&self, index: usize) -> usize {
|
||||
index.overflowing_sub(1).0.min(self.len() - 1)
|
||||
}
|
||||
pub fn index_after (&self, index: usize) -> usize {
|
||||
(index + 1) % self.len()
|
||||
}
|
||||
pub fn index_of (&self, phrase: &Phrase) -> Option<usize> {
|
||||
for i in 0..self.phrases.len() {
|
||||
if *self.phrases[i].read().unwrap() == *phrase { return Some(i) }
|
||||
}
|
||||
return None
|
||||
}
|
||||
fn new_phrase (name: Option<&str>, color: Option<ItemColorTriplet>) -> Arc<RwLock<Phrase>> {
|
||||
Arc::new(RwLock::new(Phrase::new(
|
||||
String::from(name.unwrap_or("(new)")), true, 4 * PPQ, None, color
|
||||
)))
|
||||
}
|
||||
pub fn delete_selected (&mut self) {
|
||||
if self.phrase > 0 {
|
||||
self.phrases.remove(self.phrase);
|
||||
self.phrase = self.phrase.min(self.phrases.len().saturating_sub(1));
|
||||
}
|
||||
}
|
||||
pub fn append_new (&mut self, name: Option<&str>, color: Option<ItemColorTriplet>) {
|
||||
self.phrases.push(Self::new_phrase(name, color));
|
||||
self.phrase = self.phrases.len() - 1;
|
||||
}
|
||||
pub fn insert_new (&mut self, name: Option<&str>, color: Option<ItemColorTriplet>) {
|
||||
self.phrases.insert(self.phrase + 1, Self::new_phrase(name, color));
|
||||
self.phrase += 1;
|
||||
}
|
||||
pub fn insert_dup (&mut self) {
|
||||
let mut phrase = self.phrases[self.phrase].read().unwrap().duplicate();
|
||||
phrase.color = ItemColorTriplet::random_near(phrase.color, 0.25);
|
||||
self.phrases.insert(self.phrase + 1, Arc::new(RwLock::new(phrase)));
|
||||
self.phrase += 1;
|
||||
}
|
||||
pub fn randomize_color (&mut self) {
|
||||
let mut phrase = self.phrases[self.phrase].write().unwrap();
|
||||
phrase.color = ItemColorTriplet::random();
|
||||
}
|
||||
pub fn begin_rename (&mut self) {
|
||||
self.mode = Some(PhrasePoolMode::Rename(
|
||||
self.phrase,
|
||||
self.phrases[self.phrase].read().unwrap().name.clone()
|
||||
));
|
||||
}
|
||||
pub fn begin_length (&mut self) {
|
||||
self.mode = Some(PhrasePoolMode::Length(
|
||||
self.phrase,
|
||||
self.phrases[self.phrase].read().unwrap().length,
|
||||
PhraseLengthFocus::Bar
|
||||
));
|
||||
}
|
||||
pub fn move_up (&mut self) {
|
||||
if self.phrase > 1 {
|
||||
self.phrases.swap(self.phrase - 1, self.phrase);
|
||||
self.phrase -= 1;
|
||||
}
|
||||
}
|
||||
pub fn move_down (&mut self) {
|
||||
if self.phrase < self.phrases.len().saturating_sub(1) {
|
||||
self.phrases.swap(self.phrase + 1, self.phrase);
|
||||
self.phrase += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Display phrases always in order of appearance
|
||||
impl Content for PhrasePool<Tui> {
|
||||
type Engine = Tui;
|
||||
fn content (&self) -> impl Widget<Engine = Tui> {
|
||||
let Self { focused, phrases, mode, .. } = self;
|
||||
let content = col!(
|
||||
(i, phrase) in phrases.iter().enumerate() => Layers::new(|add|{
|
||||
let Phrase { ref name, color, length, .. } = *phrase.read().unwrap();
|
||||
let mut length = PhraseLength::new(length, None);
|
||||
if let Some(PhrasePoolMode::Length(phrase, new_length, focus)) = mode {
|
||||
if *focused && i == *phrase {
|
||||
length.pulses = *new_length;
|
||||
length.focus = Some(*focus);
|
||||
}
|
||||
}
|
||||
let length = length.align_e().fill_x();
|
||||
let row1 = lay!(format!(" {i}").align_w().fill_x(), length).fill_x();
|
||||
let mut row2 = format!(" {name}");
|
||||
if let Some(PhrasePoolMode::Rename(phrase, _)) = mode {
|
||||
if *focused && i == *phrase { row2 = format!("{row2}▄"); }
|
||||
};
|
||||
let row2 = TuiStyle::bold(row2, true);
|
||||
add(&col!(row1, row2).fill_x().bg(color.base.rgb))?;
|
||||
Ok(if *focused && i == self.phrase { add(&CORNERS)?; })
|
||||
})
|
||||
);
|
||||
let border_color = if *focused {Color::Rgb(100, 110, 40)} else {Color::Rgb(70, 80, 50)};
|
||||
let border = Lozenge(Style::default().bg(Color::Rgb(40, 50, 30)).fg(border_color));
|
||||
let content = content.fill_xy().bg(Color::Rgb(28, 35, 25)).border(border);
|
||||
let title_color = if *focused {Color::Rgb(150, 160, 90)} else {Color::Rgb(120, 130, 100)};
|
||||
let upper_left = format!("[{}] Phrases", if self.entered {"■"} else {" "});
|
||||
let upper_right = format!("({})", phrases.len());
|
||||
lay!(
|
||||
content,
|
||||
TuiStyle::fg(upper_left.to_string(), title_color).push_x(1).align_nw().fill_xy(),
|
||||
TuiStyle::fg(upper_right.to_string(), title_color).pull_x(1).align_ne().fill_xy(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Content for PhraseLength<Tui> {
|
||||
type Engine = Tui;
|
||||
fn content (&self) -> impl Widget<Engine = Tui> {
|
||||
Layers::new(move|add|{
|
||||
match self.focus {
|
||||
None => add(&row!(
|
||||
" ", self.bars_string(),
|
||||
".", self.beats_string(),
|
||||
".", self.ticks_string(),
|
||||
" "
|
||||
)),
|
||||
Some(PhraseLengthFocus::Bar) => add(&row!(
|
||||
"[", self.bars_string(),
|
||||
"]", self.beats_string(),
|
||||
".", self.ticks_string(),
|
||||
" "
|
||||
)),
|
||||
Some(PhraseLengthFocus::Beat) => add(&row!(
|
||||
" ", self.bars_string(),
|
||||
"[", self.beats_string(),
|
||||
"]", self.ticks_string(),
|
||||
" "
|
||||
)),
|
||||
Some(PhraseLengthFocus::Tick) => add(&row!(
|
||||
" ", self.bars_string(),
|
||||
".", self.beats_string(),
|
||||
"[", self.ticks_string(),
|
||||
"]"
|
||||
)),
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue