summaryrefslogtreecommitdiff
path: root/src/gui/buy.cpp
blob: 60cc22c0d5f0e3486573c5e128e09fc2c9c400e1 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
 *  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 <sstream>

#include <guichan/widgets/label.hpp>

#include "button.h"
#include "listbox.h"
#include "scrollarea.h"
#include "shop.h"
#include "slider.h"

#include "../game.h"

#include "../resources/iteminfo.h"
#include "../resources/itemmanager.h"

#include "../net/network.h"


BuyDialog::BuyDialog():
    Window("Buy"),
    m_money(0), m_amountItems(0), m_maxItems(0)
{
    itemList = new ListBox(this);
    scrollArea = new ScrollArea(itemList);
    slider = new Slider(1.0);
    quantityLabel = new gcn::Label("0");
    moneyLabel = new gcn::Label("price : 0 G");
    increaseButton = new Button("+");
    decreaseButton = new Button("-");
    buyButton = new Button("Buy");
    quitButton = new Button("Quit");
    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));
    slider->setEnabled(false);

    quantityLabel->setPosition(215, 120);
    moneyLabel->setPosition(5, 133);

    increaseButton->setPosition(40, 174);
    increaseButton->setSize(20, 20);
    increaseButton->setEnabled(false);

    decreaseButton->setPosition(10, 174);
    decreaseButton->setSize(20, 20);
    decreaseButton->setEnabled(false);

    buyButton->setPosition(180, 174);
    buyButton->setEnabled(false);

    quitButton->setPosition(212, 174);

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

    itemList->setEventId("item");
    slider->setEventId("slider");
    increaseButton->setEventId("+");
    decreaseButton->setEventId("-");
    buyButton->setEventId("buy");
    quitButton->setEventId("quit");

    itemList->addActionListener(this);
    slider->addActionListener(this);
    increaseButton->addActionListener(this);
    decreaseButton->addActionListener(this);
    buyButton->addActionListener(this);
    quitButton->addActionListener(this);


    add(scrollArea);
    add(slider);
    add(quantityLabel);
    add(moneyLabel);
    add(buyButton);
    add(quitButton);
    add(increaseButton);
    add(decreaseButton);
    add(itemNameLabel);
    add(itemDescLabel);

    setLocationRelativeTo(getParent());
}

BuyDialog::~BuyDialog()
{
    delete increaseButton;
    delete decreaseButton;
    delete quitButton;
    delete buyButton;
    delete moneyLabel;
    delete slider;
    delete itemList;
    delete scrollArea;
    delete itemNameLabel;
    delete itemDescLabel;
}

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

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

    // Reset Previous Selected Items to prevent failing asserts
    itemList->setSelected(-1);
    increaseButton->setEnabled(false);
    decreaseButton->setEnabled(false);
}

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

    ss << itemDb->getItemInfo(id)->getName() << " " << price << " GP";
    item_shop.name = ss.str();
    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 == "item") {
        // Reset amount of items and update labels
        m_amountItems = 0;
        slider->setValue(0);
        quantityLabel->setCaption("0");
        quantityLabel->adjustSize();
        moneyLabel->setCaption("price : 0 GP");
        moneyLabel->adjustSize();

        // Disable buttons for buying and decreasing
        buyButton->setEnabled(false);
        decreaseButton->setEnabled(false);

        // If no item was selected, none can be bought, otherwise
        // calculate how many the player can afford
        m_maxItems = (itemList->getSelected() == -1) ? 0 :
            m_money / shopInventory[selectedItem].price;

        // When at least one item can be bought, enable the slider and the
        // increase button
        increaseButton->setEnabled(m_maxItems > 0);
        slider->setEnabled(m_maxItems > 0);
    }
    else if (eventId == "quit") {
        setVisible(false);
        current_npc = 0;
    }

    // The following actions require a valid selection
    if (selectedItem < 0 || selectedItem >= int(shopInventory.size())) {
        return;
    }

    bool updateButtonsAndLabels = false;

    if (eventId == "slider") {
        m_amountItems = (int)(slider->getValue() * m_maxItems);
        updateButtonsAndLabels = true;
    }
    else if (eventId == "+") {
        if (m_amountItems < m_maxItems) {
            m_amountItems++;
        } else {
            m_amountItems = m_maxItems;
        }

        slider->setValue(double(m_amountItems)/double(m_maxItems));
        updateButtonsAndLabels = true;
    }
    else if (eventId == "-") {
        if (m_amountItems > 0) {
            m_amountItems--;
        } else {
            m_amountItems = 0;
        }

        slider->setValue(double(m_amountItems)/double(m_maxItems));
        updateButtonsAndLabels = true;
    }
    // TODO Actually we'd have a bug elsewhere if this check for the number
    // of items to be bought ever fails, Bertram removed the assertions, is
    // there a better way to ensure this fails in an _obivous_ way in C++?
    else if (eventId == "buy" && (m_amountItems > 0 &&
                m_amountItems <= m_maxItems)) {
        WFIFOW(0) = net_w_value(0x00c8);
        WFIFOW(2) = net_w_value(8);
        WFIFOW(4) = net_w_value(m_amountItems);
        WFIFOW(6) = net_w_value(shopInventory[selectedItem].id);
        WFIFOSET(8);

        // update money !
        m_money -= m_amountItems * shopInventory[selectedItem].price;
        // Update number of items that can be bought at max
        m_maxItems -= m_amountItems;

        if (!m_maxItems) {
            slider->setEnabled(false);
        }

        // Reset selection
        m_amountItems = 0;
        slider->setValue(0);

        updateButtonsAndLabels = true;
    }

    // If anything has changed, we have to update the buttons and labels
    if (updateButtonsAndLabels) {
        std::stringstream oss;

        // Update buttons
        increaseButton->setEnabled(m_amountItems < m_maxItems);
        decreaseButton->setEnabled(m_amountItems > 0);
        buyButton->setEnabled(m_amountItems > 0);

        // Update labels
        oss << m_amountItems;
        quantityLabel->setCaption(oss.str());
        quantityLabel->adjustSize();

        oss.str("");
        oss << "price : " << m_amountItems * shopInventory[selectedItem].price << " G";
        moneyLabel->setCaption(oss.str());
        moneyLabel->adjustSize();
    }
}

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;
}