Edges are clickable now.
[av.git] / gtk / src / edge.cpp
blobcb34218608fa66217af1a38fa9a3f09226813a1d
1 #include <cmath>
3 #include "edge.hpp"
5 Edge::Edge(Vertex &source, Vertex &target) :
6 Drawable<Edge *>(),
7 _source(&source),
8 _target(&target)
13 Edge::~Edge(void)
18 void Edge::draw(Cairo::RefPtr<Cairo::Context> cr)
20 gdouble start_x, start_y;
21 gdouble end_x, end_y;
23 if (cr) {
24 _source->get_bottom_anchor(start_x, start_y);
25 _target->get_top_anchor(end_x, end_y);
27 cr->save();
28 cr->begin_new_path();
29 cr->move_to(start_x, start_y);
30 cr->line_to(end_x, end_y);
31 cr->set_line_width(2.0);
32 cr->set_line_cap(Cairo::LINE_CAP_ROUND);
33 cr->stroke();
34 cr->restore();
38 bool Edge::select(Cairo::RefPtr<Cairo::Context> cr, gdouble x, gdouble y)
40 if (!_is_in_region(cr, x, y)) {
41 return false;
44 // If y-coordinate is positive, it means that it is closer to the target
45 // sentence
46 if (y > 0) {
47 change_target_follow_mouse();
48 } else {
49 change_source_follow_mouse();
52 return true;
55 void Edge::deselect(void)
60 void Edge::change_source(Vertex &source)
65 void Edge::change_target(Vertex &target)
70 void Edge::change_source_follow_mouse(void)
75 void Edge::change_target_follow_mouse(void)
80 bool
81 Edge::_is_in_region(Cairo::RefPtr<Cairo::Context> cr, gdouble x, gdouble y) const
83 gdouble start_x, start_y;
84 gdouble end_x, end_y;
85 gdouble a, b, distance;
87 _source->get_bottom_anchor(start_x, start_y);
88 _target->get_top_anchor(end_x, end_y);
90 // Discards the point if its higher than the source anchor or lower than
91 // the target anchor
92 if (y < start_y || y > end_y) {
93 return false;
96 // The point has to be within 5 units away from the line segment
97 a = start_x - end_x;
98 b = start_y - end_y;
99 distance = (fabs(a * (end_y - y) - (end_x - x) * b) /
100 sqrt(pow(a, 2) + pow(b, 2)));
101 if (distance < 5.0) {
102 return true;
105 return false;