/
gui_graph.cc
870 lines (680 loc) · 22.9 KB
/
gui_graph.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
/*
$Id: gui_graph.cc,v 1.4 2009/04/03 22:00:34 ksterker Exp $
Copyright (C) 2002 Kai Sterker <kaisterker@linuxgames.com>
Part of the Adonthell Project http://adonthell.linuxgames.com
Dlgedit is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Dlgedit is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Dlgedit; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
* @file gui_graph.cc
*
* @author Kai Sterker
* @brief View for the dialogue graph
*/
#include <gtk/gtk.h>
#include "cfg_data.h"
#include "dlg_mover.h"
#include "gui_dlgedit.h"
#include "gui_graph_events.h"
#include "gui_resources.h"
#include "gui_circle.h"
#include "gui_file.h"
// Constructor
GuiGraph::GuiGraph (GtkWidget *paned) : Scrollable ()
{
// initialize members to sane values
mover = NULL;
module = NULL;
offset = NULL;
surface = NULL;
tooltip = NULL;
// create drawing area for the graph
graph = gtk_drawing_area_new ();
gtk_drawing_area_size (GTK_DRAWING_AREA (graph), 200, 450);
gtk_paned_add2 (GTK_PANED (paned), graph);
gtk_widget_show (graph);
gtk_widget_grab_focus (graph);
// register our event callbacks
gtk_signal_connect (GTK_OBJECT (graph), "expose_event", (GtkSignalFunc) expose_event, this);
gtk_signal_connect (GTK_OBJECT (graph), "configure_event", (GtkSignalFunc) configure_event, this);
gtk_signal_connect (GTK_OBJECT (graph), "button_press_event", (GtkSignalFunc) button_press_event, this);
gtk_signal_connect (GTK_OBJECT (graph), "button_release_event", (GtkSignalFunc) button_release_event, this);
gtk_signal_connect (GTK_OBJECT (graph), "motion_notify_event", (GtkSignalFunc) motion_notify_event, this);
gtk_signal_connect (GTK_OBJECT (GuiDlgedit::window->getWindow ()), "key_press_event", (GtkSignalFunc) key_press_notify_event, this);
gtk_widget_set_events (graph, GDK_EXPOSURE_MASK | GDK_LEAVE_NOTIFY_MASK | GDK_BUTTON_PRESS_MASK |
GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_KEY_PRESS_MASK);
}
// dtor
GuiGraph::~GuiGraph()
{
}
// attach a module
void GuiGraph::attachModule (DlgModule *m, bool cntr)
{
module = m;
// get reference of the module's offset
offset = &m->offset ();
// center the module in view
if (cntr) center ();
// if a node is selected, update the instant preview
GuiDlgedit::window->list ()->display (module->selected ());
// update the module structure
GuiDlgedit::window->tree ()->select (module);
// update the program state
GuiDlgedit::window->setMode (module->state ());
// set the size of the dialogue
drawing_area.resize (graph->allocation.width, graph->allocation.height);
// tell the module that it is in view
module->setDisplayed (true);
// display the module
draw ();
}
// detach a module
void GuiGraph::detachModule ()
{
module = NULL;
// clear the instant preview
GuiDlgedit::window->list ()->clear ();
// update the program state
GuiDlgedit::window->setMode (IDLE);
// remove the tooltip if it is open
if (tooltip)
{
delete tooltip;
tooltip = NULL;
}
}
// display a different module
void GuiGraph::switchModule (DlgModule *m)
{
// we're switching between sub-dialogues
if (module) module->setDisplayed (false);
detachModule ();
attachModule (m);
}
// create a new circle
bool GuiGraph::newCircle (DlgPoint &point, node_type type)
{
// if there is no module assigned to the view, there is nothing to do
if (module == NULL) return false;
// get the serial number to use for this node
int &serial = module->serial ();
// create the new node ...
DlgCircle *circle = new DlgCircle (point, type, serial, module->node_id ());
// ... add it to the module ...
module->addNode (circle);
// ... and select it for editing
module->selectNode (circle);
// see whether creation was cancelled
if (!editNode ())
{
// cleanup
module->deleteNode ();
return false;
}
// update the program state
GuiDlgedit::window->setMode (NODE_SELECTED);
// node created -> increase serial number for next node
serial++;
return true;
}
// create a new arrow
bool GuiGraph::newArrow (DlgPoint &point)
{
// if there is no module assigned to the view, there is nothing to do
if (module == NULL) return false;
// calculate absolute position of the point
point.move (-offset->x (), -offset->y ());
// get start and end of the circle
DlgNode *start = module->selected ();
DlgNode *end = module->getNode (point);
// sanity checks
if (!start || start->type () == LINK) return false;
if (end && end->type () == LINK) return false;
// if no end selected, create a new circle first
if (end == NULL)
{
// chose a sensible type for the new circle
node_type type = (start->type () == NPC ? PLAYER : NPC);
// there musn't be a node selected to create a new circle
module->deselectNode ();
// try to create a new circle
if (newCircle (point, type))
{
// chose the newly created circle as end of the arrow
end = module->getNode (point);
// restore selection
deselectNode ();
selectNode (start);
}
// do we have a valid end now?
if (end == NULL) return false;
}
// no loops and no duplicate connections
if (start == end || ((DlgCircle *) start)->hasChild (end)) return false;
// no connection between start and end if both are PLAYER nodes
if (start->type () == PLAYER && end->type () == PLAYER) return false;
// now create the arrow between start and end, ...
DlgArrow *arrow = new DlgArrow (start, end);
// ... add it to the module ...
module->addNode (arrow);
// ... and update everything
GuiDlgedit::window->list ()->display (start);
arrow->draw (surface, *offset, graph);
module->setChanged ();
return true;
}
// add a new subdialogue
bool GuiGraph::newModule (DlgPoint &point)
{
// if there is no module assigned to the view, there is nothing to do
if (module == NULL) return false;
// if a project root exists, use that in file selector
std::string dir = CfgData::data->getBasedir (module->entry ()->project ());
// otherwise revert to directory last opened
if (dir == "") dir = GuiDlgedit::window->directory ();
// allow the user to select a module
GtkWindow *parent = GTK_WINDOW (GuiDlgedit::window->getWindow());
GuiFile fs (parent, GTK_FILE_CHOOSER_ACTION_OPEN, "Select sub-dialogue to add", dir + "/");
fs.add_filter ("*"FILE_EXT, "Adonthell Dialogue Source");
// set shortcuts
const std::vector<std::string> & projects = CfgData::data->projectsFromDatadir ();
for (std::vector<std::string>::const_iterator i = projects.begin (); i != projects.end (); i++)
{
const std::string &dir = CfgData::data->getBasedir (*i);
fs.add_shortcut (dir);
}
if (fs.run ())
{
DlgModule *subdlg = GuiDlgedit::window->loadSubdialogue (fs.getSelection());
if (subdlg == NULL) return false;
// set parent of the sub-dialogue
subdlg->setParent (module);
// set id of the sub-dialogue
subdlg->setID ();
// draw the sub-dialogue
subdlg->initShape (point);
subdlg->draw (surface, *offset, graph);
// update the module
module->setChanged ();
module->addNode (subdlg);
// update the module tree
GuiDlgedit::window->tree ()->insert (module, subdlg);
return true;
}
return false;
}
// delete the selected node
bool GuiGraph::deleteNode ()
{
// if there is no module assigned to the view, there is nothing to select
if (module == NULL) return false;
// try to delete node
if (module->deleteNode ())
{
// update the program state
GuiDlgedit::window->setMode (IDLE);
// update the instant preview
GuiDlgedit::window->list ()->clear ();
// remove tooltip if it is open
if (tooltip)
{
delete tooltip;
tooltip = NULL;
}
// redraw the dialogue
draw ();
return true;
}
return false;
}
// select a node
bool GuiGraph::selectNode (DlgNode *node)
{
// if there is no module assigned to the view, there is nothing to select
if (module == NULL) return false;
// a node has been selected
if (module->selectNode (node))
{
// update the program state
GuiDlgedit::window->setMode (NODE_SELECTED);
// update the instant preview
GuiDlgedit::window->list ()->display (node);
// redraw the node
node->draw (surface, *offset, graph);
return true;
}
return false;
}
// select the node at the given position
bool GuiGraph::selectNode (DlgPoint &point)
{
// if there is no module assigned to the view, there is nothing to select
if (module == NULL) return false;
// is point within module's boundaries?
if (!drawing_area.contains (point)) return false;
// calculate absolute position of the point
point.move (-offset->x (), -offset->y ());
// see if we're over a node
DlgNode *node = module->getNode (point);
// no node at that position
if (node == NULL) return false;
// otherwise select the node
if (selectNode (node))
{
module->traverse ()->select (node);
return true;
}
return false;
}
// select parent
bool GuiGraph::selectParent ()
{
// if there is no module assigned to the view, there is nothing to select
if (module == NULL) return false;
// see if a node is currently selected
DlgNode *selected = module->selected ();
// if so ...
if (selected)
{
// ... try to retrieve it's parent
DlgNode *parent = module->traverse ()->up ();
// deselect current
deselectNode ();
// if we have it, then select it
if (parent) return selectNode (parent);
}
// if no node is selected, we simply select the first one
return selectRoot ();
}
// select the child of a node
bool GuiGraph::selectChild ()
{
// if there is no module assigned to the view, there is nothing to select
if (module == NULL) return false;
// see if a node is currently selected
DlgNode *selected = module->selected ();
// if so ...
if (selected)
{
// ... try to retrieve it's child
DlgNode *child = module->traverse ()->down ();
// if we have it, then select it
if (child)
{
deselectNode ();
return selectNode (child);
}
}
// if no node is selected, we simply select the first one
else return selectRoot ();
return false;
}
// select a sibling of the currently selected node
bool GuiGraph::selectSibling (query_type pos)
{
// if there is no module assigned to the view, there is nothing to select
if (module == NULL) return false;
// see if a node is currently selected
DlgNode *selected = module->selected ();
// if so ...
if (selected)
{
DlgNode *sibling;
// see if selected is arrow
if (selected->type () == LINK)
selected = selected->next (FIRST);
// ... try to retrieve it's child
if (pos == PREV) sibling = module->traverse ()->left ();
else sibling = module->traverse ()->right ();
// if we have something now
if (sibling && sibling != selected)
{
deselectNode ();
return selectNode (sibling);
}
}
// if no node is selected, we simply select the first one
else return selectRoot ();
return false;
}
// select the first node in the dialogue
bool GuiGraph::selectRoot ()
{
return selectNode (module->traverse ()->selectRoot (&module->getNodes ()));
}
// deselect a node
void GuiGraph::deselectNode ()
{
// if there is no module assigned to the view, there is nothing to select
if (module == NULL) return;
DlgNode *deselected = module->deselectNode ();
if (deselected)
{
// update the program state
GuiDlgedit::window->setMode (IDLE);
// update the instant preview
GuiDlgedit::window->list ()->clear ();
// redraw the node
deselected->draw (surface, *offset, graph);
}
return;
}
// center the view on given node
bool GuiGraph::centerNode (DlgNode *node)
{
if (module == NULL) return false;
if (node == NULL && module->selected () != NULL)
node = module->selected ();
else return false;
// calculate the correct offset for the given node
DlgPoint pos = node->center ().offset (*offset);
int x, y;
x = drawing_area.width () / 5;
y = drawing_area.height () / 5;
// is node outside the views inner 60% ?
if (!drawing_area.inflate (-x, -y).contains (pos))
{
// then move the view so it is centered on the given point
DlgPoint o (-(pos.x()-drawing_area.width()/2), -(pos.y()-drawing_area.height()/2));
offset->move (o);
draw ();
return true;
}
return false;
}
// center whole dialogue in view
void GuiGraph::center ()
{
if (module == NULL) return;
int min_x, max_x, y;
module->extension (min_x, max_x, y);
int x_off = (graph->allocation.width - (max_x - min_x))/2 - min_x;
int y_off = -y + 20;
offset->move (x_off, y_off);
}
// edit selected node
bool GuiGraph::editNode ()
{
if (module == NULL) return false;
// see if a node is currently selected
DlgNode *selected = module->selected ();
// disable scrolling (just in case)
stopScrolling();
// if we have a sub-dialogue, descent for editing
if (selected->type () == MODULE)
{
switchModule ((DlgModule *) selected);
return true;
}
// if we have a circle, open edit dialog
if (selected && selected->type () != LINK)
{
GtkWindow *parent = GTK_WINDOW (GuiDlgedit::window->getWindow());
GuiCircle edit (parent, &selected->type (), ((DlgCircle *) selected)->entry (), module->entry ());
// editing aborted?
if (!edit.run ()) return false;
// otherwise update everything
GuiDlgedit::window->list ()->display (selected);
selected->draw (surface, *offset, graph);
module->setChanged ();
return true;
}
return false;
}
// set everything up for moving nodes around
bool GuiGraph::prepareDragging (DlgPoint &point)
{
// if there is no module assigned to the view, there is nothing to do
if (module == NULL) return false;
// calculate absolute position of the point
point.move (-offset->x (), -offset->y ());
// see if we're over a node
DlgNode *node = module->getNode (point);
// Not over a node
if (node == NULL) return false;
// if no node selected, select node for dragging
if (module->selected () == NULL) selectNode (node);
// else check whether dragged and selected node are the same
else if (node != module->selected ()) return false;
// Is dragged node circle or arrow?
if (node->type () != LINK)
{
// circles and modules can be dragged directly
mover = node;
// remove any tooltip, as it only gets in the way
if (tooltip)
{
delete tooltip;
tooltip = NULL;
}
}
else
{
// arrows have to be attached to a (invisible) mover
mover = new DlgMover (point);
// try to attach arrow to mover
if (!((DlgMover *) mover)->attach ((DlgArrow *) node))
{
// moving of arrow failed, so clean up
delete mover;
mover = NULL;
}
}
// if we have a mover, update program state
if (mover != NULL)
{
GuiDlgedit::window->setMode (NODE_DRAGGED);
module->setState (NODE_DRAGGED);
return true;
}
return false;
}
// drag a node around
void GuiGraph::drag (DlgPoint &point)
{
static int redraw = 0;
// if there is no module assigned to the view, there is nothing to do
if (module == NULL) return;
// calculate absolute position of the point
point.move (-offset->x (), -offset->y ());
// move node
mover->setPos (point);
// update arrows
for (DlgNode *a = mover->prev (FIRST); a != NULL; a = mover->prev (NEXT))
((DlgArrow *) a)->initShape ();
for (DlgNode *a = mover->next (FIRST); a != NULL; a = mover->next (NEXT))
((DlgArrow *) a)->initShape ();
// update view
switch (redraw)
{
case 0:
{
draw ();
redraw++;
break;
}
case 7:
{
redraw = 0;
break;
}
default:
{
redraw++;
}
}
}
// stop dragging node
void GuiGraph::stopDragging (DlgPoint &point)
{
// if there is no module assigned to the view, there is nothing to do
if (module == NULL || mover == NULL) return;
// calculate absolute position of the point
point.move (-offset->x (), -offset->y ());
// see whether arrow was dragged
if (mover->type () == MOVER)
{
// see whether we are over a node
DlgNode *node = module->getNode (point);
// drop the mover onto the node
((DlgMover *) mover)->drop (node);
// cleanup
delete mover;
}
// if circle moved, realign it to the grid
else
{
// make sure we drop on an empty location
DlgNode *node = module->getNode (point);
while (node != NULL && node->type() != LINK)
{
point.move (0, 2*CIRCLE_DIAMETER);
node = module->getNode (point);
}
mover->setPos (DlgPoint (point.x () - (point.x () % CIRCLE_DIAMETER),
point.y () - (point.y () % CIRCLE_DIAMETER)));
// also need to update arrows and reorder children and parents
for (DlgNode *a = mover->prev (FIRST); a != NULL; a = mover->prev (NEXT))
{
a->prev (FIRST)->removeNext (a);
a->prev (FIRST)->addNext (a);
((DlgArrow *) a)->initShape ();
}
for (DlgNode *a = mover->next (FIRST); a != NULL; a = mover->next (NEXT))
{
a->next (FIRST)->removePrev (a);
a->next (FIRST)->addPrev (a);
((DlgArrow *) a)->initShape ();
}
}
// update everything
if (mover->type() == MODULE)
deselectNode ();
else
{
GuiDlgedit::window->list ()->display (module->selected ());
GuiDlgedit::window->setMode (NODE_SELECTED);
module->setState (NODE_SELECTED);
module->setChanged ();
}
// clear mover
mover = NULL;
draw ();
}
// resize the drawing area
void GuiGraph::resizeSurface (GtkWidget *widget)
{
// delete the old surface
if (surface) gdk_pixmap_unref (surface);
// create a new one with the proper size
surface = gdk_pixmap_new (widget->window, widget->allocation.width,
widget->allocation.height, -1);
// init the surface
if (GuiResources::getColor (GC_GREY)) clear ();
// set the size of the attached dialogues
drawing_area.resize (widget->allocation.width, widget->allocation.height);
}
// empty the drawing area
void GuiGraph::clear ()
{
GdkRectangle t;
gdk_draw_rectangle (surface, GuiResources::getColor (GC_GREY),
TRUE, 0, 0, graph->allocation.width, graph->allocation.height);
t.x = 0;
t.y = 0;
t.width = graph->allocation.width;
t.height = graph->allocation.height;
gtk_widget_draw (graph, &t);
}
// draw the graph to the surface
void GuiGraph::draw ()
{
// nothing to draw
if (module == NULL) return;
GdkRectangle t;
std::vector<DlgNode*>::reverse_iterator i;
std::vector<DlgNode*> nodes = module->getNodes ();
// get visible part of graph
t.x = -offset->x ();
t.y = -offset->y ();
t.width = drawing_area.width ();
t.height = drawing_area.height ();
DlgRect rect (t);
// Clear graph
gdk_draw_rectangle (surface, GuiResources::getColor (GC_WHITE), TRUE, 0, 0, t.width, t.height);
// normalize rect
t.x = 0;
t.y = 0;
// check for each node, wether it is visible
for (i = nodes.rbegin (); i != nodes.rend (); i++)
// draw nodes and arrows
if ((*i)->contains (rect))
(*i)->draw (surface, *offset, NULL);
// draw backing image to screen
gtk_widget_draw (graph, &t);
}
// the mouse has been moved
void GuiGraph::mouseMoved (DlgPoint &point)
{
// if there is no module assigned to the view, there is nothing to do
if (module == NULL) return;
// calculate absolute position of the point
point.move (-offset->x (), -offset->y ());
// see if we're over a node
DlgNode *node = module->getNode (point);
// get the node that was highlighted before (if any)
DlgNode *prev = module->highlightNode (node);
// there's no need for action unless old and new highlight differs
if (prev != node)
{
// clear old if necessary
if (prev != NULL)
{
prev->draw (surface, *offset, graph);
if (tooltip)
{
delete tooltip;
tooltip = NULL;
}
}
// then highlight the new one
if (node != NULL)
{
node->draw (surface, *offset, graph, NODE_HILIGHTED);
tooltip = new GuiTooltip (node);
tooltip->draw (graph, *offset);
}
}
return;
}
// is scrolling allowed?
bool GuiGraph::scrollingAllowed () const
{
// if there is no module assigned to the view or no nodes
// in the module yet, there is nothing to do
if (module == NULL || module->getNodes ().empty ()) return false;
return true;
}
// the actual scrolling
void GuiGraph::scroll ()
{
offset->move (scroll_offset.x, scroll_offset.y);
draw ();
}
// get the toplevel dialogue module
DlgModule *GuiGraph::dialogue ()
{
// if there is no module assigned to the view, there is nothing to do
if (module == NULL) return NULL;
return module->toplevel ();
}