Skip to content

Instantly share code, notes, and snippets.

@rolandoam
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rolandoam/789df44adf67450f8461 to your computer and use it in GitHub Desktop.
Save rolandoam/789df44adf67450f8461 to your computer and use it in GitHub Desktop.
text buttons & actions in play-clj
(defscreen main-screen
:on-show
(fn [screen entities]
(let [ui-skin (skin "uiskin.json")
;;; this is the trick, we use a proxy (http://clojure.org/java_interop#toc25) to implement a ClickListener
cb (proxy [ClickListener] []
;;; for this object, we only care about 'clicked'
(clicked [evt x y] (println "clicked at " x y evt)))]
(update! screen :renderer (stage) :camera (orthographic))
;;; add a couple of entities to the screen. We could've used a table here, but
;;; for testing purposes, I'm hard coding the positions.
;;; as a test, we use the same listener for both buttons
[(assoc (text-button "I'm a text button" ui-skin
:add-listener ^EventListener cb)
:x 5 :y 300)
(assoc (text-button "I'm another text button" ui-skin
:add-listener ^EventListener cb)
:x 5 :y 180)
;;; and the clojure logo, of course :)
(assoc (texture "clojure.png")
:x 50 :y 50 :width 100 :height 100)]))
@oakes
Copy link

oakes commented May 8, 2014

Cool! You can also create an :on-ui-clicked or :on-ui-changed function in your defscreen. For example, my ui-gallery example uses the latter. Sorry for not documenting this; I will add all the possible defscreen functions to the docs next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment