HPL1: implement KeyboardSDL class

This commit is contained in:
grisenti 2022-07-07 19:11:19 +02:00 committed by Eugene Sandulenko
parent a2d839ea30
commit 8bcc9be062
No known key found for this signature in database
GPG Key ID: 014D387312D34F08
2 changed files with 323 additions and 227 deletions

View File

@ -27,98 +27,349 @@
#include "hpl1/engine/impl/KeyboardSDL.h"
//#include "SDL/SDL.h"
#include "common/system.h"
#include "hpl1/engine/impl/LowLevelInputSDL.h"
#if 0 // def WIN32
#include <conio.h>
#endif
namespace hpl {
//////////////////////////////////////////////////////////////////////////
// CONSTRUCTORS
//////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------
cKeyboardSDL::cKeyboardSDL(cLowLevelInputSDL *apLowLevelInputSDL) : iKeyboard("SDL Portable Keyboard") {
#if 0
mpLowLevelInputSDL = apLowLevelInputSDL;
_lowLevelSystem = apLowLevelInputSDL;
mvKeyArray.resize(eKey_LastEnum);
ClearKeyList();
SDL_EnableUNICODE(1);
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
#endif
_downKeys.set_size(eKey_LastEnum);
_downKeys.clear();
}
//-----------------------------------------------------------------------
//////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
//////////////////////////////////////////////////////////////////////////
static eKey convertKey(int alKey) {
switch (alKey) {
case Common::KEYCODE_BACKSPACE:
return eKey_BACKSPACE;
case Common::KEYCODE_TAB:
return eKey_TAB;
case Common::KEYCODE_CLEAR:
return eKey_CLEAR;
case Common::KEYCODE_RETURN:
return eKey_RETURN;
case Common::KEYCODE_PAUSE:
return eKey_PAUSE;
case Common::KEYCODE_ESCAPE:
return eKey_ESCAPE;
case Common::KEYCODE_SPACE:
return eKey_SPACE;
case Common::KEYCODE_EXCLAIM:
return eKey_EXCLAIM;
case Common::KEYCODE_QUOTEDBL:
return eKey_QUOTEDBL;
case Common::KEYCODE_HASH:
return eKey_HASH;
case Common::KEYCODE_DOLLAR:
return eKey_DOLLAR;
case Common::KEYCODE_AMPERSAND:
return eKey_AMPERSAND;
case Common::KEYCODE_QUOTE:
return eKey_QUOTE;
case Common::KEYCODE_LEFTPAREN:
return eKey_LEFTPAREN;
case Common::KEYCODE_RIGHTPAREN:
return eKey_RIGHTPAREN;
case Common::KEYCODE_ASTERISK:
return eKey_ASTERISK;
case Common::KEYCODE_PLUS:
return eKey_PLUS;
case Common::KEYCODE_COMMA:
return eKey_COMMA;
case Common::KEYCODE_MINUS:
return eKey_MINUS;
case Common::KEYCODE_PERIOD:
return eKey_PERIOD;
case Common::KEYCODE_SLASH:
return eKey_SLASH;
case Common::KEYCODE_0:
return eKey_0;
case Common::KEYCODE_1:
return eKey_1;
case Common::KEYCODE_2:
return eKey_2;
case Common::KEYCODE_3:
return eKey_3;
case Common::KEYCODE_4:
return eKey_4;
case Common::KEYCODE_5:
return eKey_5;
case Common::KEYCODE_6:
return eKey_6;
case Common::KEYCODE_7:
return eKey_7;
case Common::KEYCODE_8:
return eKey_8;
case Common::KEYCODE_9:
return eKey_9;
case Common::KEYCODE_COLON:
return eKey_COLON;
case Common::KEYCODE_SEMICOLON:
return eKey_SEMICOLON;
case Common::KEYCODE_LESS:
return eKey_LESS;
case Common::KEYCODE_EQUALS:
return eKey_EQUALS;
case Common::KEYCODE_GREATER:
return eKey_GREATER;
case Common::KEYCODE_QUESTION:
return eKey_QUESTION;
case Common::KEYCODE_AT:
return eKey_AT;
case Common::KEYCODE_LEFTBRACKET:
return eKey_LEFTBRACKET;
case Common::KEYCODE_BACKSLASH:
return eKey_BACKSLASH;
case Common::KEYCODE_RIGHTBRACKET:
return eKey_RIGHTBRACKET;
case Common::KEYCODE_CARET:
return eKey_CARET;
case Common::KEYCODE_UNDERSCORE:
return eKey_UNDERSCORE;
case Common::KEYCODE_BACKQUOTE:
return eKey_BACKQUOTE;
case Common::KEYCODE_a:
return eKey_a;
case Common::KEYCODE_b:
return eKey_b;
case Common::KEYCODE_c:
return eKey_c;
case Common::KEYCODE_d:
return eKey_d;
case Common::KEYCODE_e:
return eKey_e;
case Common::KEYCODE_f:
return eKey_f;
case Common::KEYCODE_g:
return eKey_g;
case Common::KEYCODE_h:
return eKey_h;
case Common::KEYCODE_i:
return eKey_i;
case Common::KEYCODE_j:
return eKey_j;
case Common::KEYCODE_k:
return eKey_k;
case Common::KEYCODE_l:
return eKey_l;
case Common::KEYCODE_m:
return eKey_m;
case Common::KEYCODE_n:
return eKey_n;
case Common::KEYCODE_o:
return eKey_o;
case Common::KEYCODE_p:
return eKey_p;
case Common::KEYCODE_q:
return eKey_q;
case Common::KEYCODE_r:
return eKey_r;
case Common::KEYCODE_s:
return eKey_s;
case Common::KEYCODE_t:
return eKey_t;
case Common::KEYCODE_u:
return eKey_u;
case Common::KEYCODE_v:
return eKey_v;
case Common::KEYCODE_w:
return eKey_w;
case Common::KEYCODE_x:
return eKey_x;
case Common::KEYCODE_y:
return eKey_y;
case Common::KEYCODE_z:
return eKey_z;
case Common::KEYCODE_DELETE:
return eKey_DELETE;
case Common::KEYCODE_KP0:
return eKey_KP0;
case Common::KEYCODE_KP1:
return eKey_KP1;
case Common::KEYCODE_KP2:
return eKey_KP2;
case Common::KEYCODE_KP3:
return eKey_KP3;
case Common::KEYCODE_KP4:
return eKey_KP4;
case Common::KEYCODE_KP5:
return eKey_KP5;
case Common::KEYCODE_KP6:
return eKey_KP6;
case Common::KEYCODE_KP7:
return eKey_KP7;
case Common::KEYCODE_KP8:
return eKey_KP8;
case Common::KEYCODE_KP9:
return eKey_KP9;
case Common::KEYCODE_KP_PERIOD:
return eKey_KP_PERIOD;
case Common::KEYCODE_KP_DIVIDE:
return eKey_KP_DIVIDE;
case Common::KEYCODE_KP_MULTIPLY:
return eKey_KP_MULTIPLY;
case Common::KEYCODE_KP_MINUS:
return eKey_KP_MINUS;
case Common::KEYCODE_KP_PLUS:
return eKey_KP_PLUS;
case Common::KEYCODE_KP_ENTER:
return eKey_KP_ENTER;
case Common::KEYCODE_KP_EQUALS:
return eKey_KP_EQUALS;
case Common::KEYCODE_UP:
return eKey_UP;
case Common::KEYCODE_DOWN:
return eKey_DOWN;
case Common::KEYCODE_RIGHT:
return eKey_RIGHT;
case Common::KEYCODE_LEFT:
return eKey_LEFT;
case Common::KEYCODE_INSERT:
return eKey_INSERT;
case Common::KEYCODE_HOME:
return eKey_HOME;
case Common::KEYCODE_END:
return eKey_END;
case Common::KEYCODE_PAGEUP:
return eKey_PAGEUP;
case Common::KEYCODE_PAGEDOWN:
return eKey_PAGEDOWN;
case Common::KEYCODE_F1:
return eKey_F1;
case Common::KEYCODE_F2:
return eKey_F2;
case Common::KEYCODE_F3:
return eKey_F3;
case Common::KEYCODE_F4:
return eKey_F4;
case Common::KEYCODE_F5:
return eKey_F5;
case Common::KEYCODE_F6:
return eKey_F6;
case Common::KEYCODE_F7:
return eKey_F7;
case Common::KEYCODE_F8:
return eKey_F8;
case Common::KEYCODE_F9:
return eKey_F9;
case Common::KEYCODE_F10:
return eKey_F10;
case Common::KEYCODE_F11:
return eKey_F11;
case Common::KEYCODE_F12:
return eKey_F12;
case Common::KEYCODE_F13:
return eKey_F13;
case Common::KEYCODE_F14:
return eKey_F14;
case Common::KEYCODE_F15:
return eKey_F15;
case Common::KEYCODE_NUMLOCK:
return eKey_NUMLOCK;
case Common::KEYCODE_CAPSLOCK:
return eKey_CAPSLOCK;
case Common::KEYCODE_SCROLLOCK:
return eKey_SCROLLOCK;
case Common::KEYCODE_RSHIFT:
return eKey_RSHIFT;
case Common::KEYCODE_LSHIFT:
return eKey_LSHIFT;
case Common::KEYCODE_RCTRL:
return eKey_RCTRL;
case Common::KEYCODE_LCTRL:
return eKey_LCTRL;
case Common::KEYCODE_RALT:
return eKey_RALT;
case Common::KEYCODE_LALT:
return eKey_LALT;
case Common::KEYCODE_RMETA:
return eKey_RMETA;
case Common::KEYCODE_LMETA:
return eKey_LMETA;
case Common::KEYCODE_LSUPER:
return eKey_LSUPER;
case Common::KEYCODE_RSUPER:
return eKey_RSUPER;
case Common::KEYCODE_MODE:
return eKey_MODE;
case Common::KEYCODE_HELP:
return eKey_HELP;
case Common::KEYCODE_PRINT:
return eKey_PRINT;
case Common::KEYCODE_SYSREQ:
return eKey_SYSREQ;
case Common::KEYCODE_BREAK:
return eKey_BREAK;
case Common::KEYCODE_MENU:
return eKey_MENU;
case Common::KEYCODE_POWER:
return eKey_POWER;
case Common::KEYCODE_EURO:
return eKey_EURO;
}
return eKey_NONE;
}
static eKeyModifier convertModifiers(const int mods) {
eKeyModifier out = eKeyModifier_NONE;
if (mods & Common::KBD_CTRL)
out |= eKeyModifier_CTRL;
if (mods & Common::KBD_SHIFT)
out |= eKeyModifier_SHIFT;
if (mods & Common::KBD_ALT)
out |= eKeyModifier_ALT;
if (mods & Common::KBD_META)
out |= eKeyModifier_META;
return out;
}
void cKeyboardSDL::processEvent(const Common::Event &ev) {
if (ev.type != Common::EVENT_KEYDOWN && ev.type != Common::EVENT_KEYUP)
return;
eKey key = convertKey(ev.kbd.keycode);
if (ev.type == Common::EVENT_KEYDOWN) {
_downKeys.set(key);
_modifiers = convertModifiers(ev.kbd.flags);
_pressedKeys.push(cKeyPress(key, ev.kbd.ascii, _modifiers));
}
else
_downKeys.unset(key);
}
void cKeyboardSDL::Update() {
#if 0
mlstKeysPressed.clear();
std::list<SDL_Event>::iterator it = mpLowLevelInputSDL->mlstEvents.begin();
for(; it != mpLowLevelInputSDL->mlstEvents.end(); ++it)
{
SDL_Event *pEvent = &(*it);
if(pEvent->type != SDL_KEYDOWN && pEvent->type != SDL_KEYUP)
{
continue;
}
eKey key = SDLToKey(pEvent->key.keysym.sym);
mvKeyArray[key] = pEvent->type == SDL_KEYDOWN?true:false;
if(pEvent->type == SDL_KEYDOWN)
{
int sdl_mod = pEvent->key.keysym.mod;
mModifier = eKeyModifier_NONE;
if(sdl_mod & KMOD_CTRL) mModifier |= eKeyModifier_CTRL;
if(sdl_mod & KMOD_SHIFT) mModifier |= eKeyModifier_SHIFT;
if(sdl_mod & KMOD_ALT) mModifier |= eKeyModifier_ALT;
if(sdl_mod & KMOD_META) mModifier |= eKeyModifier_META;
mlstKeysPressed.push_back(cKeyPress(key,pEvent->key.keysym.unicode,mModifier));
//if(mlstKeysPressed.size()>MAX_KEY_PRESSES) mlstKeysPressed.pop_front();
}
}
#endif
_pressedKeys.clear();
for (const Common::Event &ev : _lowLevelSystem->_events)
processEvent(ev);
}
//-----------------------------------------------------------------------
bool cKeyboardSDL::KeyIsDown(eKey aKey) {
return mvKeyArray[aKey];
bool cKeyboardSDL::KeyIsDown(eKey key) {
return _downKeys.get(key);
}
//-----------------------------------------------------------------------
cKeyPress cKeyboardSDL::GetKey() {
cKeyPress key = mlstKeysPressed.front();
mlstKeysPressed.pop_front();
return key;
return _pressedKeys.pop();
}
//-----------------------------------------------------------------------
bool cKeyboardSDL::KeyIsPressed() {
return mlstKeysPressed.empty() == false;
return _pressedKeys.empty() == false;
}
//-----------------------------------------------------------------------
eKeyModifier cKeyboardSDL::GetModifier() {
return mModifier;
return _modifiers;
}
//-----------------------------------------------------------------------
@ -135,162 +386,4 @@ eKey cKeyboardSDL::StringToKey(tString) {
//-----------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
/////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------
eKey cKeyboardSDL::SDLToKey(int alKey) {
#if 0
switch(alKey)
{
case SDLK_BACKSPACE: return eKey_BACKSPACE;
case SDLK_TAB:return eKey_TAB;
case SDLK_CLEAR: return eKey_CLEAR;
case SDLK_RETURN: return eKey_RETURN;
case SDLK_PAUSE: return eKey_PAUSE;
case SDLK_ESCAPE: return eKey_ESCAPE;
case SDLK_SPACE: return eKey_SPACE;
case SDLK_EXCLAIM: return eKey_EXCLAIM;
case SDLK_QUOTEDBL: return eKey_QUOTEDBL;
case SDLK_HASH: return eKey_HASH;
case SDLK_DOLLAR: return eKey_DOLLAR;
case SDLK_AMPERSAND: return eKey_AMPERSAND;
case SDLK_QUOTE: return eKey_QUOTE;
case SDLK_LEFTPAREN: return eKey_LEFTPAREN;
case SDLK_RIGHTPAREN: return eKey_RIGHTPAREN;
case SDLK_ASTERISK: return eKey_ASTERISK;
case SDLK_PLUS: return eKey_PLUS;
case SDLK_COMMA: return eKey_COMMA;
case SDLK_MINUS: return eKey_MINUS;
case SDLK_PERIOD: return eKey_PERIOD;
case SDLK_SLASH: return eKey_SLASH;
case SDLK_0: return eKey_0;
case SDLK_1: return eKey_1;
case SDLK_2: return eKey_2;
case SDLK_3: return eKey_3;
case SDLK_4: return eKey_4;
case SDLK_5: return eKey_5;
case SDLK_6: return eKey_6;
case SDLK_7: return eKey_7;
case SDLK_8: return eKey_8;
case SDLK_9: return eKey_9;
case SDLK_COLON: return eKey_COLON;
case SDLK_SEMICOLON: return eKey_SEMICOLON;
case SDLK_LESS: return eKey_LESS;
case SDLK_EQUALS: return eKey_EQUALS;
case SDLK_GREATER: return eKey_GREATER;
case SDLK_QUESTION: return eKey_QUESTION;
case SDLK_AT: return eKey_AT;
case SDLK_LEFTBRACKET: return eKey_LEFTBRACKET;
case SDLK_BACKSLASH: return eKey_BACKSLASH;
case SDLK_RIGHTBRACKET: return eKey_RIGHTBRACKET;
case SDLK_CARET: return eKey_CARET;
case SDLK_UNDERSCORE: return eKey_UNDERSCORE;
case SDLK_BACKQUOTE: return eKey_BACKQUOTE;
case SDLK_a: return eKey_a;
case SDLK_b: return eKey_b;
case SDLK_c: return eKey_c;
case SDLK_d: return eKey_d;
case SDLK_e: return eKey_e;
case SDLK_f: return eKey_f;
case SDLK_g: return eKey_g;
case SDLK_h: return eKey_h;
case SDLK_i: return eKey_i;
case SDLK_j: return eKey_j;
case SDLK_k: return eKey_k;
case SDLK_l: return eKey_l;
case SDLK_m: return eKey_m;
case SDLK_n: return eKey_n;
case SDLK_o: return eKey_o;
case SDLK_p: return eKey_p;
case SDLK_q: return eKey_q;
case SDLK_r: return eKey_r;
case SDLK_s: return eKey_s;
case SDLK_t: return eKey_t;
case SDLK_u: return eKey_u;
case SDLK_v: return eKey_v;
case SDLK_w: return eKey_w;
case SDLK_x: return eKey_x;
case SDLK_y: return eKey_y;
case SDLK_z: return eKey_z;
case SDLK_DELETE: return eKey_DELETE;
case SDLK_KP0: return eKey_KP0;
case SDLK_KP1: return eKey_KP1;
case SDLK_KP2: return eKey_KP2;
case SDLK_KP3: return eKey_KP3;
case SDLK_KP4: return eKey_KP4;
case SDLK_KP5: return eKey_KP5;
case SDLK_KP6: return eKey_KP6;
case SDLK_KP7: return eKey_KP7;
case SDLK_KP8: return eKey_KP8;
case SDLK_KP9: return eKey_KP9;
case SDLK_KP_PERIOD: return eKey_KP_PERIOD;
case SDLK_KP_DIVIDE: return eKey_KP_DIVIDE;
case SDLK_KP_MULTIPLY: return eKey_KP_MULTIPLY;
case SDLK_KP_MINUS: return eKey_KP_MINUS;
case SDLK_KP_PLUS: return eKey_KP_PLUS;
case SDLK_KP_ENTER: return eKey_KP_ENTER;
case SDLK_KP_EQUALS: return eKey_KP_EQUALS;
case SDLK_UP: return eKey_UP;
case SDLK_DOWN: return eKey_DOWN;
case SDLK_RIGHT: return eKey_RIGHT;
case SDLK_LEFT: return eKey_LEFT;
case SDLK_INSERT: return eKey_INSERT;
case SDLK_HOME: return eKey_HOME;
case SDLK_END: return eKey_END;
case SDLK_PAGEUP: return eKey_PAGEUP;
case SDLK_PAGEDOWN: return eKey_PAGEDOWN;
case SDLK_F1: return eKey_F1;
case SDLK_F2: return eKey_F2;
case SDLK_F3: return eKey_F3;
case SDLK_F4: return eKey_F4;
case SDLK_F5: return eKey_F5;
case SDLK_F6: return eKey_F6;
case SDLK_F7: return eKey_F7;
case SDLK_F8: return eKey_F8;
case SDLK_F9: return eKey_F9;
case SDLK_F10: return eKey_F10;
case SDLK_F11: return eKey_F11;
case SDLK_F12: return eKey_F12;
case SDLK_F13: return eKey_F13;
case SDLK_F14: return eKey_F14;
case SDLK_F15: return eKey_F15;
case SDLK_NUMLOCK: return eKey_NUMLOCK;
case SDLK_CAPSLOCK: return eKey_CAPSLOCK;
case SDLK_SCROLLOCK: return eKey_SCROLLOCK;
case SDLK_RSHIFT: return eKey_RSHIFT;
case SDLK_LSHIFT: return eKey_LSHIFT;
case SDLK_RCTRL: return eKey_RCTRL;
case SDLK_LCTRL: return eKey_LCTRL;
case SDLK_RALT: return eKey_RALT;
case SDLK_LALT: return eKey_LALT;
case SDLK_RMETA: return eKey_RMETA;
case SDLK_LMETA: return eKey_LMETA;
case SDLK_LSUPER: return eKey_LSUPER;
case SDLK_RSUPER: return eKey_RSUPER;
case SDLK_MODE: return eKey_MODE;
case SDLK_HELP: return eKey_HELP;
case SDLK_PRINT: return eKey_PRINT;
case SDLK_SYSREQ: return eKey_SYSREQ;
case SDLK_BREAK: return eKey_BREAK;
case SDLK_MENU: return eKey_MENU;
case SDLK_POWER: return eKey_POWER;
case SDLK_EURO: return eKey_EURO;
}
#endif
return eKey_NONE;
}
//-----------------------------------------------------------------------
void cKeyboardSDL::ClearKeyList() {
mvKeyArray.assign(mvKeyArray.size(), false);
}
//-----------------------------------------------------------------------
} // namespace hpl

View File

@ -30,8 +30,14 @@
#include "hpl1/engine/input/Keyboard.h"
#include "hpl1/engine/system/SystemTypes.h"
#include <list>
#include <vector>
#include "common/bitarray.h"
#include "common/queue.h"
namespace Common {
class Event;
}
namespace hpl {
@ -54,16 +60,13 @@ public:
eKey StringToKey(tString);
private:
eKey SDLToKey(int alKey);
void ClearKeyList();
void processEvent(const Common::Event &ev);
eKey AsciiToKey(int alChar);
eKeyModifier mModifier;
std::vector<bool> mvKeyArray;
std::list<cKeyPress> mlstKeysPressed;
cLowLevelInputSDL *mpLowLevelInputSDL;
eKeyModifier _modifiers;
Common::BitArray _downKeys;
Common::Queue<cKeyPress> _pressedKeys;
cLowLevelInputSDL *_lowLevelSystem;
};
}; // namespace hpl