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