#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_("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,
gui::IGUIElement* parent, s32 id, IMenuManager *menumgr) :
GUIModalMenu(env, parent, id, menumgr)
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);
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);
{
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);
{
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);
{
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);
{
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);
{
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);
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);
{
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
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);
{
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);
{
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);
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);
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;
}
{
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;
&& 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;
#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;}
CHECKKEY(KEY_PA1)
CHECKKEY(KEY_OEM_CLEAR)
- return irr::KEY_KEY_CODES_COUNT;
+ throw UnknownKeycode(name);
}
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()