1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-15 21:19:57 +00:00
openmw-tes3mp/apps/openmw/mwgui/companionwindow.cpp

200 lines
5.6 KiB
C++
Raw Normal View History

2013-03-31 12:36:03 +00:00
#include "companionwindow.hpp"
2018-02-24 12:13:14 +00:00
#include <cmath>
2015-01-10 01:50:43 +00:00
#include <MyGUI_InputManager.h>
2013-03-31 12:36:03 +00:00
#include "../mwbase/environment.hpp"
2015-01-10 02:56:06 +00:00
#include "../mwbase/windowmanager.hpp"
2013-03-31 12:36:03 +00:00
2013-05-11 16:38:27 +00:00
#include "../mwworld/class.hpp"
2013-03-31 12:36:03 +00:00
#include "messagebox.hpp"
2013-05-11 16:38:27 +00:00
#include "itemview.hpp"
#include "sortfilteritemmodel.hpp"
#include "companionitemmodel.hpp"
#include "draganddrop.hpp"
2013-05-11 16:38:27 +00:00
#include "countdialog.hpp"
2017-09-27 10:40:47 +00:00
#include "widgets.hpp"
#include "tooltips.hpp"
2013-03-31 12:36:03 +00:00
namespace
{
int getProfit(const MWWorld::Ptr& actor)
{
std::string script = actor.getClass().getScript(actor);
if (!script.empty())
{
return actor.getRefData().getLocals().getIntVar(script, "minimumprofit");
}
return 0;
}
}
2013-03-31 12:36:03 +00:00
namespace MWGui
{
CompanionWindow::CompanionWindow(DragAndDrop *dragAndDrop, MessageBoxManager* manager)
2013-05-11 16:38:27 +00:00
: WindowBase("openmw_companion_window.layout")
2018-10-09 06:21:12 +00:00
, mSortModel(nullptr)
, mModel(nullptr)
, mSelectedItem(-1)
2013-05-11 16:38:27 +00:00
, mDragAndDrop(dragAndDrop)
2013-03-31 12:36:03 +00:00
, mMessageBoxManager(manager)
{
getWidget(mCloseButton, "CloseButton");
getWidget(mProfitLabel, "ProfitLabel");
getWidget(mEncumbranceBar, "EncumbranceBar");
getWidget(mFilterEdit, "FilterEdit");
2013-05-11 16:38:27 +00:00
getWidget(mItemView, "ItemView");
mItemView->eventBackgroundClicked += MyGUI::newDelegate(this, &CompanionWindow::onBackgroundSelected);
mItemView->eventItemClicked += MyGUI::newDelegate(this, &CompanionWindow::onItemSelected);
mFilterEdit->eventEditTextChange += MyGUI::newDelegate(this, &CompanionWindow::onNameFilterChanged);
2013-03-31 12:36:03 +00:00
mCloseButton->eventMouseButtonClick += MyGUI::newDelegate(this, &CompanionWindow::onCloseButtonClicked);
setCoord(200,0,600,300);
}
2013-05-11 16:38:27 +00:00
void CompanionWindow::onItemSelected(int index)
2013-03-31 12:36:03 +00:00
{
if (mDragAndDrop->mIsOnDragAndDrop)
{
mDragAndDrop->drop(mModel, mItemView);
updateEncumbranceBar();
return;
}
const ItemStack& item = mSortModel->getItem(index);
// We can't take conjured items from a companion NPC
if (item.mFlags & ItemStack::Flag_Bound)
{
MWBase::Environment::get().getWindowManager()->messageBox("#{sBarterDialog12}");
return;
}
2013-05-11 16:38:27 +00:00
MWWorld::Ptr object = item.mBase;
int count = item.mCount;
bool shift = MyGUI::InputManager::getInstance().isShiftPressed();
if (MyGUI::InputManager::getInstance().isControlPressed())
count = 1;
mSelectedItem = mSortModel->mapToSource(index);
if (count > 1 && !shift)
{
CountDialog* dialog = MWBase::Environment::get().getWindowManager()->getCountDialog();
std::string name = object.getClass().getName(object) + MWGui::ToolTips::getSoulString(object.getCellRef());
dialog->openCountDialog(name, "#{sTake}", count);
2013-05-11 16:38:27 +00:00
dialog->eventOkClicked.clear();
dialog->eventOkClicked += MyGUI::newDelegate(this, &CompanionWindow::dragItem);
}
else
2018-10-09 06:21:12 +00:00
dragItem (nullptr, count);
2013-03-31 12:36:03 +00:00
}
void CompanionWindow::onNameFilterChanged(MyGUI::EditBox* _sender)
{
mSortModel->setNameFilter(_sender->getCaption());
mItemView->update();
}
2013-05-11 16:38:27 +00:00
void CompanionWindow::dragItem(MyGUI::Widget* sender, int count)
2013-03-31 12:36:03 +00:00
{
2013-05-11 16:38:27 +00:00
mDragAndDrop->startDrag(mSelectedItem, mSortModel, mModel, mItemView, count);
}
void CompanionWindow::onBackgroundSelected()
{
if (mDragAndDrop->mIsOnDragAndDrop)
2013-03-31 12:36:03 +00:00
{
2013-05-11 16:38:27 +00:00
mDragAndDrop->drop(mModel, mItemView);
updateEncumbranceBar();
2013-03-31 12:36:03 +00:00
}
2013-05-11 16:38:27 +00:00
}
void CompanionWindow::setPtr(const MWWorld::Ptr& npc)
2013-05-11 16:38:27 +00:00
{
mPtr = npc;
updateEncumbranceBar();
mModel = new CompanionItemModel(npc);
mSortModel = new SortFilterItemModel(mModel);
mFilterEdit->setCaption(std::string());
2013-05-11 16:38:27 +00:00
mItemView->setModel(mSortModel);
mItemView->resetScrollBars();
setTitle(npc.getClass().getName(npc));
2013-05-11 16:38:27 +00:00
}
void CompanionWindow::onFrame(float dt)
2013-05-11 16:38:27 +00:00
{
checkReferenceAvailable();
2013-03-31 12:36:03 +00:00
updateEncumbranceBar();
}
void CompanionWindow::updateEncumbranceBar()
{
2013-05-11 16:38:27 +00:00
if (mPtr.isEmpty())
return;
float capacity = mPtr.getClass().getCapacity(mPtr);
float encumbrance = mPtr.getClass().getEncumbrance(mPtr);
2018-02-24 12:13:14 +00:00
mEncumbranceBar->setValue(std::ceil(encumbrance), static_cast<int>(capacity));
2013-03-31 12:36:03 +00:00
if (mModel && mModel->hasProfit(mPtr))
2013-03-31 12:36:03 +00:00
{
mProfitLabel->setCaptionWithReplacing("#{sProfitValue} " + MyGUI::utility::toString(getProfit(mPtr)));
2013-03-31 12:36:03 +00:00
}
else
mProfitLabel->setCaption("");
2013-03-31 12:36:03 +00:00
}
void CompanionWindow::onCloseButtonClicked(MyGUI::Widget* _sender)
{
2017-09-23 10:18:39 +00:00
if (exit())
MWBase::Environment::get().getWindowManager()->removeGuiMode(GM_Companion);
}
2017-09-23 10:18:39 +00:00
bool CompanionWindow::exit()
2013-03-31 12:36:03 +00:00
{
if (mModel && mModel->hasProfit(mPtr) && getProfit(mPtr) < 0)
2013-03-31 12:36:03 +00:00
{
std::vector<std::string> buttons;
2020-10-17 08:26:35 +00:00
buttons.emplace_back("#{sCompanionWarningButtonOne}");
buttons.emplace_back("#{sCompanionWarningButtonTwo}");
2013-03-31 12:36:03 +00:00
mMessageBoxManager->createInteractiveMessageBox("#{sCompanionWarningMessage}", buttons);
mMessageBoxManager->eventButtonPressed += MyGUI::newDelegate(this, &CompanionWindow::onMessageBoxButtonClicked);
2017-09-23 10:18:39 +00:00
return false;
2013-03-31 12:36:03 +00:00
}
2017-09-23 10:18:39 +00:00
return true;
2013-03-31 12:36:03 +00:00
}
void CompanionWindow::onMessageBoxButtonClicked(int button)
{
if (button == 0)
{
MWBase::Environment::get().getWindowManager()->removeGuiMode(GM_Companion);
// Important for Calvus' contract script to work properly
MWBase::Environment::get().getWindowManager()->exitCurrentGuiMode();
2013-03-31 12:36:03 +00:00
}
}
void CompanionWindow::onReferenceUnavailable()
{
MWBase::Environment::get().getWindowManager()->removeGuiMode(GM_Companion);
}
void CompanionWindow::resetReference()
{
ReferenceInterface::resetReference();
2018-10-09 06:21:12 +00:00
mItemView->setModel(nullptr);
mModel = nullptr;
mSortModel = nullptr;
}
2013-03-31 12:36:03 +00:00
}