1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-29 18:32:36 +00:00
OpenMW/apps/openmw/mwgui/itemview.cpp

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

169 lines
5.2 KiB
C++
Raw Normal View History

2013-05-11 18:38:27 +02:00
#include "itemview.hpp"
2014-12-17 09:33:40 +01:00
#include <cmath>
2013-05-11 18:38:27 +02:00
#include <MyGUI_FactoryManager.h>
#include <MyGUI_Gui.h>
#include <MyGUI_ImageBox.h>
#include <MyGUI_ScrollView.h>
#include "itemmodel.hpp"
#include "itemwidget.hpp"
2013-05-11 18:38:27 +02:00
namespace MWGui
2013-05-11 18:38:27 +02:00
{
ItemView::ItemView()
2022-08-31 19:03:45 +02:00
: mScrollView(nullptr)
2013-05-11 18:38:27 +02:00
{
}
2022-08-31 19:03:45 +02:00
void ItemView::setModel(std::unique_ptr<ItemModel> model)
2013-05-11 18:38:27 +02:00
{
2022-08-31 19:03:45 +02:00
mModel = std::move(model);
update();
2013-05-11 18:38:27 +02:00
}
void ItemView::initialiseOverride()
{
Base::initialiseOverride();
assignWidget(mScrollView, "ScrollView");
2018-10-09 10:21:12 +04:00
if (mScrollView == nullptr)
2013-05-11 18:38:27 +02:00
throw std::runtime_error("Item view needs a scroll view");
mScrollView->setCanvasAlign(MyGUI::Align::Left | MyGUI::Align::Top);
}
void ItemView::layoutWidgets()
{
if (!mScrollView->getChildCount())
return;
int x = 0;
int y = 0;
MyGUI::Widget* dragArea = mScrollView->getChildAt(0);
2014-12-18 17:05:24 +01:00
int maxHeight = mScrollView->getHeight();
2014-12-15 19:04:53 +01:00
int rows = maxHeight / 42;
rows = std::max(rows, 1);
bool showScrollbar = int(std::ceil(dragArea->getChildCount() / float(rows))) > mScrollView->getWidth() / 42;
2014-12-15 19:04:53 +01:00
if (showScrollbar)
maxHeight -= 18;
for (unsigned int i = 0; i < dragArea->getChildCount(); ++i)
{
MyGUI::Widget* w = dragArea->getChildAt(i);
w->setPosition(x, y);
y += 42;
2014-12-15 19:04:53 +01:00
if (y > maxHeight - 42 && i < dragArea->getChildCount() - 1)
{
x += 42;
y = 0;
2022-09-22 21:26:05 +03:00
}
}
2022-09-22 21:26:05 +03:00
x += 42;
MyGUI::IntSize size = MyGUI::IntSize(std::max(mScrollView->getSize().width, x), mScrollView->getSize().height);
2022-09-22 21:26:05 +03:00
// Canvas size must be expressed with VScroll disabled, otherwise MyGUI would expand the scroll area when the
// scrollbar is hidden
mScrollView->setVisibleVScroll(false);
mScrollView->setVisibleHScroll(false);
mScrollView->setCanvasSize(size);
mScrollView->setVisibleVScroll(true);
mScrollView->setVisibleHScroll(true);
dragArea->setSize(size);
}
void ItemView::update()
2022-09-22 21:26:05 +03:00
{
while (mScrollView->getChildCount())
MyGUI::Gui::getInstance().destroyWidget(mScrollView->getChildAt(0));
if (!mModel)
return;
2013-05-11 18:38:27 +02:00
mModel->update();
MyGUI::Widget* dragArea = mScrollView->createWidget<MyGUI::Widget>(
{}, 0, 0, mScrollView->getWidth(), mScrollView->getHeight(), MyGUI::Align::Stretch);
2013-05-11 18:38:27 +02:00
dragArea->setNeedMouseFocus(true);
dragArea->eventMouseButtonClick += MyGUI::newDelegate(this, &ItemView::onSelectedBackground);
dragArea->eventMouseWheel += MyGUI::newDelegate(this, &ItemView::onMouseWheelMoved);
2013-05-11 18:38:27 +02:00
for (ItemModel::ModelIndex i = 0; i < static_cast<int>(mModel->getItemCount()); ++i)
2022-09-22 21:26:05 +03:00
{
2013-05-11 18:38:27 +02:00
const ItemStack& item = mModel->getItem(i);
2022-09-22 21:26:05 +03:00
ItemWidget* itemWidget = dragArea->createWidget<ItemWidget>(
"MW_ItemIcon", MyGUI::IntCoord(0, 0, 42, 42), MyGUI::Align::Default);
itemWidget->setUserString("ToolTipType", "ItemModelIndex");
2013-05-11 18:38:27 +02:00
itemWidget->setUserData(std::make_pair(i, mModel.get()));
ItemWidget::ItemState state = ItemWidget::None;
if (item.mType == ItemStack::Type_Barter)
state = ItemWidget::Barter;
if (item.mType == ItemStack::Type_Equipped)
state = ItemWidget::Equip;
itemWidget->setItem(item.mBase, state);
2014-09-19 09:29:00 +02:00
itemWidget->setCount(item.mCount);
2022-09-22 21:26:05 +03:00
2013-05-11 18:38:27 +02:00
itemWidget->eventMouseButtonClick += MyGUI::newDelegate(this, &ItemView::onSelectedItem);
itemWidget->eventMouseWheel += MyGUI::newDelegate(this, &ItemView::onMouseWheelMoved);
2022-09-22 21:26:05 +03:00
}
2013-05-11 18:38:27 +02:00
layoutWidgets();
2022-09-22 21:26:05 +03:00
}
2013-05-11 18:38:27 +02:00
void ItemView::resetScrollBars()
{
mScrollView->setViewOffset(MyGUI::IntPoint(0, 0));
2013-05-11 18:38:27 +02:00
}
2013-05-11 18:38:27 +02:00
void ItemView::onSelectedItem(MyGUI::Widget* sender)
{
ItemModel::ModelIndex index = (*sender->getUserData<std::pair<ItemModel::ModelIndex, ItemModel*>>()).first;
eventItemClicked(index);
}
void ItemView::onSelectedBackground(MyGUI::Widget* sender)
{
eventBackgroundClicked();
}
void ItemView::onMouseWheelMoved(MyGUI::Widget* _sender, int _rel)
2013-05-11 18:38:27 +02:00
{
if (mScrollView->getViewOffset().left + _rel * 0.3f > 0)
2013-05-11 18:38:27 +02:00
mScrollView->setViewOffset(MyGUI::IntPoint(0, 0));
else
mScrollView->setViewOffset(
MyGUI::IntPoint(static_cast<int>(mScrollView->getViewOffset().left + _rel * 0.3f), 0));
2013-05-11 18:38:27 +02:00
}
void ItemView::setSize(const MyGUI::IntSize& _value)
{
bool changed = (_value.width != getWidth() || _value.height != getHeight());
2013-05-11 18:38:27 +02:00
Base::setSize(_value);
if (changed)
layoutWidgets();
2013-05-11 18:38:27 +02:00
}
void ItemView::setCoord(const MyGUI::IntCoord& _value)
{
bool changed = (_value.width != getWidth() || _value.height != getHeight());
2013-05-11 18:38:27 +02:00
Base::setCoord(_value);
if (changed)
layoutWidgets();
2013-05-11 18:38:27 +02:00
}
void ItemView::registerComponents()
{
MyGUI::FactoryManager::getInstance().registerFactory<MWGui::ItemView>("Widget");
}
}