-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgame-pasteboard.rkt
196 lines (164 loc) · 9.36 KB
/
game-pasteboard.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
#lang racket/gui
;; a pasteboard editor for displaying the current state of the board and movable avatar snips
;; it 'knows' nothing about the meaning of the snips or the background, though it makes one
;; assumption about the size of the snip it gets
;
;
; ;;;
; ; ;
; ; ;
; ;;;; ;; ;; ;;;;;; ;;;; ;;;; ; ;;; ;;; ;
; ; ; ; ; ; ; ; ;; ; ;; ; ; ; ;
; ;;;;;; ;; ; ;;;;;; ; ; ; ; ;
; ; ;; ; ; ; ; ; ;;;;; ;
; ; ;; ; ; ; ; ; ; ; ;
; ;; ; ; ; ; ;; ; ; ; ; ; ;; ;
; ;;;;; ; ; ;;; ;;;;; ; ; ; ;;; ; ;;;
;
;
;
;
(require (only-in Fish/GUI/game-image-snip game-image-snip%))
(require (only-in Fish/Common/board posn/c))
(provide
(contract-out
[game-pasteboard%
(class/c
[init-field
[background (is-a?/c game-image-snip%)]
[avatars0 (listof (is-a?/c game-image-snip%))]
[control [-> (is-a?/c game-image-snip%) posn/c [channel/c any/c] any]]])]))
;
;
; ; ; ;
; ; ;
; ; ;
; ;;; ; ;;;; ; ;;; ;;;; ; ;;; ;;; ; ;;;; ; ;;; ;;; ;;; ;;;; ;;;;
; ;; ;; ; ; ;; ;; ; ; ;; ; ;; ;; ; ; ;; ; ; ; ; ; ; ; ;
; ; ; ;;;;;; ; ; ;;;;;; ; ; ; ; ;;;;;; ; ; ; ; ;;;;;; ;
; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;;;;
; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
; ;; ;; ;; ; ;; ;; ;; ; ; ; ;; ;; ;; ; ; ; ; ; ; ;; ; ; ;
; ;;; ; ;;;;; ; ;;; ;;;;; ; ; ;;; ; ;;;;; ; ; ;;; ;;;;; ;;;;; ;;;;
; ;
; ;
; ;
;
(require Fish/Lib/toint)
;
;
; ; ;
; ; ; ;
; ; ; ;
; ; ;;; ;;; ;;;; ;;;;;; ;;;; ; ;;; ;;;; ;;; ;;;; ;;; ;
; ;; ;; ; ; ; ; ; ; ; ;; ;; ;; ;; ; ; ;; ; ;; ;;
; ; ; ; ; ; ;;;;;; ; ; ; ; ; ; ; ;
; ; ; ;;;;; ;;;; ; ; ; ; ; ; ;;;;; ; ; ;
; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;
; ;; ;; ; ;; ; ; ; ;; ; ;; ;; ;; ;; ; ;; ; ;; ;;
; ; ;;; ;;; ; ;;;; ;;; ;;;;; ; ;;; ;;;; ;;; ; ; ;;; ;
; ;
; ;
; ;
;
(define game-pasteboard%
(class pasteboard%
(init-field background avatars0 (control void))
(inherit move-to insert resize get-snip-location is-selected?)
(inherit begin-edit-sequence end-edit-sequence delete get-view-size)
(field [width0 (get-field width background)]) ;; of the original display
(field [height0 (get-field height background)]) ;; ditto
;; -----------------------------------------------------------------------------------------------
;; size managamennt
;; ASSUME all background picts are of the same size
(field [the-width width0]) ;; of the current display
(field [the-height height0]) ;; ditto
(field [scaled-width 1.0]) ;; the scaled width from the very first background
(field [scaled-height 1.0]) ;; ditto
(define/private (inverse-scaled x-ce y-ce)
(list (toint (/ x-ce scaled-width)) (toint (/ y-ce scaled-height))))
(define/private (scaled x-ce y-ce)
(values (toint (* x-ce scaled-width)) (toint (* y-ce scaled-height))))
(define/augment (on-display-size)
(define-values (scaled-width scaled-height) (scaling-ratios))
(define &background (send background scale2 scaled-width scaled-height))
(define &avatars0 (map (λ (a) (send a scale2 scaled-width scaled-height)) avatars0))
(do-replace &background &avatars0))
(define/public (replace &background &avatars0 &control)
(define bg (send &background scale2 scaled-width scaled-height))
(define av (map (λ (a) (send a scale2 scaled-width scaled-height)) &avatars0))
(do-replace bg av)
(set! control &control))
(define/private (do-replace &background &avatars0)
(set! the-selected-snip #false)
(begin-edit-sequence)
(for ([s avatars0]) (delete s))
(delete background)
(set! background &background)
(set! avatars0 &avatars0)
(show)
(end-edit-sequence))
#; {-> (values Real Real)}
;; EFFECT set width, height, scaled-width, and scaled-height
(define/private (scaling-ratios)
(define-values (width height) (my-dimensions))
(begin0
(values (/ width the-width) (/ height the-height))
(set! scaled-width (/ width width0))
(set! scaled-height (/ height height0))
(set! the-width width)
(set! the-height height)))
#; { -> (values N N)}
;; determine the current size of the pasteboard
(define/private (my-dimensions)
(define bw (box 0))
(define bh (box 0))
(get-view-size bw bh)
(values (unbox bw) (unbox bh)))
;; now show initial state
(define/private (show)
(for ([snip avatars0])
(insert snip (send snip get-x0) (send snip get-y0)))
(insert background (send background get-x0) (send background get-y0)))
;; -----------------------------------------------------------------------------------------------
;; snip move management
(field [the-selected-snip #false])
(define/augment (can-select? s on?)
(cond
[(not on?) (set! the-selected-snip #false) #true]
[else (and (not the-selected-snip) (not (eq? s background)))]))
(define/augment (after-select the-snip on?)
(set! the-selected-snip (and on? the-snip)))
(define/augment (can-interactive-move? e)
(not (is-selected? background)))
(define/augment (after-interactive-move event)
(when the-selected-snip
(define-values (x-tl y-tl) (find-snip-location the-selected-snip))
(define-values (x-ce y-ce) (send the-selected-snip translate x-tl y-tl #:place 'top->center))
(wait-for-next-location the-selected-snip x-ce y-ce)))
#; {[Instanceof Snip%] N N -> Void}
;; communicate with the concurrent control component to get the next location of the snip
(define/private (wait-for-next-location the-snip x-ce y-ce)
(define view-channel (make-channel))
(control (send the-snip current-row-column) (inverse-scaled x-ce y-ce) view-channel)
(define move-snip-to (channel-get view-channel))
(match move-snip-to
[#false
(move-to the-snip (send the-snip get-x0) (send the-snip get-y0))]
[[list cx0 cy0]
(define-values (cx cy) (scaled cx0 cy0))
(define-values (x-tl y-tl) (send the-snip translate cx cy #:place 'center->top))
(move-to the-snip x-tl y-tl)]))
#; {[Instanceof Snip] Boolean -> N N}
;; retrireveretrieve the top-left corner of the snip
(define/private (find-snip-location the-snip)
(define x-tl (box 10))
(define y-tl (box 10))
(get-snip-location the-snip x-tl y-tl)
(define x (toint (unbox x-tl)))
(define y (toint (unbox y-tl)))
(values x y))
;; -----------------------------------------------------------------------------------------------
;; go
(super-new)
(show)))