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