Skip to content

Commit 58a2edd

Browse files
committed
show chord color in BarIndicator
1 parent a00b35e commit 58a2edd

File tree

5 files changed

+82
-27
lines changed

5 files changed

+82
-27
lines changed

crates/notation_bevy/src/chord/chord_playing.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ impl ChordPlaying {
2626
query: &mut Query<(Entity, &mut ChordPlaying), With<ChordPlaying>>,
2727
tab_state: &TabState,
2828
new_position: &Position,
29-
) {
29+
) -> usize {
30+
let mut changed = 0;
3031
let is_current = |c: Chord| {
3132
if tab_state.play_control.play_state.is_playing() {
3233
let chord = tab_state
@@ -51,12 +52,15 @@ impl ChordPlaying {
5152
if is_current(chord_playing.value.chord) {
5253
if chord_playing.value.state != PlayingState::Current {
5354
chord_playing.value.state = PlayingState::Current;
55+
changed += 1;
5456
}
5557
} else {
5658
if chord_playing.value.state != PlayingState::Idle {
5759
chord_playing.value.state = PlayingState::Idle;
60+
changed += 1;
5861
}
5962
}
6063
}
64+
changed
6165
}
6266
}

crates/notation_bevy/src/play/bar_indicator.rs

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
1+
use std::sync::Arc;
12
use bevy::prelude::*;
23
use bevy_prototype_lyon::prelude::*;
34
use notation_bevy_utils::prelude::{BevyUtil, LayoutData, StrokeRectangle, ShapeOp};
4-
use notation_model::prelude::TabBarProps;
5+
use notation_model::prelude::{TabBarProps, Chord, Tab, Units};
56

67
use crate::prelude::{NotationTheme};
78

89
#[derive(Clone, Debug)]
910
pub struct BarIndicatorData {
11+
pub tab: Arc<Tab>,
1012
pub bar_props: TabBarProps,
1113
pub bar_layout: LayoutData,
14+
pub chord: Option<Chord>,
1215
}
1316

1417
impl BarIndicatorData {
15-
pub fn new() -> Self {
18+
pub fn new(tab: Arc<Tab>) -> Self {
1619
BarIndicatorData {
20+
tab,
1721
bar_props: TabBarProps::default(),
1822
bar_layout: LayoutData::ZERO,
23+
chord: None,
1924
}
2025
}
2126
}
@@ -29,13 +34,50 @@ impl ShapeOp<NotationTheme, StrokeRectangle> for BarIndicatorData {
2934
let y = self.bar_layout.offset.y + theme.sizes.bar.bar_separator_extra;
3035
Vec3::new(x, y, theme.core.bar_indicator_z)
3136
};
37+
let color = theme
38+
.colors
39+
.of_option_chord(self.chord);
3240
StrokeRectangle {
3341
width: self.bar_layout.size.width,
3442
height: self.bar_layout.size.height + theme.sizes.bar.bar_separator_extra * 2.0,
3543
origin: shapes::RectangleOrigin::TopLeft,
36-
color: theme.colors.bar.bar_indicator,
44+
color, //: theme.colors.bar.bar_indicator,
3745
line_width: theme.sizes.bar.pos_indicator_size,
3846
offset,
3947
}
4048
}
49+
}
50+
51+
impl BarIndicatorData {
52+
fn update_chord(&mut self,
53+
bar_props: TabBarProps,
54+
in_bar_pos: Option<Units>,
55+
) {
56+
self.bar_props = bar_props;
57+
self.chord = self.tab.get_bar_of_ordinal(bar_props.bar_ordinal).and_then(|x| x.get_chord(in_bar_pos));
58+
}
59+
pub fn update_data(&mut self,
60+
commands: &mut Commands,
61+
theme: &NotationTheme,
62+
entity: Entity,
63+
bar_props: TabBarProps,
64+
bar_layout: LayoutData,
65+
in_bar_pos: Option<Units>,
66+
) {
67+
self.update_chord(bar_props, in_bar_pos);
68+
self.bar_layout = bar_layout;
69+
self.update(commands, theme, entity);
70+
}
71+
pub fn update_pos(
72+
commands: &mut Commands,
73+
theme: &NotationTheme,
74+
bar_indicator_query: &mut Query<(Entity, &mut BarIndicatorData), With<BarIndicatorData>>,
75+
bar_props: TabBarProps,
76+
in_bar_pos: Units,
77+
) {
78+
if let Ok((entity, mut data)) = bar_indicator_query.single_mut() {
79+
data.update_chord(bar_props, Some(in_bar_pos));
80+
data.update(commands, theme, entity);
81+
}
82+
}
4183
}

crates/notation_bevy/src/play/play_plugin.rs

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::sync::Arc;
33
use notation_bevy_utils::prelude::{DoLayoutEvent, GridData, LayoutData, ShapeOp};
44
use notation_midi::prelude::PlayControlEvent;
55
use notation_model::prelude::{
6-
LaneEntry, PlayState, PlayingState, Position, Tab, TabBarProps, TickResult,
6+
LaneEntry, PlayState, PlayingState, Position, Tab, TickResult,
77
};
88

99
use bevy::prelude::*;
@@ -51,9 +51,9 @@ impl PlayPlugin {
5151
commands: &mut Commands,
5252
theme: &NotationTheme,
5353
entity: Entity,
54-
tab: &Tab,
54+
tab: &Arc<Tab>,
5555
) {
56-
let bar_data = BarIndicatorData::new();
56+
let bar_data = BarIndicatorData::new(tab.clone());
5757
bar_data.create(commands, &theme, entity);
5858
let pos_data = PosIndicatorData::new(tab.bar_units());
5959
pos_data.create(commands, &theme, entity);
@@ -73,21 +73,24 @@ fn update_indicators(
7373
&LayoutData,
7474
&Arc<GridData>,
7575
)>,
76-
bar_props: TabBarProps,
76+
bar_playing: &BarPlaying,
7777
bar_layout: LayoutData,
7878
) {
79-
for (entity, mut data) in bar_indicator_query.iter_mut() {
80-
data.bar_props = bar_props;
81-
data.bar_layout = bar_layout;
82-
data.update(commands, &theme, entity);
83-
}
79+
let bar_props = bar_playing.bar_props;
80+
let mut in_bar_pos = None;
8481
for (entity, mut data) in pos_indicator_query.iter_mut() {
8582
data.bar_props = bar_props;
8683
data.bar_layout = bar_layout;
8784
data.update(commands, &theme, entity);
8885
settings
8986
.layout
9087
.focus_bar(commands, theme, tab_bars_query, &data);
88+
in_bar_pos = Some(data.bar_position.in_bar_pos);
89+
}
90+
for (entity, mut data) in bar_indicator_query.iter_mut() {
91+
data.bar_props = bar_props;
92+
data.bar_layout = bar_layout;
93+
data.update_data(commands, theme, entity, bar_props, bar_layout, in_bar_pos);
9194
}
9295
}
9396

