Modernize client code (#6250)
[oweals/minetest.git] / src / keycode.cpp
index 48d010cf4f4de98d345f8754acbb5ce0b43e13a5..3551c385a768d4ee1cfaf7343d420eb9a4ab16e5 100644 (file)
@@ -24,6 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "debug.h"
 #include "util/hex.h"
 #include "util/string.h"
+#include "util/basic_macros.h"
 
 class UnknownKeycode : public BaseException
 {
@@ -242,11 +243,10 @@ static const struct table_key table[] = {
 
 #undef N_
 
-#define ARRAYSIZE(a) (sizeof(a) / sizeof((a)[0]))
 
 struct table_key lookup_keyname(const char *name)
 {
-       for (u16 i = 0; i < ARRAYSIZE(table); i++) {
+       for (u16 i = 0; i < ARRLEN(table); i++) {
                if (strcmp(table[i].Name, name) == 0)
                        return table[i];
        }
@@ -256,7 +256,7 @@ struct table_key lookup_keyname(const char *name)
 
 struct table_key lookup_keykey(irr::EKEY_CODE key)
 {
-       for (u16 i = 0; i < ARRAYSIZE(table); i++) {
+       for (u16 i = 0; i < ARRLEN(table); i++) {
                if (table[i].Key == key)
                        return table[i];
        }
@@ -268,7 +268,7 @@ struct table_key lookup_keykey(irr::EKEY_CODE key)
 
 struct table_key lookup_keychar(wchar_t Char)
 {
-       for (u16 i = 0; i < ARRAYSIZE(table); i++) {
+       for (u16 i = 0; i < ARRLEN(table); i++) {
                if (table[i].Char == Char)
                        return table[i];
        }
@@ -278,12 +278,6 @@ struct table_key lookup_keychar(wchar_t Char)
        throw UnknownKeycode(os.str().c_str());
 }
 
-KeyPress::KeyPress() :
-       Key(irr::KEY_KEY_CODES_COUNT),
-       Char(L'\0'),
-       m_name("")
-{}
-
 KeyPress::KeyPress(const char *name)
 {
        if (strlen(name) == 0) {
@@ -357,10 +351,6 @@ const char *KeyPress::name() const
 
 const KeyPress EscapeKey("KEY_ESCAPE");
 const KeyPress CancelKey("KEY_CANCEL");
-const KeyPress NumberKey[] = {
-       KeyPress("0"), KeyPress("1"), KeyPress("2"), KeyPress("3"), KeyPress("4"),
-       KeyPress("5"), KeyPress("6"), KeyPress("7"), KeyPress("8"), KeyPress("9")
-};
 
 /*
        Key config
@@ -385,3 +375,8 @@ void clearKeyCache()
 {
        g_key_setting_cache.clear();
 }
+
+irr::EKEY_CODE keyname_to_keycode(const char *name)
+{
+       return lookup_keyname(name).Key;
+}