1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-03-19 16:21:08 +00:00

Simplify message box manager, should fix a random bug with boxes not disappearing

This commit is contained in:
scrawl 2013-11-20 05:49:05 +01:00
parent 3452bd2e0b
commit fa63924884
2 changed files with 22 additions and 61 deletions

View File

@ -10,7 +10,6 @@ namespace MWGui
MessageBoxManager::MessageBoxManager () MessageBoxManager::MessageBoxManager ()
{ {
// defines
mMessageBoxSpeed = 0.1; mMessageBoxSpeed = 0.1;
mInterMessageBoxe = NULL; mInterMessageBoxe = NULL;
mStaticMessageBox = NULL; mStaticMessageBox = NULL;
@ -19,47 +18,26 @@ namespace MWGui
void MessageBoxManager::onFrame (float frameDuration) void MessageBoxManager::onFrame (float frameDuration)
{ {
std::vector<MessageBoxManagerTimer>::iterator it; std::vector<MessageBox*>::iterator it;
for(it = mTimers.begin(); it != mTimers.end();) for(it = mMessageBoxes.begin(); it != mMessageBoxes.end();)
{ {
// if this messagebox is already deleted, remove the timer and move on (*it)->mCurrentTime += frameDuration;
if (std::find(mMessageBoxes.begin(), mMessageBoxes.end(), it->messageBox) == mMessageBoxes.end()) if((*it)->mCurrentTime >= (*it)->mMaxTime && *it != mStaticMessageBox)
{ {
it = mTimers.erase(it); delete *it;
continue; it = mMessageBoxes.erase(it);
}
it->current += frameDuration;
if(it->current >= it->max)
{
it->messageBox->mMarkedToDelete = true;
if(*mMessageBoxes.begin() == it->messageBox) // if this box is the last one
{
// collect all with mMarkedToDelete and delete them.
// and place the other messageboxes on the right position
int height = 0;
std::vector<MessageBox*>::iterator it2 = mMessageBoxes.begin();
while(it2 != mMessageBoxes.end())
{
if((*it2)->mMarkedToDelete)
{
delete (*it2);
it2 = mMessageBoxes.erase(it2);
}
else {
(*it2)->update(height);
height += (*it2)->getHeight();
++it2;
}
}
}
it = mTimers.erase(it);
} }
else else
{
++it; ++it;
} }
float height = 0;
it = mMessageBoxes.begin();
while(it != mMessageBoxes.end())
{
(*it)->update(height);
height += (*it)->getHeight();
++it;
} }
if(mInterMessageBoxe != NULL && mInterMessageBoxe->mMarkedToDelete) { if(mInterMessageBoxe != NULL && mInterMessageBoxe->mMarkedToDelete) {
@ -74,14 +52,13 @@ namespace MWGui
void MessageBoxManager::createMessageBox (const std::string& message, bool stat) void MessageBoxManager::createMessageBox (const std::string& message, bool stat)
{ {
MessageBox *box = new MessageBox(*this, message); MessageBox *box = new MessageBox(*this, message);
box->mCurrentTime = 0;
box->mMaxTime = message.length()*mMessageBoxSpeed;
if(stat) if(stat)
mStaticMessageBox = box; mStaticMessageBox = box;
else
removeMessageBox(message.length()*mMessageBoxSpeed, box);
mMessageBoxes.push_back(box); mMessageBoxes.push_back(box);
std::vector<MessageBox*>::iterator it;
if(mMessageBoxes.size() > 3) { if(mMessageBoxes.size() > 3) {
delete *mMessageBoxes.begin(); delete *mMessageBoxes.begin();
@ -89,7 +66,7 @@ namespace MWGui
} }
int height = 0; int height = 0;
for(it = mMessageBoxes.begin(); it != mMessageBoxes.end(); ++it) for(std::vector<MessageBox*>::iterator it = mMessageBoxes.begin(); it != mMessageBoxes.end(); ++it)
{ {
(*it)->update(height); (*it)->update(height);
height += (*it)->getHeight(); height += (*it)->getHeight();
@ -119,15 +96,6 @@ namespace MWGui
return mInterMessageBoxe != NULL; return mInterMessageBoxe != NULL;
} }
void MessageBoxManager::removeMessageBox (float time, MessageBox *msgbox)
{
MessageBoxManagerTimer timer;
timer.current = 0;
timer.max = time;
timer.messageBox = msgbox;
mTimers.insert(mTimers.end(), timer);
}
bool MessageBoxManager::removeMessageBox (MessageBox *msgbox) bool MessageBoxManager::removeMessageBox (MessageBox *msgbox)
{ {
@ -169,12 +137,13 @@ namespace MWGui
: Layout("openmw_messagebox.layout") : Layout("openmw_messagebox.layout")
, mMessageBoxManager(parMessageBoxManager) , mMessageBoxManager(parMessageBoxManager)
, mMessage(message) , mMessage(message)
, mCurrentTime(0)
, mMaxTime(0)
{ {
// defines // defines
mFixedWidth = 300; mFixedWidth = 300;
mBottomPadding = 20; mBottomPadding = 20;
mNextBoxPadding = 20; mNextBoxPadding = 20;
mMarkedToDelete = false;
getWidget(mMessageWidget, "message"); getWidget(mMessageWidget, "message");

View File

@ -19,13 +19,6 @@ namespace MWGui
class InteractiveMessageBox; class InteractiveMessageBox;
class MessageBoxManager; class MessageBoxManager;
class MessageBox; class MessageBox;
struct MessageBoxManagerTimer {
float current;
float max;
MessageBox *messageBox;
};
class MessageBoxManager class MessageBoxManager
{ {
public: public:
@ -36,7 +29,6 @@ namespace MWGui
bool createInteractiveMessageBox (const std::string& message, const std::vector<std::string>& buttons); bool createInteractiveMessageBox (const std::string& message, const std::vector<std::string>& buttons);
bool isInteractiveMessageBox (); bool isInteractiveMessageBox ();
void removeMessageBox (float time, MessageBox *msgbox);
bool removeMessageBox (MessageBox *msgbox); bool removeMessageBox (MessageBox *msgbox);
void setMessageBoxSpeed (int speed); void setMessageBoxSpeed (int speed);
@ -54,7 +46,6 @@ namespace MWGui
std::vector<MessageBox*> mMessageBoxes; std::vector<MessageBox*> mMessageBoxes;
InteractiveMessageBox* mInterMessageBoxe; InteractiveMessageBox* mInterMessageBoxe;
MessageBox* mStaticMessageBox; MessageBox* mStaticMessageBox;
std::vector<MessageBoxManagerTimer> mTimers;
float mMessageBoxSpeed; float mMessageBoxSpeed;
int mLastButtonPressed; int mLastButtonPressed;
}; };
@ -67,7 +58,8 @@ namespace MWGui
int getHeight (); int getHeight ();
void update (int height); void update (int height);
bool mMarkedToDelete; float mCurrentTime;
float mMaxTime;
protected: protected:
MessageBoxManager& mMessageBoxManager; MessageBoxManager& mMessageBoxManager;