Mercurial > hg > indyvon
changeset 68:9b511fe09867
Code cleanup, docstrings.
author | Mikhail Kryshen <mikhail@kryshen.net> |
---|---|
date | Sun, 29 Aug 2010 03:59:10 +0400 |
parents | a19cf5007d14 |
children | 01b4187c19e4 |
files | project.clj src/net/kryshen/indyvon/async.clj src/net/kryshen/indyvon/core.clj src/net/kryshen/indyvon/layers.clj |
diffstat | 4 files changed, 64 insertions(+), 67 deletions(-) [+] |
line wrap: on
line diff
--- a/project.clj Sat Aug 28 02:37:30 2010 +0400 +++ b/project.clj Sun Aug 29 03:59:10 2010 +0400 @@ -1,5 +1,6 @@ (defproject indyvon "1.0.0-SNAPSHOT" :description "INteractive DYnamic VisualizatiON library" + ;;:warn-on-reflection true :dependencies [[org.clojure/clojure "1.2.0"] [org.clojure/clojure-contrib "1.2.0"]] :dev-dependencies [[swank-clojure/swank-clojure "1.2.1"]]
--- a/src/net/kryshen/indyvon/async.clj Sat Aug 28 02:37:30 2010 +0400 +++ b/src/net/kryshen/indyvon/async.clj Sun Aug 29 03:59:10 2010 +0400 @@ -28,21 +28,22 @@ ;; not in use (defn- create-image [async-layer] + ;; TODO: support different image types. (BufferedImage. (:width async-layer) (:height async-layer) - BufferedImage/TYPE_INT_ARGB)) + BufferedImage/TYPE_INT_ARGB_PRE)) -(defn- create-buffer [async-layer id] - (Buffer. id (create-image async-layer) 0 :free)) +(defn- create-buffer [async-layer] + (Buffer. (Object.) (create-image async-layer) 0 :free)) -(defn- find-buffer [buffers & states] +(defn- find-buffer "Find a buffer with the one of the specified states given in the order of preference." + [buffers & states] (some identity (for [state states] (some #(if (= (:state %) state) % nil) buffers)))) -(defn- replace-buffer - [buffers buffer] +(defn- replace-buffer [buffers buffer] (conj (remove #(= (:id %) (:id buffer)) buffers) buffer)) @@ -62,31 +63,28 @@ (alter (:buffers al) replace-buffer b) b))) -(defn- release-buffer - [al buffer] +(defn- release-buffer [al buffer] (dosync - (let [buffers @(:buffers al) - state (:state buffer) + (let [state (:state buffer) readers (if (= state :front) (dec (:readers buffer)) (:readers buffer)) state (cond (pos? readers) :front (= :back (:state buffer)) :fresh - :default :free) - buffers - (if (= state :fresh) - ;; Change state of all the other buffers from :fresh to :free. - (reduce replace-buffer buffers - (for [b buffers :when (= (:state b) :fresh)] - (assoc b :state :free))) - buffers) - buffers (replace-buffer buffers (assoc buffer + :default :free)] + (when (= state :fresh) + ;; Change state of the prefiously fresh buffer to :free. + (when-let [fresh (find-buffer @(:buffers al) :fresh)] + (alter (:buffers al) replace-buffer (assoc fresh + :state :free)))) + (alter (:buffers al) replace-buffer (assoc buffer :state state - :readers readers))] - (ref-set (:buffers al) buffers)))) - -(defmacro with-buffer [al type [name] & body] + :readers readers))))) + +(defmacro with-buffer + {:private true} + [al type [name] & body] `(let [al# ~al ~name (take-buffer al# ~type)] (try @@ -98,7 +96,7 @@ ;;(Thread/sleep 1000) (with-buffer async-layer :back [b] (draw-root! (:content async-layer) - (.getGraphics (:image b)) + (.getGraphics ^Image (:image b)) (:width async-layer) (:height async-layer) dummy-event-dispatcher)) @@ -115,8 +113,7 @@ (add-context-observer content (fn [_] (draw-offscreen-async layer))) (when-not @buffers ;; TODO: dynamic size, recreate buffers when size increases. - (let [new-buffers (list (create-buffer layer 1) - (create-buffer layer 2))] + (let [new-buffers (repeatedly 2 (partial create-buffer layer))] (dosync (ref-set buffers new-buffers))) (draw-offscreen-async layer)) @@ -126,7 +123,7 @@ (Size. width height))) (defn async-layer - "Creates layer that draws the content asynchronously in an + "Creates layer that draws the content asynchronously using offscreen buffer." [content width height] (AsyncLayer. content width height
--- a/src/net/kryshen/indyvon/core.clj Sat Aug 28 02:37:30 2010 +0400 +++ b/src/net/kryshen/indyvon/core.clj Sun Aug 29 03:59:10 2010 +0400 @@ -23,9 +23,11 @@ :doc "Target AWT component, may be nil if drawing off-screen."} *target*) -(def *width*) +(def ^{:doc "Width of the rendering area."} + *width*) -(def *height*) +(def ^{:doc "Height of the rendering area."} + *height*) (def ^Shape *clip*) @@ -34,19 +36,19 @@ (def *event-dispatcher*) (def ^{:tag AffineTransform - :doc "Initial transform associated with the graphics context"} + :doc "Initial transform associated with the graphics context."} *initial-transform*) (def ^{:tag AffineTransform :doc "Inversion of the initial transform associated with - the graphics context"} + the graphics context."} *inverse-initial-transform*) (defrecord Theme [fore-color back-color alt-back-color border-color font]) ;; REMIND: use system colors, see java.awt.SystemColor. (defn default-theme [] - (Theme. Color/BLACK Color/LIGHT_GRAY Color/WHITE + (Theme. Color/BLACK Color/WHITE Color/LIGHT_GRAY Color/BLUE (Font. "Sans" Font/PLAIN 12))) (def *theme* (default-theme)) @@ -83,23 +85,26 @@ "Anchor point: [x y], h-align could be :left, :center or :right, v-align is :top, :center or :bottom")) +(defn default-anchor [layer h-align v-align] + (if (and (= h-align :left) + (= v-align :top)) + (Location. 0 0) + (let [size (layer-size layer)] + (Location. + (case h-align + :top 0 + :center (/ (:width size) 2) + :right (:width size)) + (case v-align + :left 0 + :center (/ (:height size) 2) + :bottom (:height size)))))) + ;; Default implementation of Anchored for any Layer. (extend-protocol Anchored net.kryshen.indyvon.core.Layer (anchor [this h-align v-align] - (if (and (= h-align :left) - (= v-align :top)) - (Location. 0 0) - (let [size (layer-size this)] - (Location. - (case h-align - :top 0 - :center (/ (:width size) 2) - :right (:width size)) - (case v-align - :left 0 - :center (/ (:height size) 2) - :bottom (:height size))))))) + (default-anchor this h-align v-align))) (defn- assoc-cons [m key val] (->> (get m key) (cons val) (assoc m key))) @@ -114,7 +119,7 @@ (def observers (atom nil)) ;; TODO: groups should be weakly referenced. -;; Need persistent analog to java.util.WeakHashMap. +;; Need persistent analog of java.util.WeakHashMap. (defn add-observer "Add observer fn for the target to the specified group." [group target f] @@ -143,11 +148,6 @@ (doseq [f (reduce #(concat %1 (get %2 target)) nil (vals @observers))] (apply f target args))) -(defn repaint - "Repaint the current scene." - [] - (update *root*)) - (defn add-context-observer "Observer registered with this function will be automatically removed after the next frame rendering is complete." @@ -162,6 +162,11 @@ (if (not= root target) (add-observer root target (fn [_] (update root)))))) +(defn repaint + "Repaint the current scene." + [] + (update *root*)) + ;; ;; Rendering ;; @@ -262,13 +267,10 @@ (finally (.setColor *graphics* color#))))) -;; TODO: -;; -;; (with-transform -;; (rotate ...) -;; (draw ...) -;; (scale ...) -;; (draw ...)) +;; TODO: constructor for AffineTransform. +;; (transform :scale 0.3 0.5 +;; :translate 5 10 +;; :rotate (/ Math/PI 2)) (defmacro with-transform [transform & body] `(let [old-t# (.getTransform *graphics*)] @@ -282,11 +284,6 @@ `(let [transform# (AffineTransform/getRotateInstance ~theta ~ax ~ay)] (with-transform transform# ~@body))) -(defn- geometry-vec [geometry] - (if (vector? geometry) - geometry - [(:x geometry) (:y geometry) (:width geometry) (:height geometry)])) - (defn draw! "Draws layer." ([layer]
--- a/src/net/kryshen/indyvon/layers.clj Sat Aug 28 02:37:30 2010 +0400 +++ b/src/net/kryshen/indyvon/layers.clj Sun Aug 29 03:59:10 2010 +0400 @@ -111,9 +111,10 @@ (reduce (fn [w ^TextLayout tl] (max w (.getAdvance tl))) 0 layouts)) (defn- text-height [layouts] - (reduce (fn [w ^TextLayout tl] (+ w (.getAscent tl) - (.getDescent tl) - (.getLeading tl))) + (reduce (fn [w ^TextLayout tl] + (+ w (.getAscent tl) + (.getDescent tl) + (.getLeading tl))) 0 layouts)) (defn text-layer @@ -226,7 +227,8 @@ (ref-set fix-x (:x-on-screen e)) (ref-set fix-y (:y-on-screen e))) (update layer)))) - (layer-size [layer] (layer-size content)))))) + (layer-size [layer] + (layer-size content)))))) ;; ;; Layer context decorators.