Permalink
Browse files

Replaced the naive dynamic style system with the (dynamic) macro whic…

…h sets a special context
  • Loading branch information...
1 parent 0d545ea commit 432e117a169a83f42437b3e9a508a8156e69c2bf @stathissideris committed Dec 11, 2012
Showing with 33 additions and 9 deletions.
  1. +2 −1 src/dali/backend.clj
  2. +8 −2 src/dali/backend/java_2d.clj
  3. +8 −0 src/dali/core.clj
  4. +12 −3 src/dali/style.clj
  5. +3 −3 src/dali/test.clj
View
@@ -37,7 +37,8 @@
(render-path [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]
@@ -194,7 +194,7 @@
size (.getSize *DEFAULT-FONT*)}}]
(Font. family java.awt.Font/PLAIN size))
-(defn text-bounds
+(defn text-bounds-java-2d
[backend {content :content {position :position} :geometry :as text}]
(let [font (font->java-font (get-in text [:style :font]))
rect (.getStringBounds
@@ -218,10 +218,12 @@
(let [st (if (has-stroke? shape) (:stroke shape) DEFAULT-STROKE)]
(isolate-style backend
(set-stroke backend (eval-dynamic-style
+ backend
shape
(get-in shape [:style :stroke])))
(if (has-transform? shape)
(with-transform backend (eval-dynamic-style
+ backend
shape
(:transform shape))
(draw backend shape))
@@ -231,10 +233,12 @@
(when (has-fill? shape)
(isolate-style backend
(set-fill backend (eval-dynamic-style
+ backend
shape
(get-in shape [:style :fill])))
(if (has-transform? shape)
(with-transform backend (eval-dynamic-style
+ backend
shape
(:transform shape))
(fill backend shape))
@@ -323,7 +327,9 @@
(assoc shape
:style (deep-merge (:style group)
(: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]
(if (keyword? type)
View
@@ -1,3 +1,4 @@
+
(ns dali.core
(:use [dali.math]
[dali.utils]))
@@ -156,6 +157,13 @@
{:type :group
: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
[{{[px py] :position [w h] :dimensions} :geometry :as shape}]
View
@@ -135,9 +135,18 @@
:stops stops
:cycle-method cycle-method}))
-(defn eval-dynamic-style [shape style]
- (postwalk (fn eval-dynamic-style-fn [x]
- (if (function? x) (x shape) x)) style))
+(declare #^{:dynamic true} backend)
+(declare #^{:dynamic true} this)
+
+(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 ;;;;;;
View
@@ -4,7 +4,7 @@
[dali.math]
[dali.backend]
[dali.backend.java-2d])
- (:require [clarity.dev :as dev])
+ (:require [dali.dev :as dev])
(:import [java.awt.geom CubicCurve2D$Double Path2D$Double AffineTransform]))
#_(dev/watch-image #(test-dali))
@@ -71,11 +71,11 @@
(rectangle {:stroke {:width 8
:color (color 0 130 0)
:join :miter}
- :transform [:translate #(minus (center %))
+ :transform [:translate (dynamic (minus (center this)))
:skew [0.2 0.2]
:scale 0.8
:rotate 30
- :translate center]}
+ :translate (dynamic (center this))]}
[330 170] [100 70]))
(draw (rotate-around (rectangle [160 100] [60 60])

0 comments on commit 432e117

Please sign in to comment.