#include "stdafx.h" #include "UI.h" #include "UIScene_KeybindsMenu.h" #ifdef _WINDOWS64 #include "../../Windows64/KeyboardMouseInput.h" #include "../../Windows64/ControllerBinds.h" static const wchar_t* s_bindNames[KeyboardMouseInput::KB_COUNT] = { L"Walk Forwards", L"Walk Backwards", L"Strafe Left", L"Strafe Right", L"Jump", L"Sneak", L"Sprint", L"Inventory", L"Drop Item", L"Crafting", L"Crafting (Alternate)", L"Chat", L"Change Camera Mode", L"Pick Block", L"Hotbar Slot 1", L"Hotbar Slot 2", L"Hotbar Slot 3", L"Hotbar Slot 4", L"Hotbar Slot 5", L"Hotbar Slot 6", L"Hotbar Slot 7", L"Hotbar Slot 8", L"Hotbar Slot 9", L"Toggle HUD", L"Take Screenshot", L"Debug Info", L"Toggle Fullscreen", }; struct KeybindSection { const wchar_t* title; int first; int last; }; static const KeybindSection s_sections[] = { { L"Movement", KeyboardMouseInput::KB_FORWARD, KeyboardMouseInput::KB_SPRINT }, { L"Gameplay", KeyboardMouseInput::KB_INVENTORY, KeyboardMouseInput::KB_PICK_BLOCK }, { L"Hotbar", KeyboardMouseInput::KB_HOTBAR_1, KeyboardMouseInput::KB_HOTBAR_9 }, { L"Miscellaneous", KeyboardMouseInput::KB_TOGGLE_HUD, KeyboardMouseInput::KB_FULLSCREEN }, }; UIScene_KeybindsMenu::UIScene_KeybindsMenu(int iPad, void *initData, UILayer *parentLayer) : UIScene(iPad, parentLayer) { initialiseMovie(); m_bController = reinterpret_cast(initData) != 0; m_bNeedsMultiListPopulate = true; m_bInitialPopulateDone = false; m_bChanged = false; m_iCaptureBind = -1; m_bCaptureArmed = false; m_bIgnoreInputUntilRelease = false; m_bPendingReset = false; m_uiPrevPadButtons = 0; m_bPendingLayout = false; m_bPendingCheckboxes = false; m_iPendingLayout = 0; doHorizontalResizeCheck(); } UIScene_KeybindsMenu::~UIScene_KeybindsMenu() { g_KBMInput.SetBindCapture(false); } wstring UIScene_KeybindsMenu::getMoviePath() { if (app.GetLocalPlayerCount() > 1) return L"MultilistMenuSplit"; return L"MultilistMenu"; } int UIScene_KeybindsMenu::getBindCount() const { return m_bController ? ControllerBinds::GetBindCount() : static_cast(KeyboardMouseInput::KB_COUNT); } wstring UIScene_KeybindsMenu::getBindLabel(int bind, bool waiting) { wchar_t valueName[96]; bool clash = false; if (waiting) { wcsncpy_s(valueName, m_bController ? L"Press a button..." : L"> Press a key <", _TRUNCATE); } else if (m_bController) { const unsigned char layout = static_cast(currentLayout()); unsigned int button = ControllerBinds::GetFromMap(bind, layout); for (int i = 0; i < ControllerBinds::GetBindCount(); ++i) { if (i != bind && (ControllerBinds::GetFromMap(i, layout) & button) != 0) { clash = true; break; } } wstring text = ControllerBinds::GetBindName(bind); if (button == 0) text += L": None"; if (clash) text += L" (!)"; return text; } else { for (int i = 0; i < KeyboardMouseInput::KB_COUNT; ++i) { if (i != bind && g_KBMInput.GetBind(i) == g_KBMInput.GetBind(bind)) { clash = true; break; } } if (KeyboardMouseInput::GetKeyIconName(g_KBMInput.GetBind(bind), nullptr) != nullptr) { wstring text = s_bindNames[bind]; if (clash) text += L" (!)"; return text; } KeyboardMouseInput::GetKeyName(g_KBMInput.GetBind(bind), valueName, 96); } const wchar_t* name = m_bController ? ControllerBinds::GetBindName(bind) : s_bindNames[bind]; wchar_t text[256]; swprintf(text, 256, clash ? L"%ls: %ls (!)" : L"%ls: %ls", name, valueName); return text; } void UIScene_KeybindsMenu::registerPadIcons() { const wchar_t* folder = L"xboxOne"; switch (app.GetGameSettings(m_iPad, eGameSetting_ControlType)) { case 2: folder = L"xbox360"; break; case 3: folder = L"PS3"; break; case 4: folder = L"PS4"; break; case 5: folder = L"WiiU"; break; } static unsigned int s_padIconGeneration = 0; wchar_t prefix[64]; swprintf(prefix, 64, L"PadIcon_%ls_%u_", folder, ++s_padIconGeneration); m_padIconPrefix = prefix; vector iconNames; iconNames.push_back(L"ButtonLeftStick"); iconNames.push_back(L"ButtonRightStick"); unsigned int mask = ControllerBinds::GetBindableButtonMask(); for (unsigned int bit = 1; bit != 0; bit <<= 1) { if ((mask & bit) == 0) continue; const wchar_t* icon = ControllerBinds::GetButtonIconName(bit); if (icon) iconNames.push_back(icon); } for (const wchar_t* icon : iconNames) { wstring textureName = m_padIconPrefix + icon; if (hasRegisteredSubstitutionTexture(textureName)) continue; wstring path = wstring(L"Graphics\\PadIcons\\") + folder + L"\\" + icon + L".png"; if (!app.hasArchiveFile(path)) continue; byteArray data = app.getArchiveFile(path); if (data.data && data.length > 0) registerSubstitutionTexture(textureName, data.data, data.length, true); } } wstring UIScene_KeybindsMenu::registerKeyIcon(int vkCode) { bool wide = false; const wchar_t* icon = KeyboardMouseInput::GetKeyIconName(vkCode, &wide); if (!icon) return L""; wstring textureName = m_padIconPrefix + icon; if (!hasRegisteredSubstitutionTexture(textureName)) { wstring path = wstring(L"Graphics\\KeyIcons\\") + icon + L".png"; if (!app.hasArchiveFile(path)) return L""; byteArray data = app.getArchiveFile(path); if (!data.data || data.length == 0) return L""; registerSubstitutionTexture(textureName, data.data, data.length, true); } return wide ? textureName + L"@2" : textureName; } void UIScene_KeybindsMenu::setBindRow(int bind, bool waiting) { m_multiList.SetItemLabelLeft(eControl_BindBase + bind, getBindLabel(bind, waiting)); int iconSize = (getSceneResolution() == UIScene::eSceneResolution_1080) ? 36 : 24; if (!m_bController) { m_multiList.SetItemIcons(eControl_BindBase + bind, waiting ? wstring() : registerKeyIcon(g_KBMInput.GetBind(bind)), iconSize); return; } wstring icons; if (!waiting) { unsigned int shown = InputManager.ApplyButtonSwap(m_iPad, ControllerBinds::GetFromMap(bind, static_cast(currentLayout()))); for (unsigned int bit = 1; bit != 0; bit <<= 1) { if ((shown & bit) == 0) continue; const wchar_t* icon = ControllerBinds::GetButtonIconName(bit); if (!icon) continue; if (!icons.empty()) icons += L","; icons += m_padIconPrefix; icons += icon; } } m_multiList.SetItemIcons(eControl_BindBase + bind, icons, iconSize); } void UIScene_KeybindsMenu::setStickRows() { const bool southpaw = app.GetGameSettings(m_iPad, eGameSetting_ControlSouthPaw) != 0; int iconSize = (getSceneResolution() == UIScene::eSceneResolution_1080) ? 36 : 24; m_multiList.SetItemIcons(eControl_Look, m_padIconPrefix + (southpaw ? L"ButtonLeftStick" : L"ButtonRightStick"), iconSize); m_multiList.SetItemIcons(eControl_Move, m_padIconPrefix + (southpaw ? L"ButtonRightStick" : L"ButtonLeftStick"), iconSize); } void UIScene_KeybindsMenu::refreshBindLabels() { for (int i = 0; i < getBindCount(); ++i) setBindRow(i, i == m_iCaptureBind); } void UIScene_KeybindsMenu::beginCapture(int bind) { m_iCaptureBind = bind; m_bCaptureArmed = false; g_KBMInput.SetBindCapture(true); setBindRow(bind, true); } void UIScene_KeybindsMenu::endCapture() { int bind = m_iCaptureBind; m_iCaptureBind = -1; m_bCaptureArmed = false; m_bIgnoreInputUntilRelease = true; if (bind >= 0) refreshBindLabels(); } bool UIScene_KeybindsMenu::anyInputHeld() { if (g_KBMInput.IsAnyKeyDown()) return true; return (InputManager.GetRawButtons(m_iPad) & ControllerBinds::GetBindableButtonMask()) != 0; } int UIScene_KeybindsMenu::currentLayout() { int layout = app.GetGameSettings(m_iPad, eGameSetting_ControlScheme); if (layout < 0 || layout >= ControllerBinds::MAP_COUNT) layout = 0; return layout; } wstring UIScene_KeybindsMenu::getLayoutLabel(int layout) { static const int s_schemeText[3] = { IDS_CONTROLS_SCHEME0, IDS_CONTROLS_SCHEME1, IDS_CONTROLS_SCHEME2 }; wchar_t text[128]; swprintf(text, 128, L"%ls : %ls", app.GetString(IDS_CURRENT_LAYOUT), layout < 3 ? app.GetString(s_schemeText[layout]) : L"Custom"); return text; } void UIScene_KeybindsMenu::setLayout(int layout) { if (layout < 0 || layout >= ControllerBinds::MAP_COUNT) return; if (currentLayout() != layout) { app.SetGameSettings(m_iPad, eGameSetting_ControlScheme, static_cast(layout)); InputManager.SetJoypadMapVal(m_iPad, static_cast(layout)); } refreshBindLabels(); } void UIScene_KeybindsMenu::tick() { if (m_bNeedsMultiListPopulate) { m_bNeedsMultiListPopulate = false; m_bInitialPopulateDone = false; m_multiList.setupControl(this, m_rootPath, "MultiList"); m_controls.push_back(&m_multiList); m_multiList.clearList(); m_multiList.init(eControl_MultiList); { IggyName funcSpacing = registerFastName(L"SetListSpacing"); IggyDataValue spacingResult; IggyDataValue spacingArg[1]; spacingArg[0].type = IGGY_DATATYPE_number; spacingArg[0].number = (getSceneResolution() == UIScene::eSceneResolution_1080) ? 4.0 : 2.0; IggyPlayerCallMethodRS(getMovie(), &spacingResult, m_rootPath, funcSpacing, 1, spacingArg); } m_multiList.AddNewButton(app.GetString(IDS_RESET_TO_DEFAULTS), eControl_ResetToDefaults); if (m_bController) { registerPadIcons(); m_multiList.AddNewLabel(L" "); m_multiList.AddNewLabel(L"Movement"); m_multiList.AddNewCheckbox(app.GetString(IDS_INVERT_LOOK), eControl_InvertLook, app.GetGameSettings(m_iPad, eGameSetting_ControlInvertLook) != 0); m_multiList.AddNewCheckbox(app.GetString(IDS_SOUTHPAW), eControl_SouthPaw, app.GetGameSettings(m_iPad, eGameSetting_ControlSouthPaw) != 0); m_multiList.AddNewLabel(L"Look", eControl_Look); m_multiList.SetItemFlatLabel(eControl_Look, eControl_ResetToDefaults, L"Look"); m_multiList.AddNewLabel(L"Move/Sprint", eControl_Move); m_multiList.SetItemFlatLabel(eControl_Move, eControl_ResetToDefaults, L"Move/Sprint"); setStickRows(); const wchar_t* currentSection = nullptr; for (int b = 0; b < ControllerBinds::GetBindCount(); ++b) { const wchar_t* section = ControllerBinds::GetBindSection(b); if (currentSection == nullptr || wcscmp(currentSection, section) != 0) { m_multiList.AddNewLabel(L" "); m_multiList.AddNewLabel(section); currentSection = section; } m_multiList.AddNewButton(ControllerBinds::GetBindName(b), eControl_BindBase + b); setBindRow(b, false); } } else { static unsigned int s_keyIconGeneration = 0; wchar_t prefix[64]; swprintf(prefix, 64, L"KeyIcon_%u_", ++s_keyIconGeneration); m_padIconPrefix = prefix; m_multiList.AddNewLabel(L" "); m_multiList.AddNewLabel(L"Options"); m_multiList.AddNewCheckbox(app.GetString(IDS_INVERT_LOOK), eControl_InvertLook, app.GetGameSettings(m_iPad, eGameSetting_ControlInvertLook) != 0); for (int s = 0; s < static_cast(sizeof(s_sections) / sizeof(s_sections[0])); ++s) { m_multiList.AddNewLabel(L" "); m_multiList.AddNewLabel(s_sections[s].title); for (int b = s_sections[s].first; b <= s_sections[s].last; ++b) { m_multiList.AddNewButton(getBindLabel(b, false), eControl_BindBase + b); setBindRow(b, false); } } } IggyName funcDoVert = registerFastName(L"DoVerticalResizeCheck"); IggyName funcHideDesc = registerFastName(L"HideDescription"); IggyDataValue result; IggyPlayerCallMethodRS(getMovie(), &result, m_rootPath, funcDoVert, 0, nullptr); doHorizontalResizeCheck(); IggyPlayerCallMethodRS(getMovie(), &result, m_rootPath, funcHideDesc, 0, nullptr); m_multiList.HighlightItem(eControl_ResetToDefaults); } if (m_bPendingLayout) { m_bPendingLayout = false; setLayout(m_iPendingLayout); } if (m_bPendingCheckboxes) { m_bPendingCheckboxes = false; app.SetGameSettings(m_iPad, eGameSetting_ControlInvertLook, m_multiList.GetCheckboxValue(eControl_InvertLook) ? 1 : 0); if (m_bController) { app.SetGameSettings(m_iPad, eGameSetting_ControlSouthPaw, m_multiList.GetCheckboxValue(eControl_SouthPaw) ? 1 : 0); setStickRows(); } } if (m_bPendingReset) { m_bPendingReset = false; m_bChanged = true; if (m_bController) { ControllerBinds::ResetToDefaults(); setLayout(MAP_STYLE_0); } else { g_KBMInput.ResetBinds(); refreshBindLabels(); } } unsigned int padButtons = InputManager.GetRawButtons(m_iPad) & ControllerBinds::GetBindableButtonMask(); if (m_iCaptureBind >= 0) { if (!m_bCaptureArmed) { if (!anyInputHeld()) m_bCaptureArmed = true; } else if (g_KBMInput.IsKeyPressed(VK_ESCAPE)) { endCapture(); } else if (m_bController) { unsigned int newButtons = padButtons & ~m_uiPrevPadButtons; if (newButtons != 0) { unsigned int button = newButtons & (~newButtons + 1); if (currentLayout() != ControllerBinds::CUSTOM_MAP) { ControllerBinds::CopyLayoutToCustom(static_cast(currentLayout())); setLayout(ControllerBinds::CUSTOM_MAP); } ControllerBinds::Set(m_iCaptureBind, InputManager.ApplyButtonSwap(m_iPad, button)); m_bChanged = true; ui.PlayUISFX(eSFX_Press); endCapture(); } } else { int vk = g_KBMInput.GetPressedKey(); if (vk != 0) { g_KBMInput.SetBind(m_iCaptureBind, vk); m_bChanged = true; ui.PlayUISFX(eSFX_Press); endCapture(); } } } else if (m_bIgnoreInputUntilRelease && !anyInputHeld()) { m_bIgnoreInputUntilRelease = false; g_KBMInput.SetBindCapture(false); } m_uiPrevPadButtons = padButtons; UIScene::tick(); m_bInitialPopulateDone = true; } void UIScene_KeybindsMenu::updateTooltips() { ui.SetTooltips(m_iPad, IDS_TOOLTIPS_SELECT, IDS_TOOLTIPS_BACK); } void UIScene_KeybindsMenu::updateComponents() { bool bNotInGame = (Minecraft::GetInstance()->level == nullptr); if (bNotInGame) { m_parentLayer->showComponent(m_iPad, eUIComponent_Panorama, true); m_parentLayer->showComponent(m_iPad, eUIComponent_Logo, true); } else { m_parentLayer->showComponent(m_iPad, eUIComponent_Panorama, false); m_parentLayer->showComponent(m_iPad, eUIComponent_Logo, app.GetLocalPlayerCount() == 1); } } void UIScene_KeybindsMenu::handleGainFocus(bool navBack) { if (navBack) m_bNeedsMultiListPopulate = true; } void UIScene_KeybindsMenu::handleDestroy() { g_KBMInput.SetBindCapture(false); } void UIScene_KeybindsMenu::handleInput(int iPad, int key, bool repeat, bool pressed, bool released, bool &handled) { if (m_iCaptureBind >= 0 || m_bIgnoreInputUntilRelease) { handled = true; return; } ui.AnimateKeyPress(iPad, key, repeat, pressed, released); switch (key) { case ACTION_MENU_CANCEL: if (pressed) { if (m_bChanged) { if (m_bController) ControllerBinds::Save(); else g_KBMInput.SaveBinds(); } g_KBMInput.SetBindCapture(false); app.CheckGameSettingsChanged(true, iPad); navigateBack(); } break; case ACTION_MENU_OK: case ACTION_MENU_UP: case ACTION_MENU_DOWN: case ACTION_MENU_LEFT: case ACTION_MENU_RIGHT: sendInputToMovie(key, repeat, pressed, released); break; } } void UIScene_KeybindsMenu::handlePress(F64 controlId, F64 childId) { if (m_iCaptureBind >= 0 || m_bIgnoreInputUntilRelease) return; ui.PlayUISFX(eSFX_Press); int id = static_cast(childId); if (id == eControl_ResetToDefaults) { m_bPendingReset = true; } else if (id >= eControl_BindBase && id < eControl_BindBase + getBindCount()) { beginCapture(id - eControl_BindBase); } } void UIScene_KeybindsMenu::handleCheckboxToggled(F64 controlId, bool selected) { if (!m_bInitialPopulateDone) return; ui.PlayUISFX(eSFX_Press); m_bPendingCheckboxes = true; } void UIScene_KeybindsMenu::handleSliderMove(F64 sliderId, F64 currentValue) { int id = static_cast(sliderId); int value = static_cast(currentValue); m_multiList.handleSliderMove(id, value); if (id == eControl_Layout && m_bController && m_bInitialPopulateDone) { m_bPendingLayout = true; m_iPendingLayout = value; } } #endif