forked from djeedai/bevy_tweening
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform_translation.rs
135 lines (121 loc) · 3.66 KB
/
transform_translation.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
use bevy::prelude::*;
use bevy_egui::{egui, EguiContext, EguiPlugin};
use bevy_tweening::{lens::*, *};
fn main() {
App::default()
.insert_resource(WindowDescriptor {
title: "TransformPositionLens".to_string(),
width: 1400.,
height: 600.,
present_mode: bevy::window::PresentMode::Fifo, // vsync
..Default::default()
})
.add_plugins(DefaultPlugins)
.add_plugin(TweeningPlugin)
.add_plugin(EguiPlugin)
.add_startup_system(setup)
.init_resource::<Options>()
.add_system(options_panel)
.add_system(update_animation_speed)
.run();
}
#[derive(Copy, Clone, PartialEq)]
struct Options {
open: bool,
speed: f32,
}
impl Default for Options {
fn default() -> Self {
Self {
open: true,
speed: 1.,
}
}
}
fn setup(mut commands: Commands) {
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
let size = 25.;
let spacing = 1.5;
let screen_x = 570.;
let screen_y = 150.;
let mut x = -screen_x;
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,
TweeningType::PingPong,
std::time::Duration::from_secs(1),
TransformPositionLens {
start: Vec3::new(x, screen_y, 0.),
end: Vec3::new(x, -screen_y, 0.),
},
);
commands
.spawn_bundle(SpriteBundle {
sprite: Sprite {
color: Color::RED,
custom_size: Some(Vec2::new(size, size)),
..Default::default()
},
..Default::default()
})
.insert(Animator::new(tween));
x += size * spacing;
}
}
fn options_panel(mut egui_context: ResMut<EguiContext>, mut options: ResMut<Options>) {
let mut local_options = options.clone();
egui::Window::new("Options")
.open(&mut local_options.open)
.show(egui_context.ctx_mut(), |ui| {
ui.horizontal(|ui| {
ui.label("Speed modifier");
ui.add(
egui::DragValue::new(&mut local_options.speed)
.speed(0.01)
.clamp_range(0.01..=100.),
);
});
});
if local_options != *options {
*options = local_options;
}
}
fn update_animation_speed(options: Res<Options>, mut animators: Query<&mut Animator<Transform>>) {
if !options.is_changed() {
return;
}
for mut animator in animators.iter_mut() {
animator.set_speed(options.speed);
}
}