Skip to content

Commit c47ba16

Browse files
committed
Add Helpers and private some imports
This is for the goal of cleaning up the code code imports. Building animations is now a lot cleaner.
1 parent 3850416 commit c47ba16

File tree

9 files changed

+291
-182
lines changed

9 files changed

+291
-182
lines changed

examples/counter/src/main.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ use iced::{
55
Alignment, Application, Command, Element, Event, Length, Settings, Subscription, Theme,
66
};
77

8-
use cosmic_time::{self, chain, keyframes, Timeline};
8+
use cosmic_time::{self, anim, chain, id, Timeline};
99

1010
use once_cell::sync::Lazy;
1111

12-
static CONTAINER: Lazy<keyframes::container::Id> = Lazy::new(keyframes::container::Id::unique);
12+
static CONTAINER: Lazy<id::Container> = Lazy::new(id::Container::unique);
1313

1414
pub fn main() -> iced::Result {
1515
Counter::run(Settings::default())
@@ -34,6 +34,7 @@ impl Application for Counter {
3434
type Flags = ();
3535

3636
fn new(_flags: ()) -> (Self, Command<Message>) {
37+
use cosmic_time::container;
3738
// This is new! This is how we build a timeline!
3839
// These values can be created at anytime, but because this example is
3940
// simple and we want to animate from application init, we will build the
@@ -42,21 +43,17 @@ impl Application for Counter {
4243
let mut timeline = Timeline::new();
4344
let animation = chain![
4445
CONTAINER,
45-
keyframes::Container::new(Duration::ZERO)
46-
.width(0.)
47-
.height(100.),
48-
keyframes::Container::new(Duration::from_secs(2))
49-
.width(200.)
50-
.height(100.),
51-
keyframes::Container::new(Duration::from_secs(2))
46+
container(Duration::ZERO).width(0.).height(100.),
47+
container(Duration::from_secs(2)).width(200.).height(100.),
48+
container(Duration::from_secs(2))
5249
.width(200.)
5350
.height(300.)
5451
.padding([0, 0, 0, 0]),
55-
keyframes::Container::new(Duration::from_secs(2))
52+
container(Duration::from_secs(2))
5653
.width(700.)
5754
.height(300.)
5855
.padding([0, 0, 0, 500]),
59-
keyframes::Container::new(Duration::from_secs(2))
56+
container(Duration::from_secs(2))
6057
.width(150.)
6158
.height(150.)
6259
.padding([0, 0, 0, 0]),
@@ -119,8 +116,8 @@ impl Application for Counter {
119116
// just define the width with a `width` method like any other widget, then
120117
// animate the height in your view! Only control the animatable values with
121118
// cosmic-time, all others should be in your view!
122-
keyframes::Container::as_widget(
123-
CONTAINER.clone(),
119+
anim!(
120+
CONTAINER,
124121
&self.timeline,
125122
column![
126123
button("Increment")

examples/pokedex/src/main.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ use iced::{
66
};
77

88
use cosmic_time::{
9-
self, anim, chain, keyframes, Back, Bounce, Circular, Ease, Elastic, Exponential, Linear,
10-
Quadratic, Quartic, Quintic, Sinusoidal, Timeline,
9+
self, anim, chain, id, Back, Bounce, Circular, Ease, Elastic, Exponential, Linear, Quadratic,
10+
Quartic, Quintic, Sinusoidal, Timeline,
1111
};
1212
use once_cell::sync::Lazy;
1313

14-
static SPACE: Lazy<keyframes::space::Id> = Lazy::new(keyframes::space::Id::unique);
14+
static SPACE: Lazy<id::Space> = Lazy::new(id::Space::unique);
1515

1616
const EASE_IN: [Ease; 10] = [
1717
Ease::Linear(Linear::InOut),
@@ -188,6 +188,7 @@ impl Pokemon {
188188
}
189189

190190
async fn search() -> Result<Pokemon, Error> {
191+
use cosmic_time::space;
191192
use rand::Rng;
192193
use serde::Deserialize;
193194

@@ -248,11 +249,11 @@ impl Pokemon {
248249

249250
let animation = chain![
250251
SPACE,
251-
keyframes::Space::new(Duration::ZERO).height(50.),
252-
keyframes::Space::new(Duration::from_millis(1500))
252+
space(Duration::ZERO).height(50.),
253+
space(Duration::from_millis(1500))
253254
.height(250.)
254255
.ease(EASE_IN[rand]),
255-
keyframes::Space::new(Duration::from_millis(3000))
256+
space(Duration::from_millis(3000))
256257
.height(50.)
257258
.ease(EASE_OUT[rand])
258259
]

examples/pong/src/main.rs

Lines changed: 94 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use iced::widget::{column, container, row, Space};
55
use iced::{executor, Application, Command, Event, Length, Settings, Subscription};
66
use iced_native::window;
77

8-
use cosmic_time::{self, anim, chain, keyframes, Duration, Instant, Speed, Timeline};
8+
use cosmic_time::{self, anim, chain, id, Duration, Instant, Speed, Timeline};
99

1010
use once_cell::sync::Lazy;
1111
use rand::prelude::*;
@@ -15,10 +15,10 @@ mod theme;
1515
use layer::Layer;
1616
use theme::{widget::Element, Theme};
1717

18-
static PADDLE_LEFT: Lazy<keyframes::space::Id> = Lazy::new(keyframes::space::Id::unique);
19-
static PADDLE_RIGHT: Lazy<keyframes::space::Id> = Lazy::new(keyframes::space::Id::unique);
20-
static BALL_X: Lazy<keyframes::space::Id> = Lazy::new(keyframes::space::Id::unique);
21-
static BALL_Y: Lazy<keyframes::space::Id> = Lazy::new(keyframes::space::Id::unique);
18+
static PADDLE_LEFT: Lazy<id::Space> = Lazy::new(id::Space::unique);
19+
static PADDLE_RIGHT: Lazy<id::Space> = Lazy::new(id::Space::unique);
20+
static BALL_X: Lazy<id::Space> = Lazy::new(id::Space::unique);
21+
static BALL_Y: Lazy<id::Space> = Lazy::new(id::Space::unique);
2222

2323
pub fn main() -> iced::Result {
2424
Pong::run(Settings::default())
@@ -185,122 +185,142 @@ impl Application for Pong {
185185
}
186186

187187
impl Pong {
188-
fn anim_left(&mut self, direction: Direction) -> Option<cosmic_time::space::Chain> {
188+
fn anim_left(&mut self, direction: Direction) -> Option<cosmic_time::Chain> {
189+
use cosmic_time::lazy::space as lazy;
190+
use cosmic_time::space;
191+
let height = self.window.height - self.window.paddle_height;
192+
189193
if self.left != direction {
190194
self.left = direction;
191-
Some(match direction {
192-
Direction::Down => chain![
193-
PADDLE_LEFT,
194-
// OOh here are the lazy keyframes!
195-
// This means that this animation will start at wherever the previous
196-
// animation left off!
197-
// Lazy still takes a duration, this will usually be `Duration::ZERO`
198-
// like regular animations, but you can put them anywhere in your
199-
// animation chain. Meaning that you would have an animation start
200-
// at the previous animations's interupted location, animate to elsewhere,
201-
// then go back to that spot!
202-
//
203-
// Also notice the speed here is per_millis! This is important.
204-
// The animation is only as granular as your definition in the chain.
205-
// If you animation time is not in exact seconds, I highly recommend
206-
// using a smaller unit.
207-
keyframes::Space::lazy(Duration::ZERO),
208-
keyframes::Space::new(Speed::per_millis(0.3)).height(self.window.height - 100.),
209-
],
210-
Direction::Up => chain![
211-
PADDLE_LEFT,
212-
keyframes::Space::lazy(Duration::ZERO),
213-
keyframes::Space::new(Speed::per_millis(0.3)).height(0.)
214-
],
215-
})
195+
Some(
196+
match direction {
197+
Direction::Down => chain![
198+
PADDLE_LEFT,
199+
// OOh here are the lazy keyframes!
200+
// This means that this animation will start at wherever the previous
201+
// animation left off!
202+
// Lazy still takes a duration, this will usually be `Duration::ZERO`
203+
// like regular animations, but you can put them anywhere in your
204+
// animation chain. Meaning that you would have an animation start
205+
// at the previous animations's interupted location, animate to elsewhere,
206+
// then go back to that spot!
207+
//
208+
// Also notice the speed here is per_millis! This is important.
209+
// The animation is only as granular as your definition in the chain.
210+
// If you animation time is not in exact seconds, I highly recommend
211+
// using a smaller unit.
212+
lazy(Duration::ZERO),
213+
space(Speed::per_millis(0.3)).height(height),
214+
],
215+
Direction::Up => chain![
216+
PADDLE_LEFT,
217+
lazy(Duration::ZERO),
218+
space(Speed::per_millis(0.3)).height(0.)
219+
],
220+
}
221+
.into(),
222+
)
216223
} else {
217224
None
218225
}
219226
}
220227

221-
fn anim_right(&mut self, direction: Direction) -> Option<cosmic_time::space::Chain> {
228+
fn anim_right(&mut self, direction: Direction) -> Option<cosmic_time::Chain> {
229+
use cosmic_time::lazy::space as lazy;
230+
use cosmic_time::space;
231+
let height = self.window.height - self.window.paddle_height;
232+
222233
if self.right != direction {
223234
self.right = direction;
224-
Some(match direction {
225-
Direction::Down => chain![
226-
PADDLE_RIGHT,
227-
keyframes::Space::lazy(Duration::ZERO),
228-
keyframes::Space::new(Speed::per_millis(0.3)).height(self.window.height - 100.),
229-
],
230-
Direction::Up => chain![
231-
PADDLE_RIGHT,
232-
keyframes::Space::lazy(Duration::ZERO),
233-
keyframes::Space::new(Speed::per_millis(0.3)).height(0.)
234-
],
235-
})
235+
Some(
236+
match direction {
237+
Direction::Down => chain![
238+
PADDLE_RIGHT,
239+
lazy(Duration::ZERO),
240+
space(Speed::per_millis(0.3)).height(height),
241+
],
242+
Direction::Up => chain![
243+
PADDLE_RIGHT,
244+
lazy(Duration::ZERO),
245+
space(Speed::per_millis(0.3)).height(0.)
246+
],
247+
}
248+
.into(),
249+
)
236250
} else {
237251
None
238252
}
239253
}
240254

241-
fn init_ball_y(&mut self) -> cosmic_time::space::Chain {
255+
fn init_ball_y(&mut self) -> cosmic_time::Chain {
256+
use cosmic_time::space;
242257
let min = self.window.height * 0.3;
243258
let max = self.window.height - min - self.window.paddle_height;
244259
let height = self.rng.gen_range(min..max);
245260

246-
chain![BALL_Y, keyframes::Space::new(Duration::ZERO).height(height)]
261+
chain![BALL_Y, space(Duration::ZERO).height(height)].into()
247262
}
248263

249-
fn init_ball_x(&mut self) -> cosmic_time::space::Chain {
264+
fn init_ball_x(&mut self) -> cosmic_time::Chain {
265+
use cosmic_time::space;
250266
let min = self.window.width * 0.3;
251267
let max = self.window.width - min - self.window.paddle_width;
252268
let width = self.rng.gen_range(min..max);
253269

254-
chain![BALL_X, keyframes::Space::new(Duration::ZERO).width(width)]
270+
chain![BALL_X, space(Duration::ZERO).width(width)].into()
255271
}
256272

257-
fn rand_vertical_bounce(&mut self) -> cosmic_time::space::Chain {
273+
fn rand_vertical_bounce(&mut self) -> cosmic_time::Chain {
274+
use cosmic_time::lazy::space as lazy;
275+
use cosmic_time::space;
258276
let speed = 100. * self.rng.gen_range(0.9..1.1);
277+
let height = self.window.height - self.window.paddle_width;
278+
259279
if self.rng.gen() {
260280
chain![
261281
BALL_Y,
262-
keyframes::Space::lazy(Duration::ZERO),
263-
keyframes::Space::new(Speed::per_secs(speed))
264-
.height(self.window.height - self.window.paddle_width),
265-
keyframes::Space::new(Speed::per_secs(speed)).height(0.),
266-
keyframes::Space::lazy(Speed::per_secs(speed))
282+
lazy(Duration::ZERO),
283+
space(Speed::per_secs(speed)).height(height),
284+
space(Speed::per_secs(speed)).height(0.),
285+
lazy(Speed::per_secs(speed))
267286
]
268-
.loop_forever()
269287
} else {
270288
chain![
271289
BALL_Y,
272-
keyframes::Space::lazy(Duration::ZERO),
273-
keyframes::Space::new(Speed::per_secs(speed)).height(0.),
274-
keyframes::Space::new(Speed::per_secs(speed))
275-
.height(self.window.height - self.window.paddle_width),
276-
keyframes::Space::lazy(Speed::per_secs(speed))
290+
lazy(Duration::ZERO),
291+
space(Speed::per_secs(speed)).height(0.),
292+
space(Speed::per_secs(speed)).height(height),
293+
lazy(Speed::per_secs(speed))
277294
]
278-
.loop_forever()
279295
}
296+
.loop_forever()
297+
.into()
280298
}
281299

282-
fn rand_horizontal_bounce(&mut self) -> cosmic_time::space::Chain {
300+
fn rand_horizontal_bounce(&mut self) -> cosmic_time::Chain {
301+
use cosmic_time::lazy::space as lazy;
302+
use cosmic_time::space;
283303
let speed = 100. * self.rng.gen_range(0.9..1.1);
304+
let width = self.window.width - self.window.paddle_width;
305+
284306
if self.rng.gen() {
285307
chain![
286308
BALL_X,
287-
keyframes::Space::lazy(Duration::ZERO),
288-
keyframes::Space::new(Speed::per_secs(speed))
289-
.width(self.window.width - self.window.paddle_width),
290-
keyframes::Space::new(Speed::per_secs(speed)).width(0.),
291-
keyframes::Space::lazy(Speed::per_secs(speed))
309+
lazy(Duration::ZERO),
310+
space(Speed::per_secs(speed)).width(width),
311+
space(Speed::per_secs(speed)).width(0.),
312+
lazy(Speed::per_secs(speed))
292313
]
293-
.loop_forever()
294314
} else {
295315
chain![
296316
BALL_X,
297-
keyframes::Space::lazy(Duration::ZERO),
298-
keyframes::Space::new(Speed::per_secs(speed)).width(0.),
299-
keyframes::Space::new(Speed::per_secs(speed))
300-
.width(self.window.width - self.window.paddle_width),
301-
keyframes::Space::lazy(Speed::per_secs(speed))
317+
lazy(Duration::ZERO),
318+
space(Speed::per_secs(speed)).width(0.),
319+
space(Speed::per_secs(speed)).width(width),
320+
lazy(Speed::per_secs(speed))
302321
]
303-
.loop_forever()
304322
}
323+
.loop_forever()
324+
.into()
305325
}
306326
}

0 commit comments

Comments
 (0)