Files
rossnome/src/grid.rs
T

132 lines
4.5 KiB
Rust
Raw Normal View History

2025-09-20 22:44:26 -05:00
use std::ops::RangeInclusive;
2025-09-20 20:36:57 -05:00
use eframe::egui;
2025-10-04 00:43:45 -05:00
use eframe::egui::{Color32, Context, PointerButton, Pos2, Rangef, Sense, Ui, Vec2};
2025-09-27 01:13:01 -05:00
use serde::{Deserialize, Serialize};
2025-10-04 00:43:45 -05:00
use crate::gui::Showable;
2025-09-24 00:34:54 -05:00
use crate::noise::NoteType;
2025-09-26 01:06:33 -05:00
use crate::scale::Scale;
2025-09-24 00:34:54 -05:00
use crate::tenori::LOOP_LENGTH;
2025-09-20 20:36:57 -05:00
impl NoteType {
fn color(&self) -> Color32 {
match self {
NoteType::Sine => Color32::from_rgb(3, 211, 252),
NoteType::Triangle => Color32::from_rgb(252, 202, 3),
NoteType::Sawtooth => Color32::from_rgb(252, 18, 61),
NoteType::Square => Color32::from_rgb(4, 219, 51),
2025-09-20 22:44:26 -05:00
NoteType::Noise => Color32::from_rgb(240, 240, 240),
2025-09-20 20:36:57 -05:00
}
}
}
2025-09-27 01:13:01 -05:00
#[derive(Serialize, Deserialize, Clone)]
2025-09-20 20:36:57 -05:00
pub struct Grid {
2025-09-24 00:34:54 -05:00
pub note_type: NoteType,
pub volume: f32,
pub length: u64,
2025-09-26 01:06:33 -05:00
scale: Scale,
2025-09-20 20:36:57 -05:00
notes: Vec<bool>,
2025-09-24 00:34:54 -05:00
id: String
2025-09-20 20:36:57 -05:00
}
impl Grid {
pub fn for_note_type(note_type: NoteType, counter: usize) -> Self {
Self {
note_type,
2025-09-24 00:34:54 -05:00
volume: 1.0,
length: 250,
2025-09-26 01:06:33 -05:00
scale: Scale::CMajor,
2025-09-20 20:36:57 -05:00
notes: vec![false; (LOOP_LENGTH * LOOP_LENGTH) as usize],
id: format!("Track {}", counter)
}
}
2025-09-20 22:44:26 -05:00
fn draw_grid(&mut self, ui: &mut Ui, cursor: f32) {
let dim = 20.0 * LOOP_LENGTH as f32;
let (rect, response) = ui.allocate_exact_size(Vec2::new(dim, dim), Sense::click_and_drag());
for (i, lit) in self.notes.iter().enumerate() {
let (x, y) = (i as u32 % LOOP_LENGTH, i as u32 / LOOP_LENGTH);
let center = Pos2::new(
(x * 20 + 10) as f32 + rect.left(),
(y * 20 + 10) as f32 + rect.top());
if *lit {
ui.painter().circle_filled(center, 10.0, self.note_type.color());
} else {
ui.painter().circle_stroke(center, 10.0, (1.0, Color32::from_gray(0x88)));
2025-09-20 20:36:57 -05:00
}
2025-09-20 22:44:26 -05:00
}
2025-09-20 20:36:57 -05:00
2025-09-20 22:44:26 -05:00
ui.painter().vline(
cursor * dim + rect.left(),
Rangef::new(rect.top(), rect.top() + dim),
(1.0, self.note_type.color())
);
2025-09-20 20:36:57 -05:00
2025-09-20 22:44:26 -05:00
if response.contains_pointer() {
ui.input(|input| {
if input.pointer.button_clicked(PointerButton::Primary) {
if let Some(pos) = input.pointer.latest_pos() {
let x = ((pos.x - rect.left()) / 20.0).floor() as usize;
let y = ((pos.y - rect.top()) / 20.0).floor() as usize;
let n = x + y * LOOP_LENGTH as usize;
self.notes[n] = !self.notes[n]
2025-09-20 20:36:57 -05:00
}
2025-09-20 22:44:26 -05:00
}
})
}
2025-09-20 20:36:57 -05:00
}
2025-09-26 01:06:33 -05:00
pub fn notes(&self, beat: u32) -> Vec<i32> {
2025-09-20 20:36:57 -05:00
let mut notes = vec![];
for y in 0..LOOP_LENGTH {
if self.notes[(y * LOOP_LENGTH + beat) as usize] {
2025-09-26 01:06:33 -05:00
let row = LOOP_LENGTH - y - 1;
notes.push(self.scale.tone(row))
2025-09-20 20:36:57 -05:00
}
}
notes
}
}
2025-10-04 00:43:45 -05:00
impl Showable<f32> for Grid {
fn show(&mut self, ctx: &Context, cursor: &f32) {
let win = egui::Window::new(&self.id).resizable(false).scroll([false, false]);
win.show(ctx, |ui| {
egui::MenuBar::new().ui(ui, |ui| {
if ui.button("Clear").clicked() {
self.notes = vec![false; (LOOP_LENGTH * LOOP_LENGTH) as usize]
}
ui.menu_button("Scale...", |ui| {
if ui.button(Scale::CMajor.label_text(self.scale)).clicked() {
self.scale = Scale::CMajor
}
if ui.button(Scale::CMinor.label_text(self.scale)).clicked() {
self.scale = Scale::CMinor
}
if ui.button(Scale::Chromatic.label_text(self.scale)).clicked() {
self.scale = Scale::Chromatic
}
if ui.button(Scale::Pentatonic.label_text(self.scale)).clicked() {
self.scale = Scale::Pentatonic
}
})
});
egui::MenuBar::new().ui(ui, |ui| {
ui.label("Volume");
ui.add(egui::Slider::new(&mut self.volume, RangeInclusive::new(0.0, 2.0)).show_value(false));
ui.label("Length");
ui.add(egui::Slider::new(&mut self.length, RangeInclusive::new(0, 2000)).show_value(false));
});
egui::Frame::new().inner_margin(3).show(ui, |ui| {
self.draw_grid(ui, *cursor)
});
});
}
}