-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiano.rs
107 lines (91 loc) · 3.09 KB
/
piano.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use bevy::prelude::*;
use bevy_midix::prelude::*;
use crate::ui::HoverNode;
pub fn plugin(app: &mut App) {
app.add_systems(Startup, spawn_piano);
}
#[derive(Component)]
struct Piano;
fn spawn_piano(mut commands: Commands) {
commands
.spawn((
Piano,
Node {
// fill the entire window
width: Val::Percent(100.),
height: Val::Px(100.),
row_gap: Val::Px(3.),
flex_direction: FlexDirection::Row,
position_type: PositionType::Relative,
..Default::default()
},
BackgroundColor(Color::linear_rgb(1., 0., 0.)),
))
.with_children(|parent| {
for key in Key::all() {
let background_color = if key.is_sharp() {
Color::BLACK
} else {
Color::WHITE
};
parent
.spawn((
key,
Node {
width: Val::Px(1000.),
height: Val::Percent(100.),
..Default::default()
},
Outline {
width: Val::Px(2.),
offset: Val::Px(0.),
color: Color::BLACK,
},
BorderColor(Color::BLACK),
BackgroundColor(background_color),
))
.observe(on_mouse_down)
.observe(on_mouse_up)
.observe(on_mouse_enter);
}
});
}
fn on_mouse_enter(
trigger: Trigger<Pointer<Over>>,
keys: Query<&Key<'static>>,
mut hover_node: Query<&mut Text, With<HoverNode>>,
) {
let key = keys.get(trigger.entity()).unwrap();
let mut hover_node = hover_node.get_single_mut().unwrap();
hover_node.0 = format!("Last Hovered Key: {}", key);
}
fn on_mouse_down(
trigger: Trigger<Pointer<Down>>,
mut keys: Query<(&Key<'static>, &mut BackgroundColor)>,
output: Res<MidiOutput>,
) {
let (clicked_key, mut bg) = keys.get_mut(trigger.entity()).unwrap();
//then send to a virtual audio out the message of a key press with some velocity
bg.0 = Color::linear_rgb(1.0, 1.0, 0.);
let event = VoiceEvent::note_on(clicked_key.clone(), Velocity::max())
.send_to_channel(Channel::new(1).unwrap());
output.send(event);
println!("clicked {}", clicked_key);
}
fn on_mouse_up(
trigger: Trigger<Pointer<Up>>,
mut keys: Query<(&Key<'static>, &mut BackgroundColor)>,
output: Res<MidiOutput>,
) {
let triggered_entity = trigger.entity();
let (clicked_key, mut bg) = keys.get_mut(triggered_entity).unwrap();
if clicked_key.is_sharp() {
bg.0 = Color::BLACK;
} else {
bg.0 = Color::WHITE;
};
let event = VoiceEvent::note_on(clicked_key.clone(), Velocity::zero())
.send_to_channel(Channel::new(1).unwrap());
output.send(event);
println!("unclicked {}", clicked_key);
}