]> git.tdb.fi Git - libs/gui.git/blob - source/input/android/mouse.cpp
Add touchscreen support on Android
[libs/gui.git] / source / input / android / mouse.cpp
1 #include <msp/graphics/window_private.h>
2 #include "mouse.h"
3
4 namespace Msp {
5 namespace Input {
6
7 void Mouse::input_event(const Graphics::Window::Event &event)
8 {
9         int type = AInputEvent_getType(event.aevent);
10         if(type!=AINPUT_EVENT_TYPE_MOTION)
11                 return;
12
13         int source = AInputEvent_getSource(event.aevent);
14         if(window.get_touch_input() && source!=AINPUT_SOURCE_MOUSE && source!=AINPUT_SOURCE_TOUCHPAD)
15                 return;
16
17         /* Emulate a mouse with the touch events of a single finger.  If more
18         fingers appear while the first one is held down, they are ignored, even if
19         the first finger is released. */
20
21         int action = AMotionEvent_getAction(event.aevent);
22         int action_pointer = (action&AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)>>AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
23         action &= AMOTION_EVENT_ACTION_MASK;
24
25         int pointer_count = AMotionEvent_getPointerCount(event.aevent);
26         int pointer_zero = -1;
27         for(int i=0; (i<pointer_count && pointer_zero<0); ++i)
28                 if(AMotionEvent_getPointerId(event.aevent, i)==0)
29                         pointer_zero = i;
30
31         if(pointer_zero>=0)
32         {
33                 float x = AMotionEvent_getX(event.aevent, pointer_zero);
34                 float y = AMotionEvent_getY(event.aevent, pointer_zero);
35                 set_axis_value(0, x*2/window.get_width()-1, true);
36                 set_axis_value(1, 1-y*2/window.get_height(), true);
37         }
38
39         switch(action)
40         {
41         case AMOTION_EVENT_ACTION_DOWN:
42         case AMOTION_EVENT_ACTION_UP:
43         case AMOTION_EVENT_ACTION_POINTER_DOWN:
44         case AMOTION_EVENT_ACTION_POINTER_UP:
45                 if(action_pointer==0)
46                         set_button_state(1, action==AMOTION_EVENT_ACTION_DOWN, true);
47                 break;
48         default:;
49         }
50 }
51
52 } // namespace Input
53 } // namespace Msp