2023-04-05 23:07:41 +00:00
|
|
|
//! Tests how different transforms behave when clipped with `Overflow::Hidden`
|
2024-02-19 19:15:47 +00:00
|
|
|
use bevy::{input::common_conditions::input_just_pressed, prelude::*};
|
2023-04-05 23:07:41 +00:00
|
|
|
use std::f32::consts::{FRAC_PI_2, PI, TAU};
|
|
|
|
|
|
|
|
const CONTAINER_SIZE: f32 = 150.0;
|
|
|
|
const HALF_CONTAINER_SIZE: f32 = CONTAINER_SIZE / 2.0;
|
|
|
|
const LOOP_LENGTH: f32 = 4.0;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
App::new()
|
|
|
|
.add_plugins(DefaultPlugins)
|
2024-02-19 19:15:47 +00:00
|
|
|
.init_resource::<AnimationState>()
|
2023-04-05 23:07:41 +00:00
|
|
|
.add_systems(Startup, setup)
|
|
|
|
.add_systems(
|
|
|
|
Update,
|
|
|
|
(
|
2024-02-19 19:15:47 +00:00
|
|
|
toggle_overflow.run_if(input_just_pressed(KeyCode::KeyO)),
|
|
|
|
next_container_size.run_if(input_just_pressed(KeyCode::KeyS)),
|
2023-04-05 23:07:41 +00:00
|
|
|
update_transform::<Move>,
|
|
|
|
update_transform::<Scale>,
|
|
|
|
update_transform::<Rotate>,
|
|
|
|
update_animation,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
2024-02-19 19:15:47 +00:00
|
|
|
#[derive(Component)]
|
|
|
|
struct Instructions;
|
|
|
|
|
|
|
|
#[derive(Resource, Default)]
|
2023-04-05 23:07:41 +00:00
|
|
|
struct AnimationState {
|
|
|
|
playing: bool,
|
|
|
|
paused_at: f32,
|
|
|
|
paused_total: f32,
|
|
|
|
t: f32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
struct Container(u8);
|
|
|
|
|
|
|
|
trait UpdateTransform {
|
|
|
|
fn update(&self, t: f32, transform: &mut Transform);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
struct Move;
|
|
|
|
|
|
|
|
impl UpdateTransform for Move {
|
|
|
|
fn update(&self, t: f32, transform: &mut Transform) {
|
|
|
|
transform.translation.x = (t * TAU - FRAC_PI_2).sin() * HALF_CONTAINER_SIZE;
|
|
|
|
transform.translation.y = -(t * TAU - FRAC_PI_2).cos() * HALF_CONTAINER_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
struct Scale;
|
|
|
|
|
|
|
|
impl UpdateTransform for Scale {
|
|
|
|
fn update(&self, t: f32, transform: &mut Transform) {
|
|
|
|
transform.scale.x = 1.0 + 0.5 * (t * TAU).cos().max(0.0);
|
|
|
|
transform.scale.y = 1.0 + 0.5 * (t * TAU + PI).cos().max(0.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
struct Rotate;
|
|
|
|
|
|
|
|
impl UpdateTransform for Rotate {
|
|
|
|
fn update(&self, t: f32, transform: &mut Transform) {
|
|
|
|
transform.rotation = Quat::from_axis_angle(Vec3::Z, ((t * TAU).cos() * 45.0).to_radians());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|
|
|
// Camera
|
2024-02-19 19:15:47 +00:00
|
|
|
|
2023-04-05 23:07:41 +00:00
|
|
|
commands.spawn(Camera2dBundle::default());
|
|
|
|
|
2024-02-19 19:15:47 +00:00
|
|
|
// Instructions
|
|
|
|
|
|
|
|
let text_style = TextStyle {
|
|
|
|
font_size: 20.,
|
|
|
|
..default()
|
|
|
|
};
|
|
|
|
|
|
|
|
commands.spawn((
|
|
|
|
TextBundle::from_sections([
|
|
|
|
TextSection::new(
|
|
|
|
"Next Overflow Setting (O)\nNext Container Size (S)\nToggle Animation (space)\n\n",
|
|
|
|
text_style.clone(),
|
|
|
|
),
|
|
|
|
TextSection::new(format!("{:?}", Overflow::clip()), text_style.clone()),
|
|
|
|
])
|
|
|
|
.with_style(Style {
|
|
|
|
position_type: PositionType::Absolute,
|
|
|
|
top: Val::Px(12.0),
|
|
|
|
left: Val::Px(12.0),
|
|
|
|
..default()
|
|
|
|
}),
|
|
|
|
Instructions,
|
|
|
|
));
|
|
|
|
|
|
|
|
// Overflow Debug
|
|
|
|
|
2023-04-05 23:07:41 +00:00
|
|
|
commands
|
|
|
|
.spawn(NodeBundle {
|
|
|
|
style: Style {
|
Flatten UI `Style` properties that use `Size` + remove `Size` (#8548)
# Objective
- Simplify API and make authoring styles easier
See:
https://github.com/bevyengine/bevy/issues/8540#issuecomment-1536177102
## Solution
- The `size`, `min_size`, `max_size`, and `gap` properties have been
replaced by `width`, `height`, `min_width`, `min_height`, `max_width`,
`max_height`, `row_gap`, and `column_gap` properties
---
## Changelog
- Flattened `Style` properties that have a `Size` value directly into
`Style`
## Migration Guide
- The `size`, `min_size`, `max_size`, and `gap` properties have been
replaced by the `width`, `height`, `min_width`, `min_height`,
`max_width`, `max_height`, `row_gap`, and `column_gap` properties. Use
the new properties instead.
---------
Co-authored-by: ickshonpe <david.curthoys@googlemail.com>
2023-05-16 01:36:32 +00:00
|
|
|
width: Val::Percent(100.),
|
2024-02-19 19:15:47 +00:00
|
|
|
height: Val::Percent(100.),
|
|
|
|
justify_content: JustifyContent::Center,
|
|
|
|
align_items: AlignItems::Center,
|
2023-04-05 23:07:41 +00:00
|
|
|
..default()
|
|
|
|
},
|
|
|
|
..default()
|
|
|
|
})
|
|
|
|
.with_children(|parent| {
|
|
|
|
parent
|
|
|
|
.spawn(NodeBundle {
|
|
|
|
style: Style {
|
2024-02-19 19:15:47 +00:00
|
|
|
display: Display::Grid,
|
|
|
|
grid_template_columns: RepeatedGridTrack::px(3, CONTAINER_SIZE),
|
|
|
|
grid_template_rows: RepeatedGridTrack::px(2, CONTAINER_SIZE),
|
|
|
|
row_gap: Val::Px(80.),
|
|
|
|
column_gap: Val::Px(80.),
|
2023-04-05 23:07:41 +00:00
|
|
|
..default()
|
|
|
|
},
|
|
|
|
..default()
|
|
|
|
})
|
|
|
|
.with_children(|parent| {
|
2024-02-19 19:15:47 +00:00
|
|
|
spawn_image(parent, &asset_server, Move);
|
|
|
|
spawn_image(parent, &asset_server, Scale);
|
|
|
|
spawn_image(parent, &asset_server, Rotate);
|
2023-04-05 23:07:41 +00:00
|
|
|
|
2024-02-19 19:15:47 +00:00
|
|
|
spawn_text(parent, &asset_server, Move);
|
|
|
|
spawn_text(parent, &asset_server, Scale);
|
|
|
|
spawn_text(parent, &asset_server, Rotate);
|
2023-04-05 23:07:41 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn spawn_image(
|
|
|
|
parent: &mut ChildBuilder,
|
|
|
|
asset_server: &Res<AssetServer>,
|
|
|
|
update_transform: impl UpdateTransform + Component,
|
|
|
|
) {
|
|
|
|
spawn_container(parent, update_transform, |parent| {
|
|
|
|
parent.spawn(ImageBundle {
|
|
|
|
image: UiImage::new(asset_server.load("branding/bevy_logo_dark_big.png")),
|
|
|
|
style: Style {
|
Flatten UI `Style` properties that use `Size` + remove `Size` (#8548)
# Objective
- Simplify API and make authoring styles easier
See:
https://github.com/bevyengine/bevy/issues/8540#issuecomment-1536177102
## Solution
- The `size`, `min_size`, `max_size`, and `gap` properties have been
replaced by `width`, `height`, `min_width`, `min_height`, `max_width`,
`max_height`, `row_gap`, and `column_gap` properties
---
## Changelog
- Flattened `Style` properties that have a `Size` value directly into
`Style`
## Migration Guide
- The `size`, `min_size`, `max_size`, and `gap` properties have been
replaced by the `width`, `height`, `min_width`, `min_height`,
`max_width`, `max_height`, `row_gap`, and `column_gap` properties. Use
the new properties instead.
---------
Co-authored-by: ickshonpe <david.curthoys@googlemail.com>
2023-05-16 01:36:32 +00:00
|
|
|
height: Val::Px(100.),
|
2023-04-05 23:07:41 +00:00
|
|
|
position_type: PositionType::Absolute,
|
|
|
|
top: Val::Px(-50.),
|
|
|
|
left: Val::Px(-200.),
|
|
|
|
..default()
|
|
|
|
},
|
|
|
|
..default()
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn spawn_text(
|
|
|
|
parent: &mut ChildBuilder,
|
|
|
|
asset_server: &Res<AssetServer>,
|
|
|
|
update_transform: impl UpdateTransform + Component,
|
|
|
|
) {
|
|
|
|
spawn_container(parent, update_transform, |parent| {
|
|
|
|
parent.spawn(TextBundle::from_section(
|
|
|
|
"Bevy",
|
|
|
|
TextStyle {
|
|
|
|
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
|
|
|
|
font_size: 120.0,
|
2023-11-03 12:57:38 +00:00
|
|
|
..default()
|
2023-04-05 23:07:41 +00:00
|
|
|
},
|
|
|
|
));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn spawn_container(
|
|
|
|
parent: &mut ChildBuilder,
|
|
|
|
update_transform: impl UpdateTransform + Component,
|
|
|
|
spawn_children: impl FnOnce(&mut ChildBuilder),
|
|
|
|
) {
|
|
|
|
let mut transform = Transform::default();
|
|
|
|
|
|
|
|
update_transform.update(0.0, &mut transform);
|
|
|
|
|
|
|
|
parent
|
|
|
|
.spawn((
|
|
|
|
NodeBundle {
|
|
|
|
style: Style {
|
2024-02-19 19:15:47 +00:00
|
|
|
width: Val::Percent(100.),
|
|
|
|
height: Val::Percent(100.),
|
2023-04-05 23:07:41 +00:00
|
|
|
align_items: AlignItems::Center,
|
|
|
|
justify_content: JustifyContent::Center,
|
2023-04-17 22:23:52 +00:00
|
|
|
overflow: Overflow::clip(),
|
2023-04-05 23:07:41 +00:00
|
|
|
..default()
|
|
|
|
},
|
2024-02-24 21:35:32 +00:00
|
|
|
background_color: LegacyColor::DARK_GRAY.into(),
|
2023-04-05 23:07:41 +00:00
|
|
|
..default()
|
|
|
|
},
|
|
|
|
Container(0),
|
|
|
|
))
|
|
|
|
.with_children(|parent| {
|
|
|
|
parent
|
|
|
|
.spawn((
|
|
|
|
NodeBundle {
|
|
|
|
style: Style {
|
|
|
|
align_items: AlignItems::Center,
|
|
|
|
justify_content: JustifyContent::Center,
|
|
|
|
top: Val::Px(transform.translation.x),
|
|
|
|
left: Val::Px(transform.translation.y),
|
|
|
|
..default()
|
|
|
|
},
|
|
|
|
transform,
|
|
|
|
..default()
|
|
|
|
},
|
|
|
|
update_transform,
|
|
|
|
))
|
|
|
|
.with_children(spawn_children);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_animation(
|
|
|
|
mut animation: ResMut<AnimationState>,
|
|
|
|
time: Res<Time>,
|
2023-12-06 20:32:34 +00:00
|
|
|
keys: Res<ButtonInput<KeyCode>>,
|
2023-04-05 23:07:41 +00:00
|
|
|
) {
|
2024-02-19 19:15:47 +00:00
|
|
|
let delta = time.elapsed_seconds();
|
2023-04-05 23:07:41 +00:00
|
|
|
|
|
|
|
if keys.just_pressed(KeyCode::Space) {
|
|
|
|
animation.playing = !animation.playing;
|
|
|
|
|
|
|
|
if !animation.playing {
|
2024-02-19 19:15:47 +00:00
|
|
|
animation.paused_at = delta;
|
2023-04-05 23:07:41 +00:00
|
|
|
} else {
|
2024-02-19 19:15:47 +00:00
|
|
|
animation.paused_total += delta - animation.paused_at;
|
2023-04-05 23:07:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if animation.playing {
|
2024-02-19 19:15:47 +00:00
|
|
|
animation.t = (delta - animation.paused_total) % LOOP_LENGTH / LOOP_LENGTH;
|
2023-04-05 23:07:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_transform<T: UpdateTransform + Component>(
|
|
|
|
animation: Res<AnimationState>,
|
|
|
|
mut containers: Query<(&mut Transform, &mut Style, &T)>,
|
|
|
|
) {
|
|
|
|
for (mut transform, mut style, update_transform) in &mut containers {
|
|
|
|
update_transform.update(animation.t, &mut transform);
|
|
|
|
|
|
|
|
style.left = Val::Px(transform.translation.x);
|
|
|
|
style.top = Val::Px(transform.translation.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-06 20:32:34 +00:00
|
|
|
fn toggle_overflow(
|
|
|
|
mut containers: Query<&mut Style, With<Container>>,
|
2024-02-19 19:15:47 +00:00
|
|
|
mut instructions: Query<&mut Text, With<Instructions>>,
|
2023-12-06 20:32:34 +00:00
|
|
|
) {
|
2024-02-19 19:15:47 +00:00
|
|
|
for mut style in &mut containers {
|
|
|
|
style.overflow = match style.overflow {
|
|
|
|
Overflow {
|
|
|
|
x: OverflowAxis::Visible,
|
|
|
|
y: OverflowAxis::Visible,
|
|
|
|
} => Overflow::clip_y(),
|
|
|
|
Overflow {
|
|
|
|
x: OverflowAxis::Visible,
|
|
|
|
y: OverflowAxis::Clip,
|
|
|
|
} => Overflow::clip_x(),
|
|
|
|
Overflow {
|
|
|
|
x: OverflowAxis::Clip,
|
|
|
|
y: OverflowAxis::Visible,
|
|
|
|
} => Overflow::clip(),
|
|
|
|
_ => Overflow::visible(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut text = instructions.single_mut();
|
|
|
|
text.sections[1].value = format!("{:?}", style.overflow);
|
2023-04-05 23:07:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-19 19:15:47 +00:00
|
|
|
fn next_container_size(mut containers: Query<(&mut Style, &mut Container)>) {
|
|
|
|
for (mut style, mut container) in &mut containers {
|
|
|
|
container.0 = (container.0 + 1) % 3;
|
|
|
|
|
|
|
|
style.width = match container.0 {
|
|
|
|
2 => Val::Percent(30.),
|
|
|
|
_ => Val::Percent(100.),
|
|
|
|
};
|
|
|
|
style.height = match container.0 {
|
|
|
|
1 => Val::Percent(30.),
|
|
|
|
_ => Val::Percent(100.),
|
|
|
|
};
|
2023-04-05 23:07:41 +00:00
|
|
|
}
|
|
|
|
}
|