1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-02-21 09:09:43 +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::ConfirmationDialog* getConfirmationDialog() = 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;
/// Make the player use an item, while updating GUI state accordingly

View file

@ -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<MessageBox*>::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<int>(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<MessageBox>(*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<InteractiveMessageBox>(*this, std::string{message}, buttons);
mLastButtonPressed = -1;
return true;
@ -145,12 +131,10 @@ namespace MWGui
bool MessageBoxManager::removeMessageBox (MessageBox *msgbox)
{
std::vector<MessageBox*>::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<MessageBox*> MessageBoxManager::getActiveMessageBoxes()
const std::vector<std::unique_ptr<MessageBox>>& 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);
}

View file

@ -1,6 +1,8 @@
#ifndef MWGUI_MESSAGE_BOX_H
#define MWGUI_MESSAGE_BOX_H
#include <memory>
#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<MessageBox*> getActiveMessageBoxes();
const std::vector<std::unique_ptr<MessageBox>>& getActiveMessageBoxes() const;
private:
std::vector<MessageBox*> mMessageBoxes;
InteractiveMessageBox* mInterMessageBoxe;
std::vector<std::unique_ptr<MessageBox>> mMessageBoxes;
std::unique_ptr<InteractiveMessageBox> mInterMessageBoxe;
MessageBox* mStaticMessageBox;
float mMessageBoxSpeed;
int mLastButtonPressed;

View file

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

View file

@ -190,7 +190,7 @@ namespace MWGui
MWGui::CountDialog* getCountDialog() override;
MWGui::ConfirmationDialog* getConfirmationDialog() 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;
/// 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))
{
player.setAutoMove (false);
std::vector<MWGui::MessageBox*> msgboxs = MWBase::Environment::get().getWindowManager()->getActiveMessageBoxes();
const std::vector<MWGui::MessageBox*>::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<MWGui::MessageBox>& msgbox)
{
return (msgbox->getMessage() == "#{sNotifyMessage59}");
});