f989ea6f64a543418e2ab78697fa3f27feb7ae8d
[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
25 namespace lisp {
26 class Writer;
27 class Lisp;
28 }
29
30 #include <SDL.h>
31
32 #include <string>
33 #include <map>
34 #include <vector>
35
36 class Menu;
37
38 class JoystickKeyboardController : public Controller
39 {
40 public:
41   JoystickKeyboardController();
42   virtual ~JoystickKeyboardController();
43
44   /** Process an SDL Event and return true if the event has been used
45    */
46   void process_event(const SDL_Event& event);
47
48   void write(lisp::Writer& writer);
49   void read(const lisp::Lisp& lisp);
50   void reset();
51
52   Menu* get_key_options_menu();
53   Menu* get_joystick_options_menu();
54   void updateAvailableJoysticks();
55
56 private:
57   void process_key_event(const SDL_Event& event);
58   void process_hat_event(const SDL_JoyHatEvent& jhat);
59   void process_axis_event(const SDL_JoyAxisEvent& jaxis);
60   void process_button_event(const SDL_JoyButtonEvent& jbutton);
61   void process_console_key_event(const SDL_Event& event);
62   void process_menu_key_event(const SDL_Event& event);
63
64   void print_joystick_mappings();
65
66   typedef std::map<SDLKey, Control> KeyMap;
67   KeyMap keymap;
68
69   typedef std::map<int, Control> ButtonMap;
70   ButtonMap joy_button_map;
71
72   typedef std::map<int, Control> AxisMap;
73   AxisMap joy_axis_map;
74
75   typedef std::map<int, Control> HatMap;
76   HatMap joy_hat_map;
77
78   std::vector<SDL_Joystick*> joysticks;
79
80   std::string name;
81
82   int dead_zone;
83   /// the number of buttons all joysticks have
84   int min_joybuttons;
85   /// the max number of buttons a joystick has
86   int max_joybuttons;
87
88   int max_joyaxis;
89
90   int max_joyhats;
91
92   Uint8 hat_state;
93
94   bool jump_with_up_joy; // Joystick up jumps
95   bool jump_with_up_kbd; // Keyboard up jumps
96
97   SDLKey reversemap_key(Control c);
98   int    reversemap_joybutton(Control c);
99   int    reversemap_joyaxis(Control c);
100   int    reversemap_joyhat(Control c);
101
102   void unbind_joystick_control(Control c);
103
104   void bind_joybutton(int button, Control c);
105   void bind_joyaxis(int axis, Control c);
106   void bind_joyhat(int dir, Control c);
107   void bind_key(SDLKey key, Control c);
108
109   void set_joy_controls(Control id, bool value);
110
111   int wait_for_key;
112   int wait_for_joystick;
113
114   class KeyboardMenu;
115   class JoystickMenu;
116
117   KeyboardMenu* key_options_menu;
118   JoystickMenu* joystick_options_menu;
119   friend class KeyboardMenu;
120   friend class JoystickMenu;
121 };
122
123 #endif