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