Overhaul the input system

This allows us to map the keys which are not considered in
irrlicht's EKEY_CODE system, such as \, [, /, ] etc.
This commit is contained in:
Giuseppe Bilotta 2011-08-13 22:44:31 +02:00
parent 16aedc0ef6
commit 7e610aece5
8 changed files with 329 additions and 176 deletions

@ -28,7 +28,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "materials.h" #include "materials.h"
#include "config.h" #include "config.h"
#include "clouds.h" #include "clouds.h"
#include "keycode.h"
#include "farmesh.h" #include "farmesh.h"
#include "mapblock.h" #include "mapblock.h"
@ -1314,7 +1313,7 @@ void the_game(
menu->drop(); menu->drop();
} }
else if(input->wasKeyDown(KEY_ESCAPE)) else if(input->wasKeyDown(EscapeKey))
{ {
dstream<<DTIME<<"the_game: " dstream<<DTIME<<"the_game: "
<<"Launching pause menu"<<std::endl; <<"Launching pause menu"<<std::endl;
@ -1417,10 +1416,8 @@ void the_game(
// Item selection // Item selection
for(u16 i=0; i<10; i++) for(u16 i=0; i<10; i++)
{ {
s32 keycode = irr::KEY_KEY_1 + i; const KeyPress *kp = NumberKey + (i + 1) % 10;
if(i == 9) if(input->wasKeyDown(*kp))
keycode = irr::KEY_KEY_0;
if(input->wasKeyDown((irr::EKEY_CODE)keycode))
{ {
if(i < PLAYER_INVENTORY_SIZE && i < hotbar_itemcount) if(i < PLAYER_INVENTORY_SIZE && i < hotbar_itemcount)
{ {

@ -23,6 +23,69 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "common_irrlicht.h" #include "common_irrlicht.h"
#include <string> #include <string>
#include "keycode.h"
class KeyList : protected core::list<KeyPress>
{
typedef core::list<KeyPress> super;
typedef super::Iterator Iterator;
typedef super::ConstIterator ConstIterator;
virtual ConstIterator find(const KeyPress &key) const
{
ConstIterator f(begin());
ConstIterator e(end());
while (f!=e) {
if (*f == key)
return f;
++f;
}
return e;
}
virtual Iterator find(const KeyPress &key)
{
Iterator f(begin());
Iterator e(end());
while (f!=e) {
if (*f == key)
return f;
++f;
}
return e;
}
public:
void clear() { super::clear(); }
void set(const KeyPress &key)
{
if (find(key) == end())
push_back(key);
}
void unset(const KeyPress &key)
{
Iterator p(find(key));
if (p != end())
erase(p);
}
void toggle(const KeyPress &key)
{
Iterator p(this->find(key));
if (p != end())
erase(p);
else
push_back(key);
}
bool operator[](const KeyPress &key) const
{
return find(key) != end();
}
};
class InputHandler class InputHandler
{ {
public: public:
@ -33,8 +96,8 @@ public:
{ {
} }
virtual bool isKeyDown(EKEY_CODE keyCode) = 0; virtual bool isKeyDown(const KeyPress &keyCode) = 0;
virtual bool wasKeyDown(EKEY_CODE keyCode) = 0; virtual bool wasKeyDown(const KeyPress &keyCode) = 0;
virtual v2s32 getMousePos() = 0; virtual v2s32 getMousePos() = 0;
virtual void setMousePos(s32 x, s32 y) = 0; virtual void setMousePos(s32 x, s32 y) = 0;

@ -290,12 +290,9 @@ bool GUIInventoryMenu::OnEvent(const SEvent& event)
{ {
if(event.EventType==EET_KEY_INPUT_EVENT) if(event.EventType==EET_KEY_INPUT_EVENT)
{ {
if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown) KeyPress kp(event.KeyInput);
{ if (event.KeyInput.PressedDown && (kp == EscapeKey ||
quitMenu(); kp == getKeySetting("keymap_inventory")))
return true;
}
if(event.KeyInput.Key==getKeySetting("keymap_inventory") && event.KeyInput.PressedDown)
{ {
quitMenu(); quitMenu();
return true; return true;

@ -22,38 +22,9 @@
#include "guiKeyChangeMenu.h" #include "guiKeyChangeMenu.h"
#include "debug.h" #include "debug.h"
#include "serialization.h" #include "serialization.h"
#include "keycode.h"
#include "main.h" #include "main.h"
#include <string> #include <string>
static const char *KeyNamesLang[] =
{ "-", N_("Left Button"), N_("Right Button"), N_("Cancel"), N_("Middle Button"), N_("X Button 1"),
N_("X Button 2"), "-", N_("Back"), N_("Tab"), "-", "-", N_("Clear"), N_("Return"), "-",
"-", N_("Shift"), N_("Control"), N_("Menu"), N_("Pause"), N_("Capital"), N_("Kana"), "-",
N_("Junja"), N_("Final"), N_("Kanji"), "-", N_("Escape"), N_("Convert"), N_("Nonconvert"),
N_("Accept"), N_("Mode Change"), N_("Space"), N_("Prior"), N_("Next"), N_("End"), N_("Home"),
N_("Left"), N_("Up"), N_("Right"), N_("Down"), N_("Select"), N_("Print"), N_("Execute"),
N_("Snapshot"), N_("Insert"), N_("Delete"), N_("Help"), "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "-", "-", "-", "-", "-", "-", "-", "A", "B", "C",
"D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q",
"R", "S", "T", "U", "V", "W", "X", "Y", "Z", N_("Left Windows"),
N_("Right Windows"), N_("Apps"), "-", N_("Sleep"), N_("Numpad 0"), N_("Numpad 1"),
N_("Numpad 2"), N_("Numpad 3"), N_("Numpad 4"), N_("Numpad 5"), N_("Numpad 6"), N_("Numpad 7"),
N_("Numpad 8"), N_("Numpad 9"), N_("Numpad *"), N_("Numpad +"), N_("Numpad /"), N_("Numpad -"),
"Numpad .", "Numpad /", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8",
"F9", "F10", "F11", "F12", "F13", "F14", "F15", "F16", "F17", "F18",
"F19", "F20", "F21", "F22", "F23", "F24", "-", "-", "-", "-", "-", "-",
"-", "-", N_("Num Lock"), N_("Scroll Lock"), "-", "-", "-", "-", "-", "-", "-",
"-", "-", "-", "-", "-", "-", "-", N_("Left Shift"), N_("Right Shift"),
N_("Left Control"), N_("Right Control"), N_("Left Menu"), N_("Right Menu"), "-", "-",
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-",
"-", "-", "-", "-", "-", N_("Plus"), N_("Comma"), N_("Minus"), N_("Period"), "-", "-",
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-",
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-",
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-",
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", N_("Attn"), N_("CrSel"),
N_("ExSel"), N_("Erase OEF"), N_("Play"), N_("Zoom"), N_("PA1"), N_("OEM Clear"), "-" };
GUIKeyChangeMenu::GUIKeyChangeMenu(gui::IGUIEnvironment* env, GUIKeyChangeMenu::GUIKeyChangeMenu(gui::IGUIEnvironment* env,
gui::IGUIElement* parent, s32 id, IMenuManager *menumgr) : gui::IGUIElement* parent, s32 id, IMenuManager *menumgr) :
GUIModalMenu(env, parent, id, menumgr) GUIModalMenu(env, parent, id, menumgr)
@ -129,7 +100,7 @@ void GUIKeyChangeMenu::regenerateGui(v2u32 screensize)
rect += topleft + v2s32(offset.X + 105, offset.Y - 5); rect += topleft + v2s32(offset.X + 105, offset.Y - 5);
this->forward = Environment->addButton(rect, this, this->forward = Environment->addButton(rect, this,
GUI_ID_KEY_FORWARD_BUTTON, GUI_ID_KEY_FORWARD_BUTTON,
wgettext(KeyNamesLang[key_forward])); wgettext(key_forward.name()));
} }
offset += v2s32(0, 25); offset += v2s32(0, 25);
@ -146,7 +117,7 @@ void GUIKeyChangeMenu::regenerateGui(v2u32 screensize)
rect += topleft + v2s32(offset.X + 105, offset.Y - 5); rect += topleft + v2s32(offset.X + 105, offset.Y - 5);
this->backward = Environment->addButton(rect, this, this->backward = Environment->addButton(rect, this,
GUI_ID_KEY_BACKWARD_BUTTON, GUI_ID_KEY_BACKWARD_BUTTON,
wgettext(KeyNamesLang[key_backward])); wgettext(key_backward.name()));
} }
offset += v2s32(0, 25); offset += v2s32(0, 25);
{ {
@ -161,7 +132,7 @@ void GUIKeyChangeMenu::regenerateGui(v2u32 screensize)
core::rect < s32 > rect(0, 0, 100, 30); core::rect < s32 > rect(0, 0, 100, 30);
rect += topleft + v2s32(offset.X + 105, offset.Y - 5); rect += topleft + v2s32(offset.X + 105, offset.Y - 5);
this->left = Environment->addButton(rect, this, GUI_ID_KEY_LEFT_BUTTON, this->left = Environment->addButton(rect, this, GUI_ID_KEY_LEFT_BUTTON,
wgettext(KeyNamesLang[key_left])); wgettext(key_left.name()));
} }
offset += v2s32(0, 25); offset += v2s32(0, 25);
{ {
@ -177,7 +148,7 @@ void GUIKeyChangeMenu::regenerateGui(v2u32 screensize)
rect += topleft + v2s32(offset.X + 105, offset.Y - 5); rect += topleft + v2s32(offset.X + 105, offset.Y - 5);
this->right = Environment->addButton(rect, this, this->right = Environment->addButton(rect, this,
GUI_ID_KEY_RIGHT_BUTTON, GUI_ID_KEY_RIGHT_BUTTON,
wgettext(KeyNamesLang[key_right])); wgettext(key_right.name()));
} }
offset += v2s32(0, 25); offset += v2s32(0, 25);
{ {
@ -192,7 +163,7 @@ void GUIKeyChangeMenu::regenerateGui(v2u32 screensize)
core::rect < s32 > rect(0, 0, 100, 30); core::rect < s32 > rect(0, 0, 100, 30);
rect += topleft + v2s32(offset.X + 105, offset.Y - 5); rect += topleft + v2s32(offset.X + 105, offset.Y - 5);
this->use = Environment->addButton(rect, this, GUI_ID_KEY_USE_BUTTON, this->use = Environment->addButton(rect, this, GUI_ID_KEY_USE_BUTTON,
wgettext(KeyNamesLang[key_use])); wgettext(key_use.name()));
} }
offset += v2s32(0, 25); offset += v2s32(0, 25);
{ {
@ -208,7 +179,7 @@ void GUIKeyChangeMenu::regenerateGui(v2u32 screensize)
rect += topleft + v2s32(offset.X + 105, offset.Y - 5); rect += topleft + v2s32(offset.X + 105, offset.Y - 5);
this->sneak = Environment->addButton(rect, this, this->sneak = Environment->addButton(rect, this,
GUI_ID_KEY_SNEAK_BUTTON, GUI_ID_KEY_SNEAK_BUTTON,
wgettext(KeyNamesLang[key_sneak])); wgettext(key_sneak.name()));
} }
offset += v2s32(0, 25); offset += v2s32(0, 25);
{ {
@ -222,7 +193,7 @@ void GUIKeyChangeMenu::regenerateGui(v2u32 screensize)
core::rect < s32 > rect(0, 0, 100, 30); core::rect < s32 > rect(0, 0, 100, 30);
rect += topleft + v2s32(offset.X + 105, offset.Y - 5); rect += topleft + v2s32(offset.X + 105, offset.Y - 5);
this->jump = Environment->addButton(rect, this, GUI_ID_KEY_JUMP_BUTTON, this->jump = Environment->addButton(rect, this, GUI_ID_KEY_JUMP_BUTTON,
wgettext(KeyNamesLang[key_jump])); wgettext(key_jump.name()));
} }
offset += v2s32(0, 25); offset += v2s32(0, 25);
@ -239,7 +210,7 @@ void GUIKeyChangeMenu::regenerateGui(v2u32 screensize)
rect += topleft + v2s32(offset.X + 105, offset.Y - 5); rect += topleft + v2s32(offset.X + 105, offset.Y - 5);
this->inventory = Environment->addButton(rect, this, this->inventory = Environment->addButton(rect, this,
GUI_ID_KEY_INVENTORY_BUTTON, GUI_ID_KEY_INVENTORY_BUTTON,
wgettext(KeyNamesLang[key_inventory])); wgettext(key_inventory.name()));
} }
offset += v2s32(0, 25); offset += v2s32(0, 25);
{ {
@ -253,7 +224,7 @@ void GUIKeyChangeMenu::regenerateGui(v2u32 screensize)
core::rect < s32 > rect(0, 0, 100, 30); core::rect < s32 > rect(0, 0, 100, 30);
rect += topleft + v2s32(offset.X + 105, offset.Y - 5); rect += topleft + v2s32(offset.X + 105, offset.Y - 5);
this->chat = Environment->addButton(rect, this, GUI_ID_KEY_CHAT_BUTTON, this->chat = Environment->addButton(rect, this, GUI_ID_KEY_CHAT_BUTTON,
wgettext(KeyNamesLang[key_chat])); wgettext(key_chat.name()));
} }
//next col //next col
@ -270,7 +241,7 @@ void GUIKeyChangeMenu::regenerateGui(v2u32 screensize)
core::rect < s32 > rect(0, 0, 100, 30); core::rect < s32 > rect(0, 0, 100, 30);
rect += topleft + v2s32(offset.X + 105, offset.Y - 5); rect += topleft + v2s32(offset.X + 105, offset.Y - 5);
this->fly = Environment->addButton(rect, this, GUI_ID_KEY_FLY_BUTTON, this->fly = Environment->addButton(rect, this, GUI_ID_KEY_FLY_BUTTON,
wgettext(KeyNamesLang[key_fly])); wgettext(key_fly.name()));
} }
offset += v2s32(0, 25); offset += v2s32(0, 25);
{ {
@ -285,7 +256,7 @@ void GUIKeyChangeMenu::regenerateGui(v2u32 screensize)
core::rect < s32 > rect(0, 0, 100, 30); core::rect < s32 > rect(0, 0, 100, 30);
rect += topleft + v2s32(offset.X + 105, offset.Y - 5); rect += topleft + v2s32(offset.X + 105, offset.Y - 5);
this->fast = Environment->addButton(rect, this, GUI_ID_KEY_FAST_BUTTON, this->fast = Environment->addButton(rect, this, GUI_ID_KEY_FAST_BUTTON,
wgettext(KeyNamesLang[key_fast])); wgettext(key_fast.name()));
} }
offset += v2s32(0, 25); offset += v2s32(0, 25);
{ {
@ -301,7 +272,7 @@ void GUIKeyChangeMenu::regenerateGui(v2u32 screensize)
rect += topleft + v2s32(offset.X + 105, offset.Y - 5); rect += topleft + v2s32(offset.X + 105, offset.Y - 5);
this->range = Environment->addButton(rect, this, this->range = Environment->addButton(rect, this,
GUI_ID_KEY_RANGE_BUTTON, GUI_ID_KEY_RANGE_BUTTON,
wgettext(KeyNamesLang[key_range])); wgettext(key_range.name()));
} }
offset += v2s32(0, 25); offset += v2s32(0, 25);
@ -317,7 +288,7 @@ void GUIKeyChangeMenu::regenerateGui(v2u32 screensize)
core::rect < s32 > rect(0, 0, 100, 30); core::rect < s32 > rect(0, 0, 100, 30);
rect += topleft + v2s32(offset.X + 105, offset.Y - 5); rect += topleft + v2s32(offset.X + 105, offset.Y - 5);
this->dump = Environment->addButton(rect, this, GUI_ID_KEY_DUMP_BUTTON, this->dump = Environment->addButton(rect, this, GUI_ID_KEY_DUMP_BUTTON,
wgettext(KeyNamesLang[key_dump])); wgettext(key_dump.name()));
} }
{ {
core::rect < s32 > rect(0, 0, 100, 30); core::rect < s32 > rect(0, 0, 100, 30);
@ -354,19 +325,19 @@ void GUIKeyChangeMenu::drawMenu()
bool GUIKeyChangeMenu::acceptInput() bool GUIKeyChangeMenu::acceptInput()
{ {
g_settings.set("keymap_forward", keycode_to_keyname(key_forward)); g_settings.set("keymap_forward", key_forward.sym());
g_settings.set("keymap_backward", keycode_to_keyname(key_backward)); g_settings.set("keymap_backward", key_backward.sym());
g_settings.set("keymap_left", keycode_to_keyname(key_left)); g_settings.set("keymap_left", key_left.sym());
g_settings.set("keymap_right", keycode_to_keyname(key_right)); g_settings.set("keymap_right", key_right.sym());
g_settings.set("keymap_jump", keycode_to_keyname(key_jump)); g_settings.set("keymap_jump", key_jump.sym());
g_settings.set("keymap_sneak", keycode_to_keyname(key_sneak)); g_settings.set("keymap_sneak", key_sneak.sym());
g_settings.set("keymap_inventory", keycode_to_keyname(key_inventory)); g_settings.set("keymap_inventory", key_inventory.sym());
g_settings.set("keymap_chat", keycode_to_keyname(key_chat)); g_settings.set("keymap_chat", key_chat.sym());
g_settings.set("keymap_rangeselect", keycode_to_keyname(key_range)); g_settings.set("keymap_rangeselect", key_range.sym());
g_settings.set("keymap_freemove", keycode_to_keyname(key_fly)); g_settings.set("keymap_freemove", key_fly.sym());
g_settings.set("keymap_fastmove", keycode_to_keyname(key_fast)); g_settings.set("keymap_fastmove", key_fast.sym());
g_settings.set("keymap_special1", keycode_to_keyname(key_use)); g_settings.set("keymap_special1", key_use.sym());
g_settings.set("keymap_print_debug_stacks", keycode_to_keyname(key_dump)); g_settings.set("keymap_print_debug_stacks", key_dump.sym());
clearKeyCache(); clearKeyCache();
return true; return true;
} }
@ -395,45 +366,45 @@ bool GUIKeyChangeMenu::resetMenu()
{ {
case GUI_ID_KEY_FORWARD_BUTTON: case GUI_ID_KEY_FORWARD_BUTTON:
this->forward->setText( this->forward->setText(
wgettext(KeyNamesLang[key_forward])); wgettext(key_forward.name()));
break; break;
case GUI_ID_KEY_BACKWARD_BUTTON: case GUI_ID_KEY_BACKWARD_BUTTON:
this->backward->setText( this->backward->setText(
wgettext(KeyNamesLang[key_backward])); wgettext(key_backward.name()));
break; break;
case GUI_ID_KEY_LEFT_BUTTON: case GUI_ID_KEY_LEFT_BUTTON:
this->left->setText(wgettext(KeyNamesLang[key_left])); this->left->setText(wgettext(key_left.name()));
break; break;
case GUI_ID_KEY_RIGHT_BUTTON: case GUI_ID_KEY_RIGHT_BUTTON:
this->right->setText(wgettext(KeyNamesLang[key_right])); this->right->setText(wgettext(key_right.name()));
break; break;
case GUI_ID_KEY_JUMP_BUTTON: case GUI_ID_KEY_JUMP_BUTTON:
this->jump->setText(wgettext(KeyNamesLang[key_jump])); this->jump->setText(wgettext(key_jump.name()));
break; break;
case GUI_ID_KEY_SNEAK_BUTTON: case GUI_ID_KEY_SNEAK_BUTTON:
this->sneak->setText(wgettext(KeyNamesLang[key_sneak])); this->sneak->setText(wgettext(key_sneak.name()));
break; break;
case GUI_ID_KEY_INVENTORY_BUTTON: case GUI_ID_KEY_INVENTORY_BUTTON:
this->inventory->setText( this->inventory->setText(
wgettext(KeyNamesLang[key_inventory])); wgettext(key_inventory.name()));
break; break;
case GUI_ID_KEY_CHAT_BUTTON: case GUI_ID_KEY_CHAT_BUTTON:
this->chat->setText(wgettext(KeyNamesLang[key_chat])); this->chat->setText(wgettext(key_chat.name()));
break; break;
case GUI_ID_KEY_RANGE_BUTTON: case GUI_ID_KEY_RANGE_BUTTON:
this->range->setText(wgettext(KeyNamesLang[key_range])); this->range->setText(wgettext(key_range.name()));
break; break;
case GUI_ID_KEY_FLY_BUTTON: case GUI_ID_KEY_FLY_BUTTON:
this->fly->setText(wgettext(KeyNamesLang[key_fly])); this->fly->setText(wgettext(key_fly.name()));
break; break;
case GUI_ID_KEY_FAST_BUTTON: case GUI_ID_KEY_FAST_BUTTON:
this->fast->setText(wgettext(KeyNamesLang[key_fast])); this->fast->setText(wgettext(key_fast.name()));
break; break;
case GUI_ID_KEY_USE_BUTTON: case GUI_ID_KEY_USE_BUTTON:
this->use->setText(wgettext(KeyNamesLang[key_use])); this->use->setText(wgettext(key_use.name()));
break; break;
case GUI_ID_KEY_DUMP_BUTTON: case GUI_ID_KEY_DUMP_BUTTON:
this->dump->setText(wgettext(KeyNamesLang[key_dump])); this->dump->setText(wgettext(key_dump.name()));
break; break;
} }
activeKey = -1; activeKey = -1;
@ -447,83 +418,72 @@ bool GUIKeyChangeMenu::OnEvent(const SEvent& event)
&& event.KeyInput.PressedDown) && event.KeyInput.PressedDown)
{ {
changeCtype(""); changeCtype("");
KeyPress kp(event.KeyInput);
if (activeKey == GUI_ID_KEY_FORWARD_BUTTON) if (activeKey == GUI_ID_KEY_FORWARD_BUTTON)
{ {
this->forward->setText( this->forward->setText(wgettext(kp.name()));
wgettext(KeyNamesLang[event.KeyInput.Key])); this->key_forward = kp;
this->key_forward = event.KeyInput.Key;
} }
else if (activeKey == GUI_ID_KEY_BACKWARD_BUTTON) else if (activeKey == GUI_ID_KEY_BACKWARD_BUTTON)
{ {
this->backward->setText( this->backward->setText(wgettext(kp.name()));
wgettext(KeyNamesLang[event.KeyInput.Key])); this->key_backward = kp;
this->key_backward = event.KeyInput.Key;
} }
else if (activeKey == GUI_ID_KEY_LEFT_BUTTON) else if (activeKey == GUI_ID_KEY_LEFT_BUTTON)
{ {
this->left->setText( this->left->setText(wgettext(kp.name()));
wgettext(KeyNamesLang[event.KeyInput.Key])); this->key_left = kp;
this->key_left = event.KeyInput.Key;
} }
else if (activeKey == GUI_ID_KEY_RIGHT_BUTTON) else if (activeKey == GUI_ID_KEY_RIGHT_BUTTON)
{ {
this->right->setText( this->right->setText(wgettext(kp.name()));
wgettext(KeyNamesLang[event.KeyInput.Key])); this->key_right = kp;
this->key_right = event.KeyInput.Key;
} }
else if (activeKey == GUI_ID_KEY_JUMP_BUTTON) else if (activeKey == GUI_ID_KEY_JUMP_BUTTON)
{ {
this->jump->setText( this->jump->setText(wgettext(kp.name()));
wgettext(KeyNamesLang[event.KeyInput.Key])); this->key_jump = kp;
this->key_jump = event.KeyInput.Key;
} }
else if (activeKey == GUI_ID_KEY_SNEAK_BUTTON) else if (activeKey == GUI_ID_KEY_SNEAK_BUTTON)
{ {
this->sneak->setText( this->sneak->setText(wgettext(kp.name()));
wgettext(KeyNamesLang[event.KeyInput.Key])); this->key_sneak = kp;
this->key_sneak = event.KeyInput.Key;
} }
else if (activeKey == GUI_ID_KEY_INVENTORY_BUTTON) else if (activeKey == GUI_ID_KEY_INVENTORY_BUTTON)
{ {
this->inventory->setText( this->inventory->setText(wgettext(kp.name()));
wgettext(KeyNamesLang[event.KeyInput.Key])); this->key_inventory = kp;
this->key_inventory = event.KeyInput.Key;
} }
else if (activeKey == GUI_ID_KEY_CHAT_BUTTON) else if (activeKey == GUI_ID_KEY_CHAT_BUTTON)
{ {
this->chat->setText( this->chat->setText(wgettext(kp.name()));
wgettext(KeyNamesLang[event.KeyInput.Key])); this->key_chat = kp;
this->key_chat = event.KeyInput.Key;
} }
else if (activeKey == GUI_ID_KEY_RANGE_BUTTON) else if (activeKey == GUI_ID_KEY_RANGE_BUTTON)
{ {
this->range->setText( this->range->setText(wgettext(kp.name()));
wgettext(KeyNamesLang[event.KeyInput.Key])); this->key_range = kp;
this->key_range = event.KeyInput.Key;
} }
else if (activeKey == GUI_ID_KEY_FLY_BUTTON) else if (activeKey == GUI_ID_KEY_FLY_BUTTON)
{ {
this->fly->setText( this->fly->setText(wgettext(kp.name()));
wgettext(KeyNamesLang[event.KeyInput.Key])); this->key_fly = kp;
this->key_fly = event.KeyInput.Key;
} }
else if (activeKey == GUI_ID_KEY_FAST_BUTTON) else if (activeKey == GUI_ID_KEY_FAST_BUTTON)
{ {
this->fast->setText( this->fast->setText(wgettext(kp.name()));
wgettext(KeyNamesLang[event.KeyInput.Key])); this->key_fast = kp;
this->key_fast = event.KeyInput.Key;
} }
else if (activeKey == GUI_ID_KEY_USE_BUTTON) else if (activeKey == GUI_ID_KEY_USE_BUTTON)
{ {
this->use->setText( this->use->setText(wgettext(kp.name()));
wgettext(KeyNamesLang[event.KeyInput.Key])); this->key_use = kp;
this->key_use = event.KeyInput.Key;
} }
else if (activeKey == GUI_ID_KEY_DUMP_BUTTON) else if (activeKey == GUI_ID_KEY_DUMP_BUTTON)
{ {
this->dump->setText( this->dump->setText(wgettext(kp.name()));
wgettext(KeyNamesLang[event.KeyInput.Key])); this->key_dump = kp;
this->key_dump = event.KeyInput.Key;
} }
changeCtype("C"); changeCtype("C");
activeKey = -1; activeKey = -1;

@ -27,6 +27,7 @@
#include "modalMenu.h" #include "modalMenu.h"
#include "client.h" #include "client.h"
#include "gettext.h" #include "gettext.h"
#include "keycode.h"
#include <string> #include <string>
enum enum
@ -88,19 +89,19 @@ private:
gui::IGUIButton *chat; gui::IGUIButton *chat;
s32 activeKey; s32 activeKey;
s32 key_forward; KeyPress key_forward;
s32 key_backward; KeyPress key_backward;
s32 key_left; KeyPress key_left;
s32 key_right; KeyPress key_right;
s32 key_use; KeyPress key_use;
s32 key_sneak; KeyPress key_sneak;
s32 key_jump; KeyPress key_jump;
s32 key_inventory; KeyPress key_inventory;
s32 key_fly; KeyPress key_fly;
s32 key_fast; KeyPress key_fast;
s32 key_range; KeyPress key_range;
s32 key_chat; KeyPress key_chat;
s32 key_dump; KeyPress key_dump;
}; };
#endif #endif

@ -19,6 +19,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "keycode.h" #include "keycode.h"
#include "main.h" // For g_settings #include "main.h" // For g_settings
#include "exceptions.h"
class UnknownKeycode : public BaseException
{
public:
UnknownKeycode(const char *s) :
BaseException(s) {};
};
#define CHECKKEY(x){if(strcmp(name, #x)==0) return irr::x;} #define CHECKKEY(x){if(strcmp(name, #x)==0) return irr::x;}
@ -168,7 +176,7 @@ irr::EKEY_CODE keyname_to_keycode(const char *name)
CHECKKEY(KEY_PA1) CHECKKEY(KEY_PA1)
CHECKKEY(KEY_OEM_CLEAR) CHECKKEY(KEY_OEM_CLEAR)
return irr::KEY_KEY_CODES_COUNT; throw UnknownKeycode(name);
} }
static const char *KeyNames[] = static const char *KeyNames[] =
@ -206,27 +214,128 @@ static const char *KeyNames[] =
"-", "-", "-", "-", "-", "-", "-", "-", "Attn", "CrSel", "ExSel", "-", "-", "-", "-", "-", "-", "-", "-", "Attn", "CrSel", "ExSel",
"Erase OEF", "Play", "Zoom", "PA1", "OEM Clear", "-" }; "Erase OEF", "Play", "Zoom", "PA1", "OEM Clear", "-" };
const char *keycode_to_keyname(s32 keycode) #define N_(text) text
static const char *KeyNamesLang[] =
{ "-", N_("Left Button"), N_("Right Button"), N_("Cancel"), N_("Middle Button"), N_("X Button 1"),
N_("X Button 2"), "-", N_("Back"), N_("Tab"), "-", "-", N_("Clear"), N_("Return"), "-",
"-", N_("Shift"), N_("Control"), N_("Menu"), N_("Pause"), N_("Capital"), N_("Kana"), "-",
N_("Junja"), N_("Final"), N_("Kanji"), "-", N_("Escape"), N_("Convert"), N_("Nonconvert"),
N_("Accept"), N_("Mode Change"), N_("Space"), N_("Prior"), N_("Next"), N_("End"), N_("Home"),
N_("Left"), N_("Up"), N_("Right"), N_("Down"), N_("Select"), N_("Print"), N_("Execute"),
N_("Snapshot"), N_("Insert"), N_("Delete"), N_("Help"), "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "-", "-", "-", "-", "-", "-", "-", "A", "B", "C",
"D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q",
"R", "S", "T", "U", "V", "W", "X", "Y", "Z", N_("Left Windows"),
N_("Right Windows"), N_("Apps"), "-", N_("Sleep"), N_("Numpad 0"), N_("Numpad 1"),
N_("Numpad 2"), N_("Numpad 3"), N_("Numpad 4"), N_("Numpad 5"), N_("Numpad 6"), N_("Numpad 7"),
N_("Numpad 8"), N_("Numpad 9"), N_("Numpad *"), N_("Numpad +"), N_("Numpad /"), N_("Numpad -"),
"Numpad .", "Numpad /", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8",
"F9", "F10", "F11", "F12", "F13", "F14", "F15", "F16", "F17", "F18",
"F19", "F20", "F21", "F22", "F23", "F24", "-", "-", "-", "-", "-", "-",
"-", "-", N_("Num Lock"), N_("Scroll Lock"), "-", "-", "-", "-", "-", "-", "-",
"-", "-", "-", "-", "-", "-", "-", N_("Left Shift"), N_("Right Shift"),
N_("Left Control"), N_("Right Control"), N_("Left Menu"), N_("Right Menu"), "-", "-",
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-",
"-", "-", "-", "-", "-", N_("Plus"), N_("Comma"), N_("Minus"), N_("Period"), "-", "-",
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-",
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-",
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-",
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", N_("Attn"), N_("CrSel"),
N_("ExSel"), N_("Erase OEF"), N_("Play"), N_("Zoom"), N_("PA1"), N_("OEM Clear"), "-" };
#undef N_
KeyPress::KeyPress() :
Key(irr::KEY_KEY_CODES_COUNT),
Char(L'\0')
{}
KeyPress::KeyPress(const char *name)
{ {
return KeyNames[keycode]; if (strlen(name) > 4) {
try {
Key = keyname_to_keycode(name);
m_name = name;
if (strlen(name) > 8)
mbtowc(&Char, name + 8, 1);
else
Char = L'\0';
return;
} catch (UnknownKeycode &e) {};
} else {
// see if we can set it up as a KEY_KEY_something
m_name = "KEY_KEY_";
m_name += name;
try {
Key = keyname_to_keycode(m_name.c_str());
mbtowc(&Char, name, 1);
return;
} catch (UnknownKeycode &e) {};
} }
// it's not a (known) key, just take the first char and use that
Key = irr::KEY_KEY_CODES_COUNT;
mbtowc(&Char, name, 1);
m_name = name[0];
}
KeyPress::KeyPress(const irr::SEvent::SKeyInput &in)
{
Key = in.Key;
Char = in.Char;
if (valid_kcode(Key)) {
m_name = KeyNames[Key];
} else {
size_t maxlen = wctomb(NULL, Char);
m_name.resize(maxlen+1, '\0');
wctomb(&m_name[0], Char);
}
}
const char *KeyPress::sym() const
{
if (Key && Key < irr::KEY_KEY_CODES_COUNT)
return KeyNames[Key];
else {
return m_name.c_str();
}
}
const char *KeyPress::name() const
{
if (Key && Key < irr::KEY_KEY_CODES_COUNT)
return KeyNamesLang[Key];
else {
return m_name.c_str();
}
}
const KeyPress EscapeKey("KEY_ESCAPE");
const KeyPress NumberKey[] = {
KeyPress("KEY_KEY_0"), KeyPress("KEY_KEY_1"), KeyPress("KEY_KEY_2"),
KeyPress("KEY_KEY_3"), KeyPress("KEY_KEY_4"), KeyPress("KEY_KEY_5"),
KeyPress("KEY_KEY_6"), KeyPress("KEY_KEY_7"), KeyPress("KEY_KEY_8"),
KeyPress("KEY_KEY_9")};
/* /*
Key config Key config
*/ */
// A simple cache for quicker lookup // A simple cache for quicker lookup
core::map<std::string, irr::EKEY_CODE> g_key_setting_cache; core::map<std::string, KeyPress> g_key_setting_cache;
irr::EKEY_CODE getKeySetting(const char *settingname) KeyPress getKeySetting(const char *settingname)
{ {
core::map<std::string, irr::EKEY_CODE>::Node *n; core::map<std::string, KeyPress>::Node *n;
n = g_key_setting_cache.find(settingname); n = g_key_setting_cache.find(settingname);
if(n) if(n)
return n->getValue(); return n->getValue();
irr::EKEY_CODE c = keyname_to_keycode(g_settings.get(settingname).c_str()); g_key_setting_cache.insert(settingname,
g_key_setting_cache.insert(settingname, c); g_settings.get(settingname).c_str());
return c; return g_key_setting_cache.find(settingname)->getValue();
} }
void clearKeyCache() void clearKeyCache()

@ -23,11 +23,42 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "common_irrlicht.h" #include "common_irrlicht.h"
#include <string> #include <string>
irr::EKEY_CODE keyname_to_keycode(const char *name); /* A key press, consisting of either an Irrlicht keycode
const char *keycode_to_keyname(s32 keycode); or an actual char */
class KeyPress
{
public:
KeyPress();
KeyPress(const char *name);
KeyPress(const irr::SEvent::SKeyInput &in);
bool operator==(const KeyPress &o) const
{
return valid_kcode(Key) ? Key == o.Key : Char == o.Char;
}
const char *sym() const;
const char *name() const;
std::string debug() const;
protected:
static bool valid_kcode(irr::EKEY_CODE k)
{
return k > 0 && k < irr::KEY_KEY_CODES_COUNT;
}
irr::EKEY_CODE Key;
wchar_t Char;
std::string m_name;
};
extern const KeyPress EscapeKey;
extern const KeyPress NumberKey[10];
// Key configuration getter // Key configuration getter
irr::EKEY_CODE getKeySetting(const char *settingname); KeyPress getKeySetting(const char *settingname);
// Clear fast lookup cache // Clear fast lookup cache
void clearKeyCache(); void clearKeyCache();

@ -563,10 +563,12 @@ 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)
{ {
keyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown; if(event.KeyInput.PressedDown) {
keyIsDown.set(event.KeyInput);
if(event.KeyInput.PressedDown) keyWasDown.set(event.KeyInput);
keyWasDown[event.KeyInput.Key] = true; } else {
keyIsDown.unset(event.KeyInput);
}
} }
if(event.EventType == irr::EET_MOUSE_INPUT_EVENT) if(event.EventType == irr::EET_MOUSE_INPUT_EVENT)
@ -610,16 +612,17 @@ public:
return false; return false;
} }
bool IsKeyDown(EKEY_CODE keyCode) const bool IsKeyDown(const KeyPress &keyCode) const
{ {
return keyIsDown[keyCode]; return keyIsDown[keyCode];
} }
// Checks whether a key was down and resets the state // Checks whether a key was down and resets the state
bool WasKeyDown(EKEY_CODE keyCode) bool WasKeyDown(const KeyPress &keyCode)
{ {
bool b = keyWasDown[keyCode]; bool b = keyWasDown[keyCode];
keyWasDown[keyCode] = false; if (b)
keyWasDown.unset(keyCode);
return b; return b;
} }
@ -632,11 +635,8 @@ public:
void clearInput() void clearInput()
{ {
for(u32 i=0; i<KEY_KEY_CODES_COUNT; i++) keyIsDown.clear();
{ keyWasDown.clear();
keyIsDown[i] = false;
keyWasDown[i] = false;
}
leftclicked = false; leftclicked = false;
rightclicked = false; rightclicked = false;
@ -670,9 +670,9 @@ private:
IrrlichtDevice *m_device; IrrlichtDevice *m_device;
// The current state of keys // The current state of keys
bool keyIsDown[KEY_KEY_CODES_COUNT]; KeyList keyIsDown;
// Whether a key has been pressed or not // Whether a key has been pressed or not
bool keyWasDown[KEY_KEY_CODES_COUNT]; KeyList keyWasDown;
}; };
/* /*
@ -687,11 +687,11 @@ public:
m_receiver(receiver) m_receiver(receiver)
{ {
} }
virtual bool isKeyDown(EKEY_CODE keyCode) virtual bool isKeyDown(const KeyPress &keyCode)
{ {
return m_receiver->IsKeyDown(keyCode); return m_receiver->IsKeyDown(keyCode);
} }
virtual bool wasKeyDown(EKEY_CODE keyCode) virtual bool wasKeyDown(const KeyPress &keyCode)
{ {
return m_receiver->WasKeyDown(keyCode); return m_receiver->WasKeyDown(keyCode);
} }
@ -772,14 +772,13 @@ public:
rightclicked = false; rightclicked = false;
leftreleased = false; leftreleased = false;
rightreleased = false; rightreleased = false;
for(u32 i=0; i<KEY_KEY_CODES_COUNT; ++i) keydown.clear();
keydown[i] = false;
} }
virtual bool isKeyDown(EKEY_CODE keyCode) virtual bool isKeyDown(const KeyPress &keyCode)
{ {
return keydown[keyCode]; return keydown[keyCode];
} }
virtual bool wasKeyDown(EKEY_CODE keyCode) virtual bool wasKeyDown(const KeyPress &keyCode)
{ {
return false; return false;
} }
@ -848,8 +847,7 @@ public:
if(counter1 < 0.0) if(counter1 < 0.0)
{ {
counter1 = 0.1*Rand(1, 40); counter1 = 0.1*Rand(1, 40);
keydown[getKeySetting("keymap_jump")] = keydown.toggle(getKeySetting("keymap_jump"));
!keydown[getKeySetting("keymap_jump")];
} }
} }
{ {
@ -858,8 +856,7 @@ public:
if(counter1 < 0.0) if(counter1 < 0.0)
{ {
counter1 = 0.1*Rand(1, 40); counter1 = 0.1*Rand(1, 40);
keydown[getKeySetting("keymap_special1")] = keydown.toggle(getKeySetting("keymap_special1"));
!keydown[getKeySetting("keymap_special1")];
} }
} }
{ {
@ -868,8 +865,7 @@ public:
if(counter1 < 0.0) if(counter1 < 0.0)
{ {
counter1 = 0.1*Rand(1, 40); counter1 = 0.1*Rand(1, 40);
keydown[getKeySetting("keymap_forward")] = keydown.toggle(getKeySetting("keymap_forward"));
!keydown[getKeySetting("keymap_forward")];
} }
} }
{ {
@ -878,8 +874,7 @@ public:
if(counter1 < 0.0) if(counter1 < 0.0)
{ {
counter1 = 0.1*Rand(1, 40); counter1 = 0.1*Rand(1, 40);
keydown[getKeySetting("keymap_left")] = keydown.toggle(getKeySetting("keymap_left"));
!keydown[getKeySetting("keymap_left")];
} }
} }
{ {
@ -925,7 +920,7 @@ public:
return (myrand()%(max-min+1))+min; return (myrand()%(max-min+1))+min;
} }
private: private:
bool keydown[KEY_KEY_CODES_COUNT]; KeyList keydown;
v2s32 mousepos; v2s32 mousepos;
v2s32 mousespeed; v2s32 mousespeed;
bool leftdown; bool leftdown;