Tell irrlicht if we handle a key or not.

We can remove the function in MtNativeActivity now
as it serves precisely that purpose: to tell irrlicht
that we handled the esc key.

TODO for later:
 * Perhaps try to find a more performant container than KeyList
This commit is contained in:
est31 2016-05-14 16:25:57 +02:00
parent ef100f12a1
commit fa6b21a15b
4 changed files with 57 additions and 11 deletions

@ -19,11 +19,6 @@ public class MtNativeActivity extends NativeActivity {
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
} }
@Override
public void onBackPressed() {
}
public void copyAssets() { public void copyAssets() {
Intent intent = new Intent(this, MinetestAssetCopy.class); Intent intent = new Intent(this, MinetestAssetCopy.class);

@ -42,11 +42,15 @@ public:
// Remember whether each key is down or up // Remember whether each key is down or up
if (event.EventType == irr::EET_KEY_INPUT_EVENT) { if (event.EventType == irr::EET_KEY_INPUT_EVENT) {
if (event.KeyInput.PressedDown) { const KeyPress &keyCode = event.KeyInput;
keyIsDown.set(event.KeyInput); if (keysListenedFor[keyCode]) {
keyWasDown.set(event.KeyInput); if (event.KeyInput.PressedDown) {
} else { keyIsDown.set(keyCode);
keyIsDown.unset(event.KeyInput); keyWasDown.set(keyCode);
} else {
keyIsDown.unset(keyCode);
}
return true;
} }
} }
@ -116,6 +120,15 @@ public:
return b; return b;
} }
void listenForKey(const KeyPress &keyCode)
{
keysListenedFor.set(keyCode);
}
void dontListenForKeys()
{
keysListenedFor.clear();
}
s32 getMouseWheel() s32 getMouseWheel()
{ {
s32 a = mouse_wheel; s32 a = mouse_wheel;
@ -168,6 +181,12 @@ private:
KeyList keyIsDown; KeyList keyIsDown;
// Whether a key has been pressed or not // Whether a key has been pressed or not
KeyList keyWasDown; KeyList keyWasDown;
// List of keys we listen for
// TODO perhaps the type of this is not really
// performant as KeyList is designed for few but
// often changing keys, and keysListenedFor is expected
// to change seldomly but contain lots of keys.
KeyList keysListenedFor;
}; };
@ -192,6 +211,14 @@ public:
{ {
return m_receiver->WasKeyDown(keyCode); return m_receiver->WasKeyDown(keyCode);
} }
virtual void listenForKey(const KeyPress &keyCode)
{
m_receiver->listenForKey(keyCode);
}
virtual void dontListenForKeys()
{
m_receiver->dontListenForKeys();
}
virtual v2s32 getMousePos() virtual v2s32 getMousePos()
{ {
if (m_device->getCursorControl()) { if (m_device->getCursorControl()) {

@ -1297,7 +1297,11 @@ static void updateChat(Client &client, f32 dtime, bool show_debug,
*/ */
struct KeyCache { struct KeyCache {
KeyCache() { populate(); } KeyCache()
{
handler = NULL;
populate();
}
enum { enum {
// Player movement // Player movement
@ -1349,6 +1353,7 @@ struct KeyCache {
void populate(); void populate();
KeyPress key[KEYMAP_INTERNAL_ENUM_COUNT]; KeyPress key[KEYMAP_INTERNAL_ENUM_COUNT];
InputHandler *handler;
}; };
void KeyCache::populate() void KeyCache::populate()
@ -1399,6 +1404,19 @@ void KeyCache::populate()
key[KEYMAP_ID_QUICKTUNE_DEC] = getKeySetting("keymap_quicktune_dec"); key[KEYMAP_ID_QUICKTUNE_DEC] = getKeySetting("keymap_quicktune_dec");
key[KEYMAP_ID_DEBUG_STACKS] = getKeySetting("keymap_print_debug_stacks"); key[KEYMAP_ID_DEBUG_STACKS] = getKeySetting("keymap_print_debug_stacks");
if (handler) {
// First clear all keys, then re-add the ones we listen for
handler->dontListenForKeys();
for (size_t i = 0; i < KEYMAP_INTERNAL_ENUM_COUNT; i++) {
handler->listenForKey(key[i]);
}
handler->listenForKey(EscapeKey);
handler->listenForKey(CancelKey);
for (size_t i = 0; i < 10; i++) {
handler->listenForKey(NumberKey[i]);
}
}
} }
@ -1829,6 +1847,9 @@ bool Game::startup(bool *kill,
this->chat_backend = chat_backend; this->chat_backend = chat_backend;
this->simple_singleplayer_mode = simple_singleplayer_mode; this->simple_singleplayer_mode = simple_singleplayer_mode;
keycache.handler = input;
keycache.populate();
driver = device->getVideoDriver(); driver = device->getVideoDriver();
smgr = device->getSceneManager(); smgr = device->getSceneManager();

@ -110,6 +110,9 @@ public:
virtual bool isKeyDown(const KeyPress &keyCode) = 0; virtual bool isKeyDown(const KeyPress &keyCode) = 0;
virtual bool wasKeyDown(const KeyPress &keyCode) = 0; virtual bool wasKeyDown(const KeyPress &keyCode) = 0;
virtual void listenForKey(const KeyPress &keyCode) {}
virtual void dontListenForKeys() {}
virtual v2s32 getMousePos() = 0; virtual v2s32 getMousePos() = 0;
virtual void setMousePos(s32 x, s32 y) = 0; virtual void setMousePos(s32 x, s32 y) = 0;