forked from tiendan/OpenGazer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GazeArea.cpp
executable file
·106 lines (87 loc) · 2.4 KB
/
GazeArea.cpp
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
#include "GazeArea.h"
GazeArea::GazeArea(int argc, char **argv):
_lastPointId(-1),
gazeTracker(argc, argv)
{
set_size_request(gazeTracker.canvas->size().width, gazeTracker.canvas->size().height);
Glib::signal_idle().connect(sigc::mem_fun(*this, &GazeArea::onIdle));
add_events(Gdk::BUTTON_PRESS_MASK);
add_events(Gdk::BUTTON_RELEASE_MASK);
}
GazeArea::~GazeArea() {}
bool GazeArea::onIdle() {
try {
gazeTracker.process();
queue_draw();
gazeTracker.simulateClicks();
}
catch (Utils::QuitNow) {
gazeTracker.cleanUp();
exit(0);
}
return true;
}
bool GazeArea::on_expose_event(GdkEventExpose *event) {
Glib::RefPtr<Gdk::Window> window = get_window();
if (window) {
Gtk::Allocation allocation = get_allocation();
const int width = allocation.get_width();
const int height = allocation.get_height();
Glib::RefPtr<Gdk::GC> gc = Gdk::GC::create(window);
const cv::Mat *image = gazeTracker.canvas.get();
Glib::RefPtr<Gdk::Pixbuf> pixbuf = Gdk::Pixbuf::create_from_data((guint8*) image->data, Gdk::COLORSPACE_RGB, false, 8, image->size().width, image->size().height, (int) image->step[0]);
window->draw_pixbuf(gc, pixbuf, 0, 0, 0, 0, width, height, Gdk::RGB_DITHER_NONE, 0, 0);
}
return true;
}
bool GazeArea::on_button_press_event(GdkEventButton *event) {
if (event->button == 1) {
switch(event->type) {
case GDK_BUTTON_PRESS:
_clickCount = 1;
break;
case GDK_2BUTTON_PRESS:
_clickCount = 2;
break;
case GDK_3BUTTON_PRESS:
_clickCount = 3;
break;
default:
break;
}
if (event->type == GDK_BUTTON_PRESS) {
PointTracker &pointTracker = gazeTracker.trackingSystem->pointTracker;
Point point(event->x, event->y);
int closest = pointTracker.getClosestTracker(point);
if (closest >= 0 && point.distance2f(pointTracker.currentPoints[closest]) <= 25) {
_lastPointId = closest;
} else {
_lastPointId = -1;
}
}
return true;
}
return false;
}
bool GazeArea::on_button_release_event(GdkEventButton *event) {
if (event->button == 1) {
PointTracker &pointTracker = gazeTracker.trackingSystem->pointTracker;
Point point(event->x, event->y);
if (_lastPointId >= 0) {
switch(_clickCount) {
case 1:
pointTracker.updateTracker(_lastPointId, point);
break;
case 2:
pointTracker.removeTracker(_lastPointId);
break;
default:
break;
}
} else {
pointTracker.addTracker(point);
}
return true;
}
return false;
}