(define stack%
{class
object%
(init garden)
(super-new)
(define cards null)
(define my-region null)
(define my-garden garden)
(define/public (get-garden) my-garden)
(define/public (get-layout)
(send my-garden get-layout))
(define/public (add-card card-to-add)
(send (send this get-garden) syncronize-stacks card-to-add)
(set! cards (sea-cat card-to-add cards))
(send* card-to-add
(home-region my-region)
)
(send this add-card-pre-layout)
(send (get-layout) layout-cards this)
)
(define/public (remove-card card-to-remove)
(set! cards (filter {lambda (card)
(not (eq? card card-to-remove))}
cards)))
(define/public (move-done)
#t)
(define/public (number-of-cards) (length cards))
(define/public (empty?) (null? cards))
(define/public (get-cards) cards)
(define/public (set-cards bucket-cards)
(set! cards bucket-cards))
(define/public (add-card-pre-layout) #t)
(define/public (get-region) my-region)
(define/public (set-region new-region)
(set! my-region new-region)
)
(define/public (save-state)
(list (sea-cat 'cards cards)
(sea-cat 'flower-states (map flower-save-state cards))))
(define/public (restore-state state)
(set! cards (cast (first state)))
(let ((flower-states (cast (second state))))
(map flower-restore-state
cards
flower-states))
(send (send this get-layout) layout-cards this)
)
})
(define foundation%
{class
stack%
(init foundation-index)
(super-new)
(define my-index foundation-index)
(define (foundation-region-callback cards)
(send (send this get-garden)
place-on-foundation-request (wheels cards) this))
(let ((layout (send this get-layout)))
(send this set-region
(make-region
(send layout get-foundation-x-coordinate this)
(send layout get-foundation-y-coordinate this)
(send layout get-foundation-width this)
(send layout get-foundation-height this)
(send layout get-foundation-title this)
foundation-region-callback)))
(send (send (send this get-garden) get-table)
add-region (send this get-region))
(define/public (get-foundation-index) my-index)
})
(define flower-bed%
{class
stack%
(init flower-bed-index)
(super-new)
(define my-index flower-bed-index)
(define (flower-bed-region-callback cards)
(send (send this get-garden)
place-on-flower-bed-request (wheels cards) this))
(let ((layout (send this get-layout)))
(send this set-region
(make-region
(send layout get-flower-bed-x-coordinate this)
(send layout get-flower-bed-y-coordinate this)
(send layout get-flower-bed-width this)
(send layout get-flower-bed-height this)
(send layout get-flower-bed-title this)
flower-bed-region-callback)))
(send (send (send this get-garden) get-table)
add-region (send this get-region))
(define/public (get-flower-bed-index) my-index)
(define (reconfigure-cards)
(let ((my-cards (send this get-cards)))
(broadcast* my-cards
(user-can-move #f)
(snap-back-after-move #t))
[when [send (send this get-garden)
flowers-are-laughing]
(broadcast my-cards face-up)]
(when (not (null? my-cards))
(let ((top-card (wheels my-cards)))
(when [send top-card face-down?]
(send (send this get-garden)
card-face-up top-card))
(send top-card user-can-move #t)) )))
(define/public (laughing-flowers)
(reconfigure-cards))
(define/override (move-done)
(reconfigure-cards))
})
(define bucket%
{class
stack%
(super-new)
(define (bucket-region-callback cards)
(send (send this get-garden)
place-on-bucket-request (wheels cards) this))
(let ((layout (send this get-layout)))
(send this set-region
(make-region
(send layout get-bucket-x-coordinate this)
(send layout get-bucket-y-coordinate this)
(send layout get-bucket-width this)
(send layout get-bucket-height this)
(send layout get-bucket-title this)
bucket-region-callback)))
(send (send (send this get-garden) get-table)
add-region (send this get-region))
(define (prepare-card-for-bucket card)
(when (send card face-down?) (send card face-up))
(send* card
(snap-back-after-move #f)
(user-can-move #t)
))
(define/override (add-card card)
(prepare-card-for-bucket card)
(super add-card card))
(define (sort-cards-by-x-axis-order)
(let ((table (send (send this get-garden)
get-table)))
(sort (send this get-cards)
[lambda (first-card second-card)
(let-values (([x1 y1] (send table
card-location first-card))
([x2 y2] (send table
card-location second-card)))
(< x1 x2) )])))
(define/override (move-done)
(send this set-cards (sort-cards-by-x-axis-order))
(send (send this get-layout) layout-cards this))
(define/override (add-card-pre-layout)
(send this set-cards (sort-cards-by-x-axis-order)))
})