Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

events.clj

  • Loading branch information...
commit df73ea7b8cf2b3663ddac6f7317216e4589c3986 1 parent 7e87576
@gavilancomun authored
View
61 src/jinput_overtone/core.clj 100755 → 100644
@@ -1,13 +1,9 @@
(ns jinput-overtone.core
(:require [overtone.config.log :as log]
- [overtone.libs.event :as evnt])
- (:use [clojure.math.numeric-tower :only [abs]])
- (import (net.java.games.input Component Controller)))
+ [clojure.math.numeric-tower :as math]))
(def DELAY 30)
-(def NO_DIFFS {})
-
(def ZERO 0.0)
(def NEAR_ZERO 0.1)
@@ -16,55 +12,28 @@
(def LIMIT 1.0)
-(defn find-name [item]
- (cond
- (instance? Component) (.getName item)
- (instance? Controller) (.getName item)
- :else (.toString item)))
-
-(defn message-path [controller component]
- [:jinput controller (find-name component)])
-
(defn poll-state [controller]
(if (.poll controller)
(into {} (map #(-> [% (.getPollData %)]) (.getComponents controller)))
- NO_DIFFS))
+ {}))
(defn poll-states [controllers]
(into {} (map #(-> [% (poll-state %)]) controllers)))
-(defn event-send [event-type value]
- (evnt/event event-type {:val value}))
-
-(defn controller-event-handlers
- "parameters are a controller and a map of components to functions"
- [controller handlers]
- (when controller
- (doseq [[k v] handlers]
- (evnt/on-event (message-path controller k) v (keyword k)))))
-
-(defn send-diffs
- "Send diffs if there are any"
- [sender controller diffs]
- (when (not= diffs NO_DIFFS)
- (println (.getName controller) diffs)
- (doseq [[k v] diffs]
- (sender (message-path controller k) v))))
-
-(defn send-and-store [controller old-state sender diffs]
- (send-diffs sender controller diffs)
+(defn send-and-store [controller old-state send-fn diffs]
+ (send-fn controller diffs)
[controller (conj old-state diffs)])
(defn different-values? [old new]
(and
(not= old new)
(or
- (> (abs (- old new)) CONSIDER_SAME)
- (= LIMIT (abs new))
- (= ZERO (abs new)))))
+ (> (math/abs (- old new)) CONSIDER_SAME)
+ (= LIMIT (math/abs new))
+ (= ZERO (math/abs new)))))
(defn round-to-zero [[k v]]
- (if (< (abs v) NEAR_ZERO)
+ (if (< (math/abs v) NEAR_ZERO)
[k ZERO]
[k v]))
@@ -76,15 +45,5 @@
(filter
(fn [[k v]] (different-values? (get previous-state k) v)) current-state))))
-(defn new-state [controller old-state sender]
- (send-and-store controller old-state sender (diff-state old-state (poll-state controller))))
-
-(defn loop-states [controllers sender old-states]
- (loop [states old-states]
- (Thread/sleep DELAY)
- (recur
- (into {}
- (map #(new-state % (get states %) sender) controllers)))))
-
-(defn loop-controllers [controllers sender]
- (loop-states controllers sender (poll-states controllers)))
+(defn new-state [controller old-state send-fn]
+ (send-and-store controller old-state send-fn (diff-state old-state (poll-state controller))))
View
29 src/jinput_overtone/events.clj
@@ -0,0 +1,29 @@
+(ns jinput-overtone.events
+ (:require [overtone.libs.event :as evnt])
+ (import (net.java.games.input Component Controller)))
+
+(defn- find-name [item]
+ (cond
+ (instance? Component) (.getName item)
+ (instance? Controller) (.getName item)
+ :else (.toString item)))
+
+(defn mk-event-type [controller component]
+ [:jinput controller (find-name component)])
+
+(defn event-send [event-type value]
+ (evnt/event event-type {:val value}))
+
+(defn send-diffs
+ "Send diffs if there are any"
+ [controller diffs]
+ (when (not= diffs {})
+ (doseq [[k v] diffs]
+ (event-send (mk-event-type controller k) v))))
+
+(defn controller-event-handlers
+ "parameters are a controller and a map of components to functions"
+ [controller handlers]
+ (when controller
+ (doseq [[k v] handlers]
+ (evnt/on-event (mk-event-type controller k) v (keyword k)))))
View
9 src/jinput_overtone/finders.clj
@@ -1,6 +1,5 @@
(ns jinput-overtone.finders
- (:use [jinput-overtone.core])
- (import (net.java.games.input ControllerEnvironment)))
+ (import (net.java.games.input ControllerEnvironment Component Controller)))
(defn controller-type [controller]
(-> controller .getType .toString))
@@ -29,5 +28,11 @@
(defn find-gamepads []
(find-controllers gamepad?))
+(defn- find-name [item]
+ (cond
+ (instance? Component) (.getName item)
+ (instance? Controller) (.getName item)
+ :else (.toString item)))
+
(defn find-controller [pattern]
(first (find-controllers #(re-find (re-pattern (str "(?i)" pattern)) (find-name %)))))
View
14 src/jinput_overtone/loop.clj 100755 → 100644
@@ -1,9 +1,19 @@
(ns jinput-overtone.loop
(:use [jinput-overtone.core]
- [jinput-overtone.finders]))
+ [jinput-overtone.finders]
+ [jinput-overtone.events]))
+
+(defn loop-states
+ ([controllers send-fn] (loop-states controllers send-fn (poll-states controllers)))
+ ([controllers send-fn old-states]
+ (loop [states old-states]
+ (Thread/sleep DELAY)
+ (recur
+ (into {}
+ (map #(new-state % (get states %) send-fn) controllers))))))
(defn event-loop-game []
- (loop-controllers (find-controllers game-controller?) #'event-send))
+ (loop-states (find-controllers game-controller?) #'send-diffs))
(defn start-input []
(let [t (Thread. event-loop-game)]
View
1  src/jinput_overtone/percussion_toy.clj
@@ -5,6 +5,7 @@
[midiutils-overtone.core]
[jinput-overtone.core]
[jinput-overtone.finders]
+ [jinput-overtone.events]
[jinput-overtone.handlers]
[jinput-overtone.loop]))

0 comments on commit df73ea7

Please sign in to comment.
Something went wrong with that request. Please try again.