summaryrefslogtreecommitdiff
path: root/src/gui/skilldialog.cpp
blob: 8ed6b097dd4ec9e1feb3892857b1cf7e6c4c7244 (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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
 *  The Mana World
 *  Copyright (C) 2004  The Mana World Development Team
 *
 *  This file is part of The Mana World.
 *
 *  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, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "gui/skilldialog.h"

#include "gui/widgets/button.h"
#include "gui/widgets/container.h"
#include "gui/widgets/icon.h"
#include "gui/widgets/label.h"
#include "gui/widgets/layouthelper.h"
#include "gui/widgets/listbox.h"
#include "gui/widgets/progressbar.h"
#include "gui/widgets/scrollarea.h"
#include "gui/widgets/tab.h"
#include "gui/widgets/tabbedarea.h"
#include "gui/widgets/vertcontainer.h"
#include "gui/widgets/windowcontainer.h"

#include "localplayer.h"
#include "log.h"

#include "net/net.h"
#include "net/skillhandler.h"

#include "utils/dtor.h"
#include "utils/gettext.h"
#include "utils/stringutils.h"
#include "utils/xml.h"

#include <string>
#include <vector>

class SkillEntry;

struct SkillInfo
{
    unsigned short id;
    std::string name;
    std::string icon;
    bool modifiable;
    SkillEntry *display;
};

class SkillEntry : public Container, gcn::WidgetListener
{
    public:
        SkillEntry(struct SkillInfo *info);

        void widgetResized(const gcn::Event &event);

        void update();

    protected:
        friend class SkillDialog;
        struct SkillInfo *mInfo;

    private:
        Icon *mIcon;
        Label *mNameLabel;
        Label *mLevelLabel;
        Label *mExpLabel;
        Button *mIncrease;
        ProgressBar *mProgress;
};

SkillDialog::SkillDialog():
    Window(_("Skills"))
{
    setWindowName("Skills");
    setCloseButton(true);
    setResizable(true);
    setSaveVisible(true);
    setDefaultSize(windowContainer->getWidth() - 280, 30, 275, 425);

    mTabs = new TabbedArea();
    mPointsLabel = new Label("0");

    place(0, 0, mTabs, 5, 5);
    place(0, 5, mPointsLabel);

    setLocationRelativeTo(getParent());
    loadWindowState();
}

SkillDialog::~SkillDialog()
{
    //delete_all(mTabs);
}

void SkillDialog::action(const gcn::ActionEvent &event)
{
    if (event.getId() == "inc")
    {
        SkillEntry *disp = dynamic_cast<SkillEntry*>(event.getSource()->getParent());

        if (disp)
            Net::getSkillHandler()->up(disp->mInfo->id);
    }
    else if (event.getId() == "close")
    {
        setVisible(false);
    }
}

void SkillDialog::adjustTabSize()
{
    gcn::Widget *content = mTabs->getCurrentWidget();
    if (content) {
        int width = mTabs->getWidth() - 2 * content->getFrameSize() - 2 * mTabs->getFrameSize();
        int height = mTabs->getContainerHeight() - 2 * content->getFrameSize();
        content->setSize(width, height);
        content->setVisible(true);
        content->logic();
    }
}

void SkillDialog::widgetResized(const gcn::Event &event)
{
    Window::widgetResized(event);

    adjustTabSize();
}

void SkillDialog::logic()
{
    Window::logic();

    Tab *tab = dynamic_cast<Tab*>(mTabs->getSelectedTab());
    if (tab != mCurrentTab) {
        mCurrentTab = tab;
        adjustTabSize();
    }
}

std::string SkillDialog::update(int id)
{
    SkillInfo *info = mSkills[id];

    if (info)
    {
        info->display->update();
        return info->name;
    }
    else
        return "";
}

void SkillDialog::update()
{
    mPointsLabel->setCaption(strprintf(_("Skill points available: %d"),
                                       player_node->getSkillPoints()));
    mPointsLabel->adjustSize();

    for (SkillMap::iterator it = mSkills.begin(); it != mSkills.end(); it++)
    {
        if ((*it).second->modifiable)
            (*it).second->display->update();
    }
}

void SkillDialog::loadSkills(const std::string &file)
{
    // TODO: mTabs->clear();
    delete_all(mSkills);

    XML::Document doc(file);
    xmlNodePtr root = doc.rootNode();

    if (!root || !xmlStrEqual(root->name, BAD_CAST "skills"))
    {
        logger->log("Error loading skills file: %s", file.c_str());
        return;
    }

    int setCount = 0;
    std::string setName;
    ScrollArea *scroll;
    VertContainer *container;

    for_each_xml_child_node(set, root)
    {
        if (xmlStrEqual(set->name, BAD_CAST "set"))
        {
            setCount++;
            setName = XML::getProperty(set, "name", strprintf(_("Skill Set %d"), setCount));

            container = new VertContainer(32);
            container->setOpaque(false);
            scroll = new ScrollArea(container);
            scroll->setOpaque(false);
            scroll->setHorizontalScrollPolicy(ScrollArea::SHOW_NEVER);
            scroll->setVerticalScrollPolicy(ScrollArea::SHOW_ALWAYS);

            mTabs->addTab(setName, scroll);
            for_each_xml_child_node(node, set)
            {
                if (xmlStrEqual(node->name, BAD_CAST "skill"))
                {
                    int id = atoi(XML::getProperty(node, "id", "-1").c_str());
                    std::string name = XML::getProperty(node, "name", strprintf(_("Skill %d"), id));
                    std::string icon = XML::getProperty(node, "icon", "");

                    SkillInfo *skill = new SkillInfo;
                    skill->id = id;
                    skill->name = name;
                    skill->icon = icon;
                    skill->modifiable = 0;
                    skill->display = new SkillEntry(skill);

                    container->add(skill->display);

                    mSkills[id] = skill;
                }
            }
        }
    }

    adjustTabSize();
    update();
}

void SkillDialog::setModifiable(int id, bool modifiable)
{
    SkillInfo *info = mSkills[id];

    if (info)
    {
        info->modifiable = modifiable;
        info->display->update();
    }
}

SkillEntry::SkillEntry(struct SkillInfo *info) : mInfo(info),
    mIcon(NULL),
    mNameLabel(new Label(info->name)),
    mProgress(new ProgressBar(0.0f, 200, 20, gcn::Color(150, 150, 150))),
    mIncrease(new Button("+", "inc", skillDialog)),
    mLevelLabel(new Label("999"))
{
    setFrameSize(1);
    setOpaque(false);

    addWidgetListener(this);

    if (!info->icon.empty())
        mIcon = new Icon(info->icon);
    else
        mIcon = new Icon("graphics/gui/unknown-item.png");

    mIcon->setPosition(1, 0);
    add(mIcon);

    mNameLabel->setPosition(35, 0);
    add(mNameLabel);

    mLevelLabel->setPosition(165, 0);
    add(mLevelLabel);

    mProgress->setPosition(35, 13);
    add(mProgress);

    mIncrease->setPosition(getWidth() - mIncrease->getWidth(), 13);
    add(mIncrease);

    update();
}

void SkillEntry::widgetResized(const gcn::Event &event)
{
    gcn::Rectangle size = getChildrenArea();

    if (mProgress->isVisible() && mIncrease->isVisible())
    {
        mLevelLabel->setPosition(size.width - mLevelLabel->getWidth()
                                 - mIncrease->getWidth() - 4, 0);
        mProgress->setWidth(size.width - mIncrease->getWidth() - 39);
        mIncrease->setPosition(getWidth() - mIncrease->getWidth() - 2, 6);
    }
    else if (mProgress->isVisible())
    {
        mLevelLabel->setPosition(size.width - mLevelLabel->getWidth(), 0);
        mProgress->setWidth(size.width - 39);
    }
    else if (mIncrease->isVisible())
    {
        mLevelLabel->setPosition(size.width - mLevelLabel->getWidth()
                                 - mIncrease->getWidth() - 4, 0);
        mIncrease->setPosition(getWidth() - mIncrease->getWidth() - 2, 6);
    }
    else
        mLevelLabel->setPosition(size.width - mLevelLabel->getWidth(), 0);
}

void SkillEntry::update()
{
    int baseLevel = player_node->getAttributeBase(mInfo->id);

    int effLevel = player_node->getAttributeEffective(mInfo->id);

    if (baseLevel <= 0 && !mInfo->modifiable)
    {
        setVisible(false);
        return;
    }

    setVisible(true);

    std::string skillLevel("Lvl: " + toString(baseLevel));
    if (effLevel < baseLevel)
    {
        skillLevel.append(" - " + toString(baseLevel - effLevel));
    }
    else if (effLevel > baseLevel)
    {
        skillLevel.append(" + " + toString(effLevel - baseLevel));
    }
    mLevelLabel->setCaption(skillLevel);

    std::pair<int, int> exp = player_node->getExperience(mInfo->id);
    std::string sExp (toString(exp.first) + " / " + toString(exp.second));

    mLevelLabel->adjustSize();

    if (exp.second)
    {
        mProgress->setVisible(true);
        mProgress->setText(sExp);

        // More intense red as exp grows
        int color = 150 - (int)(150 * ((float) exp.first / exp.second));
        mProgress->setColor(244, color, color);
        mProgress->setProgress((float) exp.first / exp.second);
    }
    else
        mProgress->setVisible(false);

    if (mInfo->modifiable)
    {
        mIncrease->setVisible(true);
        mIncrease->setEnabled(player_node->getSkillPoints());
    }
    else
    {
        mIncrease->setVisible(false);
        mIncrease->setEnabled(false);
    }

    widgetResized(NULL);
}