use bevy::prelude::*; use bevy_asset_loader::prelude::*; use bevy_kenney_assets::{ KenneyAssetPlugin, KenneySpriteSheetAsset, }; use bevy_mod_picking::{ prelude::{Listener, On}, DefaultPickingPlugins, }; use woodpecker_ui::prelude::*; fn main() { App::new() .add_plugins(( DefaultPlugins, KenneyAssetPlugin, WoodpeckerUIPlugin::default(), DefaultPickingPlugins, )) .init_state::() .add_loading_state( LoadingState::new(MyStates::AssetLoading) .continue_to_state(MyStates::Next) .load_collection::(), ) .add_systems( OnEnter(MyStates::Next), (startup_ui, setup).chain(), ) .add_systems( Update, ( input.run_if(in_state(MyStates::Next)), on_change_resource.run_if( resource_exists::, ), // .run_if( // resource_changed::, // ), ), ) .run(); } #[derive( Clone, Eq, PartialEq, Debug, Hash, Default, States, )] enum MyStates { #[default] AssetLoading, Next, } #[derive(AssetCollection, Resource)] pub struct ImageAssets { #[asset( path = "generic-items/genericItems_spritesheet_colored.xml" )] pub generic_items: Handle, #[asset(path = "space-shooter-redux/sheet.xml")] pub space_sheet: Handle, } fn setup( mut commands: Commands, spritesheets: Res>, current_sheet: Res, ) { let kenney_sheet = spritesheets.get(¤t_sheet.0).unwrap(); commands.spawn(( SpriteBundle { texture: kenney_sheet.sheet.clone(), ..default() }, TextureAtlas { index: 0, layout: kenney_sheet .texture_atlas_layout .clone(), }, )); } fn on_change_resource( mut query: Query<( &mut Handle, &mut TextureAtlas, )>, spritesheets: Res>, current_sheet: Res, ) { if current_sheet.is_changed() { let kenney_sheet = spritesheets.get(¤t_sheet.0).unwrap(); for (mut sheet, mut atlas) in &mut query { *sheet = kenney_sheet.sheet.clone(); atlas.layout = kenney_sheet.texture_atlas_layout.clone(); atlas.index = 0; } } } fn input( input: Res>, spritesheets: Res>, current_sheet: Res, mut atlas: Query<&mut TextureAtlas>, ) { if input.just_pressed(KeyCode::Space) { spritesheets.get(¤t_sheet.0).unwrap(); let mut atlas = atlas.single_mut(); atlas.index += 1; } } // UI #[derive(Resource)] struct CurrentSheet(Handle); fn startup_ui( mut commands: Commands, mut ui_context: ResMut, asset_server: ResMut, images: Res, ) { commands.spawn(Camera2dBundle::default()); commands.insert_resource(CurrentSheet( images.space_sheet.clone(), )); let root = commands .spawn((WoodpeckerAppBundle { styles: WoodpeckerStyle { padding: Edge::all(10.0), ..default() }, children: WidgetChildren::default() .with_child::(( DropdownBundle { dropdown: Dropdown { current_value: "Space Shooter" .to_string(), list: vec![ "Space Shooter".to_string(), "Generic Items".to_string(), ], ..default() }, ..default() }, On::>::run( |event: Listener< Change, >, images: Res, mut current_sheet: ResMut< CurrentSheet, >| { current_sheet.0 = match event .data .value .as_str() { "Generic Items" => images .generic_items .clone(), "Space Shooter" => images .space_sheet .clone(), _ => unreachable!(), }; }, ), )), ..default() },)) .id(); ui_context.set_root_widget(root); }