| 1 |
|
| 2 |
|
| 3 |
use bevy::{ |
| 4 |
asset::RenderAssetUsages, image::ImageLoaderSettings, |
| 5 |
input::common_conditions::input_just_pressed, prelude::*, |
| 6 |
}; |
| 7 |
|
| 8 |
fn main() { |
| 9 |
App::new() |
| 10 |
.add_plugins(DefaultPlugins) |
| 11 |
.add_systems(Startup, (setup, spawn_text)) |
| 12 |
.add_systems( |
| 13 |
Update, |
| 14 |
alter_handle.run_if(input_just_pressed(KeyCode::Space)), |
| 15 |
) |
| 16 |
.add_systems( |
| 17 |
Update, |
| 18 |
alter_asset.run_if(input_just_pressed(KeyCode::Enter)), |
| 19 |
) |
| 20 |
.run(); |
| 21 |
} |
| 22 |
|
| 23 |
#[derive(Component, Debug)] |
| 24 |
enum Bird { |
| 25 |
Normal, |
| 26 |
Logo, |
| 27 |
} |
| 28 |
|
| 29 |
impl Bird { |
| 30 |
fn get_texture_path(&self) -> String { |
| 31 |
match self { |
| 32 |
Bird::Normal => "branding/bevy_bird_dark.png".into(), |
| 33 |
Bird::Logo => "branding/bevy_logo_dark.png".into(), |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
fn set_next_variant(&mut self) { |
| 38 |
*self = match self { |
| 39 |
Bird::Normal => Bird::Logo, |
| 40 |
Bird::Logo => Bird::Normal, |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
#[derive(Component, Debug)] |
| 46 |
struct Left; |
| 47 |
|
| 48 |
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) { |
| 49 |
let bird_left = Bird::Normal; |
| 50 |
let bird_right = Bird::Normal; |
| 51 |
commands.spawn(Camera2d); |
| 52 |
|
| 53 |
let texture_left = asset_server |
| 54 |
.load_builder() |
| 55 |
.with_settings( |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|settings: &mut ImageLoaderSettings| settings.asset_usage = RenderAssetUsages::all(), |
| 71 |
) |
| 72 |
.load(bird_left.get_texture_path()); |
| 73 |
|
| 74 |
commands.spawn(( |
| 75 |
Name::new("Bird Left"), |
| 76 |
|
| 77 |
|
| 78 |
Left, |
| 79 |
Sprite::from_image(texture_left), |
| 80 |
Transform::from_xyz(-200.0, 0.0, 0.0), |
| 81 |
bird_left, |
| 82 |
)); |
| 83 |
|
| 84 |
commands.spawn(( |
| 85 |
Name::new("Bird Right"), |
| 86 |
|
| 87 |
Sprite::from_image(asset_server.load(bird_right.get_texture_path())), |
| 88 |
Transform::from_xyz(200.0, 0.0, 0.0), |
| 89 |
bird_right, |
| 90 |
)); |
| 91 |
} |
| 92 |
|
| 93 |
fn spawn_text(mut commands: Commands) { |
| 94 |
commands.spawn(( |
| 95 |
Name::new("Instructions"), |
| 96 |
Text::new( |
| 97 |
"Space: swap the right sprite's image handle\n\ |
| 98 |
Return: modify the image Asset of the left sprite, affecting all uses of it", |
| 99 |
), |
| 100 |
Node { |
| 101 |
position_type: PositionType::Absolute, |
| 102 |
top: px(12), |
| 103 |
left: px(12), |
| 104 |
..default() |
| 105 |
}, |
| 106 |
)); |
| 107 |
} |
| 108 |
|
| 109 |
fn alter_handle( |
| 110 |
asset_server: Res<AssetServer>, |
| 111 |
right_bird: Single<(&mut Bird, &mut Sprite), Without<Left>>, |
| 112 |
) { |
| 113 |
|
| 114 |
|
| 115 |
let (mut bird, mut sprite) = right_bird.into_inner(); |
| 116 |
|
| 117 |
|
| 118 |
bird.set_next_variant(); |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
sprite.image = asset_server.load(bird.get_texture_path()); |
| 124 |
} |
| 125 |
|
| 126 |
fn alter_asset(mut images: ResMut<Assets<Image>>, left_bird: Single<&Sprite, With<Left>>) { |
| 127 |
|
| 128 |
let Some(mut image) = images.get_mut(&left_bird.image) else { |
| 129 |
return; |
| 130 |
}; |
| 131 |
|
| 132 |
for pixel in image.data.as_mut().unwrap() { |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
*pixel = 255 - *pixel; |
| 138 |
} |
| 139 |
} |