Skip to content

Commit

Permalink
Fix screenshot example (#15094)
Browse files Browse the repository at this point in the history
# 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::Window>>")``

## Solution

1. Caused by cursor updating system not responding to [`CursorIcon`
component
removal](https://github.com/bevyengine/bevy/blob/5cfcbf47ed386515adefe80f99f8e8f7e7f3ce5a/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>
  • Loading branch information
akimakinai and alice-i-cecile authored Sep 9, 2024
1 parent adc2cf7 commit bafffe1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
18 changes: 15 additions & 3 deletions crates/bevy_render/src/view/window/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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>> {
Expand Down
4 changes: 3 additions & 1 deletion examples/window/screenshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>();
Expand Down

0 comments on commit bafffe1

Please sign in to comment.