Skip to content

Commit

Permalink
run takes :interactive, bind-applets is private
Browse files Browse the repository at this point in the history
  • Loading branch information
rosado committed Sep 4, 2009
1 parent 4e762d4 commit 2636bec
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions src/rosado/processing/applet.clj
Original file line number Diff line number Diff line change
@@ -1,38 +1,44 @@
(ns rosado.processing.applet
(:use [rosado.processing]
[clojure.contrib.java-utils :only [as-str]])
(:import (java.awt Frame)))
(:use [rosado.processing])
(:import (javax.swing JFrame)))

(defn bind-applets
(defn- bind-applets
"Turn the method map into something one that update-proxy can use."
[methods [method-name f]]
(assoc methods (name method-name)
(fn [this & args]
(binding [*applet* this]
(apply f args)))))
`(fn [this# & args#]
(binding [*applet* this#]
(apply ~f args#)))))

(defmacro defapplet
"Define an applet. Takes an app-name and a map of options."
[app-name & opts]
(let [options (assoc (apply hash-map opts) :name (str app-name))
fns (dissoc options :name :title :height :width)]
fns (dissoc options :name :title :height :width)
methods (reduce bind-applets {} fns)]
`(def ~app-name
(let [frame# (atom nil)
methods# (reduce bind-applets {} ~fns)
prx# (proxy [processing.core.PApplet
clojure.lang.IMeta] []
(meta [] (assoc ~options :frame frame#)))]
(update-proxy prx# methods#)
(update-proxy prx# ~methods)
prx#))))

(defn run [applet]
(defn run
"Launches the applet. If given the flag :interactive, it won't exit
on clicking the close button - it will only dispose the window."
[applet & interactive?]
(.init applet)
(let [m (.meta applet)
width (or (:width m) 200)
height (or (:height m) 200)]
height (or (:height m) 200)
close-op (if (first interactive?)
JFrame/DISPOSE_ON_CLOSE
JFrame/EXIT_ON_CLOSE)]
(.size applet width height)
(reset! (:frame m)
(doto (Frame. (or (:title m) (:name m)))
(doto (JFrame. (or (:title m) (:name m)))
(.setDefaultCloseOperation close-op)
(.setSize width height)
(.add applet)
(.pack)
Expand Down

0 comments on commit 2636bec

Please sign in to comment.