@@ -136,7 +139,7 @@ fn on_tab_resized(
136139
&mut bar_indicator_query,
137140
&mut pos_indicator_query,
138141
&mut tab_bars_query,
139-
playing.bar_props,
142+
playing,
140143
layout.clone(),
141144
);
142145
}
@@ -167,7 +170,7 @@ fn on_bar_playing_changed(
167170
&mut bar_indicator_query,
168171
&mut pos_indicator_query,
169172
&mut tab_bars_query,
170-
playing.bar_props,
173+
playing,
171174
layout.clone(),
172175
);
173176
break;
@@ -218,6 +221,7 @@ fn on_tick(
218221
commands: &mut Commands,
219222
theme: &NotationTheme,
220223
settings: &mut NotationSettings,
224+
bar_indicator_query: &mut Query<(Entity, &mut BarIndicatorData), With<BarIndicatorData>>,
221225
pos_indicator_query: &mut Query<(Entity, &mut PosIndicatorData), With<PosIndicatorData>>,
222226
bar_playing_query: &mut Query<(Entity, &mut BarPlaying), With<BarPlaying>>,
223227
entry_playing_query: &mut Query<
@@ -247,25 +251,28 @@ fn on_tick(
247251
if *stopped {
248252
tab_state.set_play_state(commands, state_entity, PlayState::Stopped);
249253
} else {
250-
if let Some(pos_data) =
251-
PosIndicatorData::update_pos(commands, theme, pos_indicator_query, *new_position)
252-
{
253-
if settings.layout.mode == LayoutMode::Line && pos_data.is_synced() {
254-
settings
255-
.layout
256-
.focus_bar(commands, theme, tab_bars_query, &pos_data);
257-
}
258-
}
259254
let playing_bar_ordinal = new_position.bar.bar_ordinal;
260255
BarPlaying::update(bar_playing_query, tab_state, playing_bar_ordinal);
261-
ChordPlaying::update(chord_playing_query, tab_state, new_position);
262256
EntryPlaying::update_with_pos(
263257
entry_playing_query,
264258
tab_state,
265259
new_position,
266260
*end_passed,
267261
*jumped,
268262
);
263+
let chord_changed = ChordPlaying::update(chord_playing_query, tab_state, new_position);
264+
if let Some(pos_data) =
265+
PosIndicatorData::update_pos(commands, theme, pos_indicator_query, *new_position)
266+
{
267+
if settings.layout.mode == LayoutMode::Line && pos_data.is_synced() {
268+
settings
269+
.layout
270+
.focus_bar(commands, theme, tab_bars_query, &pos_data);
271+
}
272+
if chord_changed > 0 {
273+
BarIndicatorData::update_pos(commands, theme, bar_indicator_query, pos_data.bar_props, pos_data.bar_position.in_bar_pos);
274+
}
275+
}
269276
}
270277
}
271278

@@ -275,6 +282,7 @@ fn on_play_control_evt(
275282
mut settings: ResMut<NotationSettings>,
276283
mut evts: EventReader<PlayControlEvent>,
277284
mut tab_state_query: Query<(Entity, &mut TabState)>,
285+
mut bar_indicator_query: Query<(Entity, &mut BarIndicatorData), With<BarIndicatorData>>,
278286
mut pos_indicator_query: Query<(Entity, &mut PosIndicatorData), With<PosIndicatorData>>,
279287
mut bar_playing_query: Query<(Entity, &mut BarPlaying), With<BarPlaying>>,
280288
mut entry_playing_query: Query<
@@ -304,6 +312,7 @@ fn on_play_control_evt(
304312
&mut commands,
305313
&theme,
306314
&mut settings,
315+
&mut bar_indicator_query,
307316
&mut pos_indicator_query,
308317
&mut bar_playing_query,
309318
&mut entry_playing_query,

crates/notation_bevy/src/play/pos_indicator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl ShapeOp<NotationTheme, OutlineRectangle> for PosIndicatorData {
3838
let height = self.bar_layout.size.height + theme.sizes.bar.bar_separator_extra * 2.0;
3939
let color = theme
4040
.colors
41-
.of_section(self.bar_props.section_index);
41+
.of_section(self.bar_props.section_ordinal);
4242
let outline_color = theme.colors.bar.pos_indicator_color;
4343
let outline_width = theme.sizes.bar.pos_indicator_outline;
4444
let offset = if self.bar_layout.size.width <= 0.0 {

crates/notation_bevy/src/tab/tab_bars.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ impl TabBars {
187187
let view_bundle = ViewBundle::from(TabBars::new(tab.clone(), Arc::new(bar_layouts)));
188188
let view = view_bundle.view.clone();
189189
let bars_entity = BevyUtil::spawn_child_bundle(commands, entity, view_bundle);
190-
PlayPlugin::spawn_indicators(commands, theme, bars_entity, &view.tab);
190+
PlayPlugin::spawn_indicators(commands, theme, bars_entity, tab);
191191
let bar_bundles: Vec<(&Arc<TabBar>, &BarLayoutData)> = view
192192
.tab
193193
.bars

0 commit comments

Comments
 (0)