Added key configuration in the configuration file.
authorPerttu Ahola <celeron55@gmail.com>
Sat, 14 May 2011 12:43:26 +0000 (15:43 +0300)
committerPerttu Ahola <celeron55@gmail.com>
Sat, 14 May 2011 12:43:26 +0000 (15:43 +0300)
minetest.conf.example
src/CMakeLists.txt
src/defaultsettings.cpp
src/game.cpp
src/guiInventoryMenu.cpp
src/keycode.cpp [new file with mode: 0644]
src/keycode.h [new file with mode: 0644]
src/main.cpp

index 48f326229889d957fdd86698a0e603201fe36dc1..6f5e7a9f7646c1f4f54784784a25303b2a8a072f 100644 (file)
 # Client side stuff
 #
 
+#keymap_forward = KEY_KEY_W
+#keymap_backward = KEY_KEY_S
+#keymap_left = KEY_KEY_A
+#keymap_right = KEY_KEY_D
+#keymap_jump = KEY_SPACE
+#keymap_sneak = KEY_RSHIFT
+#keymap_inventory = KEY_KEY_I
+#keymap_chat = KEY_KEY_T
+#keymap_rangeselect = KEY_KEY_R
+# Some (temporary) keys for debugging
+#keymap_special1 = KEY_KEY_E
+#keymap_print_debug_stacks = KEY_KEY_P
+
 #wanted_fps = 30
 #fps_max = 60
 #viewing_range_nodes_max = 300
