@@ -5,7 +5,7 @@ use iced::widget::{column, container, row, Space};
55use iced:: { executor, Application , Command , Event , Length , Settings , Subscription } ;
66use 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
1010use once_cell:: sync:: Lazy ;
1111use rand:: prelude:: * ;
@@ -15,10 +15,10 @@ mod theme;
1515use layer:: Layer ;
1616use 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
2323pub fn main ( ) -> iced:: Result {
2424 Pong :: run ( Settings :: default ( ) )
@@ -185,122 +185,142 @@ impl Application for Pong {
185185}
186186
187187impl 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