Compare commits

..

No commits in common. '7054bdd469ff6007394b88d604e7b5d8e46aac4e' and '1c0f5ef623ae963bd1ab3def77a566a0968e103d' have entirely different histories.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 392 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 198 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 316 B

@ -1,28 +1,12 @@
// TODO:
// * Debug Panel
// * Describe cell I am hovering over (Note, Octave, etc)
// * Count # of cells on the board
// * Fix tiling repeating
// * Negative coordinates should not mirror
// * Fix randomly audio not working
// * space -> play/pause sim
// * m -> un/mute audio
#![allow(clippy::complexity)] #![allow(clippy::complexity)]
// Only because of FromTemplat macros unfortunately... // Only because of FromTemplat macros unfortunately...
#![allow(dead_code)] #![allow(dead_code)]
use bevy::{ use bevy::{
feathers::{ feathers::{containers::*, theme::ThemeProps},
constants::{fonts, size},
containers::*,
font_styles::InheritableFont,
theme::{InheritableThemeTextColor, ThemeProps},
tokens,
},
text::FontWeight,
ui::{Checked, InteractionDisabled}, ui::{Checked, InteractionDisabled},
ui_widgets::{ ui_widgets::{
ActivateOnPress, Checkbox, SliderPrecision, SliderStep, ValueChange, checkbox_self_update, ActivateOnPress, SliderPrecision, SliderStep, ValueChange, checkbox_self_update,
slider_self_update, slider_self_update,
}, },
}; };
@ -42,7 +26,6 @@ fn main() {
.init_resource::<CellAssets>() .init_resource::<CellAssets>()
.init_resource::<Panning>() .init_resource::<Panning>()
.init_resource::<PitchMap>() .init_resource::<PitchMap>()
.init_resource::<LastBoardState>()
.insert_resource(ClearColor(WHITE.into())) .insert_resource(ClearColor(WHITE.into()))
.insert_resource(UiTheme(create_light_theme())) .insert_resource(UiTheme(create_light_theme()))
.init_state::<SimulationPlayback>() .init_state::<SimulationPlayback>()
@ -84,8 +67,6 @@ fn main() {
reassign_cell_pitch.run_if(resource_changed::<PitchMap>), reassign_cell_pitch.run_if(resource_changed::<PitchMap>),
update_audio, update_audio,
update_material, update_material,
update_debug_info_cell_count,
update_debug_info_coordinates,
), ),
) )
.add_systems(Update, cell_lifecycle.run_if(on_message::<Lifecycle>)) .add_systems(Update, cell_lifecycle.run_if(on_message::<Lifecycle>))
@ -96,14 +77,6 @@ fn main() {
const SCALE: f32 = 100.0; const SCALE: f32 = 100.0;
#[derive(Component, PartialEq, Clone, Default)]
enum DebugInfo {
CellCount,
Coordinates,
#[default]
Empty,
}
#[derive(States, Debug, Default, Hash, PartialEq, Eq, Clone, Component, FromTemplate)] #[derive(States, Debug, Default, Hash, PartialEq, Eq, Clone, Component, FromTemplate)]
enum AudioPlayback { enum AudioPlayback {
#[default] #[default]
@ -127,8 +100,6 @@ enum UiButton {
AddLowOctave, AddLowOctave,
SubLowOctave, SubLowOctave,
Note, Note,
Reset,
Wipe,
#[default] #[default]
None, None,
} }
@ -149,8 +120,6 @@ impl UiButton {
Self::AddLowOctave => "todo.png", Self::AddLowOctave => "todo.png",
Self::SubLowOctave => "todo.png", Self::SubLowOctave => "todo.png",
Self::Note => "todo.png", Self::Note => "todo.png",
Self::Reset => "return.png",
Self::Wipe => "trashcan.png",
Self::None => todo!(), Self::None => todo!(),
} }
} }
@ -206,120 +175,24 @@ fn update_pitch_map_resource_ui(
}); });
} }
fn text_setup() -> impl Scene {
bsn! {
// Feathers propagates text styles only through entities that themselves have
// ThemedText, so a body holding loose text must carry these itself.
InheritableThemeTextColor(tokens::TEXT_MAIN)
InheritableFont {
font: fonts::REGULAR,
font_size: size::MEDIUM_FONT,
weight: FontWeight::NORMAL,
}
}
}
fn the_ui() -> impl Scene { fn the_ui() -> impl Scene {
bsn! {
Node {
width: Val::Percent(100.0),
top: Val::Px(0.0),
left: Val::Px(0.0),
display: Display::Flex,
flex_direction: FlexDirection::Row,
justify_content: JustifyContent::SpaceBetween,
}
Transform
Children [
primary_ui(),
debug_ui(),
]
}
}
fn debug_ui() -> impl Scene {
bsn! {
Node {
justify_self: JustifySelf::End,
display: Display::Flex,
flex_direction: FlexDirection::Column,
}
// TODO: Toggle visibility when debugging disabled
pane()
Transform
Children [
pane_header()
Children [
Text("Debug Info") ThemedText,
],
pane_body()
Children [
subpane()
Node {
display: Display::Flex,
flex_direction: FlexDirection::Row,
}
Children [
subpane_header() Children [
Text("Cell Count") ThemedText
]
subpane_body()
Node {
display: Display::Flex,
flex_direction: FlexDirection::Row,
align_items: AlignItems::Stretch,
justify_content: JustifyContent::Start,
}
text_setup()
Children [
Text("###") ThemedText template_value(DebugInfo::CellCount)
]
],
subpane()
Node {
display: Display::Flex,
flex_direction: FlexDirection::Row,
}
Children [
subpane_header() Children [
Text("Coordinates") ThemedText
]
subpane_body()
Node {
display: Display::Flex,
flex_direction: FlexDirection::Row,
align_items: AlignItems::Stretch,
justify_content: JustifyContent::Start,
}
text_setup()
Children [
Text("##,##") ThemedText template_value(DebugInfo::Coordinates)
]
]
]
]
}
}
fn primary_ui() -> impl Scene {
fn toggle_note( fn toggle_note(
change: On<ValueChange<bool>>, this: On<Activate>,
notes: Query<&Note, With<Checkbox>>, notes: Query<&Note, With<UiButton>>,
mut pitch_map: ResMut<PitchMap>, mut pitch_map: ResMut<PitchMap>,
) { ) {
if change.is_final { let this_note = notes.get(this.entity).unwrap();
// TODO: if last selected note and de-selected, undo that move please pitch_map.toggle(this_note);
let this_note = notes.get(change.source).unwrap();
pitch_map.set_note(this_note, change.value);
info!("(note {:?}) Updated pitch map: {:#?}", this_note, pitch_map);
}
} }
bsn! { bsn! {
Node { Node {
top: Val::Px(0.0),
left: Val::Px(0.0),
display: Display::Flex, display: Display::Flex,
flex_direction: FlexDirection::Column, flex_direction: FlexDirection::Column,
justify_content: JustifyContent::Start, justify_content: JustifyContent::Start,
} }
Transform Transform
pane() pane()
@ -362,13 +235,7 @@ fn primary_ui() -> impl Scene {
}), }),
ui_button(UiButton::Play) ui_button(UiButton::Play)
SimulationPlayback::Run SimulationPlayback::Run
on(| on(|_: On<Activate>, mut next: ResMut<NextState<SimulationPlayback>>| {
_: On<Activate>,
mut next: ResMut<NextState<SimulationPlayback>>,
mut last_board_state: ResMut<LastBoardState>,
coordinates: Query<&Coordinates, With<Cell>>
| {
last_board_state.coordinates = coordinates.iter().cloned().collect();
// Set simulation state // Set simulation state
next.set(SimulationPlayback::Run); next.set(SimulationPlayback::Run);
}), }),
@ -376,28 +243,6 @@ fn primary_ui() -> impl Scene {
on(|_: On<Activate>, mut next: ResMut<NextState<SimulationPlayback>>| { on(|_: On<Activate>, mut next: ResMut<NextState<SimulationPlayback>>| {
next.set(SimulationPlayback::Step); next.set(SimulationPlayback::Step);
}), }),
ui_button(UiButton::Reset)
on(|
_: On<Activate>,
last_board_state: Res<LastBoardState>,
mut lifecycle: MessageWriter<Lifecycle>,
cells: Query<Entity, With<Cell>>,
mut commands: Commands,
| {
// Todo: reconcile board instead of despawn/spawning the world
cells.iter().for_each(|e| {
commands.entity(e).despawn();
});
last_board_state.coordinates.iter().for_each(|c| {
lifecycle.write(Lifecycle::Alive(c.clone()));
});
}),
ui_button(UiButton::Wipe)
on(|_: On<Activate>, cells: Query<Entity, With<Cell>>, mut commands: Commands| {
cells.iter().for_each(|e| {
commands.entity(e).despawn();
});
}),
ui_button(UiButton::ZoomOut) ui_button(UiButton::ZoomOut)
on(|_: On<Activate>, mut projection: Query<&mut Projection>| { on(|_: On<Activate>, mut projection: Query<&mut Projection>| {
projection.iter_mut().for_each(|mut p| match *p { projection.iter_mut().for_each(|mut p| match *p {
@ -442,40 +287,40 @@ fn primary_ui() -> impl Scene {
} }
Children [ Children [
ui_checkbox("A") ui_checkbox("A")
template_value(Note::A) Note::A
on(toggle_note), on(toggle_note),
ui_checkbox("A#") ui_checkbox("A#")
template_value(Note::ASharp) Note::ASharp
on(toggle_note), on(toggle_note),
ui_checkbox("B") ui_checkbox("B")
template_value(Note::B) Note::B
on(toggle_note), on(toggle_note),
ui_checkbox("C") ui_checkbox("C")
template_value(Note::C) Note::C
on(toggle_note), on(toggle_note),
ui_checkbox("C#") ui_checkbox("C#")
template_value(Note::CSharp) Note::CSharp
on(toggle_note), on(toggle_note),
ui_checkbox("D") ui_checkbox("D")
template_value(Note::D) Note::D
on(toggle_note), on(toggle_note),
ui_checkbox("D#") ui_checkbox("D#")
template_value(Note::DSharp) Note::DSharp
on(toggle_note), on(toggle_note),
ui_checkbox("E") ui_checkbox("E")
template_value(Note::E) Note::E
on(toggle_note), on(toggle_note),
ui_checkbox("F") ui_checkbox("F")
template_value(Note::F) Note::F
on(toggle_note), on(toggle_note),
ui_checkbox("F#") ui_checkbox("F#")
template_value(Note::FSharp) Note::FSharp
on(toggle_note), on(toggle_note),
ui_checkbox("G") ui_checkbox("G")
template_value(Note::G) Note::G
on(toggle_note), on(toggle_note),
ui_checkbox("G#") ui_checkbox("G#")
template_value(Note::GSharp) Note::GSharp
on(toggle_note), on(toggle_note),
] ]
], ],
@ -497,7 +342,6 @@ fn primary_ui() -> impl Scene {
on(|value_change: On<ValueChange<f32>>, mut pitch_map: ResMut<PitchMap>| { on(|value_change: On<ValueChange<f32>>, mut pitch_map: ResMut<PitchMap>| {
if value_change.is_final { if value_change.is_final {
pitch_map.octaves_up = value_change.value as usize; pitch_map.octaves_up = value_change.value as usize;
info!("(octave up) Updated pitch map: {:#?}", pitch_map);
} }
}) })
on(slider_self_update), on(slider_self_update),
@ -507,7 +351,6 @@ fn primary_ui() -> impl Scene {
on(|value_change: On<ValueChange<f32>>, mut pitch_map: ResMut<PitchMap>| { on(|value_change: On<ValueChange<f32>>, mut pitch_map: ResMut<PitchMap>| {
if value_change.is_final { if value_change.is_final {
pitch_map.octaves_down = value_change.value as usize; pitch_map.octaves_down = value_change.value as usize;
info!("(octave down) Updated pitch map: {:#?}", pitch_map);
} }
}) })
on(slider_self_update), on(slider_self_update),
@ -520,11 +363,6 @@ fn primary_ui() -> impl Scene {
#[derive(Default, Debug, Resource)] #[derive(Default, Debug, Resource)]
struct NextCoordinates(Coordinates); struct NextCoordinates(Coordinates);
#[derive(Default, Debug, Resource)]
struct LastBoardState {
coordinates: Vec<Coordinates>,
}
#[derive(Debug, Default, Clone, PartialEq, Hash, Eq, Component, FromTemplate)] #[derive(Debug, Default, Clone, PartialEq, Hash, Eq, Component, FromTemplate)]
#[require(Visibility, Transform)] #[require(Visibility, Transform)]
struct Coordinates { struct Coordinates {
@ -772,23 +610,15 @@ fn new_cell(
let playback_settings = PlaybackSettings::ONCE.with_volume(volume); let playback_settings = PlaybackSettings::ONCE.with_volume(volume);
let cell_pitch = pitch_map.coordinates_cell_pitch(&c); info!("using coordinates {:?}", c);
#[derive(Clone, Default, Component)]
enum Foo {
#[default]
A,
}
bsn! { bsn! {
Cell Cell
Coordinates { x, y } Coordinates { x, y }
template_value(cell_pitch)
template_value(playback_settings) template_value(playback_settings)
template_value(Transform::from_translation(c.as_translation() + Vec3::new(0.0, 0.0, 1.0))) template_value(Transform::from_translation(c.as_translation() + Vec3::new(0.0, 0.0, 1.0)))
template_value(Mesh2dTemplate(cell_assets.mesh.clone().into())) template_value(Mesh2dTemplate(cell_assets.mesh.clone().into()))
// AudioPlayer added by update_audio template_value(AudioPlayerTemplate(cell_assets.pitches.get(&pitch_map.coordinates_cell_pitch(&c)).unwrap().clone().into()))
// MeshMaterial2d added by update_material
template_value(MeshMaterial2dTemplate(cell_assets.materials.get(&pitch_map.coordinates_cell_pitch(&c)).unwrap().clone().into())) template_value(MeshMaterial2dTemplate(cell_assets.materials.get(&pitch_map.coordinates_cell_pitch(&c)).unwrap().clone().into()))
} }
} }
@ -843,7 +673,8 @@ struct CellAssets {
mesh: Handle<Mesh>, mesh: Handle<Mesh>,
} }
#[derive(Component, Clone, Debug, Default, Reflect, PartialEq, Eq, Hash, Ord, PartialOrd)] // TODO: Impl Ord to sort Notes
#[derive(Hash, PartialEq, Eq, Debug, Clone, Component, Default, FromTemplate)]
enum Note { enum Note {
#[default] #[default]
A, A,
@ -897,7 +728,7 @@ impl Note {
} }
} }
#[derive(Component, Clone, Debug, Default, Reflect, PartialEq, Eq, Hash)] #[derive(Hash, PartialEq, Eq, Debug, Component)]
struct CellPitch { struct CellPitch {
note: Note, note: Note,
octave: isize, octave: isize,
@ -952,12 +783,12 @@ impl CellPitch {
Note::FSharp, Note::FSharp,
Note::G, Note::G,
Note::GSharp, Note::GSharp,
] ].into_iter().flat_map(|note| {
.into_iter() (-5..6).map(move |octave| {
.flat_map(|note| { CellPitch {
(-5..6).map(move |octave| CellPitch { note: note.clone(),
note: note.clone(), octave,
octave, }
}) })
}) })
} }
@ -1131,14 +962,12 @@ impl Default for PitchMap {
} }
impl PitchMap { impl PitchMap {
fn set_note(&mut self, n: &Note, value: bool) { fn toggle(&mut self, n: &Note) {
if !value && let Some(idx) = self.notes.iter().position(|v| v == n) { if let Some(idx) = self.notes.iter().position(|v| v == n) {
self.notes.swap_remove(idx); self.notes.swap_remove(idx);
} else if value { } else {
self.notes.push(n.clone()); self.notes.push(n.clone());
} }
// always ensure the notes list is sorted
self.notes.sort();
} }
/// Given the currently selected notes and octave range selected in the PitchMap convert the given coordinates to a CellPitch /// Given the currently selected notes and octave range selected in the PitchMap convert the given coordinates to a CellPitch
@ -1150,35 +979,37 @@ impl PitchMap {
}; };
let octave: isize = { let octave: isize = {
let num_octaves: usize = self.octaves_up + self.octaves_down + 1; let num_octaves: usize = self.octaves_up + self.octaves_down + 1;
let y_coord: usize = c.y.strict_abs() as usize; // TODO: shift first or something let y_coord: usize = c.y.strict_abs() as usize; // TODO: shift first or something
debug!("num_octaves: {:?}", num_octaves); info!("num_octaves: {:?}", num_octaves);
debug!("y_coord: {:?}", y_coord); info!("y_coord: {:?}", y_coord);
(y_coord % num_octaves) as isize - 5 (y_coord % num_octaves) as isize - 5
}; };
let cp = CellPitch { note, octave }; let cp = CellPitch { note, octave };
debug!("Cell Pitch: {:?}", cp); info!("Cell Pitch: {:?}", cp);
cp cp
} }
} }
/// When the pitch map changes, update all cells to use the correct pitch (note + octave) /// When the pitch map changes, update all cells to use the correct pitch (note + octave)
fn reassign_cell_pitch(mut query: Query<(&mut CellPitch, &Coordinates)>, pitch_map: Res<PitchMap>) { fn reassign_cell_pitch(
info!("query size: {:?}", query.iter().len()); mut query: Query<(&mut CellPitch, &Coordinates), With<Cell>>,
pitch_map: Res<PitchMap>,
) {
query.iter_mut().for_each(|(mut cell_pitch, c)| { query.iter_mut().for_each(|(mut cell_pitch, c)| {
*cell_pitch = pitch_map.coordinates_cell_pitch(c); *cell_pitch = pitch_map.coordinates_cell_pitch(c);
info!("Updating cell pitch for {:?}", c);
}); });
} }
// When CellPitch is added/updated on a cell, update the material // When CellPitch is added/updated on a cell, update the material
fn update_material( fn update_material(
mut query: Query<(&CellPitch, Entity), Or<(Added<CellPitch>, Changed<CellPitch>)>>, mut query: Query<
(&CellPitch, &mut MeshMaterial2d<ColorMaterial>),
Or<(Added<CellPitch>, Changed<CellPitch>)>,
>,
cell_assets: ResMut<CellAssets>, cell_assets: ResMut<CellAssets>,
mut commands: Commands,
) { ) {
query.iter_mut().for_each(|(cell_pitch, e)| { query.iter_mut().for_each(|(cell_pitch, mut material)| {
let h = cell_assets.materials.get(cell_pitch).unwrap().clone(); material.0 = cell_assets.materials.get(cell_pitch).unwrap().clone();
commands.entity(e).insert(MeshMaterial2d(h));
}) })
} }
@ -1193,22 +1024,3 @@ fn update_audio(
commands.entity(e).insert(AudioPlayer(h)); commands.entity(e).insert(AudioPlayer(h));
}); });
} }
fn update_debug_info_cell_count(query: Query<&Cell>, mut text: Query<(&DebugInfo, &mut Text)>) {
text.iter_mut()
.filter(|(di, _)| **di == DebugInfo::CellCount)
.for_each(|(_, mut t)| {
*t = Text::new(format!("{}", query.count()));
});
}
fn update_debug_info_coordinates(
coordinates: Res<NextCoordinates>,
mut text: Query<(&DebugInfo, &mut Text)>,
) {
text.iter_mut()
.filter(|(di, _)| **di == DebugInfo::Coordinates)
.for_each(|(_, mut t)| {
*t = Text::new(format!("{},{}", coordinates.0.x, coordinates.0.y));
});
}

Loading…
Cancel
Save