mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 07:04:33 +00:00
Fix screenshot example (#15094)
# Objective
I noticed some issues in `screenshot` example:
1. Cursor icon won't return from `SystemCursorIcon::Progress` to default
icon, even though screen shot saving is done.
2. Panics when exiting window: ``called `Result::unwrap()` on an `Err`
value:
NoEntities("bevy_ecs::query::state::QueryState<bevy_ecs::entity::Entity,
bevy_ecs::query::filter::With<bevy_window:🪟:Window>>")``
## Solution
1. Caused by cursor updating system not responding to [`CursorIcon`
component
removal](5cfcbf47ed/examples/window/screenshot.rs (L38)
).
I believe it should, so change it to react to
`RemovedComponents<CursorIcon>`. (a suggestion)
2. Use `get_single` for window.
## Testing
- run screenshot example
---------
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
This commit is contained in:
parent
adc2cf7dfe
commit
bafffe1c5f
2 changed files with 18 additions and 4 deletions
|
@ -4,10 +4,11 @@ use bevy_ecs::{
|
|||
change_detection::DetectChanges,
|
||||
component::Component,
|
||||
entity::Entity,
|
||||
observer::Trigger,
|
||||
query::With,
|
||||
reflect::ReflectComponent,
|
||||
system::{Commands, Local, Query, Res},
|
||||
world::Ref,
|
||||
world::{OnRemove, Ref},
|
||||
};
|
||||
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
|
||||
use bevy_utils::{tracing::warn, HashSet};
|
||||
|
@ -27,6 +28,8 @@ impl Plugin for CursorPlugin {
|
|||
app.register_type::<CursorIcon>()
|
||||
.init_resource::<CustomCursorCache>()
|
||||
.add_systems(Last, update_cursors);
|
||||
|
||||
app.observe(on_remove_cursor_icon);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -84,12 +87,12 @@ pub enum CustomCursor {
|
|||
|
||||
pub fn update_cursors(
|
||||
mut commands: Commands,
|
||||
mut windows: Query<(Entity, Ref<CursorIcon>), With<Window>>,
|
||||
windows: Query<(Entity, Ref<CursorIcon>), With<Window>>,
|
||||
cursor_cache: Res<CustomCursorCache>,
|
||||
images: Res<Assets<Image>>,
|
||||
mut queue: Local<HashSet<Entity>>,
|
||||
) {
|
||||
for (entity, cursor) in windows.iter_mut() {
|
||||
for (entity, cursor) in windows.iter() {
|
||||
if !(queue.remove(&entity) || cursor.is_changed()) {
|
||||
continue;
|
||||
}
|
||||
|
@ -161,6 +164,15 @@ pub fn update_cursors(
|
|||
}
|
||||
}
|
||||
|
||||
/// Resets the cursor to the default icon when `CursorIcon` is removed.
|
||||
pub fn on_remove_cursor_icon(trigger: Trigger<OnRemove, CursorIcon>, mut commands: Commands) {
|
||||
commands
|
||||
.entity(trigger.entity())
|
||||
.insert(PendingCursor(Some(CursorSource::System(
|
||||
convert_system_cursor_icon(SystemCursorIcon::Default),
|
||||
))));
|
||||
}
|
||||
|
||||
/// Returns the image data as a `Vec<u8>`.
|
||||
/// Only supports rgba8 and rgba32float formats.
|
||||
fn image_to_rgba_pixels(image: &Image) -> Option<Vec<u8>> {
|
||||
|
|
|
@ -32,7 +32,9 @@ fn screenshot_saving(
|
|||
screenshot_saving: Query<Entity, With<Capturing>>,
|
||||
windows: Query<Entity, With<Window>>,
|
||||
) {
|
||||
let window = windows.single();
|
||||
let Ok(window) = windows.get_single() else {
|
||||
return;
|
||||
};
|
||||
match screenshot_saving.iter().count() {
|
||||
0 => {
|
||||
commands.entity(window).remove::<CursorIcon>();
|
||||
|
|
Loading…
Reference in a new issue