From 3b42d02cfc602230c2d92f2369235269e2cecb8a Mon Sep 17 00:00:00 2001 From: Andrew Lanzone Date: Sun, 11 May 2025 00:20:31 -0700 Subject: [PATCH] Add controller support to message boxes --- apps/openmw/mwgui/messagebox.cpp | 50 ++++++++++++++++++++++++++++++++ apps/openmw/mwgui/messagebox.hpp | 3 ++ 2 files changed, 53 insertions(+) diff --git a/apps/openmw/mwgui/messagebox.cpp b/apps/openmw/mwgui/messagebox.cpp index 1d6e1511c4..7bc6d70931 100644 --- a/apps/openmw/mwgui/messagebox.cpp +++ b/apps/openmw/mwgui/messagebox.cpp @@ -8,6 +8,7 @@ #include #include +#include #include "../mwbase/environment.hpp" #include "../mwbase/inputmanager.hpp" @@ -259,6 +260,7 @@ namespace MWGui button->setCaptionWithReplacing(buttonId); button->eventMouseButtonClick += MyGUI::newDelegate(this, &InteractiveMessageBox::mousePressed); + trackFocusEvents(button); mButtons.push_back(button); @@ -280,6 +282,12 @@ namespace MWGui } } + if (Settings::gui().mControllerMenus && mButtons.size() > 1) + { + // If we have more than one button, we need to set the focus to the first one. + mButtons[0]->setStateSelected(true); + } + MyGUI::IntSize mainWidgetSize; if (buttonsWidth < textSize.width) { @@ -431,4 +439,46 @@ namespace MWGui return mButtonPressed; } + bool InteractiveMessageBox::onControllerButtonEvent(const SDL_ControllerButtonEvent& arg) + { + if (arg.button == SDL_CONTROLLER_BUTTON_A) + { + if (mMouseFocus != nullptr) + return false; + + buttonActivated(mButtons[mControllerFocus]); + return true; + } + else if (arg.button == SDL_CONTROLLER_BUTTON_B) + { + if (mButtons.size() == 1) + buttonActivated(mButtons[0]); + } + else if (arg.button == SDL_CONTROLLER_BUTTON_DPAD_LEFT) + { + if (mButtons.size() > 1) + { + mButtons[mControllerFocus]->setStateSelected(false); + if (mControllerFocus == 0) + mControllerFocus = mButtons.size() - 1; + else + mControllerFocus--; + mButtons[mControllerFocus]->setStateSelected(true); + } + } + else if (arg.button == SDL_CONTROLLER_BUTTON_DPAD_RIGHT) + { + if (mButtons.size() > 1) + { + mButtons[mControllerFocus]->setStateSelected(false); + if (mControllerFocus == mButtons.size() - 1) + mControllerFocus = 0; + else + mControllerFocus++; + mButtons[mControllerFocus]->setStateSelected(true); + } + } + + return true; + } } diff --git a/apps/openmw/mwgui/messagebox.hpp b/apps/openmw/mwgui/messagebox.hpp index feb717e0ad..e6128ee0d1 100644 --- a/apps/openmw/mwgui/messagebox.hpp +++ b/apps/openmw/mwgui/messagebox.hpp @@ -103,6 +103,8 @@ namespace MWGui bool mMarkedToDelete; + bool onControllerButtonEvent(const SDL_ControllerButtonEvent& arg) override; + private: void buttonActivated(MyGUI::Widget* _widget); @@ -114,6 +116,7 @@ namespace MWGui int mButtonPressed; int mDefaultFocus; bool mImmediate; + int mControllerFocus = 0; }; }