9b6a39fe7cad236fe3241f5059c3ead5632f753f
[supertux.git] / src / control / joystickkeyboardcontroller.hpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #ifndef __JOYSTICKKEYBOARDCONTROLLER_H__
21 #define __JOYSTICKKEYBOARDCONTROLLER_H__
22
23 #include "controller.hpp"
24 #include "lisp/lisp.hpp"
25 #include "lisp/writer.hpp"
26 #include <SDL.h>
27 #include <string>
28 #include <map>
29
30 class Menu;
31
32 class JoystickKeyboardController : public Controller
33 {
34 public:
35   JoystickKeyboardController();
36   virtual ~JoystickKeyboardController();
37
38   /** Process an SDL Event and return true if the event has been used
39    */
40   void process_event(const SDL_Event& event);
41
42   void write(lisp::Writer& writer);
43   void read(const lisp::Lisp& lisp);
44   void reset();
45
46   Menu* get_key_options_menu();
47   Menu* get_joystick_options_menu();
48
49 private:
50   void process_key_event(const SDL_Event& event);
51   void process_hat_event(const SDL_JoyHatEvent& jhat);
52   void process_axis_event(const SDL_JoyAxisEvent& jaxis);
53   void process_button_event(const SDL_JoyButtonEvent& jbutton);
54   void process_console_key_event(const SDL_Event& event);
55   void process_menu_key_event(const SDL_Event& event);
56
57   void print_joystick_mappings();
58
59   typedef std::map<SDLKey, Control> KeyMap;
60   KeyMap keymap;
61
62   typedef std::map<int, Control> ButtonMap;
63   ButtonMap joy_button_map;
64
65   typedef std::map<int, Control> AxisMap;
66   AxisMap joy_axis_map;
67
68   typedef std::map<int, Control> HatMap;
69   HatMap joy_hat_map;
70
71   std::vector<SDL_Joystick*> joysticks;
72
73   std::string name;
74
75   int dead_zone;
76   /// the number of buttons all joysticks have
77   int min_joybuttons;
78   /// the max number of buttons a joystick has
79   int max_joybuttons;
80
81   int max_joyaxis;
82
83   int max_joyhats;
84
85   Uint8 hat_state;
86
87   bool jump_with_up_joy; // Joystick up jumps
88   bool jump_with_up_kbd; // Keyboard up jumps
89
90   SDLKey reversemap_key(Control c);
91   int    reversemap_joybutton(Control c);
92   int    reversemap_joyaxis(Control c);
93   int    reversemap_joyhat(Control c);
94
95   void unbind_joystick_control(Control c);
96
97   void bind_joybutton(int button, Control c);
98   void bind_joyaxis(int axis, Control c);
99   void bind_joyhat(int dir, Control c);
100   void bind_key(SDLKey key, Control c);
101
102   void set_joy_controls(Control id, bool value);
103
104   int wait_for_key;
105   int wait_for_joystick;
106
107   class KeyboardMenu;
108   class JoystickMenu;
109
110   KeyboardMenu* key_options_menu;
111   JoystickMenu* joystick_options_menu;
112   friend class KeyboardMenu;
113   friend class JoystickMenu;
114 };
115
116 #endif