1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-02-09 09:39:53 +00:00
OpenMW/apps/openmw/mwgui/companionwindow.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

204 lines
6.3 KiB
C++
Raw Normal View History

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