initial commit, lordsawar source, slightly modified
[lordsawar] / src / gui / input-helpers.h
1 //  Copyright (C) 2007 Ole Laursen
2 //
3 //  This program is free software; you can redistribute it and/or modify
4 //  it under the terms of the GNU General Public License as published by
5 //  the Free Software Foundation; either version 3 of the License, or
6 //  (at your option) any later version.
7 //
8 //  This program is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 //  GNU Library General Public License for more details.
12 //
13 //  You should have received a copy of the GNU General Public License
14 //  along with this program; if not, write to the Free Software
15 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
16 //  02110-1301, USA.
17
18 #ifndef GUI_INPUT_HELPERS_H
19 #define GUI_INPUT_HELPERS_H
20
21 #include "input-events.h"
22
23 inline MouseButtonEvent to_input_event(GdkEventButton *e)
24 {
25     MouseButtonEvent m;
26     m.pos = make_vector(int(e->x), int(e->y));
27     
28     if (e->button == 1)
29         m.button = MouseButtonEvent::LEFT_BUTTON;
30     else if (e->button == 3)
31         m.button = MouseButtonEvent::RIGHT_BUTTON;
32     else if (e->button == 4)
33         m.button = MouseButtonEvent::WHEEL_UP;
34     else if (e->button == 5)
35         m.button = MouseButtonEvent::WHEEL_DOWN;
36     else
37         m.button = MouseButtonEvent::MIDDLE_BUTTON;
38     
39     if (e->type == GDK_BUTTON_PRESS)
40         m.state = MouseButtonEvent::PRESSED;
41     else if (e->type == GDK_BUTTON_RELEASE)
42         m.state = MouseButtonEvent::RELEASED;
43     
44     return m;
45 }
46
47 inline MouseMotionEvent to_input_event(GdkEventMotion *e)
48 {
49     MouseMotionEvent m;
50     m.pos = make_vector(int(e->x), int(e->y));
51     
52     m.pressed[MouseMotionEvent::LEFT_BUTTON] = e->state & GDK_BUTTON1_MASK;
53     m.pressed[MouseMotionEvent::MIDDLE_BUTTON] = e->state & GDK_BUTTON2_MASK;
54     m.pressed[MouseMotionEvent::RIGHT_BUTTON] = e->state & GDK_BUTTON3_MASK;
55             
56     return m;
57 }
58
59 #endif