-
Notifications
You must be signed in to change notification settings - Fork 3
/
selecting3.rkt
39 lines (35 loc) · 1.57 KB
/
selecting3.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
#lang s-exp rosette
; Example of selecting objects. There are three circles and we select one
(require "../applications/geothings.rkt")
(require "reactive.rkt")
; put item as the first element in the list referred to by the box things
(define (put-first item things)
(let* ([things-list (unbox things)]
[others (filter (lambda (x) (not (eq? item x))) things-list)])
(set-box! things (cons item others))))
(define selection-example%
(class reactive-thing%
(inherit button-going-down? mouse-position image previous)
(super-new)
(define c1 (make-circle this (circle (point 150 150) 50 (color "blue"))))
(define c2 (make-circle this (circle (point 200 150) 50 (color "red"))))
(define c3 (make-circle this (circle (point 250 150) 50 (color "green"))))
(define my-image (box (list c1 c2 c3)))
(define actual-target null)
(define actual-offset null)
(send this set-image! my-image)
(define (potential-targets)
; (printf "computing potential targets\n")
(filter (lambda (c) (contains-point c (mouse-position)))
(send this wally-evaluate (unbox my-image))))
(when (button-going-down?)
(let ([tgs (potential-targets)])
; (printf "tgs = ~a mousepos ~a \n" tgs (mouse-position))
(cond [(pair? tgs)
(set! actual-target (car tgs))
(set! actual-offset (point-minus (mouse-position) (circle-center actual-target)))
(put-first actual-target my-image)]))
(send this solve)
)))
(define s (new selection-example%))
(make-viewer s #:title "Selection example")