From f3ad16620affe24ce5a7d61423b98a376821ab67 Mon Sep 17 00:00:00 2001 From: kuyondo Date: Sat, 18 Nov 2023 00:41:44 +0800 Subject: [PATCH] refactor and maintenance alchemywindow and alchemy --- apps/openmw/mwgui/alchemywindow.cpp | 50 +++++++++-------------------- apps/openmw/mwgui/alchemywindow.hpp | 3 -- apps/openmw/mwmechanics/alchemy.cpp | 8 ++--- apps/openmw/mwmechanics/alchemy.hpp | 4 +-- 4 files changed, 22 insertions(+), 43 deletions(-) diff --git a/apps/openmw/mwgui/alchemywindow.cpp b/apps/openmw/mwgui/alchemywindow.cpp index 2ac9c281b6..7208fce5f6 100644 --- a/apps/openmw/mwgui/alchemywindow.cpp +++ b/apps/openmw/mwgui/alchemywindow.cpp @@ -147,12 +147,12 @@ namespace MWGui } // remove ingredient slots that have been fully used up - for (int i = 0; i < 4; ++i) + for (size_t i = 0; i < mIngredients.size(); ++i) if (mIngredients[i]->isUserString("ToolTipType")) { MWWorld::Ptr ingred = *mIngredients[i]->getUserData(); if (ingred.getRefData().getCount() == 0) - removeIngredient(mIngredients[i]); + mAlchemy->removeIngredient(i); } updateFilters(); @@ -295,7 +295,8 @@ namespace MWGui void AlchemyWindow::onIngredientSelected(MyGUI::Widget* _sender) { - removeIngredient(_sender); + size_t i = std::distance(mIngredients.begin(), std::find(mIngredients.begin(), mIngredients.end(), _sender)); + mAlchemy->removeIngredient(i); update(); } @@ -330,10 +331,10 @@ namespace MWGui void AlchemyWindow::onApparatusSelected(MyGUI::Widget* _sender) { + size_t i = std::distance(mApparatus.begin(), std::find(mApparatus.begin(), mApparatus.end(), _sender)); if (_sender->getUserData()->isEmpty()) // if this apparatus slot is empty { std::string title; - size_t i = std::distance(mApparatus.begin(), std::find(mApparatus.begin(), mApparatus.end(), _sender)); switch (i) { case ESM::Apparatus::AppaType::MortarPestle: @@ -361,7 +362,17 @@ namespace MWGui mItemSelectionDialog->setFilter(SortFilterItemModel::Filter_OnlyAlchemyTools); } else - removeApparatus(_sender); + { + const auto& widget = mApparatus[i]; + mAlchemy->removeApparatus(i); + + if (widget->getChildCount()) + MyGUI::Gui::getInstance().destroyWidget(widget->getChildAt(0)); + + widget->clearUserStrings(); + widget->setItem(MWWorld::Ptr()); + widget->setUserData(MWWorld::Ptr()); + } update(); } @@ -459,35 +470,6 @@ namespace MWGui effectsWidget->setCoord(coord); } - void AlchemyWindow::removeIngredient(MyGUI::Widget* ingredient) - { - for (int i = 0; i < mIngredients.size(); ++i) - if (mIngredients[i] == ingredient) - mAlchemy->removeIngredient(i); - - update(); - } - - void AlchemyWindow::removeApparatus(MyGUI::Widget* apparatus) - { - for (size_t i = 0; i < mApparatus.size(); ++i) - { - if (mApparatus[i] == apparatus) - { - const auto& widget = mApparatus[i]; - mAlchemy->removeApparatus(i); - - if (widget->getChildCount()) - MyGUI::Gui::getInstance().destroyWidget(widget->getChildAt(0)); - - widget->clearUserStrings(); - widget->setItem(MWWorld::Ptr()); - widget->setUserData(MWWorld::Ptr()); - break; - } - } - } - void AlchemyWindow::addRepeatController(MyGUI::Widget* widget) { MyGUI::ControllerItem* item diff --git a/apps/openmw/mwgui/alchemywindow.hpp b/apps/openmw/mwgui/alchemywindow.hpp index a88c36c22d..82e5c3f583 100644 --- a/apps/openmw/mwgui/alchemywindow.hpp +++ b/apps/openmw/mwgui/alchemywindow.hpp @@ -88,9 +88,6 @@ namespace MWGui void onSelectedItem(int index); - void removeIngredient(MyGUI::Widget* ingredient); - void removeApparatus(MyGUI::Widget* ingredient); - void onItemSelected(MWWorld::Ptr item); void onItemCancel(); diff --git a/apps/openmw/mwmechanics/alchemy.cpp b/apps/openmw/mwmechanics/alchemy.cpp index 4fd8ab7738..b697387f50 100644 --- a/apps/openmw/mwmechanics/alchemy.cpp +++ b/apps/openmw/mwmechanics/alchemy.cpp @@ -452,9 +452,9 @@ int MWMechanics::Alchemy::addIngredient(const MWWorld::Ptr& ingredient) return slot; } -void MWMechanics::Alchemy::removeIngredient(int index) +void MWMechanics::Alchemy::removeIngredient(size_t index) { - if (index >= 0 && index < static_cast(mIngredients.size())) + if (index >= 0 && index < mIngredients.size()) { mIngredients[index] = MWWorld::Ptr(); updateEffects(); @@ -470,9 +470,9 @@ void MWMechanics::Alchemy::addApparatus(const MWWorld::Ptr& apparatus) updateEffects(); } -void MWMechanics::Alchemy::removeApparatus(int index) +void MWMechanics::Alchemy::removeApparatus(size_t index) { - if (index >= 0 && index < static_cast(mTools.size())) + if (index >= 0 && index < mTools.size()) { mTools[index] = MWWorld::Ptr(); updateEffects(); diff --git a/apps/openmw/mwmechanics/alchemy.hpp b/apps/openmw/mwmechanics/alchemy.hpp index 0ae7fd3f2e..1b76e400f5 100644 --- a/apps/openmw/mwmechanics/alchemy.hpp +++ b/apps/openmw/mwmechanics/alchemy.hpp @@ -122,10 +122,10 @@ namespace MWMechanics void addApparatus(const MWWorld::Ptr& apparatus); ///< Add apparatus into the appropriate slot. - void removeIngredient(int index); + void removeIngredient(size_t index); ///< Remove ingredient from slot (calling this function on an empty slot is a no-op). - void removeApparatus(int index); + void removeApparatus(size_t index); ///< Remove apparatus from slot. std::string suggestPotionName();