1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-05 15:55:45 +00:00
OpenMW/apps/openmw/mwgui/levelupdialog.cpp

240 lines
8.4 KiB
C++
Raw Normal View History

2012-09-14 17:44:00 +00:00
#include "levelupdialog.hpp"
#include <boost/lexical_cast.hpp>
#include "../mwbase/windowmanager.hpp"
#include "../mwbase/environment.hpp"
2012-09-15 15:12:42 +00:00
#include "../mwbase/world.hpp"
#include "../mwworld/class.hpp"
2013-03-17 12:50:15 +00:00
#include "../mwworld/fallback.hpp"
2014-02-23 19:11:05 +00:00
#include "../mwworld/esmstore.hpp"
#include "../mwworld/cellstore.hpp"
2012-09-15 15:12:42 +00:00
#include "../mwmechanics/creaturestats.hpp"
#include "../mwmechanics/npcstats.hpp"
2012-09-14 17:44:00 +00:00
namespace MWGui
{
const unsigned int LevelupDialog::sMaxCoins = 3;
LevelupDialog::LevelupDialog()
: WindowBase("openmw_levelup_dialog.layout"),
mCoinCount(sMaxCoins)
2012-09-14 17:44:00 +00:00
{
getWidget(mOkButton, "OkButton");
getWidget(mClassImage, "ClassImage");
getWidget(mLevelText, "LevelText");
getWidget(mLevelDescription, "LevelDescription");
getWidget(mCoinBox, "Coins");
2014-09-03 01:03:03 +00:00
getWidget(mAssignWidget, "AssignWidget");
2012-09-14 17:44:00 +00:00
mOkButton->eventMouseButtonClick += MyGUI::newDelegate(this, &LevelupDialog::onOkButtonClicked);
for (int i=1; i<9; ++i)
{
MyGUI::TextBox* t;
getWidget(t, "AttribVal" + boost::lexical_cast<std::string>(i));
MyGUI::Button* b;
getWidget(b, "Attrib" + boost::lexical_cast<std::string>(i));
b->setUserData (i-1);
b->eventMouseButtonClick += MyGUI::newDelegate(this, &LevelupDialog::onAttributeClicked);
2012-09-15 15:12:42 +00:00
mAttributes.push_back(b);
2012-09-14 17:44:00 +00:00
mAttributeValues.push_back(t);
getWidget(t, "AttribMultiplier" + boost::lexical_cast<std::string>(i));
mAttributeMultipliers.push_back(t);
}
for (unsigned int i = 0; i < mCoinCount; ++i)
2012-09-15 15:12:42 +00:00
{
MyGUI::ImageBox* image = mCoinBox->createWidget<MyGUI::ImageBox>("ImageBox", MyGUI::IntCoord(0,0,16,16), MyGUI::Align::Default);
2012-09-15 15:12:42 +00:00
image->setImageTexture ("icons\\tx_goldicon.dds");
mCoins.push_back(image);
}
2012-09-14 17:44:00 +00:00
center();
2012-09-15 15:12:42 +00:00
}
void LevelupDialog::setAttributeValues()
{
MWWorld::Ptr player = MWBase::Environment::get().getWorld ()->getPlayerPtr();
MWMechanics::CreatureStats& creatureStats = player.getClass().getCreatureStats(player);
MWMechanics::NpcStats& pcStats = player.getClass().getNpcStats (player);
2012-09-15 15:12:42 +00:00
for (int i = 0; i < 8; ++i)
2012-09-15 15:12:42 +00:00
{
int val = creatureStats.getAttribute(i).getBase();
2012-09-15 15:12:42 +00:00
if (std::find(mSpentAttributes.begin(), mSpentAttributes.end(), i) != mSpentAttributes.end())
{
val += pcStats.getLevelupAttributeMultiplier(i);
2012-09-15 15:12:42 +00:00
}
if (val >= 100)
val = 100;
mAttributeValues[i]->setCaption(boost::lexical_cast<std::string>(val));
}
}
void LevelupDialog::resetCoins()
2012-09-15 15:12:42 +00:00
{
const int coinSpacing = 10;
int curX = mCoinBox->getWidth()/2 - (coinSpacing*(mCoinCount - 1) + 16*mCoinCount)/2;
for (unsigned int i=0; i<sMaxCoins; ++i)
2012-09-15 15:12:42 +00:00
{
MyGUI::ImageBox* image = mCoins[i];
image->detachFromWidget();
image->attachToWidget(mCoinBox);
if (i < mCoinCount)
{
mCoins[i]->setVisible(true);
image->setCoord(MyGUI::IntCoord(curX,0,16,16));
curX += 16+coinSpacing;
}
else
mCoins[i]->setVisible(false);
2012-09-15 15:12:42 +00:00
}
}
void LevelupDialog::assignCoins()
2012-09-15 15:12:42 +00:00
{
resetCoins();
for (unsigned int i=0; i<mSpentAttributes.size(); ++i)
{
MyGUI::ImageBox* image = mCoins[i];
image->detachFromWidget();
2014-09-03 01:03:03 +00:00
image->attachToWidget(mAssignWidget);
2012-09-15 15:12:42 +00:00
int attribute = mSpentAttributes[i];
int xdiff = mAttributeMultipliers[attribute]->getCaption() == "" ? 0 : 20;
2012-09-15 15:12:42 +00:00
2014-09-03 01:03:03 +00:00
MyGUI::IntPoint pos = mAttributes[attribute]->getAbsolutePosition() - mAssignWidget->getAbsolutePosition() - MyGUI::IntPoint(22+xdiff,0);
pos.top += (mAttributes[attribute]->getHeight() - image->getHeight())/2;
2012-09-15 15:12:42 +00:00
image->setPosition(pos);
}
2012-09-14 17:44:00 +00:00
2012-09-15 15:12:42 +00:00
setAttributeValues();
2012-09-14 17:44:00 +00:00
}
void LevelupDialog::open()
{
2012-11-08 12:37:57 +00:00
MWBase::World *world = MWBase::Environment::get().getWorld();
MWWorld::Ptr player = world->getPlayerPtr();
MWMechanics::CreatureStats& creatureStats = player.getClass().getCreatureStats(player);
MWMechanics::NpcStats& pcStats = player.getClass().getNpcStats(player);
2012-09-15 15:12:42 +00:00
2012-11-08 12:37:57 +00:00
const ESM::NPC *playerData = player.get<ESM::NPC>()->mBase;
2012-09-15 15:12:42 +00:00
// set class image
const ESM::Class *cls =
2012-11-08 12:37:57 +00:00
world->getStore().get<ESM::Class>().find(playerData->mClass);
if(world->getStore().get<ESM::Class>().isDynamic(cls->mId))
2014-04-15 12:30:41 +00:00
{
// Vanilla uses thief.dds for custom classes.
// Choosing Stealth specialization and Speed/Agility as attributes, if possible. Otherwise fall back to first class found.
2014-04-15 12:30:41 +00:00
MWWorld::SharedIterator<ESM::Class> it = world->getStore().get<ESM::Class>().begin();
for(; it != world->getStore().get<ESM::Class>().end(); it++)
{
if(it->mData.mIsPlayable && it->mData.mSpecialization == 2 && it->mData.mAttribute[0] == 4 && it->mData.mAttribute[1] == 3)
break;
}
if (it == world->getStore().get<ESM::Class>().end())
it = world->getStore().get<ESM::Class>().begin();
if (it != world->getStore().get<ESM::Class>().end())
cls = &*it;
2014-04-15 12:30:41 +00:00
}
mClassImage->setImageTexture ("textures\\levelup\\" + cls->mId + ".dds");
2012-09-15 15:12:42 +00:00
int level = creatureStats.getLevel ()+1;
2012-09-14 17:44:00 +00:00
mLevelText->setCaptionWithReplacing("#{sLevelUpMenu1} " + boost::lexical_cast<std::string>(level));
2012-09-15 15:12:42 +00:00
std::string levelupdescription;
if(level > 20)
2013-03-17 12:50:15 +00:00
levelupdescription=world->getFallback()->getFallbackString("Level_Up_Default");
else
2013-03-17 12:50:15 +00:00
levelupdescription=world->getFallback()->getFallbackString("Level_Up_Level"+boost::lexical_cast<std::string>(level));
mLevelDescription->setCaption (levelupdescription);
unsigned int availableAttributes = 0;
for (int i = 0; i < 8; ++i)
2012-09-15 15:12:42 +00:00
{
MyGUI::TextBox* text = mAttributeMultipliers[i];
if (pcStats.getAttribute(i).getBase() < 100)
{
mAttributes[i]->setEnabled(true);
availableAttributes++;
int mult = pcStats.getLevelupAttributeMultiplier (i);
text->setCaption(mult <= 1 ? "" : "x" + boost::lexical_cast<std::string>(mult));
}
else
{
mAttributes[i]->setEnabled(false);
text->setCaption("");
}
}
mCoinCount = std::min(sMaxCoins, availableAttributes);
mSpentAttributes.clear();
resetCoins();
setAttributeValues();
center();
2012-09-14 17:44:00 +00:00
}
void LevelupDialog::onOkButtonClicked(MyGUI::Widget* sender)
2012-09-14 17:44:00 +00:00
{
MWWorld::Ptr player = MWBase::Environment::get().getWorld()->getPlayerPtr();
MWMechanics::NpcStats& pcStats = player.getClass().getNpcStats (player);
2012-09-15 15:12:42 +00:00
if (mSpentAttributes.size() < mCoinCount)
MWBase::Environment::get().getWindowManager()->messageBox("#{sNotifyMessage36}");
2012-09-15 15:12:42 +00:00
else
{
// increase attributes
for (unsigned int i = 0; i < mCoinCount; ++i)
2012-09-15 15:12:42 +00:00
{
MWMechanics::AttributeValue attribute = pcStats.getAttribute(mSpentAttributes[i]);
attribute.setBase(attribute.getBase() + pcStats.getLevelupAttributeMultiplier(mSpentAttributes[i]));
2012-09-15 15:12:42 +00:00
if (attribute.getBase() >= 100)
attribute.setBase(100);
pcStats.setAttribute(mSpentAttributes[i], attribute);
2012-09-15 15:12:42 +00:00
}
pcStats.levelUp();
2012-09-15 15:12:42 +00:00
MWBase::Environment::get().getWindowManager()->removeGuiMode(GM_Levelup);
2012-09-15 15:12:42 +00:00
}
2012-09-14 17:44:00 +00:00
}
void LevelupDialog::onAttributeClicked(MyGUI::Widget *sender)
2012-09-14 17:44:00 +00:00
{
2012-09-15 15:12:42 +00:00
int attribute = *sender->getUserData<int>();
std::vector<int>::iterator found = std::find(mSpentAttributes.begin(), mSpentAttributes.end(), attribute);
if (found != mSpentAttributes.end())
mSpentAttributes.erase(found);
2012-09-15 15:12:42 +00:00
else
{
if (mSpentAttributes.size() == mCoinCount)
mSpentAttributes[mCoinCount - 1] = attribute;
2012-09-15 15:12:42 +00:00
else
mSpentAttributes.push_back(attribute);
}
assignCoins();
2012-09-14 17:44:00 +00:00
}
}