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