#include "stdafx.h" // Non-RmlUi includes that depend on Windows macros #include "UI.h" #include "../../Minecraft.h" // RmlUi includes (Windows macros temporarily suppressed) #pragma push_macro("byte") #pragma push_macro("GetNextSibling") #pragma push_macro("GetFirstChild") #undef byte #undef GetNextSibling #undef GetFirstChild #include "UIScene_SettingsUIMenu.h" #include "RmlManager.h" #include "../../Windows64/KeyboardMouseInput.h" #include #include #include #include #pragma pop_macro("GetFirstChild") #pragma pop_macro("GetNextSibling") #pragma pop_macro("byte") static Rml::String WideToUTF8(LPCWSTR wsz) { if (!wsz) return Rml::String(); int len = WideCharToMultiByte(CP_UTF8, 0, wsz, -1, nullptr, 0, nullptr, nullptr); if (len <= 0) return Rml::String(); Rml::String result(static_cast(len) - 1, '\0'); WideCharToMultiByte(CP_UTF8, 0, wsz, -1, &result[0], len, nullptr, nullptr); return result; } struct ToggleEntry { const char* id; eGameSetting setting; }; struct SliderEntry { const char* id; int* value; int idsString; int setting; }; static const ToggleEntry s_toggles[] = { { "DisplayHUD", eGameSetting_DisplayHUD }, { "DisplayHand", eGameSetting_DisplayHand }, { "DisplayDeathMessages", eGameSetting_DeathMessages }, { "DisplayAnimatedCharacter", eGameSetting_AnimatedCharacter }, { "Splitscreen", eGameSetting_SplitScreenVertical }, { "ShowSplitscreenGamertags", eGameSetting_DisplaySplitscreenGamertags }, { "ShowClassicCrafting", eGameSetting_ClassicCrafting }, }; static const char* s_sliderIds[] = { "UISize", "UISizeSplitscreen" }; UIScene_SettingsUIMenu::UIScene_SettingsUIMenu(int iPad, void *initData, UILayer *parentLayer) : UIScene(iPad, parentLayer) , m_document(nullptr) , m_uiSizeValue(2) , m_uiSizeSplitscreenValue(2) , m_dragSliderIndex(-1) { Rml::Context* ctx = RmlManager::Get().GetContext(); if (!ctx) { app.DebugPrintf("[RmlSettingsUIMenu] No RmlUi context available\n"); return; } m_document = ctx->LoadDocument("SettingsUIMenu.rml"); if (!m_document) { app.DebugPrintf("[RmlSettingsUIMenu] Failed to load SettingsUIMenu.rml\n"); return; } m_document->Show(); for (size_t i = 0; i < sizeof(s_toggles) / sizeof(s_toggles[0]); i++) setToggleChecked(s_toggles[i].id, app.GetGameSettings(m_iPad, s_toggles[i].setting) != 0); m_uiSizeValue = app.GetGameSettings(m_iPad, eGameSetting_UISize) + 1; m_uiSizeSplitscreenValue = app.GetGameSettings(m_iPad, eGameSetting_UISizeSplitscreen) + 1; setSliderValue("UISize", WideToUTF8(app.GetString(IDS_SLIDER_UISIZE)), m_uiSizeValue, 1, 3); setSliderValue("UISizeSplitscreen", WideToUTF8(app.GetString(IDS_SLIDER_UISIZESPLITSCREEN)), m_uiSizeSplitscreenValue, 1, 3); registerEvents(); bool bInGame = (Minecraft::GetInstance()->level != nullptr); bool bPrimaryPlayer = ProfileManager.GetPrimaryPad() == m_iPad; if (bInGame && !bPrimaryPlayer) { Rml::Element* el = m_document->GetElementById("Splitscreen"); if (el) el->SetProperty("display", "none"); el = m_document->GetElementById("ShowSplitscreenGamertags"); if (el) el->SetProperty("display", "none"); } ui.HidePressStart(); TelemetryManager->RecordMenuShown(m_iPad, eUIScene_SettingsUIMenu, 0); } UIScene_SettingsUIMenu::~UIScene_SettingsUIMenu() { if (m_document) { deregisterEvents(); m_document->Close(); m_document = nullptr; } } void UIScene_SettingsUIMenu::registerEvents() { for (size_t i = 0; i < sizeof(s_toggles) / sizeof(s_toggles[0]); i++) { Rml::Element* el = m_document->GetElementById(Rml::String(s_toggles[i].id) + "_toggle"); if (el) el->AddEventListener(Rml::EventId::Click, this); } for (size_t i = 0; i < sizeof(s_sliderIds) / sizeof(s_sliderIds[0]); i++) { Rml::Element* elTrack = m_document->GetElementById(Rml::String(s_sliderIds[i]) + "_track"); if (elTrack) elTrack->AddEventListener(Rml::EventId::Mousedown, this); Rml::Element* elThumb = m_document->GetElementById(Rml::String(s_sliderIds[i]) + "_thumb"); if (elThumb) elThumb->AddEventListener(Rml::EventId::Mousedown, this); } m_document->AddEventListener(Rml::EventId::Mousemove, this); m_document->AddEventListener(Rml::EventId::Mouseup, this); } void UIScene_SettingsUIMenu::deregisterEvents() { for (size_t i = 0; i < sizeof(s_toggles) / sizeof(s_toggles[0]); i++) { Rml::Element* el = m_document->GetElementById(Rml::String(s_toggles[i].id) + "_toggle"); if (el) el->RemoveEventListener(Rml::EventId::Click, this); } for (size_t i = 0; i < sizeof(s_sliderIds) / sizeof(s_sliderIds[0]); i++) { Rml::Element* elTrack = m_document->GetElementById(Rml::String(s_sliderIds[i]) + "_track"); if (elTrack) elTrack->RemoveEventListener(Rml::EventId::Mousedown, this); Rml::Element* elThumb = m_document->GetElementById(Rml::String(s_sliderIds[i]) + "_thumb"); if (elThumb) elThumb->RemoveEventListener(Rml::EventId::Mousedown, this); } m_document->RemoveEventListener(Rml::EventId::Mousemove, this); m_document->RemoveEventListener(Rml::EventId::Mouseup, this); } void UIScene_SettingsUIMenu::setToggleChecked(const Rml::String& id, bool checked) { Rml::Element* el = m_document->GetElementById(id); if (!el) return; el->SetClass("checked", checked); } bool UIScene_SettingsUIMenu::isToggleChecked(const Rml::String& id) { Rml::Element* el = m_document->GetElementById(id); return el && el->IsClassSet("checked"); } void UIScene_SettingsUIMenu::setSliderValue(const Rml::String& id, const Rml::String& labelPrefix, int value, int min, int max) { Rml::Element* label = m_document->GetElementById(id + "_label"); if (label) { char buf[64]; snprintf(buf, sizeof(buf), "%s: %d", labelPrefix.c_str(), value); label->SetInnerRML(Rml::String(buf)); } Rml::Element* thumb = m_document->GetElementById(id + "_thumb"); if (thumb) { float fraction = (max > min) ? (float)(value - min) / (float)(max - min) : 0.0f; char buf[16]; snprintf(buf, sizeof(buf), "%.0f%%", fraction * 100.0f); thumb->SetProperty("left", Rml::String(buf)); } } int UIScene_SettingsUIMenu::getSliderValue(const Rml::String& id) { if (id == "UISize") return m_uiSizeValue; if (id == "UISizeSplitscreen") return m_uiSizeSplitscreenValue; return 0; } void UIScene_SettingsUIMenu::handleSliderMouseEvent(int sliderIndex, const Rml::Event& event) { if (!m_document) return; Rml::String trackId = Rml::String(s_sliderIds[sliderIndex]) + "_track"; Rml::Element* track = m_document->GetElementById(trackId); if (!track) return; float mouseX = event.GetParameter("mouse_x", 0.0f); Rml::Box box = track->GetBox(); float boxLeft = track->GetAbsoluteOffset().x; float boxWidth = box.GetSize().x; if (boxWidth > 0.0f) { float fraction = (mouseX - boxLeft) / boxWidth; if (fraction < 0.0f) fraction = 0.0f; if (fraction > 1.0f) fraction = 1.0f; int newValue = 1 + static_cast(fraction * 2.0f + 0.5f); if (newValue < 1) newValue = 1; if (newValue > 3) newValue = 3; handleSliderMove(sliderIndex, newValue); } } void UIScene_SettingsUIMenu::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); if (app.GetLocalPlayerCount() == 1) m_parentLayer->showComponent(m_iPad, eUIComponent_Logo, true); else m_parentLayer->showComponent(m_iPad, eUIComponent_Logo, false); } } void UIScene_SettingsUIMenu::gainFocus() { if (!bHasFocus && stealsFocus()) { bHasFocus = true; updateComponents(); handleGainFocus(m_bFocussedOnce); if (bHasFocus) m_bFocussedOnce = true; if (m_document) m_document->Show(); } } void UIScene_SettingsUIMenu::handleLoseFocus() { if (m_document) m_document->Hide(); } void UIScene_SettingsUIMenu::tick() { m_hasTickedOnce = true; UIScene::tick(); Rml::Context* ctx = RmlManager::Get().GetContext(); if (!ctx || !bHasFocus) return; ctx->ProcessMouseMove(g_KBMInput.GetMouseX(), g_KBMInput.GetMouseY(), 0); if (g_KBMInput.IsMouseButtonPressed(KeyboardMouseInput::MOUSE_LEFT)) ctx->ProcessMouseButtonDown(0, 0); if (g_KBMInput.IsMouseButtonReleased(KeyboardMouseInput::MOUSE_LEFT)) ctx->ProcessMouseButtonUp(0, 0); if (g_KBMInput.IsMouseButtonPressed(KeyboardMouseInput::MOUSE_RIGHT)) ctx->ProcessMouseButtonDown(1, 0); if (g_KBMInput.IsMouseButtonReleased(KeyboardMouseInput::MOUSE_RIGHT)) ctx->ProcessMouseButtonUp(1, 0); int wheelDelta = g_KBMInput.GetMouseWheel(); if (wheelDelta != 0) ctx->ProcessMouseWheel(wheelDelta / -120, 0); RmlManager::Get().Update(); } void UIScene_SettingsUIMenu::ProcessEvent(Rml::Event& event) { if (event.GetId() == Rml::EventId::Click) { Rml::Element* el = event.GetCurrentElement(); const Rml::String& id = el->GetId(); for (size_t i = 0; i < sizeof(s_toggles) / sizeof(s_toggles[0]); i++) { if (id == Rml::String(s_toggles[i].id) + "_toggle") { Rml::Element* row = el->GetParentNode(); if (row) row->SetClass("checked", !row->IsClassSet("checked")); ui.PlayUISFX(eSFX_Press); return; } } } else if (event.GetId() == Rml::EventId::Mousedown) { Rml::Element* el = event.GetCurrentElement(); const Rml::String& id = el->GetId(); for (size_t i = 0; i < sizeof(s_sliderIds) / sizeof(s_sliderIds[0]); i++) { Rml::String trackId = Rml::String(s_sliderIds[i]) + "_track"; Rml::String thumbId = Rml::String(s_sliderIds[i]) + "_thumb"; if (id == trackId || id == thumbId) { m_dragSliderIndex = static_cast(i); handleSliderMouseEvent(static_cast(i), event); ui.PlayUISFX(eSFX_Scroll); return; } } } else if (event.GetId() == Rml::EventId::Mousemove) { if (m_dragSliderIndex >= 0) { int oldValue = getSliderValue(s_sliderIds[m_dragSliderIndex]); handleSliderMouseEvent(m_dragSliderIndex, event); if (getSliderValue(s_sliderIds[m_dragSliderIndex]) != oldValue) ui.PlayUISFX(eSFX_Scroll); } } else if (event.GetId() == Rml::EventId::Mouseup) { m_dragSliderIndex = -1; } } static void saveToggle(Rml::ElementDocument* doc, int iPad, const ToggleEntry& t) { Rml::Element* el = doc->GetElementById(t.id); bool checked = el && el->IsClassSet("checked"); app.SetGameSettings(iPad, t.setting, checked ? 1 : 0); } void UIScene_SettingsUIMenu::handleInput(int iPad, int key, bool repeat, bool pressed, bool released, bool &handled) { Rml::Context* ctx = RmlManager::Get().GetContext(); if (!ctx) return; if (!pressed) return; switch (key) { case ACTION_MENU_UP: ctx->ProcessKeyDown(Rml::Input::KI_UP, 0); handled = true; break; case ACTION_MENU_DOWN: ctx->ProcessKeyDown(Rml::Input::KI_DOWN, 0); handled = true; break; case ACTION_MENU_LEFT: ctx->ProcessKeyDown(Rml::Input::KI_LEFT, 0); handled = true; break; case ACTION_MENU_RIGHT: ctx->ProcessKeyDown(Rml::Input::KI_RIGHT, 0); handled = true; break; case ACTION_MENU_OK: ctx->ProcessKeyDown(Rml::Input::KI_RETURN, 0); handled = true; break; case ACTION_MENU_CANCEL: case ACTION_MENU_PAUSEMENU: for (size_t i = 0; i < sizeof(s_toggles) / sizeof(s_toggles[0]); i++) saveToggle(m_document, m_iPad, s_toggles[i]); if (app.GetGameSettings(m_iPad, eGameSetting_SplitScreenVertical) != (isToggleChecked("Splitscreen") ? 1 : 0)) { app.SetGameSettings(m_iPad, eGameSetting_SplitScreenVertical, isToggleChecked("Splitscreen") ? 1 : 0); if (app.GetLocalPlayerCount() == 2) ui.CloseAllPlayersScenes(); else navigateBack(); } else { navigateBack(); } handled = true; break; default: break; } } void UIScene_SettingsUIMenu::handleSliderMove(int sliderIndex, int value) { int* member = (sliderIndex == 0) ? &m_uiSizeValue : &m_uiSizeSplitscreenValue; const char* id = s_sliderIds[sliderIndex]; int idsString = (sliderIndex == 0) ? IDS_SLIDER_UISIZE : IDS_SLIDER_UISIZESPLITSCREEN; eGameSetting setting = (sliderIndex == 0) ? eGameSetting_UISize : eGameSetting_UISizeSplitscreen; *member = value; Rml::String prefix = WideToUTF8(app.GetString(idsString)); setSliderValue(id, prefix, value, 1, 3); if (value != app.GetGameSettings(m_iPad, setting) + 1) { app.SetGameSettings(m_iPad, setting, value - 1); ui.UpdateSelectedItemPos(m_iPad); } }