2013-03-22 13:13:10 +00:00
|
|
|
#include "merchantrepair.hpp"
|
|
|
|
|
2014-02-23 19:11:05 +00:00
|
|
|
#include <components/esm/loadgmst.hpp>
|
|
|
|
|
2015-01-10 01:50:43 +00:00
|
|
|
#include <MyGUI_Button.h>
|
|
|
|
#include <MyGUI_ScrollView.h>
|
|
|
|
#include <MyGUI_Gui.h>
|
|
|
|
|
2013-03-22 13:13:10 +00:00
|
|
|
#include "../mwbase/world.hpp"
|
|
|
|
#include "../mwbase/environment.hpp"
|
|
|
|
#include "../mwbase/mechanicsmanager.hpp"
|
|
|
|
#include "../mwbase/windowmanager.hpp"
|
|
|
|
#include "../mwbase/soundmanager.hpp"
|
|
|
|
|
2014-07-27 22:55:57 +00:00
|
|
|
#include "../mwmechanics/creaturestats.hpp"
|
2015-08-21 09:12:39 +00:00
|
|
|
#include "../mwmechanics/actorutil.hpp"
|
2014-07-27 22:55:57 +00:00
|
|
|
|
2013-05-11 16:38:27 +00:00
|
|
|
#include "../mwworld/class.hpp"
|
|
|
|
#include "../mwworld/containerstore.hpp"
|
2014-02-23 19:11:05 +00:00
|
|
|
#include "../mwworld/esmstore.hpp"
|
2013-03-22 13:13:10 +00:00
|
|
|
|
|
|
|
namespace MWGui
|
|
|
|
{
|
|
|
|
|
2013-04-10 18:46:21 +00:00
|
|
|
MerchantRepair::MerchantRepair()
|
|
|
|
: WindowBase("openmw_merchantrepair.layout")
|
2013-03-22 13:13:10 +00:00
|
|
|
{
|
|
|
|
getWidget(mList, "RepairView");
|
|
|
|
getWidget(mOkButton, "OkButton");
|
|
|
|
getWidget(mGoldLabel, "PlayerGold");
|
|
|
|
|
|
|
|
mOkButton->eventMouseButtonClick += MyGUI::newDelegate(this, &MerchantRepair::onOkButtonClick);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MerchantRepair::startRepair(const MWWorld::Ptr &actor)
|
|
|
|
{
|
|
|
|
mActor = actor;
|
|
|
|
|
|
|
|
while (mList->getChildCount())
|
|
|
|
MyGUI::Gui::getInstance().destroyWidget(mList->getChildAt(0));
|
|
|
|
|
|
|
|
int currentY = 0;
|
|
|
|
|
2015-08-21 09:12:39 +00:00
|
|
|
MWWorld::Ptr player = MWMechanics::getPlayer();
|
2014-01-08 22:37:46 +00:00
|
|
|
int playerGold = player.getClass().getContainerStore(player).count(MWWorld::ContainerStore::sGoldId);
|
|
|
|
|
2017-02-19 14:30:26 +00:00
|
|
|
const MWWorld::ContainerStore& store = player.getClass().getContainerStore(player);
|
2013-04-03 19:13:01 +00:00
|
|
|
int categories = MWWorld::ContainerStore::Type_Weapon | MWWorld::ContainerStore::Type_Armor;
|
2017-02-19 14:30:26 +00:00
|
|
|
for (MWWorld::ConstContainerStoreIterator iter (store.cbegin(categories));
|
|
|
|
iter!=store.cend(); ++iter)
|
2013-03-22 13:13:10 +00:00
|
|
|
{
|
2014-05-22 18:37:22 +00:00
|
|
|
if (iter->getClass().hasItemHealth(*iter))
|
2013-03-22 13:13:10 +00:00
|
|
|
{
|
2014-05-22 18:37:22 +00:00
|
|
|
int maxDurability = iter->getClass().getItemMaxHealth(*iter);
|
2014-05-25 12:13:07 +00:00
|
|
|
int durability = iter->getClass().getItemHealth(*iter);
|
2013-03-22 13:13:10 +00:00
|
|
|
if (maxDurability == durability)
|
|
|
|
continue;
|
|
|
|
|
2014-05-22 18:37:22 +00:00
|
|
|
int basePrice = iter->getClass().getValue(*iter);
|
2013-03-22 13:13:10 +00:00
|
|
|
float fRepairMult = MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>()
|
|
|
|
.find("fRepairMult")->getFloat();
|
|
|
|
|
2015-03-08 00:07:29 +00:00
|
|
|
float p = static_cast<float>(std::max(1, basePrice));
|
|
|
|
float r = static_cast<float>(std::max(1, static_cast<int>(maxDurability / p)));
|
2013-03-22 13:13:10 +00:00
|
|
|
|
2015-03-08 00:07:29 +00:00
|
|
|
int x = static_cast<int>((maxDurability - durability) / r);
|
|
|
|
x = static_cast<int>(fRepairMult * x);
|
2013-03-22 13:13:10 +00:00
|
|
|
|
|
|
|
int price = MWBase::Environment::get().getMechanicsManager()->getBarterOffer(mActor, x, true);
|
|
|
|
|
|
|
|
|
2014-05-22 18:37:22 +00:00
|
|
|
std::string name = iter->getClass().getName(*iter)
|
2015-01-10 02:01:01 +00:00
|
|
|
+ " - " + MyGUI::utility::toString(price)
|
2013-03-22 13:13:10 +00:00
|
|
|
+ MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>()
|
2014-09-06 18:04:52 +00:00
|
|
|
.find("sgp")->getString();
|
2013-03-22 13:13:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
MyGUI::Button* button =
|
2014-10-17 16:58:20 +00:00
|
|
|
mList->createWidget<MyGUI::Button>(price <= playerGold ? "SandTextButton" : "SandTextButtonDisabled", // can't use setEnabled since that removes tooltip
|
2013-03-22 13:13:10 +00:00
|
|
|
0,
|
|
|
|
currentY,
|
|
|
|
0,
|
|
|
|
18,
|
|
|
|
MyGUI::Align::Default
|
|
|
|
);
|
|
|
|
|
|
|
|
currentY += 18;
|
|
|
|
|
2015-01-10 02:01:01 +00:00
|
|
|
button->setUserString("Price", MyGUI::utility::toString(price));
|
2015-12-19 21:52:35 +00:00
|
|
|
button->setUserData(*iter);
|
2013-03-22 13:13:10 +00:00
|
|
|
button->setCaptionWithReplacing(name);
|
|
|
|
button->setSize(button->getTextSize().width,18);
|
|
|
|
button->eventMouseWheel += MyGUI::newDelegate(this, &MerchantRepair::onMouseWheel);
|
|
|
|
button->setUserString("ToolTipType", "ItemPtr");
|
|
|
|
button->eventMouseButtonClick += MyGUI::newDelegate(this, &MerchantRepair::onRepairButtonClick);
|
|
|
|
}
|
|
|
|
}
|
2014-07-26 00:23:42 +00:00
|
|
|
// Canvas size must be expressed with VScroll disabled, otherwise MyGUI would expand the scroll area when the scrollbar is hidden
|
|
|
|
mList->setVisibleVScroll(false);
|
2013-03-22 13:13:10 +00:00
|
|
|
mList->setCanvasSize (MyGUI::IntSize(mList->getWidth(), std::max(mList->getHeight(), currentY)));
|
2014-07-26 00:23:42 +00:00
|
|
|
mList->setVisibleVScroll(true);
|
2013-03-22 13:13:10 +00:00
|
|
|
|
|
|
|
mGoldLabel->setCaptionWithReplacing("#{sGold}: "
|
2015-01-10 02:01:01 +00:00
|
|
|
+ MyGUI::utility::toString(playerGold));
|
2013-03-22 13:13:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MerchantRepair::onMouseWheel(MyGUI::Widget* _sender, int _rel)
|
|
|
|
{
|
2015-03-08 00:07:29 +00:00
|
|
|
if (mList->getViewOffset().top + _rel*0.3f > 0)
|
2013-03-22 13:13:10 +00:00
|
|
|
mList->setViewOffset(MyGUI::IntPoint(0, 0));
|
|
|
|
else
|
2015-03-08 00:07:29 +00:00
|
|
|
mList->setViewOffset(MyGUI::IntPoint(0, static_cast<int>(mList->getViewOffset().top + _rel*0.3f)));
|
2013-03-22 13:13:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MerchantRepair::open()
|
|
|
|
{
|
|
|
|
center();
|
2015-06-04 20:09:40 +00:00
|
|
|
// Reset scrollbars
|
|
|
|
mList->setViewOffset(MyGUI::IntPoint(0, 0));
|
2013-03-22 13:13:10 +00:00
|
|
|
}
|
|
|
|
|
2014-05-27 03:13:37 +00:00
|
|
|
void MerchantRepair::exit()
|
|
|
|
{
|
|
|
|
MWBase::Environment::get().getWindowManager()->removeGuiMode(GM_MerchantRepair);
|
|
|
|
}
|
|
|
|
|
2013-03-22 13:13:10 +00:00
|
|
|
void MerchantRepair::onRepairButtonClick(MyGUI::Widget *sender)
|
|
|
|
{
|
2015-08-21 09:12:39 +00:00
|
|
|
MWWorld::Ptr player = MWMechanics::getPlayer();
|
2014-07-16 13:30:06 +00:00
|
|
|
|
2015-01-10 02:01:01 +00:00
|
|
|
int price = MyGUI::utility::parseInt(sender->getUserString("Price"));
|
2014-10-17 16:58:20 +00:00
|
|
|
if (price > player.getClass().getContainerStore(player).count(MWWorld::ContainerStore::sGoldId))
|
|
|
|
return;
|
|
|
|
|
2013-03-22 13:13:10 +00:00
|
|
|
// repair
|
|
|
|
MWWorld::Ptr item = *sender->getUserData<MWWorld::Ptr>();
|
2014-05-25 12:13:07 +00:00
|
|
|
item.getCellRef().setCharge(item.getClass().getItemMaxHealth(item));
|
2013-03-22 13:13:10 +00:00
|
|
|
|
2014-07-16 13:30:06 +00:00
|
|
|
player.getClass().getContainerStore(player).restack(item);
|
|
|
|
|
2013-03-22 13:13:10 +00:00
|
|
|
MWBase::Environment::get().getSoundManager()->playSound("Repair",1,1);
|
|
|
|
|
2013-11-21 03:27:53 +00:00
|
|
|
|
2014-01-08 22:37:46 +00:00
|
|
|
player.getClass().getContainerStore(player).remove(MWWorld::ContainerStore::sGoldId, price, player);
|
2013-03-22 13:13:10 +00:00
|
|
|
|
2014-07-27 22:55:57 +00:00
|
|
|
// add gold to NPC trading gold pool
|
|
|
|
MWMechanics::CreatureStats& actorStats = mActor.getClass().getCreatureStats(mActor);
|
|
|
|
actorStats.setGoldPool(actorStats.getGoldPool() + price);
|
|
|
|
|
2013-03-22 13:13:10 +00:00
|
|
|
startRepair(mActor);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MerchantRepair::onOkButtonClick(MyGUI::Widget *sender)
|
|
|
|
{
|
2014-05-27 03:13:37 +00:00
|
|
|
exit();
|
2013-03-22 13:13:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|