Skip to content
This repository has been archived by the owner on Oct 25, 2023. It is now read-only.

Commit

Permalink
Switch defapplet to take an options hash. (work-in-progress)
Browse files Browse the repository at this point in the history
  • Loading branch information
technomancy committed Sep 2, 2009
1 parent b25bb90 commit bf5877c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 28 deletions.
11 changes: 5 additions & 6 deletions examples/example2.clj
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
(vertex 50 -50)
(vertex -50 -50)
(end-shape CLOSE)))
(filter-kind INVERT)
(framerate 10))
(filter-kind INVERT))

(defn setup []
"Runs once."
Expand All @@ -35,9 +34,9 @@

;; Now we just need to define an applet:

(defapplet example2 "An example."
setup draw 200 200)
(defapplet example2 :title "An example."
:setup setup :draw draw :width 200 :height 200)

(run-example2)
(run example2)

;; (stop-example2)
;; (stop example2)
59 changes: 37 additions & 22 deletions src/rosado/processing/applet.clj
Original file line number Diff line number Diff line change
@@ -1,27 +1,42 @@
(ns rosado.processing.applet
(:use rosado.processing)
(:use [rosado.processing]
[clojure.contrib.java-utils :only [as-str]])
(:import (java.awt Frame)))

(defn bind-applet [f]
(fn [this & args]
(binding [*applet* this] (apply f args))))

(defmacro defapplet
[name title setup draw width height]
`(do
(def ~name
(proxy [processing.core.PApplet] []
(setup []
(binding [*applet* ~'this]
(size ~width ~height)
(~setup)))
(draw []
(binding [*applet* ~'this]
(~draw)))))
(defn ~(symbol (str "run-" name)) []
(.init ~name)
(def ~(symbol (str name "-frame")) (doto (Frame. ~title)
(.setSize ~width ~height)
(.add ~name)
(.pack)
(.show))))
"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)))
]
`(def ~app-name
(let [frame# (atom nil)
prx# (proxy [processing.core.PApplet
clojure.lang.IMeta] []
(meta [] (assoc ~options :frame frame#)))]
(update-proxy prx# ~fns)
prx#))))

(defn run [applet]
(.init applet)
(let [m (.meta applet)
width (or (:width m) 200)
height (or (:height m) 200)]
(.size applet width height)
(reset! (:frame m)
(doto (Frame. (or (:title m) (:name m)))
(.setSize width height)
(.add applet)
(.pack)
(.show)))))

(defn ~(symbol (str "stop-" name)) []
(.destroy ~name)
(.hide ~(symbol (str name "-frame"))))))
(defn stop [applet]
(.destroy applet)
(.hide @(:frame ^applet)))

This comment has been minimized.

Copy link
@leifwalsh

leifwalsh Apr 29, 2010

I get the following problem:

rosado.processing.applet=> (defn stop [applet]
  (.destroy applet)
  (.hide @(:frame ^applet)))
java.lang.Exception: Unmatched delimiter: )
java.lang.Exception: Unmatched delimiter: )
java.lang.Exception: Unmatched delimiter: )

What's the deal? I think there's a wonky reader macro going on here.

This comment has been minimized.

Copy link
@rosado

rosado Apr 29, 2010

Owner

Right. If you're using bleeding edge Clojure version, this no longer works.

Another thing: this should be executed on the gui thread.

Both issued are now fixed. Thanks for pointing this out.

0 comments on commit bf5877c

Please sign in to comment.