Skip to content
This repository
Browse code

Replace proxy with gen-class.

  • Loading branch information...
commit c3205e10644b7f5f07c11d60ce0a9f665dbdd5e8 1 parent 6f3ca31
Nikita Beloglazov authored November 05, 2012
3  project.clj
@@ -12,4 +12,5 @@
12 12
                  [org.clojars.processing-core/org.processing.jogl "1.5.1"]
13 13
                  [org.clojars.processing-core/org.processing.opengl "1.5.1"]
14 14
                  [org.clojars.processing-core/org.processing.itext "1.5.1"]
15  
-                 [org.clojars.processing-core/org.processing.pdf "1.5.1"]])
  15
+                 [org.clojars.processing-core/org.processing.pdf "1.5.1"]]
  16
+  :aot [quil.applet])
225  src/quil/applet.clj
@@ -6,7 +6,8 @@
6 6
            (javax.swing JFrame)
7 7
            (java.awt.event WindowListener))
8 8
   (:use [quil.util :only [resolve-constant-key]]
9  
-        [clojure.stacktrace :only [print-cause-trace]]))
  9
+        [clojure.stacktrace :only [print-cause-trace]])
  10
+  (:require [clojure.string :as string]))
10 11
 
11 12
 (defn applet-safe-exit
12 13
   "Similar to the exit method on PApplet, but doesn't kill the current
@@ -169,6 +170,98 @@
169 170
     (throw (IllegalArgumentException. (str "Invalid target:" target". Was expecting one of: " (vec VALID-TARGETS)))))
170 171
   target)
171 172
 
  173
+(defn- to-method-name [keyword]
  174
+  (-> keyword
  175
+      name
  176
+      (string/replace
  177
+       #"-."
  178
+       #(-> % string/upper-case (subs 1)))
  179
+      symbol))
  180
+
  181
+(defn parent-method [method]
  182
+  (symbol (str method "Parent")))
  183
+
  184
+(defn exposed-map [methods]
  185
+  (into {} (for [method methods]
  186
+             [method (parent-method method)])))
  187
+
  188
+(def listeners [:key-pressed
  189
+                :key-released
  190
+                :key-typed
  191
+                :mouse-pressed
  192
+                :mouse-released
  193
+                :mouse-moved
  194
+                :mouse-dragged
  195
+                :mouse-entered
  196
+                :mouse-exited
  197
+                :mouse-clicked
  198
+                :focus-gained
  199
+                :focus-lost])
  200
+
  201
+(def exposed-methods )
  202
+
  203
+(gen-class
  204
+ :name "quil.Applet"
  205
+ :implements [clojure.lang.IMeta]
  206
+ :extends processing.core.PApplet
  207
+ :state state
  208
+ :init quil-init
  209
+ :constructors {[java.util.Map] []}
  210
+ :exposes-methods {keyTyped keyTypedParent
  211
+                   loop loopParent
  212
+                   mouseDragged mouseDraggedParent
  213
+                   keyPressed keyPressedParent
  214
+                   mouseExited mouseExitedParent
  215
+                   mouseClicked mouseClickedParent
  216
+                   mouseEntered mouseEnteredParent
  217
+                   mouseMoved mouseMovedParent
  218
+                   keyReleased keyReleasedParent
  219
+                   mousePressed mousePressedParent
  220
+                   focusGained focusGainedParent
  221
+                   frameRate frameRateParent
  222
+                   mouseReleased mouseReleasedParent
  223
+                   focusLost focusLostParent
  224
+                   noLoop noLoopParent})
  225
+
  226
+(defn -quil-init [state]
  227
+  [[] state])
  228
+
  229
+(defn -meta [this]
  230
+  (.state this))
  231
+
  232
+(defn -setup [this]
  233
+  (println "Setup")
  234
+  (.set applet-tl this)
  235
+  (.set state-tl (:state (.state this)))
  236
+  (.set target-frame-rate-tl 60)
  237
+  ((:setup-fn (.state this))))
  238
+
  239
+(defn -draw [this]
  240
+  ((:draw-fn (.state this))))
  241
+
  242
+(defn -noLoop [this]
  243
+  (reset! (:looping? (.state this)) false)
  244
+  (.noLoopParent this))
  245
+
  246
+(defn -loop [this]
  247
+  (reset! (:looping? (.state this)) true)
  248
+  (.loopParent this))
  249
+
  250
+(defn -frameRate [this new-rate-target]
  251
+  (.set target-frame-rate-tl new-rate-target)
  252
+  (.frameRateParent this new-rate-target))
  253
+
  254
+(defmacro generate-listeners []
  255
+  (letfn [(prefix [v method]
  256
+            (symbol (str v method)))]
  257
+    `(do ~@(for [listener listeners]
  258
+             (let [method (to-method-name listener)]
  259
+               `(defn ~(prefix "-" method)
  260
+                  ([~'this] ((~listener (~'.state ~'this))))
  261
+                  ([~'this ~'evt] (~(prefix "." (parent-method method)) ~'this ~'evt))))))))
  262
+
  263
+(generate-listeners)
  264
+
172 265
 (defn applet
173 266
   "Create and start a new visualisation applet.
174 267
 
@@ -256,128 +349,22 @@
256 349
                                 (println "Exception in Quil draw-fn for sketch" title ": " e "\nstacktrace: " (with-out-str (print-cause-trace e)))
257 350
                                 (Thread/sleep 1000))))
258 351
         draw-fn           (if (:safe-draw-fn options) safe-draw-fn draw-fn)
