diff --git a/Minecraft.Client/Windows64/KeyboardMouseInput.cpp b/Minecraft.Client/Windows64/KeyboardMouseInput.cpp index be6efe906..e6d3dac8e 100644 --- a/Minecraft.Client/Windows64/KeyboardMouseInput.cpp +++ b/Minecraft.Client/Windows64/KeyboardMouseInput.cpp @@ -4,6 +4,39 @@ #include "KeyboardMouseInput.h" #include +#include +#include +#include +#include + +// Default keyboard and mouse bindings +#define INPUT_BINDING_TABLE \ + X(KEY_FORWARD, 'W') \ + X(KEY_BACKWARD, 'S') \ + X(KEY_LEFT, 'A') \ + X(KEY_RIGHT, 'D') \ + X(KEY_JUMP, VK_SPACE) \ + X(KEY_SNEAK, VK_LSHIFT) \ + X(KEY_SPRINT, VK_CONTROL) \ + X(KEY_INVENTORY, 'E') \ + X(KEY_DROP, 'Q') \ + X(KEY_CRAFTING, 'C') \ + X(KEY_CRAFTING_ALT, 'R') \ + X(KEY_CHAT, 'T') \ + X(KEY_CONFIRM, VK_RETURN) \ + X(KEY_CANCEL, VK_ESCAPE) \ + X(KEY_PAUSE, VK_ESCAPE) \ + X(KEY_TOGGLE_HUD, VK_F1) \ + X(KEY_DEBUG_INFO, VK_F3) \ + X(KEY_DEBUG_MENU, VK_F4) \ + X(KEY_THIRD_PERSON, VK_F5) \ + X(KEY_DEBUG_CONSOLE, VK_F6) \ + X(KEY_HOST_SETTINGS, VK_TAB) \ + X(KEY_SCREENSHOT, VK_F2) + +#define X(name, default_val) int KeyboardMouseInput::name = default_val; + INPUT_BINDING_TABLE +#undef X KeyboardMouseInput g_KBMInput; @@ -27,6 +60,46 @@ static bool IsModifierKeyDown(const bool* keyState, int vkCode) } } +static void LoadUserControls() +{ + std::map controlMap; + + #define X(name, default_val) controlMap[#name] = &KeyboardMouseInput::name; + INPUT_BINDING_TABLE + #undef X + + std::ifstream controlsFile("controls.txt"); + if (controlsFile.is_open()) + { + std::string line; + while (std::getline(controlsFile, line)) + { + if (!line.empty() && line.back() == '\r') line.pop_back(); + if (line.length() < 1) continue; + + size_t sep = line.find('='); + if (sep != std::string::npos && line.length() > sep + 1) + { + std::string keyName = line.substr(0, sep); + std::string valueStr = line.substr(sep + 1); + + if (controlMap.count(keyName)) + { + if (isdigit(valueStr[0])) + { + *controlMap[keyName] = std::stoi(valueStr); + } + else + { + *controlMap[keyName] = (int)valueStr[0]; + } + } + } + } + controlsFile.close(); + } +} + void KeyboardMouseInput::Init() { memset(m_keyDown, 0, sizeof(m_keyDown)); @@ -64,6 +137,8 @@ void KeyboardMouseInput::Init() rid.dwFlags = 0; rid.hwndTarget = g_hWnd; RegisterRawInputDevices(&rid, 1, sizeof(rid)); + + LoadUserControls(); } void KeyboardMouseInput::ClearAllState() diff --git a/Minecraft.Client/Windows64/KeyboardMouseInput.h b/Minecraft.Client/Windows64/KeyboardMouseInput.h index 9b25c929d..2f6917cbe 100644 --- a/Minecraft.Client/Windows64/KeyboardMouseInput.h +++ b/Minecraft.Client/Windows64/KeyboardMouseInput.h @@ -14,29 +14,29 @@ public: static const int MOUSE_MIDDLE = 2; static const int MAX_MOUSE_BUTTONS = 3; - static const int KEY_FORWARD = 'W'; - static const int KEY_BACKWARD = 'S'; - static const int KEY_LEFT = 'A'; - static const int KEY_RIGHT = 'D'; - static const int KEY_JUMP = VK_SPACE; - static const int KEY_SNEAK = VK_LSHIFT; - static const int KEY_SPRINT = VK_CONTROL; - static const int KEY_INVENTORY = 'E'; - static const int KEY_DROP = 'Q'; - static const int KEY_CRAFTING = 'C'; - static const int KEY_CRAFTING_ALT = 'R'; - static const int KEY_CHAT = 'T'; - static const int KEY_CONFIRM = VK_RETURN; - static const int KEY_CANCEL = VK_ESCAPE; - static const int KEY_PAUSE = VK_ESCAPE; - static const int KEY_TOGGLE_HUD = VK_F1; - static const int KEY_DEBUG_INFO = VK_F3; - static const int KEY_DEBUG_MENU = VK_F4; - static const int KEY_THIRD_PERSON = VK_F5; - static const int KEY_DEBUG_CONSOLE = VK_F6; - static const int KEY_HOST_SETTINGS = VK_TAB; + static int KEY_FORWARD; + static int KEY_BACKWARD; + static int KEY_LEFT; + static int KEY_RIGHT; + static int KEY_JUMP; + static int KEY_SNEAK; + static int KEY_SPRINT; + static int KEY_INVENTORY; + static int KEY_DROP; + static int KEY_CRAFTING; + static int KEY_CRAFTING_ALT; + static int KEY_CHAT; + static int KEY_CONFIRM; + static int KEY_CANCEL; + static int KEY_PAUSE; + static int KEY_TOGGLE_HUD; + static int KEY_DEBUG_INFO; + static int KEY_DEBUG_MENU; + static int KEY_THIRD_PERSON; + static int KEY_DEBUG_CONSOLE; + static int KEY_HOST_SETTINGS; static const int KEY_FULLSCREEN = VK_F11; - static const int KEY_SCREENSHOT = VK_F2; + static int KEY_SCREENSHOT; void Init(); void Tick();