|
| 1 | +use bevy::prelude::*; |
| 2 | +use bevy_generative::terrain::{Terrain, TerrainBundle, TerrainPlugin}; |
| 3 | + |
| 4 | +fn main() { |
| 5 | + App::new() |
| 6 | + .add_plugins(DefaultPlugins) |
| 7 | + .add_plugins(TerrainPlugin) |
| 8 | + .add_systems(Startup, setup) |
| 9 | + .add_systems(Update, (button_appearance, export_button)) |
| 10 | + .run(); |
| 11 | +} |
| 12 | + |
| 13 | +fn setup(mut commands: Commands) { |
| 14 | + commands.spawn(PointLightBundle { |
| 15 | + transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y), |
| 16 | + ..default() |
| 17 | + }); |
| 18 | + commands.spawn(Camera3dBundle { |
| 19 | + transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y), |
| 20 | + ..default() |
| 21 | + }); |
| 22 | + commands.spawn(TerrainBundle::default()); |
| 23 | + |
| 24 | + commands |
| 25 | + .spawn(ButtonBundle { |
| 26 | + style: Style { |
| 27 | + padding: UiRect::all(Val::Px(12.)), |
| 28 | + justify_content: JustifyContent::Center, |
| 29 | + align_items: AlignItems::Center, |
| 30 | + margin: UiRect::all(Val::Px(12.)), |
| 31 | + ..default() |
| 32 | + }, |
| 33 | + border_radius: BorderRadius::all(Val::Px(5.)), |
| 34 | + background_color: NORMAL_BUTTON.into(), |
| 35 | + ..default() |
| 36 | + }) |
| 37 | + .with_children(|parent| { |
| 38 | + parent.spawn(TextBundle::from_section( |
| 39 | + "Export", |
| 40 | + TextStyle { |
| 41 | + font_size: 30.0, |
| 42 | + color: BUTTON_TEXT.into(), |
| 43 | + ..default() |
| 44 | + }, |
| 45 | + )); |
| 46 | + }); |
| 47 | +} |
| 48 | + |
| 49 | +fn export_button( |
| 50 | + interaction_query: Query<&Interaction, (Changed<Interaction>, With<Button>)>, |
| 51 | + mut terrain_query: Query<&mut Terrain>, |
| 52 | +) { |
| 53 | + if interaction_query.iter().any(|i| *i == Interaction::Pressed) { |
| 54 | + for mut terrain in &mut terrain_query { |
| 55 | + terrain.export = true; |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +const NORMAL_BUTTON: Srgba = bevy::color::palettes::tailwind::BLUE_500; |
| 61 | +const HOVERED_BUTTON: Srgba = bevy::color::palettes::tailwind::BLUE_600; |
| 62 | +const PRESSED_BUTTON: Srgba = bevy::color::palettes::tailwind::BLUE_700; |
| 63 | +const BUTTON_TEXT: Srgba = bevy::color::palettes::tailwind::BLUE_50; |
| 64 | + |
| 65 | +fn button_appearance( |
| 66 | + mut interaction_query: Query< |
| 67 | + (&Interaction, &mut BackgroundColor), |
| 68 | + (Changed<Interaction>, With<Button>), |
| 69 | + >, |
| 70 | +) { |
| 71 | + for (interaction, mut color) in &mut interaction_query { |
| 72 | + *color = match *interaction { |
| 73 | + Interaction::Pressed => PRESSED_BUTTON.into(), |
| 74 | + Interaction::Hovered => HOVERED_BUTTON.into(), |
| 75 | + Interaction::None => NORMAL_BUTTON.into(), |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments