1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Patrick Boenzli |
---|
13 | co-programmer: Christian Meyer |
---|
14 | |
---|
15 | This code was inspired by the command_node.cc code from Christian Meyer in revision |
---|
16 | 4386 and earlier. |
---|
17 | */ |
---|
18 | |
---|
19 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_EVENT |
---|
20 | |
---|
21 | #include "key_mapper.h" |
---|
22 | |
---|
23 | #include "event_def.h" |
---|
24 | |
---|
25 | #include "globals.h" |
---|
26 | #include "parser/ini_parser/ini_parser.h" |
---|
27 | #include "key_names.h" |
---|
28 | #include "debug.h" |
---|
29 | |
---|
30 | |
---|
31 | using namespace std; |
---|
32 | |
---|
33 | |
---|
34 | /* initialize all variables to a reasonable value*/ |
---|
35 | int KeyMapper::PEV_FORWARD = EV_UNKNOWN; |
---|
36 | int KeyMapper::PEV_BACKWARD = EV_UNKNOWN; |
---|
37 | int KeyMapper::PEV_LEFT = EV_UNKNOWN; |
---|
38 | int KeyMapper::PEV_RIGHT = EV_UNKNOWN; |
---|
39 | int KeyMapper::PEV_UP = EV_UNKNOWN; |
---|
40 | int KeyMapper::PEV_DOWN = EV_UNKNOWN; |
---|
41 | int KeyMapper::PEV_ROLL_LEFT = EV_UNKNOWN; |
---|
42 | int KeyMapper::PEV_ROLL_RIGHT = EV_UNKNOWN; |
---|
43 | int KeyMapper::PEV_STRAFE_LEFT = EV_UNKNOWN; |
---|
44 | int KeyMapper::PEV_STRAFE_RIGHT = EV_UNKNOWN; |
---|
45 | |
---|
46 | int KeyMapper::PEV_FIRE1 = EV_UNKNOWN; |
---|
47 | int KeyMapper::PEV_FIRE2 = EV_UNKNOWN; |
---|
48 | int KeyMapper::PEV_PREVIOUS_WEAPON = EV_UNKNOWN; |
---|
49 | int KeyMapper::PEV_NEXT_WEAPON = EV_UNKNOWN; |
---|
50 | |
---|
51 | int KeyMapper::PEV_CHANGE_SHIP = EV_UNKNOWN; |
---|
52 | |
---|
53 | int KeyMapper::PEV_VIEW0 = EV_UNKNOWN; |
---|
54 | int KeyMapper::PEV_VIEW1 = EV_UNKNOWN; |
---|
55 | int KeyMapper::PEV_VIEW2 = EV_UNKNOWN; |
---|
56 | int KeyMapper::PEV_VIEW3 = EV_UNKNOWN; |
---|
57 | int KeyMapper::PEV_VIEW4 = EV_UNKNOWN; |
---|
58 | int KeyMapper::PEV_VIEW5 = EV_UNKNOWN; |
---|
59 | |
---|
60 | int KeyMapper::PEV_NEXT_WORLD = EV_UNKNOWN; |
---|
61 | int KeyMapper::PEV_PREVIOUS_WORLD = EV_UNKNOWN; |
---|
62 | |
---|
63 | int KeyMapper::PEV_PAUSE = EV_UNKNOWN; |
---|
64 | int KeyMapper::PEV_QUIT = EV_UNKNOWN; |
---|
65 | |
---|
66 | |
---|
67 | |
---|
68 | //! this is the mapping array from names to ids: enter all orxonox.conf keys here |
---|
69 | /** @todo use globals.h for this.... everything is done there for those Options, |
---|
70 | * and you do not have to care about The namings, as they might change |
---|
71 | */ |
---|
72 | orxKeyMapping map[] = { |
---|
73 | {&KeyMapper::PEV_FORWARD, CONFIG_NAME_PLAYER_FORWARD}, |
---|
74 | {&KeyMapper::PEV_BACKWARD, CONFIG_NAME_PLAYER_BACKWARD}, |
---|
75 | {&KeyMapper::PEV_UP, CONFIG_NAME_PLAYER_UP}, |
---|
76 | {&KeyMapper::PEV_DOWN, CONFIG_NAME_PLAYER_DOWN}, |
---|
77 | {&KeyMapper::PEV_LEFT, CONFIG_NAME_PLAYER_LEFT}, |
---|
78 | {&KeyMapper::PEV_RIGHT, CONFIG_NAME_PLAYER_RIGHT}, |
---|
79 | {&KeyMapper::PEV_ROLL_LEFT, CONFIG_NAME_PLAYER_ROLL_RIGHT}, |
---|
80 | {&KeyMapper::PEV_ROLL_RIGHT, CONFIG_NAME_PLAYER_ROLL_LEFT}, |
---|
81 | {&KeyMapper::PEV_STRAFE_LEFT, "StrafeLeft"}, |
---|
82 | {&KeyMapper::PEV_STRAFE_RIGHT, "StrafeRight"}, |
---|
83 | |
---|
84 | {&KeyMapper::PEV_FIRE1, CONFIG_NAME_PLAYER_FIRE}, |
---|
85 | {&KeyMapper::PEV_FIRE1, "Fire1"}, |
---|
86 | {&KeyMapper::PEV_FIRE2, "Fire2"}, |
---|
87 | {&KeyMapper::PEV_NEXT_WEAPON, CONFIG_NAME_PLAYER_NEXT_WEAPON}, |
---|
88 | {&KeyMapper::PEV_PREVIOUS_WEAPON, CONFIG_NAME_PLAYER_PREV_WEAPON}, |
---|
89 | |
---|
90 | {&KeyMapper::PEV_CHANGE_SHIP, CONFIG_NAME_PLAYER_CHANGE_SHIP}, |
---|
91 | |
---|
92 | |
---|
93 | {&KeyMapper::PEV_VIEW0, CONFIG_NAME_VIEW0}, |
---|
94 | {&KeyMapper::PEV_VIEW1, CONFIG_NAME_VIEW1}, |
---|
95 | {&KeyMapper::PEV_VIEW2, CONFIG_NAME_VIEW2}, |
---|
96 | {&KeyMapper::PEV_VIEW3, CONFIG_NAME_VIEW3}, |
---|
97 | {&KeyMapper::PEV_VIEW4, CONFIG_NAME_VIEW4}, |
---|
98 | {&KeyMapper::PEV_VIEW5, CONFIG_NAME_VIEW5}, |
---|
99 | |
---|
100 | {&KeyMapper::PEV_NEXT_WORLD, CONFIG_NAME_NEXT_WORLD}, |
---|
101 | {&KeyMapper::PEV_PREVIOUS_WORLD, CONFIG_NAME_PREV_WORLD}, |
---|
102 | |
---|
103 | {&KeyMapper::PEV_PAUSE, CONFIG_NAME_PAUSE}, |
---|
104 | {&KeyMapper::PEV_QUIT, CONFIG_NAME_QUIT}, |
---|
105 | {NULL, NULL} |
---|
106 | }; |
---|
107 | |
---|
108 | |
---|
109 | |
---|
110 | /** |
---|
111 | * standard constructor |
---|
112 | */ |
---|
113 | KeyMapper::KeyMapper () |
---|
114 | { |
---|
115 | this->setClassID(CL_KEY_MAPPER, "KeyMapper"); |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | /** |
---|
120 | * standard deconstructor |
---|
121 | */ |
---|
122 | KeyMapper::~KeyMapper () |
---|
123 | { |
---|
124 | } |
---|
125 | |
---|
126 | |
---|
127 | /** |
---|
128 | * loads new key bindings from a file |
---|
129 | * @param filename: The path and name of the file to load the bindings from |
---|
130 | */ |
---|
131 | void KeyMapper::loadKeyBindings (const char* fileName) |
---|
132 | { |
---|
133 | IniParser parser(fileName); |
---|
134 | this->loadKeyBindings(&parser); |
---|
135 | } |
---|
136 | |
---|
137 | void KeyMapper::loadKeyBindings(IniParser* iniParser) |
---|
138 | { |
---|
139 | if( !iniParser->getSection (CONFIG_SECTION_PLAYER "1")) |
---|
140 | { |
---|
141 | PRINTF(1)("Could not find key bindings " CONFIG_SECTION_PLAYER"1\n"); |
---|
142 | return; |
---|
143 | } |
---|
144 | int* index; |
---|
145 | |
---|
146 | iniParser->firstVar(); |
---|
147 | while(iniParser->getCurrentName()) |
---|
148 | { |
---|
149 | PRINTF(3)("Keys: Parsing %s, %s now.\n", iniParser->getCurrentName(), iniParser->getCurrentValue()); |
---|
150 | // map the name to an sdl index |
---|
151 | index = nameToIndex (iniParser->getCurrentValue()); |
---|
152 | // map the index to a internal name |
---|
153 | this->mapKeys(iniParser->getCurrentName(), index); |
---|
154 | iniParser->nextVar(); |
---|
155 | } |
---|
156 | |
---|
157 | |
---|
158 | // PARSE MISC SECTION |
---|
159 | if( !iniParser->getSection (CONFIG_SECTION_MISC_KEYS)) |
---|
160 | { |
---|
161 | PRINTF(1)("Could not find key bindings" CONFIG_SECTION_MISC_KEYS "\n"); |
---|
162 | return; |
---|
163 | } |
---|
164 | |
---|
165 | iniParser->firstVar(); |
---|
166 | while(iniParser->getCurrentName()) |
---|
167 | { |
---|
168 | PRINTF(3)("MISC: Parsing %s, %s now.\n", iniParser->getCurrentName(), iniParser->getCurrentValue()); |
---|
169 | index = nameToIndex (iniParser->getCurrentValue()); |
---|
170 | this->mapKeys(iniParser->getCurrentName(), index); |
---|
171 | iniParser->nextVar(); |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|
175 | /** |
---|
176 | * this function looks up name to key index |
---|
177 | * @param the name of the button |
---|
178 | */ |
---|
179 | int* KeyMapper::nameToIndex (const char* name) |
---|
180 | { |
---|
181 | coord[0] = -1; |
---|
182 | coord[1] = -1; |
---|
183 | int c; |
---|
184 | if( (c = keynameToSDLK (name)) != -1) { |
---|
185 | coord[1] = c; |
---|
186 | coord[0] = 0; |
---|
187 | } |
---|
188 | if( (c = buttonnameToSDLB (name)) != -1) { |
---|
189 | coord[1] = c; |
---|
190 | coord[0] = 1; |
---|
191 | } |
---|
192 | return coord; |
---|
193 | } |
---|
194 | |
---|
195 | |
---|
196 | /** |
---|
197 | * the function maps name to key ids |
---|
198 | * @param name of the key |
---|
199 | * @param id of the key |
---|
200 | */ |
---|
201 | void KeyMapper::mapKeys(const char* name, int* index) |
---|
202 | { |
---|
203 | for( int i = 0; map[i].pValue != NULL; ++i ) |
---|
204 | { |
---|
205 | if( !strcmp (name, map[i].pName)) |
---|
206 | { |
---|
207 | if( index[0] == 0) |
---|
208 | { |
---|
209 | *map[i].pValue = index[1]; |
---|
210 | PRINTF(4)("Mapping %s to '%s' (id %i)\n", name, SDLKToKeyname(index[1]), index[1]); |
---|
211 | break; |
---|
212 | } |
---|
213 | else { |
---|
214 | *map[i].pValue = index[1]; |
---|
215 | PRINTF(4)("Mapping %s to '%s' (id %i)\n", name, SDLBToButtonname(index[1]), index[1]); |
---|
216 | break; |
---|
217 | } |
---|
218 | } |
---|
219 | } |
---|
220 | } |
---|
221 | |
---|
222 | |
---|
223 | /** |
---|
224 | * this function gives some debug information about the key mapper class |
---|
225 | */ |
---|
226 | void KeyMapper::debug() |
---|
227 | { |
---|
228 | PRINT(0)("\n==========================| KeyMapper::debug() |===\n"); |
---|
229 | for(int i = 0; map[i].pValue != NULL; ++i) |
---|
230 | { |
---|
231 | PRINT(0)("%s = %i\n",map[i].pName, *map[i].pValue); |
---|
232 | } |
---|
233 | PRINT(0)("=======================================================\n"); |
---|
234 | } |
---|