Add gesture detection

This commit is contained in:
oakes
2014-01-10 13:22:14 -05:00
parent 85dff89682
commit c1c04f1118
2 changed files with 59 additions and 29 deletions

View File

@@ -7,6 +7,8 @@
PerspectiveCamera Texture] PerspectiveCamera Texture]
[com.badlogic.gdx.graphics.g2d Animation BitmapFont SpriteBatch [com.badlogic.gdx.graphics.g2d Animation BitmapFont SpriteBatch
TextureRegion] TextureRegion]
[com.badlogic.gdx.input GestureDetector
GestureDetector$GestureListener]
[com.badlogic.gdx.maps MapLayer MapLayers] [com.badlogic.gdx.maps MapLayer MapLayers]
[com.badlogic.gdx.maps.tiled TiledMap TiledMapTileLayer TmxMapLoader] [com.badlogic.gdx.maps.tiled TiledMap TiledMapTileLayer TmxMapLoader]
[com.badlogic.gdx.maps.tiled.renderers [com.badlogic.gdx.maps.tiled.renderers
@@ -80,7 +82,8 @@
:pause #(execute-fn! on-pause @screen) :pause #(execute-fn! on-pause @screen)
:resize #(execute-fn! on-resize @screen) :resize #(execute-fn! on-resize @screen)
:resume #(execute-fn! on-resume @screen) :resume #(execute-fn! on-resume @screen)
:input (input* options execute-priority-fn!)})) :input (input* options execute-priority-fn!)
:gesture (gesture* options execute-priority-fn!)}))
(defmacro defscreen (defmacro defscreen
[n & {:keys [] :as options}] [n & {:keys [] :as options}]
@@ -94,7 +97,7 @@
[{:keys [on-create] :or {on-create dummy}}] [{:keys [on-create] :or {on-create dummy}}]
(proxy [Game] [] (proxy [Game] []
(create [] (create []
(set-input! (InputMultiplexer.)) (input! :setInputProcessor (InputMultiplexer.))
(on-create this)))) (on-create this))))
(defmacro defgame (defmacro defgame
@@ -105,7 +108,8 @@
[^Game game & screens] [^Game game & screens]
(let [add-inputs! (fn [] (let [add-inputs! (fn []
(doseq [screen screens] (doseq [screen screens]
(add-input! (:input screen)))) (add-input! (:input screen))
(add-input! (:gesture screen))))
run-fn! (fn [k & args] run-fn! (fn [k & args]
(doseq [screen screens] (doseq [screen screens]
(apply (get screen k) args)))] (apply (get screen k) args)))]

View File

@@ -50,41 +50,71 @@
scrolled dummy touch-down dummy touch-dragged dummy touch-up dummy}} scrolled dummy touch-down dummy touch-dragged dummy touch-up dummy}}
execute-fn!] execute-fn!]
(reify InputProcessor (reify InputProcessor
(keyDown [this keycode] (keyDown [this k]
(execute-fn! key-down {:keycode keycode}) (execute-fn! key-down {:keycode k})
false) false)
(keyTyped [this character] (keyTyped [this c]
(execute-fn! key-typed {:character character}) (execute-fn! key-typed {:character c})
false) false)
(keyUp [this keycode] (keyUp [this k]
(execute-fn! key-up {:keycode keycode}) (execute-fn! key-up {:keycode k})
false) false)
(mouseMoved [this screen-x screen-y] (mouseMoved [this sx sy]
(execute-fn! mouse-moved {:screen-x screen-x :screen-y screen-y}) (execute-fn! mouse-moved {:screen-x sx :screen-y sy})
false) false)
(scrolled [this amount] (scrolled [this a]
(execute-fn! scrolled {:amount amount}) (execute-fn! scrolled {:amount a})
false) false)
(touchDown [this screen-x screen-y pointer button] (touchDown [this sx sy p b]
(execute-fn! touch-down {:screen-x screen-x :screen-y screen-y (execute-fn! touch-down {:screen-x sx :screen-y sy :pointer p :button b})
:pointer pointer :button button})
false) false)
(touchDragged [this screen-x screen-y pointer] (touchDragged [this sx sy p]
(execute-fn! touch-dragged (execute-fn! touch-dragged {:screen-x sx :screen-y sy :pointer p})
{:screen-x screen-x :screen-y screen-y :pointer pointer})
false) false)
(touchUp [this screen-x screen-y pointer button] (touchUp [this sx sy p b]
(execute-fn! touch-up {:screen-x screen-x :screen-y screen-y (execute-fn! touch-up {:screen-x sx :screen-y sy :pointer p :button b})
:pointer pointer :button button})
false))) false)))
(defmacro input (defmacro input
[& args] [& args]
`(input* ~args (fn [func# options#] (func# options#)))) `(input* ~args (fn [func# options#] (func# options#))))
(defn set-input! (defn gesture*
[^InputProcessor p] [{:keys [fling long-press pan pan-stop pinch tap zoom]
(.setInputProcessor ^Input (Gdx/input) p)) :or {fling dummy long-press dummy pan dummy pan-stop dummy
pinch dummy tap dummy zoom dummy}}
execute-fn!]
(let [listener
(reify GestureDetector$GestureListener
(fling [this vx vy b]
(execute-fn! fling {:velocity-x vx :velocity-y vy :button b})
false)
(longPress [this x y]
(execute-fn! long-press {:x x :y y})
false)
(pan [this x y dx dy]
(execute-fn! pan {:x x :y y :delta-x dx :delta-y dy})
false)
(panStop [this x y p b]
(execute-fn! pan-stop {:x x :y y :pointer p :button b})
false)
(pinch [this ip1 ip2 p1 p2]
(execute-fn! pinch {:initial-pointer-1 ip1 :initial-pointer-2 ip2
:pointer1 p1 :pointer2 p2})
false)
(tap [this x y c b]
(execute-fn! tap {:x x :y y :count c :button b})
false)
(touchDown [this x y p b]
false)
(zoom [this id d]
(execute-fn! zoom {:initial-distance id :distance d})
false))]
(proxy [GestureDetector] [listener])))
(defmacro gesture
[& args]
`(gesture* ~args (fn [func# options#] (func# options#))))
(defn add-input! (defn add-input!
[^InputProcessor p] [^InputProcessor p]
@@ -93,7 +123,3 @@
(defn remove-input! (defn remove-input!
[^InputProcessor p] [^InputProcessor p]
(.removeProcessor ^InputMultiplexer (.getInputProcessor (Gdx/input)) p)) (.removeProcessor ^InputMultiplexer (.getInputProcessor (Gdx/input)) p))
(defn clear-inputs!
[]
(.clear ^InputMultiplexer (.getInputProcessor (Gdx/input))))