Mercurial > hg > indyvon
changeset 42:d3e3c43df1cd
Rename size to layer-size in Layer protocol to avoid name conflict when using defrecord.
Implement graph layer in defrecord.
author | Mikhail Kryshen <mikhail@kryshen.net> |
---|---|
date | Sun, 11 Jul 2010 06:34:36 +0400 |
parents | 2475c99fbb8c |
children | 7d67064f0880 |
files | src/kryshen/indyvon/component.clj src/kryshen/indyvon/core.clj src/kryshen/indyvon/demo.clj src/kryshen/indyvon/graph.clj src/kryshen/indyvon/layers.clj |
diffstat | 5 files changed, 51 insertions(+), 38 deletions(-) [+] |
line wrap: on
line diff
--- a/src/kryshen/indyvon/component.clj Sat Jul 10 07:28:19 2010 +0400 +++ b/src/kryshen/indyvon/component.clj Sun Jul 11 06:34:36 2010 +0400 @@ -35,7 +35,7 @@ (defn preferred-size [component layer] (binding [*target* component *font-context*' (font-context component)] - (let [s (size layer)] + (let [s (layer-size layer)] (Dimension. (:width s) (:height s))))) (defn make-jpanel
--- a/src/kryshen/indyvon/core.clj Sat Jul 10 07:28:19 2010 +0400 +++ b/src/kryshen/indyvon/core.clj Sun Jul 11 06:34:36 2010 +0400 @@ -32,7 +32,7 @@ (defprotocol Layer "Basic UI element." (render! [this]) - (size [this])) + (layer-size [this])) ;; TODO: modifiers (defrecord MouseEvent [id when x y x-on-screen y-on-screen button]) @@ -60,7 +60,7 @@ (if (and (= h-align :left) (= v-align :top)) (Location. 0 0) - (let [size (size this)] + (let [size (layer-size this)] (Location. (case h-align :top 0 @@ -148,7 +148,7 @@ (finally (.dispose graphics))))) ([layer x y] - (let [size (size layer)] + (let [size (layer-size layer)] (draw! layer x y (:width size) (:height size)))) ([layer x y width height] (with-bounds* x y width height render! layer)))
--- a/src/kryshen/indyvon/demo.clj Sat Jul 10 07:28:19 2010 +0400 +++ b/src/kryshen/indyvon/demo.clj Sun Jul 11 06:34:36 2010 +0400 @@ -26,7 +26,7 @@ (:mouse-entered e (println e)) (:mouse-exited e (println e)) (:mouse-moved e (println e)))) - (size [layer] (Size. 30 20)))) + (layer-size [layer] (Size. 30 20)))) (def layer1b (border layer1 2 3)) @@ -39,7 +39,7 @@ (.fillRect 0 0 (:width *bounds*) (:height *bounds*))) (draw! layer1b 10 5) (draw! layer1 55 5)) - (size [layer] (Size. 70 65)))) + (layer-size [layer] (Size. 70 65)))) (def layer3 (border (text-layer "Sample\ntext" :right :center))) @@ -64,7 +64,7 @@ (ref-set fl (fps-layer (/ @frames elapsed))) (ref-set frames 0) (ref-set last time))))) - (size [layer] (size @fl))))) + (layer-size [layer] (layer-size @fl))))) (def layer (reify @@ -77,7 +77,7 @@ (draw! layer2 15 20) (draw! layer3 100 100 80 50) (draw! fps)) - (size [layer] (Size. 400 300)))) + (layer-size [layer] (Size. 400 300)))) (defn -main [] (doto frame
--- a/src/kryshen/indyvon/graph.clj Sat Jul 10 07:28:19 2010 +0400 +++ b/src/kryshen/indyvon/graph.clj Sun Jul 11 06:34:36 2010 +0400 @@ -19,7 +19,7 @@ Layer (render! [v] (.drawOval *graphics* 0 0 (:width *bounds*) (:height *bounds*))) - (size [v] + (layer-size [v] (Size. (+ (.getLeftBound v) (.getRightBound v)) (+ (.getTopBound v) (.getBottomBound v)))) @@ -39,7 +39,7 @@ (let [x (+ x (.getX v)) y (+ y (.getY v)) anchor (anchor v :center :center) - size (size v) + size (layer-size v) x (- x (:x anchor)) y (- y (:y anchor))] (with-bounds x y (:width size) (:height size) @@ -76,31 +76,30 @@ (.draw *graphics* path))) (.translate *graphics* (- x) (- y))) +(defrecord GraphLayer [graph-layout movable dragged fix-x fix-y] + Layer + (render! [layer] + (let [bounds (.getBounds graph-layout) + x (- (.getX bounds)) + y (- (.getY bounds))] + (draw-edges! graph-layout x y) + (if movable + (draw-movable-vertices! graph-layout x y dragged fix-x fix-y) + (draw-vertices! graph-layout x y)))) + (layer-size [layer] + (let [bounds (.getBounds graph-layout)] + (Size. (.getWidth bounds) (.getHeight bounds)))) + Anchored + (anchor [layer x-align y-align] + (let [bounds (.getBounds graph-layout)] + (Location. (- (.getX bounds)) + (- (.getY bounds)))))) + (defn graph-layer - ([layout] - (graph-layer layout false)) - ([^GraphLayout layout movable] - (let [dragged (ref nil) - fix-x (ref 0) - fix-y (ref 0)] - (reify - Layer - (render! [layer] - (let [bounds (.getBounds layout) - x (- (.getX bounds)) - y (- (.getY bounds))] - (draw-edges! layout x y) - (if movable - (draw-movable-vertices! layout x y dragged fix-x fix-y) - (draw-vertices! layout x y)))) - (size [layer] - (let [bounds (.getBounds layout)] - (Size. (.getWidth bounds) (.getHeight bounds)))) - Anchored - (anchor [layer x-align y-align] - (let [bounds (.getBounds layout)] - (Location. (- (.getX bounds)) - (- (.getY bounds))))))))) + ([graph-layout] + (graph-layer graph-layout false)) + ([^GraphGraph-Layout graph-layout movable] + (GraphLayer. graph-layout movable (ref nil) (ref 0) (ref 0)))) (defn build-graph "Returns Graph defined by a sequence of pairs of vertex ids,
--- a/src/kryshen/indyvon/layers.clj Sat Jul 10 07:28:19 2010 +0400 +++ b/src/kryshen/indyvon/layers.clj Sun Jul 11 06:34:36 2010 +0400 @@ -41,8 +41,8 @@ (.drawRect *graphics* i i (- w 1 i i) (- h 1 i i))) (draw! content offset offset (- w offset offset) (- h offset offset)))) - (size [l] - (let [s (size content)] + (layer-size [l] + (let [s (layer-size content)] (Size. (+ (:width s) offset offset) (+ (:height s) offset offset)))))))) @@ -81,7 +81,7 @@ x (align-x (.getAdvance layout) w h-align)] (.draw layout *graphics* x (+ y ascent)) (recur (next layouts) (+ y lh))))))) - (size [layer] + (layer-size [layer] (let [layouts (layout-text lines (:font *theme*) *font-context*) width (text-width layouts) height (text-height layouts)] @@ -124,4 +124,18 @@ (ref-set fix-x (:x-on-screen e)) (ref-set fix-y (:y-on-screen e))) (*update*)))) - (size [layer] (size content)))))) + (layer-size [layer] (layer-size content)))))) + +(defmacro decorate-layer [layer & render-tail] + `(reify + Layer + (render! ~@render-tail) + (layer-size [t#] (layer-size ~layer)) + Anchored + (anchor [t# xa# ya#] (anchor ~layer xa# ya#)))) + +(defmacro handler [layer & handlers] + `(decorate-layer ~layer [t#] + (with-handlers t# + (render! ~layer) + ~@handlers)))