From d2a80cf1125028a627864d1b12912f5d695b89a6 Mon Sep 17 00:00:00 2001 From: Evil Eye Date: Sun, 28 Aug 2022 11:35:39 +0200 Subject: [PATCH] Use unique_ptr in MWGui::DialogueWindow --- apps/openmw/mwgui/dialogue.cpp | 78 ++++++++++++++-------------------- apps/openmw/mwgui/dialogue.hpp | 19 +++++---- 2 files changed, 41 insertions(+), 56 deletions(-) diff --git a/apps/openmw/mwgui/dialogue.cpp b/apps/openmw/mwgui/dialogue.cpp index 1e740abe22..14c865f018 100644 --- a/apps/openmw/mwgui/dialogue.cpp +++ b/apps/openmw/mwgui/dialogue.cpp @@ -56,9 +56,9 @@ namespace MWGui bool mNeedMargin; }; - PersuasionDialog::PersuasionDialog(ResponseCallback* callback) + PersuasionDialog::PersuasionDialog(std::unique_ptr callback) : WindowModal("openmw_persuasion_dialog.layout") - , mCallback(callback) + , mCallback(std::move(callback)) { getWidget(mCancelButton, "CancelButton"); getWidget(mAdmireButton, "AdmireButton"); @@ -130,14 +130,15 @@ namespace MWGui mText = text; } - void Response::write(BookTypesetter::Ptr typesetter, KeywordSearchT* keywordSearch, std::map& topicLinks) const + void Response::write(BookTypesetter::Ptr typesetter, KeywordSearchT* keywordSearch, std::map>& topicLinks) const { typesetter->sectionBreak(mNeedMargin ? 9 : 0); + auto windowManager = MWBase::Environment::get().getWindowManager(); - if (mTitle != "") + if (!mTitle.empty()) { - const MyGUI::Colour& headerColour = MWBase::Environment::get().getWindowManager()->getTextColours().header; - BookTypesetter::Style* title = typesetter->createStyle("", headerColour, false); + const MyGUI::Colour& headerColour = windowManager->getTextColours().header; + BookTypesetter::Style* title = typesetter->createStyle({}, headerColour, false); typesetter->write(title, to_utf8_span(mTitle.c_str())); typesetter->sectionBreak(); } @@ -160,8 +161,7 @@ namespace MWGui std::string link = text.substr(pos_begin + 1, pos_end - pos_begin - 1); const char specialPseudoAsteriskCharacter = 127; std::replace(link.begin(), link.end(), specialPseudoAsteriskCharacter, '*'); - std::string topicName = MWBase::Environment::get().getWindowManager()-> - getTranslationDataStorage().topicStandardForm(link); + std::string topicName = Misc::StringUtils::lowerCase(windowManager->getTranslationDataStorage().topicStandardForm(link)); std::string displayName = link; while (displayName[displayName.size()-1] == '*') @@ -169,8 +169,8 @@ namespace MWGui text.replace(pos_begin, pos_end+1-pos_begin, displayName); - if (topicLinks.find(Misc::StringUtils::lowerCase(topicName)) != topicLinks.end()) - hyperLinks[std::make_pair(pos_begin, pos_begin+displayName.size())] = intptr_t(topicLinks[Misc::StringUtils::lowerCase(topicName)]); + if (topicLinks.find(topicName) != topicLinks.end()) + hyperLinks[std::make_pair(pos_begin, pos_begin+displayName.size())] = intptr_t(topicLinks[topicName].get()); } else break; @@ -235,7 +235,7 @@ namespace MWGui mText = text; } - void Message::write(BookTypesetter::Ptr typesetter, KeywordSearchT* keywordSearch, std::map& topicLinks) const + void Message::write(BookTypesetter::Ptr typesetter, KeywordSearchT* keywordSearch, std::map>& topicLinks) const { const MyGUI::Colour& textColour = MWBase::Environment::get().getWindowManager()->getTextColours().notify; BookTypesetter::Style* title = typesetter->createStyle("", textColour, false); @@ -269,9 +269,9 @@ namespace MWGui : WindowBase("openmw_dialogue_window.layout") , mIsCompanion(false) , mGoodbye(false) - , mPersuasionDialog(new ResponseCallback(this)) - , mCallback(new ResponseCallback(this)) - , mGreetingCallback(new ResponseCallback(this, false)) + , mPersuasionDialog(std::make_unique(this)) + , mCallback(std::make_unique(this)) + , mGreetingCallback(std::make_unique(this, false)) { // Centre dialog center(); @@ -301,17 +301,6 @@ namespace MWGui mMainWidget->castType()->eventWindowChangeCoord += MyGUI::newDelegate(this, &DialogueWindow::onWindowResize); } - DialogueWindow::~DialogueWindow() - { - deleteLater(); - for (Link* link : mLinks) - delete link; - for (const auto& link : mTopicLinks) - delete link.second; - for (auto history : mHistoryContents) - delete history; - } - void DialogueWindow::onTradeComplete() { addResponse("", MyGUI::LanguageManager::getInstance().replaceTags("#{sBarterDialog5}")); @@ -425,8 +414,8 @@ namespace MWGui // The history is not reset here mKeywords.clear(); mTopicsList->clear(); - for (Link* link : mLinks) - mDeleteLater.push_back(link); // Links are not deleted right away to prevent issues with event handlers + for (auto& link : mLinks) + mDeleteLater.push_back(std::move(link)); // Links are not deleted right away to prevent issues with event handlers mLinks.clear(); } @@ -472,8 +461,6 @@ namespace MWGui void DialogueWindow::deleteLater() { - for (Link* link : mDeleteLater) - delete link; mDeleteLater.clear(); } @@ -482,8 +469,6 @@ namespace MWGui if (MWBase::Environment::get().getWindowManager()->containsMode(GM_Dialogue)) return; // Reset history - for (DialogueText* text : mHistoryContents) - delete text; mHistoryContents.clear(); } @@ -501,7 +486,7 @@ namespace MWGui { mTopicsList->clear(); for (auto& linkPair : mTopicLinks) - mDeleteLater.push_back(linkPair.second); + mDeleteLater.push_back(std::move(linkPair.second)); mTopicLinks.clear(); mKeywordSearch.clear(); @@ -549,11 +534,10 @@ namespace MWGui std::string topicId = Misc::StringUtils::lowerCase(keyword); mTopicsList->addItem(keyword); - Topic* t = new Topic(keyword); + auto t = std::make_unique(keyword); + mKeywordSearch.seed(topicId, intptr_t(t.get())); t->eventTopicActivated += MyGUI::newDelegate(this, &DialogueWindow::onTopicActivated); - mTopicLinks[topicId] = t; - - mKeywordSearch.seed(topicId, intptr_t(t)); + mTopicLinks[topicId] = std::move(t); } mTopicsList->adjustSize(); @@ -577,7 +561,7 @@ namespace MWGui BookTypesetter::Ptr typesetter = BookTypesetter::create (mHistory->getWidth(), std::numeric_limits::max()); - for (DialogueText* text : mHistoryContents) + for (const auto& text : mHistoryContents) text->write(typesetter, &mKeywordSearch, mTopicLinks); BookTypesetter::Style* body = typesetter->createStyle("", MyGUI::Colour::White, false); @@ -588,27 +572,27 @@ namespace MWGui mChoices = MWBase::Environment::get().getDialogueManager()->getChoices(); for (std::pair& choice : mChoices) { - Choice* link = new Choice(choice.second); + auto link = std::make_unique(choice.second); link->eventChoiceActivated += MyGUI::newDelegate(this, &DialogueWindow::onChoiceActivated); - mLinks.push_back(link); + auto interactiveId = TypesetBook::InteractiveId(link.get()); + mLinks.push_back(std::move(link)); typesetter->lineBreak(); BookTypesetter::Style* questionStyle = typesetter->createHotStyle(body, textColours.answer, textColours.answerOver, - textColours.answerPressed, - TypesetBook::InteractiveId(link)); + textColours.answerPressed, interactiveId); typesetter->write(questionStyle, to_utf8_span(choice.first.c_str())); } mGoodbye = MWBase::Environment::get().getDialogueManager()->isGoodbye(); if (mGoodbye) { - Goodbye* link = new Goodbye(); + auto link = std::make_unique(); link->eventActivated += MyGUI::newDelegate(this, &DialogueWindow::onGoodbyeActivated); - mLinks.push_back(link); + auto interactiveId = TypesetBook::InteractiveId(link.get()); + mLinks.push_back(std::move(link)); const std::string& goodbye = MWBase::Environment::get().getWorld()->getStore().get().find("sGoodbye")->mValue.getString(); BookTypesetter::Style* questionStyle = typesetter->createHotStyle(body, textColours.answer, textColours.answerOver, - textColours.answerPressed, - TypesetBook::InteractiveId(link)); + textColours.answerPressed, interactiveId); typesetter->lineBreak(); typesetter->write(questionStyle, to_utf8_span(goodbye.c_str())); } @@ -682,13 +666,13 @@ namespace MWGui void DialogueWindow::addResponse(const std::string &title, const std::string &text, bool needMargin) { - mHistoryContents.push_back(new Response(text, title, needMargin)); + mHistoryContents.push_back(std::make_unique(text, title, needMargin)); updateHistory(); } void DialogueWindow::addMessageBox(const std::string& text) { - mHistoryContents.push_back(new Message(text)); + mHistoryContents.push_back(std::make_unique(text)); updateHistory(); } diff --git a/apps/openmw/mwgui/dialogue.hpp b/apps/openmw/mwgui/dialogue.hpp index aff09c0211..7ea70307cf 100644 --- a/apps/openmw/mwgui/dialogue.hpp +++ b/apps/openmw/mwgui/dialogue.hpp @@ -1,6 +1,8 @@ #ifndef MWGUI_DIALOGE_H #define MWGUI_DIALOGE_H +#include + #include "windowbase.hpp" #include "referenceinterface.hpp" @@ -22,7 +24,7 @@ namespace MWGui class PersuasionDialog : public WindowModal { public: - PersuasionDialog(ResponseCallback* callback); + PersuasionDialog(std::unique_ptr callback); void onOpen() override; @@ -81,14 +83,14 @@ namespace MWGui struct DialogueText { virtual ~DialogueText() {} - virtual void write (BookTypesetter::Ptr typesetter, KeywordSearchT* keywordSearch, std::map& topicLinks) const = 0; + virtual void write(BookTypesetter::Ptr typesetter, KeywordSearchT* keywordSearch, std::map>& topicLinks) const = 0; std::string mText; }; struct Response : DialogueText { Response(const std::string& text, const std::string& title = "", bool needMargin = true); - void write (BookTypesetter::Ptr typesetter, KeywordSearchT* keywordSearch, std::map& topicLinks) const override; + void write(BookTypesetter::Ptr typesetter, KeywordSearchT* keywordSearch, std::map>& topicLinks) const override; void addTopicLink (BookTypesetter::Ptr typesetter, intptr_t topicId, size_t begin, size_t end) const; std::string mTitle; bool mNeedMargin; @@ -97,14 +99,13 @@ namespace MWGui struct Message : DialogueText { Message(const std::string& text); - void write (BookTypesetter::Ptr typesetter, KeywordSearchT* keywordSearch, std::map& topicLinks) const override; + void write(BookTypesetter::Ptr typesetter, KeywordSearchT* keywordSearch, std::map>& topicLinks) const override; }; class DialogueWindow: public WindowBase, public ReferenceInterface { public: DialogueWindow(); - ~DialogueWindow(); void onTradeComplete(); @@ -158,14 +159,14 @@ namespace MWGui bool mIsCompanion; std::list mKeywords; - std::vector mHistoryContents; + std::vector> mHistoryContents; std::vector > mChoices; bool mGoodbye; - std::vector mLinks; - std::map mTopicLinks; + std::vector> mLinks; + std::map> mTopicLinks; - std::vector mDeleteLater; + std::vector> mDeleteLater; KeywordSearchT mKeywordSearch;