switched to GPLv3 ONLY, because i don't trust FSF anymore
[knightmare.git] / keybinds.d
blob3a387152be7954a905193230397ffed106e2cb87
1 /* coded by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
2 * Understanding is not required. Only obedience.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, version 3 of the License ONLY.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 module keybinds;
18 import arsd.simpledisplay;
20 import iv.cmdcon;
21 import iv.cmdcon.keybinds;
24 // ////////////////////////////////////////////////////////////////////////// //
25 enum ActionKey {
26 Up,
27 Down,
28 Left,
29 Right,
30 Fire,
31 //Pause,
32 //Abort,
36 __gshared ubyte[ActionKey.max+1] keystate = 0;
37 __gshared ActionKey[Key] keybinds;
40 void setupDefaultKeyBindings () {
41 keybinds[findKeyByName("up")] = ActionKey.Up;
42 keybinds[findKeyByName("down")] = ActionKey.Down;
43 keybinds[findKeyByName("left")] = ActionKey.Left;
44 keybinds[findKeyByName("right")] = ActionKey.Right;
45 keybinds[findKeyByName("ctrl")] = ActionKey.Fire;
46 keybinds[findKeyByName("z")] = ActionKey.Fire;
47 keybinds[findKeyByName("x")] = ActionKey.Fire;
48 keybinds[findKeyByName("alt")] = ActionKey.Fire;
49 keybinds[findKeyByName("shift")] = ActionKey.Fire;
53 // true: press/release registered
54 bool doActionBind (KeyEvent event, ActionKey* kk=null) {
55 if (!event.key) return false;
56 if (auto akp = event.key in keybinds) {
57 if (kk !is null) *kk = *akp;
58 if (event.pressed) {
59 keystate[*akp] |= 0b011;
60 } else {
61 keystate[*akp] &= 0b101;
63 return true;
65 return false;
69 void actionBindFrameEnd () {
70 foreach (ref ubyte b; keystate[]) {
71 if (b&0b10) b = 0b11; else b = 0;
76 void actionBindClear () {
77 keystate[] = 0;
81 bool actionPressed (ActionKey key) nothrow @nogc { pragma(inline, true); return ((keystate[key]&1) != 0); }
82 bool actionWasRelease (ActionKey key) nothrow @nogc { pragma(inline, true); return ((keystate[key]&0b100) != 0); }