Skip to content

Commit

Permalink
Replaced the naive dynamic style system with the (dynamic) macro whic…
Browse files Browse the repository at this point in the history
…h sets a special context
  • Loading branch information
stathissideris committed Dec 11, 2012
1 parent 0d545ea commit 432e117
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/dali/backend.clj
Expand Up @@ -37,7 +37,8 @@
(render-path [this shape]) (render-path [this shape])
(render-image [this shape]) (render-image [this shape])


(render-group [this shape])) (render-group [this shape])
(text-bounds [this shape]))




(defmacro delegate-op-to-backend [op & shape-types] (defmacro delegate-op-to-backend [op & shape-types]
Expand Down
10 changes: 8 additions & 2 deletions src/dali/backend/java_2d.clj
Expand Up @@ -194,7 +194,7 @@
size (.getSize *DEFAULT-FONT*)}}] size (.getSize *DEFAULT-FONT*)}}]
(Font. family java.awt.Font/PLAIN size)) (Font. family java.awt.Font/PLAIN size))


(defn text-bounds (defn text-bounds-java-2d
[backend {content :content {position :position} :geometry :as text}] [backend {content :content {position :position} :geometry :as text}]
(let [font (font->java-font (get-in text [:style :font])) (let [font (font->java-font (get-in text [:style :font]))
rect (.getStringBounds rect (.getStringBounds
Expand All @@ -218,10 +218,12 @@
(let [st (if (has-stroke? shape) (:stroke shape) DEFAULT-STROKE)] (let [st (if (has-stroke? shape) (:stroke shape) DEFAULT-STROKE)]
(isolate-style backend (isolate-style backend
(set-stroke backend (eval-dynamic-style (set-stroke backend (eval-dynamic-style
backend
shape shape
(get-in shape [:style :stroke]))) (get-in shape [:style :stroke])))
(if (has-transform? shape) (if (has-transform? shape)
(with-transform backend (eval-dynamic-style (with-transform backend (eval-dynamic-style
backend
shape shape
(:transform shape)) (:transform shape))
(draw backend shape)) (draw backend shape))
Expand All @@ -231,10 +233,12 @@
(when (has-fill? shape) (when (has-fill? shape)
(isolate-style backend (isolate-style backend
(set-fill backend (eval-dynamic-style (set-fill backend (eval-dynamic-style
backend
shape shape
(get-in shape [:style :fill]))) (get-in shape [:style :fill])))
(if (has-transform? shape) (if (has-transform? shape)
(with-transform backend (eval-dynamic-style (with-transform backend (eval-dynamic-style
backend
shape shape
(:transform shape)) (:transform shape))
(fill backend shape)) (fill backend shape))
Expand Down Expand Up @@ -323,7 +327,9 @@
(assoc shape (assoc shape
:style (deep-merge (:style group) :style (deep-merge (:style group)
(:style shape)))] ;;shape takes precedence (:style shape)))] ;;shape takes precedence
(render this merged)))))) (render this merged)))))
(text-bounds [this shape]
(text-bounds-java-2d this shape)))


(defn buffered-image-type [type] (defn buffered-image-type [type]
(if (keyword? type) (if (keyword? type)
Expand Down
8 changes: 8 additions & 0 deletions src/dali/core.clj
@@ -1,3 +1,4 @@

(ns dali.core (ns dali.core
(:use [dali.math] (:use [dali.math]
[dali.utils])) [dali.utils]))
Expand Down Expand Up @@ -156,6 +157,13 @@
{:type :group {:type :group
:content content})) :content content}))


(defmacro dynamic [& body]
{:type :dynamic-value
:code `'~body})

(defn dynamic-value? [x]
(if (and (map? x)) (= :dynamic-value (:type x))))



(defn rectangle->polygon (defn rectangle->polygon
[{{[px py] :position [w h] :dimensions} :geometry :as shape}] [{{[px py] :position [w h] :dimensions} :geometry :as shape}]
Expand Down
15 changes: 12 additions & 3 deletions src/dali/style.clj
Expand Up @@ -135,9 +135,18 @@
:stops stops :stops stops
:cycle-method cycle-method})) :cycle-method cycle-method}))


(defn eval-dynamic-style [shape style] (declare #^{:dynamic true} backend)
(postwalk (fn eval-dynamic-style-fn [x] (declare #^{:dynamic true} this)
(if (function? x) (x shape) x)) style))
(defn eval-dynamic-style [the-backend shape style]
(binding [*ns* (the-ns 'dali.style)
backend the-backend
this shape]
(postwalk (fn eval-dynamic-style-fn [x]
(if (dynamic-value? x)
(let [code (:code x)]
(eval `(do ~@code)))
x)) style)))


;;;;;; fills ;;;;;; ;;;;;; fills ;;;;;;


Expand Down
6 changes: 3 additions & 3 deletions src/dali/test.clj
Expand Up @@ -4,7 +4,7 @@
[dali.math] [dali.math]
[dali.backend] [dali.backend]
[dali.backend.java-2d]) [dali.backend.java-2d])
(:require [clarity.dev :as dev]) (:require [dali.dev :as dev])
(:import [java.awt.geom CubicCurve2D$Double Path2D$Double AffineTransform])) (:import [java.awt.geom CubicCurve2D$Double Path2D$Double AffineTransform]))


#_(dev/watch-image #(test-dali)) #_(dev/watch-image #(test-dali))
Expand Down Expand Up @@ -71,11 +71,11 @@
(rectangle {:stroke {:width 8 (rectangle {:stroke {:width 8
:color (color 0 130 0) :color (color 0 130 0)
:join :miter} :join :miter}
:transform [:translate #(minus (center %)) :transform [:translate (dynamic (minus (center this)))
:skew [0.2 0.2] :skew [0.2 0.2]
:scale 0.8 :scale 0.8
:rotate 30 :rotate 30
:translate center]} :translate (dynamic (center this))]}
[330 170] [100 70])) [330 170] [100 70]))


(draw (rotate-around (rectangle [160 100] [60 60]) (draw (rotate-around (rectangle [160 100] [60 60])
Expand Down

0 comments on commit 432e117

Please sign in to comment.