Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add mouse-wheel callback.

  • Loading branch information...
commit add43af5e1c7d06c92c286d078de5165ca4248b9 1 parent 63ab5d4
@nbeloglazov nbeloglazov authored
View
42 examples/mouse_wheel.clj
@@ -0,0 +1,42 @@
+(ns quil.examples.mouse-wheel
+ (:use quil.core
+ [quil.helpers.drawing :only [line-join-points]]
+ [quil.helpers.calc :only [mul-add]]))
+
+(def radius 100)
+
+(defn setup []
+ (background 255)
+ (set-state! :zoom (atom 1))
+ (stroke 0))
+
+(defn draw []
+ (background 255)
+ (translate (/ (width) 2.0) (/ (height) 2.0))
+ (scale @(state :zoom))
+ (rotate-y (* (mouse-x) 0.02))
+ (rotate-x (* (mouse-y) 0.02))
+ (let [line-args (for [t (range 0 180)]
+ (let [s (* t 18)
+ radian-s (radians s)
+ radian-t (radians t)
+ x (* radius (cos radian-s) (sin radian-t))
+ y (* radius (sin radian-s) (sin radian-t))
+ z (* radius (cos radian-t))]
+ [x y z]))]
+ (dorun
+ (map #(apply line %) (line-join-points line-args)))))
+
+(defn mouse-wheel [rotation]
+ (swap! (state :zoom) #(max 1 (- % (* 0.1 rotation)))))
+
+(defsketch gen-art-29
+ :title "Spiral Sphere"
+ :setup setup
+ :draw draw
+ :size [500 300]
+ :renderer :p3d
+ :mouse-wheel mouse-wheel)
+
+
+
View
19 src/quil/applet.clj
@@ -255,6 +255,16 @@
(generate-listeners)
+(defn wrap-mouse-wheel
+ "Wraps callback to an instance of java.awt.event.MouseEventListener
+ or returns nil if given callback is nil."
+ [mouse-wheel applet]
+ (when mouse-wheel
+ (reify java.awt.event.MouseWheelListener
+ (mouseWheelMoved [this e]
+ (with-applet applet
+ (mouse-wheel (.getWheelRotation e)))))))
+
(defn applet
"Create and start a new visualisation applet.
@@ -307,6 +317,12 @@
:mouse-dragged - Called every time the mouse moves and a button is
pressed.
+ :mouse-wheel - Called every time mouse wheel is rotated.
+ Takes 1 argument - wheel rotation, an int.
+ Negative values if the mouse wheel was rotated
+ up/away from the user, and positive values
+ if the mouse wheel was rotated down/ towards the user
+
:key-pressed - Called every time any key is pressed.
:key-released - Called every time any key is released.
@@ -333,6 +349,9 @@
setup-fn (fn []
(let [size-vec (concat size [renderer] output-file)]
(apply applet-set-size size-vec))
+ (when-let [wheel-listener (wrap-mouse-wheel (:mouse-wheel options)
+ (current-applet))]
+ (.addMouseWheelListener (current-applet) wheel-listener))
(when-let [f (:setup options)]
(f)))
safe-draw-fn (fn []
View
6 src/quil/core.clj
@@ -4418,6 +4418,12 @@
:mouse-dragged - Called every time the mouse moves and a button is
pressed.
+ :mouse-wheel - Called every time mouse wheel is rotated.
+ Takes 1 argument - wheel rotation, an int.
+ Negative values if the mouse wheel was rotated
+ up/away from the user, and positive values
+ if the mouse wheel was rotated down/ towards the user
+
:key-pressed - Called every time any key is pressed.
:key-released - Called every time any key is released.
Please sign in to comment.
Something went wrong with that request. Please try again.