-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path11-4-turn-differences-into-methods.rkt
executable file
·846 lines (626 loc) · 22.5 KB
/
11-4-turn-differences-into-methods.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
#lang racket
;; 11-4-turn-differences-into-methods.rkt
;; local functions in the subclasses weren't accessible from the
;; superclass.
;; So turn them into methods, and call them with 'this'
;; We'll clean up a bit as we go, so we can see what we're doing.
;; 11-3-unify-try1.rkt
;; Factor common parts of implementation of Ball% and Square%
;; into a superclass DraggableWidget%
;; We'll do this by simply copying Ball% to DraggableWidget% and
;; commenting out the parts that differ between Ball% and Square%.
;; 11-2-squares.rkt
;; adds squares ("s")
;; 11-1-flashing-balls.rkt :
;; extends 10-6-push-model by adding a flashing ball ("f")
(require rackunit)
(require 2htdp/universe)
(require 2htdp/image)
(require "extras.rkt")
;; start with (run framerate). Typically: (run 0.25)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; CONSTANTS
(define CANVAS-WIDTH 400)
(define CANVAS-HEIGHT 300)
(define EMPTY-CANVAS (empty-scene CANVAS-WIDTH CANVAS-HEIGHT))
(define INIT-BALL-X (/ CANVAS-HEIGHT 2))
(define INIT-BALL-Y (/ CANVAS-WIDTH 3))
(define INIT-BALL-SPEED 25)
(define INITIAL-WALL-POSITION 300)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Data Definitions
;; A Widget is an object whose class implements Widget<%>
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; INTERFACES
;; The World implements the StatefulWorld<%> interface
(define StatefulWorld<%>
(interface ()
; -> Void
; GIVEN: no arguments
; EFFECT: updates this world to its state after a tick
after-tick
; Integer Integer MouseEvent-> Void
; GIVEN: a location
; EFFECT: updates this world to the state that should follow the
; given mouse event at the given location.
after-mouse-event
; KeyEvent : KeyEvent -> Void
; GIVEN: a key event
; EFFECT: updates this world to the state that should follow the
; given key event
after-key-event
; -> Scene
; GIVEN: a scene
; RETURNS: a scene that depicts this World
to-scene
; Widget -> Void
; GIVEN: A widget
; EFFECT: add the given widget to the world
add-widget
; SWidget -> Void
; GIVEN: A stateful widget
; EFFECT: add the given widget to the world
add-stateful-widget
))
;; Every functional object that lives in the world must implement the
;; Widget<%> interface.
(define Widget<%>
(interface ()
; -> Widget
; GIVEN: no arguments
; RETURNS: the state of this object that should follow at time t+1.
after-tick
; Integer Integer -> Widget
; GIVEN: a location
; RETURNS: the state of this object that should follow the
; specified mouse event at the given location.
after-button-down
after-button-up
after-drag
; KeyEvent : KeyEvent -> Widget
; GIVEN: a key event and a time
; RETURNS: the state of this object that should follow the
; given key event
after-key-event
; Scene -> Scene
; GIVEN: a scene
; RETURNS: a scene like the given one, but with this object
; painted on it.
add-to-scene
))
;; Every stable (stateful) object that lives in the world must implement the
;; SWidget<%> interface.
(define SWidget<%>
(interface ()
; -> Void
; GIVEN: no arguments
; EFFECT: updates this widget to the state it should have
; following a tick.
after-tick
; Integer Integer -> Void
; GIVEN: a location
; EFFECT: updates this widget to the state it should have
; following the specified mouse event at the given location.
after-button-down
after-button-up
after-drag
; KeyEvent : KeyEvent -> Void
; GIVEN: a key event
; EFFECT: updates this widget to the state it should have
; following the given key event
after-key-event
; Scene -> Scene
; GIVEN: a scene
; RETURNS: a scene like the given one, but with this object
; painted on it.
add-to-scene
))
;; while we're at it, we'll rename the interfaces to reflect their
;; generic nature.
;; Additional method for Ball and other classes that receive messages
;; from the wall:
(define SWidgetListener<%>
(interface (SWidget<%>)
; Int -> Void
; EFFECT: updates the ball's cached value of the wall's position
update-wall-pos
))
;; Additional method for Wall:
(define SWidgetPublisher<%>
(interface (SWidget<%>)
; SWidgetListener<%> -> Int
; GIVEN: An SWidgetListener<%>
; EFFECT: registers the listener to receive position updates from this wall.
; RETURNS: the current x-position of the wall
register
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; initial-world : -> WorldState
;; RETURNS: a world with a wall, a ball, and a factory
(define (initial-world)
(local
((define the-wall (new Wall%))
(define the-ball (new Ball% [w the-wall]))
(define the-world
(make-world-state
empty ; (list the-ball) -- the ball is now stateful
(list the-wall)))
(define the-factory
(new WidgetFactory% [wall the-wall][world the-world])))
(begin
;; put the factory in the world
(send the-world add-stateful-widget the-factory)
;; tell the factory to start a ball
(send the-factory after-key-event "b")
the-world)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; run : PosReal -> World
; GIVEN: a frame rate, in secs/tick
; EFFECT: runs an initial world at the given frame rate
; RETURNS: the world in its final state of the world
; Note: the (begin (send w ...) w) idiom
(define (run rate)
(big-bang (initial-world)
(on-tick
(lambda (w) (begin (send w after-tick) w))
rate)
(on-draw
(lambda (w) (send w to-scene)))
(on-key
(lambda (w kev)
(begin
(send w after-key-event kev)
w)))
(on-mouse
(lambda (w mx my mev)
(begin
(send w after-mouse-event mx my mev)
w)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; The World% class
; ListOfWidget ListOfWidget -> WorldState
(define (make-world-state objs sobjs)
(new WorldState% [objs objs][sobjs sobjs]))
(define WorldState%
(class* object% (StatefulWorld<%>)
(init-field objs) ; ListOfWidget
(init-field sobjs) ; ListOfSWidget
(super-new)
(define/public (add-widget w)
(set! objs (cons w objs)))
(define/public (add-stateful-widget w)
(set! sobjs (cons w sobjs)))
;; (Widget or SWidget -> Void) -> Void
(define (process-widgets fn)
(begin
(set! objs (map fn objs))
(for-each fn sobjs)))
;; after-tick : -> Void
;; Use map on the Widgets in this World; use for-each on the
;; stateful widgets
(define/public (after-tick)
(process-widgets
(lambda (obj) (send obj after-tick))))
;; to-scene : -> Scene
;; Use HOFC foldr on the Widgets and SWidgets in this World
;; Note: the append is inefficient, but clear..
(define/public (to-scene)
(foldr
(lambda (obj scene)
(send obj add-to-scene scene))
EMPTY-CANVAS
(append objs sobjs)))
;; after-key-event : KeyEvent -> WorldState
;; STRATEGY: Pass the KeyEvents on to the objects in the world.
(define/public (after-key-event kev)
(process-widgets
(lambda (obj) (send obj after-key-event kev))))
;; world-after-mouse-event : Nat Nat MouseEvent -> WorldState
;; STRATGY: Cases on mev
(define/public (after-mouse-event mx my mev)
(cond
[(mouse=? mev "button-down")
(world-after-button-down mx my)]
[(mouse=? mev "drag")
(world-after-drag mx my)]
[(mouse=? mev "button-up")
(world-after-button-up mx my)]
[else this]))
;; the next few functions are local functions, not in the interface.
(define (world-after-button-down mx my)
(process-widgets
(lambda (obj) (send obj after-button-down mx my))))
(define (world-after-button-up mx my)
(process-widgets
(lambda (obj) (send obj after-button-up mx my))))
(define (world-after-drag mx my)
(process-widgets
(lambda (obj) (send obj after-drag mx my))))
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; The WidgetFactory% class
;; accepts key events and adds SWidgetListeners to the world.
;; gets the world as an init-field
(define WidgetFactory%
(class* object% (SWidget<%>)
(init-field world) ; the world to which the factory adds balls
(init-field wall) ; the wall that the new balls should bounce
; off of.
(super-new)
; KeyEvent -> Void
(define/public (after-key-event kev)
(cond
[(key=? kev "b")
(send world add-stateful-widget (new Ball% [w wall]))]
[(key=? kev "f")
(send world add-stateful-widget (new FlashingBall% [w wall]))]
[(key=? kev "s")
(send world add-stateful-widget (new Square% [w wall]))]
))
;; the Ball Factory has no other behavior
(define/public (after-tick) this)
(define/public (after-button-down mx my) this)
(define/public (after-button-up mx my) this)
(define/public (after-drag mx my) this)
(define/public (add-to-scene s) s)
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; A DraggableWidget is a (new DraggableWidget% [w Wall])
;; the methods implemented by DraggableWidget%.
;; This includes all the methods in SWidgetListener<%> EXCEPT
;; for add-to-scene, which is defined in the subclass
(define DraggableWidget<%>
(interface ()
update-wall-pos
after-tick
after-button-down
after-button-up
after-drag))
(define DraggableWidget%
(class* object%
;; the methods implemented in the superclass
(DraggableWidget<%>)
;; the methods to be supplied by each subclass
(abstract
next-x-pos
next-speed
in-this?)
;; the Wall that the ball should bounce off of
(init-field w)
;; initial values of x, y (center of ball)
(init-field [x INIT-BALL-X])
(init-field [y INIT-BALL-Y])
(init-field [speed INIT-BALL-SPEED])
; is this selected? Default is false.
(init-field [selected? false])
;; if this is selected, the position of
;; the last button-down event inside this, relative to the
;; object's center. Else any value.
(init-field [saved-mx 0] [saved-my 0])
;; register this ball with the wall, and use the result as the
;; initial value of wall-pos
(field [wall-pos (send w register this)])
(super-new)
;; Int -> Void
;; EFFECT: updates the widget's idea of the wall's position to the
;; given integer.
(define/public (update-wall-pos n)
(set! wall-pos n))
;; after-tick : -> Void
;; state of this ball after a tick. A selected widget doesn't move.
(define/public (after-tick)
(if selected?
this
(let ((x1 (send this next-x-pos))
(speed1 (send this next-speed)))
(begin
(set! speed speed1)
(set! x x1)))))
; after-button-down : Integer Integer -> Void
; GIVEN: the location of a button-down event
; STRATEGY: Cases on whether the event is in this
(define/public (after-button-down mx my)
(if
;; (in-this? mx my)
(send this in-this? mx my)
(begin
(set! selected? true)
(set! saved-mx (- mx x))
(set! saved-my (- my y)))
this))
; after-button-up : Integer Integer -> Void
; GIVEN: the location of a button-up event
; STRATEGY: Cases on whether the event is in this
; If this is selected, then unselect it.
(define/public (after-button-up mx my)
(if
;; (in-this? mx my)
(send this in-this? mx my)
(set! selected? false)
this))
; after-drag : Integer Integer -> Void
; GIVEN: the location of a drag event
; STRATEGY: Cases on whether the ball is selected.
; If it is selected, move it so that the vector from the center to
; the drag event is equal to (mx, my)
(define/public (after-drag mx my)
(if selected?
(begin
(set! x (- mx saved-mx))
(set! y (- my saved-my)))
this))
;; the widget ignores key events
(define/public (after-key-event kev) this)
(define/public (for-test:x) x)
(define/public (for-test:speed) speed)
(define/public (for-test:wall-pos) wall-pos)
(define/public (for-test:next-speed) (next-speed))
(define/public (for-test:next-x) (next-x-pos))
))
;; Hooks left over: these methods must be filled in from subclass.
(define DraggableWidgetHooks<%>
(interface ()
;; Int Int -> Boolean
;; is the given location in this widget?
in-this?
;; -> Int
;; RETURN: the next x position or speed of this widget
next-x-pos
next-speed
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; The Ball% class
;; A Ball is a (new Ball% [w Wall])
(define Ball%
(class*
;; inherit method implementations from DraggableWidget%
DraggableWidget%
;; must implement SBall + the open hooks from the superclass
(SWidgetListener<%> DraggableWidgetHooks<%>)
;; inherit all these fields from the superclass:
;; the Wall that the ball should bounce off of
(inherit-field w)
;; initial values of x, y (center of ball) and speed:
(inherit-field x y speed)
; is this selected? Default is false.
(inherit-field selected?)
;; position of the wall, updated by update-wall-pos
(inherit-field wall-pos)
; don't need to inherit these: all dragging is handled in the superclass
;(inherit-field [saved-mx 0] [saved-my 0])
;; this field is local to Ball%
(field [radius 20])
(super-new)
;; make this a method instead of a function:
;; -> Integer
;; position of the ball at the next tick
;; STRATEGY: use the ball's cached copy of the wall position to
;; set the upper limit of motion
(define/override (next-x-pos)
(limit-value
radius
(+ x speed)
(- wall-pos radius)))
;; Number^3 -> Number
;; WHERE: lo <= hi
;; RETURNS: val, but limited to the range [lo,hi]
(define (limit-value lo val hi)
(max lo (min val hi)))
;; make this a method instead of a function:
;; -> Integer
;; RETURNS: the velocity of the ball at the next tick
;; STRATEGY: if the ball will not be at its limit, return it
;; unchanged. Otherwise, negate the velocity.
(define/override (next-speed)
(if
(< radius (next-x-pos) (- wall-pos radius))
speed
(- speed)))
(define/public (add-to-scene s)
(place-image
(circle radius
"outline"
"red")
x y s))
;; in-this? : Integer Integer -> Boolean
;; GIVEN: a location on the canvas
;; RETURNS: true iff the location is inside this.
(define/override (in-this? other-x other-y)
(<= (+ (sqr (- x other-x)) (sqr (- y other-y)))
(sqr radius)))
))
;; unit tests for ball:
(begin-for-test
(local
((define wall1 (new Wall% [pos 200]))
(define ball1 (new Ball% [x 110][speed 50][w wall1])))
(check-equal? (send ball1 for-test:speed) 50)
(check-equal? (send ball1 for-test:wall-pos) 200)
(check-equal? (send ball1 for-test:next-speed) 50)
(check-equal? (send ball1 for-test:next-x) 160)
(send ball1 after-tick)
(check-equal? (send ball1 for-test:x) 160)
(check-equal? (send ball1 for-test:speed) 50)
(send ball1 after-tick)
(check-equal? (send ball1 for-test:x) 180)
(check-equal? (send ball1 for-test:speed) -50)
))
(begin-for-test
(local
((define wall1 (new Wall% [pos 200]))
(define ball1 (new Ball% [x 160][speed 50][w wall1])))
(check-equal? (send ball1 for-test:x) 160)
(check-equal? (send ball1 for-test:speed) 50)
(check-equal? (send ball1 for-test:next-x) 180)
(check-equal? (send ball1 for-test:next-speed) -50)
(send ball1 after-tick)
(check-equal? (send ball1 for-test:x) 180)
(check-equal? (send ball1 for-test:speed) -50)
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; FlashingBall% is like a Ball%, but it displays differently: it
;; changes color on every fourth tick
(define FlashingBall%
(class* Ball% (SWidgetListener<%>) ;
;; here are fields of the superclass that we need.
;; we should copy the interpretations here so we'll know what they mean.
(inherit-field radius x y selected?)
; how much time between color changes?
(field [color-change-interval 4])
; time left til next color change
(field [time-left color-change-interval])
; the list of possible colors, first elt is current color
(field [colors (list "red" "green")])
;; the value for init-field w is sent to the superclass.
(super-new)
;; FlashingBall% behaves just like Ball%, except for add-to-scene.
;; so we'll find on-tick, on-key, on-mouse methods in Ball%
;; Scene -> Scene
;; RETURNS: a scene like the given one, but with the flashing ball
;; painted on it.
;; EFFECT: decrements time-left and changes colors if necessary
(define/override (add-to-scene s)
(begin
;; is it time to change colors?
(if (zero? time-left)
(change-colors)
(set! time-left (- time-left 1)))
;; now paint yourself on the scene
(place-image
(circle radius
(if selected? "solid" "outline")
(first colors))
x y s)))
;; -> Void
;; EFFECT: rotate the list of colors, and reset time-left
(define (change-colors)
(set! colors (append (rest colors) (list (first colors))))
(set! time-left color-change-interval))
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; now we'll do the same thing for Square%:
;; Square is like Ball, but it has different geometry.
(define Square%
(class* DraggableWidget%
;; must implement SWidgetListener + the open hooks from the superclass
(SWidgetListener<%> DraggableWidgetHooks<%>)
(inherit-field w) ;; the Wall that the square should bounce off of
;; initial values of x, y (center of square)
(inherit-field x y speed)
; is this selected? Default is false.
(inherit-field selected?)
(inherit-field wall-pos)
;; don't need saved-mx saved-my
;; (inherit-field saved-mx saved-my)
(field [size 40])
(field [half-size (/ size 2)])
(super-new)
;; Square-specific: turn into method
;; -> Integer
;; position of the square at the next tick
;; STRATEGY: use the square's cached copy of the wall position to
;; set the upper limit of motion
(define/override (next-x-pos)
(limit-value
half-size
(+ x speed)
(- wall-pos half-size)))
;; Number^3 -> Number
;; WHERE: lo <= hi
;; RETURNS: val, but limited to the range [lo,hi]
(define (limit-value lo val hi)
(max lo (min val hi)))
;; Square-specific: turn into method
;; -> Integer
;; RETURNS: the velocity of the square at the next tick
;; STRATEGY: if the square will not be at its limit, return it
;; unchanged. Otherwise, negate the velocity.
(define/override (next-speed)
(if
(< half-size (next-x-pos) (- wall-pos half-size))
speed
(- speed)))
(define/public (add-to-scene s)
(place-image
(square size
(if selected? "solid" "outline")
"green")
x y s))
;; square-specific:
;; in-this? : Integer Integer -> Boolean
;; GIVEN: a location on the canvas
;; RETURNS: true iff the location is inside this.
(define/override (in-this? other-x other-y)
(and
(<= (- x half-size) other-x (+ x half-size))
(<= (- y half-size) other-y (+ y half-size))))
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; The Wall% class
;; A Wall is (new Wall% [pos Integer]
;; [saved-mx Integer]
;; [selected? Boolean])
;; all these fields have default values.
(define Wall%
(class* object% (SWidgetPublisher<%>)
(init-field [pos INITIAL-WALL-POSITION]) ; the x position of the wall
; is the wall selected? Default is false.
(init-field [selected? false])
;; if the wall is selected, the x position of
;; the last button-down event near the wall
(init-field [saved-mx 0])
;; the list of registered balls
;; ListOfBall
(field [balls empty])
(super-new)
;; the extra behavior for Wall<%>
;; (define/public (get-pos) pos)
;; Ball<%> -> Int
;; EFFECT: registers the given ball
;; RETURNS: the current position of the wall
(define/public (register b)
(begin
(set! balls (cons b balls))
pos))
; after-button-down : Integer Integer -> Void
; GIVEN: the (x, y) location of a button-down event
; EFFECT: if the event is near the wall, make the wall selected.
; STRATEGY: Cases on whether the event is near the wall
(define/public (after-button-down mx my)
(if (near-wall? mx)
(begin
(set! selected? true)
(set! saved-mx (- mx pos)))
this)) ;; if needs an else clause :-(
; after-button-up : Integer Integer -> Void
; GIVEN: the (x,y) location of a button-up event
; EFFECT: makes the Wall unselected
(define/public (after-button-up mx my)
(set! selected? false))
; after-drag : Integer Integer -> Void
; GIVEN: the location of a drag event
; STRATEGY: Cases on whether the wall is selected.
; If it is selected, move it so that the vector from its position to
; the drag event is equal to saved-mx. Report the new position to
; the registered listeners
(define/public (after-drag mx my)
(if selected?
(begin
(set! pos (- mx saved-mx))
(for-each
(lambda (b) (send b update-wall-pos pos))
balls))
this))
;; add-to-scene : Scene -> Scene
;; RETURNS: a scene like the given one, but with this wall painted
;; on it.
(define/public (add-to-scene scene)
(scene+line scene pos 0 pos CANVAS-HEIGHT "black"))
;; is mx near the wall? We arbitrarily say it's near if its
;; within 5 pixels.
(define (near-wall? mx)
(< (abs (- mx pos)) 5))
;; the wall has no other behaviors
(define/public (after-tick) this)
(define/public (after-key-event kev) this)
))