/usr/share/racket/pkgs/frtime/animation.rkt is in racket-common 6.1-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | (module animation frtime
(require (for-syntax racket/base (only-in racket/function identity))
racket/match
racket/class
(except-in frtime/animation/graphics make-posn posn-x posn-y make-rgb)
(lifted (only-in frtime/animation/graphics posn-x) posn-x)
(lifted (only-in frtime/animation/graphics posn-y) posn-y)
(lifted (only-in frtime/animation/graphics make-posn) make-posn)
(lifted (only-in frtime/animation/graphics make-rgb) make-rgb)
(as-is:unchecked frtime/lang-ext lift)
frtime/frlibs/list
frtime/frlibs/math)
(module test racket/base)
(open-graphics)
(define fresh-anim
(let ([first #t])
(lambda ([x 400] [y 400] [title "Animation - DrRacket"])
(if first
(set! first #f)
(begin
(set! window
(open-viewport title x y))
(set! pixmap
(open-pixmap "" x y))
(set! mouse-pos
(hold ((viewport-mouse-events window)
. ==> .
(lambda (ev) (make-posn
(send ev get-x)
(send ev get-y))))
(query-mouse-posn window)))
(set! key-strokes ((viewport-key-events window) . ==> . sixkey-value))
(set! left-clicks ((viewport-mouse-events window) . =#> . (lambda (ev) (send ev button-down? 'left))))
(set! middle-clicks ((viewport-mouse-events window) . =#> . (lambda (ev) (send ev button-down? 'middle))))
(set! right-clicks ((viewport-mouse-events window) . =#> . (lambda (ev) (send ev button-down? 'right)))))))))
(define window
(open-viewport "Animation - DrRacket" 400 400))
(define pixmap
(open-pixmap "" 400 400))
(define mouse-pos
(hold ((viewport-mouse-events window)
. =#=> .
(lambda (ev) (if (send ev moving?)
(make-posn
(send ev get-x)
(send ev get-y))
nothing)))
(query-mouse-posn window)))
(define filtered-keys (viewport-key-events window))
(define shift-down (hold (filtered-keys . ==> . sixkey-shift)))
(define control-down (hold (filtered-keys . ==> . sixkey-control)))
(define meta-down (hold (filtered-keys . ==> . sixkey-meta)))
(define alt-down (hold (filtered-keys . ==> . sixkey-alt)))
(define key-strokes ((viewport-key-events window) . ==> . sixkey-value))
(define left-clicks ((viewport-mouse-events window) . =#> . (lambda (ev) (send ev button-down? 'left))))
(define middle-clicks ((viewport-mouse-events window) . =#> . (lambda (ev) (send ev button-down? 'middle))))
(define right-clicks ((viewport-mouse-events window) . =#> . (lambda (ev) (send ev button-down? 'right))))
(define left-releases ((viewport-mouse-events window) . =#> . (lambda (ev) (send ev button-up? 'left))))
(define middle-releases ((viewport-mouse-events window) . =#> . (lambda (ev) (send ev button-up? 'middle))))
(define right-releases ((viewport-mouse-events window) . =#> . (lambda (ev) (send ev button-up? 'right))))
(define-syntax (define-shape-struct stx)
(syntax-case stx ()
[(_ name (field ...))
(with-syntax
([ctor-name (datum->syntax stx (string->symbol (format "make-~a" (syntax-e #'name))))]
[(accessor-name ...)
(map (lambda (fd)
(string->symbol (format "~a-~a" (syntax-e #'name) (syntax-e fd))))
(syntax-e #'(field ...)))]
[(index ...)
(build-list (length (syntax-e #'(field ...))) identity)])
#'(begin
(define (ctor-name field ...)
(vector 'name field ...))
(define (accessor-name obj)
(vector-ref obj index))
...))]))
(define-struct ring (center radius color))
(define-struct solid-ellipse (ul w h color))
(define-struct graph-string (pos text color))
(define-struct line (p1 p2 color))
(define-struct rect (ul w h color))
(define-struct rrect (ur w h color))
(define-struct curve (xmin xmax ymin ymax fn))
(define-struct polygon (posn-list posn color))
(define-struct solid-polygon (posn-list posn color))
(define-struct arc (pos width height start-radians end-radians color))
(define-struct solid-arc (pos width height start-radians end-radians color))
(define-struct image (pos renderer))
(define (prep-image file)
(draw-pixmap-posn file))
(define (make-circle center r color)
(make-solid-ellipse (make-posn (- (posn-x center) r)
(- (posn-y center) r))
(* 2 r) (* 2 r) color))
(define l (new-cell empty))
(define (display-shapes x)
(set-cell! l x))
(define (top-level-draw-list a-los)
(compound-lift
(lambda (vn)
((clear-viewport pixmap))
(draw-list a-los vn)
(copy-viewport pixmap window))))
(define (my-for-each proc lst v-n)
(let ([lst (v-n lst)])
(if (empty? lst)
(void)
(begin
(proc (v-n (first lst)))
(my-for-each proc (rest lst) v-n)))))
(define (draw-list a-los v-n)
(let loop ([a-los a-los])
(my-for-each
(lambda (v)
(match (v-n v)
[(? undefined?) (void)]
[(ring center radius color)
(let ([center (v-n center)]
[radius (v-n radius)]
[color (v-n color)])
(unless (or (undefined? center)
(undefined? radius))
((draw-ellipse pixmap)
(make-posn (- (v-n (posn-x center)) radius)
(- (v-n (posn-y center)) radius))
(* 2 radius)
(* 2 radius)
(if (undefined? color) "black" color))))]
[(arc pos width height start-radians end-radians color)
(let ([pos (v-n pos)]
[width (v-n width)]
[height (v-n height)]
[start-radians (v-n start-radians)]
[end-radians (v-n end-radians)])
((draw-arc pixmap) pos width height start-radians end-radians color))]
[(solid-arc pos width height start-radians end-radians color)
(let ([pos (v-n pos)]
[width (v-n width)]
[height (v-n height)]
[start-radians (v-n start-radians)]
[end-radians (v-n end-radians)])
((draw-solid-arc pixmap) pos width height start-radians end-radians color))]
[(image pos renderer)
(let ([renderer (v-n renderer)]
[pos (v-n pos)])
((renderer pixmap) pos))]
[(solid-ellipse ul w h color)
(let ([ul (v-n ul)]
[w (v-n w)]
[h (v-n h)]
[color (v-n color)])
(unless (or (undefined? ul)
(undefined? w)
(undefined? h))
((draw-solid-ellipse pixmap) ul w h (if (undefined? color) "black" color))))]
[(graph-string pos text color) ((draw-string pixmap) (v-n pos) (v-n text) (v-n color))]
[(line p1 p2 color)
(let ([p1 (v-n p1)]
[p2 (v-n p2)]
[color (v-n color)])
(unless (or (undefined? p1)
(undefined? p2))
((draw-line pixmap) p1 p2 (if (undefined? color) "black" color))))]
[(rect ul w h color)
(let ([ul (v-n ul)]
[w (v-n w)]
[h (v-n h)]
[color (v-n color)])
(cond
[(and (>= w 0) (>= h 0)) ((draw-solid-rectangle pixmap) ul w h color)]
[(>= h 0) ((draw-solid-rectangle pixmap) (make-posn (+ (posn-x ul) w) (posn-y ul)) (- w) h color)]
[(>= w 0) ((draw-solid-rectangle pixmap) (make-posn (posn-x ul) (+ (posn-y ul) h)) w (- h) color)]
[else ((draw-solid-rectangle pixmap) (make-posn (+ (posn-x ul) w) (+ (posn-y ul) h)) (- w) (- h) color)]))]
[(polygon pts offset color) ((draw-polygon pixmap) pts offset color)]
[(solid-polygon pts offset color) ((draw-solid-polygon pixmap) pts offset color)]
[(? list? x) (loop (v-n x))]
[(? void?) (void)]))
a-los v-n)))
(define d (top-level-draw-list l))
(define-struct graph-color (fn xmin xmax ymin ymax))
(define (draw-graph-color pm gc)
(let ([dp (draw-pixel pm)])
(match gc
[(graph-color fn xmin xmax ymin ymax)
(let ([xincr (/ (- xmax xmin) 300)]
[yincr (/ (- ymax ymin) 300)])
(let loop ([i 50] [y ymin])
(let loop ([j 50] [x xmin])
(dp (make-posn j i) (fn x y))
(when (< j 350)
(loop (add1 j) (+ x xincr))))
(when (< i 350)
(loop (add1 i) (+ y yincr)))))])))
(define (valid-posn? v)
(and (posn? v) (number? (posn-x v)) (number? (posn-y v))))
(define (key sym)
(key-strokes
. =#> .
(lambda (x) (eq? x (value-now sym)))))
(define (draw vp pm posl)
((clear-viewport pm))
(for-each (lambda (elt)
(cond
[(graph-color? elt) (draw-graph-color pm elt)]
[(string? elt) ((draw-string pm) (make-posn 8 20) elt)]
[(valid-posn? elt) ((draw-solid-ellipse pm)
(make-posn (- (posn-x elt) 10)
(- (posn-y elt) 10))
20 20
(make-rgb 0 .6 .6))]
[(and (cons? elt)
(valid-posn? (first elt))
(valid-posn? (rest elt))) ((draw-line pm)
(first elt)
(rest elt)
"black")]
[else (void)])) posl)
(copy-viewport pm vp))
#|
(define foldl
(case-lambda
[(f i l) (if (cons? l)
(foldl f (f (first l) i) (rest l))
i)]))
|#
(define (drop n l)
(if (empty? l)
empty
(if (<= n 0)
l
(drop (sub1 n) (rest l)))))
(define (inc-max n)
(lambda (x) (if (>= x n)
n
(add1 x))))
(define (dec-min n)
(lambda (x) (if (<= x n)
n
(sub1 x))))
(define (fix-rgb r g b)
(let ([fix (lambda (n) (min 1 (max 0 n)))])
(apply make-rgb (map fix (list r g b)))))
(define range-control
(lambda (up down limit [init 0])
(accum-b
(merge-e (up . -=> . (inc-max limit))
(down . -=> . (dec-min 0)))
init)))
(define (keyboard-control up down limit)
(accum-b
(key-strokes
. =#=> .
(match-lambda
[(? (lambda (x) (eq? x up))) (inc-max limit)]
[(? (lambda (x) (eq? x down))) (dec-min 0)]
[_ nothing]))
0))
(define-struct wave-state (hz offset))
(define (wave hz)
(let* ([state (collect-b
(snapshot-e (changes hz) milliseconds)
(make-wave-state (value-now hz) 0)
(lambda (new-freq+time old-state)
(match new-freq+time
[(list h1 t)
(match old-state
[(wave-state h0 o0)
(make-wave-state
h1
(+ o0 (* .002 pi t (- h0 h1))))])])))])
(+ (lift #f wave-state-offset state)
(* milliseconds pi (lift #f wave-state-hz state) .002))))
(define (current-and-last-value signal)
(let ([init (value-now signal)])
(collect-b (changes signal)
(list init init)
(lambda (new-value previous-two)
(list new-value (first previous-two))))))
(define (last-value signal)
(second (current-and-last-value signal)))
; (define (last-value signal)
; (let ([init (value-now signal)])
; (rest
; (collect-b (changes signal)
; (cons init init)
; (lambda (new old-pair)
; (cons new (first old-pair)))))))
(define (posn+ . args)
(make-posn (apply + (map posn-x args))
(apply + (map posn-y args))))
(define (posn- . args)
(make-posn (apply - (map posn-x args))
(apply - (map posn-y args))))
(define (posn/ p s)
(make-posn (/ (posn-x p) s)
(/ (posn-y p) s)))
(define (posn* p s)
(make-posn (* (posn-x p) s)
(* (posn-y p) s)))
(define (posn-dot p1 p2)
(+ (* (posn-x p1) (posn-x p2))
(* (posn-y p1) (posn-y p2))))
(define (posn-len p)
(sqrt (+ (sqr (posn-x p)) (sqr (posn-y p)))))
(define (normalize p)
(posn/ p (posn-len p)))
(define (current-mouse-pos)
(value-now mouse-pos))
(define (clip x lo hi)
(if (< x lo)
lo
(if (> x hi)
hi
x)))
(define (posn-diff p1 p2)
(sqrt (+ (sqr (- (posn-x p1) (posn-x p2)))
(sqr (- (posn-y p1) (posn-y p2))))))
(define (posn-derivative p)
(make-posn (derivative (posn-x p)) (derivative (posn-y p))))
(define (posn-integral p)
(make-posn (integral (posn-x p)) (integral (posn-y p))))
(provide
(except-out (all-defined-out) pixmap window draw-list l d
make-wave-state wave-state-hz wave-state-offset)
(all-from-out frtime/animation/graphics)))
|