@@ -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 , keyframes, Duration , Instant , Speed , Timeline } ;
8+ use cosmic_time:: { self , chain , keyframes, Duration , Instant , Speed , Timeline } ;
99
1010use once_cell:: sync:: Lazy ;
1111use rand:: prelude:: * ;
@@ -195,7 +195,8 @@ impl Pong {
195195 if self . left != direction {
196196 self . left = direction;
197197 Some ( match direction {
198- Direction :: Down => cosmic_time:: space:: Chain :: new ( PADDLE_LEFT . clone ( ) )
198+ Direction :: Down => chain ! [
199+ PADDLE_LEFT ,
199200 // OOh here are the lazy keyframes!
200201 // This means that this animation will start at wherever the previous
201202 // animation left off!
@@ -209,14 +210,14 @@ impl Pong {
209210 // The animation is only as granular as your definition in the chain.
210211 // If you animation time is not in exact seconds, I highly recommend
211212 // using a smaller unit.
212- . link ( keyframes:: Space :: lazy ( Duration :: ZERO ) )
213- . link (
214- keyframes :: Space :: new ( Speed :: per_millis ( 0.3 ) )
215- . height ( self . window . height - 100. ) ,
216- ) ,
217- Direction :: Up => cosmic_time :: space :: Chain :: new ( PADDLE_LEFT . clone ( ) )
218- . link ( keyframes:: Space :: lazy ( Duration :: ZERO ) )
219- . link ( keyframes :: Space :: new ( Speed :: per_millis ( 0.3 ) ) . height ( 0. ) ) ,
213+ keyframes:: Space :: lazy( Duration :: ZERO ) ,
214+ keyframes :: Space :: new ( Speed :: per_millis ( 0.3 ) ) . height ( self . window . height - 100. ) ,
215+ ] ,
216+ Direction :: Up => chain ! [
217+ PADDLE_LEFT ,
218+ keyframes :: Space :: lazy ( Duration :: ZERO ) ,
219+ keyframes:: Space :: new ( Speed :: per_millis ( 0.3 ) ) . height ( 0. )
220+ ] ,
220221 } )
221222 } else {
222223 None
@@ -227,15 +228,16 @@ impl Pong {
227228 if self . right != direction {
228229 self . right = direction;
229230 Some ( match direction {
230- Direction :: Down => cosmic_time:: space:: Chain :: new ( PADDLE_RIGHT . clone ( ) )
231- . link ( keyframes:: Space :: lazy ( Duration :: ZERO ) )
232- . link (
233- keyframes:: Space :: new ( Speed :: per_millis ( 0.3 ) )
234- . height ( self . window . height - 100. ) ,
235- ) ,
236- Direction :: Up => cosmic_time:: space:: Chain :: new ( PADDLE_RIGHT . clone ( ) )
237- . link ( keyframes:: Space :: lazy ( Duration :: ZERO ) )
238- . link ( keyframes:: Space :: new ( Speed :: per_millis ( 0.3 ) ) . height ( 0. ) ) ,
231+ Direction :: Down => chain ! [
232+ PADDLE_RIGHT ,
233+ keyframes:: Space :: lazy( Duration :: ZERO ) ,
234+ keyframes:: Space :: new( Speed :: per_millis( 0.3 ) ) . height( self . window. height - 100. ) ,
235+ ] ,
236+ Direction :: Up => chain ! [
237+ PADDLE_RIGHT ,
238+ keyframes:: Space :: lazy( Duration :: ZERO ) ,
239+ keyframes:: Space :: new( Speed :: per_millis( 0.3 ) ) . height( 0. )
240+ ] ,
239241 } )
240242 } else {
241243 None
@@ -245,64 +247,66 @@ impl Pong {
245247 fn init_ball_y ( & mut self ) -> cosmic_time:: space:: Chain {
246248 let min = self . window . height * 0.3 ;
247249 let max = self . window . height - min - self . window . paddle_height ;
248- cosmic_time:: space:: Chain :: new ( BALL_Y . clone ( ) )
249- . link ( keyframes:: Space :: new ( Duration :: ZERO ) . height ( self . rng . gen_range ( min..max) ) )
250+ let height = self . rng . gen_range ( min..max) ;
251+
252+ chain ! [ BALL_Y , keyframes:: Space :: new( Duration :: ZERO ) . height( height) ]
250253 }
251254
252255 fn init_ball_x ( & mut self ) -> cosmic_time:: space:: Chain {
253256 let min = self . window . width * 0.3 ;
254257 let max = self . window . width - min - self . window . paddle_width ;
255- cosmic_time:: space:: Chain :: new ( BALL_X . clone ( ) )
256- . link ( keyframes:: Space :: new ( Duration :: ZERO ) . width ( self . rng . gen_range ( min..max) ) )
258+ let width = self . rng . gen_range ( min..max) ;
259+
260+ chain ! [ BALL_X , keyframes:: Space :: new( Duration :: ZERO ) . width( width) ]
257261 }
258262
259263 fn rand_vertical_bounce ( & mut self ) -> cosmic_time:: space:: Chain {
260264 let speed = 100. * self . rng . gen_range ( 0.9 ..1.1 ) ;
261265 if self . rng . gen ( ) {
262- cosmic_time :: space :: Chain :: new ( BALL_Y . clone ( ) )
263- . link ( keyframes :: Space :: lazy ( Duration :: ZERO ) )
264- . link (
265- keyframes:: Space :: new ( Speed :: per_secs ( speed) )
266- . height ( self . window . height - self . window . paddle_width ) ,
267- )
268- . link ( keyframes:: Space :: new ( Speed :: per_secs ( speed) ) . height ( 0. ) )
269- . link ( keyframes :: Space :: lazy ( Speed :: per_secs ( speed ) ) )
270- . loop_forever ( )
266+ chain ! [
267+ BALL_Y ,
268+ keyframes :: Space :: lazy ( Duration :: ZERO ) ,
269+ keyframes:: Space :: new( Speed :: per_secs( speed) )
270+ . height( self . window. height - self . window. paddle_width) ,
271+ keyframes :: Space :: new ( Speed :: per_secs ( speed ) ) . height ( 0. ) ,
272+ keyframes:: Space :: lazy ( Speed :: per_secs( speed) )
273+ ]
274+ . loop_forever ( )
271275 } else {
272- cosmic_time :: space :: Chain :: new ( BALL_Y . clone ( ) )
273- . link ( keyframes :: Space :: lazy ( Duration :: ZERO ) )
274- . link ( keyframes:: Space :: new ( Speed :: per_secs ( speed ) ) . height ( 0. ) )
275- . link (
276- keyframes:: Space :: new ( Speed :: per_secs ( speed) )
277- . height ( self . window . height - self . window . paddle_width ) ,
278- )
279- . link ( keyframes :: Space :: lazy ( Speed :: per_secs ( speed ) ) )
280- . loop_forever ( )
276+ chain ! [
277+ BALL_Y ,
278+ keyframes:: Space :: lazy ( Duration :: ZERO ) ,
279+ keyframes :: Space :: new ( Speed :: per_secs ( speed ) ) . height ( 0. ) ,
280+ keyframes:: Space :: new( Speed :: per_secs( speed) )
281+ . height( self . window. height - self . window. paddle_width) ,
282+ keyframes :: Space :: lazy ( Speed :: per_secs ( speed ) )
283+ ]
284+ . loop_forever ( )
281285 }
282286 }
283287
284288 fn rand_horizontal_bounce ( & mut self ) -> cosmic_time:: space:: Chain {
285289 let speed = 100. * self . rng . gen_range ( 0.9 ..1.1 ) ;
286290 if self . rng . gen ( ) {
287- cosmic_time :: space :: Chain :: new ( BALL_X . clone ( ) )
288- . link ( keyframes :: Space :: lazy ( Duration :: ZERO ) )
289- . link (
290- keyframes:: Space :: new ( Speed :: per_secs ( speed) )
291- . width ( self . window . width - self . window . paddle_width ) ,
292- )
293- . link ( keyframes:: Space :: new ( Speed :: per_secs ( speed) ) . width ( 0. ) )
294- . link ( keyframes :: Space :: lazy ( Speed :: per_secs ( speed ) ) )
295- . loop_forever ( )
291+ chain ! [
292+ BALL_X ,
293+ keyframes :: Space :: lazy ( Duration :: ZERO ) ,
294+ keyframes:: Space :: new( Speed :: per_secs( speed) )
295+ . width( self . window. width - self . window. paddle_width) ,
296+ keyframes :: Space :: new ( Speed :: per_secs ( speed ) ) . width ( 0. ) ,
297+ keyframes:: Space :: lazy ( Speed :: per_secs( speed) )
298+ ]
299+ . loop_forever ( )
296300 } else {
297- cosmic_time :: space :: Chain :: new ( BALL_X . clone ( ) )
298- . link ( keyframes :: Space :: lazy ( Duration :: ZERO ) )
299- . link ( keyframes:: Space :: new ( Speed :: per_secs ( speed ) ) . width ( 0. ) )
300- . link (
301- keyframes:: Space :: new ( Speed :: per_secs ( speed) )
302- . width ( self . window . width - self . window . paddle_width ) ,
303- )
304- . link ( keyframes :: Space :: lazy ( Speed :: per_secs ( speed ) ) )
305- . loop_forever ( )
301+ chain ! [
302+ BALL_X ,
303+ keyframes:: Space :: lazy ( Duration :: ZERO ) ,
304+ keyframes :: Space :: new ( Speed :: per_secs ( speed ) ) . width ( 0. ) ,
305+ keyframes:: Space :: new( Speed :: per_secs( speed) )
306+ . width( self . window. width - self . window. paddle_width) ,
307+ keyframes :: Space :: lazy ( Speed :: per_secs ( speed ) )
308+ ]
309+ . loop_forever ( )
306310 }
307311 }
308312}
0 commit comments