initial stage of game pad, need to turn play file into test
This commit is contained in:
parent
c221131254
commit
5867589993
BIN
collects/2htdp/private/gamepad.png
Normal file
BIN
collects/2htdp/private/gamepad.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 15 KiB |
32
collects/2htdp/private/pad.rkt
Normal file
32
collects/2htdp/private/pad.rkt
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
#lang racket/gui
|
||||||
|
|
||||||
|
;; provide basic elements for game pad clause in big-bang: the icon, pad-event?
|
||||||
|
|
||||||
|
(require racket/runtime-path)
|
||||||
|
|
||||||
|
(provide
|
||||||
|
;; bitmap
|
||||||
|
game-pad
|
||||||
|
;; KeyEvent -> Boolean
|
||||||
|
;; is the given key-event also a pad-event?
|
||||||
|
pad-event?
|
||||||
|
)
|
||||||
|
|
||||||
|
;; ---------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
(define-runtime-path gamepad-path "gamepad.png")
|
||||||
|
(define game-pad (read-bitmap gamepad-path 'png/alpha #f #t))
|
||||||
|
(unless (send game-pad ok?)
|
||||||
|
(error 'big-bang "the game pad icon isn't available; please report error"))
|
||||||
|
|
||||||
|
(define pad-buttons
|
||||||
|
'("up" "w"
|
||||||
|
"down" "s"
|
||||||
|
"left" "a"
|
||||||
|
"right" "d"
|
||||||
|
" "
|
||||||
|
"shift" "rshift"))
|
||||||
|
|
||||||
|
(define (pad-event? ke)
|
||||||
|
(pair? (member ke pad-buttons)))
|
||||||
|
|
|
@ -6,6 +6,8 @@
|
||||||
"checked-cell.rkt"
|
"checked-cell.rkt"
|
||||||
"stop.rkt"
|
"stop.rkt"
|
||||||
"universe-image.rkt"
|
"universe-image.rkt"
|
||||||
|
"pad.rkt"
|
||||||
|
(only-in 2htdp/image scale overlay/align)
|
||||||
htdp/error
|
htdp/error
|
||||||
mzlib/runtime-path
|
mzlib/runtime-path
|
||||||
mrlib/bitmap-label
|
mrlib/bitmap-label
|
||||||
|
@ -30,6 +32,8 @@
|
||||||
;
|
;
|
||||||
;
|
;
|
||||||
|
|
||||||
|
(define MIN-WIDT-FOR-GAME-PAD 300)
|
||||||
|
|
||||||
;; -----------------------------------------------------------------------------
|
;; -----------------------------------------------------------------------------
|
||||||
;; packages for broadcasting information to the universe
|
;; packages for broadcasting information to the universe
|
||||||
|
|
||||||
|
@ -52,7 +56,7 @@
|
||||||
(class* object% (start-stop<%>)
|
(class* object% (start-stop<%>)
|
||||||
(inspect #f)
|
(inspect #f)
|
||||||
(init-field world0)
|
(init-field world0)
|
||||||
(init-field name state register check-with on-key on-release on-mouse record?)
|
(init-field name state register check-with on-key on-release on-pad on-mouse record?)
|
||||||
(init on-receive on-draw stop-when)
|
(init on-receive on-draw stop-when)
|
||||||
|
|
||||||
;; -----------------------------------------------------------------------
|
;; -----------------------------------------------------------------------
|
||||||
|
@ -148,20 +152,30 @@
|
||||||
(unless (and width height)
|
(unless (and width height)
|
||||||
(set! width first-width)
|
(set! width first-width)
|
||||||
(set! height first-height))))
|
(set! height first-height))))
|
||||||
|
(when pad
|
||||||
|
(unless (>= width MIN-WIDT-FOR-GAME-PAD)
|
||||||
|
(error 'big-bang
|
||||||
|
"a game pad requires a scene whose width is greater or equal to ~a, given ~e"
|
||||||
|
MIN-WIDT-FOR-GAME-PAD fst-scene))
|
||||||
|
(set! game-pad-image (scale (/ width (image-width game-pad)) game-pad)))
|
||||||
(create-frame)
|
(create-frame)
|
||||||
(show fst-scene)))
|
(show fst-scene)))
|
||||||
|
|
||||||
|
(define/private (add-game-pad scene)
|
||||||
|
(if (boolean? pad) scene (overlay/align 'left 'bottom game-pad-image scene)))
|
||||||
|
|
||||||
(define/public (deal-with-key %)
|
(define/public (deal-with-key %)
|
||||||
(if (and (not on-key) (not on-release))
|
(if (and (not on-key) (not on-pad) (not on-release))
|
||||||
%
|
%
|
||||||
(class %
|
(class %
|
||||||
(super-new)
|
(super-new)
|
||||||
(define/override (on-char e)
|
(define/override (on-char e)
|
||||||
(when live
|
(when live
|
||||||
(let ([e:str (key-event->parts e)])
|
(let ([e:str (key-event->parts e)])
|
||||||
(if (string=? e:str "release")
|
(cond
|
||||||
(prelease (key-release->parts e))
|
[(string=? e:str "release") (prelease (key-release->parts e))]
|
||||||
(pkey e:str))))))))
|
[(and pad (pad-event? e:str)) (ppad e:str)]
|
||||||
|
[else (pkey e:str)])))))))
|
||||||
|
|
||||||
(define/public (deal-with-mouse %)
|
(define/public (deal-with-mouse %)
|
||||||
(if (not on-mouse)
|
(if (not on-mouse)
|
||||||
|
@ -216,7 +230,8 @@
|
||||||
|
|
||||||
;; Image -> Void
|
;; Image -> Void
|
||||||
;; show the image in the visible world
|
;; show the image in the visible world
|
||||||
(define/public (show pict)
|
(define/public (show pict0)
|
||||||
|
(define pict (add-game-pad pict0))
|
||||||
(send visible begin-edit-sequence)
|
(send visible begin-edit-sequence)
|
||||||
(send visible lock #f)
|
(send visible lock #f)
|
||||||
(let ([s (send visible find-first-snip)]
|
(let ([s (send visible find-first-snip)]
|
||||||
|
@ -237,6 +252,8 @@
|
||||||
;; callbacks
|
;; callbacks
|
||||||
(field
|
(field
|
||||||
(key (if on-key on-key (lambda (w ke) w)))
|
(key (if on-key on-key (lambda (w ke) w)))
|
||||||
|
(pad on-pad)
|
||||||
|
(game-pad-image #f)
|
||||||
(release (if on-release on-release (lambda (w ke) w)))
|
(release (if on-release on-release (lambda (w ke) w)))
|
||||||
(mouse on-mouse)
|
(mouse on-mouse)
|
||||||
(rec on-receive))
|
(rec on-receive))
|
||||||
|
@ -316,6 +333,9 @@
|
||||||
;; key events
|
;; key events
|
||||||
(def/cback pubment (pkey ke) key)
|
(def/cback pubment (pkey ke) key)
|
||||||
|
|
||||||
|
;; key events
|
||||||
|
(def/cback pubment (ppad ke) pad)
|
||||||
|
|
||||||
;; release events
|
;; release events
|
||||||
(def/cback pubment (prelease ke) release)
|
(def/cback pubment (prelease ke) release)
|
||||||
|
|
||||||
|
@ -494,7 +514,7 @@
|
||||||
;; EXPLORE: put random into the library and make it an event
|
;; EXPLORE: put random into the library and make it an event
|
||||||
(define aworld-old%
|
(define aworld-old%
|
||||||
(class world% (super-new)
|
(class world% (super-new)
|
||||||
(inherit-field world0 tick key release mouse rec draw rate width height record?)
|
(inherit-field world0 tick key pad release mouse rec draw rate width height record?)
|
||||||
(inherit show callback-stop!)
|
(inherit show callback-stop!)
|
||||||
|
|
||||||
;; Frame Custodian ->* (-> Void) (-> Void)
|
;; Frame Custodian ->* (-> Void) (-> Void)
|
||||||
|
@ -537,6 +557,7 @@
|
||||||
|
|
||||||
(def/cb augment (ptock tick))
|
(def/cb augment (ptock tick))
|
||||||
(def/cb augment (pkey key e))
|
(def/cb augment (pkey key e))
|
||||||
|
(def/cb augment (ppad pad e))
|
||||||
(def/cb augment (prelease release e))
|
(def/cb augment (prelease release e))
|
||||||
(def/cb augment (pmouse mouse x y me))
|
(def/cb augment (pmouse mouse x y me))
|
||||||
(def/cb augment (prec rec m))
|
(def/cb augment (prec rec m))
|
||||||
|
|
|
@ -88,13 +88,16 @@
|
||||||
;; World Nat Nat MouseEvent -> World
|
;; World Nat Nat MouseEvent -> World
|
||||||
;; on-mouse must specify a mouse event handler
|
;; on-mouse must specify a mouse event handler
|
||||||
[on-mouse DEFAULT #f (function-with-arity 4)]
|
[on-mouse DEFAULT #f (function-with-arity 4)]
|
||||||
;; World KeyEvent -> World
|
;; (U #f (World KeyEvent -> World))
|
||||||
;; on-key must specify a key event handler
|
;; on-key must specify a key event handler
|
||||||
[on-key DEFAULT #f (function-with-arity 2)]
|
[on-key DEFAULT #f (function-with-arity 2)]
|
||||||
;; World KeyEvent -> World
|
;; (U #f (World PadEvent -> World))
|
||||||
|
;; on-pad must specify a pad event handler
|
||||||
|
[on-pad DEFAULT #f (function-with-arity 2)]
|
||||||
|
;; (U #f (World KeyEvent -> World))
|
||||||
;; on-release must specify a release event handler
|
;; on-release must specify a release event handler
|
||||||
[on-release DEFAULT #f (function-with-arity 2)]
|
[on-release DEFAULT #f (function-with-arity 2)]
|
||||||
;; (U #f (World S-expression -> World))
|
;; (World S-expression -> World)
|
||||||
;; -- on-receive must specify a receive handler
|
;; -- on-receive must specify a receive handler
|
||||||
[on-receive DEFAULT #'(lambda (w m) w) (function-with-arity 2)]
|
[on-receive DEFAULT #'(lambda (w m) w) (function-with-arity 2)]
|
||||||
;; World -> Boolean
|
;; World -> Boolean
|
||||||
|
|
Loading…
Reference in New Issue
Block a user