]> git.tdb.fi Git - libs/gui.git/blob - source/input/touchscreen.cpp
95fd272200b03802e43d517f01af99ca1ec49831
[libs/gui.git] / source / input / touchscreen.cpp
1 #include "touchscreen.h"
2
3 using namespace std;
4
5 namespace Msp {
6 namespace Input {
7
8 Touchscreen::Touchscreen(Graphics::Window &w):
9         window(w)
10 {
11         name = "Touchscreen";
12
13         window.set_touch_input(true);
14         window.signal_input_event.connect(sigc::mem_fun(this, &Touchscreen::input_event));
15 }
16
17 Touchscreen::~Touchscreen()
18 {
19         window.set_touch_input(false);
20 }
21
22 string Touchscreen::get_button_name(unsigned btn) const
23 {
24         if(btn==0)
25                 return "Primary touch";
26         else
27                 return Device::get_button_name(btn);
28 }
29
30 string Touchscreen::get_axis_name(unsigned axis) const
31 {
32         if(axis==0)
33                 return "Primary touch X";
34         else if(axis==1)
35                 return "Primary touch Y";
36         else
37                 return Device::get_axis_name(axis);
38 }
39
40 unsigned Touchscreen::map_point_id(unsigned id)
41 {
42         unsigned unused = active_points.size();
43         for(unsigned i=0; i<active_points.size(); ++i)
44         {
45                 if(active_points[i]==id)
46                         return i;
47                 else if(active_points[i]==UNUSED && i<unused)
48                         unused = i;
49         }
50
51         if(unused<active_points.size())
52                 active_points[unused] = id;
53         else
54                 active_points.push_back(id);
55
56         return unused;
57 }
58
59 void Touchscreen::touch_down(unsigned id)
60 {
61         unsigned i = map_point_id(id);
62         set_button_state(i, true, true);
63 }
64
65 void Touchscreen::touch_move(unsigned id, float x, float y)
66 {
67         unsigned i = map_point_id(id);
68         set_axis_value(i*2, x, true);
69         set_axis_value(i*2+1, y, true);
70 }
71
72 void Touchscreen::touch_up(unsigned id)
73 {
74         unsigned i = map_point_id(id);
75         set_button_state(i, true, false);
76         active_points[i] = UNUSED;
77 }
78
79 } // namespace Input
80 } // namespace Msp