Mercurial > hg > camir-aes2014
comparison toolboxes/graph_visualisation/lib/lefty/tree.lefty @ 0:e9a9cd732c1e tip
first hg version after svn
author | wolffd |
---|---|
date | Tue, 10 Feb 2015 15:05:51 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e9a9cd732c1e |
---|---|
1 load ('def.lefty'); | |
2 definit (); | |
3 # | |
4 # initialize window data | |
5 # | |
6 canvas = defcanvas; | |
7 wrect = [0 = ['x' = -5; 'y' = 0;]; 1 = ['x' = 410; 'y' = 500;];]; | |
8 setwidgetattr (canvas, ['window' = wrect;]); | |
9 # | |
10 # data structures | |
11 # | |
12 nodearray = []; | |
13 nodenum = 0; | |
14 dist = ['x' = 40; 'y' = 40;]; | |
15 defsize = ['x' = 10; 'y' = 10;]; | |
16 fontname = 'fixed'; | |
17 fontsize = 18; | |
18 tree = null; | |
19 | |
20 # drawing functions | |
21 # | |
22 boxnode = function (node) { | |
23 local center; | |
24 box (canvas, node, node.rect, ['color' = 0; 'fill' = 'on';]); | |
25 box (canvas, node, node.rect); | |
26 center = [ | |
27 'x' = (node.rect[0].x + node.rect[1].x) / 2; | |
28 'y' = (node.rect[0].y + node.rect[1].y) / 2; | |
29 ]; | |
30 if (node.name) | |
31 text (canvas, node, center, node.name, fontname, fontsize, 'cc'); | |
32 }; | |
33 circlenode = function (node) { | |
34 local center, radius; | |
35 center = [ | |
36 'x' = (node.rect[0].x + node.rect[1].x) / 2; | |
37 'y' = (node.rect[0].y + node.rect[1].y) / 2; | |
38 ]; | |
39 radius = [ | |
40 'x' = center.x - node.rect[0].x; | |
41 'y' = center.y - node.rect[0].y; | |
42 ]; | |
43 arc (canvas, node, center, radius, ['color' = 0; 'fill' = 'on';]); | |
44 arc (canvas, node, center, radius); | |
45 if (node.name) | |
46 text (canvas, node, center, node.name, fontname, fontsize, 'cc'); | |
47 }; | |
48 drawnode = boxnode; | |
49 drawedge = function (node1, node2) { | |
50 line (canvas, null, | |
51 [ | |
52 'x' = (node1.rect[1].x + node1.rect[0].x) / 2; | |
53 'y' = node1.rect[0].y; | |
54 ], [ | |
55 'x' = (node2.rect[1].x + node2.rect[0].x) / 2; | |
56 'y' = node2.rect[1].y; | |
57 ]); | |
58 }; | |
59 drawtree = function (node) { | |
60 local i; | |
61 for (i in nodearray) | |
62 drawnode (nodearray[i]); | |
63 drawtreerec (node); | |
64 }; | |
65 drawtreerec = function (node) { | |
66 local i, n; | |
67 if ((n = tablesize (node.ch)) > 0) { | |
68 for (i = 0; i < n; i = i + 1) { | |
69 drawedge (node, node.ch[i]); | |
70 drawtreerec (node.ch[i]); | |
71 } | |
72 } | |
73 }; | |
74 redraw = function (c) { | |
75 if (tree) | |
76 drawtree (tree); | |
77 }; | |
78 | |
79 # layout functions | |
80 # | |
81 complayout = function () { | |
82 leafx = 0; | |
83 leafrank = 0; | |
84 dolayout (tree, wrect[1].y - 10); | |
85 remove ('leafx'); | |
86 remove ('leafrank'); | |
87 }; | |
88 dolayout = function (node, pary) { | |
89 local r, n, i, size, lchp, rchp; | |
90 size = nodesize (node); | |
91 if (node.chn > 0) { | |
92 for (i = 0; i < node.chn; i = i + 1) | |
93 dolayout (node.ch[i], pary - size.y - dist.y); | |
94 node.rank = (node.ch[0].rank + node.ch[node.chn - 1].rank) / 2; | |
95 lchp = node.ch[0].rect; | |
96 rchp = node.ch[node.chn - 1].rect; | |
97 r[0].x = lchp[0].x + ((rchp[1].x - lchp[0].x) - size.x) / 2; | |
98 r[0].y = pary - size.y; | |
99 r[1].x = r[0].x + size.x; | |
100 r[1].y = pary; | |
101 node.rect = r; | |
102 } else { | |
103 node.rank = leafrank; | |
104 r[0].x = leafx; | |
105 r[0].y = pary - size.y; | |
106 r[1].x = r[0].x + size.x; | |
107 r[1].y = pary; | |
108 leafrank = leafrank + 1; | |
109 leafx = r[1].x + dist.x; | |
110 node.rect = r; | |
111 } | |
112 }; | |
113 | |
114 # editing functions | |
115 # | |
116 inode = function (point, name) { | |
117 local i, nnum, size; | |
118 nnum = nodenum; | |
119 if (~name) | |
120 name = ask ('give name of node:'); | |
121 nodearray[nnum].ch = []; | |
122 nodearray[nnum].chn = 0; | |
123 nodearray[nnum].name = name; | |
124 size = nodesize (nodearray[nnum]); | |
125 nodearray[nnum].rect[0] = point; | |
126 nodearray[nnum].rect[1] = ['x' = point.x + size.x; 'y' = point.y + size.y;]; | |
127 nodenum = nodenum + 1; | |
128 if (~tree) { | |
129 tree = nodearray[nnum]; | |
130 tree.depth = 0; | |
131 complayout (); | |
132 drawtree (tree); | |
133 } else | |
134 drawtree (nodearray[nnum]); | |
135 return nodearray[nnum]; | |
136 }; | |
137 iedge = function (node1, node2) { | |
138 node1.ch[node1.chn] = node2; | |
139 node1.chn = node1.chn + 1; | |
140 node2.depth = node1.depth + 1; | |
141 complayout (); | |
142 clear (canvas); | |
143 drawtree (tree); | |
144 }; | |
145 fix = function (node, op, np) { | |
146 if (node.depth ~= 0) | |
147 dist.y = dist.y + (op.y - np.y) / node.depth; | |
148 if (node.rank ~= 0) | |
149 dist.x = dist.x + (np.x - op.x) / node.rank; | |
150 complayout (); | |
151 clear (canvas); | |
152 drawtree (tree); | |
153 }; | |
154 nodesize = function (node) { | |
155 local siz; | |
156 if (~(siz = textsize (canvas, node.name, fontname, fontsize))) | |
157 siz = defsize; | |
158 else { | |
159 siz.x = siz.x + 8; | |
160 siz.y = siz.y + 8; | |
161 } | |
162 return siz; | |
163 }; | |
164 changenode = function (nodestyle) { | |
165 drawnode = nodestyle; | |
166 clear (canvas); | |
167 drawtree (tree); | |
168 }; | |
169 | |
170 # user interface functions | |
171 # | |
172 leftdown = function (data) { | |
173 if (~data.obj) | |
174 inode (data.pos, null); | |
175 }; | |
176 leftup = function (data) { | |
177 if (data.pobj) | |
178 fix (data.pobj, data.ppos, data.pos); | |
179 }; | |
180 middleup = function (data) { | |
181 if (data.pobj & data.obj) | |
182 iedge (data.pobj, data.obj); | |
183 }; | |
184 dops = function () { | |
185 local s; | |
186 | |
187 s = ['x' = 8 * 300; 'y' = 10.5 * 300;]; | |
188 fontname = 'Times-Roman'; | |
189 canvas = createwidget (-1, ['type' = 'ps'; 'size' = s;]); | |
190 setwidgetattr (canvas, ['window' = wrect;]); | |
191 drawtree (tree); | |
192 destroywidget (canvas); | |
193 canvas=defcanvas; | |
194 fontname = 'fixed'; | |
195 }; |