forked from opengazer/OpenGazer
-
Notifications
You must be signed in to change notification settings - Fork 3
/
GazeArea.cpp
86 lines (78 loc) · 2.35 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
#include "GazeArea.h"
#include "OutputMethods.h"
#include <opencv/cv.h>
GazeArea::GazeArea(int argc, char **argv,
const vector<shared_ptr<AbstractStore> > &stores):
lastPointId(-1), gazetracker(argc, argv, stores)
{
set_size_request(gazetracker.canvas->width, gazetracker.canvas->height);
Glib::signal_idle().connect(sigc::mem_fun(*this, &GazeArea::on_idle));
add_events(Gdk::BUTTON_PRESS_MASK);
add_events(Gdk::BUTTON_RELEASE_MASK);
}
GazeArea::~GazeArea(void) {}
bool GazeArea::on_idle() {
gazetracker.doprocessing();
queue_draw();
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 IplImage *image = gazetracker.canvas.get();
Glib::RefPtr<Gdk::Pixbuf> pixbuf =
Gdk::Pixbuf::create_from_data((guint8*) image->imageData,
Gdk::COLORSPACE_RGB,
false,
image->depth,
image->width,
image->height,
image->widthStep);
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) {
Point point(event->x, event->y);
PointTracker &tracker = gazetracker.tracking->tracker;
int closest = tracker.getClosestTracker(point);
if (closest >= 0 &&
point.distance(tracker.currentpoints[closest]) <= 10)
lastPointId = closest;
else
lastPointId = -1;
}
return true;
}
else
return false;
}
bool GazeArea::on_button_release_event(GdkEventButton *event) {
if (event->button == 1) {
PointTracker &tracker = gazetracker.tracking->tracker;
Point point(event->x, event->y);
if (lastPointId >= 0)
switch(clickCount) {
case 1: tracker.updatetracker(lastPointId, point); break;
case 2: tracker.removetracker(lastPointId); break;
}
else
tracker.addtracker(point);
return true;
}
else
return false;
}