summaryrefslogtreecommitdiff
path: root/src/gui/buy.cpp
blob: d291650d7411430ae28f72aeb7feb330ee102c79 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 *  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$
 */

#include "buy.h"
#include "button.h"
#include "slider.h"
#include "scrollarea.h"
#include "listbox.h"
#include "../game.h"
#include "../net/network.h"

#include <sstream>

BuyDialog::BuyDialog():
    Window("Buy"),
    money(0)
{
    itemList = new ListBox(this);
    scrollArea = new ScrollArea(itemList);
    slider = new Slider(1.0);
    quantityLabel = new gcn::Label("0");
    moneyLabel = new gcn::Label("350 G");
    okButton = new Button("OK");
    cancelButton = new Button("Cancel");
    okButton->setEnabled(false);
    itemNameLabel = new gcn::Label("Name:");
    itemDescLabel = new gcn::Label("Description:");

    setContentSize(260, 198);
    scrollArea->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER);
    scrollArea->setDimension(gcn::Rectangle(5, 5, 250, 110));
    itemList->setDimension(gcn::Rectangle(5, 5, 238, 110));
    slider->setDimension(gcn::Rectangle(5, 120, 200, 10));
    quantityLabel->setPosition(215, 120);
    moneyLabel->setPosition(5, 133);
    okButton->setPosition(180, 174);
    cancelButton->setPosition(208, 174);

    itemNameLabel->setDimension(gcn::Rectangle(5, 145, 240, 14));
    itemDescLabel->setDimension(gcn::Rectangle(5, 157, 240, 14));

    itemList->setEventId("item");
    slider->setEventId("slider");
    okButton->setEventId("ok");
    cancelButton->setEventId("cancel");

    itemList->addActionListener(this);
    slider->addActionListener(this);
    okButton->addActionListener(this);
    cancelButton->addActionListener(this);


    add(scrollArea);
    add(slider);
    add(quantityLabel);
    add(moneyLabel);
    add(okButton);
    add(cancelButton);
    add(itemNameLabel);
    add(itemDescLabel);

    setLocationRelativeTo(getParent());
}

BuyDialog::~BuyDialog()
{
    delete cancelButton;
    delete okButton;
    delete moneyLabel;
    delete slider;
    delete itemList;
    delete scrollArea;
    delete itemNameLabel;
    delete itemDescLabel;
}

void BuyDialog::setMoney(int amount)
{
    money = amount;
    std::stringstream ss;
    ss << money << " G";
    moneyLabel->setCaption(ss.str());
    moneyLabel->adjustSize();
}

void BuyDialog::reset()
{
    shopInventory.clear();
    money = 0;
}

void BuyDialog::addItem(short id, int price)
{
    ITEM_SHOP item_shop;

    sprintf(item_shop.name, "%s %i gp",
            itemDb->getItemInfo(id)->getName().c_str(), price);
    item_shop.price = price;
    item_shop.id = id;

    shopInventory.push_back(item_shop);
    itemList->adjustSize();
}

void BuyDialog::action(const std::string& eventId)
{
    int selectedItem = itemList->getSelected();

    if (eventId == "slider" || eventId == "item") {
        if (selectedItem > -1) {
            int maxItems = money / shopInventory[selectedItem].price;
            int numItems = (int)(slider->getValue() * maxItems);
            std::stringstream ss;

            ss << numItems;
            quantityLabel->setCaption(ss.str());
            quantityLabel->adjustSize();

            okButton->setEnabled(numItems > 0);
        }
        else {
            okButton->setEnabled(false);
        }
    }
    else if (eventId == "ok") {
        if (selectedItem > -1) {
            // Attempt purchase
            int maxItems = money / shopInventory[selectedItem].price;
            int amount = (int)(slider->getValue() * maxItems);

            if (amount > 0) {
                WFIFOW(0) = net_w_value(0x00c8);
                WFIFOW(2) = net_w_value(8);
                WFIFOW(4) = net_w_value(amount);
                WFIFOW(6) = net_w_value(shopInventory[selectedItem].id);
                WFIFOSET(8);
            }
        }
        setVisible(false);
        current_npc = 0;
    }
    else if (eventId == "cancel") {
        setVisible(false);
        current_npc = 0;
    }
}

void BuyDialog::mouseClick(int x, int y, int button, int count)
{
    Window::mouseClick(x, y, button, count);

//    shopInventory[selectedItem];
    int selectedItem = itemList->getSelected();
    if (selectedItem > -1)
    {
        itemNameLabel->setCaption("Name: " +
                itemDb->getItemInfo(shopInventory[selectedItem].id)->getName());
        itemDescLabel->setCaption("Description: " +
                itemDb->getItemInfo(shopInventory[selectedItem].id)->getDescription());
    }
}

int BuyDialog::getNumberOfElements()
{
    return shopInventory.size();
}

std::string BuyDialog::getElementAt(int i)
{
    return shopInventory[i].name;
}