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_UP = EV_UNKNOWN; |
---|
36 | int KeyMapper::PEV_DOWN = EV_UNKNOWN; |
---|
37 | int KeyMapper::PEV_LEFT = EV_UNKNOWN; |
---|
38 | int KeyMapper::PEV_RIGHT = EV_UNKNOWN; |
---|
39 | int KeyMapper::PEV_STRAFE_LEFT = EV_UNKNOWN; |
---|
40 | int KeyMapper::PEV_STRAFE_RIGHT = EV_UNKNOWN; |
---|
41 | |
---|
42 | int KeyMapper::PEV_FIRE1 = EV_UNKNOWN; |
---|
43 | int KeyMapper::PEV_FIRE2 = EV_UNKNOWN; |
---|
44 | int KeyMapper::PEV_PREVIOUS_WEAPON = EV_UNKNOWN; |
---|
45 | int KeyMapper::PEV_NEXT_WEAPON = EV_UNKNOWN; |
---|
46 | |
---|
47 | int KeyMapper::PEV_VIEW0 = EV_UNKNOWN; |
---|
48 | int KeyMapper::PEV_VIEW1 = EV_UNKNOWN; |
---|
49 | int KeyMapper::PEV_VIEW2 = EV_UNKNOWN; |
---|
50 | int KeyMapper::PEV_VIEW3 = EV_UNKNOWN; |
---|
51 | int KeyMapper::PEV_VIEW4 = EV_UNKNOWN; |
---|
52 | int KeyMapper::PEV_VIEW5 = EV_UNKNOWN; |
---|
53 | |
---|
54 | int KeyMapper::PEV_NEXT_WORLD = EV_UNKNOWN; |
---|
55 | int KeyMapper::PEV_PREVIOUS_WORLD = EV_UNKNOWN; |
---|
56 | |
---|
57 | int KeyMapper::PEV_PAUSE = EV_UNKNOWN; |
---|
58 | int KeyMapper::PEV_QUIT = EV_UNKNOWN; |
---|
59 | |
---|
60 | |
---|
61 | |
---|
62 | //! this is the mapping array from names to ids: enter all orxonox.conf keys here |
---|
63 | /** @todo use globals.h for this.... everything is done there for those Options, |
---|
64 | * and you do not have to care about The namings, as they might change |
---|
65 | */ |
---|
66 | orxKeyMapping map[] = { |
---|
67 | {&KeyMapper::PEV_UP, CONFIG_NAME_PLAYER_UP}, |
---|
68 | {&KeyMapper::PEV_DOWN, CONFIG_NAME_PLAYER_DOWN}, |
---|
69 | {&KeyMapper::PEV_LEFT, CONFIG_NAME_PLAYER_LEFT}, |
---|
70 | {&KeyMapper::PEV_RIGHT, CONFIG_NAME_PLAYER_RIGHT}, |
---|
71 | {&KeyMapper::PEV_STRAFE_LEFT, "StrafeLeft"}, |
---|
72 | {&KeyMapper::PEV_STRAFE_RIGHT, "StrafeRight"}, |
---|
73 | |
---|
74 | {&KeyMapper::PEV_FIRE1, CONFIG_NAME_PLAYER_FIRE}, |
---|
75 | {&KeyMapper::PEV_FIRE1, "Fire1"}, |
---|
76 | {&KeyMapper::PEV_FIRE2, "Fire2"}, |
---|
77 | {&KeyMapper::PEV_NEXT_WEAPON, CONFIG_NAME_PLAYER_NEXT_WEAPON}, |
---|
78 | {&KeyMapper::PEV_PREVIOUS_WEAPON, CONFIG_NAME_PLAYER_PREV_WEAPON}, |
---|
79 | |
---|
80 | |
---|
81 | {&KeyMapper::PEV_VIEW0, CONFIG_NAME_VIEW0}, |
---|
82 | {&KeyMapper::PEV_VIEW1, CONFIG_NAME_VIEW1}, |
---|
83 | {&KeyMapper::PEV_VIEW2, CONFIG_NAME_VIEW2}, |
---|
84 | {&KeyMapper::PEV_VIEW3, CONFIG_NAME_VIEW3}, |
---|
85 | {&KeyMapper::PEV_VIEW4, CONFIG_NAME_VIEW4}, |
---|
86 | {&KeyMapper::PEV_VIEW5, CONFIG_NAME_VIEW5}, |
---|
87 | |
---|
88 | {&KeyMapper::PEV_NEXT_WORLD, CONFIG_NAME_NEXT_WORLD}, |
---|
89 | {&KeyMapper::PEV_PREVIOUS_WORLD, CONFIG_NAME_PREV_WORLD}, |
---|
90 | |
---|
91 | {&KeyMapper::PEV_PAUSE, CONFIG_NAME_PAUSE}, |
---|
92 | {&KeyMapper::PEV_QUIT, CONFIG_NAME_QUIT}, |
---|
93 | {NULL, NULL} |
---|
94 | }; |
---|
95 | |
---|
96 | |
---|
97 | |
---|
98 | /** |
---|
99 | * standard constructor |
---|
100 | */ |
---|
101 | KeyMapper::KeyMapper () |
---|
102 | { |
---|
103 | this->setClassID(CL_KEY_MAPPER, "KeyMapper"); |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | /** |
---|
108 | * standard deconstructor |
---|
109 | */ |
---|
110 | KeyMapper::~KeyMapper () |
---|
111 | { |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | /** |
---|
116 | * loads new key bindings from a file |
---|
117 | * @param filename: The path and name of the file to load the bindings from |
---|
118 | */ |
---|
119 | void KeyMapper::loadKeyBindings (const char* fileName) |
---|
120 | { |
---|
121 | IniParser parser(fileName); |
---|
122 | this->loadKeyBindings(&parser); |
---|
123 | } |
---|
124 | |
---|
125 | void KeyMapper::loadKeyBindings(IniParser* iniParser) |
---|
126 | { |
---|
127 | if( !iniParser->getSection (CONFIG_SECTION_PLAYER "1")) |
---|
128 | { |
---|
129 | PRINTF(1)("Could not find key bindings " CONFIG_SECTION_PLAYER"1\n"); |
---|
130 | return; |
---|
131 | } |
---|
132 | int* index; |
---|
133 | |
---|
134 | iniParser->firstVar(); |
---|
135 | while(iniParser->getCurrentName()) |
---|
136 | { |
---|
137 | PRINTF(3)("Keys: Parsing %s, %s now.\n", iniParser->getCurrentName(), iniParser->getCurrentValue()); |
---|
138 | // map the name to an sdl index |
---|
139 | index = nameToIndex (iniParser->getCurrentValue()); |
---|
140 | // map the index to a internal name |
---|
141 | this->mapKeys(iniParser->getCurrentName(), index); |
---|
142 | iniParser->nextVar(); |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | // PARSE MISC SECTION |
---|
147 | if( !iniParser->getSection (CONFIG_SECTION_MISC_KEYS)) |
---|
148 | { |
---|
149 | PRINTF(1)("Could not find key bindings" CONFIG_SECTION_MISC_KEYS "\n"); |
---|
150 | return; |
---|
151 | } |
---|
152 | |
---|
153 | iniParser->firstVar(); |
---|
154 | while(iniParser->getCurrentName()) |
---|
155 | { |
---|
156 | PRINTF(3)("MISC: Parsing %s, %s now.\n", iniParser->getCurrentName(), iniParser->getCurrentValue()); |
---|
157 | index = nameToIndex (iniParser->getCurrentValue()); |
---|
158 | this->mapKeys(iniParser->getCurrentName(), index); |
---|
159 | iniParser->nextVar(); |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | /** |
---|
164 | * this function looks up name to key index |
---|
165 | * @param the name of the button |
---|
166 | */ |
---|
167 | int* KeyMapper::nameToIndex (const char* name) |
---|
168 | { |
---|
169 | coord[0] = -1; |
---|
170 | coord[1] = -1; |
---|
171 | int c; |
---|
172 | if( (c = keynameToSDLK (name)) != -1) { |
---|
173 | coord[1] = c; |
---|
174 | coord[0] = 0; |
---|
175 | } |
---|
176 | if( (c = buttonnameToSDLB (name)) != -1) { |
---|
177 | coord[1] = c; |
---|
178 | coord[0] = 1; |
---|
179 | } |
---|
180 | return coord; |
---|
181 | } |
---|
182 | |
---|
183 | |
---|
184 | /** |
---|
185 | * the function maps name to key ids |
---|
186 | * @param name of the key |
---|
187 | * @param id of the key |
---|
188 | */ |
---|
189 | void KeyMapper::mapKeys(const char* name, int* index) |
---|
190 | { |
---|
191 | for( int i = 0; map[i].pValue != NULL; ++i ) |
---|
192 | { |
---|
193 | if( !strcmp (name, map[i].pName)) |
---|
194 | { |
---|
195 | if( index[0] == 0) |
---|
196 | { |
---|
197 | *map[i].pValue = index[1]; |
---|
198 | PRINTF(4)("Mapping %s to '%s' (id %i)\n", name, SDLKToKeyname(index[1]), index[1]); |
---|
199 | break; |
---|
200 | } |
---|
201 | else { |
---|
202 | *map[i].pValue = index[1]; |
---|
203 | PRINTF(4)("Mapping %s to '%s' (id %i)\n", name, SDLBToButtonname(index[1]), index[1]); |
---|
204 | break; |
---|
205 | } |
---|
206 | } |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | |
---|
211 | /** |
---|
212 | * this function gives some debug information about the key mapper class |
---|
213 | */ |
---|
214 | void KeyMapper::debug() |
---|
215 | { |
---|
216 | PRINT(0)("\n==========================| KeyMapper::debug() |===\n"); |
---|
217 | for(int i = 0; map[i].pValue != NULL; ++i) |
---|
218 | { |
---|
219 | PRINT(0)("%s = %i\n",map[i].pName, *map[i].pValue); |
---|
220 | } |
---|
221 | PRINT(0)("=======================================================\n"); |
---|
222 | } |
---|