-
Notifications
You must be signed in to change notification settings - Fork 3
/
selecting-live3.rkt
37 lines (32 loc) · 1.57 KB
/
selecting-live3.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
#lang s-exp rosette
; Example of selecting objects. There are three circles and we select one.
; The list of potential targets is stored in a variable, rather than being a procedure call.
; This results in *non-linear constraints* which causes z3 to return 'unknown' and crash.
(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 mp (make-point))
(always (equal? mp (mouse-position)))
(define potential-targets (filter (lambda (c) (contains-point c mp)) (unbox my-image)))
(when (button-going-down?)
(cond [(pair? potential-targets)
(set! actual-target (car potential-targets))
(set! actual-offset (point-minus (mouse-position) (circle-center actual-target)))
(put-first actual-target my-image)]))
))
(define s (new selection-example%))
(make-viewer s #:title "Selection example")