Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#15] looping functions #26

Merged
merged 6 commits into from
Nov 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/quil.core.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
- [ ] [`load-shader`](https://github.com/quil/quil/blob/2.8.0/src/cljc/quil/core.cljc#L2240) - "Loads a shader into the PShader object"
- [ ] [`load-shape`](https://github.com/quil/quil/blob/2.8.0/src/cljc/quil/core.cljc#L2254) - "Load a geometry from a file as a PShape"
- [ ] [`log`](https://github.com/quil/quil/blob/2.8.0/src/cljc/quil/core.cljc#L2265) - "Calculates the natural logarithm (the base-e logarithm) of a number"
- [ ] [`looping?`](https://github.com/quil/quil/blob/2.8.0/src/cljc/quil/core.cljc#L1705) - "Returns whether the sketch is looping"
- [x] [`looping?`](https://github.com/quil/quil/blob/2.8.0/src/cljc/quil/core.cljc#L1705) - "Returns whether the sketch is looping"
- [ ] [`mag`](https://github.com/quil/quil/blob/2.8.0/src/cljc/quil/core.cljc#L2278) - "Calculates the magnitude (or length) of a vector"
- [ ] [`map-range`](https://github.com/quil/quil/blob/2.8.0/src/cljc/quil/core.cljc#L2297) - "Re-maps a number from one range to another"
- [ ] [`mask-image`](https://github.com/quil/quil/blob/2.8.0/src/cljc/quil/core.cljc#L2313) - "Masks part of an image from displaying by loading another image and using it as an alpha channel"
Expand All @@ -139,7 +139,7 @@
- [ ] [`no-cursor`](https://github.com/quil/quil/blob/2.8.0/src/cljc/quil/core.cljc#L2490) - "Hides the cursor from view"
- [ ] [`no-fill`](https://github.com/quil/quil/blob/2.8.0/src/cljc/quil/core.cljc#L2502) - "Disables filling geometry"
- [ ] [`no-lights`](https://github.com/quil/quil/blob/2.8.0/src/cljc/quil/core.cljc#L2595) - "Disable all lighting"
- [ ] [`no-loop`](https://github.com/quil/quil/blob/2.8.0/src/cljc/quil/core.cljc#L2609) - "Stops Processing from continuously executing the code within draw"
- [x] [`no-loop`](https://github.com/quil/quil/blob/2.8.0/src/cljc/quil/core.cljc#L2609) - "Stops Processing from continuously executing the code within draw"
- [ ] [`no-smooth`](https://github.com/quil/quil/blob/2.8.0/src/cljc/quil/core.cljc#L2664) - "Draws all geometry with jagged (aliased) edges"
- [ ] [`no-stroke`](https://github.com/quil/quil/blob/2.8.0/src/cljc/quil/core.cljc#L2675) - "Disables drawing the stroke (outline)"
- [ ] [`no-tint`](https://github.com/quil/quil/blob/2.8.0/src/cljc/quil/core.cljc#L2688) - "Removes the current fill value for displaying images and reverts to displaying images with their original hues"
Expand Down Expand Up @@ -221,7 +221,7 @@
- [ ] [`spot-light`](https://github.com/quil/quil/blob/2.8.0/src/cljc/quil/core.cljc#L3818) - "Adds a spot light"
- [ ] [`sq`](https://github.com/quil/quil/blob/2.8.0/src/cljc/quil/core.cljc#L3838) - "Squares a number (multiplies a number by itself)"
- [ ] [`sqrt`](https://github.com/quil/quil/blob/2.8.0/src/cljc/quil/core.cljc#L3852) - "Calculates the square root of a number"
- [ ] [`start-loop`](https://github.com/quil/quil/blob/2.8.0/src/cljc/quil/core.cljc#L3867) - "Causes Processing to continuously execute the code within draw"
- [x] [`start-loop`](https://github.com/quil/quil/blob/2.8.0/src/cljc/quil/core.cljc#L3867) - "Causes Processing to continuously execute the code within draw"
- [ ] [`state`](https://github.com/quil/quil/blob/2.8.0/src/cljc/quil/core.cljc#L227) - "Retrieve sketch-specific state by key"
- [ ] [`state-atom`](https://github.com/quil/quil/blob/2.8.0/src/cljc/quil/core.cljc#L202) - "Retrieve sketch-specific state-atom"
- [x] [`stroke`](https://github.com/quil/quil/blob/2.8.0/src/cljc/quil/core.cljc#L3885) - "Sets the color used to draw lines and borders around shapes"
Expand Down
17 changes: 15 additions & 2 deletions src/doodler/core.clje
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

(defn- save-current-stroke
[color]
(set! *sketch* (assoc *sketch* :current-stroke color)))
(u/swap-var! *sketch* assoc :current-stroke color))

(defn current-stroke
[]
Expand Down Expand Up @@ -79,7 +79,7 @@

(defn- save-current-fill
[color]
(set! *sketch* (assoc *sketch* :current-fill color)))
(u/swap-var! *sketch* assoc :current-fill color))

(defn current-fill
[]
Expand Down Expand Up @@ -170,6 +170,19 @@
[]
(- (u/time-mark) (*sketch* :epoch)))

(defn no-loop
[]
(u/swap-var! *sketch* assoc :looping? false))

(defn looping?
[]
(*sketch* :looping?))

(defn start-loop
[]
(u/swap-var! *sketch* assoc :looping? true)
(u/send-message (:pid *sketch*) :refresh))

(defmacro defsketch
[name & opts]
(let [opts (apply hash-map opts)]
Expand Down
91 changes: 91 additions & 0 deletions src/doodler/events.clje
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
(ns doodler.events)

(def event-names
[:set_focus ;; focus-gained
:kill_focus ;; focus-lost
:enter_window ;; mouse-entered
:leave_window ;; mouse-exited
:left_down ;; mouse-pressed
:middle_down ;; mouse-pressed
:right_down ;; mouse-pressed
:left_up ;; mouse-released
:middle_up ;; mouse-released
:right_up ;; mouse-released
:motion ;; mouse-moved
:left_dclick ;; mouse-clicked (twice)
:middle_dclick ;; mouse-clicked (twice)
:right_dclick ;; mouse-clicked (twice)
:mousewheel ;; mouse-wheel
;; Listen to all key events
:key_down ;; key-pressed
:key_up ;; key-released
:char ;; key-typed (?)
])

(defn* event-type->mouse-event
([:enter_window] :mouse-entered)
([:leave_window] :mouse-exited)
([:left_down] :mouse-pressed)
([:middle_down] :mouse-pressed)
([:right_down] :mouse-pressed)
([:left_up] :mouse-released)
([:middle_up] :mouse-released)
([:right_up] :mouse-released)
([:left_dclick] :mouse-clicked)
([:middle_dclick] :mouse-clicked)
([:right_dclick] :mouse-clicked)
([:motion] :mouse-moved)
([:mousewheel] :mouse-wheel))

(defn* event-type->mouse-clicks
([:left_dclick] 2)
([:middle_dclick] 2)
([:right_dclick] 2)
([:left_up] 1)
([:middle_up] 1)
([:right_up] 1)
([_] 0))

(defn* event-type->key-event
([:key_down] :key-pressed)
([:key_up] :key-released)
([:char] :key-typed))

(defn* event->map
([#erl[:wxClose :close_window]]
#erl{:event :on-close})

([#erl[:wxSize :size size rect]]
#erl{:event :on-resize
:size size})

([#erl[:wxFocus :set_focus window]]
#erl{:event :focus-gained
:window window})
([#erl[:wxFocus :kill_focus window]]
#erl{:event :focus-lost
:window window})

([#erl[:wxMouse type
x y
left middle right
control shift alt meta
wheel-rotation wheel-delta lines-per-action]]
#erl{:event (event-type->mouse-event type)
:clicks (event-type->mouse-clicks type)
:x x :y y
:left left :middle middle :right right
:control control :shift shift :alt alt :meta meta
:wheel-rotation wheel-rotation :wheel-delta wheel-delta
:lines-per-action lines-per-action})

([#erl[:wxKey type
x y
key-code control shift alt meta
scan-code uni-char raw-code raw-flags]]
#erl{:event (event-type->key-event type)
:x x :y y
:key-code key-code
:control control :shift shift :alt alt :meta meta
:scan-code scan-code :uni-char uni-char
:raw-code raw-code :raw-flags raw-flags}))
33 changes: 23 additions & 10 deletions src/doodler/examples/mandelbrot.clje
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
minx -2.5
miny -2.0
wh 4.0]
[(+ minx (* wh (/ x w)))
(+ miny (* wh (/ y h)))]))
#erl[(+ minx (* wh (/ x w)))
(+ miny (* wh (/ y h)))]))

(defn ->color
[n max-iters]
(int (* 255 (/ n max-iters))))
(erlang/div (* 255 n) max-iters))

(defn mandelbrot [x y max-iters]
(loop [a 0.0
Expand All @@ -35,10 +35,19 @@
(inc n)))))

(defn mandelbrot-point [x y w h max-iters]
(let [[i j] (->complex x y w h)
n (mandelbrot i j max-iters)
color (->color n max-iters)]
[x y color]))
(let* [#erl[i j] (->complex x y w h)
n (mandelbrot i j max-iters)
color (->color n max-iters)]
#erl[x y color]))

(defn on-mouse-pressed [state handler]
(if-not (d/looping?)
(do
(d/start-loop)
(d/background 0)
{:points (points)
:m-points nil})
state))

(defn setup
[]
Expand All @@ -48,14 +57,17 @@

(defn update-state
[{points :points :as state}]
(when points
(let [max-iters 100
(if-not points
(do
(d/no-loop)
state)
(let [max-iters 255
w (d/width)
h (d/height)]
(loop* [#erl(#erl[x y] & rest) points
m-points #erl()
n 0]
(if (and (< n 500) (seq rest))
(if (and (< n 1000) (seq rest))
(recur rest
(conj m-points (mandelbrot-point x y w h max-iters))
(inc n))
Expand All @@ -73,6 +85,7 @@
:size [300 300]
:setup setup
:update update-state
:mouse-pressed on-mouse-pressed
:draw draw
:bgcolor [0]
:features [:keep-on-top]
Expand Down
6 changes: 3 additions & 3 deletions src/doodler/middlewares/state_mode.clje
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
(if update
(assoc opts
:draw #(do
(set! core/*state* (update core/*state*))
(u/swap-var! core/*state* update)
(draw core/*state*)))
(assoc opts
:draw #(draw core/*state*)))))
Expand All @@ -38,9 +38,9 @@
(if-let [handler (name acc)]
(if (state-only name)
(assoc acc
name #(set! core/*state* (handler core/*state*)))
name #(u/swap-var! core/*state* handler))
(assoc acc
name #(set! core/*state* (handler core/*state* %))))
name #(u/swap-var! core/*state* handler %)))
acc))
opts
callbacks))
Expand Down
Loading