Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to bevy 0.12 #34

Merged
merged 19 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ Cargo.lock
**/*.rs.bk

package-lock.json
.vscode/launch.json
19 changes: 10 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bevy_midi"
version = "0.6.0"
version = "0.7.0"
authors = ["Black Phlox <bphlox@gmail.com>"]
edition = "2021"
license = "MIT OR Apache-2.0"
Expand All @@ -22,23 +22,24 @@ name = "bevy_midi"

[dependencies]
midir = "0.9"
crossbeam-channel = "0.5.6"
crossbeam-channel = "0.5.8"

[dev-dependencies]
bevy_egui = { version = "0.20", features = ["immutable_ctx"]}
strum = { version = "0.24", features = ["derive"] }
bevy_mod_picking = "0.12"
bevy_egui = { version = "0.23", features = ["immutable_ctx"]}
strum = { version = "0.25", features = ["derive"] }
bevy_mod_picking = "0.17"

[dependencies.bevy]
version = "0.10"
version = "0.12"
default-features = false
features = ["multi-threaded"]

[dev-dependencies.bevy]
version = "0.10"
features = ["bevy_core_pipeline","bevy_asset", "bevy_scene", "bevy_render", "bevy_winit", "bevy_gltf", "bevy_ui", "bevy_text"]
version = "0.12"
features = ["bevy_core_pipeline","bevy_asset", "bevy_scene", "bevy_render", "bevy_winit", "bevy_gltf", "bevy_ui", "bevy_text", "zstd", "tonemapping_luts", "ktx2", "hdr"]
default-features = false

[target.'cfg(target_os = "linux")'.dev-dependencies.bevy]
version = "0.10"
version = "0.12"
features = ["x11", "wayland"]
default-features = false
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ See examples
|0.7|0.4.X|
|0.8|0.5.X|
|0.10|0.6.X|
|0.12|0.7.X|

# Licensing
The project is under dual license MIT and Apache 2.0, so joink to your hearts content, just remember the license agreements.
Expand Down
19 changes: 12 additions & 7 deletions examples/egui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use std::iter::{Cycle, Peekable};

use bevy::prelude::*;
use bevy_egui::{
egui::{self, Color32, ColorImage, ImageButton, Key, TextureHandle, TextureOptions, Ui},
egui::{
self, load::SizedTexture, Color32, ColorImage, ImageButton, Key, TextureHandle,
TextureOptions, Ui,
},
EguiContext, EguiPlugin,
};
use bevy_midi::prelude::*;
Expand All @@ -14,10 +17,10 @@ use strum::{EnumCount, EnumIter, IntoEnumIterator};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugin(EguiPlugin)
.add_plugins(EguiPlugin)
// Systems that create Egui widgets should be run during the `CoreStage::Update` stage,
// or after the `EguiSystem::BeginFrame` system (which belongs to the `CoreStage::PreUpdate` stage).
.add_system(ui_example)
.add_systems(Update, ui_example)
.init_resource::<PianoRoll>()
.run();
}
Expand Down Expand Up @@ -183,7 +186,7 @@ impl PianoRoll {
}

fn update_key_states(&mut self, ui: &mut Ui) {
let input = ui.input(|i| i.key_pressed(egui::Key::A));
let _input = ui.input(|i| i.key_pressed(egui::Key::A));
let next_keys = std::array::from_fn(|index| ui.input(|i| i.key_down(KEYS[index])));

self.key_states
Expand Down Expand Up @@ -214,7 +217,6 @@ impl PianoRoll {
});

self.key_states = next_keys;
drop(input);
}

fn get_key_texture_tint(&self, note: NoteName, index: usize) -> Color32 {
Expand Down Expand Up @@ -257,6 +259,7 @@ impl PianoRoll {
)
})
.id();

// Draw the actual piano keys for clicking
ui.vertical(|ui| {
ui.spacing_mut().item_spacing = bevy_egui::egui::Vec2 {
Expand All @@ -271,7 +274,8 @@ impl PianoRoll {
all_notes_iter.for_each(|(index, (note, _octave))| {
let color = self.get_key_texture_tint(note, index);

let button_top = ImageButton::new(texture_id, TOP_KEY_SIZE).tint(color);
let button_top =
ImageButton::new(SizedTexture::new(texture_id, TOP_KEY_SIZE)).tint(color);
if ui.add(button_top).clicked() {
//sync.trigger_note(index, selected_instrument);
println!("Pressed {}{}", KEY_RANGE[index % 12], index / 12);
Expand All @@ -291,7 +295,8 @@ impl PianoRoll {
let tint = self.get_key_texture_tint(note, index);

let button_bottom =
ImageButton::new(texture_id, BOTTOM_KEY_SIZE).tint(tint);
ImageButton::new(SizedTexture::new(texture_id, BOTTOM_KEY_SIZE))
.tint(tint);

if ui.add(button_bottom).clicked() {
//sync.trigger_note(index, selected_instrument);
Expand Down
23 changes: 14 additions & 9 deletions examples/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,19 @@ fn main() {
level: Level::WARN,
filter: "bevy_midi=debug".to_string(),
}))
.add_plugin(MidiInputPlugin)
.add_system(refresh_ports)
.add_system(connect)
.add_system(disconnect)
.add_system(show_ports)
.add_system(show_connection)
.add_system(show_last_message)
.add_startup_system(setup)
.add_plugins(MidiInputPlugin)
.add_systems(Startup, setup)
.add_systems(
Update,
(
refresh_ports,
connect,
disconnect,
show_ports,
show_connection,
show_last_message,
),
)
.run();
}

Expand Down Expand Up @@ -96,7 +101,7 @@ fn show_last_message(
mut midi_data: EventReader<MidiData>,
mut instructions: Query<&mut Text, With<Instructions>>,
) {
for data in midi_data.iter() {
for data in midi_data.read() {
let text_section = &mut instructions.single_mut().sections[3];
text_section.value = format!(
"Last Message: {} - {:?}",
Expand Down
21 changes: 13 additions & 8 deletions examples/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,19 @@ fn main() {
.insert_resource(MidiOutputSettings {
port_name: "output",
})
.add_plugin(MidiOutputPlugin)
.add_system(refresh_ports)
.add_system(connect)
.add_system(disconnect)
.add_system(play_notes)
.add_system(show_ports)
.add_system(show_connection)
.add_startup_system(setup)
.add_plugins(MidiOutputPlugin)
.add_systems(
Update,
(
refresh_ports,
connect,
disconnect,
play_notes,
show_ports,
show_connection,
),
)
.add_systems(Startup, setup)
.run();
}

Expand Down
88 changes: 36 additions & 52 deletions examples/piano.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ use bevy::{
prelude::*,
};
use bevy_midi::prelude::*;
use bevy_mod_picking::{
DefaultPickingPlugins, HoverEvent, PickableBundle, PickingCameraBundle, PickingEvent,
SelectionEvent,
};
use bevy_mod_picking::prelude::*;

fn main() {
App::new()
Expand All @@ -21,17 +18,21 @@ fn main() {
filter: "bevy_midi=debug".to_string(),
}))
.add_plugins(DefaultPickingPlugins)
.add_plugin(MidiInputPlugin)
.add_plugins(MidiInputPlugin)
.init_resource::<MidiInputSettings>()
.add_plugin(MidiOutputPlugin)
.add_plugins(MidiOutputPlugin)
.init_resource::<MidiOutputSettings>()
.add_startup_system(setup)
.add_system(handle_midi_input)
.add_system(connect_to_first_input_port)
.add_system(connect_to_first_output_port)
.add_system(print_events.in_base_set(CoreSet::PostUpdate))
.add_system(display_press)
.add_system(display_release)
.add_systems(Startup, setup)
.add_systems(
Update,
(
handle_midi_input,
connect_to_first_input_port,
connect_to_first_output_port,
display_press,
display_release,
),
)
.run();
}

Expand All @@ -41,52 +42,29 @@ struct Key {
y_reset: f32,
}

pub fn print_events(
mut events: EventReader<PickingEvent>,
mut commands: Commands,
mouse_button_input: Res<Input<MouseButton>>,
) {
for event in events.iter() {
let entity = match event {
PickingEvent::Selection(SelectionEvent::JustSelected(e)) => e,
PickingEvent::Selection(SelectionEvent::JustDeselected(e)) => e,
PickingEvent::Hover(HoverEvent::JustEntered(e)) => e,
PickingEvent::Hover(HoverEvent::JustLeft(e)) => e,
PickingEvent::Clicked(e) => e,
};

if mouse_button_input.pressed(MouseButton::Left) {
commands.entity(*entity).insert(PressedKey);
} else {
commands.entity(*entity).remove::<PressedKey>();
}
}
}

#[derive(Component)]
struct PressedKey;

#[rustfmt::skip]
fn setup(
mut commands: Commands,
mut cmds: Commands,
mut materials: ResMut<Assets<StandardMaterial>>,
asset_server: Res<AssetServer>,
) {
let mid = -6.3;

// light
commands.spawn(PointLightBundle {
cmds.spawn(PointLightBundle {
transform: Transform::from_xyz(0.0, 6.0, mid),
..Default::default()
});

//Camera
commands.spawn((
cmds.spawn((
Camera3dBundle {
transform: Transform::from_xyz(8., 5., mid).looking_at(Vec3::new(0., 0., mid), Vec3::Y),
..Default::default()
},
PickingCameraBundle::default(),
));

let pos: Vec3 = Vec3::new(0., 0., 0.);
Expand All @@ -102,18 +80,18 @@ fn setup(
let pos_black = pos + Vec3::new(0., 0.06, 0.);

for i in 0..8 {
spawn_note(&mut commands, &w_mat, 0.00, pos, &mut white_key_0, i, "C");
spawn_note(&mut commands, &b_mat, 0.15, pos_black, &mut black_key, i, "C#");
spawn_note(&mut commands, &w_mat, 0.27, pos, &mut white_key_1, i, "D");
spawn_note(&mut commands, &b_mat, 0.39, pos_black, &mut black_key, i, "D#");
spawn_note(&mut commands, &w_mat, 0.54, pos, &mut white_key_2, i, "E");
spawn_note(&mut commands, &w_mat, 0.69, pos, &mut white_key_0, i, "F");
spawn_note(&mut commands, &b_mat, 0.85, pos_black, &mut black_key, i, "F#");
spawn_note(&mut commands, &w_mat, 0.96, pos, &mut white_key_1, i, "G");
spawn_note(&mut commands, &b_mat, 1.08, pos_black, &mut black_key, i, "G#");
spawn_note(&mut commands, &w_mat, 1.19, pos, &mut white_key_1, i, "A");
spawn_note(&mut commands, &b_mat, 1.31, pos_black, &mut black_key, i, "A#");
spawn_note(&mut commands, &w_mat, 1.46, pos, &mut white_key_2, i, "B");
spawn_note(&mut cmds, &w_mat, 0.00, pos, &mut white_key_0, i, "C");
spawn_note(&mut cmds, &b_mat, 0.15, pos_black, &mut black_key, i, "C#/Db");
spawn_note(&mut cmds, &w_mat, 0.27, pos, &mut white_key_1, i, "D");
spawn_note(&mut cmds, &b_mat, 0.39, pos_black, &mut black_key, i, "D#/Eb");
spawn_note(&mut cmds, &w_mat, 0.54, pos, &mut white_key_2, i, "E");
spawn_note(&mut cmds, &w_mat, 0.69, pos, &mut white_key_0, i, "F");
spawn_note(&mut cmds, &b_mat, 0.85, pos_black, &mut black_key, i, "F#/Gb");
spawn_note(&mut cmds, &w_mat, 0.96, pos, &mut white_key_1, i, "G");
spawn_note(&mut cmds, &b_mat, 1.08, pos_black, &mut black_key, i, "G#/Ab");
spawn_note(&mut cmds, &w_mat, 1.19, pos, &mut white_key_1, i, "A");
spawn_note(&mut cmds, &b_mat, 1.31, pos_black, &mut black_key, i, "A#/Bb");
spawn_note(&mut cmds, &w_mat, 1.46, pos, &mut white_key_2, i, "B");
}
}

Expand Down Expand Up @@ -142,6 +120,12 @@ fn spawn_note(
y_reset: pos.y,
},
PickableBundle::default(),
On::<Pointer<Down>>::target_commands_mut(|_click, entity_commands| {
entity_commands.insert(PressedKey);
}),
On::<Pointer<Up>>::target_commands_mut(|_click, entity_commands| {
entity_commands.remove::<PressedKey>();
}),
));
}

Expand All @@ -162,7 +146,7 @@ fn handle_midi_input(
mut midi_events: EventReader<MidiData>,
query: Query<(Entity, &Key)>,
) {
for data in midi_events.iter() {
for data in midi_events.read() {
let [_, index, _value] = data.message.msg;
let off = index % 12;
let oct = index.overflowing_div(12).0;
Expand Down
11 changes: 7 additions & 4 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ impl Plugin for MidiInputPlugin {
.init_resource::<MidiInputConnection>()
.add_event::<MidiInputError>()
.add_event::<MidiData>()
.add_startup_system(setup)
.add_system(reply.in_base_set(CoreSet::PreUpdate))
.add_system(debug);
.add_systems(Startup, setup)
.add_systems(PreUpdate, reply)
.add_systems(Update, debug);
}
}

Expand Down Expand Up @@ -113,6 +113,8 @@ pub struct MidiData {
pub message: MidiMessage,
}

impl bevy::prelude::Event for MidiData {}

/// The [`Error`] type for midi input operations, accessible as an [`Event`](bevy::ecs::event::Event).
#[derive(Clone, Debug)]
pub enum MidiInputError {
Expand All @@ -121,6 +123,7 @@ pub enum MidiInputError {
}

impl Error for MidiInputError {}
impl Event for MidiInputError {}
impl Display for MidiInputError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
match self {
Expand Down Expand Up @@ -339,7 +342,7 @@ fn get_available_ports(input: &midir::MidiInput) -> Reply {

// A system which debug prints note events
fn debug(mut midi: EventReader<MidiData>) {
for data in midi.iter() {
for data in midi.read() {
let pitch = data.message.msg[1];
let octave = pitch / 12;
let key = KEY_RANGE[pitch as usize % 12];
Expand Down
Loading
Loading