Skip to content

Commit a315337

Browse files
authored
Merge pull request #8 from pop-os/lazy
Lazy KeyFrames + Speed Controlled Keyframes + Pong Example
2 parents 66685b0 + a7fb8e5 commit a315337

File tree

22 files changed

+1695
-688
lines changed

22 files changed

+1695
-688
lines changed

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ TODOs before release:
1818
- [ ] Add `Cosmic` cargo feature for compatibility with both iced and System76's temporary fork.
1919
- [x] optimize for `as_subscription` logic
2020
- [x] Add pause for animations
21+
- [ ] Lazy keyframes. (Keyframes that can use the position of a previous (active or not) animation to start another animation.)
2122

2223
Other TODOs:
2324
- [x] test for easing

examples/counter/src/main.rs

Lines changed: 22 additions & 34 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, 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,42 +34,30 @@ 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
4041
// timeline Struct and the "timeline" itself here.
4142
// Though more complicated applications will likely do this in the `update`
4243
let mut timeline = Timeline::new();
43-
let animation = cosmic_time::container::Chain::new(CONTAINER.clone())
44-
// .loop_forever() // Uncomment this line to loop the animation!
45-
.link(
46-
keyframes::Container::new(Duration::ZERO)
47-
.width(Length::Fixed(0.))
48-
.height(Length::Fixed(100.)),
49-
)
50-
.link(
51-
keyframes::Container::new(Duration::from_secs(2))
52-
.width(Length::Fixed(200.))
53-
.height(Length::Fixed(100.)),
54-
)
55-
.link(
56-
keyframes::Container::new(Duration::from_secs(4))
57-
.width(Length::Fixed(200.))
58-
.height(Length::Fixed(300.))
59-
.padding([0, 0, 0, 0]),
60-
)
61-
.link(
62-
keyframes::Container::new(Duration::from_secs(6))
63-
.width(Length::Fixed(700.))
64-
.height(Length::Fixed(300.))
65-
.padding([0, 0, 0, 500]),
66-
)
67-
.link(
68-
keyframes::Container::new(Duration::from_secs(8))
69-
.width(Length::Fixed(150.))
70-
.height(Length::Fixed(150.))
71-
.padding([0, 0, 0, 0]),
72-
);
44+
let animation = chain![
45+
CONTAINER,
46+
container(Duration::ZERO).width(0.).height(100.),
47+
container(Duration::from_secs(2)).width(200.).height(100.),
48+
container(Duration::from_secs(2))
49+
.width(200.)
50+
.height(300.)
51+
.padding([0, 0, 0, 0]),
52+
container(Duration::from_secs(2))
53+
.width(700.)
54+
.height(300.)
55+
.padding([0, 0, 0, 500]),
56+
container(Duration::from_secs(2))
57+
.width(150.)
58+
.height(150.)
59+
.padding([0, 0, 0, 0]),
60+
];
7361

7462
// Notice how we had to specify the start and end of the widget dimensions?
7563
// Iced's default values for widgets are usually not animatable, because
@@ -128,8 +116,8 @@ impl Application for Counter {
128116
// just define the width with a `width` method like any other widget, then
129117
// animate the height in your view! Only control the animatable values with
130118
// cosmic-time, all others should be in your view!
131-
keyframes::Container::as_widget(
132-
CONTAINER.clone(),
119+
anim!(
120+
CONTAINER,
133121
&self.timeline,
134122
column![
135123
button("Increment")

examples/pokedex/src/main.rs

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

88
use cosmic_time::{
9-
self, keyframes, Back, Bounce, Circular, Ease, Elastic, Exponential, Linear, Quadratic,
9+
self, anim, chain, id, Back, Bounce, Circular, Ease, Elastic, Exponential, Linear, Quadratic,
1010
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),
@@ -44,6 +44,7 @@ pub fn main() -> iced::Result {
4444
}
4545

4646
#[derive(Debug)]
47+
#[allow(clippy::large_enum_variant)]
4748
enum Pokedex {
4849
Loading,
4950
Loaded { pokemon: Pokemon },
@@ -164,7 +165,7 @@ impl Pokemon {
164165
fn view(&self) -> Element<Message> {
165166
row![
166167
column![
167-
keyframes::Space::as_widget(SPACE.clone(), &self.timeline),
168+
anim!(SPACE, &self.timeline),
168169
image::viewer(self.image.clone())
169170
],
170171
column![
@@ -180,13 +181,14 @@ impl Pokemon {
180181
]
181182
.spacing(20),
182183
]
183-
.height(Length::Fixed(400.))
184+
.height(400.)
184185
.spacing(20)
185186
.align_items(Alignment::Center)
186187
.into()
187188
}
188189

189190
async fn search() -> Result<Pokemon, Error> {
191+
use cosmic_time::space;
190192
use rand::Rng;
191193
use serde::Deserialize;
192194

@@ -245,21 +247,18 @@ impl Pokemon {
245247
EASE_IN[rand], EASE_OUT[rand]
246248
);
247249

248-
let animation = cosmic_time::space::Chain::new(SPACE.clone())
249-
.link(keyframes::Space::new(Duration::ZERO).height(Length::Fixed(50.)))
250-
.link(
251-
keyframes::Space::new(Duration::from_millis(1500))
252-
.height(Length::Fixed(250.))
253-
.ease(EASE_IN[rand]),
254-
)
255-
.link(
256-
keyframes::Space::new(Duration::from_millis(3000))
257-
.height(Length::Fixed(50.))
258-
.ease(EASE_OUT[rand]),
259-
)
260-
.loop_forever();
261-
262-
// println!("Animating wth ease: {ease:?}"); // Uncomment to see the type of easing
250+
let animation = chain![
251+
SPACE,
252+
space(Duration::ZERO).height(50.),
253+
space(Duration::from_millis(1500))
254+
.height(250.)
255+
.ease(EASE_IN[rand]),
256+
space(Duration::from_millis(3000))
257+
.height(50.)
258+
.ease(EASE_OUT[rand])
259+
]
260+
.loop_forever();
261+
263262
timeline.set_chain(animation).start();
264263

265264
Ok(Pokemon {

examples/pong/Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "pong"
3+
version = "0.1.0"
4+
authors = ["Brock Szuszczewicz <[email protected]>"]
5+
edition = "2021"
6+
publish = false
7+
8+
[dependencies]
9+
iced = "0.8.0"
10+
iced_native = "0.9.1"
11+
cosmic-time = { path = "../.."}
12+
once_cell = "1.17"
13+
rand = "0.8.5"

0 commit comments

Comments
 (0)