diff options
author | Jared Adams <jaxad0127@gmail.com> | 2009-07-08 17:32:26 -0600 |
---|---|---|
committer | Jared Adams <jaxad0127@gmail.com> | 2009-07-08 17:37:52 -0600 |
commit | 6c68287a7a66ba6933bc4dbfe43f6c4761e683c1 (patch) | |
tree | 203960dfdebe15cffff9abd7089441ecbf1965cf /src/gui | |
parent | 8963ef3e6d0ede6a3e22687642bbc39c24f21dad (diff) | |
download | mana-client-6c68287a7a66ba6933bc4dbfe43f6c4761e683c1.tar.gz mana-client-6c68287a7a66ba6933bc4dbfe43f6c4761e683c1.tar.bz2 mana-client-6c68287a7a66ba6933bc4dbfe43f6c4761e683c1.tar.xz mana-client-6c68287a7a66ba6933bc4dbfe43f6c4761e683c1.zip |
Have both builds use the same SkillDialog
Diffstat (limited to 'src/gui')
-rw-r--r-- | src/gui/skill.cpp | 300 | ||||
-rw-r--r-- | src/gui/skill.h | 85 | ||||
-rw-r--r-- | src/gui/skilldialog.cpp | 54 | ||||
-rw-r--r-- | src/gui/skilldialog.h | 12 |
4 files changed, 61 insertions, 390 deletions
diff --git a/src/gui/skill.cpp b/src/gui/skill.cpp deleted file mode 100644 index 7698098c..00000000 --- a/src/gui/skill.cpp +++ /dev/null @@ -1,300 +0,0 @@ -/* - * 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/skill.h" - -#include "gui/table.h" - -#include "gui/widgets/button.h" -#include "gui/widgets/label.h" -#include "gui/widgets/layout.h" -#include "gui/widgets/listbox.h" -#include "gui/widgets/scrollarea.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" - -static const char *SKILLS_FILE = "skills.xml"; - -struct SkillInfo -{ - std::string name; - bool modifiable; -}; - -static const SkillInfo fakeSkillInfo = { - _("Mystery Skill"), - false -}; - -std::vector<SkillInfo> skill_db; - -static void initSkillinfo(); - -class SkillGuiTableModel : public StaticTableModel -{ -public: - SkillGuiTableModel(SkillDialog *dialog) : - StaticTableModel(0, 3) - { - mEntriesNr = 0; - mDialog = dialog; - update(); - } - - virtual int getRows() const - { - return mEntriesNr; - } - - virtual int getColumnWidth(int index) const - { - if (index == 0) - return 160; - - return 35; - } - - virtual int getRowHeight() const - { - return 12; - } - - virtual void update() - { - mEntriesNr = mDialog->getSkills().size(); - resize(); - - for (int i = 0; i < mEntriesNr; i++) - { - SKILL *skill = mDialog->getSkills()[i]; - SkillInfo const *info; - char tmp[128]; - - if (skill->id >= 0 - && (unsigned int) skill->id < skill_db.size()) - info = &skill_db[skill->id]; - else - info = &fakeSkillInfo; - - sprintf(tmp, "%c%s", info->modifiable? ' ' : '*', info->name.c_str()); - gcn::Label *name_label = new Label(tmp); - - sprintf(tmp, "Lv:%i", skill->lv); - gcn::Label *lv_label = new Label(tmp); - - sprintf(tmp, "Sp:%i", skill->sp); - gcn::Label *sp_label = new Label(tmp); - - set(i, 0, name_label); - set(i, 1, lv_label); - set(i, 2, sp_label); - } - } - -private: - SkillDialog *mDialog; - int mEntriesNr; -}; - - -SkillDialog::SkillDialog(): - Window(_("Skills")) -{ - initSkillinfo(); - mTableModel = new SkillGuiTableModel(this); - mTable = new GuiTable(mTableModel); - mTable->setOpaque(false); - mTable->setLinewiseSelection(true); - mTable->setWrappingEnabled(true); - mTable->setActionEventId("skill"); - mTable->addActionListener(this); - - setWindowName("Skills"); - setCloseButton(true); - setDefaultSize(255, 260, ImageRect::CENTER); - - setMinHeight(50 + mTableModel->getHeight()); - setMinWidth(200); - - ScrollArea *skillScrollArea = new ScrollArea(mTable); - mPointsLabel = new Label(strprintf(_("Skill points: %d"), 0)); - mIncButton = new Button(_("Up"), "inc", this); - mUseButton = new Button(_("Use"), "use", this); - mUseButton->setEnabled(false); - - skillScrollArea->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER); - - place(0, 0, skillScrollArea, 5).setPadding(3); - place(0, 1, mPointsLabel, 4); - place(3, 2, mIncButton); - place(4, 2, mUseButton); - - Layout &layout = getLayout(); - layout.setRowHeight(0, Layout::AUTO_SET); - - center(); - loadWindowState(); -} - -SkillDialog::~SkillDialog() -{ - delete_all(mSkillList); -} - -void SkillDialog::action(const gcn::ActionEvent &event) -{ - if (event.getId() == "inc") - { - // Increment skill - int selectedSkill = mTable->getSelectedRow(); - if (selectedSkill >= 0) - Net::getSkillHandler()->up(mSkillList[selectedSkill]->id); - } - else if (event.getId() == "skill" && mTable->getSelectedRow() > -1) - { - SKILL *skill = mSkillList[mTable->getSelectedRow()]; - SkillInfo const *info; - - if (skill->id >= 0 && (unsigned int) skill->id < skill_db.size()) - info = &skill_db[skill->id]; - else - info = &fakeSkillInfo; - - mIncButton->setEnabled(player_node->mSkillPoint > 0 && - info->modifiable); - } - else if (event.getId() == "close") - setVisible(false); -} - -void SkillDialog::update() -{ - mPointsLabel->setCaption(strprintf(_("Skill points: %d"), - player_node->mSkillPoint)); - - int selectedSkill = mTable->getSelectedRow(); - - if (selectedSkill >= 0) - { - int skillId = mSkillList[selectedSkill]->id; - bool modifiable; - - if (skillId >= 0 && (unsigned int) skillId < skill_db.size()) - modifiable = skill_db[skillId].modifiable; - else - modifiable = false; - - mIncButton->setEnabled(modifiable - && player_node->mSkillPoint > 0); - } - else - mIncButton->setEnabled(false); - - mTableModel->update(); - setMinHeight(50 + mTableModel->getHeight()); -} - -int SkillDialog::getNumberOfElements() -{ - return mSkillList.size(); -} - -bool SkillDialog::hasSkill(int id) -{ - for (unsigned int i = 0; i < mSkillList.size(); i++) - { - if (mSkillList[i]->id == id) - return true; - } - return false; -} - -void SkillDialog::addSkill(int id, int lvl, int mp) -{ - SKILL *tmp = new SKILL; - tmp->id = id; - tmp->lv = lvl; - tmp->sp = mp; - mSkillList.push_back(tmp); -} - -void SkillDialog::setSkill(int id, int lvl, int mp) -{ - for (unsigned int i = 0; i < mSkillList.size(); i++) - { - if (mSkillList[i]->id == id) - { - mSkillList[i]->lv = lvl; - mSkillList[i]->sp = mp; - } - } -} - -void SkillDialog::cleanList() -{ - delete_all(mSkillList); - mSkillList.clear(); -} - -static void initSkillinfo() -{ - SkillInfo emptySkillInfo = { "", false }; - - XML::Document doc(SKILLS_FILE); - xmlNodePtr root = doc.rootNode(); - - if (!root || !xmlStrEqual(root->name, BAD_CAST "skills")) - { - logger->log("Error loading skills file: %s", SKILLS_FILE); - skill_db.resize(2, emptySkillInfo); - skill_db[1].name = "Basic"; - skill_db[1].modifiable = true; - return; - } - - for_each_xml_child_node(node, root) - { - if (xmlStrEqual(node->name, BAD_CAST "skill")) - { - int index = atoi(XML::getProperty(node, "id", "-1").c_str()); - std::string name = XML::getProperty(node, "name", ""); - bool modifiable = !atoi(XML::getProperty(node, "fixed", "0").c_str()); - - if (index >= 0) - { - skill_db.resize(index + 1, emptySkillInfo); - skill_db[index].name = name; - skill_db[index].modifiable = modifiable; - } - } - } -} - diff --git a/src/gui/skill.h b/src/gui/skill.h deleted file mode 100644 index 0879f7e1..00000000 --- a/src/gui/skill.h +++ /dev/null @@ -1,85 +0,0 @@ -/* - * 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 - */ - -#ifndef SKILL_H -#define SKILL_H - -#include "gui/widgets/window.h" - -#include <guichan/actionlistener.hpp> - -#include <vector> - -struct SKILL -{ - short id; /**< Index into "skill_db" array */ - short lv, sp; -}; - -class GuiTable; -class ScrollArea; -class SkillGuiTableModel; - -/** - * The skill dialog. - * - * \ingroup Interface - */ -class SkillDialog : public Window, public gcn::ActionListener -{ - public: - /** - * Constructor. - */ - SkillDialog(); - - /** - * Destructor. - */ - ~SkillDialog(); - - void action(const gcn::ActionEvent &event); - - void update(); - - int getNumberOfElements(); - - bool hasSkill(int id); - void addSkill(int id, int lv, int sp); - void setSkill(int id, int lv, int sp); - void cleanList(); - - const std::vector<SKILL*>& getSkills() const { return mSkillList; } - - private: - GuiTable *mTable; - ScrollArea *skillScrollArea; - SkillGuiTableModel *mTableModel; - gcn::Label *mPointsLabel; - gcn::Button *mIncButton; - gcn::Button *mUseButton; - - std::vector<SKILL*> mSkillList; -}; - -extern SkillDialog *skillDialog; - -#endif diff --git a/src/gui/skilldialog.cpp b/src/gui/skilldialog.cpp index d9e2c5bb..3f5c8ca0 100644 --- a/src/gui/skilldialog.cpp +++ b/src/gui/skilldialog.cpp @@ -37,6 +37,9 @@ #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" @@ -65,12 +68,16 @@ class SkillEntry : public Container, gcn::WidgetListener void update(); - private: + protected: + friend class SkillDialog; struct SkillInfo *mInfo; + + private: Icon *mIcon; Label *mNameLabel; Label *mLevelLabel; Label *mExpLabel; + Button *mIncrease; ProgressBar *mProgress; }; @@ -84,9 +91,10 @@ SkillDialog::SkillDialog(): setDefaultSize(windowContainer->getWidth() - 280, 30, 275, 425); mTabs = new TabbedArea(); + mPointsLabel = new Label("0"); place(0, 0, mTabs, 5, 5); - place(0, 5, new Label("TODO")); + place(0, 5, mPointsLabel); setLocationRelativeTo(getParent()); loadWindowState(); @@ -99,8 +107,12 @@ SkillDialog::~SkillDialog() void SkillDialog::action(const gcn::ActionEvent &event) { - if (event.getId() == "skill") + 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") { @@ -151,6 +163,19 @@ std::string SkillDialog::update(int id) 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, bool fixed) { // TODO: mTabs->clear(); @@ -211,12 +236,25 @@ void SkillDialog::loadSkills(const std::string &file, bool fixed) } 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); @@ -242,6 +280,9 @@ SkillEntry::SkillEntry(struct SkillInfo *info) : mInfo(info), mProgress->setPosition(35, 13); add(mProgress); + mIncrease->setPosition(getWidth() - mIncrease->getWidth(), 13); + add(mIncrease); + update(); } @@ -250,7 +291,8 @@ void SkillEntry::widgetResized(const gcn::Event &event) gcn::Rectangle size = getChildrenArea(); mLevelLabel->setPosition(size.width - mLevelLabel->getWidth(), 0); - mProgress->setWidth(size.width - 35); + mProgress->setWidth(size.width - mIncrease->getWidth() - 39); + mIncrease->setPosition(getWidth() - mIncrease->getWidth() - 2, 13); } void SkillEntry::update() @@ -288,4 +330,8 @@ void SkillEntry::update() int color = 150 - (int)(150 * ((float) exp.first / exp.second)); mProgress->setColor(244, color, color); mProgress->setProgress((float) exp.first / exp.second); + + mIncrease->setEnabled(mInfo->modifiable && player_node->getSkillPoints()); + + widgetResized(NULL); } diff --git a/src/gui/skilldialog.h b/src/gui/skilldialog.h index fbd3c26d..4a3f4f56 100644 --- a/src/gui/skilldialog.h +++ b/src/gui/skilldialog.h @@ -31,6 +31,7 @@ #include <list> #include <map> +class Label; class ScrollArea; class Tab; class TabbedArea; @@ -67,14 +68,23 @@ class SkillDialog : public Window, public gcn::ActionListener */ std::string update(int id); + /** + * Update other parts of the display + */ + void update(); + void loadSkills(const std::string &file, bool fixed = true); + void setModifiable(int id, bool modifiable); + private: void adjustTabSize(); + typedef std::map<int, SkillInfo*> SkillMap; + SkillMap mSkills; Tab *mCurrentTab; TabbedArea *mTabs; - std::map<int, SkillInfo*> mSkills; + Label *mPointsLabel; }; extern SkillDialog *skillDialog; |