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: ... |
---|
14 | */ |
---|
15 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_EVENT |
---|
17 | |
---|
18 | #include "key_mapper.h" |
---|
19 | |
---|
20 | #include "ini_parser.h" |
---|
21 | #include "keynames.h" |
---|
22 | |
---|
23 | using namespace std; |
---|
24 | |
---|
25 | |
---|
26 | |
---|
27 | int KeyMapper::PEV_UP = -1; |
---|
28 | int KeyMapper::PEV_DOWN = -1; |
---|
29 | int KeyMapper::PEV_LEFT = -1; |
---|
30 | int KeyMapper::PEV_RIGHT = -1; |
---|
31 | int KeyMapper::PEV_STRAFE_LEFT = -1; |
---|
32 | int KeyMapper::PEV_STRAFE_RIGHT = -1; |
---|
33 | |
---|
34 | int KeyMapper::PEV_FIRE1 = -1; |
---|
35 | int KeyMapper::PEV_FIRE2 = -1; |
---|
36 | |
---|
37 | int KeyMapper::PEV_VIEW1 = -1; |
---|
38 | int KeyMapper::PEV_VIEW2 = -1; |
---|
39 | int KeyMapper::PEV_VIEW3 = -1; |
---|
40 | int KeyMapper::PEV_VIEW4 = -1; |
---|
41 | |
---|
42 | |
---|
43 | |
---|
44 | /** |
---|
45 | \brief standard constructor |
---|
46 | \todo this constructor is not jet implemented - do it |
---|
47 | */ |
---|
48 | KeyMapper::KeyMapper () |
---|
49 | { |
---|
50 | this->setClassID(CL_KEY_MAPPER, "KeyMapper"); |
---|
51 | this->keyAliases = NULL; |
---|
52 | } |
---|
53 | |
---|
54 | |
---|
55 | /** |
---|
56 | \brief standard deconstructor |
---|
57 | |
---|
58 | */ |
---|
59 | KeyMapper::~KeyMapper () |
---|
60 | { |
---|
61 | // delete what has to be deleted here |
---|
62 | } |
---|
63 | |
---|
64 | \ |
---|
65 | /** |
---|
66 | \brief loads new key bindings from a file |
---|
67 | \param filename: The path and name of the file to load the bindings from |
---|
68 | */ |
---|
69 | void KeyMapper::loadKeyBindings (const char* fileName) |
---|
70 | { |
---|
71 | FILE* stream; |
---|
72 | |
---|
73 | PRINTF(4)("Loading key bindings from %s\n", fileName); |
---|
74 | |
---|
75 | // remove old bindings if present |
---|
76 | if( this->keyAliases != NULL) |
---|
77 | { |
---|
78 | free (this->keyAliases); |
---|
79 | this->keyAliases = NULL; |
---|
80 | } |
---|
81 | |
---|
82 | // create parser |
---|
83 | IniParser parser(fileName); |
---|
84 | if( parser.getSection (CONFIG_SECTION_PLAYER "1") == -1) |
---|
85 | { |
---|
86 | PRINTF(1)("Could not find key bindings " CONFIG_SECTION_PLAYER"1 in %s\n", fileName); |
---|
87 | return; |
---|
88 | } |
---|
89 | // allocate empty lookup table |
---|
90 | this->keyAliases = (KeyBindings*) calloc (1, sizeof (KeyBindings)); |
---|
91 | |
---|
92 | char namebuf[256]; |
---|
93 | char valuebuf[256]; |
---|
94 | memset (namebuf, 0, 256); |
---|
95 | memset (valuebuf, 0, 256); |
---|
96 | int* index; |
---|
97 | |
---|
98 | while( parser.nextVar (namebuf, valuebuf) != -1) |
---|
99 | { |
---|
100 | //index = nameToIndex (valuebuf); |
---|
101 | int c; |
---|
102 | if( (c = keynameToSDLK (valuebuf)) != -1) |
---|
103 | { |
---|
104 | index[1] = c; index[0] = 0; |
---|
105 | } |
---|
106 | if( (c = buttonnameToSDLB (valuebuf)) != -1) |
---|
107 | { |
---|
108 | index[1] = c; index[0] = 1; |
---|
109 | } |
---|
110 | |
---|
111 | switch( index[0]) |
---|
112 | { |
---|
113 | case 0: |
---|
114 | PRINTF(4)("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), namebuf); |
---|
115 | strcpy (this->keyAliases->keys[index[1]], namebuf); |
---|
116 | break; |
---|
117 | case 1: |
---|
118 | PRINTF(4)("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), namebuf); |
---|
119 | strcpy (this->keyAliases->buttons[index[1]], namebuf); |
---|
120 | break; |
---|
121 | default: |
---|
122 | break; |
---|
123 | } |
---|
124 | memset (namebuf, 0, 256); |
---|
125 | memset (valuebuf, 0, 256); |
---|
126 | } |
---|
127 | |
---|
128 | |
---|
129 | // PARSE MISC SECTION |
---|
130 | if( parser.getSection (CONFIG_SECTION_MISC_KEYS) == -1) |
---|
131 | { |
---|
132 | PRINTF(1)("Could not find key bindings in %s\n", fileName); |
---|
133 | return; |
---|
134 | } |
---|
135 | |
---|
136 | while( parser.nextVar (namebuf, valuebuf) != -1) |
---|
137 | { |
---|
138 | index = nameToIndex (valuebuf); |
---|
139 | |
---|
140 | switch( index[0]) |
---|
141 | { |
---|
142 | case 0: |
---|
143 | PRINTF(4)("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), namebuf); |
---|
144 | strcpy (keyAliases->keys[index[1]], namebuf); |
---|
145 | break; |
---|
146 | case 1: |
---|
147 | PRINTF(4)("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), namebuf); |
---|
148 | strcpy (keyAliases->buttons[index[1]], namebuf); |
---|
149 | break; |
---|
150 | default: |
---|
151 | break; |
---|
152 | } |
---|
153 | memset (namebuf, 0, 256); |
---|
154 | memset (valuebuf, 0, 256); |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | |
---|
159 | |
---|
160 | int* KeyMapper::nameToIndex (char* name) |
---|
161 | { |
---|
162 | coord[0] = -1; |
---|
163 | coord[1] = -1; |
---|
164 | int c; |
---|
165 | if( (c = keynameToSDLK (name)) != -1) |
---|
166 | { |
---|
167 | coord[1] = c; |
---|
168 | coord[0] = 0; |
---|
169 | } |
---|
170 | if( (c = buttonnameToSDLB (name)) != -1) |
---|
171 | { |
---|
172 | coord[1] = c; |
---|
173 | coord[0] = 1; |
---|
174 | } |
---|
175 | return coord; |
---|
176 | } |
---|