bevy/examples/ios/src/lib.rs
ickshonpe 9eefd7c022 Remove VerticalAlign from TextAlignment (#6807)
# Objective

Remove the `VerticalAlign` enum.

Text's alignment field should only affect the text's internal text alignment, not its position. The only way to control a `TextBundle`'s position and bounds should be through the manipulation of the constraints in the `Style` components of the nodes in the Bevy UI's layout tree.

 `Text2dBundle` should have a separate `Anchor` component that sets its position relative to its transform.

Related issues: #676, #1490, #5502, #5513, #5834, #6717, #6724, #6741, #6748

## Changelog
* Changed `TextAlignment` into an enum with `Left`, `Center`, and `Right` variants.
* Removed the `HorizontalAlign` and `VerticalAlign` types.
* Added an `Anchor` component to `Text2dBundle`
* Added `Component` derive to `Anchor`
* Use `f32::INFINITY` instead of `f32::MAX` to represent unbounded text in Text2dBounds

## Migration Guide
The `alignment` field of `Text` now only affects the text's internal alignment.

### Change `TextAlignment` to TextAlignment` which is now an enum. Replace:
  * `TextAlignment::TOP_LEFT`, `TextAlignment::CENTER_LEFT`, `TextAlignment::BOTTOM_LEFT` with `TextAlignment::Left`
  * `TextAlignment::TOP_CENTER`, `TextAlignment::CENTER_LEFT`, `TextAlignment::BOTTOM_CENTER` with `TextAlignment::Center`
  * `TextAlignment::TOP_RIGHT`, `TextAlignment::CENTER_RIGHT`, `TextAlignment::BOTTOM_RIGHT` with `TextAlignment::Right`

### Changes for `Text2dBundle`
`Text2dBundle` has a new field 'text_anchor' that takes an `Anchor` component that controls its position relative to its transform.
2023-01-18 02:19:17 +00:00

154 lines
4.6 KiB
Rust

use bevy::{input::touch::TouchPhase, prelude::*, window::WindowMode};
// the `bevy_main` proc_macro generates the required ios boilerplate
#[bevy_main]
fn main() {
App::new()
.add_plugins(DefaultPlugins.set(WindowPlugin {
window: WindowDescriptor {
resizable: false,
mode: WindowMode::BorderlessFullscreen,
..default()
},
..default()
}))
.add_startup_system(setup_scene)
.add_startup_system(setup_music)
.add_system(touch_camera)
.add_system(button_handler)
.run();
}
fn touch_camera(
windows: ResMut<Windows>,
mut touches: EventReader<TouchInput>,
mut camera: Query<&mut Transform, With<Camera3d>>,
mut last_position: Local<Option<Vec2>>,
) {
for touch in touches.iter() {
if touch.phase == TouchPhase::Started {
*last_position = None;
}
if let Some(last_position) = *last_position {
let window = windows.primary();
let mut transform = camera.single_mut();
*transform = Transform::from_xyz(
transform.translation.x
+ (touch.position.x - last_position.x) / window.width() * 5.0,
transform.translation.y,
transform.translation.z
+ (touch.position.y - last_position.y) / window.height() * 5.0,
)
.looking_at(Vec3::ZERO, Vec3::Y);
}
*last_position = Some(touch.position);
}
}
/// set up a simple 3D scene
fn setup_scene(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
asset_server: Res<AssetServer>,
) {
// plane
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })),
material: materials.add(Color::rgb(0.1, 0.2, 0.1).into()),
..default()
});
// cube
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
material: materials.add(Color::rgb(0.5, 0.4, 0.3).into()),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..default()
});
// sphere
commands.spawn(PbrBundle {
mesh: meshes.add(
Mesh::try_from(shape::Icosphere {
subdivisions: 4,
radius: 0.5,
})
.unwrap(),
),
material: materials.add(Color::rgb(0.1, 0.4, 0.8).into()),
transform: Transform::from_xyz(1.5, 1.5, 1.5),
..default()
});
// light
commands.spawn(PointLightBundle {
transform: Transform::from_xyz(4.0, 8.0, 4.0),
point_light: PointLight {
intensity: 5000.0,
shadows_enabled: true,
..default()
},
..default()
});
// camera
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
// Test ui
commands
.spawn(ButtonBundle {
style: Style {
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
position_type: PositionType::Absolute,
position: UiRect {
left: Val::Px(50.0),
right: Val::Px(50.0),
top: Val::Auto,
bottom: Val::Px(50.0),
},
..default()
},
..default()
})
.with_children(|b| {
b.spawn(
TextBundle::from_section(
"Test Button",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 30.0,
color: Color::BLACK,
},
)
.with_text_alignment(TextAlignment::Center),
);
});
}
fn button_handler(
mut interaction_query: Query<
(&Interaction, &mut BackgroundColor),
(Changed<Interaction>, With<Button>),
>,
) {
for (interaction, mut color) in &mut interaction_query {
match *interaction {
Interaction::Clicked => {
*color = Color::BLUE.into();
}
Interaction::Hovered => {
*color = Color::GRAY.into();
}
Interaction::None => {
*color = Color::WHITE.into();
}
}
}
}
fn setup_music(asset_server: Res<AssetServer>, audio: Res<Audio>) {
let music = asset_server.load("sounds/Windless Slopes.ogg");
audio.play(music);
}