comparison dwtx/draw2d/graph/RouteEdges.d @ 98:95307ad235d9

Added Draw2d code, still work in progress
author Frank Benoit <benoit@tionex.de>
date Sun, 03 Aug 2008 00:52:14 +0200
parents
children
comparison
equal deleted inserted replaced
96:b492ba44e44d 98:95307ad235d9
1 /*******************************************************************************
2 * Copyright (c) 2003, 2005 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module dwtx.draw2d.graph.RouteEdges;
14
15 import dwt.dwthelper.utils;
16
17 import dwtx.draw2d.geometry.Insets;
18 import dwtx.draw2d.geometry.Point;
19 import dwtx.draw2d.geometry.PointList;
20 import dwtx.draw2d.geometry.Rectangle;
21 import dwtx.draw2d.graph.GraphVisitor;
22 import dwtx.draw2d.graph.DirectedGraph;
23 import dwtx.draw2d.graph.Edge;
24 import dwtx.draw2d.graph.SubgraphBoundary;
25 import dwtx.draw2d.graph.ShortestPathRouter;
26 import dwtx.draw2d.graph.Path;
27 import dwtx.draw2d.graph.VirtualNode;
28 import dwtx.draw2d.graph.Node;
29
30 /**
31 * @author Randy Hudson
32 */
33 class RouteEdges : GraphVisitor {
34
35 /**
36 * @see GraphVisitor#visit(DirectedGraph)
37 */
38 public void revisit(DirectedGraph g) {
39 for (int i = 0; i < g.edges.size(); i++) {
40 Edge edge = cast(Edge)g.edges.get(i);
41 edge.start = new Point(
42 edge.getSourceOffset() + edge.source.x,
43 edge.source.y + edge.source.height);
44 if (auto boundary = cast(SubgraphBoundary)edge.source ) {
45 if (boundary.getParent().head is boundary)
46 edge.start.y = boundary.getParent().y + boundary.getParent().insets.top;
47 }
48 edge.end = new Point(
49 edge.getTargetOffset() + edge.target.x,
50 edge.target.y);
51
52 if (edge.vNodes !is null)
53 routeLongEdge(edge, g);
54 else {
55 PointList list = new PointList();
56 list.addPoint(edge.start);
57 list.addPoint(edge.end);
58 edge.setPoints(list);
59 }
60 }
61 }
62
63 static void routeLongEdge(Edge edge, DirectedGraph g) {
64 ShortestPathRouter router = new ShortestPathRouter();
65 Path path = new Path(edge.start, edge.end);
66 router.addPath(path);
67 Rectangle o;
68 Insets padding;
69 for (int i = 0; i < edge.vNodes.size(); i++) {
70 VirtualNode node = cast(VirtualNode)edge.vNodes.get(i);
71 Node neighbor;
72 if (node.left !is null) {
73 neighbor = node.left;
74 o = new Rectangle(neighbor.x, neighbor.y, neighbor.width, neighbor.height);
75 padding = g.getPadding(neighbor);
76 o.width += padding.right + padding.left;
77 o.width += (edge.getPadding() * 2);
78 o.x -= (padding.left + edge.getPadding());
79 o.union_(o.getLocation().translate(-100000, 2));
80 router.addObstacle(o);
81 }
82 if (node.right !is null) {
83 neighbor = node.right;
84 o = new Rectangle(neighbor.x, neighbor.y, neighbor.width, neighbor.height);
85 padding = g.getPadding(neighbor);
86 o.width += padding.right + padding.left;
87 o.width += (edge.getPadding() * 2);
88 o.x -= (padding.left + edge.getPadding());
89 o.union_(o.getLocation().translate(100000, 2));
90 router.addObstacle(o);
91 }
92 }
93 router.setSpacing(0);
94 router.solve();
95 edge.setPoints(path.getPoints());
96 }
97
98 }