From 674285de46d6d29f110dd4659957a2bc7fce4613 Mon Sep 17 00:00:00 2001 From: aprzn Date: Fri, 10 Feb 2023 23:15:57 -0500 Subject: [PATCH] hopefully last refactor of editor widget reference stuff --- Cargo.lock | 156 ---------------------------------------------------- src/main.rs | 115 ++++++++++++++++++++++---------------- 2 files changed, 67 insertions(+), 204 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2512e4e..f26cc09 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -835,15 +835,6 @@ dependencies = [ "bytemuck", ] -[[package]] -name = "encoding_rs" -version = "0.8.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "071a31f4ee85403370b58aca746f01041ede6f0da2730960ad001edc2b71b394" -dependencies = [ - "cfg-if", -] - [[package]] name = "enumset" version = "1.0.12" @@ -1781,7 +1772,6 @@ dependencies = [ "home", "ordered-float", "rodio", - "symphonia", ] [[package]] @@ -2182,152 +2172,6 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" -[[package]] -name = "symphonia" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3671dd6f64f4f9d5c87179525054cfc1f60de23ba1f193bd6ceab812737403f1" -dependencies = [ - "lazy_static", - "symphonia-bundle-flac", - "symphonia-bundle-mp3", - "symphonia-codec-adpcm", - "symphonia-codec-pcm", - "symphonia-codec-vorbis", - "symphonia-core", - "symphonia-format-mkv", - "symphonia-format-ogg", - "symphonia-format-wav", - "symphonia-metadata", -] - -[[package]] -name = "symphonia-bundle-flac" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3dc2deed3204967871ba60f913378f95820cb47a2fe9b2eef5a9eedb417dfdc8" -dependencies = [ - "log", - "symphonia-core", - "symphonia-metadata", - "symphonia-utils-xiph", -] - -[[package]] -name = "symphonia-bundle-mp3" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "55a0846e7a2c9a8081ff799fc83a975170417ad2a143f644a77ec2e3e82a2b73" -dependencies = [ - "bitflags", - "lazy_static", - "log", - "symphonia-core", - "symphonia-metadata", -] - -[[package]] -name = "symphonia-codec-adpcm" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2a5cfb8d4405e26eb9593157dc45b05e102b8d774b38ed2a95946d6bb9e26e3e" -dependencies = [ - "log", - "symphonia-core", -] - -[[package]] -name = "symphonia-codec-pcm" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8cb9a9f0b9991cccf3217b74644af412d5d082a4815e5e2943f26e0ecabdf3c9" -dependencies = [ - "log", - "symphonia-core", -] - -[[package]] -name = "symphonia-codec-vorbis" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7dfed6f7b6bfa21d7cef1acefc8eae5db80df1608a1aca91871b07cbd28d7b74" -dependencies = [ - "log", - "symphonia-core", - "symphonia-utils-xiph", -] - -[[package]] -name = "symphonia-core" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b9567e2d8a5f866b2f94f5d366d811e0c6826babcff6d37de9e1a6690d38869" -dependencies = [ - "arrayvec 0.7.2", - "bitflags", - "bytemuck", - "lazy_static", - "log", -] - -[[package]] -name = "symphonia-format-mkv" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bd22f2def8c8f078495ad66111648bfc7d5222ee33774f2077cb665588f3119" -dependencies = [ - "lazy_static", - "log", - "symphonia-core", - "symphonia-metadata", - "symphonia-utils-xiph", -] - -[[package]] -name = "symphonia-format-ogg" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "474df6e86b871dcb56913130bada1440245f483057c4a2d8a2981455494c4439" -dependencies = [ - "log", - "symphonia-core", - "symphonia-metadata", - "symphonia-utils-xiph", -] - -[[package]] -name = "symphonia-format-wav" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06679bd5646b3037300f88891dfc8a6e1cc4e1133206cc17a98e5d7c22f88296" -dependencies = [ - "log", - "symphonia-core", - "symphonia-metadata", -] - -[[package]] -name = "symphonia-metadata" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "acd35c263223ef6161000be79b124a75de3e065eea563bf3ef169b3e94c7bb2e" -dependencies = [ - "encoding_rs", - "lazy_static", - "log", - "symphonia-core", -] - -[[package]] -name = "symphonia-utils-xiph" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce340a6c33ac06cb42de01220308ec056e8a2a3d5cc664aaf34567392557136b" -dependencies = [ - "symphonia-core", - "symphonia-metadata", -] - [[package]] name = "syn" version = "1.0.107" diff --git a/src/main.rs b/src/main.rs index cb793e0..f3ba112 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,8 +5,6 @@ use eframe; use eframe::egui; use std::boxed::Box; use std::collections::VecDeque; -use std::marker::PhantomData; -use symphonia::core::io::{MediaSource, MediaSourceStream}; struct PipeDash { msg_queue: VecDeque, @@ -29,10 +27,14 @@ enum Message { } struct Editor { + state: EditorState, + data: GdlData, +} + +struct EditorState { scroll_pos: f32, - pts_per_second: f32, + pts_per_second: f32, // zoom level subdivisions: f32, - data: GdlData, } struct GdlData { @@ -46,20 +48,30 @@ struct GdlData { struct Song { name: String, id: u32, - stream: MediaSourceStream, } -struct Orange; -struct Yellow; -struct Green; - -struct BeatRateWidget<'a>(&'a mut Editor); -struct TimeSignatureWidget<'a>(&'a mut Editor); -struct LinesWidget<'a, C: WithColor>(&'a mut Editor, PhantomData); -struct WaveformWidget<'a>(&'a mut Editor); +struct BeatRateWidget<'a> { + state: &'a mut EditorState, + beat_rate: &'a mut music::BeatRate, +} +struct TimeSignatureWidget<'a> { + state: &'a mut EditorState, + time_signatures: &'a mut music::TimeSignature, +} +struct LinesWidget<'a> { + state: &'a mut EditorState, + lines: &'a mut music::Lines, + color: Color, +} +struct WaveformWidget<'a> { + state: &'a mut EditorState, + song: &'a Song, +} -trait WithColor { - const COLOR: Color; +fn allocate_editor_space(ui: &mut egui::Ui) -> (egui::Rect, egui::Response) { + let max_rect = ui.max_rect(); + let preferred_size = egui::Vec2::new(max_rect.size().x, 60.0); + ui.allocate_exact_size(preferred_size, egui::Sense::click_and_drag()) } impl From for eframe::epaint::Color32 { @@ -72,43 +84,46 @@ impl From for eframe::epaint::Color32 { } } -impl WithColor for Orange { - const COLOR: Color = Color::Orange; -} -impl WithColor for Yellow { - const COLOR: Color = Color::Yellow; -} -impl WithColor for Green { - const COLOR: Color = Color::Green; -} - impl Editor { pub fn beat_rate_widget(&mut self) -> BeatRateWidget { - BeatRateWidget(self) + BeatRateWidget { + state: &mut self.state, + beat_rate: &mut self.data.beat_rate, + } } pub fn time_signature_widget(&mut self) -> TimeSignatureWidget { - TimeSignatureWidget(self) + TimeSignatureWidget { + state: &mut self.state, + time_signatures: &mut self.data.time_signatures, + } } - pub fn lines_widget(&mut self) -> LinesWidget { - LinesWidget(self, Default::default()) + pub fn lines_widget(&mut self, col: Color) -> LinesWidget { + LinesWidget { + state: &mut self.state, + lines: match col { + Color::Green => &mut self.data.green_lines, + Color::Yellow => &mut self.data.yellow_lines, + Color::Orange => &mut self.data.orange_lines, + }, + color: col, + } } - pub fn waveform_widget(&mut self) -> WaveformWidget { - WaveformWidget(self) + pub fn waveform_widget<'a>(&'a mut self, song: &'a Song) -> WaveformWidget { + WaveformWidget { + state: &mut self.state, + song, + } } } impl<'a> egui::Widget for BeatRateWidget<'a> { fn ui(self, ui: &mut egui::Ui) -> egui::Response { - // 1. choose size - let max_rect = ui.max_rect(); - let preferred_size = egui::Vec2::new(max_rect.size().x, 60.0); - // 2. allocate space - let (rect, res) = ui.allocate_exact_size(preferred_size, egui::Sense::click_and_drag()); - // 3. handle interactions - // 4. draw widget + let (rect, res) = allocate_editor_space(ui); + // handle interactions + // draw widget if ui.is_rect_visible(rect) { ui.painter() .rect_filled(rect, 0f32, eframe::epaint::Color32::from_gray(0)); @@ -134,7 +149,7 @@ impl<'a> egui::Widget for TimeSignatureWidget<'a> { } } -impl<'a, C: WithColor> egui::Widget for LinesWidget<'a, C> { +impl<'a> egui::Widget for LinesWidget<'a> { fn ui(self, ui: &mut egui::Ui) -> egui::Response { // 1. choose size let max_rect = ui.max_rect(); @@ -176,9 +191,11 @@ impl PipeDash { level_list: gd::OuterLevel::load_all(), loaded_song: None, editor: Editor { - scroll_pos: 0f32, - pts_per_second: 5f32, - subdivisions: 4.0, + state: EditorState { + scroll_pos: 0f32, + pts_per_second: 5f32, + subdivisions: 4.0, + }, data: GdlData { beat_rate: music::StaticBeatRate::from_bpm(120f32).into(), time_signatures: music::StaticTimeSignature::new(4, 4).into(), @@ -233,12 +250,14 @@ impl PipeDash { .size(20.0), ); - ui.add(self.editor.time_signature_widget()); - ui.add(self.editor.beat_rate_widget()); - ui.add(self.editor.lines_widget::()); - ui.add(self.editor.lines_widget::()); - ui.add(self.editor.lines_widget::()); - ui.add(self.editor.waveform_widget()); + if let Some(song) = &self.loaded_song { + ui.add(self.editor.time_signature_widget()); + ui.add(self.editor.beat_rate_widget()); + ui.add(self.editor.lines_widget(Color::Green)); + ui.add(self.editor.lines_widget(Color::Orange)); + ui.add(self.editor.lines_widget(Color::Yellow)); + ui.add(self.editor.waveform_widget(&song)); + } }); }); }