2012-01-16 18:16:05 -05:00
|
|
|
;;; scgamewidgets.scm - a scheme game library (needs scx-0.2 or scheme48-fb)
|
2012-01-16 11:22:48 -05:00
|
|
|
;;;
|
|
|
|
;;; Copyright (c) 2011-2012 Johan Ceuppens
|
|
|
|
;;;
|
|
|
|
;;; All rights reserved.
|
|
|
|
;;;
|
|
|
|
;;; Redistribution and use in source and binary forms, with or without
|
|
|
|
;;; modification, are permitted provided that the following conditions
|
|
|
|
;;; are met:
|
|
|
|
;;; 1. Redistributions of source code must retain the above copyright
|
|
|
|
;;; notice, this list of conditions and the following disclaimer.
|
|
|
|
;;; 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
;;; notice, this list of conditions and the following disclaimer in the
|
|
|
|
;;; documentation and/or other materials provided with the distribution.
|
|
|
|
;;; 3. The name of the authors may not be used to endorse or promote products
|
|
|
|
;;; derived from this software without specific prior written permission.
|
|
|
|
;;;
|
|
|
|
;;; THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
;;; IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
;;; OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
;;; IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
;;; INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
;;; NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
;;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
;;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
;;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
;;; THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
2012-01-16 18:38:15 -05:00
|
|
|
(load "scgame.scm")
|
2012-01-16 11:22:48 -05:00
|
|
|
|
2012-01-16 19:29:33 -05:00
|
|
|
;; for inits see scgame.scm
|
|
|
|
|
2012-01-16 19:43:11 -05:00
|
|
|
;; FIXME refactor and cleanup
|
|
|
|
|
2012-01-16 20:28:29 -05:00
|
|
|
|
|
|
|
;; widget tree
|
|
|
|
|
|
|
|
(define (make-widget-tree)
|
|
|
|
'())
|
|
|
|
|
|
|
|
(define (make-widget-tree-leaf widget)
|
|
|
|
(delay widget))
|
|
|
|
|
|
|
|
(define (widget-leaf? n)
|
|
|
|
(and (widget? n)(not (list? n))))
|
|
|
|
|
|
|
|
(define (make-widget-tree-node widgetlist)
|
|
|
|
(delay widgetlist))
|
|
|
|
|
|
|
|
(define (widget-node? n)
|
|
|
|
(and (not (widget? n))(list? n)))
|
|
|
|
|
|
|
|
(define (widget-tree-add! tree n)
|
|
|
|
(set! tree (append tree (list n))))
|
|
|
|
|
|
|
|
(define (widget-node-add! node n)
|
|
|
|
(set! node (append node (list n))))
|
|
|
|
|
2012-01-17 01:00:50 -05:00
|
|
|
;; throws backwards inner widget
|
2012-01-16 20:28:29 -05:00
|
|
|
(define (widget-node-collide? node x y)
|
|
|
|
(define (frec l)
|
|
|
|
(cond ((null? l) #f)
|
2012-01-16 20:46:39 -05:00
|
|
|
((list? (car l))
|
|
|
|
(frec (car l)))
|
2012-01-16 20:28:29 -05:00
|
|
|
((and (widget? (car l))
|
|
|
|
(>= x (((car l)'get-x)))
|
|
|
|
(<= x (+ (((car l)'get-x)))(((car l)'get-w)))
|
|
|
|
(>= y (((car l)'get-y)))
|
|
|
|
(<= y (+ (((car l)'get-y)))(((car l)'get-h))))
|
2012-01-17 01:00:50 -05:00
|
|
|
(car l))
|
2012-01-16 20:28:29 -05:00
|
|
|
(else (frec (cdr l)))))
|
|
|
|
|
|
|
|
(if (widget-node? node)
|
|
|
|
(for-each frec (force node))
|
|
|
|
#f))
|
|
|
|
|
|
|
|
(define (widget-leaf-collide? leaf x y)
|
|
|
|
(if (widget-leaf? leaf)
|
|
|
|
(cond ((and (widget? leaf)
|
|
|
|
(>= x ((leaf 'get-x)))
|
|
|
|
(<= x (+ ((leaf 'get-x)))((leaf 'get-w)))
|
|
|
|
(>= y ((leaf 'get-y)))
|
|
|
|
(<= y (+ ((leaf 'get-y)))((leaf 'get-h))))
|
|
|
|
#t))
|
|
|
|
#f))
|
|
|
|
|
|
|
|
;; widgets
|
|
|
|
|
2012-01-16 19:29:33 -05:00
|
|
|
(define (make-scgame-widget)
|
|
|
|
(define (draw)
|
2012-01-16 20:28:29 -05:00
|
|
|
(display-msg "widget - draw - subclass responsability"))
|
|
|
|
|
|
|
|
(define (widget?)
|
|
|
|
#t)
|
|
|
|
|
|
|
|
(define (get-x)
|
|
|
|
(display-msg "widget - get-x - subclass responsability")
|
|
|
|
0)
|
|
|
|
|
|
|
|
(define (get-y)
|
|
|
|
(display-msg "widget - get-y - subclass responsability")
|
|
|
|
0)
|
|
|
|
|
|
|
|
(define (get-w)
|
|
|
|
(display-msg "widget - get-w - subclass responsability")
|
|
|
|
0)
|
|
|
|
|
|
|
|
(define (get-h)
|
|
|
|
(display-msg "widget - get-h - subclass responsability")
|
|
|
|
0)
|
2012-01-16 19:29:33 -05:00
|
|
|
|
|
|
|
(lambda (msg)
|
|
|
|
(cond ((eq? msg 'draw) draw)
|
2012-01-16 20:28:29 -05:00
|
|
|
((eq? msg 'widget?) widget?)
|
2012-01-16 19:29:33 -05:00
|
|
|
(else
|
|
|
|
(display-msg "subclass responsability")))))
|
2012-01-16 11:22:48 -05:00
|
|
|
|
2012-01-16 20:28:29 -05:00
|
|
|
(define (make-button-widget xx yy)
|
2012-01-16 19:29:33 -05:00
|
|
|
(let ((widget (make-scgame-widget))
|
|
|
|
(image #f) ;; pixel array
|
|
|
|
(pressed-image #f) ;; pixel array
|
|
|
|
(pressed #f)
|
2012-01-16 20:28:29 -05:00
|
|
|
(x xx)
|
|
|
|
(y yy)
|
2012-01-16 19:29:33 -05:00
|
|
|
(width 0)
|
|
|
|
(height 0))
|
2012-01-16 16:14:35 -05:00
|
|
|
|
2012-01-16 20:28:29 -05:00
|
|
|
(define (get-x)
|
|
|
|
x)
|
|
|
|
|
|
|
|
(define (get-y)
|
|
|
|
y)
|
|
|
|
|
|
|
|
(define (get-w)
|
|
|
|
w)
|
|
|
|
|
|
|
|
(define (get-h)
|
|
|
|
h)
|
|
|
|
|
2012-01-16 19:52:04 -05:00
|
|
|
(define (set-image win filename)
|
|
|
|
(set! image (((make-scimage2)'load-image) win filename))
|
2012-01-16 19:29:33 -05:00
|
|
|
(let ((wh (vector-ref (list->vector image) 1)))
|
|
|
|
(set! width (car wh))
|
|
|
|
(set! height (cadr wh))
|
|
|
|
))
|
|
|
|
|
2012-01-16 19:52:04 -05:00
|
|
|
(define (set-pressed-image win filename)
|
|
|
|
(set! pressed-image (((make-scimage2)'load-image) win filename))
|
2012-01-16 19:29:33 -05:00
|
|
|
(let ((wh (vector-ref (list->vector image) 1)))
|
|
|
|
(set! width (car wh))
|
|
|
|
(set! height (cadr wh))
|
|
|
|
))
|
|
|
|
|
|
|
|
(define (draw-pressed-image dpy win gc)
|
|
|
|
(init-sync-x-events dpy)
|
|
|
|
(map-window dpy win)
|
2012-01-17 00:11:08 -05:00
|
|
|
(draw-points dpy win gc (* width height) 0 0
|
|
|
|
(/ width 2) (/ height 2)))
|
2012-01-16 19:29:33 -05:00
|
|
|
|
2012-01-16 20:28:29 -05:00
|
|
|
;; NOTE : you can remap a button (image) to a new window win if you like
|
2012-01-16 19:29:33 -05:00
|
|
|
(define (draw-image dpy win gc)
|
|
|
|
(init-sync-x-events dpy)
|
|
|
|
(map-window dpy win)
|
2012-01-17 00:11:08 -05:00
|
|
|
(draw-points dpy win gc (* width height) 0 0
|
|
|
|
(/ width 2) (/ height 2)))
|
2012-01-16 19:29:33 -05:00
|
|
|
|
|
|
|
(define (draw-points dpy win gc count x y)
|
|
|
|
(if (zero? (modulo count 100))
|
|
|
|
(display-flush dpy))
|
|
|
|
(if (not (zero? count))
|
|
|
|
(let ((xf (floor (* (+ 1.2 x) width))) ; These lines center the picture
|
|
|
|
(yf (floor (* (+ 0.5 y) height))))
|
|
|
|
(draw-point dpy win gc (inexact->exact xf) (inexact->exact yf))
|
|
|
|
(draw-points dpy win gc ;; FIXME1
|
|
|
|
(- count 1)
|
|
|
|
(- (* y (+ 1 (sin (* 0.7 x))))
|
|
|
|
(* 1.2 (sqrt (abs x))))
|
|
|
|
(- 0.21 x)
|
|
|
|
width height))))
|
|
|
|
|
|
|
|
(define (draw)
|
|
|
|
(if pressed
|
|
|
|
(draw-image)
|
|
|
|
(draw-pressed-image))
|
|
|
|
(map-window dpy win))
|
|
|
|
|
|
|
|
(define (press!)
|
2012-01-16 19:52:04 -05:00
|
|
|
(press-button dpy win gc)
|
2012-01-16 19:29:33 -05:00
|
|
|
(set! pressed #t))
|
|
|
|
|
|
|
|
(define (release!)
|
2012-01-16 19:52:04 -05:00
|
|
|
(release-button dpy win gc)
|
2012-01-16 19:29:33 -05:00
|
|
|
(set! pressed #f))
|
2012-01-16 11:22:48 -05:00
|
|
|
|
2012-01-17 00:11:08 -05:00
|
|
|
|
2012-01-16 11:22:48 -05:00
|
|
|
(lambda (msg)
|
|
|
|
(cond ((eq? 'set-image) set-image)
|
2012-01-16 19:29:33 -05:00
|
|
|
((eq? 'set-pressed-image) set-pressed-image)
|
|
|
|
((eq? 'press!) press!)
|
|
|
|
((eq? 'release!) release!)
|
2012-01-16 20:28:29 -05:00
|
|
|
((eq? get-x) get-x)
|
|
|
|
((eq? get-y) get-y)
|
|
|
|
((eq? get-w) get-w)
|
|
|
|
((eq? get-h) get-h)
|
2012-01-17 01:00:50 -05:00
|
|
|
((eq? draw) draw)
|
2012-01-16 19:29:33 -05:00
|
|
|
(widget msg)
|
2012-01-16 11:22:48 -05:00
|
|
|
))))
|
2012-01-17 01:00:50 -05:00
|
|
|
|
|
|
|
;; This is the main loop you call on your window's
|
|
|
|
;; widget tree (see above)
|
|
|
|
|
|
|
|
(define (widget-tree-eventloop widget-tree)
|
|
|
|
(let ((mousex 0)
|
|
|
|
(mousey 0))
|
|
|
|
(init-sync-x-events dpy)
|
|
|
|
(map-window dpy win)
|
|
|
|
(call-with-event-channel
|
|
|
|
dpy win (event-mask button-press)
|
|
|
|
(lambda (channel)
|
|
|
|
;; FIXME calibrate at 10 times or using nanosleep
|
|
|
|
(let loop ()
|
|
|
|
(if
|
|
|
|
(let ((e (receive channel)))
|
|
|
|
(cond
|
|
|
|
((motion-event? e)
|
|
|
|
(set! mousex (motion-event-x))
|
|
|
|
(set! mousey (motion-event-y)))
|
|
|
|
((button-press-event? e)
|
|
|
|
(let ((widget (widget-node-collide? widget-tree mousex mouse)))
|
|
|
|
((widget 'press!))))
|
|
|
|
((button-release-event? e)
|
|
|
|
(let ((widget (widget-node-collide? widget-tree mousex mouse)))
|
|
|
|
((widget 'press!))))
|
|
|
|
(else #f))))
|
|
|
|
(loop))))))
|
|
|
|
|