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 | index = nameToIndex (iniParser->getCurrentValue()); |
---|
136 | this->mapKeys(iniParser->getCurrentName(), index[1]); |
---|
137 | iniParser->nextVar(); |
---|
138 | } |
---|
139 | |
---|
140 | |
---|
141 | // PARSE MISC SECTION |
---|
142 | if( !iniParser->getSection (CONFIG_SECTION_MISC_KEYS)) |
---|
143 | { |
---|
144 | PRINTF(1)("Could not find key bindings" CONFIG_SECTION_MISC_KEYS "\n"); |
---|
145 | return; |
---|
146 | } |
---|
147 | |
---|
148 | iniParser->getFirstVar(); |
---|
149 | while(iniParser->getCurrentName()) |
---|
150 | { |
---|
151 | PRINTF(3)("MISC: Parsing %s, %s now.\n", iniParser->getCurrentName(), iniParser->getCurrentValue()); |
---|
152 | index = nameToIndex (iniParser->getCurrentValue()); |
---|
153 | this->mapKeys(iniParser->getCurrentName(), index[1]); |
---|
154 | iniParser->nextVar(); |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | /** |
---|
159 | * this function looks up name to key index |
---|
160 | * @param the name of the button |
---|
161 | */ |
---|
162 | int* KeyMapper::nameToIndex (const char* name) |
---|
163 | { |
---|
164 | coord[0] = -1; |
---|
165 | coord[1] = -1; |
---|
166 | int c; |
---|
167 | if( (c = keynameToSDLK (name)) != -1) |
---|
168 | { |
---|
169 | coord[1] = c; |
---|
170 | coord[0] = 0; |
---|
171 | } |
---|
172 | if( (c = buttonnameToSDLB (name)) != -1) |
---|
173 | { |
---|
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 keyID) |
---|
187 | { |
---|
188 | for(int i = 0; map[i].pValue != NULL; ++i ) |
---|
189 | { |
---|
190 | if( !strcmp (name, map[i].pName)) |
---|
191 | { |
---|
192 | *map[i].pValue = keyID; |
---|
193 | PRINTF(0)("Mapping %s to id %i\n", name, keyID); |
---|
194 | break; |
---|
195 | } |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | |
---|
200 | /** |
---|
201 | * this function gives some debug information about the key mapper class |
---|
202 | */ |
---|
203 | void KeyMapper::debug() |
---|
204 | { |
---|
205 | PRINT(0)("\n==========================| KeyMapper::debug() |===\n"); |
---|
206 | for(int i = 0; map[i].pValue != NULL; ++i) |
---|
207 | { |
---|
208 | PRINT(0)("%s = %i\n",map[i].pName, *map[i].pValue); |
---|
209 | } |
---|
210 | PRINT(0)("=======================================================\n"); |
---|
211 | } |
---|