Skip to content

Commit

Permalink
Fix defapplet macro's use of bind-applets.
Browse files Browse the repository at this point in the history
  • Loading branch information
technomancy committed Sep 3, 2009
1 parent bf5877c commit 66de837
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/rosado/processing/applet.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@
[clojure.contrib.java-utils :only [as-str]])
(:import (java.awt Frame)))

(defn bind-applet [f]
(fn [this & args]
(binding [*applet* this] (apply f args))))
(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)))))

(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)
;; TODO: fix this to automatically bind *applet* in fns
;; methods (zipmap (map name (keys fns))
;; (map bind-applet (vals fns)))
]
fns (dissoc options :name :title :height :width)]
`(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# ~fns)
(update-proxy prx# methods#)
prx#))))

(defn run [applet]
Expand All @@ -40,3 +41,10 @@
(defn stop [applet]
(.destroy applet)
(.hide @(:frame ^applet)))

(comment ;; Usage:
(defapplet growing-triangle
:draw (fn [] (line 10 10 (frame-count) 100)))

(run growing-triangle)
(stop growing-triangle))

0 comments on commit 66de837

Please sign in to comment.