Debian Codegraph @

src/tools/dlgedit/dlg_module.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_module.cc
21 *
22 * @author Kai Sterker
23 * @brief One complete dialogue or sub-dialogue.
24 */
25
26 #include <algorithm>
27 #include "dlg_module.h"
28 #include "dlg_arrow.h"
29 #include "gui_dlgedit.h"
30 #include "gui_resources.h"
31
32 // ctor
33 DlgModule::DlgModule (std::string p, std::string n, std::string u, std::string d)
34 {
35 entry_.setDescription (d);
36 uid_ = u;
37 path_ = p + "/";
38 name_ = n;
39
40 init ();
41 }
42
43 // dtor
44 DlgModule::~DlgModule ()
45 {
46 if (GuiDlgedit::window != NULL)
47 {
48 GuiDlgedit::window->tree ()->removeModule (this);
49 }
50 }
51
52 // initialize a newly constructed DlgModule
53 void DlgModule::init ()
54 {
55 type_ = MODULE;
56 mode_ = IDLE;
57 state_ = IDLE;
58 selected_ = NULL;
59 highlighted_ = NULL;
60 parent_ = NULL;
61 changed_ = false;
62 displayed_ = false;
63 nid_ = 0;
64 serial_ = 1;
65 }
66
67 // reset dialogue to initial state
68 void DlgModule::clear ()
69 {
70 // delete all nodes
71 while (!nodes.empty ()) deleteNode (nodes.front ());
72
73 // clear custom code and such
74 entry_.clear ();
75
76 // reset all variables
77 init ();
78 }
79
80 // calculate shape of sub-dialogue
81 void DlgModule::initShape (const DlgPoint &center)
82 {
83 int width;
84
85 // calculate width of the module icon
86 PangoLayout *font = GuiResources::font ();
87 pango_layout_set_text (font, name().c_str(), -1);
88 pango_layout_get_pixel_size (font, &width, NULL);
89
90 width += 10;
91
92 // align module to the (imaginary) grid and set shape
93 top_left = DlgPoint (center.x (), center.y ());
94 top_left.move (-width/2 - (center.x () % CIRCLE_DIAMETER), -(center.y () % CIRCLE_DIAMETER));
95 resize (width, 20);
96 }
97
98 // select a given node
99 bool DlgModule::selectNode (DlgNode *node)
100 {
101 // try to find the given node in our array of nodes
102 std::vector<DlgNode*>::iterator i = find (nodes.begin(), nodes.end(), node);
103
104 // if it is not there, return
105 if (i == nodes.end ()) return false;
106
107 // see if a node is already selected
108 if (selected_ != NULL) return false;
109
110 // remember which node is selected
111 selected_ = node;
112
113 // set the mode of dialogue and node
114 state_ = NODE_SELECTED;
115 node->setMode (NODE_SELECTED);
116
117 return true;
118 }
119
120 // highlight the given node
121 DlgNode* DlgModule::highlightNode (DlgNode *node)
122 {
123 // mark the new node as highlighted ...
124 DlgNode *prev = highlighted_;
125 highlighted_ = node;
126
127 // ... and return the node highlighted before
128 return prev;
129 }
130
131 // select the dialogue's root node.
132 bool DlgModule::selectRoot ()
133 {
134 for (std::vector<DlgNode*>::iterator i=nodes.begin(); i != nodes.end(); i++)
135 if ((*i)->type () != LINK)
136 if ((*i)->prev (FIRST) == NULL)
137 return selectNode (*i);
138
139 return false;
140 }
141
142 // get the node at the given postion
143 DlgNode* DlgModule::getNode (DlgPoint &position)
144 {
145 // find the node with the given pos in our array of nodes
146 std::vector<DlgNode*>::iterator i;
147
148 for (i = nodes.begin (); i != nodes.end (); i++)
149 if (*(*i) == position) return *i;
150
151 return NULL;
152 }
153
154 // get the node with the given module and node ids
155 DlgNode* DlgModule::getNode (int mid, int nid)
156 {
157 // first, get the module
158 DlgModule *module = getModule (mid);
159
160 // then, get the node
161 if (module) return module->getNode (nid);
162
163 return NULL;
164 }
165
166 // get node with the given node id
167 DlgNode* DlgModule::getNode (int id)
168 {
169 // find the node with the given pos in our array of nodes
170 std::vector<DlgNode*>::iterator i;
171
172 for (i = nodes.begin (); i != nodes.end (); i++)
173 if ((*i)->node_id () == id && (*i)->type () != MODULE)
174 return *i;
175
176 return NULL;
177 }
178
179 // get module with the given module id
180 DlgModule* DlgModule::getModule (int id)
181 {
182 if (id == nid_) return this;
183
184 std::vector<DlgNode*>::iterator i;
185 DlgModule *module;
186
187 for (i = nodes.begin (); i != nodes.end (); i++)
188 if ((*i)->type () == MODULE)
189 {
190 // FIXME shouldn't this be (*i)->getModule(id) ???
191 module = getModule (id);
192 if (module) return module;
193 }
194
195 // nothing found
196 return NULL;
197 }
198
199 // add a node to the dialogue
200 void DlgModule::addNode (DlgNode *node)
201 {
202 nodes.push_back (node);
203 }
204
205 // delete a node from the dialogue
206 bool DlgModule::deleteNode ()
207 {
208 // if no node is selected, there's nothing to delete
209 if (selected_ == NULL) return false;
210
211 // otherwise, first deselect it
212 DlgNode *node = deselectNode ();
213
214 // actually delete it
215 deleteNode (node);
216
217 return true;
218 }
219
220 // delete the given node
221 void DlgModule::deleteNode (DlgNode *node)
222 {
223 // if the node is a circle, also delete the attached arrows
224 if (node->type () != LINK)
225 {
226 // reset traversal info if current node gets deleted
227 traverse_.clear();
228
229 // delete all preceding arrows
230 for (DlgNode *i = node->prev (FIRST); i != NULL; i = node->prev (FIRST))
231 {
232 nodes.erase (remove (nodes.begin (), nodes.end (), i), nodes.end ());
233 if (highlighted_ == i) highlighted_ = NULL;
234 delete i;
235 }
236
237 // delete all following arrows
238 for (DlgNode *i = node->next (FIRST); i != NULL; i = node->next (FIRST))
239 {
240 nodes.erase (remove (nodes.begin (), nodes.end (), i), nodes.end ());
241 if (highlighted_ == i) highlighted_ = NULL;
242 delete i;
243 }
244 }
245
246 // remove the node itself from the vector
247 nodes.erase (remove (nodes.begin (), nodes.end (), node), nodes.end ());
248 if (highlighted_ == node) highlighted_ = NULL;
249 delete node;
250 }
251
252 // deselect the selected node
253 DlgNode* DlgModule::deselectNode ()
254 {
255 DlgNode *retval = selected_;
256
257 // see if a node is selected at all
258 if (selected_ == NULL) return NULL;
259
260 // unselect node
261 state_ = IDLE;
262 selected_->setMode (IDLE);
263
264 // nothing selected
265 selected_ = NULL;
266
267 // return the node for drawing
268 return retval;
269 }
270
271 // indicate that this module has been modified by the user
272 void DlgModule::setChanged (bool changed)
273 {
274 if (changed_ != changed)
275 {
276 changed_ = changed;
277
278 // updated view
279 GuiDlgedit::window->setChanged ();
280 }
281 }
282
283 // draw the module
284 void DlgModule::draw (cairo_surface_t *surface, DlgPoint &offset, GtkWidget *widget)
285 {
286 cairo_t *cr = cairo_create (surface);
287
288 // get the color for drawing the circle
289 const GdkColor *gc = GuiResources::getColor (mode_, type_);
290
291 // offset circle
292 DlgPoint position = topLeft ().offset (offset);
293 DlgRect area (position, width () + 1, height () + 1);
294
295 // draw everything to the surface
296 drawRectangle (cr, GuiResources::getColor (GC_WHITE), TRUE, position.x (), position.y (), width (), height ());
297 drawRectangle (cr, gc, FALSE, position.x (), position.y (), width (), height ());
298
299 // get the font to draw name
300 const PangoFontDescription *desc = pango_layout_get_font_description (GuiResources::font ());
301
302 // create pango cairo compatible layout
303 PangoLayout *font = pango_cairo_create_layout(cr);
304 pango_layout_set_font_description (font, desc);
305 pango_layout_set_text (font, name().c_str(), -1);
306
307 // place text in module's center
308 int h;
309 pango_layout_get_pixel_size (font, NULL, &h);
310
311 int x = position.x () + 5;
312 int y = position.y () + (height () - h) / 2;
313
314 // set font color and position
315 gdk_cairo_set_source_color(cr, gc);
316 pango_cairo_update_layout (cr, font);
317 cairo_move_to(cr, x, y);
318
319 // draw text
320 pango_cairo_show_layout (cr, font);
321 g_object_unref(font);
322
323 // Update the drawing area
324 update (widget, area);
325
326 cairo_destroy(cr);
327 }
328
329 // get toplevel module
330 DlgModule* DlgModule::toplevel ()
331 {
332 DlgModule *toplevel = this;
333
334 while (toplevel->parent () != NULL) toplevel = toplevel->parent ();
335
336 return toplevel;
337 }
338
339 // Get extension of the graph for proper displaying
340 void DlgModule::extension (int &min_x, int &max_x, int &y)
341 {
342 y = 0;
343 min_x = 0;
344 max_x = 0;
345
346 for (std::vector<DlgNode*>::iterator i=nodes.begin(); i != nodes.end(); i++)
347 if ((*i)->type () != LINK)
348 {
349 if ((*i)->y () < y) y = (*i)->y ();
350
351 if ((*i)->x () < min_x) min_x = (*i)->x ();
352 else if ((*i)->x () > max_x) max_x = (*i)->x ();
353 }
354 }
355
356 // load a dialogue from file
357 bool DlgModule::load ()
358 {
359 int i = 1, id = 0, n;
360 DlgCircle *circle;
361 DlgArrow *arrow;
362 std::string s;
363
364 // load all nodes and toplevel items
365 while (i)
366 {
367 switch (i = parse_dlgfile (s, n))
368 {
369 // load text node
370 case LOAD_CIRCLE:
371 {
372 circle = new DlgCircle (nid_, id++);
373 circle->load ();
374
375 nodes.push_back (circle);
376
377 break;
378 }
379
380 // load arrow node
381 case LOAD_ARROW:
382 {
383 arrow = new DlgArrow;
384
385 if (arrow->load (this))
386 nodes.push_back (arrow);
387
388 break;
389 }
390
391 // load submodule node
392 case LOAD_MODULE:
393 {
394 if (parse_dlgfile (s, n) == LOAD_STR)
395 {
396 // get filename of submodule
397 std::string file = path_ + s;
398
399 // try to create the subdialogue
400 DlgModule *subdlg = GuiDlgedit::window->loadSubdialogue (file);
401
402 // if succeeded, read the final bits
403 if (subdlg)
404 {
405 subdlg->loadSubdialogue ();
406 subdlg->setParent (this);
407
408 nodes.push_back (subdlg);
409 }
410
411 }
412
413 break;
414 }
415
416 case LOAD_PROJECT:
417 {
418 if (parse_dlgfile (s, n) == LOAD_STR) entry_.setProject (s);
419 break;
420 }
421
422 case LOAD_NOTE:
423 {
424 if (parse_dlgfile (s, n) == LOAD_STR) entry_.setDescription (s);
425 break;
426 }
427
428 case LOAD_FUNC:
429 {
430 if (parse_dlgfile (s, n) == LOAD_STR) entry_.setMethods (s);
431 break;
432 }
433
434 case LOAD_CTOR:
435 {
436 if (parse_dlgfile (s, n) == LOAD_STR) entry_.setCtor (s);
437 break;
438 }
439
440 case LOAD_DTOR:
441 {
442 if (parse_dlgfile (s, n) == LOAD_STR) entry_.setDtor (s);
443 break;
444 }
445
446 case LOAD_IMPORTS:
447 {
448 if (parse_dlgfile (s, n) == LOAD_STR) entry_.setImports (s);
449 break;
450 }
451
452 case LOAD_RACE:
453 {
454 if (parse_dlgfile (s, n) == LOAD_NUM); //->myplayer->set_val ("race", n);
455 break;
456 }
457
458 case LOAD_GENDER:
459 {
460 if (parse_dlgfile (s, n) == LOAD_NUM); //->myplayer->set_val ("gender", n);
461 break;
462 }
463
464 case LOAD_NPC:
465 {
466 if (parse_dlgfile (s, n) == LOAD_STR);
467 break;
468 }
469
470 case LOAD_ID:
471 {
472 if (parse_dlgfile (s, n) == LOAD_NUM) serial_ = n;
473 break;
474 }
475
476 default: break;
477 }
478 }
479
480 // set serial of version 1 dialogues
481 if (serial_ < id) serial_ = id;
482
483 return true;
484 }
485
486 // load sub-dialogue
487 void DlgModule::loadSubdialogue ()
488 {
489 int i = 1, n;
490 std::string s;
491
492 while (i)
493 {
494 switch (i = parse_dlgfile (s, n))
495 {
496 // module loaded or EOF
497 case LOAD_END:
498 return;
499
500 // get id of submodule
501 case LOAD_ID:
502 {
503 if (parse_dlgfile (s, n) == LOAD_NUM) nid_ = n;
504 break;
505 }
506
507 // get position of submodule
508 case LOAD_POS:
509 {
510 int x, y;
511 int width;
512
513 PangoLayout *font = GuiResources::font ();
514 pango_layout_set_text (font, name().c_str(), -1);
515 pango_layout_get_pixel_size (font, &width, NULL);
516
517 if (parse_dlgfile (s, n) == LOAD_NUM) x = n;
518 if (parse_dlgfile (s, n) == LOAD_NUM) y = n;
519
520 top_left = DlgPoint (x, y);
521 bottom_right = DlgPoint (x + width + 10, y + 20);
522 break;
523 }
524
525 default: break;
526 }
527 }
528 }
529
530 // save dialogue to file
531 bool DlgModule::save (std::string &path, std::string &name)
532 {
533 // update path and name
534 path_ = path + "/";
535 if (name_ != name)
536 {
537 name_ = name;
538 changed_ = true;
539 }
540
541 // open file
542 std::ofstream out (fullName ().c_str ());
543
544 // opening failed for some reasons
545 if (!out.is_open()) return false;
546
547 // Write Header: Adonthell Dialogue System file version 2
548 out << "# Dlgedit File Format 2\n#\n"
549 << "# Produced by Adonthell Dlgedit v" << _VERSION_ << "\n"
550 << "# (C) 2000/2001/2002/2003 The Adonthell Team & Kai Sterker\n#\n"
551 << "# $I" << "d$\n\n"
552 << "Note \xa7" << entry_.description () << "\xa7\n\n";
553
554 // Node ID
555 out << "Id " << serial_ << "\n";
556
557 // Save settings and stuff
558 if (entry_.project () != "none")
559 out << "Proj \xa7" << entry_.project () << "\xa7\n";
560
561 if (entry_.imports () != "")
562 out << "Inc \xa7" << entry_.imports () << "\xa7\n";
563
564 if (entry_.ctor () != "")
565 out << "Ctor \xa7" << entry_.ctor () << "\xa7\n";
566
567 if (entry_.dtor () != "")
568 out << "Dtor \xa7" << entry_.dtor () << "\xa7\n";
569
570 if (entry_.methods () != "")
571 out << "Func \xa7" << entry_.methods () << "\xa7\n";
572
573 // Save Circles first, as arrows depend on them when loading later on
574 for (std::vector<DlgNode*>::iterator i = nodes.begin (); i != nodes.end (); i++)
575 if ((*i)->type () != LINK)
576 (*i)->save (out);
577
578 // Save Arrows
579 for (std::vector<DlgNode*>::iterator i = nodes.begin (); i != nodes.end (); i++)
580 if ((*i)->type () == LINK)
581 (*i)->save (out);
582
583 out.flush();
584 out.close();
585
586 // mark dialogue as unchanged
587 setChanged (false);
588
589 return true;
590 }
591
592 // save a sub-module
593 void DlgModule::save (std::ofstream &file)
594 {
595 // module's relative filename
596 file << "\nModule " << relativeName () << "\n";
597
598 // module's id
599 file << " Id " << nid_ << "\n";
600
601 // module's position
602 file << " Pos " << x () << " " << y () << "\n";
603
604 file << "End\n";
605 }
606
607 // return the module's path relative to its parent
608 std::string DlgModule::relativeName ()
609 {
610 // no parent -> return absolute Name
611 if (parent_ == NULL) return fullName ();
612
613 std::string p_path = parent_->path (); // parent path
614 std::string m_path = path_; // module path
615 std::string r_path = ""; // module's path relative to parent
616
617 unsigned long pos = 0;
618
619 // find the part of the filename that matches
620 while (pos < m_path.length () && pos < p_path.length () &&
621 p_path[pos] == m_path[pos]) pos++;
622
623 // complete module path matched
624 if (pos == m_path.length ())
625 {
626 // so either both files are in the same path, or the sub-dialogue
627 // is on a higher level
628 while ((pos = p_path.find ('/', pos)) != p_path.npos) r_path += "../";
629 }
630
631 // complete parent path matched
632 else if (pos == p_path.length ())
633 {
634 // so the sub-dialogue is in a sub-directory
635 r_path = m_path.substr (pos);
636 }
637
638 // none of the paths completely matched
639 else
640 {
641 // sub-dialogue is in a different directory on a higher level
642 unsigned long i = pos;
643 while ((i = p_path.find ('/', i)) != p_path.npos) r_path += "../";
644 r_path += m_path.substr (pos);
645 }
646
647 return r_path + name_ + FILE_EXT;
648 }