summaryrefslogtreecommitdiff
path: root/src/gui/equipmentwindow.cpp
blob: c86a27fce4b257ac1b0ec94952de4c09a65ddc5e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 *  The Mana World
 *  Copyright 2004 The Mana World Development Team
 *
 *  This file is part of The Mana World.
 *
 *  The Mana World 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.
 *
 *  The Mana World 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 The Mana World; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  $Id$
 */

#define BOX_WIDTH 36
#define BOX_HEIGHT 36

#include "equipmentwindow.h"

#include "button.h"

#include "../graphics.h"
#include "../item.h"
#include "../localplayer.h"

#include "../resources/image.h"
#include "../resources/resourcemanager.h"

#include "../utils/gettext.h"

// Positions of the boxes, 2nd dimension is X and Y respectively.
static const int boxPosition[][2] = {
    {90, 100},   // EQUIP_TORSO_SLOT
    {135, 60},  // EQUIP_ARMS_SLOT
    {90, 10},    // EQUIP_HEAD_SLOT
    {90, 145},  // EQUIP_LEGS_SLOT
    {90, 190},  // EQUIP_FEET_SLOT
    {35, 105},   // EQUIP_RING1_SLOT
    {145, 105},  // EQUIP_RING2_SLOT
    {90, 55},   // EQUIP_NECKLACE_SLOT
    {20, 150},   // EQUIP_FIGHT1_SLOT
    {160, 150},  // EQUIP_FIGHT2_SLOT
    {45, 60}    // EQUIP_PROJECTILE_SLOT
};

EquipmentWindow::EquipmentWindow(Equipment *equipment):

    Window(_("Equipment")),
    mEquipment(equipment),
    mBackground(NULL),
    mSelected(-1)
{
    setCloseButton(true);
    setDefaultSize(5, 195, 216, 260);
    loadWindowState("Equipment");

    mUnequip = new Button(_("Unequip"), "unequip", this);
    mUnequip->setPosition(150,235);
    add(mUnequip);

    for (int i = 0; i < EQUIPMENT_SIZE; i++)
    {
        mEquipBox[i].posX = boxPosition[i][0] + getPadding();
        mEquipBox[i].posY = boxPosition[i][1] + getTitleBarHeight();
    }

    ResourceManager *resman = ResourceManager::getInstance();
    mBackground = resman->getImage("graphics/gui/equip_bg.png");
    mBackground->setAlpha(0.3);
}

EquipmentWindow::~EquipmentWindow()
{
    mBackground->decRef();
    delete mUnequip;
}

void EquipmentWindow::draw(gcn::Graphics *graphics)
{
    // Draw window graphics
    Window::draw(graphics);

    Graphics *g = static_cast<Graphics*>(graphics);

    if (mBackground)
    {
        g->drawImage(mBackground, getPadding() + 10, 0);
    }

    Window::drawChildren(graphics);

    for (int i = 0; i < EQUIPMENT_SIZE; i++)
    {
        int itemId = mEquipment->getEquipment(i);
        if (itemId)
        {
            // Draw Item.
            Image *image = Item(itemId).getInfo().getImage();
            g->drawImage(image, mEquipBox[i].posX, mEquipBox[i].posY);
        }

        if (i == mSelected)
        {
            // Set color red.
            g->setColor(gcn::Color(255, 0, 0));
        }
        else
        {
            // Set color black.
            g->setColor(gcn::Color(0, 0, 0));
        }
        // Draw box border.
        g->drawRectangle(gcn::Rectangle(mEquipBox[i].posX, mEquipBox[i].posY,
            BOX_WIDTH, BOX_HEIGHT));
    }
}

void EquipmentWindow::action(const gcn::ActionEvent &event)
{
    if (event.getId() == "unequip" && mSelected > -1)
    {
        player_node->unequipItem(mSelected);
        mSelected = -1;
    }
}

void EquipmentWindow::mousePressed(gcn::MouseEvent& mouseEvent)
{
    Window::mousePressed(mouseEvent);

    const int x = mouseEvent.getX();
    const int y = mouseEvent.getY();

    // Checks if any of the presses were in the equip boxes.
    for (int i = 0; i < EQUIPMENT_SIZE; i++)
    {
        gcn::Rectangle tRect(mEquipBox[i].posX, mEquipBox[i].posY,
                             BOX_WIDTH, BOX_HEIGHT);
        if (tRect.isPointInRect(x, y))
        {
            int itemId = mEquipment->getEquipment(i);
            if (itemId)
            {
                mSelected = i;
            }
        }
    }
}