版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Chapter 15,A Module of Reactive Animations,Motivation,The design of animations in Chapter 13 is elegant, and in fact has the feel of a small domain-specific language (DSL), embedded in Haskell. However, the language lacks reactivity:the ability to interact with the user or other external stimuli. In
2、 this chapter we add reactivity, and call the resulting DSL functional animation language, or FAL. In addition, an implementation of FAL is described, using streams,FAL by Example,As before, we use the polymorphic data type “Behavior” to capture time-varying values. For example:color1 : Behavior Col
3、orcolor1 = red untilB (lbp - blue)ball1 : Behavior Pictureball1 = paint color1 circcirc : Behavior Regioncirc = translate (cos time, sin time) (ell 0.2 0.2) The function “untilB” reflects reactive behavior, and “lbp” corresponds to a left button press,More Reactivity,Recursive reactivity:color1r = r
4、ed untilB lbp -blue untilB lbp -color1r Choice reactivity:color2 = red untilB(lbp - blue) .|. (key - yellow) Recursive, choice reactivity:color2r = red untilB colorEvent where colorEvent = (lbp - blue untilB colorEvent) .|. (key - yellow untilB colorEvent) Pushing recursion into combinator:color2h =
5、 red switch (lbp - blue) .|. (key - yellow,Events With Data,Convert button-press events into color events:color1h = red switch (lbp withElem_ cycle blue, red) Dispatch on key press:color3 = white switch (key = c - case c of R - redB - blueY - yellow _ - white ) Carrying state forward:color4 = white
6、switch (key snapshot color4) = (c, old) - case c of R - redB - blueY - yellow _ - lift0 old,Dynamic Events,Not all events are external. For example: while (time * 42) generates no events until time exceeds 42, and then generates events “infinitely often”. when (time * 42)generates exactly one event
7、when the time exceeds 42. color5 = red untilB (when (time * 5) - blue,Integration,The position of a mass under the influence of an accelerating force f:s, v : Behavior Floats = s0 + integral vv = v0 + integral f Combining with reactivity, a bouncing ball:ball2 = paint red (translate (x,y) (ell 0.2 0
8、.2)whereg = -4x = -3 + integral 0.5y = 1.5 + integral vv = integral g switch (hit snapshot_ v = v- lift0 (-v) + integral g)hit = when (y * -1.5,Note similarity to mathematical equations,Implementing FAL,Previously a behavior was conceptually a function:Behavior a Time - a But somehow we must now int
9、roduce events. One obvious approach would be:Behavior a (UserAction, Time) - Time - aBut this would be very inefficient (why?). Better to do this:Behavior a (UserAction, Time) - Time - a Or, even more efficient, and now as Haskell code:newtype Behavior a = Behavior ( (Maybe UserAction, Time) - a )(s
10、ee text for definition of UserAction,Time and Constants,Recall:newtype Behavior a = Behavior ( (Maybe UserAction, Time) - a ) With this representation, lets define time:time : Behavior Timetime = Behavior (_,ts) - ts) Constant behaviors are achieved via lifting:constB : a - Behavior aconstB x = Beha
11、vior (_ - repeat x) For example:red, blue : Behavior Colorred = constB Redblue = constB Blue,Curried Liftings,From this “lifted” version of application:($*) : Beh (a-b) - Beh a - Beh bBeh ff $* Beh fb = Beh (uts - zipWith ($) (ff uts) (fb uts) and the constant lifting operator:lift0 : a - Beh alift0
12、 = constB all other lifting operators can be defined:lift1 : (a - b) - (Beh a - Beh b)lift1 f b1 = lift0 f $* b1lift2 : (a - b - c) - (Beh a - Beh b - Beh c)lift2 f b1 b2 = lift1 f b1 $* b2lift3 : (a - b - c - d) - (Beh a - Beh b - Beh c - Beh d)lift3 f b1 b2 b3 = lift2 f b1 b2 $* b3,For conciseness
13、, “Beh” is used instead of “Behavior”.,Sample Liftings,pairB : Behavior a - Behavior b - Behavior (a,b)pairB = lift2 (,)fstB : Behavior (a,b) - Behavior afstB = lift1 fstpaint : Behavior Color - Behavior Region - Behavior Picturepaint = lift2 Regionred, blue, yellow, green, white, black : Behavior C
14、olorred = lift0 Redblue = lift0 Blue. . .shape : Behavior Shape - Behavior Regionshape = lift1 Shapeell, rec : Behavior Float - Behavior Float - Behavior Regionell x y = shape (lift2 Ellipse x y) rec x y = shape (lift2 Rectangle x y) See text for more liftings,Events and Reactivity,Abstractly, we ca
15、n think of events as:type Event a = Behavior (Maybe a) But for type safety, this is better:newtype Event a = Event ( (Maybe UserAction, Time) - Maybe a ) Core of FALs reactivity:untilB: Behavior a - Event (Behavior a) - Behavior aswitch : Behavior a - Event (Behavior a) - Behavior a(-) : Event a - b
16、 - Event b(=) : Event a - (a-b) - Event bplus primitive events such as:lbp : Event (,Primitive Events,lbp” must look for a “left button press” in the stream of UserActions:lbp : Event ( )lbp = Event (uas,_) - map getlbp uas) where getlbp (Just (Button _ True True) = Just ( ) getlbp _ = Nothing Simil
17、arly for “key”:key : Event Charkey = Event (uas,_) - map getkey uas) where getkey (Just (Key ch True) = Just ch getkey _= Nothing,Implementing UntilB,untilB switches into a new behavior carried by the event. untilB: Behavior a - Event (Behavior a) - Behavior a Behavior fb untilB Event fe =memoB $ Be
18、havior (uts(us,ts) - loop us ts (fe uts) (fb uts)where loop (_:us) (_:ts) (e:es) (b:bs) = b : case e ofNothing - loop us ts es bsJust (Behavior fb) - fb (us,ts) memoB : Behavior a - Behavior a memoB (Behavior fb) = Behavior (memo1 fb) Stare at this code until you understand it completely! The defini
19、tion of “switch” is very similar (see text,Event Map,Recall:color1 : Behavior Colorcolor1 = red untilB (lbp - blue)What does “-” do? Consider types:red, blue : Behavior ColoruntilB : Behavior Color - Event (Behavior Color) - Behavior Colorlbp : Event ( )(-) : Event ( ) - Behavior Color - Event (Beha
20、vior Color) So (-) somehow “tags” an event with a Behavior. Polymorphically speaking:(-) : Event a - b - Event b It is actually a special case of the more general:(=) : Event a - (a-b) - Event b,Implementing Event Map,) is defined as:Event fe = f = Event (uts - map aux (fe uts)whereaux (Just a)= Jus
21、t (f a)aux Nothing= Nothing Which can be defined more succinctly using fmap from the Functor class (discussed in Chapter 18!):Event fe = f = Event (map (fmap f) . fe) (-) is then defined in terms of (=):e - v = e = _ - v,ImplementingPredicate Events,while” is defined as:while : Behavior Bool - Event
22、 ()while (Behavior fb) = Event (uts - map aux (fb uts)where aux True= Just ()aux False = Nothing “when” is defined similarly (see text,Implementing Integration,integral” is defined by:integral : Behavior Float - Behavior Floatintegral (Behavior fb) =Behavior (uts(us,t:ts) - 0 : loop t 0 ts (fb uts)w
23、here loop t0 acc (t1:ts) (a:as) = let acc = acc + (t1-t0)*a in acc : loop t1 acc ts as This corresponds to the standard definition of integration as a limit in calculus (see text,Steppers,Steppers” are convenient variations of switch:step : a - Event a - Behavior aa step e = constB a switch e = cons
24、tBstepAccum : a - Event (a-a) - Behavior aa stepAccum e = b where b = a step (e snapshot b = uncurry ($) For example, a counter:counter = 0 stepAccum lbp - (+1)an example involving step is on the next slide,Mouse Movement,Its convenient to treat mouse position as a pair of Behaviors:mouse : (Behavior Float, Behavior Float)mouse = (fstB m, sndB m)where m = (0,0) step mm where “mm” is defined as:mm : Event Coordinatemm = Event (uas,_) - map getmm uas)where getmm (Just (MouseMove pt) =Just (gPtT
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024-2029年中国泵喷嘴行业市场现状分析及竞争格局与投资发展研究报告
- 2024-2029年中国注射抗凝剂行业市场现状分析及竞争格局与投资发展研究报告
- 2024-2029年中国泡沫塑料板材行业市场发展现状及前景趋势与投资研究报告
- 2024-2029年中国油菜脱粒机行业发展分析及发展前景与投资研究报告
- 2024-2029年中国油气管道和运输自动化行业市场现状供需分析及市场深度研究发展前景及规划战略投资分析研究报告
- 2024-2029年中国汽车零部件清洗系统行业市场现状供需分析及市场深度研究发展前景及规划战略投资分析研究报告
- 2024-2029年中国汽车金融行业深度分析及发展战略研究咨询报告
- 2024-2029年中国汽车调温器行业发展趋势及发展前景研究报告
- 2024国际合作办学协议书范本
- 2024-2029年中国汽车清洁剂行业市场发展运行及发展趋势与投资前景研究报告
- 角膜移植术后高眼压的护理与控制
- 2023-2024学年成都市金牛区九年级上英语(一诊)期末考试题(含答案)
- 消防战斗服穿戴培训课件
- 复烤厂消防培训课件
- 新生儿颅内出血的护理查房课件
- 生产车间新员工入职培训ppt
- 小学生情绪管理教育
- 健康咨询服务合同
- 幼儿园中班音乐《不认输的小火车》
- 医院消防安全管理手册
- 艺术概论第八章《综合艺术》
评论
0/150
提交评论