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