Skip to content

Commit 78d2149

Browse files
rparrettmockersf
andauthored
Fix panics in scene_viewer and audio_control (#16983)
# Objective Fixes #16978 While testing, discovered that the morph weight interface in `scene_viewer` has been broken for a while (panics when loaded model has morph weights), probably since #15591. Fixed that too. While testing, saw example text in morph interface with [wrong padding](https://bevyengine.org/learn/contribute/helping-out/creating-examples/#visual-guidelines). Fixed that too. Left the small font size because there may be a lot of morphs to display, so that seems intentional. ## Solution Use normal queries and bail early ## Testing Morph interface can be tested with ``` cargo run --example scene_viewer assets/models/animated/MorphStressTest.gltf ``` ## Discussion I noticed that this fix is different than what is happening in #16976. Feel free to discard this for an alternative fix. I opened this anyway to document the issue with morph weight display. This is on top of #16966 which is required to test. --------- Co-authored-by: François Mockers <[email protected]> Co-authored-by: François Mockers <[email protected]>
1 parent e8fc279 commit 78d2149

File tree

3 files changed

+47
-17
lines changed

3 files changed

+47
-17
lines changed

examples/audio/audio_control.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,48 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
3434
#[derive(Component)]
3535
struct MyMusic;
3636

37-
fn update_speed(sink: Single<&AudioSink, With<MyMusic>>, time: Res<Time>) {
37+
fn update_speed(music_controller: Query<&AudioSink, With<MyMusic>>, time: Res<Time>) {
38+
let Ok(sink) = music_controller.get_single() else {
39+
return;
40+
};
41+
3842
sink.set_speed((ops::sin(time.elapsed_secs() / 5.0) + 1.0).max(0.1));
3943
}
4044

41-
fn pause(keyboard_input: Res<ButtonInput<KeyCode>>, sink: Single<&AudioSink, With<MyMusic>>) {
45+
fn pause(
46+
keyboard_input: Res<ButtonInput<KeyCode>>,
47+
music_controller: Query<&AudioSink, With<MyMusic>>,
48+
) {
49+
let Ok(sink) = music_controller.get_single() else {
50+
return;
51+
};
52+
4253
if keyboard_input.just_pressed(KeyCode::Space) {
4354
sink.toggle_playback();
4455
}
4556
}
4657

4758
fn mute(
4859
keyboard_input: Res<ButtonInput<KeyCode>>,
49-
mut sink: Single<&mut AudioSink, With<MyMusic>>,
60+
mut music_controller: Query<&mut AudioSink, With<MyMusic>>,
5061
) {
62+
let Ok(mut sink) = music_controller.get_single_mut() else {
63+
return;
64+
};
65+
5166
if keyboard_input.just_pressed(KeyCode::KeyM) {
5267
sink.toggle_mute();
5368
}
5469
}
5570

5671
fn volume(
5772
keyboard_input: Res<ButtonInput<KeyCode>>,
58-
mut sink: Single<&mut AudioSink, With<MyMusic>>,
73+
mut music_controller: Query<&mut AudioSink, With<MyMusic>>,
5974
) {
75+
let Ok(mut sink) = music_controller.get_single_mut() else {
76+
return;
77+
};
78+
6079
if keyboard_input.just_pressed(KeyCode::Equal) {
6180
let current_volume = sink.volume();
6281
sink.set_volume(current_volume + 0.1);

examples/helpers/camera_controller.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,13 @@ fn run_camera_controller(
109109
key_input: Res<ButtonInput<KeyCode>>,
110110
mut toggle_cursor_grab: Local<bool>,
111111
mut mouse_cursor_grab: Local<bool>,
112-
query: Single<(&mut Transform, &mut CameraController), With<Camera>>,
112+
mut query: Query<(&mut Transform, &mut CameraController), With<Camera>>,
113113
) {
114114
let dt = time.delta_secs();
115115

116-
let (mut transform, mut controller) = query.into_inner();
116+
let Ok((mut transform, mut controller)) = query.get_single_mut() else {
117+
return;
118+
};
117119

118120
if !controller.initialized {
119121
let (yaw, pitch, _roll) = transform.rotation.to_euler(EulerRot::YXZ);

examples/tools/scene_viewer/morph_viewer_plugin.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use crate::scene_viewer_plugin::SceneHandle;
1212
use bevy::prelude::*;
1313
use std::fmt;
1414

15+
const FONT_SIZE: f32 = 13.0;
16+
1517
const WEIGHT_PER_SECOND: f32 = 0.8;
1618
const ALL_MODIFIERS: &[KeyCode] = &[KeyCode::ShiftLeft, KeyCode::ControlLeft, KeyCode::AltLeft];
1719
const AVAILABLE_KEYS: [MorphKey; 56] = [
@@ -122,8 +124,8 @@ impl fmt::Display for Target {
122124
}
123125
}
124126
impl Target {
125-
fn text_span(&self, key: &str, style: TextFont) -> (String, TextFont) {
126-
(format!("[{key}] {self}\n"), style)
127+
fn text_span(&self, key: &str, style: TextFont) -> (TextSpan, TextFont) {
128+
(TextSpan::new(format!("[{key}] {self}\n")), style)
127129
}
128130
fn new(
129131
entity_name: Option<&Name>,
@@ -178,13 +180,18 @@ impl MorphKey {
178180
}
179181
fn update_text(
180182
controls: Option<ResMut<WeightsControl>>,
181-
text: Single<Entity, With<Text>>,
183+
texts: Query<Entity, With<Text>>,
182184
morphs: Query<&MorphWeights>,
183185
mut writer: TextUiWriter,
184186
) {
185187
let Some(mut controls) = controls else {
186188
return;
187189
};
190+
191+
let Ok(text) = texts.get_single() else {
192+
return;
193+
};
194+
188195
for (i, target) in controls.weights.iter_mut().enumerate() {
189196
let Ok(weights) = morphs.get(target.entity) else {
190197
continue;
@@ -196,7 +203,8 @@ fn update_text(
196203
target.weight = actual_weight;
197204
}
198205
let key_name = &AVAILABLE_KEYS[i].name;
199-
*writer.text(*text, i + 3) = format!("[{key_name}] {target}\n");
206+
207+
*writer.text(text, i + 3) = format!("[{key_name}] {target}\n");
200208
}
201209
}
202210
fn update_morphs(
@@ -254,12 +262,12 @@ fn detect_morphs(
254262
}
255263
detected.truncate(AVAILABLE_KEYS.len());
256264
let style = TextFont {
257-
font_size: 13.0,
265+
font_size: FONT_SIZE,
258266
..default()
259267
};
260268
let mut spans = vec![
261-
("Morph Target Controls\n".into(), style.clone()),
262-
("---------------\n".into(), style.clone()),
269+
(TextSpan::new("Morph Target Controls\n"), style.clone()),
270+
(TextSpan::new("---------------\n"), style.clone()),
263271
];
264272
let target_to_text =
265273
|(i, target): (usize, &Target)| target.text_span(AVAILABLE_KEYS[i].name, style.clone());
@@ -270,14 +278,15 @@ fn detect_morphs(
270278
Text::default(),
271279
Node {
272280
position_type: PositionType::Absolute,
273-
top: Val::Px(10.0),
274-
left: Val::Px(10.0),
281+
top: Val::Px(12.0),
282+
left: Val::Px(12.0),
275283
..default()
276284
},
277285
))
278286
.with_children(|p| {
279-
p.spawn((TextSpan::new("Morph Target Controls\n"), style.clone()));
280-
p.spawn((TextSpan::new("---------------\n"), style));
287+
for span in spans {
288+
p.spawn(span);
289+
}
281290
});
282291
}
283292

0 commit comments

Comments
 (0)