1
/*
2
 *  The Mana World
3
 *  Copyright (C) 2007  The Mana World Development Team
4
 *
5
 *  This file is part of The Mana World.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program; if not, write to the Free Software
19
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
 */
21
22
#include "configuration.h"
23
#include "inventory.h"
24
#include "item.h"
25
#include "itemshortcut.h"
26
#include "localplayer.h"
27
28
#include "net/inventoryhandler.h"
29
#include "net/net.h"
30
31
#include "utils/stringutils.h"
32
33
ItemShortcut::ItemShortcut *itemShortcut;
34
35
ItemShortcut::ItemShortcut():
36
    mItemSelected(-1)
37
{
38
    for (int i = 0; i < SHORTCUT_ITEMS; i++)
39
        mItems[i] = -1;
40
41
    load();
42
}
43
44
ItemShortcut::~ItemShortcut()
45
{
46
    save();
47
}
48
49
void ItemShortcut::load()
50
{
51
    for (int i = 0; i < SHORTCUT_ITEMS; i++)
52
    {
53
        int itemId = (int) config.getValue("shortcut" + toString(i), -1);
54
55
        if (itemId != -1)
56
            mItems[i] = itemId;
57
    }
58
}
59
60
void ItemShortcut::save()
61
{
62
    for (int i = 0; i < SHORTCUT_ITEMS; i++)
63
    {
64
        const int itemId = mItems[i] ? mItems[i] : -1;
65
        config.setValue("shortcut" + toString(i), itemId);
66
    }
67
}
68
69
void ItemShortcut::useItem(int index)
70
{
71
    if (mItems[index])
72
    {
73
        Item *item = player_node->getInventory()->findItem(mItems[index]);
74
        if (item && item->getQuantity())
75
        {
76
            if (item->isEquipment())
77
            {
78
                if (item->isEquipped())
79
                    Net::getInventoryHandler()->unequipItem(item);
80
                else
81
                    Net::getInventoryHandler()->equipItem(item);
82
            }
83
            else
84
            {
85
                Net::getInventoryHandler()->useItem(item);
86
            }
87
        }
88
    }
89
}