wip: refactor pt.42 (84e) lotta todo!

This commit is contained in:
🪞👃🪞 2024-11-15 22:06:52 +01:00
parent bf0e14c252
commit 638298ad32
11 changed files with 536 additions and 335 deletions

View file

@ -3,7 +3,7 @@ use crate::*;
pub struct PhrasePoolView<E: Engine> {
_engine: PhantomData<E>,
/// Collection of phrases
pub model: PhrasePoolModel,
pub phrases: Vec<Arc<RwLock<Phrase>>>,
/// Selected phrase
pub phrase: usize,
/// Scroll offset
@ -25,7 +25,7 @@ pub enum PhrasePoolMode {
}
impl<E: Engine> PhrasePoolView<E> {
pub fn new (model: PhrasePoolModel) -> Self {
pub fn new (phrases: Vec<Arc<RwLock<Phrase>>>) -> Self {
Self {
_engine: Default::default(),
scroll: 0,
@ -33,14 +33,14 @@ impl<E: Engine> PhrasePoolView<E> {
mode: None,
focused: false,
entered: false,
model,
phrases,
}
}
pub fn len (&self) -> usize {
self.model.phrases.len()
self.phrases.len()
}
pub fn phrase (&self) -> &Arc<RwLock<Phrase>> {
&self.model.phrases[self.phrase]
&self.phrases[self.phrase]
}
pub fn index_before (&self, index: usize) -> usize {
index.overflowing_sub(1).0.min(self.len() - 1)
@ -49,8 +49,8 @@ impl<E: Engine> PhrasePoolView<E> {
(index + 1) % self.len()
}
pub fn index_of (&self, phrase: &Phrase) -> Option<usize> {
for i in 0..self.model.phrases.len() {
if *self.model.phrases[i].read().unwrap() == *phrase { return Some(i) }
for i in 0..self.phrases.len() {
if *self.phrases[i].read().unwrap() == *phrase { return Some(i) }
}
return None
}
@ -61,33 +61,33 @@ impl<E: Engine> PhrasePoolView<E> {
}
pub fn delete_selected (&mut self) {
if self.phrase > 0 {
self.model.phrases.remove(self.phrase);
self.phrase = self.phrase.min(self.model.phrases.len().saturating_sub(1));
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.model.phrases.push(Self::new_phrase(name, color));
self.phrase = self.model.phrases.len() - 1;
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.model.phrases.insert(self.phrase + 1, Self::new_phrase(name, color));
self.phrases.insert(self.phrase + 1, Self::new_phrase(name, color));
self.phrase += 1;
}
pub fn insert_dup (&mut self) {
let mut phrase = self.model.phrases[self.phrase].read().unwrap().duplicate();
let mut phrase = self.phrases[self.phrase].read().unwrap().duplicate();
phrase.color = ItemColorTriplet::random_near(phrase.color, 0.25);
self.model.phrases.insert(self.phrase + 1, Arc::new(RwLock::new(phrase)));
self.phrases.insert(self.phrase + 1, Arc::new(RwLock::new(phrase)));
self.phrase += 1;
}
pub fn move_up (&mut self) {
if self.phrase > 1 {
self.model.phrases.swap(self.phrase - 1, self.phrase);
self.phrases.swap(self.phrase - 1, self.phrase);
self.phrase -= 1;
}
}
pub fn move_down (&mut self) {
if self.phrase < self.model.phrases.len().saturating_sub(1) {
self.model.phrases.swap(self.phrase + 1, self.phrase);
if self.phrase < self.phrases.len().saturating_sub(1) {
self.phrases.swap(self.phrase + 1, self.phrase);
self.phrase += 1;
}
}
@ -97,9 +97,9 @@ impl<E: Engine> PhrasePoolView<E> {
impl Content for PhrasePoolView<Tui> {
type Engine = Tui;
fn content (&self) -> impl Widget<Engine = Tui> {
let Self { focused, model, mode, .. } = self;
let Self { focused, phrases, mode, .. } = self;
let content = col!(
(i, phrase) in model.phrases.iter().enumerate() => Layers::new(|add|{
(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 {
@ -124,7 +124,7 @@ impl Content for PhrasePoolView<Tui> {
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!("({})", model.phrases.len());
let upper_right = format!("({})", phrases.len());
lay!(
content,
TuiStyle::fg(upper_left.to_string(), title_color).push_x(1).align_nw().fill_xy(),
@ -187,13 +187,13 @@ impl<E: Engine> Command<PhrasePoolView<E>> for PhrasePoolViewCommand {
view.phrase = phrase
},
Self::Edit(command) => {
return Ok(command.execute(&mut view.model)?.map(Self::Edit))
return Ok(command.execute(&mut view)?.map(Self::Edit))
}
Self::Rename(command) => match command {
Rename::Begin => {
view.mode = Some(PhrasePoolMode::Rename(
view.phrase,
view.model.phrases[view.phrase].read().unwrap().name.clone()
view.phrases[view.phrase].read().unwrap().name.clone()
))
},
_ => {
@ -204,7 +204,7 @@ impl<E: Engine> Command<PhrasePoolView<E>> for PhrasePoolViewCommand {
Length::Begin => {
view.mode = Some(PhrasePoolMode::Length(
view.phrase,
view.model.phrases[view.phrase].read().unwrap().length,
view.phrases[view.phrase].read().unwrap().length,
PhraseLengthFocus::Bar
))
},
@ -216,3 +216,244 @@ impl<E: Engine> Command<PhrasePoolView<E>> for PhrasePoolViewCommand {
Ok(None)
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum PhraseLengthCommand {
Begin,
Next,
Prev,
Inc,
Dec,
Set(usize),
Cancel,
}
impl InputToCommand<Tui, PhrasePoolView<Tui>> for PhraseLengthCommand {
fn input_to_command (view: &PhrasePoolView<Tui>, from: &TuiInput) -> Option<Self> {
if let Some(PhrasePoolMode::Length(_, length, _)) = view.mode {
Some(match from.event() {
key!(KeyCode::Up) => Self::Inc,
key!(KeyCode::Down) => Self::Dec,
key!(KeyCode::Right) => Self::Next,
key!(KeyCode::Left) => Self::Prev,
key!(KeyCode::Enter) => Self::Set(length),
key!(KeyCode::Esc) => Self::Cancel,
_ => return None
})
} else {
unreachable!()
}
}
}
impl<E: Engine> Command<PhrasePoolView<E>> for PhraseLengthCommand {
fn execute (self, view: &mut PhrasePoolView<E>) -> Perhaps<Self> {
use PhraseLengthFocus::*;
use PhraseLengthCommand::*;
if let Some(PhrasePoolMode::Length(phrase, ref mut length, ref mut focus)) = view.mode {
match self {
Self::Cancel => {
view.mode = None;
},
Self::Prev => {
focus.prev()
},
Self::Next => {
focus.next()
},
Self::Inc => match focus {
Bar => { *length += 4 * PPQ },
Beat => { *length += PPQ },
Tick => { *length += 1 },
},
Self::Dec => match focus {
Bar => { *length = length.saturating_sub(4 * PPQ) },
Beat => { *length = length.saturating_sub(PPQ) },
Tick => { *length = length.saturating_sub(1) },
},
Self::Set(length) => {
let mut phrase = view.phrases[phrase].write().unwrap();
let old_length = phrase.length;
phrase.length = length;
view.mode = None;
return Ok(Some(Self::Set(old_length)))
},
_ => unreachable!()
}
Ok(None)
} else if self == Begin {
view.mode = Some(PhrasePoolMode::Length(
view.phrase,
view.phrases[view.phrase].read().unwrap().length,
PhraseLengthFocus::Bar
));
Ok(None)
} else {
unreachable!()
}
}
}
/// 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())
}
}
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(),
"]"
)),
}
})
}
}
/// 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,
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum PhraseRenameCommand {
Begin,
Set(String),
Confirm,
Cancel,
}
impl InputToCommand<Tui, PhrasePoolView<Tui>> for PhraseRenameCommand {
fn input_to_command (view: &PhrasePoolView<Tui>, from: &TuiInput) -> Option<Self> {
if let Some(PhrasePoolMode::Rename(_, ref old_name)) = view.mode {
Some(match from.event() {
key!(KeyCode::Char(c)) => {
let mut new_name = old_name.clone();
new_name.push(*c);
Self::Set(new_name)
},
key!(KeyCode::Backspace) => {
let mut new_name = old_name.clone();
new_name.pop();
Self::Set(new_name)
},
key!(KeyCode::Enter) => Self::Confirm,
key!(KeyCode::Esc) => Self::Cancel,
_ => return None
})
} else {
unreachable!()
}
}
}
impl<E: Engine> Command<PhrasePoolView<E>> for PhraseRenameCommand {
fn execute (self, view: &mut PhrasePoolView<E>) -> Perhaps<Self> {
use PhraseRenameCommand::*;
if let Some(PhrasePoolMode::Rename(phrase, ref mut old_name)) = view.mode {
match self {
Set(s) => {
view.phrases[phrase].write().unwrap().name = s.into();
return Ok(Some(Self::Set(old_name.clone())))
},
Confirm => {
let old_name = old_name.clone();
view.mode = None;
return Ok(Some(Self::Set(old_name)))
},
Cancel => {
let mut phrase = view.phrases[phrase].write().unwrap();
phrase.name = old_name.clone();
},
_ => unreachable!()
};
Ok(None)
} else if self == Begin {
view.mode = Some(PhrasePoolMode::Rename(
view.phrase,
view.phrases[view.phrase].read().unwrap().name.clone()
));
Ok(None)
} else {
unreachable!()
}
}
}