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