diff options
author | Perttu Ahola <celeron55@gmail.com> | 2011-08-22 14:46:52 +0300 |
---|---|---|
committer | Perttu Ahola <celeron55@gmail.com> | 2011-08-22 14:46:52 +0300 |
commit | 836e43db60ff1f3c9d160d0d58ba5a631c4293fc (patch) | |
tree | 89096c3ffd4128694542ebdbfabe94b6a618e16d /src | |
parent | 6596e585fe844ce87a957bb7deaf2bf92d828592 (diff) | |
parent | 7e610aece52ad547d4ae263aff5297342d5a4bff (diff) | |
download | minetest-836e43db60ff1f3c9d160d0d58ba5a631c4293fc.tar.gz minetest-836e43db60ff1f3c9d160d0d58ba5a631c4293fc.tar.bz2 minetest-836e43db60ff1f3c9d160d0d58ba5a631c4293fc.zip |
Merge remote-tracking branch 'oblomov/new_input'
Diffstat (limited to 'src')
-rw-r--r-- | src/game.cpp | 9 | ||||
-rw-r--r-- | src/game.h | 67 | ||||
-rw-r--r-- | src/guiInventoryMenu.cpp | 9 | ||||
-rw-r--r-- | src/guiKeyChangeMenu.cpp | 174 | ||||
-rw-r--r-- | src/guiKeyChangeMenu.h | 27 | ||||
-rw-r--r-- | src/keycode.cpp | 127 | ||||
-rw-r--r-- | src/keycode.h | 37 | ||||
-rw-r--r-- | src/main.cpp | 55 |
8 files changed, 329 insertions, 176 deletions
diff --git a/src/game.cpp b/src/game.cpp index c740ed252..7bf1c6f5c 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -28,7 +28,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "materials.h" #include "config.h" #include "clouds.h" -#include "keycode.h" #include "farmesh.h" #include "mapblock.h" @@ -1314,7 +1313,7 @@ void the_game( menu->drop(); } - else if(input->wasKeyDown(KEY_ESCAPE)) + else if(input->wasKeyDown(EscapeKey)) { dstream<<DTIME<<"the_game: " <<"Launching pause menu"<<std::endl; @@ -1417,10 +1416,8 @@ void the_game( // Item selection for(u16 i=0; i<10; i++) { - s32 keycode = irr::KEY_KEY_1 + i; - if(i == 9) - keycode = irr::KEY_KEY_0; - if(input->wasKeyDown((irr::EKEY_CODE)keycode)) + const KeyPress *kp = NumberKey + (i + 1) % 10; + if(input->wasKeyDown(*kp)) { if(i < PLAYER_INVENTORY_SIZE && i < hotbar_itemcount) { diff --git a/src/game.h b/src/game.h index 95623316a..a9db6c3e1 100644 --- a/src/game.h +++ b/src/game.h @@ -23,6 +23,69 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "common_irrlicht.h" #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 { public: @@ -33,8 +96,8 @@ public: { } - virtual bool isKeyDown(EKEY_CODE keyCode) = 0; - virtual bool wasKeyDown(EKEY_CODE keyCode) = 0; + virtual bool isKeyDown(const KeyPress &keyCode) = 0; + virtual bool wasKeyDown(const KeyPress &keyCode) = 0; virtual v2s32 getMousePos() = 0; virtual void setMousePos(s32 x, s32 y) = 0; diff --git a/src/guiInventoryMenu.cpp b/src/guiInventoryMenu.cpp index 786a4dd22..f90a2e959 100644 --- a/src/guiInventoryMenu.cpp +++ b/src/guiInventoryMenu.cpp @@ -290,12 +290,9 @@ bool GUIInventoryMenu::OnEvent(const SEvent& event) { if(event.EventType==EET_KEY_INPUT_EVENT) { - if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown) - { - quitMenu(); - return true; - } - if(event.KeyInput.Key==getKeySetting("keymap_inventory") && event.KeyInput.PressedDown) + KeyPress kp(event.KeyInput); + if (event.KeyInput.PressedDown && (kp == EscapeKey || + kp == getKeySetting("keymap_inventory"))) { quitMenu(); return true; diff --git a/src/guiKeyChangeMenu.cpp b/src/guiKeyChangeMenu.cpp index 9c22dba2c..5968d5c12 100644 --- a/src/guiKeyChangeMenu.cpp +++ b/src/guiKeyChangeMenu.cpp @@ -22,38 +22,9 @@ #include "guiKeyChangeMenu.h" #include "debug.h" #include "serialization.h" -#include "keycode.h" #include "main.h" #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_("Priot"), 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 Shight"), - 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, gui::IGUIElement* parent, s32 id, IMenuManager *menumgr) : GUIModalMenu(env, parent, id, menumgr) @@ -129,7 +100,7 @@ void GUIKeyChangeMenu::regenerateGui(v2u32 screensize) rect += topleft + v2s32(offset.X + 105, offset.Y - 5); this->forward = Environment->addButton(rect, this, GUI_ID_KEY_FORWARD_BUTTON, - wgettext(KeyNamesLang[key_forward])); + wgettext(key_forward.name())); } offset += v2s32(0, 25); @@ -146,7 +117,7 @@ void GUIKeyChangeMenu::regenerateGui(v2u32 screensize) rect += topleft + v2s32(offset.X + 105, offset.Y - 5); this->backward = Environment->addButton(rect, this, GUI_ID_KEY_BACKWARD_BUTTON, - wgettext(KeyNamesLang[key_backward])); + wgettext(key_backward.name())); } offset += v2s32(0, 25); { @@ -161,7 +132,7 @@ void GUIKeyChangeMenu::regenerateGui(v2u32 screensize) core::rect < s32 > rect(0, 0, 100, 30); rect += topleft + v2s32(offset.X + 105, offset.Y - 5); this->left = Environment->addButton(rect, this, GUI_ID_KEY_LEFT_BUTTON, - wgettext(KeyNamesLang[key_left])); + wgettext(key_left.name())); } offset += v2s32(0, 25); { @@ -177,7 +148,7 @@ void GUIKeyChangeMenu::regenerateGui(v2u32 screensize) rect += topleft + v2s32(offset.X + 105, offset.Y - 5); this->right = Environment->addButton(rect, this, GUI_ID_KEY_RIGHT_BUTTON, - wgettext(KeyNamesLang[key_right])); + wgettext(key_right.name())); } offset += v2s32(0, 25); { @@ -192,7 +163,7 @@ void GUIKeyChangeMenu::regenerateGui(v2u32 screensize) core::rect < s32 > rect(0, 0, 100, 30); rect += topleft + v2s32(offset.X + 105, offset.Y - 5); this->use = Environment->addButton(rect, this, GUI_ID_KEY_USE_BUTTON, - wgettext(KeyNamesLang[key_use])); + wgettext(key_use.name())); } offset += v2s32(0, 25); { @@ -208,7 +179,7 @@ void GUIKeyChangeMenu::regenerateGui(v2u32 screensize) rect += topleft + v2s32(offset.X + 105, offset.Y - 5); this->sneak = Environment->addButton(rect, this, GUI_ID_KEY_SNEAK_BUTTON, - wgettext(KeyNamesLang[key_sneak])); + wgettext(key_sneak.name())); } offset += v2s32(0, 25); { @@ -222,7 +193,7 @@ void GUIKeyChangeMenu::regenerateGui(v2u32 screensize) core::rect < s32 > rect(0, 0, 100, 30); rect += topleft + v2s32(offset.X + 105, offset.Y - 5); this->jump = Environment->addButton(rect, this, GUI_ID_KEY_JUMP_BUTTON, - wgettext(KeyNamesLang[key_jump])); + wgettext(key_jump.name())); } offset += v2s32(0, 25); @@ -239,7 +210,7 @@ void GUIKeyChangeMenu::regenerateGui(v2u32 screensize) rect += topleft + v2s32(offset.X + 105, offset.Y - 5); this->inventory = Environment->addButton(rect, this, GUI_ID_KEY_INVENTORY_BUTTON, - wgettext(KeyNamesLang[key_inventory])); + wgettext(key_inventory.name())); } offset += v2s32(0, 25); { @@ -253,7 +224,7 @@ void GUIKeyChangeMenu::regenerateGui(v2u32 screensize) core::rect < s32 > rect(0, 0, 100, 30); rect += topleft + v2s32(offset.X + 105, offset.Y - 5); this->chat = Environment->addButton(rect, this, GUI_ID_KEY_CHAT_BUTTON, - wgettext(KeyNamesLang[key_chat])); + wgettext(key_chat.name())); } //next col @@ -270,7 +241,7 @@ void GUIKeyChangeMenu::regenerateGui(v2u32 screensize) core::rect < s32 > rect(0, 0, 100, 30); rect += topleft + v2s32(offset.X + 105, offset.Y - 5); this->fly = Environment->addButton(rect, this, GUI_ID_KEY_FLY_BUTTON, - wgettext(KeyNamesLang[key_fly])); + wgettext(key_fly.name())); } offset += v2s32(0, 25); { @@ -285,7 +256,7 @@ void GUIKeyChangeMenu::regenerateGui(v2u32 screensize) core::rect < s32 > rect(0, 0, 100, 30); rect += topleft + v2s32(offset.X + 105, offset.Y - 5); this->fast = Environment->addButton(rect, this, GUI_ID_KEY_FAST_BUTTON, - wgettext(KeyNamesLang[key_fast])); + wgettext(key_fast.name())); } offset += v2s32(0, 25); { @@ -301,7 +272,7 @@ void GUIKeyChangeMenu::regenerateGui(v2u32 screensize) rect += topleft + v2s32(offset.X + 105, offset.Y - 5); this->range = Environment->addButton(rect, this, GUI_ID_KEY_RANGE_BUTTON, - wgettext(KeyNamesLang[key_range])); + wgettext(key_range.name())); } offset += v2s32(0, 25); @@ -317,7 +288,7 @@ void GUIKeyChangeMenu::regenerateGui(v2u32 screensize) core::rect < s32 > rect(0, 0, 100, 30); rect += topleft + v2s32(offset.X + 105, offset.Y - 5); 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); @@ -354,19 +325,19 @@ void GUIKeyChangeMenu::drawMenu() bool GUIKeyChangeMenu::acceptInput() { - g_settings.set("keymap_forward", keycode_to_keyname(key_forward)); - g_settings.set("keymap_backward", keycode_to_keyname(key_backward)); - g_settings.set("keymap_left", keycode_to_keyname(key_left)); - g_settings.set("keymap_right", keycode_to_keyname(key_right)); - g_settings.set("keymap_jump", keycode_to_keyname(key_jump)); - g_settings.set("keymap_sneak", keycode_to_keyname(key_sneak)); - g_settings.set("keymap_inventory", keycode_to_keyname(key_inventory)); - g_settings.set("keymap_chat", keycode_to_keyname(key_chat)); - g_settings.set("keymap_rangeselect", keycode_to_keyname(key_range)); - g_settings.set("keymap_freemove", keycode_to_keyname(key_fly)); - g_settings.set("keymap_fastmove", keycode_to_keyname(key_fast)); - g_settings.set("keymap_special1", keycode_to_keyname(key_use)); - g_settings.set("keymap_print_debug_stacks", keycode_to_keyname(key_dump)); + g_settings.set("keymap_forward", key_forward.sym()); + g_settings.set("keymap_backward", key_backward.sym()); + g_settings.set("keymap_left", key_left.sym()); + g_settings.set("keymap_right", key_right.sym()); + g_settings.set("keymap_jump", key_jump.sym()); + g_settings.set("keymap_sneak", key_sneak.sym()); + g_settings.set("keymap_inventory", key_inventory.sym()); + g_settings.set("keymap_chat", key_chat.sym()); + g_settings.set("keymap_rangeselect", key_range.sym()); + g_settings.set("keymap_freemove", key_fly.sym()); + g_settings.set("keymap_fastmove", key_fast.sym()); + g_settings.set("keymap_special1", key_use.sym()); + g_settings.set("keymap_print_debug_stacks", key_dump.sym()); clearKeyCache(); return true; } @@ -395,45 +366,45 @@ bool GUIKeyChangeMenu::resetMenu() { case GUI_ID_KEY_FORWARD_BUTTON: this->forward->setText( - wgettext(KeyNamesLang[key_forward])); + wgettext(key_forward.name())); break; case GUI_ID_KEY_BACKWARD_BUTTON: this->backward->setText( - wgettext(KeyNamesLang[key_backward])); + wgettext(key_backward.name())); break; case GUI_ID_KEY_LEFT_BUTTON: - this->left->setText(wgettext(KeyNamesLang[key_left])); + this->left->setText(wgettext(key_left.name())); break; case GUI_ID_KEY_RIGHT_BUTTON: - this->right->setText(wgettext(KeyNamesLang[key_right])); + this->right->setText(wgettext(key_right.name())); break; case GUI_ID_KEY_JUMP_BUTTON: - this->jump->setText(wgettext(KeyNamesLang[key_jump])); + this->jump->setText(wgettext(key_jump.name())); break; case GUI_ID_KEY_SNEAK_BUTTON: - this->sneak->setText(wgettext(KeyNamesLang[key_sneak])); + this->sneak->setText(wgettext(key_sneak.name())); break; case GUI_ID_KEY_INVENTORY_BUTTON: this->inventory->setText( - wgettext(KeyNamesLang[key_inventory])); + wgettext(key_inventory.name())); break; case GUI_ID_KEY_CHAT_BUTTON: - this->chat->setText(wgettext(KeyNamesLang[key_chat])); + this->chat->setText(wgettext(key_chat.name())); break; case GUI_ID_KEY_RANGE_BUTTON: - this->range->setText(wgettext(KeyNamesLang[key_range])); + this->range->setText(wgettext(key_range.name())); break; case GUI_ID_KEY_FLY_BUTTON: - this->fly->setText(wgettext(KeyNamesLang[key_fly])); + this->fly->setText(wgettext(key_fly.name())); break; case GUI_ID_KEY_FAST_BUTTON: - this->fast->setText(wgettext(KeyNamesLang[key_fast])); + this->fast->setText(wgettext(key_fast.name())); break; case GUI_ID_KEY_USE_BUTTON: - this->use->setText(wgettext(KeyNamesLang[key_use])); + this->use->setText(wgettext(key_use.name())); break; case GUI_ID_KEY_DUMP_BUTTON: - this->dump->setText(wgettext(KeyNamesLang[key_dump])); + this->dump->setText(wgettext(key_dump.name())); break; } activeKey = -1; @@ -447,83 +418,72 @@ bool GUIKeyChangeMenu::OnEvent(const SEvent& event) && event.KeyInput.PressedDown) { changeCtype(""); + KeyPress kp(event.KeyInput); + if (activeKey == GUI_ID_KEY_FORWARD_BUTTON) { - this->forward->setText( - wgettext(KeyNamesLang[event.KeyInput.Key])); - this->key_forward = event.KeyInput.Key; + this->forward->setText(wgettext(kp.name())); + this->key_forward = kp; } else if (activeKey == GUI_ID_KEY_BACKWARD_BUTTON) { - this->backward->setText( - wgettext(KeyNamesLang[event.KeyInput.Key])); - this->key_backward = event.KeyInput.Key; + this->backward->setText(wgettext(kp.name())); + this->key_backward = kp; } else if (activeKey == GUI_ID_KEY_LEFT_BUTTON) { - this->left->setText( - wgettext(KeyNamesLang[event.KeyInput.Key])); - this->key_left = event.KeyInput.Key; + this->left->setText(wgettext(kp.name())); + this->key_left = kp; } else if (activeKey == GUI_ID_KEY_RIGHT_BUTTON) { - this->right->setText( - wgettext(KeyNamesLang[event.KeyInput.Key])); - this->key_right = event.KeyInput.Key; + this->right->setText(wgettext(kp.name())); + this->key_right = kp; } else if (activeKey == GUI_ID_KEY_JUMP_BUTTON) { - this->jump->setText( - wgettext(KeyNamesLang[event.KeyInput.Key])); - this->key_jump = event.KeyInput.Key; + this->jump->setText(wgettext(kp.name())); + this->key_jump = kp; } else if (activeKey == GUI_ID_KEY_SNEAK_BUTTON) { - this->sneak->setText( - wgettext(KeyNamesLang[event.KeyInput.Key])); - this->key_sneak = event.KeyInput.Key; + this->sneak->setText(wgettext(kp.name())); + this->key_sneak = kp; } else if (activeKey == GUI_ID_KEY_INVENTORY_BUTTON) { - this->inventory->setText( - wgettext(KeyNamesLang[event.KeyInput.Key])); - this->key_inventory = event.KeyInput.Key; + this->inventory->setText(wgettext(kp.name())); + this->key_inventory = kp; } else if (activeKey == GUI_ID_KEY_CHAT_BUTTON) { - this->chat->setText( - wgettext(KeyNamesLang[event.KeyInput.Key])); - this->key_chat = event.KeyInput.Key; + this->chat->setText(wgettext(kp.name())); + this->key_chat = kp; } else if (activeKey == GUI_ID_KEY_RANGE_BUTTON) { - this->range->setText( - wgettext(KeyNamesLang[event.KeyInput.Key])); - this->key_range = event.KeyInput.Key; + this->range->setText(wgettext(kp.name())); + this->key_range = kp; } else if (activeKey == GUI_ID_KEY_FLY_BUTTON) { - this->fly->setText( - wgettext(KeyNamesLang[event.KeyInput.Key])); - this->key_fly = event.KeyInput.Key; + this->fly->setText(wgettext(kp.name())); + this->key_fly = kp; } else if (activeKey == GUI_ID_KEY_FAST_BUTTON) { - this->fast->setText( - wgettext(KeyNamesLang[event.KeyInput.Key])); - this->key_fast = event.KeyInput.Key; + this->fast->setText(wgettext(kp.name())); + this->key_fast = kp; } else if (activeKey == GUI_ID_KEY_USE_BUTTON) { - this->use->setText( - wgettext(KeyNamesLang[event.KeyInput.Key])); - this->key_use = event.KeyInput.Key; + this->use->setText(wgettext(kp.name())); + this->key_use = kp; } else if (activeKey == GUI_ID_KEY_DUMP_BUTTON) { - this->dump->setText( - wgettext(KeyNamesLang[event.KeyInput.Key])); - this->key_dump = event.KeyInput.Key; + this->dump->setText(wgettext(kp.name())); + this->key_dump = kp; } changeCtype("C"); activeKey = -1; diff --git a/src/guiKeyChangeMenu.h b/src/guiKeyChangeMenu.h index 2caf69216..a1dfa17c2 100644 --- a/src/guiKeyChangeMenu.h +++ b/src/guiKeyChangeMenu.h @@ -27,6 +27,7 @@ #include "modalMenu.h" #include "client.h" #include "gettext.h" +#include "keycode.h" #include <string> enum @@ -88,19 +89,19 @@ private: gui::IGUIButton *chat; s32 activeKey; - s32 key_forward; - s32 key_backward; - s32 key_left; - s32 key_right; - s32 key_use; - s32 key_sneak; - s32 key_jump; - s32 key_inventory; - s32 key_fly; - s32 key_fast; - s32 key_range; - s32 key_chat; - s32 key_dump; + KeyPress key_forward; + KeyPress key_backward; + KeyPress key_left; + KeyPress key_right; + KeyPress key_use; + KeyPress key_sneak; + KeyPress key_jump; + KeyPress key_inventory; + KeyPress key_fly; + KeyPress key_fast; + KeyPress key_range; + KeyPress key_chat; + KeyPress key_dump; }; #endif diff --git a/src/keycode.cpp b/src/keycode.cpp index 052cd7dc6..5ad5ab55d 100644 --- a/src/keycode.cpp +++ b/src/keycode.cpp @@ -19,6 +19,14 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "keycode.h" #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;} @@ -168,7 +176,7 @@ irr::EKEY_CODE keyname_to_keycode(const char *name) CHECKKEY(KEY_PA1) CHECKKEY(KEY_OEM_CLEAR) - return irr::KEY_KEY_CODES_COUNT; + throw UnknownKeycode(name); } static const char *KeyNames[] = @@ -206,27 +214,128 @@ static const char *KeyNames[] = "-", "-", "-", "-", "-", "-", "-", "-", "Attn", "CrSel", "ExSel", "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 */ // 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); if(n) return n->getValue(); - irr::EKEY_CODE c = keyname_to_keycode(g_settings.get(settingname).c_str()); - g_key_setting_cache.insert(settingname, c); - return c; + g_key_setting_cache.insert(settingname, + g_settings.get(settingname).c_str()); + return g_key_setting_cache.find(settingname)->getValue(); } void clearKeyCache() diff --git a/src/keycode.h b/src/keycode.h index e64cb362c..28fb3f1fb 100644 --- a/src/keycode.h +++ b/src/keycode.h @@ -23,11 +23,42 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "common_irrlicht.h" #include <string> -irr::EKEY_CODE keyname_to_keycode(const char *name); -const char *keycode_to_keyname(s32 keycode); +/* A key press, consisting of either an Irrlicht 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 -irr::EKEY_CODE getKeySetting(const char *settingname); +KeyPress getKeySetting(const char *settingname); // Clear fast lookup cache void clearKeyCache(); diff --git a/src/main.cpp b/src/main.cpp index d93cdba1e..b9a957322 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -563,10 +563,12 @@ public: // Remember whether each key is down or up if(event.EventType == irr::EET_KEY_INPUT_EVENT) { - keyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown; - - if(event.KeyInput.PressedDown) - keyWasDown[event.KeyInput.Key] = true; + if(event.KeyInput.PressedDown) { + keyIsDown.set(event.KeyInput); + keyWasDown.set(event.KeyInput); + } else { + keyIsDown.unset(event.KeyInput); + } } if(event.EventType == irr::EET_MOUSE_INPUT_EVENT) @@ -610,16 +612,17 @@ public: return false; } - bool IsKeyDown(EKEY_CODE keyCode) const + bool IsKeyDown(const KeyPress &keyCode) const { return keyIsDown[keyCode]; } // Checks whether a key was down and resets the state - bool WasKeyDown(EKEY_CODE keyCode) + bool WasKeyDown(const KeyPress &keyCode) { bool b = keyWasDown[keyCode]; - keyWasDown[keyCode] = false; + if (b) + keyWasDown.unset(keyCode); return b; } @@ -632,12 +635,9 @@ public: void clearInput() { - for(u32 i=0; i<KEY_KEY_CODES_COUNT; i++) - { - keyIsDown[i] = false; - keyWasDown[i] = false; - } - + keyIsDown.clear(); + keyWasDown.clear(); + leftclicked = false; rightclicked = false; leftreleased = false; @@ -670,9 +670,9 @@ private: IrrlichtDevice *m_device; // The current state of keys - bool keyIsDown[KEY_KEY_CODES_COUNT]; + KeyList keyIsDown; // Whether a key has been pressed or not - bool keyWasDown[KEY_KEY_CODES_COUNT]; + KeyList keyWasDown; }; /* @@ -687,11 +687,11 @@ public: m_receiver(receiver) { } - virtual bool isKeyDown(EKEY_CODE keyCode) + virtual bool isKeyDown(const KeyPress &keyCode) { return m_receiver->IsKeyDown(keyCode); } - virtual bool wasKeyDown(EKEY_CODE keyCode) + virtual bool wasKeyDown(const KeyPress &keyCode) { return m_receiver->WasKeyDown(keyCode); } @@ -772,14 +772,13 @@ public: rightclicked = false; leftreleased = false; rightreleased = false; - for(u32 i=0; i<KEY_KEY_CODES_COUNT; ++i) - keydown[i] = false; + keydown.clear(); } - virtual bool isKeyDown(EKEY_CODE keyCode) + virtual bool isKeyDown(const KeyPress &keyCode) { return keydown[keyCode]; } - virtual bool wasKeyDown(EKEY_CODE keyCode) + virtual bool wasKeyDown(const KeyPress &keyCode) { return false; } @@ -848,8 +847,7 @@ public: if(counter1 < 0.0) { counter1 = 0.1*Rand(1, 40); - keydown[getKeySetting("keymap_jump")] = - !keydown[getKeySetting("keymap_jump")]; + keydown.toggle(getKeySetting("keymap_jump")); } } { @@ -858,8 +856,7 @@ public: if(counter1 < 0.0) { counter1 = 0.1*Rand(1, 40); - keydown[getKeySetting("keymap_special1")] = - !keydown[getKeySetting("keymap_special1")]; + keydown.toggle(getKeySetting("keymap_special1")); } } { @@ -868,8 +865,7 @@ public: if(counter1 < 0.0) { counter1 = 0.1*Rand(1, 40); - keydown[getKeySetting("keymap_forward")] = - !keydown[getKeySetting("keymap_forward")]; + keydown.toggle(getKeySetting("keymap_forward")); } } { @@ -878,8 +874,7 @@ public: if(counter1 < 0.0) { counter1 = 0.1*Rand(1, 40); - keydown[getKeySetting("keymap_left")] = - !keydown[getKeySetting("keymap_left")]; + keydown.toggle(getKeySetting("keymap_left")); } } { @@ -925,7 +920,7 @@ public: return (myrand()%(max-min+1))+min; } private: - bool keydown[KEY_KEY_CODES_COUNT]; + KeyList keydown; v2s32 mousepos; v2s32 mousespeed; bool leftdown; |