]> git.tdb.fi Git - libs/gui.git/blob - source/input/android/mouse.cpp
b16b6be2331077f48e57ddc2ee4deadde932ea01
[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         /* Emulate a mouse with the touch events of a single finger.  If more
14         fingers appear while the first one is held down, they are ignored, even if
15         the first finger is released. */
16
17         int action = AMotionEvent_getAction(event.aevent);
18         int action_pointer = (action&AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)>>AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
19         action &= AMOTION_EVENT_ACTION_MASK;
20
21         int pointer_count = AMotionEvent_getPointerCount(event.aevent);
22         int pointer_zero = -1;
23         for(int i=0; (i<pointer_count && pointer_zero<0); ++i)
24                 if(AMotionEvent_getPointerId(event.aevent, i)==0)
25                         pointer_zero = i;
26
27         if(pointer_zero>=0)
28         {
29                 float x = AMotionEvent_getX(event.aevent, pointer_zero);
30                 float y = AMotionEvent_getY(event.aevent, pointer_zero);
31                 set_axis_value(0, x*2/window.get_width()-1, true);
32                 set_axis_value(1, 1-y*2/window.get_height(), true);
33         }
34
35         switch(action)
36         {
37         case AMOTION_EVENT_ACTION_DOWN:
38         case AMOTION_EVENT_ACTION_UP:
39         case AMOTION_EVENT_ACTION_POINTER_DOWN:
40         case AMOTION_EVENT_ACTION_POINTER_UP:
41                 if(action_pointer==0)
42                         set_button_state(1, action==AMOTION_EVENT_ACTION_DOWN, true);
43                 break;
44         default:;
45         }
46 }
47
48 } // namespace Input
49 } // namespace Msp