diff options
author | Aki <please@ignore.pl> | 2022-04-01 21:23:39 +0200 |
---|---|---|
committer | Aki <please@ignore.pl> | 2022-04-01 21:23:39 +0200 |
commit | 3c487c5cd69c53d6fea948643c0a76df03516605 (patch) | |
tree | 72730c7b8b26a5ef8fc9a987ec4c16129efd5aac /StarsEx/Keyboard.cpp | |
parent | 8f353abd0bfe18baddd8a8250ab7c4f2d1c83a6e (diff) | |
download | starshatter-3c487c5cd69c53d6fea948643c0a76df03516605.zip starshatter-3c487c5cd69c53d6fea948643c0a76df03516605.tar.gz starshatter-3c487c5cd69c53d6fea948643c0a76df03516605.tar.bz2 |
Moved Stars45 to StarsEx
Diffstat (limited to 'StarsEx/Keyboard.cpp')
-rw-r--r-- | StarsEx/Keyboard.cpp | 215 |
1 files changed, 215 insertions, 0 deletions
diff --git a/StarsEx/Keyboard.cpp b/StarsEx/Keyboard.cpp new file mode 100644 index 0000000..63b93e9 --- /dev/null +++ b/StarsEx/Keyboard.cpp @@ -0,0 +1,215 @@ +/* Starshatter: The Open Source Project + Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors + Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors + Copyright (c) 1997-2006, Destroyer Studios LLC. + + AUTHOR: John DiCamillo + + + OVERVIEW + ======== + Keyboard Input class +*/ + +#include "Keyboard.h" +#include "Game.h" + +// +--------------------------------------------------------------------+ + +static Keyboard* instance = 0; +int Keyboard::map[KEY_MAP_SIZE]; +int Keyboard::alt[KEY_MAP_SIZE]; + +Keyboard::Keyboard() +: x(0), y(0), z(0), p(0), r(0), w(0), c(0), p1(0), r1(0), w1(0), t(0) +{ + instance = this; + sensitivity = 25; + dead_zone = 100; + + for (int i = 0; i < MotionController::MaxActions; i++) + action[i] = 0; + + memset(map, 0, sizeof(map)); + memset(alt, 0, sizeof(alt)); + + map[KEY_PLUS_X] = 'R'; + map[KEY_MINUS_X] = 'E'; + map[KEY_PLUS_Y] = VK_HOME; + map[KEY_MINUS_Y] = VK_END; + map[KEY_PLUS_Z] = VK_PRIOR; // page up + map[KEY_MINUS_Z] = VK_NEXT; // page down + + map[KEY_PITCH_UP] = VK_DOWN; + map[KEY_PITCH_DOWN] = VK_UP; + map[KEY_YAW_LEFT] = VK_LEFT; + map[KEY_YAW_RIGHT] = VK_RIGHT; + map[KEY_ROLL_ENABLE] = 0; // used to be VK_CONTROL; +} + +Keyboard::~Keyboard() +{ + instance = 0; +} + +Keyboard* +Keyboard::GetInstance() +{ + return instance; +} + +// +--------------------------------------------------------------------+ + +void +Keyboard::MapKeys(KeyMapEntry* mapping, int nkeys) +{ + for (int i = 0; i < nkeys; i++) { + KeyMapEntry k = mapping[i]; + + if (k.act >= KEY_MAP_FIRST && k.act <= KEY_MAP_LAST) { + if (k.key == 0 || k.key > VK_MBUTTON && k.key < KEY_JOY_1) { + map[k.act] = k.key; + alt[k.act] = k.alt; + } + } + } +} + +// +--------------------------------------------------------------------+ + +bool Keyboard::KeyDown(int key) +{ + if (key) { + short k = GetAsyncKeyState(key); + return (k<0)||(k&1); + } + + return 0; +} + +// +--------------------------------------------------------------------+ + +bool Keyboard::KeyDownMap(int key) +{ + if (key >= KEY_MAP_FIRST && key <= KEY_MAP_LAST && map[key]) { + short k = GetAsyncKeyState(map[key]); + short a = -1; + + if (alt[key] > 0 && alt[key] < KEY_JOY_1) { + a = GetAsyncKeyState(alt[key]); + } + else { + a = !GetAsyncKeyState(VK_SHIFT) && + !GetAsyncKeyState(VK_MENU); + } + + return ((k<0)||(k&1)) && ((a<0)||(a&1)); + } + + return 0; +} + +// +--------------------------------------------------------------------+ + +void +Keyboard::FlushKeys() +{ + for (int i = 0; i < 255; i++) + GetAsyncKeyState(i); +} + +// +--------------------------------------------------------------------+ + +static inline double sqr(double a) { return a; } //*a; } + +void +Keyboard::Acquire() +{ + t = x = y = z = p = r = w = c = 0; + + for (int i = 0; i < MotionController::MaxActions; i++) + action[i] = 0; + + int speed = 10; + + // lateral translations: + if (KeyDownMap(KEY_PLUS_Y)) y = 1; + else if (KeyDownMap(KEY_MINUS_Y)) y = -1; + + if (KeyDownMap(KEY_PLUS_Z)) z = 1; + else if (KeyDownMap(KEY_MINUS_Z)) z = -1; + + if (KeyDownMap(KEY_MINUS_X)) x = -1; + else if (KeyDownMap(KEY_PLUS_X)) x = 1; + + const double steps=10; + + // if roll and yaw are swapped -------------------------- + if (swapped) { + // yaw: + if (KeyDownMap(KEY_ROLL_LEFT)) { if (w1<steps) w1+=1; w = -sqr(w1/steps); } + else if (KeyDownMap(KEY_ROLL_RIGHT)) { if (w1<steps) w1+=1; w = sqr(w1/steps); } + + // another way to yaw: + if (KeyDownMap(KEY_ROLL_ENABLE)) { + if (KeyDownMap(KEY_YAW_LEFT)) { if (w1<steps) w1+=1; w = -sqr(w1/steps); } + else if (KeyDownMap(KEY_YAW_RIGHT)) { if (w1<steps) w1+=1; w = sqr(w1/steps); } + else w1 = 0; + } + + // roll: + else { + if (KeyDownMap(KEY_YAW_LEFT)) { if (r1<steps) r1+=1; r = sqr(r1/steps); } + else if (KeyDownMap(KEY_YAW_RIGHT)) { if (r1<steps) r1+=1; r = -sqr(r1/steps); } + else r1 = 0; + } + } + + // else roll and yaw are NOT swapped --------------------- + else { + // roll: + if (KeyDownMap(KEY_ROLL_LEFT)) { if (r1<steps) r1+=1; r = sqr(r1/steps); } + else if (KeyDownMap(KEY_ROLL_RIGHT)) { if (r1<steps) r1+=1; r = -sqr(r1/steps); } + + // another way to roll: + if (KeyDownMap(KEY_ROLL_ENABLE)) { + if (KeyDownMap(KEY_YAW_LEFT)) { if (r1<steps) r1+=1; r = sqr(r1/steps); } + else if (KeyDownMap(KEY_YAW_RIGHT)) { if (r1<steps) r1+=1; r = -sqr(r1/steps); } + else r1 = 0; + } + + // yaw left-right + else { + if (KeyDownMap(KEY_YAW_LEFT)) { if (w1<steps) w1+=1; w = -sqr(w1/steps); } + else if (KeyDownMap(KEY_YAW_RIGHT)) { if (w1<steps) w1+=1; w = sqr(w1/steps); } + else w1 = 0; + } + } + + // if pitch is inverted ---------------------------------- + if (inverted) { + if (KeyDownMap(KEY_PITCH_DOWN)) { if (p1<steps) p1+=1; p = -sqr(p1/steps); } + else if (KeyDownMap(KEY_PITCH_UP)) { if (p1<steps) p1+=1; p = sqr(p1/steps); } + else p1 = 0; + } + + // else pitch is NOT inverted ---------------------------- + else { + if (KeyDownMap(KEY_PITCH_UP)) { if (p1<steps) p1+=1; p = -sqr(p1/steps); } + else if (KeyDownMap(KEY_PITCH_DOWN)) { if (p1<steps) p1+=1; p = sqr(p1/steps); } + else p1 = 0; + } + + if (KeyDownMap(KEY_CENTER)) c = 1; + + // actions + if (KeyDownMap(KEY_ACTION_0)) action[0] = 1; + if (KeyDownMap(KEY_ACTION_1)) action[1] = 1; + if (KeyDownMap(KEY_ACTION_2)) action[2] = 1; + if (KeyDownMap(KEY_ACTION_3)) action[3] = 1; +} + +// +--------------------------------------------------------------------+ + + + |