diff --git a/apps/openmw/mwbase/windowmanager.hpp b/apps/openmw/mwbase/windowmanager.hpp index 95eb3e68df..efca05d0cd 100644 --- a/apps/openmw/mwbase/windowmanager.hpp +++ b/apps/openmw/mwbase/windowmanager.hpp @@ -150,7 +150,7 @@ namespace MWBase virtual MWGui::CountDialog* getCountDialog() = 0; virtual MWGui::ConfirmationDialog* getConfirmationDialog() = 0; virtual MWGui::TradeWindow* getTradeWindow() = 0; - virtual const std::vector getActiveMessageBoxes() = 0; + virtual const std::vector>& getActiveMessageBoxes() const = 0; virtual MWGui::PostProcessorHud* getPostProcessorHud() = 0; /// Make the player use an item, while updating GUI state accordingly diff --git a/apps/openmw/mwgui/messagebox.cpp b/apps/openmw/mwgui/messagebox.cpp index e1ea0a46b9..4e163f7d37 100644 --- a/apps/openmw/mwgui/messagebox.cpp +++ b/apps/openmw/mwgui/messagebox.cpp @@ -18,7 +18,6 @@ namespace MWGui MessageBoxManager::MessageBoxManager (float timePerChar) { - mInterMessageBoxe = nullptr; mStaticMessageBox = nullptr; mLastButtonPressed = -1; mMessageBoxSpeed = timePerChar; @@ -39,31 +38,22 @@ namespace MWGui if (mInterMessageBoxe) { mInterMessageBoxe->setVisible(false); - - delete mInterMessageBoxe; - mInterMessageBoxe = nullptr; + mInterMessageBoxe.reset(); } - for (MessageBox* messageBox : mMessageBoxes) - { - if (messageBox == mStaticMessageBox) - mStaticMessageBox = nullptr; - delete messageBox; - } mMessageBoxes.clear(); + mStaticMessageBox = nullptr; mLastButtonPressed = -1; } void MessageBoxManager::onFrame (float frameDuration) { - std::vector::iterator it; - for(it = mMessageBoxes.begin(); it != mMessageBoxes.end();) + for(auto it = mMessageBoxes.begin(); it != mMessageBoxes.end();) { (*it)->mCurrentTime += frameDuration; - if((*it)->mCurrentTime >= (*it)->mMaxTime && *it != mStaticMessageBox) + if((*it)->mCurrentTime >= (*it)->mMaxTime && it->get() != mStaticMessageBox) { - delete *it; it = mMessageBoxes.erase(it); } else @@ -71,7 +61,7 @@ namespace MWGui } float height = 0; - it = mMessageBoxes.begin(); + auto it = mMessageBoxes.begin(); while(it != mMessageBoxes.end()) { (*it)->update(static_cast(height)); @@ -82,8 +72,7 @@ namespace MWGui if(mInterMessageBoxe != nullptr && mInterMessageBoxe->mMarkedToDelete) { mLastButtonPressed = mInterMessageBoxe->readPressedButton(); mInterMessageBoxe->setVisible(false); - delete mInterMessageBoxe; - mInterMessageBoxe = nullptr; + mInterMessageBoxe.reset(); MWBase::Environment::get().getInputManager()->changeInputMode( MWBase::Environment::get().getWindowManager()->isGuiMode()); } @@ -91,25 +80,24 @@ namespace MWGui void MessageBoxManager::createMessageBox(std::string_view message, bool stat) { - MessageBox *box = new MessageBox(*this, message); + auto box = std::make_unique(*this, message); box->mCurrentTime = 0; auto realMessage = MyGUI::LanguageManager::getInstance().replaceTags({message.data(), message.size()}); box->mMaxTime = realMessage.length()*mMessageBoxSpeed; if(stat) - mStaticMessageBox = box; + mStaticMessageBox = box.get(); box->setVisible(mVisible); - mMessageBoxes.push_back(box); + mMessageBoxes.push_back(std::move(box)); if(mMessageBoxes.size() > 3) { - delete *mMessageBoxes.begin(); mMessageBoxes.erase(mMessageBoxes.begin()); } int height = 0; - for (MessageBox* messageBox : mMessageBoxes) + for (const auto& messageBox : mMessageBoxes) { messageBox->update(height); height += messageBox->getHeight(); @@ -128,11 +116,9 @@ namespace MWGui { Log(Debug::Warning) << "Warning: replacing an interactive message box that was not answered yet"; mInterMessageBoxe->setVisible(false); - delete mInterMessageBoxe; - mInterMessageBoxe = nullptr; } - mInterMessageBoxe = new InteractiveMessageBox(*this, std::string{message}, buttons); + mInterMessageBoxe = std::make_unique(*this, std::string{message}, buttons); mLastButtonPressed = -1; return true; @@ -145,12 +131,10 @@ namespace MWGui bool MessageBoxManager::removeMessageBox (MessageBox *msgbox) { - std::vector::iterator it; - for(it = mMessageBoxes.begin(); it != mMessageBoxes.end(); ++it) + for(auto it = mMessageBoxes.begin(); it != mMessageBoxes.end(); ++it) { - if((*it) == msgbox) + if(it->get() == msgbox) { - delete (*it); mMessageBoxes.erase(it); return true; } @@ -158,7 +142,7 @@ namespace MWGui return false; } - const std::vector MessageBoxManager::getActiveMessageBoxes() + const std::vector>& MessageBoxManager::getActiveMessageBoxes() const { return mMessageBoxes; } @@ -174,7 +158,7 @@ namespace MWGui void MessageBoxManager::setVisible(bool value) { mVisible = value; - for (MessageBox* messageBox : mMessageBoxes) + for (const auto& messageBox : mMessageBoxes) messageBox->setVisible(value); } diff --git a/apps/openmw/mwgui/messagebox.hpp b/apps/openmw/mwgui/messagebox.hpp index 7552bb50d6..3fec60caee 100644 --- a/apps/openmw/mwgui/messagebox.hpp +++ b/apps/openmw/mwgui/messagebox.hpp @@ -1,6 +1,8 @@ #ifndef MWGUI_MESSAGE_BOX_H #define MWGUI_MESSAGE_BOX_H +#include + #include "windowbase.hpp" namespace MyGUI @@ -28,7 +30,7 @@ namespace MWGui int getMessagesCount(); - const InteractiveMessageBox* getInteractiveMessageBox() const { return mInterMessageBoxe; } + const InteractiveMessageBox* getInteractiveMessageBox() const { return mInterMessageBoxe.get(); } /// Remove all message boxes void clear(); @@ -47,11 +49,11 @@ namespace MWGui void setVisible(bool value); - const std::vector getActiveMessageBoxes(); + const std::vector>& getActiveMessageBoxes() const; private: - std::vector mMessageBoxes; - InteractiveMessageBox* mInterMessageBoxe; + std::vector> mMessageBoxes; + std::unique_ptr mInterMessageBoxe; MessageBox* mStaticMessageBox; float mMessageBoxSpeed; int mLastButtonPressed; diff --git a/apps/openmw/mwgui/windowmanagerimp.cpp b/apps/openmw/mwgui/windowmanagerimp.cpp index 600e77ae51..892345b99b 100644 --- a/apps/openmw/mwgui/windowmanagerimp.cpp +++ b/apps/openmw/mwgui/windowmanagerimp.cpp @@ -773,7 +773,7 @@ namespace MWGui mMessageBoxManager->removeStaticMessageBox(); } - const std::vector WindowManager::getActiveMessageBoxes() + const std::vector>& WindowManager::getActiveMessageBoxes() const { return mMessageBoxManager->getActiveMessageBoxes(); } diff --git a/apps/openmw/mwgui/windowmanagerimp.hpp b/apps/openmw/mwgui/windowmanagerimp.hpp index 0b506d17a2..904f2cd78e 100644 --- a/apps/openmw/mwgui/windowmanagerimp.hpp +++ b/apps/openmw/mwgui/windowmanagerimp.hpp @@ -190,7 +190,7 @@ namespace MWGui MWGui::CountDialog* getCountDialog() override; MWGui::ConfirmationDialog* getConfirmationDialog() override; MWGui::TradeWindow* getTradeWindow() override; - const std::vector getActiveMessageBoxes() override; + const std::vector>& getActiveMessageBoxes() const override; MWGui::PostProcessorHud* getPostProcessorHud() override; /// Make the player use an item, while updating GUI state accordingly diff --git a/apps/openmw/mwinput/actionmanager.cpp b/apps/openmw/mwinput/actionmanager.cpp index 458066a5ee..610f88cef9 100644 --- a/apps/openmw/mwinput/actionmanager.cpp +++ b/apps/openmw/mwinput/actionmanager.cpp @@ -97,8 +97,8 @@ namespace MWInput if (playerPtr.getClass().getEncumbrance(playerPtr) > playerPtr.getClass().getCapacity(playerPtr)) { player.setAutoMove (false); - std::vector msgboxs = MWBase::Environment::get().getWindowManager()->getActiveMessageBoxes(); - const std::vector::iterator it = std::find_if(msgboxs.begin(), msgboxs.end(), [](MWGui::MessageBox*& msgbox) + const auto& msgboxs = MWBase::Environment::get().getWindowManager()->getActiveMessageBoxes(); + auto it = std::find_if(msgboxs.begin(), msgboxs.end(), [](const std::unique_ptr& msgbox) { return (msgbox->getMessage() == "#{sNotifyMessage59}"); });