1 /******************************************************************************
2  *  Warmux is a convivial mass murder game.
3  *  Copyright (C) 2001-2011 Warmux Team.
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18  ******************************************************************************
19  * Virtual class to handle man/machine interaction
20  *****************************************************************************/
21 
22 #ifndef MAN_MACHINE_INTERFACE_H
23 #define MAN_MACHINE_INTERFACE_H
24 //-----------------------------------------------------------------------------
25 #include <map>
26 #include <list>
27 #include <vector>
28 #include <WARMUX_base.h>
29 //-----------------------------------------------------------------------------
30 
31 // Forward declarations
32 #ifndef _SDL_events_h
33 union SDL_Event;
34 #endif
35 
36 class ManMachineInterface
37 {
38 public:
39   typedef enum
40   {
41     KEY_QUIT,
42     KEY_WEAPONS1,
43     KEY_WEAPONS2,
44     KEY_WEAPONS3,
45     KEY_WEAPONS4,
46     KEY_WEAPONS5,
47     KEY_WEAPONS6,
48     KEY_WEAPONS7,
49     KEY_WEAPONS8,
50     KEY_PAUSE,
51     KEY_FULLSCREEN,
52     KEY_TOGGLE_INTERFACE,
53     KEY_CENTER,
54     KEY_TOGGLE_WEAPONS_MENUS,
55     KEY_CHAT,
56     KEY_MOVE_LEFT,
57     KEY_MOVE_LEFT_SLOWLY,
58     KEY_MOVE_CAMERA_LEFT,
59     KEY_MOVE_RIGHT,
60     KEY_MOVE_RIGHT_SLOWLY,
61     KEY_MOVE_CAMERA_RIGHT,
62     KEY_UP,
63     KEY_UP_SLOWLY,
64     KEY_MOVE_CAMERA_UP,
65     KEY_DOWN,
66     KEY_DOWN_SLOWLY,
67     KEY_MOVE_CAMERA_DOWN,
68     KEY_JUMP,
69     KEY_HIGH_JUMP,
70     KEY_BACK_JUMP,
71     KEY_SHOOT,
72     KEY_CHANGE_WEAPON,
73     KEY_WEAPON_1,
74     KEY_WEAPON_2,
75     KEY_WEAPON_3,
76     KEY_WEAPON_4,
77     KEY_WEAPON_5,
78     KEY_WEAPON_6,
79     KEY_WEAPON_7,
80     KEY_WEAPON_8,
81     KEY_WEAPON_9,
82     KEY_WEAPON_LESS,
83     KEY_WEAPON_MORE,
84     KEY_NEXT_CHARACTER,
85     KEY_MENU_OPTIONS_FROM_GAME,
86     KEY_MINIMAP_FROM_GAME,
87     KEY_HELP,
88     KEY_DECREASE_MINIMAP,
89     KEY_INCREASE_MINIMAP,
90     KEY_DECREASE_VOLUME,
91     KEY_INCREASE_VOLUME,
92     KEY_SCREENSHOT,
93     KEY_NONE
94   } Key_t;
95 
96 protected:
97   // This is a widget in charge of displaying and setting the config
98   friend class ControlItem;
99   friend class ControlConfig;
100 
101   typedef enum
102   {
103     KEY_PRESSED,
104     KEY_RELEASED,
105     KEY_REFRESH,
106     X_AXIS_MOTION,
107     Y_AXIS_MOTION
108   } Key_Event_t;
109 
SetDefaultConfig()110   virtual void SetDefaultConfig() { };
111   std::map<int, std::vector<Key_t> > layout;
112   std::list<uint32_t> registred_event;
113   bool PressedKeys[256]; // stupid default value
114 
RegisterEvent(uint32_t event_type)115   void RegisterEvent(uint32_t event_type) { registred_event.push_back(event_type); };
116   bool IsRegistredEvent(uint32_t event_type);
117   void HandleKeyPressed(const Key_t &action_key);
118   void HandleKeyReleased(const Key_t &action_key);
119 
SetKeyAction(int key,Key_t at)120   void SetKeyAction(int key, Key_t at) { layout[key].push_back(at); };
121   void ClearKeyAction(Key_t at);
ClearKeyBindings()122   void ClearKeyBindings() { layout.clear(); }
123 
124   int GetKeyFromKeyName(const std::string &name) const;
125   std::string GetKeyNameFromKey(int key) const;
126 
127   Key_t GetActionFromActionName(const std::string &name) const;
128   std::string GetActionNameFromAction(Key_t) const;
129   std::string GetHumanReadableActionName(Key_t) const;
130 
ManMachineInterface()131   ManMachineInterface() { SetDefaultConfig(); };
~ManMachineInterface()132   virtual ~ManMachineInterface() { };
133 
134 public:
135   virtual bool HandleKeyEvent(const SDL_Event& evnt) = 0;
136   virtual void Reset();
137 
138   // Get the key associated to an action.
139   int GetKeyAssociatedToAction(Key_t at) const;
140 };
141 
142 //-----------------------------------------------------------------------------
143 #endif /* MAN_MACHINE_INTERFACE_H */
144