Add quil renderer for thi.ng-hiccup scenes
This commit is contained in:
parent
5f6c31c09b
commit
cc59932278
2 changed files with 118 additions and 0 deletions
54
src/aphorisms/quil/drawable.clj
Normal file
54
src/aphorisms/quil/drawable.clj
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
(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)))))))
|
||||
Loading…
Add table
Add a link
Reference in a new issue