1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-03-28 08:37:12 +00:00

Use std::unique_ptr in MessageBoxManager

This commit is contained in:
Evil Eye 2022-08-31 19:44:04 +02:00
parent 30d320f651
commit 84911a300b
6 changed files with 26 additions and 40 deletions

View File

@ -150,7 +150,7 @@ namespace MWBase
virtual MWGui::CountDialog* getCountDialog() = 0; virtual MWGui::CountDialog* getCountDialog() = 0;
virtual MWGui::ConfirmationDialog* getConfirmationDialog() = 0; virtual MWGui::ConfirmationDialog* getConfirmationDialog() = 0;
virtual MWGui::TradeWindow* getTradeWindow() = 0; virtual MWGui::TradeWindow* getTradeWindow() = 0;
virtual const std::vector<MWGui::MessageBox*> getActiveMessageBoxes() = 0; virtual const std::vector<std::unique_ptr<MWGui::MessageBox>>& getActiveMessageBoxes() const = 0;
virtual MWGui::PostProcessorHud* getPostProcessorHud() = 0; virtual MWGui::PostProcessorHud* getPostProcessorHud() = 0;
/// Make the player use an item, while updating GUI state accordingly /// Make the player use an item, while updating GUI state accordingly

View File

@ -18,7 +18,6 @@ namespace MWGui
MessageBoxManager::MessageBoxManager (float timePerChar) MessageBoxManager::MessageBoxManager (float timePerChar)
{ {
mInterMessageBoxe = nullptr;
mStaticMessageBox = nullptr; mStaticMessageBox = nullptr;
mLastButtonPressed = -1; mLastButtonPressed = -1;
mMessageBoxSpeed = timePerChar; mMessageBoxSpeed = timePerChar;
@ -39,31 +38,22 @@ namespace MWGui
if (mInterMessageBoxe) if (mInterMessageBoxe)
{ {
mInterMessageBoxe->setVisible(false); mInterMessageBoxe->setVisible(false);
mInterMessageBoxe.reset();
delete mInterMessageBoxe;
mInterMessageBoxe = nullptr;
} }
for (MessageBox* messageBox : mMessageBoxes)
{
if (messageBox == mStaticMessageBox)
mStaticMessageBox = nullptr;
delete messageBox;
}
mMessageBoxes.clear(); mMessageBoxes.clear();
mStaticMessageBox = nullptr;
mLastButtonPressed = -1; mLastButtonPressed = -1;
} }
void MessageBoxManager::onFrame (float frameDuration) void MessageBoxManager::onFrame (float frameDuration)
{ {
std::vector<MessageBox*>::iterator it; for(auto it = mMessageBoxes.begin(); it != mMessageBoxes.end();)
for(it = mMessageBoxes.begin(); it != mMessageBoxes.end();)
{ {
(*it)->mCurrentTime += frameDuration; (*it)->mCurrentTime += frameDuration;
if((*it)->mCurrentTime >= (*it)->mMaxTime && *it != mStaticMessageBox) if((*it)->mCurrentTime >= (*it)->mMaxTime && it->get() != mStaticMessageBox)
{ {
delete *it;
it = mMessageBoxes.erase(it); it = mMessageBoxes.erase(it);
} }
else else
@ -71,7 +61,7 @@ namespace MWGui
} }
float height = 0; float height = 0;
it = mMessageBoxes.begin(); auto it = mMessageBoxes.begin();
while(it != mMessageBoxes.end()) while(it != mMessageBoxes.end())
{ {
(*it)->update(static_cast<int>(height)); (*it)->update(static_cast<int>(height));
@ -82,8 +72,7 @@ namespace MWGui
if(mInterMessageBoxe != nullptr && mInterMessageBoxe->mMarkedToDelete) { if(mInterMessageBoxe != nullptr && mInterMessageBoxe->mMarkedToDelete) {
mLastButtonPressed = mInterMessageBoxe->readPressedButton(); mLastButtonPressed = mInterMessageBoxe->readPressedButton();
mInterMessageBoxe->setVisible(false); mInterMessageBoxe->setVisible(false);
delete mInterMessageBoxe; mInterMessageBoxe.reset();
mInterMessageBoxe = nullptr;
MWBase::Environment::get().getInputManager()->changeInputMode( MWBase::Environment::get().getInputManager()->changeInputMode(
MWBase::Environment::get().getWindowManager()->isGuiMode()); MWBase::Environment::get().getWindowManager()->isGuiMode());
} }
@ -91,25 +80,24 @@ namespace MWGui
void MessageBoxManager::createMessageBox(std::string_view message, bool stat) void MessageBoxManager::createMessageBox(std::string_view message, bool stat)
{ {
MessageBox *box = new MessageBox(*this, message); auto box = std::make_unique<MessageBox>(*this, message);
box->mCurrentTime = 0; box->mCurrentTime = 0;
auto realMessage = MyGUI::LanguageManager::getInstance().replaceTags({message.data(), message.size()}); auto realMessage = MyGUI::LanguageManager::getInstance().replaceTags({message.data(), message.size()});
box->mMaxTime = realMessage.length()*mMessageBoxSpeed; box->mMaxTime = realMessage.length()*mMessageBoxSpeed;
if(stat) if(stat)
mStaticMessageBox = box; mStaticMessageBox = box.get();
box->setVisible(mVisible); box->setVisible(mVisible);
mMessageBoxes.push_back(box); mMessageBoxes.push_back(std::move(box));
if(mMessageBoxes.size() > 3) { if(mMessageBoxes.size() > 3) {
delete *mMessageBoxes.begin();
mMessageBoxes.erase(mMessageBoxes.begin()); mMessageBoxes.erase(mMessageBoxes.begin());
} }
int height = 0; int height = 0;
for (MessageBox* messageBox : mMessageBoxes) for (const auto& messageBox : mMessageBoxes)
{ {
messageBox->update(height); messageBox->update(height);
height += messageBox->getHeight(); height += messageBox->getHeight();
@ -128,11 +116,9 @@ namespace MWGui
{ {
Log(Debug::Warning) << "Warning: replacing an interactive message box that was not answered yet"; Log(Debug::Warning) << "Warning: replacing an interactive message box that was not answered yet";
mInterMessageBoxe->setVisible(false); mInterMessageBoxe->setVisible(false);
delete mInterMessageBoxe;
mInterMessageBoxe = nullptr;
} }
mInterMessageBoxe = new InteractiveMessageBox(*this, std::string{message}, buttons); mInterMessageBoxe = std::make_unique<InteractiveMessageBox>(*this, std::string{message}, buttons);
mLastButtonPressed = -1; mLastButtonPressed = -1;
return true; return true;
@ -145,12 +131,10 @@ namespace MWGui
bool MessageBoxManager::removeMessageBox (MessageBox *msgbox) bool MessageBoxManager::removeMessageBox (MessageBox *msgbox)
{ {
std::vector<MessageBox*>::iterator it; for(auto it = mMessageBoxes.begin(); it != mMessageBoxes.end(); ++it)
for(it = mMessageBoxes.begin(); it != mMessageBoxes.end(); ++it)
{ {
if((*it) == msgbox) if(it->get() == msgbox)
{ {
delete (*it);
mMessageBoxes.erase(it); mMessageBoxes.erase(it);
return true; return true;
} }
@ -158,7 +142,7 @@ namespace MWGui
return false; return false;
} }
const std::vector<MessageBox*> MessageBoxManager::getActiveMessageBoxes() const std::vector<std::unique_ptr<MessageBox>>& MessageBoxManager::getActiveMessageBoxes() const
{ {
return mMessageBoxes; return mMessageBoxes;
} }
@ -174,7 +158,7 @@ namespace MWGui
void MessageBoxManager::setVisible(bool value) void MessageBoxManager::setVisible(bool value)
{ {
mVisible = value; mVisible = value;
for (MessageBox* messageBox : mMessageBoxes) for (const auto& messageBox : mMessageBoxes)
messageBox->setVisible(value); messageBox->setVisible(value);
} }

View File

@ -1,6 +1,8 @@
#ifndef MWGUI_MESSAGE_BOX_H #ifndef MWGUI_MESSAGE_BOX_H
#define MWGUI_MESSAGE_BOX_H #define MWGUI_MESSAGE_BOX_H
#include <memory>
#include "windowbase.hpp" #include "windowbase.hpp"
namespace MyGUI namespace MyGUI
@ -28,7 +30,7 @@ namespace MWGui
int getMessagesCount(); int getMessagesCount();
const InteractiveMessageBox* getInteractiveMessageBox() const { return mInterMessageBoxe; } const InteractiveMessageBox* getInteractiveMessageBox() const { return mInterMessageBoxe.get(); }
/// Remove all message boxes /// Remove all message boxes
void clear(); void clear();
@ -47,11 +49,11 @@ namespace MWGui
void setVisible(bool value); void setVisible(bool value);
const std::vector<MessageBox*> getActiveMessageBoxes(); const std::vector<std::unique_ptr<MessageBox>>& getActiveMessageBoxes() const;
private: private:
std::vector<MessageBox*> mMessageBoxes; std::vector<std::unique_ptr<MessageBox>> mMessageBoxes;
InteractiveMessageBox* mInterMessageBoxe; std::unique_ptr<InteractiveMessageBox> mInterMessageBoxe;
MessageBox* mStaticMessageBox; MessageBox* mStaticMessageBox;
float mMessageBoxSpeed; float mMessageBoxSpeed;
int mLastButtonPressed; int mLastButtonPressed;

View File

@ -773,7 +773,7 @@ namespace MWGui
mMessageBoxManager->removeStaticMessageBox(); mMessageBoxManager->removeStaticMessageBox();
} }
const std::vector<MWGui::MessageBox*> WindowManager::getActiveMessageBoxes() const std::vector<std::unique_ptr<MWGui::MessageBox>>& WindowManager::getActiveMessageBoxes() const
{ {
return mMessageBoxManager->getActiveMessageBoxes(); return mMessageBoxManager->getActiveMessageBoxes();
} }

View File

@ -190,7 +190,7 @@ namespace MWGui
MWGui::CountDialog* getCountDialog() override; MWGui::CountDialog* getCountDialog() override;
MWGui::ConfirmationDialog* getConfirmationDialog() override; MWGui::ConfirmationDialog* getConfirmationDialog() override;
MWGui::TradeWindow* getTradeWindow() override; MWGui::TradeWindow* getTradeWindow() override;
const std::vector<MWGui::MessageBox*> getActiveMessageBoxes() override; const std::vector<std::unique_ptr<MWGui::MessageBox>>& getActiveMessageBoxes() const override;
MWGui::PostProcessorHud* getPostProcessorHud() override; MWGui::PostProcessorHud* getPostProcessorHud() override;
/// Make the player use an item, while updating GUI state accordingly /// Make the player use an item, while updating GUI state accordingly

View File

@ -97,8 +97,8 @@ namespace MWInput
if (playerPtr.getClass().getEncumbrance(playerPtr) > playerPtr.getClass().getCapacity(playerPtr)) if (playerPtr.getClass().getEncumbrance(playerPtr) > playerPtr.getClass().getCapacity(playerPtr))
{ {
player.setAutoMove (false); player.setAutoMove (false);
std::vector<MWGui::MessageBox*> msgboxs = MWBase::Environment::get().getWindowManager()->getActiveMessageBoxes(); const auto& msgboxs = MWBase::Environment::get().getWindowManager()->getActiveMessageBoxes();
const std::vector<MWGui::MessageBox*>::iterator it = std::find_if(msgboxs.begin(), msgboxs.end(), [](MWGui::MessageBox*& msgbox) auto it = std::find_if(msgboxs.begin(), msgboxs.end(), [](const std::unique_ptr<MWGui::MessageBox>& msgbox)
{ {
return (msgbox->getMessage() == "#{sNotifyMessage59}"); return (msgbox->getMessage() == "#{sNotifyMessage59}");
}); });