heyarne.aphorisms/src/aphorisms/quil/drawable.clj

54 lines
1.4 KiB
Clojure

(ns aphorisms.quil.drawable
(:require [quil.core :as q]
[thi.ng.geom.types]
[thi.ng.geom.circle :as c])
(:import [thi.ng.geom.types Circle2 Line2 Polygon2]))
(defn quil-attrs! [attrs]
(when-let [color (:stroke/color attrs)] (apply q/stroke color))
(when-let [weight (:stroke/weight attrs)] (q/stroke-weight weight))
(when-let [stroke-cap (:stroke/line-cap attrs)] (q/stroke-cap stroke-cap))
(when-let [stroke-join (:stroke/line-join attrs)] (q/stroke-join stroke-join))
(when-let [color (:fill/color attrs)] (apply q/fill color)))
(defprotocol QuilDrawable
(draw [shape] [shape attrs]))
(extend-protocol QuilDrawable
Circle2
(draw
([circle] (draw circle {}))
([circle attrs]
(quil-attrs! attrs)
(q/ellipse (-> circle :p :x) (-> circle :p :y) (:r circle) (:r circle))))
Polygon2
(draw
([poly] (draw poly {}))
([poly attrs]
(quil-attrs! attrs)
(doseq [[a b] (partition 2 1 (:points poly))]
(q/poly a b))))
Line2
(draw
([line] (draw line {}))
([line attrs]
(quil-attrs! attrs)
(apply q/line (:points line)))))
(defn draw-scene! [scene]
(let [[f & rs] scene
[attrs & rrs] rs]
(when f
(if (sequential? f)
(do
(draw-scene! f)
(recur rs))
(if (map? attrs)
(do
(draw f attrs)
(recur rrs))
(do
(draw f)
(recur rs)))))))