259  
-        key-pressed-fn    (or (:key-pressed options) (fn [] nil))
260  
-        key-released-fn   (or (:key-released options) (fn [] nil))
261  
-        key-typed-fn      (or (:key-typed options) (fn [] nil))
262  
-        mouse-pressed-fn  (or (:mouse-pressed options) (fn [] nil))
263  
-        mouse-released-fn (or (:mouse-released options) (fn [] nil))
264  
-        mouse-moved-fn    (or (:mouse-moved options) (fn [] nil))
265  
-        mouse-dragged-fn  (or (:mouse-dragged options) (fn [] nil))
266  
-        mouse-entered-fn  (or (:mouse-entered options) (fn [] nil))
267  
-        mouse-exited-fn   (or (:mouse-exited options) (fn [] nil))
268  
-        mouse-clicked-fn  (or (:mouse-clicked options) (fn [] nil))
269  
-        focus-gained-fn   (or (:focus-gained options) (fn [] nil))
270  
-        focus-lost-fn     (or (:focus-lost options) (fn [] nil))
271 352
         on-close-fn       (or (:on-close options) (fn [] nil))
272 353
         state             (atom nil)
273 354
         target-obj        (atom nil)
274 355
         looping?          (atom true)
275  
-        prx-obj           (proxy [processing.core.PApplet
276  
-                                  clojure.lang.IMeta] []
277  
-                            (meta [] (assoc options
278  
-                                       :state state
279  
-                                       :target-obj target-obj
280  
-                                       :target target
281  
-                                       :looping? looping?
282  
-                                       :on-close on-close-fn))
283  
-                            (keyPressed
284  
-                              ([]
285  
-                                 (key-pressed-fn))
286  
-                              ([e]
287  
-                                 (proxy-super keyPressed e)))
288  
-
289  
-                            (keyReleased
290  
-                              ([]
291  
-                                 (key-released-fn))
292  
-                              ([e]
293  
-                                 (proxy-super keyReleased e)))
294  
-
295  
-                            (keyTyped
296  
-                              ([]
297  
-                                 (key-typed-fn))
298  
-                              ([e]
299  
-                                 (proxy-super keyTyped e)))
300  
-
301  
-                            (mousePressed
302  
-                              ([]
303  
-                                 (mouse-pressed-fn))
304  
-                              ([e]
305  
-                                 (proxy-super mousePressed e)))
306  
-
307  
-                            (mouseReleased
308  
-                              ([]
309  
-                                 (mouse-released-fn))
310  
-                              ([e]
311  
-                                 (proxy-super mouseReleased e)))
312  
-
313  
-                            (mouseMoved
314  
-                              ([]
315  
-                                 (mouse-moved-fn))
316  
-                              ([e]
317  
-                                 (proxy-super mouseMoved e)))
318  
-
319  
-                            (mouseDragged
320  
-                              ([]
321  
-                                 (mouse-dragged-fn))
322  
-                              ([e]
323  
-                                 (proxy-super mouseDragged e)))
324  
-
325  
-                            (mouseClicked
326  
-                              ([]
327  
-                                 (mouse-clicked-fn))
328  
-                              ([e]
329  
-                                 (proxy-super mouseClicked e)))
330  
-
331  
-                            (focusGained
332  
-                              ([] nil) ;;The no arg version of the focus
333  
-                              ;;fns don't appear too be called
334  
-                              ([e]
335  
-                                 (proxy-super focusGained e)
336  
-                                 (focus-gained-fn)))
337  
-
338  
-                            (focusLost
339  
-                              ([] nil)
340  
-                              ([e]
341  
-                                 (proxy-super focusLost e)
342  
-                                 (focus-lost-fn)))
343  
-
344  
-                            (mouseEntered
345  
-                              ([] nil)
346  
-                              ([e]
347  
-                                 (proxy-super mouseEntered e)
348  
-                                 (mouse-entered-fn)))
349  
-
350  
-                            (mouseExited
351  
-                              ([] nil)
352  
-                              ([e]
353  
-                                 (proxy-super mouseExited e)
354  
-                                 (mouse-exited-fn)))
355  
-
356  
-                            (setup
357  
-                              ([]
358  
-                                 (.set applet-tl this)
359  
-                                 (.set state-tl state)
360  
-                                 (.set target-frame-rate-tl 60)
361  
-                                 (setup-fn)))
362  
-
363  
-                            (draw
364  
-                              [] (draw-fn))
365  
-
366  
-                            (loop
367  
-                              []
368  
-                              (reset! looping? true)
369  
-                              (proxy-super loop))
370  
-
371  
-                            (noLoop
372  
-                              []
373  
-                              (reset! looping? true)
374  
-                              (proxy-super noLoop))
375  
-
376  
-                            (frameRate
377  
-                              [new-rate-target]
378  
-                              (.set target-frame-rate-tl new-rate-target)
379  
-                              (proxy-super frameRate new-rate-target)))
380  
-        ]
  356
+        listeners         (into {} (for [name listeners]
  357
+                                     [name (or (options name) (fn [] nil))]))
  358
+        applet-state      (merge options
  359
+                                 {:state state
  360
+                                  :target-obj target-obj
  361
+                                  :target target
  362
+                                  :looping? looping?
  363
+                                  :on-close on-close-fn
  364
+                                  :setup-fn setup-fn
  365
+                                  :draw-fn draw-fn}
  366
+                                 listeners)
  367
+        prx-obj           (quil.Applet. applet-state)]
381 368
     (applet-run prx-obj title renderer target)
382 369
     prx-obj))
383 370
 

0 notes on commit c3205e1

Please sign in to comment.
Something went wrong with that request. Please try again.