1 |
|
2 #include "Input.hh" |
|
3 #include "Engine.hh" |
|
4 #include "Config.hh" |
|
5 |
|
6 InputKeymapEntry<PlayerInputBit> INPUT_PLAYER_KEYMAP[] = { |
|
7 { INPUT_AIM_UP, INPUT_FLAG_UNLIMITED, { -CL_KEY_ENTER, CL_KEY_UP } }, |
|
8 { INPUT_AIM_DOWN, INPUT_FLAG_UNLIMITED, { -CL_KEY_ENTER, CL_KEY_DOWN } }, |
|
9 { INPUT_MOVE_LEFT, INPUT_FLAG_UNLIMITED, { -CL_KEY_ENTER, CL_KEY_LEFT } }, |
|
10 { INPUT_MOVE_RIGHT, INPUT_FLAG_UNLIMITED, { -CL_KEY_ENTER, CL_KEY_RIGHT } }, |
|
11 { INPUT_JUMP, INPUT_FLAG_SLOWREPEAT, { -CL_KEY_ENTER, CL_KEY_RSHIFT } }, |
|
12 { INPUT_DIG, INPUT_FLAG_NOREPEAT, { CL_KEY_LEFT, CL_KEY_RIGHT } }, |
|
13 { INPUT_SHOOT, INPUT_FLAG_UNLIMITED, { CL_KEY_RCONTROL, 0 } }, |
|
14 { INPUT_CHANGE_PREV, INPUT_FLAG_SLOWREPEAT, { CL_KEY_ENTER, CL_KEY_LEFT } }, |
|
15 { INPUT_CHANGE_NEXT, INPUT_FLAG_SLOWREPEAT, { CL_KEY_ENTER, CL_KEY_RIGHT } }, |
|
16 { INPUT_ROPE, INPUT_FLAG_NOREPEAT, { CL_KEY_ENTER, CL_KEY_RSHIFT } }, |
|
17 { INPUT_UNROPE, INPUT_FLAG_SLOWREPEAT, { -CL_KEY_ENTER, CL_KEY_RSHIFT } }, |
|
18 { INPUT_ROPE_UP, INPUT_FLAG_UNLIMITED, { CL_KEY_ENTER, CL_KEY_UP } }, |
|
19 { INPUT_ROPE_DOWN, INPUT_FLAG_UNLIMITED, { CL_KEY_ENTER, CL_KEY_DOWN } }, |
|
20 { INPUT_SUICIDE, INPUT_FLAG_NOREPEAT, { CL_KEY_LCONTROL, CL_KEY_K } }, |
|
21 { INPUT_NONE, 0, { 0, 0 } } |
|
22 }; |
|
23 |
|
24 InputKeymapEntry<GuiInputBit> INPUT_GUI_KEYMAP[] = { |
|
25 { GUI_INPUT_QUIT, 0, { CL_KEY_ESCAPE, 0 } }, |
|
26 { GUI_INPUT_DISPLAY_WEAPON, 0, { CL_KEY_ENTER, 0 } }, |
|
27 { GUI_INPUT_DEBUG_PLAYER, 0, { CL_KEY_I, 0 } }, |
|
28 { GUI_INPUT_TOGGLE_FULLSCREEN, INPUT_FLAG_NOREPEAT,{ CL_KEY_LCONTROL, CL_KEY_F } }, |
|
29 { GUI_INPUT_NONE, 0, { 0, 0, } } |
|
30 }; |
|
31 |
|
32 /* |
|
33 * InputKeyRepeatEntry |
|
34 */ |
|
35 template <typename BitEnumType> |
|
36 InputKeyRepeatEntry<BitEnumType>::InputKeyRepeatEntry (BitEnumType value, TimeMS expire) : |
|
37 value(value), expire(expire) |
|
38 { |
|
39 |
|
40 } |
|
41 |
|
42 template <typename BitEnumType> |
|
43 bool InputKeyRepeatEntry<BitEnumType>::operator< (const struct InputKeyRepeatEntry &other) { |
|
44 return other.expire > expire; |
|
45 } |
|
46 |
|
47 template <typename BitEnumType> |
|
48 bool InputKeyRepeatEntry<BitEnumType>::updateExpired (TimeMS dt) { |
|
49 if (expire == 0) |
|
50 return false; |
|
51 |
|
52 expire -= dt; |
|
53 |
|
54 return (expire <= 0); |
|
55 } |
|
56 |
|
57 /* |
|
58 * InputKeyRepeatQueue |
|
59 */ |
|
60 template <typename BitEnumType> |
|
61 InputKeyRepeatQueue<BitEnumType>::InputKeyRepeatQueue (TimeMS expire) : |
|
62 expire(expire) |
|
63 { |
|
64 |
|
65 } |
|
66 |
|
67 template <typename BitEnumType> |
|
68 void InputKeyRepeatQueue<BitEnumType>::push (BitEnumType bit, bool expire) { |
|
69 list.push_back(InputKeyRepeatEntry<BitEnumType>(bit, expire ? this->expire : 0)); |
|
70 } |
|
71 |
|
72 template <typename BitEnumType> |
|
73 void InputKeyRepeatQueue<BitEnumType>::forget (BitEnumType bit) { |
|
74 // go through the list, looking for it |
|
75 for (list_iterator it = list.begin(); it != list.end(); it++) { |
|
76 if (it->value == bit) { |
|
77 // found, erase it and return |
|
78 list.erase(it); |
|
79 |
|
80 return; |
|
81 } |
|
82 } |
|
83 } |
|
84 |
|
85 template <typename BitEnumType> |
|
86 bool InputKeyRepeatQueue<BitEnumType>::find (BitEnumType bit) { |
|
87 for (list_iterator it = list.begin(); it != list.end(); it++) { |
|
88 if (it->value == bit) |
|
89 return true; |
|
90 } |
|
91 |
|
92 return false; |
|
93 } |
|
94 |
|
95 template <typename BitEnumType> |
|
96 void InputKeyRepeatQueue<BitEnumType>::update (TimeMS dt) { |
|
97 list_iterator it = list.begin(); |
|
98 |
|
99 // go through each entry, updateExpired and remove if expired |
|
100 while (it != list.end()) { |
|
101 if (it->updateExpired(dt)) |
|
102 it = list.erase(it); |
|
103 else |
|
104 it++; |
|
105 } |
|
106 } |
|
107 |
|
108 /* |
|
109 * InputHandler |
|
110 */ |
|
111 template <typename BitEnumType, typename BitMaskType> |
|
112 InputHandler<BitEnumType, BitMaskType>::InputHandler (CL_InputDevice &keyboard, InputKeymapEntry<BitEnumType> *keymap, TimeMS keyrepeat_expire) : |
|
113 keyboard(keyboard), |
|
114 keymap(keymap), |
|
115 value(0), |
|
116 prev_value(0), |
|
117 dt(0), |
|
118 queue(keyrepeat_expire) |
|
119 { |
|
120 |
|
121 } |
|
122 |
|
123 template <typename BitEnumType, typename BitMaskType> |
|
124 bool InputHandler<BitEnumType, BitMaskType>::checkKeycode (int keycode) { |
|
125 if (keycode > 0) |
|
126 return keyboard.get_keycode(keycode); |
|
127 |
|
128 else if (keycode < 0) |
|
129 return !keyboard.get_keycode(-keycode); |
|
130 |
|
131 else // == 0 |
|
132 return true; |
|
133 } |
|
134 |
|
135 template <typename BitEnumType, typename BitMaskType> |
|
136 void InputHandler<BitEnumType, BitMaskType>::readValue (BitMaskType &mask, TimeMS &dt) { |
|
137 // copy to args |
|
138 mask = this->value; |
|
139 dt = this->dt; |
|
140 |
|
141 this->value = 0; |
|
142 this->dt = 0; |
|
143 } |
|
144 |
|
145 template <typename BitEnumType, typename BitMaskType> |
|
146 void InputHandler<BitEnumType, BitMaskType>::update (TimeMS dt) { |
|
147 // all bits that are held down, even those ignored |
|
148 BitMaskType raw_value = 0; |
|
149 |
|
150 // update the key-repeat queue |
|
151 queue.update(dt); |
|
152 |
|
153 // then go through the keymap |
|
154 for (InputKeymapEntry<BitEnumType> *e = keymap; e->input != 0; e++) { |
|
155 // check if we've got the correct keycodes |
|
156 if (checkKeycode(e->keycodes[0]) && checkKeycode(e->keycodes[1])) { |
|
157 // set raw_value |
|
158 raw_value |= e->input; |
|
159 |
|
160 if (e->flags & INPUT_FLAG_SLOWREPEAT) { |
|
161 // repeat, but slowly |
|
162 if (!(prev_value & e->input)) { |
|
163 // we've released the key earlier, move it to the back of the queue |
|
164 queue.forget(e->input); |
|
165 queue.push(e->input); |
|
166 |
|
167 } else if (queue.find(e->input)) { |
|
168 // it's still in the queue, so ignore, but set it in ignore_value |
|
169 continue; |
|
170 |
|
171 } else { |
|
172 // ok, but add it to the queue |
|
173 queue.push(e->input); |
|
174 } |
|
175 |
|
176 } else if (e->flags & INPUT_FLAG_NOREPEAT) { |
|
177 // do not repeat at all |
|
178 if (prev_value & e->input) { |
|
179 // ignore repeats |
|
180 continue; |
|
181 } |
|
182 } |
|
183 |
|
184 // set bit in masks |
|
185 this->value |= e->input; |
|
186 } |
|
187 } |
|
188 |
|
189 // update prev_value, also adding ignored values |
|
190 prev_value = raw_value; |
|
191 |
|
192 // then increment our dt |
|
193 this->dt += dt; |
|
194 } |
|
195 |
|
196 /** |
|
197 * Input |
|
198 */ |
|
199 Input::Input (CL_InputDevice &keyboard) : |
|
200 keyboard(keyboard), |
|
201 update_timer(INPUT_POLL_INTERVAL), |
|
202 player(keyboard, INPUT_PLAYER_KEYMAP, INPUT_REPEAT_DELAY), |
|
203 gui(keyboard, INPUT_GUI_KEYMAP, INPUT_REPEAT_DELAY) |
|
204 { |
|
205 // connect timer |
|
206 slots.connect(update_timer.sig_tick(), &player, &InputHandler<PlayerInputBit, PlayerInput>::update); |
|
207 slots.connect(update_timer.sig_tick(), &gui, &InputHandler<GuiInputBit, GuiInput>::update); |
|
208 |
|
209 // enable timer |
|
210 update_timer.start(); |
|
211 } |
|
212 |
|
213 void Input::readPlayerInput (PlayerInput &mask, TimeMS &dt) { |
|
214 player.readValue(mask, dt); |
|
215 } |
|
216 |
|
217 GuiInput Input::readGuiInput (void) { |
|
218 GuiInput mask; |
|
219 TimeMS dt; |
|
220 |
|
221 gui.readValue(mask, dt); |
|
222 |
|
223 return mask; |
|
224 } |
|
225 |
|