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