Debian Codegraph @

src/tools/dlgedit/dlg_arrow.cc

raw
1 /*
2 Copyright (C) 2002/2003 Kai Sterker <[email protected]>
3 Part of the Adonthell Project <http://adonthell.nongnu.org>
4
5 Dlgedit is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 Dlgedit is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with Dlgedit. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20 * @file dlg_arrow.cc
21 *
22 * @author Kai Sterker
23 * @brief The node linking two DlgCircle's.
24 */
25
26 #include <math.h>
27 #include <iostream>
28 #include "dlg_arrow.h"
29 #include "dlg_module.h"
30 #include "gui_resources.h"
31
32 // Create a new link between two nodes
33 DlgArrow::DlgArrow (DlgNode *start, DlgNode *end)
34 {
35 type_ = LINK;
36 mode_ = IDLE;
37 nid_ = 0;
38
39 // add the start and end to this arrow
40 prev_.push_back (start);
41 next_.push_back (end);
42
43 // add the arrow to it's start and end node
44 start->addNext (this);
45 end->addPrev (this);
46
47 // init the Arrow's shape, for faster drawing
48 initShape ();
49 }
50
51 // Remove arrow from it's start and end node
52 DlgArrow::~DlgArrow ()
53 {
54 DlgNode *circle;
55
56 // remove arrow from previous circle
57 if (prev_.size () != 0)
58 {
59 circle = prev_.front ();
60 circle->removeNext (this);
61 }
62
63 // remove arrow from following circle
64 if (next_.size () != 0)
65 {
66 circle = next_.front ();
67 circle->removePrev (this);
68 }
69 }
70
71 // prepare the Arrow's shape
72 void DlgArrow::initShape ()
73 {
74 // start- and end-point of arrow
75 DlgPoint start = prev_.front ()->center ();
76 DlgPoint end = next_.front ()->center ();
77
78 // gradient of line
79 int x = start.x () - end.x ();
80 int y = start.y () - end.y ();
81
82 // scaled to 1
83 float s = sqrt (x*x + y*y);
84 float a = x / s;
85 float b = y / s;
86
87 DlgPoint line0 = getIntersection (start, end, *prev_.front ());
88 DlgPoint line1 = getIntersection (end, start, *next_.front ());
89
90 // line from start- to end-circle
91 line[0] = (GdkPoint) line0;
92 line[1] = (GdkPoint) line1;
93
94 // arrow's tip
95 tip[0] = line[1];
96 tip[1] = (GdkPoint) line1.offset ((int)(a * 8.0f + b * 4.0f), (int)(b * 8.0f - a * 4.0f));
97 tip[2] = (GdkPoint) line1.offset ((int)(a * 8.0f - b * 4.0f), (int)(b * 8.0f + a * 4.0f));
98
99 // calculate arrow's new dimension
100 init (line0, line1);
101 grow (2, 2);
102 }
103
104 // calculate intersection of arrow and node shape
105 DlgPoint DlgArrow::getIntersection (DlgPoint &start, DlgPoint &end, DlgRect &shape)
106 {
107 DlgPoint tl = shape.topLeft ();
108 DlgPoint br = shape.bottomRight ();
109
110 int direction;
111 double a, b;
112 DlgPoint p;
113
114 // gradient of line
115 int x = end.x () - start.x ();
116 int y = end.y () - start.y ();
117
118 // tangens of angle between line(start, end) and x-axis
119 double m = x == 0 ? 1.0 : double (y) / x;
120 double n = y == 0 ? 1.0 : double (x) / y;
121
122 // direction where line(start, end) intersects with border of start
123 enum { NORTH, EAST, SOUTH, WEST };
124
125 // calculate where line(start, end) intersects with border of start
126 if (y < 0)
127 {
128 a = tl.y () - start.y ();
129
130 if (x > 0)
131 {
132 // 1. quadrant
133 b = br.x () - start.x ();
134
135 if (m > a / b) direction = EAST;
136 else direction = NORTH;
137 }
138 else
139 {
140 // 2. quadrant
141 b = tl.x () - start.x ();
142
143 if (m >= a / b) direction = NORTH;
144 else direction = WEST;
145 }
146 }
147 else
148 {
149 a = br.y () - start.y ();
150
151 if (x < 0)
152 {
153 // 3. quadrant
154 b = tl.x () - start.x ();
155
156 if (m > a / b) direction = WEST;
157 else direction = SOUTH;
158 }
159 else
160 {
161 // 4. quadrant
162 b = br.x () - start.x ();
163
164 if (m >= a / b) direction = SOUTH;
165 else direction = EAST;
166 }
167 }
168
169 // now we have one coordinate of the intersection and can
170 // calculate the second
171 switch (direction)
172 {
173 case NORTH:
174 {
175 p = DlgPoint ((tl.y () - start.y ()) * n + start.x (), tl.y ());
176 break;
177 }
178 case EAST:
179 {
180 p = DlgPoint (br.x (), start.y () + m * (br.x () - start.x ()));
181 break;
182 }
183 case SOUTH:
184 {
185 p = DlgPoint ((br.y () - start.y ()) * n + start.x (), br.y ());
186 break;
187 }
188 case WEST:
189 {
190 p = DlgPoint (tl.x (), start.y () + m * (tl.x () - start.x ()));
191 break;
192 }
193 }
194
195 return p;
196 }
197
198 // draw the Arrow
199 void DlgArrow::draw (cairo_surface_t *surface, DlgPoint &point, GtkWidget *widget)
200 {
201 cairo_t *cr = cairo_create (surface);
202 const GdkColor *color = GuiResources::getColor (mode_, type_);
203
204 DlgPoint l[2];
205 DlgPoint t[4];
206 DlgRect area = inflate (10, 10);
207
208 // Current position
209 area.move (point);
210
211 l[0] = DlgPoint (line[0]).offset (point);
212 l[1] = DlgPoint (line[1]).offset (point);
213 t[0] = DlgPoint (tip[0]).offset (point);
214 t[1] = DlgPoint (tip[1]).offset (point);
215 t[2] = DlgPoint (tip[2]).offset (point);
216 t[3] = DlgPoint (tip[0]).offset (point);
217
218 // draw everything
219 drawPolygon (cr, color, FALSE, l, 2);
220 drawPolygon (cr, GuiResources::getColor (GC_WHITE), TRUE, t, 3);
221 drawPolygon (cr, color, FALSE, t, 4);
222
223 // update drawing area
224 update (widget, area);
225
226 cairo_destroy(cr);
227 }
228
229 bool DlgArrow::operator== (DlgPoint &point)
230 {
231 // is the point anywhere near the arrow?
232 if (contains (point))
233 {
234 int x, dx, dy;
235
236 // straight g: ax + by + c = 0 through 2 points p(x, y) und q(x, y):
237 // with a = (p.y - q.y), b = (q.x - p.x), c = (-(p.x * a + p.y * b))
238
239 // insert point in above function; values |x| < 500 are close to the arrow
240 dx = line[1].x - line[0].x;
241 dy = line[0].y - line[1].y;
242
243 x = (dy * point.x () + dx * point.y ()) - (line[0].x * dy + line[0].y * dx);
244 if (x < 500 && x > -500) return true;
245 }
246 return false;
247 }
248
249 // load an arrow
250 bool DlgArrow::load (DlgNode *m)
251 {
252 DlgModule *module = (DlgModule *) m;
253 DlgModule *owner = module;
254 DlgModule *toplevel = module->toplevel ();
255 DlgNode *circle;
256 std::string str;
257 int n;
258
259 // as long as we haven't reached EOF or are finished with loading:
260 while (1)
261 {
262 // look what we find in the file
263 switch (parse_dlgfile (str, n))
264 {
265 // EOF or finished
266 case 0:
267 case LOAD_END:
268 {
269 // claculate the Arrow's shape
270 initShape ();
271
272 // finish concatenation
273 prev_.front ()->addNext (this);
274 next_.front ()->addPrev (this);
275
276 return true;
277 }
278
279 // Module node belongs to
280 case LOAD_MODULE:
281 {
282 if (parse_dlgfile (str, n) == LOAD_NUM)
283 {
284 // get the module the node belongs to
285 owner = toplevel->getModule (n);
286
287 // failed
288 if (!owner || owner->type () != MODULE)
289 {
290 std::cout << "DlgArrow::load: failed to getModule ("
291 << n << ")!" << std::endl;
292
293 return false;
294 }
295 }
296
297 break;
298 }
299
300 // Node prior to arrow
301 case LOAD_PREV:
302 {
303 if (parse_dlgfile (str, n) == LOAD_NUM)
304 {
305 // get the id of the previous circle
306 circle = owner->getNode (n);
307
308 // failed
309 if (circle == NULL) return false;
310 }
311
312 // everything okay, so add circle
313 prev_.push_back (circle);
314
315 // restore owner
316 owner = module;
317
318 break;
319 }
320
321 // Node following arrow
322 case LOAD_NEXT:
323 {
324 if (parse_dlgfile (str, n) == LOAD_NUM)
325 {
326 // get the id of the previous circle
327 circle = owner->getNode (n);
328
329 // failed
330 if (circle == NULL) return false;
331 }
332
333 // everything okay, so add circle
334 next_.push_back (circle);
335
336 // restore owner
337 owner = module;
338
339 break;
340 }
341
342 // Nodes linked to the arrow (obsolete -> convert to normal arrow)
343 case LOAD_LINK:
344 {
345 if (parse_dlgfile (str, n) == LOAD_NUM)
346 {
347 circle = module->getNode (n);
348 if (circle == NULL) break;
349
350 DlgArrow *arrow = new DlgArrow (circle, next_.front ());
351 module->addNode (arrow);
352 }
353 break;
354 }
355
356 // Just ignore everything else
357 default: break;
358 }
359 }
360
361 return false;
362 }
363
364 // save an arrow to file
365 void DlgArrow::save (std::ofstream &file)
366 {
367 // Keyword "Arrow" and arrow's number
368 file << "\nArrow\n";
369
370 // module of start circle, if != 0
371 if (prev_.front ()->module_id () != 0)
372 file << " Module " << prev_.front ()->module_id () << "\n";
373
374 // start circle
375 file << " Prev " << prev_.front ()->node_id () << "\n";
376
377 // module of end circle, if != 0
378 if (next_.front ()->module_id () != 0)
379 file << " Module " << next_.front ()->module_id () << "\n";
380
381 // end circle
382 file << " Next " << next_.front ()->node_id () << "\n";
383
384 // end of node
385 file << "End\n";
386 }