/*
* The ManaPlus Client
* Copyright (C) 2004-2009 The Mana World Development Team
* Copyright (C) 2009-2010 The Mana Developers
* Copyright (C) 2011-2016 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/graphicsvertexes.h"
#include "resources/equipmentslots.h"
#include "resources/imageset.h"
#include "utils/delete2.h"
#include "utils/dtor.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", this)),
mImageSet(nullptr),
mBeing(being),
mSlotBackground(),
mSlotHighlightedBackground(),
mVertexes(new ImageCollection),
mPages(),
mTabs(nullptr),
mHighlightColor(getThemeColor(ThemeColorId::HIGHLIGHT)),
mBorderColor(getThemeColor(ThemeColorId::BORDER)),
mLabelsColor(getThemeColor(ThemeColorId::LABEL)),
mLabelsColor2(getThemeColor(ThemeColorId::LABEL_OUTLINE)),
mSelected(-1),
mItemPadding(getOption("itemPadding")),
mBoxSize(getOption("boxSize")),
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);
mTabs->addActionListener(this);
mTabs->setActionEventId("tab_");
mYPadding = mTabs->getHeight() + getOption("tabPadding", 2);
if (setupWindow)
setupWindow->registerWindowForReset(this);
if (!mBoxSize)
mBoxSize = 36;
// Control that shows the Player
mPlayerBox->setDimension(Rect(50, 80 + mYPadding, 74, 168));
mPlayerBox->setPlayer(being);
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)
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)
{
mImageSet->decRef();
mImageSet = nullptr;
}
if (mSlotBackground)
mSlotBackground->decRef();
if (mSlotHighlightedBackground)
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)
{
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)
{
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)
continue;
const Item *const item = mEquipment->getEquipment(i);
if (item)
{
// Draw Item.
Image *const image = item->getImage();
if (image)
{
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)
{
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)
continue;
if (i == mSelected)
{
graphics->drawImage(mSlotHighlightedBackground,
box->x, box->y);
}
else
{
graphics->drawImage(mSlotBackground, box->x, box->y);
}
}
if (!mEquipment)
{
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)
continue;
const Item *const item = mEquipment->getEquipment(i);
if (item)
{
// Draw Item.
Image *const image = item->getImage();
if (image)
{
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)
{
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 || mSelected == -1)
return;
const Item *const item = mEquipment->getEquipment(mSelected);
PlayerInfo::unequipItem(item, Sfx_true);
setSelected(-1);
}
else if (!eventId.find("tab_"))
{
Button *const button = dynamic_cast