Skip to content

Commit 41d5a06

Browse files
committed
examples/pong: Do not animate on repeat key presses
1 parent dd724b1 commit 41d5a06

File tree

1 file changed

+51
-30
lines changed

1 file changed

+51
-30
lines changed

examples/pong/src/main.rs

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ struct Pong {
2929
window: Window,
3030
in_play: bool,
3131
rng: ThreadRng,
32+
left: Direction,
33+
right: Direction,
3234
}
3335

3436
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -73,6 +75,8 @@ impl Application for Pong {
7375
window: Window::default(),
7476
rng: thread_rng(),
7577
in_play: false,
78+
left: Direction::Up,
79+
right: Direction::Up,
7680
},
7781
Command::none(),
7882
)
@@ -129,7 +133,12 @@ impl Application for Pong {
129133
.set_chain(vertical_bounce)
130134
.set_chain(horizontal_bounce);
131135
}
132-
self.timeline.set_chain(animation).start();
136+
if let Some(a) = animation {
137+
self.timeline.set_chain(a)
138+
} else {
139+
&mut self.timeline
140+
}
141+
.start();
133142
}
134143
Message::WindowResized(width, height) => {
135144
let width = width as f32;
@@ -141,6 +150,7 @@ impl Application for Pong {
141150
paddle_height: height * 0.2,
142151
};
143152

153+
self.in_play = false;
144154
let x = self.init_ball_x();
145155
let y = self.init_ball_y();
146156
self.timeline.set_chain(x).set_chain(y).start();
@@ -181,38 +191,49 @@ impl Application for Pong {
181191
}
182192

183193
impl Pong {
184-
fn anim_left(&mut self, direction: Direction) -> cosmic_time::space::Chain {
185-
match direction {
186-
Direction::Down => cosmic_time::space::Chain::new(PADDLE_LEFT.clone())
187-
// OOh here are the lazy keyframes!
188-
// This means that this animation will start at wherever the previous
189-
// animation left off!
190-
// Lazy still takes a duration, this will usually be `Duration::ZERO`
191-
// like regular animations, but you can put them anywhere in your
192-
// animation chain. Meaning that you would have an animation start
193-
// at the previous animations's interupted location, animate to elsewhere,
194-
// then go back to that spot!
195-
.link(keyframes::Space::lazy(Duration::ZERO))
196-
.link(
197-
keyframes::Space::new(Speed::per_secs(100.))
198-
.height(self.window.height - 100.),
199-
),
200-
Direction::Up => cosmic_time::space::Chain::new(PADDLE_LEFT.clone())
201-
.link(keyframes::Space::lazy(Duration::ZERO))
202-
.link(keyframes::Space::new(Speed::per_secs(100.)).height(0.)),
194+
fn anim_left(&mut self, direction: Direction) -> Option<cosmic_time::space::Chain> {
195+
if self.left != direction {
196+
self.left = direction;
197+
Some(match direction {
198+
Direction::Down => cosmic_time::space::Chain::new(PADDLE_LEFT.clone())
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+
.link(keyframes::Space::lazy(Duration::ZERO))
208+
.link(
209+
keyframes::Space::new(Speed::per_secs(100.))
210+
.height(self.window.height - 100.),
211+
),
212+
Direction::Up => cosmic_time::space::Chain::new(PADDLE_LEFT.clone())
213+
.link(keyframes::Space::lazy(Duration::ZERO))
214+
.link(keyframes::Space::new(Speed::per_secs(100.)).height(0.)),
215+
})
216+
} else {
217+
None
203218
}
204219
}
205220

206-
fn anim_right(&mut self, direction: Direction) -> cosmic_time::space::Chain {
207-
match direction {
208-
Direction::Down => cosmic_time::space::Chain::new(PADDLE_RIGHT.clone())
209-
.link(keyframes::Space::lazy(Duration::ZERO))
210-
.link(
211-
keyframes::Space::new(Speed::per_secs(100.)).height(self.window.height - 100.),
212-
),
213-
Direction::Up => cosmic_time::space::Chain::new(PADDLE_RIGHT.clone())
214-
.link(keyframes::Space::lazy(Duration::ZERO))
215-
.link(keyframes::Space::new(Speed::per_secs(100.)).height(0.)),
221+
fn anim_right(&mut self, direction: Direction) -> Option<cosmic_time::space::Chain> {
222+
if self.right != direction {
223+
self.right = direction;
224+
Some(match direction {
225+
Direction::Down => cosmic_time::space::Chain::new(PADDLE_RIGHT.clone())
226+
.link(keyframes::Space::lazy(Duration::ZERO))
227+
.link(
228+
keyframes::Space::new(Speed::per_secs(100.))
229+
.height(self.window.height - 100.),
230+
),
231+
Direction::Up => cosmic_time::space::Chain::new(PADDLE_RIGHT.clone())
232+
.link(keyframes::Space::lazy(Duration::ZERO))
233+
.link(keyframes::Space::new(Speed::per_secs(100.)).height(0.)),
234+
})
235+
} else {
236+
None
216237
}
217238
}
218239

0 commit comments

Comments
 (0)