Files
Cagire/src/state/editor.rs

140 lines
3.0 KiB
Rust

use std::cell::RefCell;
use std::ops::RangeInclusive;
use cagire_ratatui::Editor;
#[derive(Clone, Copy, PartialEq, Eq, Default)]
pub enum EditorTarget {
#[default]
Step,
Prelude,
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum PatternField {
Length,
Speed,
}
#[derive(Clone, Copy, PartialEq, Eq, Default)]
pub enum PatternPropsField {
#[default]
Name,
Length,
Speed,
Quantization,
SyncMode,
}
impl PatternPropsField {
pub fn next(&self) -> Self {
match self {
Self::Name => Self::Length,
Self::Length => Self::Speed,
Self::Speed => Self::Quantization,
Self::Quantization => Self::SyncMode,
Self::SyncMode => Self::SyncMode,
}
}
pub fn prev(&self) -> Self {
match self {
Self::Name => Self::Name,
Self::Length => Self::Name,
Self::Speed => Self::Length,
Self::Quantization => Self::Speed,
Self::SyncMode => Self::Quantization,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Default)]
pub enum EuclideanField {
#[default]
Pulses,
Steps,
Rotation,
}
impl EuclideanField {
pub fn next(&self) -> Self {
match self {
Self::Pulses => Self::Steps,
Self::Steps => Self::Rotation,
Self::Rotation => Self::Rotation,
}
}
pub fn prev(&self) -> Self {
match self {
Self::Pulses => Self::Pulses,
Self::Steps => Self::Pulses,
Self::Rotation => Self::Steps,
}
}
}
pub struct EditorContext {
pub bank: usize,
pub pattern: usize,
pub step: usize,
pub editor: Editor,
pub selection_anchor: Option<usize>,
pub copied_steps: Option<CopiedSteps>,
pub show_stack: bool,
pub stack_cache: RefCell<Option<StackCache>>,
pub target: EditorTarget,
}
#[derive(Clone)]
pub struct StackCache {
pub cursor_line: usize,
pub lines_hash: u64,
pub result: String,
}
#[derive(Clone)]
pub struct CopiedSteps {
pub bank: usize,
pub pattern: usize,
pub steps: Vec<CopiedStepData>,
}
#[derive(Clone)]
pub struct CopiedStepData {
pub script: String,
pub active: bool,
pub source: Option<usize>,
pub original_index: usize,
pub name: Option<String>,
}
impl EditorContext {
pub fn selection_range(&self) -> Option<RangeInclusive<usize>> {
let anchor = self.selection_anchor?;
let a = anchor.min(self.step);
let b = anchor.max(self.step);
Some(a..=b)
}
pub fn clear_selection(&mut self) {
self.selection_anchor = None;
}
}
impl Default for EditorContext {
fn default() -> Self {
Self {
bank: 0,
pattern: 0,
step: 0,
editor: Editor::new(),
selection_anchor: None,
copied_steps: None,
show_stack: false,
stack_cache: RefCell::new(None),
target: EditorTarget::default(),
}
}
}