diff --git a/assets/img/bevy_logo_light.png b/assets/img/bevy_logo_light.png new file mode 100644 index 0000000000000000000000000000000000000000..f144a3a6f114e28e9b68fbed643ccf9c349bb5b0 Binary files /dev/null and b/assets/img/bevy_logo_light.png differ diff --git a/examples/image_background.rs b/examples/image_background.rs new file mode 100644 index 0000000000000000000000000000000000000000..6af3efcfb178b4364ebcc8cd31f2e3cd284f3493 --- /dev/null +++ b/examples/image_background.rs @@ -0,0 +1,44 @@ +use bevy::prelude::*; +use bevy_cosmic_edit::{ + change_active_editor_sprite, change_active_editor_ui, ActiveEditor, CosmicAttrs, + CosmicBackground, CosmicEditPlugin, CosmicEditUiBundle, +}; +use cosmic_text::{Attrs, AttrsOwned}; + +fn setup(mut commands: Commands, asset_server: Res<AssetServer>) { + commands.spawn(Camera2dBundle::default()); + + let bg_image_handle = asset_server.load("img/bevy_logo_light.png"); + + let editor = commands + .spawn(CosmicEditUiBundle { + style: Style { + // Size and position of text box + width: Val::Px(300.), + height: Val::Px(50.), + left: Val::Px(100.), + top: Val::Px(100.), + ..default() + }, + cosmic_attrs: CosmicAttrs(AttrsOwned::new( + Attrs::new().color(cosmic_text::Color::rgb(0, 255, 0)), + )), + background_image: CosmicBackground(Some(bg_image_handle)), + ..default() + }) + .id(); + + commands.insert_resource(ActiveEditor { + entity: Some(editor), + }); +} + +fn main() { + App::new() + .add_plugins(DefaultPlugins) + .add_plugins(CosmicEditPlugin::default()) + .add_systems(Startup, setup) + .add_systems(Update, change_active_editor_ui) + .add_systems(Update, change_active_editor_sprite) + .run(); +} diff --git a/src/lib.rs b/src/lib.rs index 5ea2f779bff4f67f0d3c2ed76324bf8dee948563..a426c42beb3aa9289f19bc9df27a527d65d77a16 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1046,19 +1046,22 @@ fn redraw_buffer_common( let mut pixels = vec![0; width as usize * height as usize * 4]; if let Some(bg_image) = background_image { - let image = images.get(&bg_image).unwrap(); - - let mut dynamic_image = image.clone().try_into_dynamic().unwrap(); - if image.size().x != width || image.size().y != height { - dynamic_image = - dynamic_image.resize_to_fill(width as u32, height as u32, FilterType::Triangle); - } - for (i, (_, _, rgba)) in dynamic_image.pixels().enumerate() { - if let Some(p) = pixels.get_mut(i * 4..(i + 1) * 4) { - p[0] = rgba[0]; - p[1] = rgba[1]; - p[2] = rgba[2]; - p[3] = rgba[3]; + if let Some(image) = images.get(&bg_image) { + let mut dynamic_image = image.clone().try_into_dynamic().unwrap(); + if image.size().x != width || image.size().y != height { + dynamic_image = dynamic_image.resize_to_fill( + width as u32, + height as u32, + FilterType::Triangle, + ); + } + for (i, (_, _, rgba)) in dynamic_image.pixels().enumerate() { + if let Some(p) = pixels.get_mut(i * 4..(i + 1) * 4) { + p[0] = rgba[0]; + p[1] = rgba[1]; + p[2] = rgba[2]; + p[3] = rgba[3]; + } } } } else {