Skip to content
Snippets Groups Projects
Commit 6f5e97d0 authored by sam edelsten's avatar sam edelsten
Browse files

add image background example

parent 95c5912c
No related branches found
No related tags found
No related merge requests found
assets/img/bevy_logo_light.png

14.8 KiB

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();
}
......@@ -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 {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment