summaryrefslogtreecommitdiff
path: root/sly/input
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2015-12-22 14:35:44 -0500
committerDavid Thompson <dthompson2@worcester.edu>2015-12-22 16:28:18 -0500
commit8b9b5d371d1dc1c780e227ce9a555cf6c88a85c8 (patch)
treef1b6524f92aaa329667f08f4a010a7b5e6925ae8 /sly/input
parent60d601cbb5eb142d01f880b5902329ada93fc91a (diff)
Upgrade to SDL2!
This commit is massive and crazy and I'm not going to do the usual GNU ChangeLog thing because it's just too much. Let's just be happy that the port is completed!
Diffstat (limited to 'sly/input')
-rw-r--r--sly/input/joystick.scm171
-rw-r--r--sly/input/keyboard.scm22
-rw-r--r--sly/input/mouse.scm36
3 files changed, 25 insertions, 204 deletions
diff --git a/sly/input/joystick.scm b/sly/input/joystick.scm
deleted file mode 100644
index 0e465f6..0000000
--- a/sly/input/joystick.scm
+++ /dev/null
@@ -1,171 +0,0 @@
-;;; Sly
-;;; Copyright (C) 2014 Jordan Russell <jordan.likes.curry@gmail.com>
-;;;
-;;; This program is free software: you can redistribute it and/or
-;;; modify it under the terms of the GNU General Public License as
-;;; published by the Free Software Foundation, either version 3 of the
-;;; License, or (at your option) any later version.
-;;;
-;;; This program is distributed in the hope that it will be useful,
-;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
-;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-;;; General Public License for more details.
-;;;
-;;; You should have received a copy of the GNU General Public License
-;;; along with this program. If not, see
-;;; <http://www.gnu.org/licenses/>.
-
-;;; Commentary:
-;;
-;; Joystick signals
-;;
-;;; Code:
-
-(define-module (sly input joystick)
- #:use-module ((sdl sdl) #:prefix SDL:)
- #:use-module (sly event)
- #:use-module (sly signal)
- #:use-module (sly math vector)
- #:use-module (srfi srfi-1)
- #:use-module (srfi srfi-9)
- #:re-export ((SDL:joystick-name . joystick-name)
- (SDL:num-joysticks . num-joysticks))
- #:export (enable-joystick
- joystick-num-axes
- joystick-num-buttons
- joystick-axis-hook
- joystick-button-press-hook
- joystick-button-release-hook
- axis-value-raw
- raw-axis-max
- raw-axis-min
- axis-value
- button-down?
- make-directional-signal
- make-directional-signal-raw
- axis-scale))
-
-(define *joysticks* '())
-
-(define (enable-joystick)
- (set! *joysticks*
- (map SDL:joystick-open
- (iota (SDL:num-joysticks)))))
-
-(define (get-joystick idx)
- (list-ref *joysticks* idx))
-
-(define-syntax-rule (js-proc->idx-proc (js-proc . name) doc)
- (define (name idx)
- doc
- (if (> idx (SDL:num-joysticks))
- 0
- (js-proc (get-joystick idx)))))
-
-(js-proc->idx-proc (SDL:joystick-num-axes . joystick-num-axes)
- "Get number of axes of joystick at IDX.")
-
-(js-proc->idx-proc (SDL:joystick-num-buttons . joystick-num-buttons)
- "Get number of buttons of joystick at IDX.")
-
-(define joystick-axis-hook (make-hook 3))
-
-(register-event-handler
- 'joy-axis-motion
- (lambda (e)
- (run-hook joystick-axis-hook
- (SDL:event:jaxis:which e)
- (SDL:event:jaxis:axis e)
- (SDL:event:jaxis:value e))))
-
-(define-record-type <axis-event>
- (make-axis-event which axis value)
- axis-event?
- (which axis-event-joystick)
- (axis axis-event-axis)
- (value axis-event-value))
-
-(define joystick-button-press-hook (make-hook 2))
-
-(register-event-handler
- 'joy-button-down
- (lambda (e)
- (run-hook joystick-button-press-hook
- (SDL:event:jbutton:which e)
- (SDL:event:jbutton:button e))))
-
-(define joystick-button-release-hook (make-hook 2))
-
-(register-event-handler
- 'joy-button-up
- (lambda (e)
- (run-hook joystick-button-release-hook
- (SDL:event:jbutton:which e)
- (SDL:event:jbutton:button e))))
-
-(define-signal last-axis-event
- (hook->signal joystick-axis-hook 'none
- make-axis-event))
-
-(define raw-axis-min -32768)
-(define raw-axis-max 32767)
-
-(define (axis-value-raw idx axis)
- "Create a signal on the axis at AXIS of the joystick at IDX;
-joystick axis values are stored in a signed 16 bit integer and so,
-values range from [-32768,32767]."
- (signal-map axis-event-value
- (signal-filter
- (lambda (e)
- (and (axis-event? e)
- (= (axis-event-joystick e) idx)
- (= (axis-event-axis e) axis)))
- (make-axis-event idx axis 0)
- last-axis-event)))
-
-(define (make-directional-signal-raw idx x-axis y-axis)
- "Create a signal for a Dpad or Analog stick with X and Y axes;
-values range from [-32768,32767]."
- (signal-map vector2
- (axis-value-raw idx x-axis)
- (axis-value-raw idx y-axis)))
-
-(define (axis-scale raw-value)
- "Map a RAW-VALUE in [-32768, 32767] to a value in [-1, 1]."
- (define (clamp x)
- (cond ((< (abs x) 1/100) 0)
- ((> x 99/100) 1)
- ((< x -99/100) -1)
- (else x)))
- (clamp (/ raw-value 32768)))
-
-(define (axis-value idx axis)
- "Create a signal for the value of AXIS on joystick IDX;
-values are scaled to [-1,1]."
- (signal-map axis-scale (axis-value-raw idx axis)))
-
-(define (make-directional-signal idx x-axis y-axis)
- "Create a signal for a Dpad or Analog stick with X and Y axes;
-values are scaled to [-1,1]."
- (signal-map (lambda (v)
- (vector2 (axis-scale (vx v))
- (axis-scale (vy v))))
- (make-directional-signal-raw idx x-axis y-axis)))
-
-(define-signal button-last-down
- (hook->signal joystick-button-press-hook 'none
- list))
-
-(define-signal button-last-up
- (hook->signal joystick-button-release-hook 'none
- list))
-
-;; shamelessly copied from keyboard.scm
-(define (button-down? idx n)
- "Create a signal for the state of button N on joystick at IDX"
- (define (same-button? l)
- (equal? (list idx n) l))
- (define (button-filter value signal)
- (signal-constant value (signal-filter same-button? #f signal)))
- (signal-merge (button-filter #f button-last-up)
- (button-filter #t button-last-down)))
diff --git a/sly/input/keyboard.scm b/sly/input/keyboard.scm
index 574375e..370775c 100644
--- a/sly/input/keyboard.scm
+++ b/sly/input/keyboard.scm
@@ -22,7 +22,7 @@
;;; Code:
(define-module (sly input keyboard)
- #:use-module ((sdl sdl) #:prefix SDL:)
+ #:use-module ((sdl2 events) #:prefix sdl2:)
#:use-module (sly event)
#:use-module (sly signal)
#:use-module (sly math vector)
@@ -35,33 +35,25 @@
key-arrows
key-wasd))
-(define key-press-hook (make-hook 2))
+(define key-press-hook (make-hook 1))
(register-event-handler
'key-down
(lambda (e)
- (run-hook key-press-hook
- (SDL:event:key:keysym:sym e)
- (SDL:event:key:keysym:unicode e))))
+ (run-hook key-press-hook (sdl2:keyboard-event-key e))))
(define-signal key-last-down
- (hook->signal key-press-hook 'none
- (lambda (key unicode)
- key)))
+ (hook->signal key-press-hook 'none identity))
-(define key-release-hook (make-hook 2))
+(define key-release-hook (make-hook 1))
(register-event-handler
'key-up
(lambda (e)
- (run-hook key-release-hook
- (SDL:event:key:keysym:sym e)
- (SDL:event:key:keysym:unicode e))))
+ (run-hook key-release-hook (sdl2:keyboard-event-key e))))
(define-signal key-last-up
- (hook->signal key-release-hook 'none
- (lambda (key unicode)
- key)))
+ (hook->signal key-release-hook 'none identity))
(define (key-down? key)
"Create a signal for the state of KEY. The signal value is #t when
diff --git a/sly/input/mouse.scm b/sly/input/mouse.scm
index d8ff1c6..c6d2b96 100644
--- a/sly/input/mouse.scm
+++ b/sly/input/mouse.scm
@@ -22,7 +22,8 @@
;;; Code:
(define-module (sly input mouse)
- #:use-module ((sdl sdl) #:prefix SDL:)
+ #:use-module (ice-9 match)
+ #:use-module ((sdl2 events) #:prefix sdl2:)
#:use-module (sly window)
#:use-module (sly event)
#:use-module (sly signal)
@@ -37,57 +38,56 @@
mouse-last-up
mouse-down?))
-(define mouse-move-hook (make-hook 2))
+(define mouse-move-hook (make-hook 1))
(register-event-handler
'mouse-motion
(lambda (e)
(run-hook mouse-move-hook
- (SDL:event:motion:x e)
- (SDL:event:motion:y e))))
+ (vector2 (sdl2:mouse-motion-event-x e)
+ (sdl2:mouse-motion-event-y e)))))
(define-signal mouse-position
(hook->signal mouse-move-hook
(vector2 0 0)
;; Sly uses the bottom-left as the origin, so invert
;; the y-axis for convenience.
- (lambda (x y)
- (vector2 x (- (signal-ref window-height) y)))))
+ (match-lambda
+ (($ <vector2> x y)
+ (vector2 x (- (signal-ref window-height) y))))))
(define-signal mouse-x (signal-map vx mouse-position))
(define-signal mouse-y (signal-map vy mouse-position))
-(define mouse-press-hook (make-hook 3))
+(define mouse-press-hook (make-hook 2))
(register-event-handler
'mouse-button-down
(lambda (e)
(run-hook mouse-press-hook
- (SDL:event:button:button e)
- (SDL:event:button:x e)
- (SDL:event:button:y e))))
+ (sdl2:mouse-button-event-button e)
+ (vector2 (sdl2:mouse-button-event-x e)
+ (sdl2:mouse-button-event-y e)))))
(define-signal mouse-last-down
(hook->signal mouse-press-hook
'none
- (lambda (button x y)
- button)))
+ (lambda (button position) button)))
-(define mouse-click-hook (make-hook 3))
+(define mouse-click-hook (make-hook 2))
(register-event-handler
'mouse-button-up
(lambda (e)
(run-hook mouse-click-hook
- (SDL:event:button:button e)
- (SDL:event:button:x e)
- (SDL:event:button:y e))))
+ (sdl2:mouse-button-event-button e)
+ (vector2 (sdl2:mouse-button-event-x e)
+ (sdl2:mouse-button-event-y e)))))
(define-signal mouse-last-up
(hook->signal mouse-click-hook
'none
- (lambda (button x y)
- button)))
+ (lambda (button position) button)))
(define (mouse-down? button)
"Create a signal for the state of BUTTON. Value is #t when mouse