Skip to content

Commit adc30d9

Browse files
committed
refs #79, more fix to prevent panic
1 parent ee01050 commit adc30d9

30 files changed

+120
-57
lines changed

crates/notation_bevy/src/app/app.rs

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,14 @@ fn setup_camera(mut commands: Commands) {
134134
}
135135

136136
fn load_tab(
137+
mut commands: Commands,
138+
time: Res<Time>,
137139
mut state: ResMut<NotationAppState>,
140+
mut theme: ResMut<NotationTheme>,
138141
entities: Query<Entity, With<GlobalTransform>>,
139142
assets: ResMut<Assets<TabAsset>>,
140143
mut evts: EventWriter<AddTabEvent>,
144+
viewer_query: Query<(Entity, &Arc<NotationViewer>), With<Arc<NotationViewer>>>,
141145
) {
142146
if state.window_width > 0.0 && state.window_height > 0.0 && state.tab.is_none() && state.parse_error.is_none() {
143147
let mut count = 0;
@@ -146,13 +150,34 @@ fn load_tab(
146150
}
147151
//A bit hacky to make sure despawning finished, otherwise might got panic with "Entity not exist"
148152
if count > 1 {
149-
println!("Waiting for entities to be despawned: {}", count);
153+
if state._despawn_delay_seconds > 0.0 {
154+
state._despawn_delay_seconds -= time.delta_seconds();
155+
println!("load_tab(): Waiting to despawn: {} -> {}", count, state._despawn_delay_seconds);
156+
return;
157+
}
158+
let mut despawn_count = 0;
159+
for (entity, _viewer) in viewer_query.iter() {
160+
commands.entity(entity).despawn_recursive();
161+
despawn_count += 1;
162+
}
163+
if despawn_count > 0 {
164+
println!("load_tab(): Despawning viewers: {} {}", despawn_count, count);
165+
} else {
166+
println!("load_tab(): Waiting for entities to be despawned: {}", count);
167+
}
168+
return;
169+
}
170+
if state._load_tab_delay_seconds > 0.0 {
171+
state._load_tab_delay_seconds -= time.delta_seconds();
172+
println!("load_tab(): Waiting to Load tab: -> {}", state._load_tab_delay_seconds);
150173
return;
151174
}
175+
println!("\nload_tab(): Loading: {}", state.tab_path);
152176
if let Some(asset) = assets.get(&state.tab_asset) {
153177
match Tab::try_parse_arc(asset.tab.clone()) {
154178
Ok(tab) => {
155179
state.tab = Some(tab.clone());
180+
theme.loaded = true;
156181
evts.send(AddTabEvent(tab));
157182
}
158183
Err(err) => {
@@ -165,17 +190,19 @@ fn load_tab(
165190
}
166191

167192
fn handle_keyboard_inputs(
168-
mut commands: Commands,
169193
keyboard_input: Res<Input<KeyCode>>,
170-
mut state: ResMut<NotationAppState>,
194+
mut app_state: ResMut<NotationAppState>,
171195
mut settings: ResMut<NotationSettings>,
196+
mut theme: ResMut<NotationTheme>,
172197
mut midi_state: ResMut<MidiState>,
173198
mut play_control_evts: EventWriter<PlayControlEvent>,
174199
mut window_resized_evts: EventWriter<WindowResizedEvent>,
175-
viewer_query: Query<(Entity, &Arc<NotationViewer>), With<Arc<NotationViewer>>>,
176200
) {
201+
if app_state.tab.is_none() {
202+
return;
203+
}
177204
if keyboard_input.just_released(KeyCode::LControl) {
178-
state.hide_control = !state.hide_control;
205+
app_state.hide_control = !app_state.hide_control;
179206
if !ControlView::HUD_MODE {
180207
window_resized_evts.send(WindowResizedEvent());
181208
}
@@ -184,19 +211,23 @@ fn handle_keyboard_inputs(
184211
} else if keyboard_input.just_released(KeyCode::Return) {
185212
crate::viewer::control::ControlView::play_or_stop(&mut midi_state, &mut play_control_evts);
186213
} else if keyboard_input.just_released(KeyCode::Backslash) {
187-
crate::viewer::control::ControlView::toggle_layout_mode(&mut commands, &mut state, &mut settings, &viewer_query);
214+
crate::viewer::control::ControlView::toggle_layout_mode(&mut app_state, &mut settings, &mut theme);
188215
}
189216
}
190217

191218
fn handle_mouse_inputs(
192219
windows: Res<Windows>,
193220
mouse_input: Res<Input<MouseButton>>,
221+
app_state: Res<NotationAppState>,
194222
settings: Res<NotationSettings>,
195223
mut mouse_motion_events: EventReader<MouseMotion>,
196224
mut mouse_wheel_input: EventReader<bevy::input::mouse::MouseWheel>,
197225
mut mouse_clicked: EventWriter<MouseClickedEvent>,
198226
mut mouse_dragged: EventWriter<MouseDraggedEvent>,
199227
) {
228+
if app_state.tab.is_none() {
229+
return;
230+
}
200231
if mouse_input.just_released(MouseButton::Left) {
201232
windows
202233
.get_primary()
@@ -235,6 +266,9 @@ fn handle_touch_inputs(
235266
mut mouse_clicked: EventWriter<MouseClickedEvent>,
236267
//mut mouse_dragged: EventWriter<MouseDraggedEvent>,
237268
) {
269+
if app_state.tab.is_none() {
270+
return;
271+
}
238272
for (_index, finger) in touch_input.iter().enumerate() {
239273
if touch_input.just_pressed(finger.id()) {
240274
windows
@@ -288,6 +322,9 @@ fn on_window_resized(
288322
mut app_state: ResMut<NotationAppState>,
289323
mut window_resized_evts: EventWriter<WindowResizedEvent>,
290324
) {
325+
if app_state.tab.is_none() {
326+
return;
327+
}
291328
for evt in evts.iter() {
292329
if evt.width as usize != window.width as usize
293330
|| evt.height as usize != window.height as usize

crates/notation_bevy/src/app/app_state.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::sync::Arc;
22

33
use crate::prelude::*;
44
use bevy::prelude::*;
5-
use bevy::utils::Uuid;
65
use notation_model::prelude::*;
76

87
pub struct TabPathes(pub Vec<String>);
@@ -15,9 +14,10 @@ pub struct NotationAppState {
1514
pub tab_asset: Handle<TabAsset>,
1615
pub tab: Option<Arc<Tab>>,
1716
pub hide_control: bool,
18-
pub viewer_uuid: Uuid,
1917
pub parse_error: Option<ParseError>,
2018
pub debug_str: Option<String>,
19+
pub _despawn_delay_seconds: f32,
20+
pub _load_tab_delay_seconds: f32,
2121
}
2222

2323
impl NotationAppState {
@@ -31,16 +31,22 @@ impl NotationAppState {
3131
tab_asset,
3232
tab: None,
3333
hide_control: true,
34-
viewer_uuid: Uuid::new_v4(),
3534
parse_error: None,
3635
debug_str: None,
36+
_despawn_delay_seconds: 0.0,
37+
_load_tab_delay_seconds: 0.0,
3738
}
3839
}
3940
pub fn change_tab(&mut self, asset_server: &AssetServer, tab_path: String) {
4041
self.tab_path = tab_path;
4142
self.tab_asset = asset_server.load(self.tab_path.as_str());
42-
self.tab = None;
4343
self.parse_error = None;
44+
self.reset_tab()
45+
}
46+
pub fn reset_tab(&mut self) {
47+
self.tab = None;
48+
self._despawn_delay_seconds = 0.1;
49+
self._load_tab_delay_seconds = 0.2;
4450
}
4551
pub fn convert_pos(&self, pos: Vec2) -> Vec2 {
4652
Vec2::new(

crates/notation_bevy/src/bar/bar_view.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ impl BarView {
4141
mut sep_query: Query<(Entity, &mut BarSeparatorData)>,
4242
mut beat_query: Query<(Entity, &mut BarBeatData)>,
4343
) {
44+
if !theme.loaded { return; }
4445
let engine = NotationLayout::new(&theme, &state, &settings);
4546
let mut bars = Vec::new();
4647
for evt in evts.iter() {
@@ -77,6 +78,7 @@ impl BarView {
7778
mut evts: EventReader<BarViewDoLayoutEvent>,
7879
mut text_query: Query<(&Parent, &mut Transform), With<Text>>,
7980
) {
81+
if !theme.loaded { return; }
8082
if !settings.hide_bar_number {
8183
for evt in evts.iter() {
8284
for (parent, mut transform) in text_query.iter_mut() {

crates/notation_bevy/src/chord/chord_view.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ impl ChordView {
4646
mut dot_query: Query<(Entity, &mut IntervalDotData)>,
4747
mut text_query: Query<&mut Transform, With<Text>>,
4848
) {
49+
if !theme.loaded { return; }
4950
for (_entity, _view, layout, children) in query.iter() {
5051
let radius = layout.size.width * theme.sizes.chord.diagram_factor;
5152
for child in children.iter() {
@@ -106,6 +107,7 @@ impl ChordView {
106107
>,
107108
mut diagram_query: Query<(Entity, &mut ChordDiagramData)>,
108109
) {
110+
if !theme.loaded { return; }
109111
for (_entity, playing, _view, children) in query.iter_mut() {
110112
for child in children.iter() {
111113
if let Ok((diagram_entity, mut diagram_data)) = diagram_query.get_mut(*child) {

crates/notation_bevy/src/entry/entry_plugin.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ fn on_tab_bars_resized(
150150
mut shape_diagram_6_query: Query<(Entity, &mut ShapeDiagramData6), With<ShapeDiagramData6>>,
151151
mut shape_diagram_4_query: Query<(Entity, &mut ShapeDiagramData4), With<ShapeDiagramData4>>,
152152
) {
153+
if !theme.loaded { return; }
153154
for evt in evts.iter() {
154155
let bars = &evt.0;
155156
for (entity, mut data) in tone_note_query.iter_mut() {

crates/notation_bevy/src/guitar/guitar_view.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ impl GuitarView {
134134
mut capo_query: Query<(&Parent, Entity, &mut GuitarCapoData), With<GuitarCapoData>>,
135135
mut finger_query: Query<(&Parent, Entity, &mut FretFingerData), With<FretFingerData>>,
136136
) {
137+
if !theme.loaded { return; }
137138
for (entity, _view, layout) in query.iter() {
138139
let guitar_height = layout.size.width * theme.guitar.image_size.1 / theme.guitar.image_size.0;
139140
let guitar_size = LayoutSize::new(layout.size.width, guitar_height);
@@ -178,6 +179,7 @@ impl GuitarView {
178179
if Self::CHECKING_FRETS {
179180
return;
180181
}
182+
if !theme.loaded { return; }
181183
let mut current_entry_pick = None;
182184
let mut string_states = [None; 6];
183185
let mut hit_strings = [(false, Duration::Zero); 6];
@@ -247,6 +249,7 @@ impl GuitarView {
247249
if Self::CHECKING_FRETS {
248250
return;
249251
}
252+
if !theme.loaded { return; }
250253
let mut current_shape = None;
251254
for (entry, shape, playing) in query.iter() {
252255
if playing.value.is_current() {
@@ -326,6 +329,7 @@ impl GuitarView {
326329
capo_query: Query<&GuitarCapoData, Changed<GuitarCapoData>>,
327330
mut guitar_view_query: Query<&mut Transform, With<Arc<GuitarView>>>,
328331
) {
332+
if !theme.loaded { return; }
329333
if settings.override_guitar_y.is_some() {
330334
return;
331335
}

crates/notation_bevy/src/mini/mini_bar.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ impl ShapeOp<NotationTheme, OutlineRectangle> for MiniBarData {
5959
theme
6060
.colors
6161
.mini_map
62-
.bar_outline
63-
.of_state(&self.value.playing_state)
62+
.bar_outline_current
6463
} else {
6564
theme.colors.of_section(self.bar_props.section_ordinal)
6665
};

crates/notation_bevy/src/play/play_button.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ impl PlayButton {
175175
query: LayoutChangedWithChildrenQuery<PlayButton>,
176176
mut shape_query: Query<(Entity, &mut PlayButtonShape)>,
177177
) {
178+
if !theme.loaded { return; }
178179
for (_entity, _view, layout, children) in query.iter() {
179180
for child in children.iter() {
180181
if let Ok((entity, mut data)) = shape_query.get_mut(*child) {

crates/notation_bevy/src/play/play_panel.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ impl PlayPanel {
9898
mut layout_query: LayoutQuery,
9999
cell_query: ViewQuery<PlayButton>,
100100
) {
101+
if !theme.loaded { return; }
101102
let engine = NotationLayout::new(&theme, &state, &settings);
102103
for evt in evts.iter() {
103104
evt.view.do_layout(
@@ -116,6 +117,7 @@ impl PlayPanel {
116117
mut evts: EventReader<PlayControlEvent>,
117118
mut shape_query: Query<(Entity, &mut PlayButtonShape)>,
118119
) {
120+
if !theme.loaded { return; }
119121
for evt in evts.iter() {
120122
match evt {
121123
PlayControlEvent::OnTick {position: _, tick_result} => {

crates/notation_bevy/src/play/play_plugin.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ fn on_tab_resized(
114114
&Arc<GridData>,
115115
)>,
116116
) {
117+
if !theme.loaded { return; }
117118
let mut bars = None;
118119
for evt in evts.iter() {
119120
bars = Some(&evt.0);
@@ -167,6 +168,7 @@ fn on_bar_playing_changed(
167168
&Arc<GridData>,
168169
)>,
169170
) {
171+
if !theme.loaded { return; }
170172
for (_entity, playing, _view, layout) in query.iter_mut() {
171173
if playing.value == PlayingState::Current {
172174
update_indicators(
@@ -204,6 +206,7 @@ fn on_tab_play_state_changed(
204206
&Arc<GridData>,
205207
)>,
206208
) {
209+
if !theme.loaded { return; }
207210
for (state_entity, tab_state) in query.iter_mut() {
208211
TabState::clear_play_state_changed(&mut commands, state_entity);
209212
if let Some(pos_data) = PosIndicatorData::update_pos(
@@ -310,6 +313,7 @@ fn on_play_control_evt(
310313
)>,
311314
mut beat_query: Query<(Entity, &mut BarBeatData)>,
312315
) {
316+
if !theme.loaded { return; }
313317
for evt in evts.iter() {
314318
for (state_entity, mut tab_state) in tab_state_query.iter_mut() {
315319
if !tab_state.under_control {

0 commit comments

Comments
 (0)