index ef42d002ef94637d73a0d7b4f24e2a6af346fbce..8e6f54303b76106f91362ddeabc45bd68a803eff 100644 (file)
@@ -81,6 +81,7 @@ set(common_SRCS
 # Client sources
 set(minetest_SRCS
        ${common_SRCS}
+       keycode.cpp
        clouds.cpp
        clientobject.cpp
        guiFurnaceMenu.cpp
index 1ba8c119431969c00fac11ef9529c18789c13385..eb3b3c92e7abe03af60ccd3e8db8cc6dda079a38 100644 (file)
@@ -24,18 +24,33 @@ extern Settings g_settings;
 void set_default_settings()
 {
        // Client and server
+
+       g_settings.setDefault("port", "");
+       g_settings.setDefault("name", "");
        g_settings.setDefault("footprints", "false");
 
        // Client stuff
+
+       g_settings.setDefault("keymap_forward", "KEY_KEY_W");
+       g_settings.setDefault("keymap_backward", "KEY_KEY_S");
+       g_settings.setDefault("keymap_left", "KEY_KEY_A");
+       g_settings.setDefault("keymap_right", "KEY_KEY_D");
+       g_settings.setDefault("keymap_jump", "KEY_SPACE");
+       g_settings.setDefault("keymap_sneak", "KEY_RSHIFT");
+       g_settings.setDefault("keymap_inventory", "KEY_KEY_I");
+       g_settings.setDefault("keymap_chat", "KEY_KEY_T");
+       g_settings.setDefault("keymap_rangeselect", "KEY_KEY_R");
+       // Some (temporary) keys for debugging
+       g_settings.setDefault("keymap_special1", "KEY_KEY_E");
+       g_settings.setDefault("keymap_print_debug_stacks", "KEY_KEY_P");
+
        g_settings.setDefault("wanted_fps", "30");
        g_settings.setDefault("fps_max", "60");
        g_settings.setDefault("viewing_range_nodes_max", "300");
        g_settings.setDefault("viewing_range_nodes_min", "35");
        g_settings.setDefault("screenW", "800");
        g_settings.setDefault("screenH", "600");
-       g_settings.setDefault("port", "");
        g_settings.setDefault("address", "");
-       g_settings.setDefault("name", "");
        g_settings.setDefault("random_input", "false");
        g_settings.setDefault("client_delete_unused_sectors_timeout", "1200");
        g_settings.setDefault("enable_fog", "true");
index f32a2f0c98cdb91d297ec7f4fa25bd2368ceb960..43d6bd36269cc70a3cd7ab640f1502319ee75ae6 100644 (file)
@@ -28,6 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "materials.h"
 #include "config.h"
 #include "clouds.h"
+#include "keycode.h"
 
 /*
        Setting this to 1 enables a special camera mode that forces
@@ -1079,7 +1080,7 @@ void the_game(
                /*
                        Launch menus according to keys
                */
-               if(input->wasKeyDown(irr::KEY_KEY_I))
+               if(input->wasKeyDown(getKeySetting("keymap_inventory")))
                {
                        dstream<<DTIME<<"the_game: "
                                        <<"Launching inventory"<<std::endl;
@@ -1105,7 +1106,7 @@ void the_game(
 
                        menu->drop();
                }
-               else if(input->wasKeyDown(irr::KEY_ESCAPE))
+               else if(input->wasKeyDown(KEY_ESCAPE))
                {
                        dstream<<DTIME<<"the_game: "
                                        <<"Launching pause menu"<<std::endl;
@@ -1113,7 +1114,7 @@ void the_game(
                        (new GUIPauseMenu(guienv, guiroot, -1, g_gamecallback,
                                        &g_menumgr))->drop();
                }
-               else if(input->wasKeyDown(irr::KEY_KEY_T))
+               else if(input->wasKeyDown(getKeySetting("keymap_chat")))
                {
                        TextDest *dest = new TextDestChat(&client);
 
@@ -1163,7 +1164,7 @@ void the_game(
                }
 
                // Viewing range selection
-               if(input->wasKeyDown(irr::KEY_KEY_R))
+               if(input->wasKeyDown(getKeySetting("keymap_rangeselect")))
                {
                        if(draw_control.range_all)
                        {
@@ -1178,7 +1179,7 @@ void the_game(
                }
 
                // Print debug stacks
-               if(input->wasKeyDown(irr::KEY_KEY_P))
+               if(input->wasKeyDown(getKeySetting("keymap_print_debug_stacks")))
                {
                        dstream<<"-----------------------------------------"
                                        <<std::endl;
@@ -1190,6 +1191,7 @@ void the_game(
 
                /*
                        Player speed control
+                       TODO: Cache the keycodes from getKeySetting
                */
                
                {
@@ -1203,14 +1205,13 @@ void the_game(
                        float a_pitch,
                        float a_yaw*/
                        PlayerControl control(
-                               input->isKeyDown(irr::KEY_KEY_W),
-                               input->isKeyDown(irr::KEY_KEY_S),
-                               input->isKeyDown(irr::KEY_KEY_A),
-                               input->isKeyDown(irr::KEY_KEY_D),
-                               input->isKeyDown(irr::KEY_SPACE),
-                               input->isKeyDown(irr::KEY_KEY_E),
-                               input->isKeyDown(irr::KEY_LSHIFT)
-                                               || input->isKeyDown(irr::KEY_RSHIFT),
+                               input->isKeyDown(getKeySetting("keymap_forward")),
+                               input->isKeyDown(getKeySetting("keymap_backward")),
+                               input->isKeyDown(getKeySetting("keymap_left")),
+                               input->isKeyDown(getKeySetting("keymap_right")),
+                               input->isKeyDown(getKeySetting("keymap_jump")),
+                               input->isKeyDown(getKeySetting("keymap_special1")),
+                               input->isKeyDown(getKeySetting("keymap_sneak")),
                                camera_pitch,
                                camera_yaw
                        );
@@ -1281,7 +1282,7 @@ void the_game(
                                s32 dy = input->getMousePos().Y - displaycenter.Y;
                                //std::cout<<"window active, pos difference "<<dx<<","<<dy<<std::endl;
                                
-                               const float keyspeed = 500;
+                               /*const float keyspeed = 500;
                                if(input->isKeyDown(irr::KEY_UP))
                                        dy -= dtime * keyspeed;
                                if(input->isKeyDown(irr::KEY_DOWN))
@@ -1289,7 +1290,7 @@ void the_game(
                                if(input->isKeyDown(irr::KEY_LEFT))
                                        dx -= dtime * keyspeed;
                                if(input->isKeyDown(irr::KEY_RIGHT))
-                                       dx += dtime * keyspeed;
+                                       dx += dtime * keyspeed;*/
 
                                camera_yaw -= dx*0.2;
                                camera_pitch += dy*0.2;
index ec5328dcc52aa9b75c439b6104a3f64b4ed162e9..a4ba472f82578563d186ca1ef04de9ca7973eb51 100644 (file)
@@ -20,6 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 #include "guiInventoryMenu.h"
 #include "constants.h"
+#include "keycode.h"
 
 void drawInventoryItem(video::IVideoDriver *driver,
                gui::IGUIFont *font,
@@ -293,7 +294,7 @@ bool GUIInventoryMenu::OnEvent(const SEvent& event)
                        quitMenu();
                        return true;
                }
-               if(event.KeyInput.Key==KEY_KEY_I && event.KeyInput.PressedDown)
+               if(event.KeyInput.Key==getKeySetting("keymap_inventory") && event.KeyInput.PressedDown)
                {
                        quitMenu();
                        return true;
diff --git a/src/keycode.cpp b/src/keycode.cpp
new file mode 100644 (file)
index 0000000..ad3c0b4
--- /dev/null
@@ -0,0 +1,192 @@
+/*
+Minetest-c55
+Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include "keycode.h"
+#include "main.h" // For g_settings
+
+#define CHECKKEY(x){if(strcmp(name, #x)==0) return irr::x;}
+
+irr::EKEY_CODE keyname_to_keycode(const char *name)
+{
+       CHECKKEY(KEY_LBUTTON)
+       CHECKKEY(KEY_RBUTTON)
+       CHECKKEY(KEY_CANCEL)
+       CHECKKEY(KEY_MBUTTON)
+       CHECKKEY(KEY_XBUTTON1)
+       CHECKKEY(KEY_XBUTTON2)
+       CHECKKEY(KEY_BACK)
+       CHECKKEY(KEY_TAB)
+       CHECKKEY(KEY_CLEAR)
+       CHECKKEY(KEY_RETURN)
+       CHECKKEY(KEY_SHIFT)
+       CHECKKEY(KEY_CONTROL)
+       CHECKKEY(KEY_MENU)
+       CHECKKEY(KEY_PAUSE)
+       CHECKKEY(KEY_CAPITAL)
+       CHECKKEY(KEY_KANA)
+       CHECKKEY(KEY_HANGUEL)
+       CHECKKEY(KEY_HANGUL)
+       CHECKKEY(KEY_JUNJA)
+       CHECKKEY(KEY_FINAL)
+       CHECKKEY(KEY_HANJA)
+       CHECKKEY(KEY_KANJI)
+       CHECKKEY(KEY_ESCAPE)
+       CHECKKEY(KEY_CONVERT)
+       CHECKKEY(KEY_NONCONVERT)
+       CHECKKEY(KEY_ACCEPT)
+       CHECKKEY(KEY_MODECHANGE)
+       CHECKKEY(KEY_SPACE)
+       CHECKKEY(KEY_PRIOR)
+       CHECKKEY(KEY_NEXT)
+       CHECKKEY(KEY_END)
+       CHECKKEY(KEY_HOME)
+       CHECKKEY(KEY_LEFT)
+       CHECKKEY(KEY_UP)
+       CHECKKEY(KEY_RIGHT)
+       CHECKKEY(KEY_DOWN)
+       CHECKKEY(KEY_SELECT)
+       CHECKKEY(KEY_PRINT)
+       CHECKKEY(KEY_EXECUT)
+       CHECKKEY(KEY_SNAPSHOT)
+       CHECKKEY(KEY_INSERT)
+       CHECKKEY(KEY_DELETE)
+       CHECKKEY(KEY_HELP)
+       CHECKKEY(KEY_KEY_0)
+       CHECKKEY(KEY_KEY_1)
+       CHECKKEY(KEY_KEY_2)
+       CHECKKEY(KEY_KEY_3)
+       CHECKKEY(KEY_KEY_4)
+       CHECKKEY(KEY_KEY_5)
+       CHECKKEY(KEY_KEY_6)
+       CHECKKEY(KEY_KEY_7)
+       CHECKKEY(KEY_KEY_8)
+       CHECKKEY(KEY_KEY_9)
+       CHECKKEY(KEY_KEY_A)
+       CHECKKEY(KEY_KEY_B)
+       CHECKKEY(KEY_KEY_C)
+       CHECKKEY(KEY_KEY_D)
+       CHECKKEY(KEY_KEY_E)
+       CHECKKEY(KEY_KEY_F)
+       CHECKKEY(KEY_KEY_G)
+       CHECKKEY(KEY_KEY_H)
+       CHECKKEY(KEY_KEY_I)
+       CHECKKEY(KEY_KEY_J)
+       CHECKKEY(KEY_KEY_K)
+       CHECKKEY(KEY_KEY_L)
+       CHECKKEY(KEY_KEY_M)
+       CHECKKEY(KEY_KEY_N)
+       CHECKKEY(KEY_KEY_O)
+       CHECKKEY(KEY_KEY_P)
+       CHECKKEY(KEY_KEY_Q)
+       CHECKKEY(KEY_KEY_R)
+       CHECKKEY(KEY_KEY_S)
+       CHECKKEY(KEY_KEY_T)
+       CHECKKEY(KEY_KEY_U)
+       CHECKKEY(KEY_KEY_V)
+       CHECKKEY(KEY_KEY_W)
+       CHECKKEY(KEY_KEY_X)
+       CHECKKEY(KEY_KEY_Y)
+       CHECKKEY(KEY_KEY_Z)
+       CHECKKEY(KEY_LWIN)
+       CHECKKEY(KEY_RWIN)
+       CHECKKEY(KEY_APPS)
+       CHECKKEY(KEY_SLEEP)
+       CHECKKEY(KEY_NUMPAD0)
+       CHECKKEY(KEY_NUMPAD1)
+       CHECKKEY(KEY_NUMPAD2)
+       CHECKKEY(KEY_NUMPAD3)
+       CHECKKEY(KEY_NUMPAD4)
+       CHECKKEY(KEY_NUMPAD5)
+       CHECKKEY(KEY_NUMPAD6)
+       CHECKKEY(KEY_NUMPAD7)
+       CHECKKEY(KEY_NUMPAD8)
+       CHECKKEY(KEY_NUMPAD9)
+       CHECKKEY(KEY_MULTIPLY)
+       CHECKKEY(KEY_ADD)
+       CHECKKEY(KEY_SEPARATOR)
+       CHECKKEY(KEY_SUBTRACT)
+       CHECKKEY(KEY_DECIMAL)
+       CHECKKEY(KEY_DIVIDE)
+       CHECKKEY(KEY_F1)
+       CHECKKEY(KEY_F2)
+       CHECKKEY(KEY_F3)
+       CHECKKEY(KEY_F4)
+       CHECKKEY(KEY_F5)
+       CHECKKEY(KEY_F6)
+       CHECKKEY(KEY_F7)
+       CHECKKEY(KEY_F8)
+       CHECKKEY(KEY_F9)
+       CHECKKEY(KEY_F10)
+       CHECKKEY(KEY_F11)
+       CHECKKEY(KEY_F12)
+       CHECKKEY(KEY_F13)
+       CHECKKEY(KEY_F14)
+       CHECKKEY(KEY_F15)
+       CHECKKEY(KEY_F16)
+       CHECKKEY(KEY_F17)
+       CHECKKEY(KEY_F18)
+       CHECKKEY(KEY_F19)
+       CHECKKEY(KEY_F20)
+       CHECKKEY(KEY_F21)
+       CHECKKEY(KEY_F22)
+       CHECKKEY(KEY_F23)
+       CHECKKEY(KEY_F24)
+       CHECKKEY(KEY_NUMLOCK)
+       CHECKKEY(KEY_SCROLL)
+       CHECKKEY(KEY_LSHIFT)
+       CHECKKEY(KEY_RSHIFT)
+       CHECKKEY(KEY_LCONTROL)
+       CHECKKEY(KEY_RCONTROL)
+       CHECKKEY(KEY_LMENU)
+       CHECKKEY(KEY_RMENU)
+       CHECKKEY(KEY_PLUS)
+       CHECKKEY(KEY_COMMA)
+       CHECKKEY(KEY_MINUS)
+       CHECKKEY(KEY_PERIOD)
+       CHECKKEY(KEY_ATTN)
+       CHECKKEY(KEY_CRSEL)
+       CHECKKEY(KEY_EXSEL)
+       CHECKKEY(KEY_EREOF)
+       CHECKKEY(KEY_PLAY)
+       CHECKKEY(KEY_ZOOM)
+       CHECKKEY(KEY_PA1)
+       CHECKKEY(KEY_OEM_CLEAR)
+
+       return irr::KEY_KEY_CODES_COUNT;
+}
+
+/*
+       Key config
+*/
+
+// A simple cache for quicker lookup
+core::map<std::string, irr::EKEY_CODE> g_key_setting_cache;
+
+irr::EKEY_CODE getKeySetting(const char *settingname)
+{
+       core::map<std::string, irr::EKEY_CODE>::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;
+}
+
+
diff --git a/src/keycode.h b/src/keycode.h
new file mode 100644 (file)
index 0000000..4201deb
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+Minetest-c55
+Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include "common_irrlicht.h"
+
+irr::EKEY_CODE keyname_to_keycode(const char *name);
+
+// Key configuration getter
+irr::EKEY_CODE getKeySetting(const char *settingname);
+
+
index 8098a450f5cac51478e5d7aa21ec523e484ec8e4..e1760331b9218fc8edbe7d7ef0107ed00c96896e 100644 (file)
@@ -137,10 +137,10 @@ Networking and serialization:
 \r
 TODO: Get rid of GotSplitPacketException\r
 \r
-GUI:\r
-----\r
+User Interface:\r
+---------------\r
 \r
-TODO: Configuration menu, at least for keys\r
+TODO: Use getKeySetting everywhere for ESC key and stuff\r
 \r
 Graphics:\r
 ---------\r
@@ -326,6 +326,7 @@ Making it more portable:
 //#include "tile.h"\r
 #include "materials.h"\r
 #include "game.h"\r
+#include "keycode.h"\r
 \r
 // This makes textures\r
 ITextureSource *g_texturesource = NULL;\r
@@ -721,7 +722,8 @@ public:
                        if(counter1 < 0.0)\r
                        {\r
                                counter1 = 0.1*Rand(1, 40);\r
-                               keydown[irr::KEY_SPACE] = !keydown[irr::KEY_SPACE];\r
+                               keydown[getKeySetting("keymap_jump")] =\r
+                                               !keydown[getKeySetting("keymap_jump")];\r
                        }\r
                }\r
                {\r
@@ -730,7 +732,8 @@ public:
                        if(counter1 < 0.0)\r
                        {\r
                                counter1 = 0.1*Rand(1, 40);\r
-                               keydown[irr::KEY_KEY_E] = !keydown[irr::KEY_KEY_E];\r
+                               keydown[getKeySetting("keymap_special1")] =\r
+                                               !keydown[getKeySetting("keymap_special1")];\r
                        }\r
                }\r
                {\r
@@ -739,7 +742,8 @@ public:
                        if(counter1 < 0.0)\r
                        {\r
                                counter1 = 0.1*Rand(1, 40);\r
-                               keydown[irr::KEY_KEY_W] = !keydown[irr::KEY_KEY_W];\r
+                               keydown[getKeySetting("keymap_forward")] =\r
+                                               !keydown[getKeySetting("keymap_forward")];\r
                        }\r
                }\r
                {\r
@@ -748,7 +752,8 @@ public:
                        if(counter1 < 0.0)\r
                        {\r
                                counter1 = 0.1*Rand(1, 40);\r
-                               keydown[irr::KEY_KEY_A] = !keydown[irr::KEY_KEY_A];\r
+                               keydown[getKeySetting("keymap_left")] =\r
+                                               !keydown[getKeySetting("keymap_left")];\r
                        }\r
                }\r
                {\r