Mercurial > hg > indyvon
changeset 69:01b4187c19e4
Remove graph.clj for now as it depends on unreleased code and needs to be reworked.
author | Mikhail Kryshen <mikhail@kryshen.net> |
---|---|
date | Sun, 29 Aug 2010 17:18:16 +0400 |
parents | 9b511fe09867 |
children | b2f6c78413d3 |
files | src/net/kryshen/indyvon/graph.clj |
diffstat | 1 files changed, 0 insertions(+), 142 deletions(-) [+] |
line wrap: on
line diff
--- a/src/net/kryshen/indyvon/graph.clj Sun Aug 29 03:59:10 2010 +0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,142 +0,0 @@ -;; -;; Copyright (C) 2010 Mikhail Kryshen <mikhail@kryshen.net> -;; -;; This file is part of Indyvon. -;; - -(ns net.kryshen.indyvon.graph - (:use - (net.kryshen.indyvon core component layers)) - (:import - (net.kryshen.indyvon.core Location Size) - (net.kryshen.indygraph Graph DefaultGraph GraphLayout Vertex2D - Edge2D RectangularVertex2D DefaultEdge2D) - (net.kryshen.indygraph.fdl ForceDirectedLayout) - (java.awt.geom Path2D$Double) - (javax.swing JFrame))) - -(extend-type Vertex2D - Layer - (render! [v] - (.drawOval *graphics* 0 0 (:width *bounds*) (:height *bounds*))) - (layer-size [v] - (Size. - (+ (.getLeftBound v) (.getRightBound v)) - (+ (.getTopBound v) (.getBottomBound v)))) - Anchored - (anchor [v _ _] - (Location. - (.getLeftBound v) - (.getTopBound v)))) - -(defn- draw-vertices! [^GraphLayout layout x y] - (doseq [v (.vertices (.getGraph layout))] - (draw-anchored! v :center :center (+ x (.getX v)) (+ y (.getY v))))) - -(defn- draw-movable-vertex! - [^Vertex2D v ^GraphLayout layout x y dragged fix-x fix-y] - (let [x (+ x (.getX v)) - y (+ y (.getY v)) - anchor (anchor v :center :center) - size (layer-size v) - x (- x (:x anchor)) - y (- y (:y anchor))] - (with-bounds x y (:width size) (:height size) - (with-handlers v - (draw! v) - (:mouse-pressed e - (dosync (ref-set fix-x (:x-on-screen e)) - (ref-set fix-y (:y-on-screen e)) - (ref-set dragged v))) - (:mouse-released e - (dosync (if (= v @dragged) - (ref-set dragged nil)))) - (:mouse-dragged e - (let [x (:x-on-screen e) - y (:y-on-screen e) - vx (.getX v) - vy (.getY v)] - (dosync - (when @dragged - (let [dx (- x @fix-x) - dy (- y @fix-y)] - (.layoutLocation v (+ vx dx) (+ vy dy)) - (.invalidateLayout layout) - (*update*) - (ref-set fix-x x) - (ref-set fix-y y)))))))))) - -(defn- draw-movable-vertices! - [^GraphLayout layout x y dragged-ref fix-x fix-y] - (let [dragged @dragged-ref] - (doseq [v (.vertices (.getGraph layout)) - :when (not= v dragged)] - (draw-movable-vertex! v layout x y dragged-ref fix-x fix-y)) - ;; Draw the vertex being dragged above others. - (when dragged - (draw-movable-vertex! dragged layout x y dragged-ref fix-x fix-y)))) - -(defn- draw-edges! [^GraphLayout layout x y] - ;; TODO: extend Layer on Edge2D and draw like vertices. - (.translate *graphics* x y) - (let [path (Path2D$Double.)] - (doseq [^Edge2D e (.edges (.getGraph layout))] - (.getPath e path) - (.draw *graphics* path))) - (.translate *graphics* (- x) (- y))) - -(defrecord GraphLayer [layout movable dragged fix-x fix-y] - 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)))) - (layer-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)))))) - -(defn graph-layer - ([graph-layout] - (graph-layer graph-layout false)) - ([^GraphLayout 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, - and a function that maps vertex id's to Vertex objects." - [relations f] - (let [graph (DefaultGraph.) - vs (reduce #(conj %1 (first %2) (second %2)) #{} relations) - vm (reduce #(assoc %1 %2 (f %2)) {} vs) - vs (vals vm) - es (map #(DefaultEdge2D. (vm (first %)) (vm (second %))) relations)] - (doseq [v vs] - (.addVertex graph v)) - (doseq [e es] - (.addEdge graph e)) - graph)) - -(comment - (let [graph (build-graph - [[1 2] [1 3] [1 4] [2 4]] - (fn [_] (RectangularVertex2D. 100 30))) - layout (ForceDirectedLayout. graph) - frame (JFrame. "Graph test") - layer (graph-layer layout true) - layer (viewport layer :center :center)] - (.add (.getContentPane frame) (make-jpanel layer)) - (while (not (.update layout))) - (doto frame - (.setDefaultCloseOperation JFrame/DISPOSE_ON_CLOSE) - (.pack) - (.setVisible true))) - )