/*
* The ManaPlus Client
* Copyright (C) 2004-2009 The Mana World Development Team
* Copyright (C) 2009-2010 The Mana Developers
* Copyright (C) 2011-2019 The ManaPlus Developers
*
* This file is part of The ManaPlus Client.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
#include "gui/windows/equipmentwindow.h"
#include "configuration.h"
#include "dragdrop.h"
#include "being/localplayer.h"
#include "being/playerinfo.h"
#include "gui/fonts/font.h"
#include "gui/popups/popupmenu.h"
#include "gui/popups/itempopup.h"
#include "gui/windows/setupwindow.h"
#include "gui/widgets/button.h"
#include "gui/widgets/equipmentbox.h"
#include "gui/widgets/equipmentpage.h"
#include "gui/widgets/playerbox.h"
#include "gui/widgets/tabstrip.h"
#include "render/vertexes/imagecollection.h"
#include "resources/imageset.h"
#include "utils/checkutils.h"
#include "utils/delete2.h"
#include "utils/dtor.h"
#include "utils/foreach.h"
#include "utils/gettext.h"
#include "net/inventoryhandler.h"
#include "debug.h"
EquipmentWindow *equipmentWindow = nullptr;
EquipmentWindow *beingEquipmentWindow = nullptr;
static const int BOX_COUNT = 27;
StringIntMap EquipmentWindow::mSlotNames;
EquipmentWindow::EquipmentWindow(Equipment *const equipment,
Being *const being,
const bool foring) :
// TRANSLATORS: equipment window name
Window(_("Equipment"), Modal_false, nullptr, "equipment.xml"),
ActionListener(),
mEquipment(equipment),
mPlayerBox(new PlayerBox(this,
"equipment_playerbox.xml",
"equipment_selectedplayerbox.xml")),
// TRANSLATORS: equipment window button
mUnequip(new Button(this, _("Unequip"), "unequip", BUTTON_SKIN, this)),
mImageSet(nullptr),
mBeing(being),
mSlotBackground(),
mSlotHighlightedBackground(),
mVertexes(new ImageCollection),
mPages(),
mTabs(nullptr),
mHighlightColor(getThemeColor(ThemeColorId::HIGHLIGHT, 255U)),
mBorderColor(getThemeColor(ThemeColorId::BORDER, 255U)),
mLabelsColor(getThemeColor(ThemeColorId::LABEL, 255U)),
mLabelsColor2(getThemeColor(ThemeColorId::LABEL_OUTLINE, 255U)),
mSelected(-1),
mItemPadding(getOption("itemPadding", 0)),
mBoxSize(getOption("boxSize", 0)),
mButtonPadding(getOption("buttonPadding", 5)),
mMinX(180),
mMinY(345),
mMaxX(0),
mMaxY(0),
mYPadding(0),
mSelectedTab(0),
mForing(foring),
mHaveDefaultPage(false)
{
const int size = config.getIntValue("fontSize")
+ getOption("tabHeightAdjust", 16);
mTabs = new TabStrip(this, "equipment", size, 0);
mTabs->addActionListener(this);
mTabs->setActionEventId("tab_");
mTabs->setSelectable(false);
mYPadding = mTabs->getHeight() + getOption("tabPadding", 2);
if (setupWindow != nullptr)
setupWindow->registerWindowForReset(this);
if (mBoxSize == 0)
mBoxSize = 36;
// Control that shows the Player
mPlayerBox->setDimension(Rect(50, 80 + mYPadding, 74, 168));
mPlayerBox->setPlayer(being);
mPlayerBox->setSelectable(false);
if (foring)
setWindowName("Being equipment");
else
setWindowName("Equipment");
setCloseButton(true);
setSaveVisible(true);
setStickyButtonLock(true);
fillBoxes();
recalcSize();
updatePage();
loadWindowState();
}
void EquipmentWindow::postInit()
{
Window::postInit();
const Rect &area = getChildrenArea();
mUnequip->setPosition(area.width - mUnequip->getWidth() - mButtonPadding,
area.height - mUnequip->getHeight() - mButtonPadding);
mUnequip->setEnabled(false);
ImageRect rect;
theme->loadRect(rect, "equipment_background.xml", "", 0, 1);
mSlotBackground = rect.grid[0];
mSlotHighlightedBackground = rect.grid[1];
add(mTabs);
add(mPlayerBox);
add(mUnequip);
enableVisibleSound(true);
mPlayerBox->setActionEventId("playerbox");
mPlayerBox->addActionListener(this);
}
EquipmentWindow::~EquipmentWindow()
{
if (this == beingEquipmentWindow)
{
if (mEquipment != nullptr)
delete mEquipment->getBackend();
delete2(mEquipment)
}
FOR_EACH (STD_VECTOR::iterator, it, mPages)
{
STD_VECTOR &boxes = (*it)->boxes;
delete_all(boxes);
boxes.clear();
delete *it;
}
if (mImageSet != nullptr)
{
mImageSet->decRef();
mImageSet = nullptr;
}
if (mSlotBackground != nullptr)
mSlotBackground->decRef();
if (mSlotHighlightedBackground != nullptr)
mSlotHighlightedBackground->decRef();
delete2(mVertexes)
}
void EquipmentWindow::draw(Graphics *const graphics)
{
BLOCK_START("EquipmentWindow::draw")
// Draw window graphics
Window::draw(graphics);
int i = 0;
Font *const font = getFont();
const int fontHeight = font->getHeight();
const STD_VECTOR &boxes = mPages[mSelectedTab]->boxes;
if (mLastRedraw)
{
mVertexes->clear();
FOR_EACH (STD_VECTOR::const_iterator, it, boxes)
{
const EquipmentBox *const box = *it;
if (box == nullptr)
{
i ++;
continue;
}
if (i == mSelected)
{
graphics->calcTileCollection(mVertexes,
mSlotHighlightedBackground,
box->x, box->y);
}
else
{
graphics->calcTileCollection(mVertexes,
mSlotBackground,
box->x, box->y);
}
i ++;
}
graphics->finalize(mVertexes);
}
graphics->drawTileCollection(mVertexes);
if (mEquipment == nullptr)
{
BLOCK_END("EquipmentWindow::draw")
return;
}
i = 0;
const int projSlot = inventoryHandler->getProjectileSlot();
for (STD_VECTOR::const_iterator it = boxes.begin(),
it_end = boxes.end(); it != it_end; ++ it, ++ i)
{
const EquipmentBox *const box = *it;
if (box == nullptr)
continue;
const Item *const item = mEquipment->getEquipment(i);
if (item != nullptr)
{
// Draw Item.
Image *const image = item->getImage();
if (image != nullptr)
{
image->setAlpha(1.0F); // Ensure the image is drawn
// with maximum opacity
graphics->drawImage(image, box->x + mItemPadding,
box->y + mItemPadding);
if (i == projSlot)
{
const std::string str = toString(item->getQuantity());
font->drawString(graphics,
mLabelsColor,
mLabelsColor2,
str,
box->x + (mBoxSize - font->getWidth(str)) / 2,
box->y - fontHeight);
}
}
}
else if (box->image != nullptr)
{
graphics->drawImage(box->image,
box->x + mItemPadding,
box->y + mItemPadding);
}
}
BLOCK_END("EquipmentWindow::draw")
}
void EquipmentWindow::safeDraw(Graphics *const graphics)
{
BLOCK_START("EquipmentWindow::draw")
// Draw window graphics
Window::safeDraw(graphics);
int i = 0;
Font *const font = getFont();
const int fontHeight = font->getHeight();
const STD_VECTOR &boxes = mPages[mSelectedTab]->boxes;
for (STD_VECTOR::const_iterator it = boxes.begin(),
it_end = boxes.end(); it != it_end; ++ it, ++ i)
{
const EquipmentBox *const box = *it;
if (box == nullptr)
continue;
if (i == mSelected)
{
graphics->drawImage(mSlotHighlightedBackground,
box->x, box->y);
}
else
{
graphics->drawImage(mSlotBackground, box->x, box->y);
}
}
if (mEquipment == nullptr)
{
BLOCK_END("EquipmentWindow::draw")
return;
}
i = 0;
const int projSlot = inventoryHandler->getProjectileSlot();
for (STD_VECTOR::const_iterator it = boxes.begin(),
it_end = boxes.end(); it != it_end; ++ it, ++ i)
{
const EquipmentBox *const box = *it;
if (box == nullptr)
continue;
const Item *const item = mEquipment->getEquipment(i);
if (item != nullptr)
{
// Draw Item.
Image *const image = item->getImage();
if (image != nullptr)
{
image->setAlpha(1.0F); // Ensure the image is drawn
// with maximum opacity
graphics->drawImage(image, box->x + mItemPadding,
box->y + mItemPadding);
if (i == projSlot)
{
const std::string str = toString(item->getQuantity());
font->drawString(graphics,
mLabelsColor,
mLabelsColor2,
str,
box->x + (mBoxSize - font->getWidth(str)) / 2,
box->y - fontHeight);
}
}
}
else if (box->image != nullptr)
{
graphics->drawImage(box->image,
box->x + mItemPadding,
box->y + mItemPadding);
}
}
BLOCK_END("EquipmentWindow::draw")
}
void EquipmentWindow::action(const ActionEvent &event)
{
const std::string &eventId = event.getId();
if (eventId == "unequip")
{
if ((mEquipment == nullptr) || mSelected == -1)
return;
const Item *const item = mEquipment->getEquipment(mSelected);
PlayerInfo::unequipItem(item, Sfx_true);
setSelected(-1);
}
else if (eventId.find("tab_") == 0U)
{
Button *const button = dynamic_cast