Improve input functions
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
(:require [clojure.set :as set]
|
||||
[play-clj.utils :as utils])
|
||||
(:import [com.badlogic.gdx Application Audio Files Game Gdx Graphics Input
|
||||
InputProcessor Net Screen]
|
||||
InputMultiplexer InputProcessor Net Screen]
|
||||
[com.badlogic.gdx.graphics Camera Color GL20 OrthographicCamera
|
||||
PerspectiveCamera Texture]
|
||||
[com.badlogic.gdx.graphics.g2d Animation BitmapFont SpriteBatch
|
||||
|
||||
@@ -34,29 +34,56 @@
|
||||
:y (.getY ^Input (Gdx/input))
|
||||
nil))
|
||||
|
||||
(defmacro input-key
|
||||
(defmacro get-keycode
|
||||
[key]
|
||||
`~(symbol
|
||||
(str utils/gdx-package ".Input$Keys/" (utils/key->static-field key))))
|
||||
|
||||
(defmacro is-pressed?
|
||||
[key]
|
||||
`(.isKeyPressed (Gdx/input) (input-key ~key)))
|
||||
`(.isKeyPressed ^Input (Gdx/input) (get-keycode ~key)))
|
||||
|
||||
(defn input
|
||||
[& {:keys [key-down key-typed key-up mouse-moved
|
||||
scrolled touch-down touch-dragged touch-up]
|
||||
:or {key-down dummy key-typed dummy key-up dummy mouse-moved dummy
|
||||
scrolled dummy touch-down dummy touch-dragged dummy touch-up dummy}}]
|
||||
(reify InputProcessor
|
||||
(keyDown [this keycode] (key-down keycode))
|
||||
(keyTyped [this character] (key-typed character))
|
||||
(keyUp [this keycode] (key-up keycode))
|
||||
(mouseMoved [this screen-x screen-y] (mouse-moved screen-x screen-y))
|
||||
(scrolled [this amount] (scrolled amount))
|
||||
(touchDown [this screen-x screen-y pointer button]
|
||||
(touch-down screen-x screen-y pointer button))
|
||||
(touchDragged [this screen-x screen-y pointer]
|
||||
(touch-dragged screen-x screen-y pointer))
|
||||
(touchUp [this screen-x screen-y pointer button]
|
||||
(touch-up screen-x screen-y pointer button))))
|
||||
(let [return-fn (fn [return-val]
|
||||
(some #(not (nil? %)) [return-val false]))]
|
||||
(reify InputProcessor
|
||||
(keyDown [this keycode]
|
||||
(return-fn (key-down keycode)))
|
||||
(keyTyped [this character]
|
||||
(return-fn (key-typed character)))
|
||||
(keyUp [this keycode]
|
||||
(return-fn (key-up keycode)))
|
||||
(mouseMoved [this screen-x screen-y]
|
||||
(return-fn (mouse-moved screen-x screen-y)))
|
||||
(scrolled [this amount]
|
||||
(return-fn (scrolled amount)))
|
||||
(touchDown [this screen-x screen-y pointer button]
|
||||
(return-fn (touch-down screen-x screen-y pointer button)))
|
||||
(touchDragged [this screen-x screen-y pointer]
|
||||
(return-fn (touch-dragged screen-x screen-y pointer)))
|
||||
(touchUp [this screen-x screen-y pointer button]
|
||||
(return-fn (touch-up screen-x screen-y pointer button))))))
|
||||
|
||||
(defmacro input-multi
|
||||
[& args]
|
||||
`(InputMultiplexer. ~@args))
|
||||
|
||||
(defn set-input!
|
||||
[^InputProcessor p]
|
||||
(.setInputProcessor ^Input (Gdx/input) p))
|
||||
|
||||
(defn add-input!
|
||||
[^InputProcessor p]
|
||||
(.addProcessor ^InputMultiplexer (.getInputProcessor (Gdx/input)) p))
|
||||
|
||||
(defn remove-input!
|
||||
[^InputProcessor p]
|
||||
(.removeProcessor ^InputMultiplexer (.getInputProcessor (Gdx/input)) p))
|
||||
|
||||
(defn clear-inputs!
|
||||
[]
|
||||
(.clear ^InputMultiplexer (.getInputProcessor (Gdx/input))))
|
||||
|
||||
@@ -62,6 +62,10 @@
|
||||
[k & options]
|
||||
`(utils/call! ^Input (Gdx/input) ~k ~@options))
|
||||
|
||||
(defmacro input-multi!
|
||||
[obj k & options]
|
||||
`(utils/call! ^InputMultiplexer ~obj ~k ~@options))
|
||||
|
||||
(defmacro net!
|
||||
[k & options]
|
||||
`(utils/call! ^Net (Gdx/net) ~k ~@options))
|
||||
|
||||
Reference in New Issue
Block a user