-
Notifications
You must be signed in to change notification settings - Fork 65
/
ui_position.rs
137 lines (124 loc) · 4.07 KB
/
ui_position.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use bevy::{color::palettes::css::*, prelude::*};
use bevy_inspector_egui::{prelude::*, quick::ResourceInspectorPlugin};
use bevy_tweening::{lens::*, *};
mod utils;
fn main() {
App::default()
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
title: "UiPositionLens".to_string(),
resolution: (1400., 600.).into(),
present_mode: bevy::window::PresentMode::Fifo, // vsync
..default()
}),
..default()
}))
.init_resource::<Options>()
.add_systems(Update, utils::close_on_esc)
.add_plugins(TweeningPlugin)
.add_plugins(ResourceInspectorPlugin::<Options>::new())
.add_systems(Startup, setup)
.add_systems(Update, update_animation_speed)
.run();
}
#[derive(Copy, Clone, PartialEq, Resource, Reflect, InspectorOptions)]
struct Options {
#[inspector(min = 0.01, max = 100.)]
speed: f32,
}
impl Default for Options {
fn default() -> Self {
Self { speed: 1. }
}
}
fn setup(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());
let size = 25.;
let screen_x = 1400.;
let screen_y = 600.;
let offset_x = (screen_x - 30. * size) / 31. + size;
let mut x = 10.;
for ease_function in &[
EaseFunction::QuadraticIn,
EaseFunction::QuadraticOut,
EaseFunction::QuadraticInOut,
EaseFunction::CubicIn,
EaseFunction::CubicOut,
EaseFunction::CubicInOut,
EaseFunction::QuarticIn,
EaseFunction::QuarticOut,
EaseFunction::QuarticInOut,
EaseFunction::QuinticIn,
EaseFunction::QuinticOut,
EaseFunction::QuinticInOut,
EaseFunction::SineIn,
EaseFunction::SineOut,
EaseFunction::SineInOut,
EaseFunction::CircularIn,
EaseFunction::CircularOut,
EaseFunction::CircularInOut,
EaseFunction::ExponentialIn,
EaseFunction::ExponentialOut,
EaseFunction::ExponentialInOut,
EaseFunction::ElasticIn,
EaseFunction::ElasticOut,
EaseFunction::ElasticInOut,
EaseFunction::BackIn,
EaseFunction::BackOut,
EaseFunction::BackInOut,
EaseFunction::BounceIn,
EaseFunction::BounceOut,
EaseFunction::BounceInOut,
] {
let tween = Tween::new(
*ease_function,
std::time::Duration::from_secs(1),
UiPositionLens {
start: UiRect {
left: Val::Px(x),
top: Val::Px(10.),
right: Val::Auto,
bottom: Val::Auto,
},
end: UiRect {
left: Val::Px(x),
top: Val::Px(screen_y - 10. - size),
right: Val::Auto,
bottom: Val::Auto,
},
},
)
.with_repeat_count(RepeatCount::Infinite)
.with_repeat_strategy(RepeatStrategy::MirroredRepeat);
commands.spawn((
NodeBundle {
style: Style {
width: Val::Px(size),
height: Val::Px(size),
left: Val::Px(x),
top: Val::Px(10.),
right: Val::Auto,
bottom: Val::Auto,
position_type: PositionType::Absolute,
align_content: AlignContent::Center,
align_items: AlignItems::Center,
align_self: AlignSelf::Center,
justify_content: JustifyContent::Center,
..default()
},
background_color: BackgroundColor(RED.into()),
..default()
},
Animator::new(tween),
));
x += offset_x;
}
}
fn update_animation_speed(options: Res<Options>, mut animators: Query<&mut Animator<Style>>) {
if !options.is_changed() {
return;
}
for mut animator in animators.iter_mut() {
animator.set_speed(options.speed);
}
}