1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-11 00:39:59 +00:00
OpenMW/apps/openmw/mwgui/travelwindow.cpp

209 lines
8.2 KiB
C++
Raw Normal View History

2012-09-26 16:30:47 +00:00
#include "travelwindow.hpp"
#include <boost/lexical_cast.hpp>
#include <OgreVector3.h>
2012-10-16 17:34:29 +00:00
#include <libs/openengine/ogre/fader.hpp>
2012-09-26 16:30:47 +00:00
#include "../mwbase/environment.hpp"
#include "../mwbase/world.hpp"
#include "../mwbase/windowmanager.hpp"
2012-10-16 17:34:29 +00:00
#include "../mwbase/mechanicsmanager.hpp"
#include "../mwbase/soundmanager.hpp"
2012-09-26 16:30:47 +00:00
2013-05-11 16:38:27 +00:00
#include "../mwworld/class.hpp"
#include "../mwworld/containerstore.hpp"
#include "../mwworld/actionteleport.hpp"
2014-02-23 19:11:05 +00:00
#include "../mwworld/esmstore.hpp"
#include "../mwworld/cellstore.hpp"
2012-09-26 16:30:47 +00:00
namespace MWGui
{
const int TravelWindow::sLineHeight = 18;
TravelWindow::TravelWindow() :
WindowBase("openmw_travel_window.layout")
2012-09-26 16:30:47 +00:00
, mCurrentY(0)
, mLastPos(0)
{
setCoord(0, 0, 450, 300);
getWidget(mCancelButton, "CancelButton");
getWidget(mPlayerGold, "PlayerGold");
getWidget(mSelect, "Select");
2012-10-06 15:52:46 +00:00
getWidget(mDestinations, "Travel");
getWidget(mDestinationsView, "DestinationsView");
2012-09-26 16:30:47 +00:00
mCancelButton->eventMouseButtonClick += MyGUI::newDelegate(this, &TravelWindow::onCancelButtonClicked);
mDestinations->setCoord(450/2-mDestinations->getTextSize().width/2,
mDestinations->getTop(),
mDestinations->getTextSize().width,
mDestinations->getHeight());
mSelect->setCoord(8,
mSelect->getTop(),
mSelect->getTextSize().width,
mSelect->getHeight());
}
void TravelWindow::exit()
{
MWBase::Environment::get().getWindowManager()->removeGuiMode(GM_Travel);
}
void TravelWindow::addDestination(const std::string& name,ESM::Position pos,bool interior)
2012-09-26 16:30:47 +00:00
{
int price = 0;
const MWWorld::Store<ESM::GameSetting> &gmst =
MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>();
MWWorld::Ptr player = MWBase::Environment::get().getWorld ()->getPlayerPtr();
int playerGold = player.getClass().getContainerStore(player).count(MWWorld::ContainerStore::sGoldId);
2012-10-08 13:51:36 +00:00
if(interior)
{
price = gmst.find("fMagesGuildTravel")->getFloat();
2012-10-08 13:51:36 +00:00
}
else
{
ESM::Position PlayerPos = player.getRefData().getPosition();
float d = sqrt( pow(pos.pos[0] - PlayerPos.pos[0],2) + pow(pos.pos[1] - PlayerPos.pos[1],2) + pow(pos.pos[2] - PlayerPos.pos[2],2) );
price = d/gmst.find("fTravelMult")->getFloat();
2012-10-08 13:51:36 +00:00
}
2012-11-09 13:42:09 +00:00
price = MWBase::Environment::get().getMechanicsManager()->getBarterOffer(mPtr,price,true);
MyGUI::Button* toAdd = mDestinationsView->createWidget<MyGUI::Button>("SandTextButton", 0, mCurrentY, 200, sLineHeight, MyGUI::Align::Default);
toAdd->setEnabled(price<=playerGold);
mCurrentY += sLineHeight;
if(interior)
toAdd->setUserString("interior","y");
else
toAdd->setUserString("interior","n");
2012-10-08 09:14:22 +00:00
std::ostringstream oss;
oss << price;
toAdd->setUserString("price",oss.str());
2012-10-08 13:51:36 +00:00
toAdd->setCaptionWithReplacing("#{sCell=" + name + "} - " + boost::lexical_cast<std::string>(price)+"#{sgp}");
2012-09-26 16:30:47 +00:00
toAdd->setSize(toAdd->getTextSize().width,sLineHeight);
toAdd->eventMouseWheel += MyGUI::newDelegate(this, &TravelWindow::onMouseWheel);
toAdd->setUserString("Destination", name);
2012-10-08 09:14:22 +00:00
toAdd->setUserData(pos);
2012-09-26 16:30:47 +00:00
toAdd->eventMouseButtonClick += MyGUI::newDelegate(this, &TravelWindow::onTravelButtonClick);
}
void TravelWindow::clearDestinations()
{
mDestinationsView->setViewOffset(MyGUI::IntPoint(0,0));
mCurrentY = 0;
while (mDestinationsView->getChildCount())
MyGUI::Gui::getInstance().destroyWidget(mDestinationsView->getChildAt(0));
}
void TravelWindow::startTravel(const MWWorld::Ptr& actor)
{
center();
2012-09-28 15:02:27 +00:00
mPtr = actor;
2012-09-26 16:30:47 +00:00
clearDestinations();
2012-11-05 12:07:59 +00:00
for(unsigned int i = 0;i<mPtr.get<ESM::NPC>()->mBase->mTransport.size();i++)
2012-09-26 16:30:47 +00:00
{
2012-11-05 12:07:59 +00:00
std::string cellname = mPtr.get<ESM::NPC>()->mBase->mTransport[i].mCellName;
2012-10-08 09:14:22 +00:00
bool interior = true;
int x,y;
2012-11-05 12:07:59 +00:00
MWBase::Environment::get().getWorld()->positionToIndex(mPtr.get<ESM::NPC>()->mBase->mTransport[i].mPos.pos[0],
mPtr.get<ESM::NPC>()->mBase->mTransport[i].mPos.pos[1],x,y);
if (cellname == "")
{
MWWorld::CellStore* cell = MWBase::Environment::get().getWorld()->getExterior(x,y);
cellname = MWBase::Environment::get().getWorld()->getCellName(cell);
interior = false;
}
2012-11-05 12:07:59 +00:00
addDestination(cellname,mPtr.get<ESM::NPC>()->mBase->mTransport[i].mPos,interior);
2012-09-26 16:30:47 +00:00
}
updateLabels();
mDestinationsView->setCanvasSize (MyGUI::IntSize(mDestinationsView->getWidth(), std::max(mDestinationsView->getHeight(), mCurrentY)));
}
void TravelWindow::onTravelButtonClick(MyGUI::Widget* _sender)
{
2012-10-08 09:14:22 +00:00
std::istringstream iss(_sender->getUserString("price"));
int price;
iss >> price;
2012-09-26 16:30:47 +00:00
MWWorld::Ptr player = MWBase::Environment::get().getWorld()->getPlayerPtr();
int playerGold = player.getClass().getContainerStore(player).count(MWWorld::ContainerStore::sGoldId);
if (playerGold<price)
return;
if (!mPtr.getCell()->isExterior())
// Interior cell -> mages guild transport
MWBase::Environment::get().getSoundManager()->playSound("mysticism cast", 1, 1);
player.getClass().getContainerStore(player).remove(MWWorld::ContainerStore::sGoldId, price, player);
MWBase::Environment::get().getWorld ()->getFader ()->fadeOut(1);
ESM::Position pos = *_sender->getUserData<ESM::Position>();
std::string cellname = _sender->getUserString("Destination");
bool interior = _sender->getUserString("interior") == "y";
if (!interior)
2012-09-26 16:30:47 +00:00
{
ESM::Position playerPos = player.getRefData().getPosition();
float d = Ogre::Vector3(pos.pos[0], pos.pos[1], 0).distance(
Ogre::Vector3(playerPos.pos[0], playerPos.pos[1], 0));
int hours = static_cast<int>(d /MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>().find("fTravelTimeMult")->getFloat());
for(int i = 0;i < hours;i++)
2012-10-16 17:34:29 +00:00
{
2014-01-14 01:20:13 +00:00
MWBase::Environment::get().getMechanicsManager ()->rest (true);
2012-10-16 17:34:29 +00:00
}
MWBase::Environment::get().getWorld()->advanceTime(hours);
2012-10-08 09:14:22 +00:00
}
// Teleports any followers, too.
MWWorld::ActionTeleport action(interior ? cellname : "", pos);
action.execute(player);
MWBase::Environment::get().getWindowManager()->removeGuiMode(GM_Travel);
MWBase::Environment::get().getWindowManager()->removeGuiMode(GM_Dialogue);
MWBase::Environment::get().getWorld ()->getFader ()->fadeOut(0);
MWBase::Environment::get().getWorld ()->getFader ()->fadeIn(1);
2012-09-26 16:30:47 +00:00
}
void TravelWindow::onCancelButtonClicked(MyGUI::Widget* _sender)
{
exit();
2012-09-26 16:30:47 +00:00
}
void TravelWindow::updateLabels()
{
MWWorld::Ptr player = MWBase::Environment::get().getWorld ()->getPlayerPtr();
int playerGold = player.getClass().getContainerStore(player).count(MWWorld::ContainerStore::sGoldId);
mPlayerGold->setCaptionWithReplacing("#{sGold}: " + boost::lexical_cast<std::string>(playerGold));
2012-09-26 16:30:47 +00:00
mPlayerGold->setCoord(8,
mPlayerGold->getTop(),
mPlayerGold->getTextSize().width,
mPlayerGold->getHeight());
}
void TravelWindow::onReferenceUnavailable()
{
MWBase::Environment::get().getWindowManager()->removeGuiMode(GM_Travel);
MWBase::Environment::get().getWindowManager()->removeGuiMode(GM_Dialogue);
2012-09-26 16:30:47 +00:00
}
void TravelWindow::onMouseWheel(MyGUI::Widget* _sender, int _rel)
{
if (mDestinationsView->getViewOffset().top + _rel*0.3 > 0)
mDestinationsView->setViewOffset(MyGUI::IntPoint(0, 0));
else
mDestinationsView->setViewOffset(MyGUI::IntPoint(0, mDestinationsView->getViewOffset().top + _rel*0.3));
}
}