Skip to content

Commit 4a11d7e

Browse files
committed
broken stop function.
1 parent 5972952 commit 4a11d7e

File tree

1 file changed

+79
-73
lines changed

1 file changed

+79
-73
lines changed

src/rosado/processing/applet.clj

Lines changed: 79 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,79 @@
1-
(ns rosado.processing.applet
2-
(:use [rosado.processing :except (size)])
3-
(:import (javax.swing JFrame)))
4-
5-
(defn- with-binding
6-
"Turn the method map into something one that update-proxy can use."
7-
[methods [method-name f]]
8-
(assoc methods (name method-name)
9-
`(fn [this# & args#]
10-
(binding [*applet* this#]
11-
(apply ~f args#)))))
12-
13-
(defn- fix-mname
14-
"Changes :method-name to :methodName."
15-
[[mname fun]]
16-
(let [mname (name mname)
17-
mr (re-matcher #"\-[a-zA-z]" mname)
18-
replace-fn (comp #(.replaceFirst mr %) toupper #(.substring % 1))
19-
fixed-name (if-let [matched (re-find mr)]
20-
(replace-fn matched)
21-
mname)]
22-
[(keyword fixed-name) fun]))
23-
24-
(defmacro defapplet
25-
"Define an applet. Takes an app-name and a map of options."
26-
[app-name & opts]
27-
(let [options (assoc (apply hash-map opts) :name (str app-name))
28-
fns (dissoc options :name :title :size)
29-
methods (reduce with-binding {} (into {} (map fix-mname fns)))]
30-
`(def ~app-name
31-
(let [frame# (atom nil)
32-
prx# (proxy [processing.core.PApplet
33-
clojure.lang.IMeta] []
34-
(meta [] (assoc ~options :frame frame#)))]
35-
(update-proxy prx# ~methods)
36-
prx#))))
37-
38-
(def #^{:private true}
39-
modes {:JAVA2D JAVA2D :OPENGL OPENGL
40-
:P3D P3D :P2D P2D :PDF PDF})
41-
42-
(defn run
43-
"Launches the applet. If given the flag :interactive, it won't exit
44-
on clicking the close button - it will only dispose the window."
45-
[applet & interactive?]
46-
(.init applet)
47-
(let [m (.meta applet)
48-
[width height & mode] (or (:size m) [200 200])
49-
mode (if-let [kw (first mode)]
50-
(modes (-> kw name toupper keyword))
51-
JAVA2D)
52-
close-op (if (first interactive?)
53-
JFrame/DISPOSE_ON_CLOSE
54-
JFrame/EXIT_ON_CLOSE)]
55-
(.size applet width height mode)
56-
(reset! (:frame m)
57-
(doto (JFrame. (or (:title m) (:name m)))
58-
(.setDefaultCloseOperation close-op)
59-
(.setSize width height)
60-
(.add applet)
61-
(.pack)
62-
(.show)))))
63-
64-
(defn stop [applet]
65-
(.destroy applet)
66-
(.hide @(:frame ^applet)))
67-
68-
(comment ;; Usage:
69-
(defapplet growing-triangle
70-
:draw (fn [] (line 10 10 (frame-count) 100)))
71-
72-
(run growing-triangle)
73-
(stop growing-triangle))
1+
(ns rosado.processing.applet
2+
(:use [rosado.processing :except (size)])
3+
(:import (javax.swing JFrame)))
4+
5+
(defn- with-binding
6+
"Turn the method map into something one that update-proxy can use."
7+
[methods [method-name f]]
8+
(assoc methods (name method-name)
9+
`(fn [this# & args#]
10+
(binding [*applet* this#]
11+
(apply ~f args#)))))
12+
13+
(defn- fix-mname
14+
"Changes :method-name to :methodName."
15+
[[mname fun]]
16+
(let [mname (name mname)
17+
mr (re-matcher #"\-[a-zA-z]" mname)
18+
replace-fn (comp #(.replaceFirst mr %) toupper #(.substring % 1))
19+
fixed-name (if-let [matched (re-find mr)]
20+
(replace-fn matched)
21+
mname)]
22+
[(keyword fixed-name) fun]))
23+
24+
(defmacro defapplet
25+
"Define an applet. Takes an app-name and a map of options."
26+
[app-name & opts]
27+
(let [options (assoc (apply hash-map opts) :name (str app-name))
28+
fns (dissoc options :name :title :size)
29+
methods (reduce with-binding {} (into {} (map fix-mname fns)))]
30+
`(def ~app-name
31+
(let [frame# (atom nil)
32+
prx# (proxy [processing.core.PApplet
33+
clojure.lang.IMeta] []
34+
(meta [] (assoc ~options :frame frame#)))]
35+
(update-proxy prx# ~methods)
36+
prx#))))
37+
38+
(def #^{:private true}
39+
modes {:JAVA2D JAVA2D :OPENGL OPENGL
40+
:P3D P3D :P2D P2D :PDF PDF})
41+
42+
(defn run
43+
"Launches the applet. If given the flag :interactive, it won't exit
44+
on clicking the close button - it will only dispose the window."
45+
[applet & interactive?]
46+
(.init applet)
47+
(let [m (.meta applet)
48+
[width height & mode] (or (:size m) [200 200])
49+
mode (if-let [kw (first mode)]
50+
(modes (-> kw name toupper keyword))
51+
JAVA2D)
52+
close-op (if (first interactive?)
53+
JFrame/DISPOSE_ON_CLOSE
54+
JFrame/EXIT_ON_CLOSE)]
55+
(.size applet width height mode)
56+
(reset! (:frame m)
57+
(doto (JFrame. (or (:title m) (:name m)))
58+
(.setDefaultCloseOperation close-op)
59+
(.setSize width height)
60+
(.add applet)
61+
(.pack)
62+
(.show)))))
63+
64+
65+
(defn stop [applet]
66+
(let [closing-fn (fn []
67+
(let [frame @(:frame (meta applet))]
68+
(.destroy applet)
69+
(doto frame
70+
(.hide)
71+
(.dispose))))]
72+
(javax.swing.SwingUtilities/invokeAndWait closing-fn)))
73+
74+
(comment ;; Usage:
75+
(defapplet growing-triangle
76+
:draw (fn [] (line 10 10 (frame-count) 100)))
77+
78+
(run growing-triangle)
79+
(stop growing-triangle))

0 commit comments

Comments
 (0)