#include "stdafx.h" #ifdef _WINDOWS64 #include "ControllerBinds.h" struct ControllerBindDef { unsigned char action; const char* id; const wchar_t* name; const wchar_t* section; }; static const ControllerBindDef s_controllerBinds[] = { { MINECRAFT_ACTION_USE, "use", L"Use", L"Standard" }, { MINECRAFT_ACTION_ACTION, "attack", L"Action", L"Standard" }, { MINECRAFT_ACTION_JUMP, "jump", L"Jump", L"Standard" }, { MINECRAFT_ACTION_DROP, "drop", L"Drop", L"Standard" }, { MINECRAFT_ACTION_INVENTORY, "inventory", L"Inventory", L"Standard" }, { MINECRAFT_ACTION_CRAFTING, "crafting", L"Crafting", L"Standard" }, { MINECRAFT_ACTION_LEFT_SCROLL, "hotbar_left", L"Cycle Held Item Left", L"Standard" }, { MINECRAFT_ACTION_RIGHT_SCROLL, "hotbar_right", L"Cycle Held Item Right", L"Standard" }, { MINECRAFT_ACTION_SNEAK_TOGGLE, "sneak", L"Sneak/Dismount", L"Standard" }, { MINECRAFT_ACTION_SPRINT, "sprint", L"Sprint", L"Standard" }, { MINECRAFT_ACTION_PICK_BLOCK, "pick_block", L"Pick Block", L"Standard" }, { MINECRAFT_ACTION_RENDER_THIRD_PERSON, "perspective", L"Change Camera Mode", L"Standard" }, { MINECRAFT_ACTION_DPAD_LEFT, "fly_left", L"Fly Left", L"Creative" }, { MINECRAFT_ACTION_DPAD_RIGHT, "fly_right", L"Fly Right", L"Creative" }, { MINECRAFT_ACTION_DPAD_UP, "fly_up", L"Fly Up", L"Creative" }, { MINECRAFT_ACTION_DPAD_DOWN, "fly_down", L"Fly Down", L"Creative" }, }; static const int s_controllerBindCount = static_cast(sizeof(s_controllerBinds) / sizeof(s_controllerBinds[0])); int ControllerBinds::GetBindCount() { return s_controllerBindCount; } unsigned char ControllerBinds::GetBindAction(int index) { return (index >= 0 && index < s_controllerBindCount) ? s_controllerBinds[index].action : 0; } const wchar_t* ControllerBinds::GetBindName(int index) { return (index >= 0 && index < s_controllerBindCount) ? s_controllerBinds[index].name : L""; } const wchar_t* ControllerBinds::GetBindSection(int index) { return (index >= 0 && index < s_controllerBindCount) ? s_controllerBinds[index].section : L""; } static void GetControllerBindsPath(char* out, size_t outLen) { char exePath[MAX_PATH] = {}; GetModuleFileNameA(nullptr, exePath, MAX_PATH); char* lastSlash = strrchr(exePath, '\\'); if (lastSlash) *(lastSlash + 1) = '\0'; _snprintf_s(out, outLen, _TRUNCATE, "%scontrollerbinds.txt", exePath); } void ControllerBinds::Init() { for (int action = 0; action < MINECRAFT_ACTION_MAX; ++action) InputManager.SetGameJoypadMaps(CUSTOM_MAP, static_cast(action), InputManager.GetGameJoypadMapsRaw(MAP_STYLE_0, static_cast(action))); char path[MAX_PATH] = {}; GetControllerBindsPath(path, sizeof(path)); FILE* f = nullptr; if (fopen_s(&f, path, "r") != 0 || !f) return; char line[128]; while (fgets(line, sizeof(line), f)) { char* eq = strchr(line, '='); if (!eq) continue; *eq = '\0'; unsigned int button = static_cast(strtoul(eq + 1, nullptr, 16)) & GetBindableButtonMask(); if (button == 0) continue; for (int i = 0; i < s_controllerBindCount; ++i) { if (strcmp(line, s_controllerBinds[i].id) == 0) { InputManager.SetGameJoypadMaps(CUSTOM_MAP, s_controllerBinds[i].action, button); break; } } } fclose(f); } unsigned int ControllerBinds::Get(int index) { if (index < 0 || index >= s_controllerBindCount) return 0; return InputManager.GetGameJoypadMapsRaw(CUSTOM_MAP, s_controllerBinds[index].action); } unsigned int ControllerBinds::GetFromMap(int index, unsigned char map) { if (index < 0 || index >= s_controllerBindCount || map >= MAP_COUNT) return 0; return InputManager.GetGameJoypadMapsRaw(map, s_controllerBinds[index].action); } void ControllerBinds::CopyLayoutToCustom(unsigned char map) { if (map >= MAP_COUNT || map == CUSTOM_MAP) return; for (int i = 0; i < s_controllerBindCount; ++i) InputManager.SetGameJoypadMaps(CUSTOM_MAP, s_controllerBinds[i].action, InputManager.GetGameJoypadMapsRaw(map, s_controllerBinds[i].action)); } void ControllerBinds::Set(int index, unsigned int button) { if (index < 0 || index >= s_controllerBindCount) return; InputManager.SetGameJoypadMaps(CUSTOM_MAP, s_controllerBinds[index].action, button); } void ControllerBinds::ResetToDefaults() { for (int i = 0; i < s_controllerBindCount; ++i) InputManager.SetGameJoypadMaps(CUSTOM_MAP, s_controllerBinds[i].action, InputManager.GetGameJoypadMapsRaw(MAP_STYLE_0, s_controllerBinds[i].action)); } void ControllerBinds::Save() { char path[MAX_PATH] = {}; GetControllerBindsPath(path, sizeof(path)); FILE* f = nullptr; if (fopen_s(&f, path, "w") != 0 || !f) return; for (int i = 0; i < s_controllerBindCount; ++i) fprintf(f, "%s=%08X\n", s_controllerBinds[i].id, InputManager.GetGameJoypadMapsRaw(CUSTOM_MAP, s_controllerBinds[i].action)); fclose(f); } unsigned int ControllerBinds::GetBindableButtonMask() { return _360_JOY_BUTTON_A | _360_JOY_BUTTON_B | _360_JOY_BUTTON_X | _360_JOY_BUTTON_Y | _360_JOY_BUTTON_START | _360_JOY_BUTTON_BACK | _360_JOY_BUTTON_RB | _360_JOY_BUTTON_LB | _360_JOY_BUTTON_RTHUMB | _360_JOY_BUTTON_LTHUMB | _360_JOY_BUTTON_DPAD_UP | _360_JOY_BUTTON_DPAD_DOWN | _360_JOY_BUTTON_DPAD_LEFT | _360_JOY_BUTTON_DPAD_RIGHT | _360_JOY_BUTTON_RT | _360_JOY_BUTTON_LT; } struct ButtonNames { unsigned int button; const wchar_t* xbox; const wchar_t* playstation; const wchar_t* wiiu; const wchar_t* icon; }; static const ButtonNames s_buttonNames[] = { { _360_JOY_BUTTON_A, L"A", L"Cross", L"B", L"ButtonA" }, { _360_JOY_BUTTON_B, L"B", L"Circle", L"A", L"ButtonB" }, { _360_JOY_BUTTON_X, L"X", L"Square", L"Y", L"ButtonX" }, { _360_JOY_BUTTON_Y, L"Y", L"Triangle", L"X", L"ButtonY" }, { _360_JOY_BUTTON_START, L"Start", L"Start", L"+", L"ButtonStart" }, { _360_JOY_BUTTON_BACK, L"Back", L"Select", L"-", L"ButtonBack" }, { _360_JOY_BUTTON_RB, L"RB", L"R1", L"R", L"ButtonRightBumper" }, { _360_JOY_BUTTON_LB, L"LB", L"L1", L"L", L"ButtonLeftBumper" }, { _360_JOY_BUTTON_RT, L"RT", L"R2", L"ZR", L"ButtonRightTrigger" }, { _360_JOY_BUTTON_LT, L"LT", L"L2", L"ZL", L"ButtonLeftTrigger" }, { _360_JOY_BUTTON_RTHUMB, L"Right Stick Click", L"R3", L"Right Stick Click", L"ButtonRS" }, { _360_JOY_BUTTON_LTHUMB, L"Left Stick Click", L"L3", L"Left Stick Click", L"ButtonLS" }, { _360_JOY_BUTTON_DPAD_UP, L"D-Pad Up", L"D-Pad Up", L"D-Pad Up", L"ButtonDpadU" }, { _360_JOY_BUTTON_DPAD_DOWN, L"D-Pad Down", L"D-Pad Down", L"D-Pad Down", L"ButtonDpadD" }, { _360_JOY_BUTTON_DPAD_LEFT, L"D-Pad Left", L"D-Pad Left", L"D-Pad Left", L"ButtonDpadL" }, { _360_JOY_BUTTON_DPAD_RIGHT, L"D-Pad Right", L"D-Pad Right", L"D-Pad Right", L"ButtonDpadR" }, }; const wchar_t* ControllerBinds::GetButtonIconName(unsigned int button) { for (int i = 0; i < static_cast(sizeof(s_buttonNames) / sizeof(s_buttonNames[0])); ++i) { if (button == s_buttonNames[i].button) return s_buttonNames[i].icon; } return nullptr; } void ControllerBinds::GetButtonName(unsigned int button, int controlType, wchar_t* out, int outLen) { if (!out || outLen <= 0) return; out[0] = L'\0'; for (int i = 0; i < static_cast(sizeof(s_buttonNames) / sizeof(s_buttonNames[0])); ++i) { if (button & s_buttonNames[i].button) { const wchar_t* name = s_buttonNames[i].xbox; if (controlType == 3 || controlType == 4) name = s_buttonNames[i].playstation; else if (controlType == 5) name = s_buttonNames[i].wiiu; if (out[0] != L'\0') wcsncat_s(out, outLen, L" / ", _TRUNCATE); wcsncat_s(out, outLen, name, _TRUNCATE); } } if (out[0] == L'\0') wcsncpy_s(out, outLen, L"None", _TRUNCATE); } #endif