summaryrefslogtreecommitdiff
path: root/src/gui
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2012-07-24 21:37:03 +0300
committerAndrei Karas <akaras@inbox.ru>2012-07-29 16:12:41 +0300
commit9234bf25b145995d5395f98a1f5dd030783d25d6 (patch)
tree3f679edc308488057dc5d044698c053de6b4e36c /src/gui
parent2f351b828f4155f93566d685eb99416e32cba7d0 (diff)
downloadmanaplus-9234bf25b145995d5395f98a1f5dd030783d25d6.tar.gz
manaplus-9234bf25b145995d5395f98a1f5dd030783d25d6.tar.bz2
manaplus-9234bf25b145995d5395f98a1f5dd030783d25d6.tar.xz
manaplus-9234bf25b145995d5395f98a1f5dd030783d25d6.zip
Add quests netcode and quests window.
Diffstat (limited to 'src/gui')
-rw-r--r--src/gui/questswindow.cpp254
-rw-r--r--src/gui/questswindow.h81
-rw-r--r--src/gui/setupactiondata.h5
-rw-r--r--src/gui/widgets/extendednamesmodel.cpp70
-rw-r--r--src/gui/widgets/extendednamesmodel.h59
-rw-r--r--src/gui/widgets/namesmodel.h3
6 files changed, 472 insertions, 0 deletions
diff --git a/src/gui/questswindow.cpp b/src/gui/questswindow.cpp
new file mode 100644
index 000000000..f97bac1b4
--- /dev/null
+++ b/src/gui/questswindow.cpp
@@ -0,0 +1,254 @@
+/*
+ * The ManaPlus Client
+ * Copyright (C) 2012 The ManaPlus developers
+ *
+ * This file is part of The ManaPlus Client.
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "gui/questswindow.h"
+
+#include "logger.h"
+
+#include "gui/gui.h"
+
+#include "gui/widgets/browserbox.h"
+#include "gui/widgets/button.h"
+#include "gui/widgets/layouthelper.h"
+#include "gui/widgets/extendedlistbox.h"
+#include "gui/widgets/extendednamesmodel.h"
+#include "gui/widgets/scrollarea.h"
+#include "gui/widgets/textfield.h"
+
+#include "utils/gettext.h"
+#include "utils/stringutils.h"
+
+#include "utils/translation/podict.h"
+
+#include <guichan/font.hpp>
+
+#include "debug.h"
+
+struct QuestItemText
+{
+ QuestItemText(std::string text0, int type0) :
+ text(text0), type(type0)
+ {
+ }
+
+ std::string text;
+ int type;
+};
+
+struct QuestItem
+{
+ int var;
+ std::string name;
+ std::string group;
+ std::set<int> incomplete;
+ std::set<int> complete;
+ std::vector<QuestItemText> texts;
+};
+
+class QuestsModel : public ExtendedNamesModel
+{
+ public:
+ QuestsModel()
+ {
+ }
+
+ virtual ~QuestsModel()
+ { }
+};
+
+QuestsWindow::QuestsWindow() :
+ Window(_("Quests Window"), false, nullptr, "quest.xml")
+{
+ setWindowName("Quests");
+ setResizable(true);
+ setCloseButton(true);
+ setStickyButtonLock(true);
+
+ setDefaultSize(400, 350, ImageRect::RIGHT);
+ setMinWidth(400);
+ setMinHeight(350);
+
+ mQuestsModel = new QuestsModel;
+ mQuestsListBox = new ExtendedListBox(mQuestsModel);
+ mQuestsListBox->setActionEventId("select");
+ mQuestsListBox->addActionListener(this);
+
+ mQuestScrollArea = new ScrollArea(mQuestsListBox);
+ mQuestScrollArea->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER);
+ mText = new BrowserBox;
+ mText->setOpaque(false);
+ mTextScrollArea = new ScrollArea(mText);
+ mTextScrollArea->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER);
+ mQuestsListBox->setWidth(500);
+
+ mCloseButton = new Button(_("Close"), "close", this);
+
+ ContainerPlacer placer;
+ placer = getPlacer(0, 0);
+
+ placer(0, 0, mQuestScrollArea, 4, 3).setPadding(3);
+ placer(4, 0, mTextScrollArea, 4, 3).setPadding(3);
+ placer(7, 3, mCloseButton);
+
+ loadWindowState();
+ loadXml();
+
+ Layout &layout = getLayout();
+ layout.setRowHeight(0, Layout::AUTO_SET);
+}
+
+void QuestsWindow::loadXml()
+{
+ XML::Document doc("quests.xml");
+ XmlNodePtr root = doc.rootNode();
+ if (!root)
+ return;
+
+ for_each_xml_child_node(varNode, root)
+ {
+ if (xmlNameEqual(varNode, "var"))
+ {
+ int id = XML::getProperty(varNode, "id", 0);
+ if (id < 0)
+ continue;
+ for_each_xml_child_node(questNode, varNode)
+ {
+ if (xmlNameEqual(questNode, "quest"))
+ loadQuest(id, questNode);
+ }
+ }
+ }
+}
+
+void QuestsWindow::loadQuest(int var, XmlNodePtr node)
+{
+ QuestItem *quest = new QuestItem();
+ quest->name = XML::langProperty(node, "name", _("unknown"));
+ quest->group = XML::getProperty(node, "group", "");
+ std::string incompleteStr = XML::getProperty(node, "incomplete", "");
+ std::string completeStr = XML::getProperty(node, "complete", "");
+ if (incompleteStr.empty() || completeStr.empty())
+ {
+ logger->log("complete flags incorrect");
+ delete quest;
+ return;
+ }
+ quest->incomplete = splitToIntSet(incompleteStr, ',');
+ quest->complete = splitToIntSet(completeStr, ',');
+ if (quest->incomplete.size() + quest->complete.size() == 0)
+ {
+ logger->log("complete flags incorrect");
+ delete quest;
+ return;
+ }
+ for_each_xml_child_node(dataNode, node)
+ {
+ if (!xmlTypeEqual(dataNode, XML_ELEMENT_NODE))
+ continue;
+ const char *data = reinterpret_cast<const char*>(
+ xmlNodeGetContent(dataNode));
+ if (!data)
+ continue;
+ std::string str = translator->getStr(data);
+
+ if (xmlNameEqual(dataNode, "text"))
+ quest->texts.push_back(QuestItemText(str, 0));
+ else if (xmlNameEqual(dataNode, "name"))
+ quest->texts.push_back(QuestItemText(str, 1));
+ }
+ mQuests[var].push_back(quest);
+}
+
+void QuestsWindow::action(const gcn::ActionEvent &event)
+{
+ const std::string &eventId = event.getId();
+ if (eventId == "select")
+ {
+ int id = mQuestsListBox->getSelected();
+ if (id < 0)
+ return;
+ showQuest(mQuestLinks[id]);
+ }
+ else if (eventId == "close")
+ {
+ setVisible(false);
+ }
+}
+
+void QuestsWindow::updateQuest(int var, int val)
+{
+ mVars[var] = val;
+}
+
+void QuestsWindow::rebuild()
+{
+ mQuestsModel->clear();
+ StringVect &names = mQuestsModel->getNames();
+
+ for (std::map<int, int>::const_iterator it = mVars.begin(),
+ it_end = mVars.end(); it != it_end; ++ it)
+ {
+ int var = (*it).first;
+ int val = (*it).second;
+ const std::vector<QuestItem*> &quests = mQuests[var];
+ for (std::vector<QuestItem*>::const_iterator it2 = quests.begin(),
+ it2_end = quests.end(); it2 != it2_end; ++ it2)
+ {
+ if (!*it2)
+ continue;
+ QuestItem *quest = *it2;
+ if (quest->complete.find(val) != quest->complete.end())
+ {
+ names.push_back(quest->name + _("[complete]"));
+ mQuestLinks.push_back(quest);
+ }
+ else if (quest->incomplete.find(val) != quest->incomplete.end())
+ {
+ names.push_back(quest->name);
+ mQuestLinks.push_back(quest);
+ }
+ }
+ }
+// mQuestsListBox->adjustSize();
+}
+
+void QuestsWindow::showQuest(QuestItem *quest)
+{
+ if (!quest)
+ return;
+
+ const std::vector<QuestItemText> &texts = quest->texts;
+ mText->clearRows();
+ for (std::vector<QuestItemText>::const_iterator it = texts.begin(),
+ it_end = texts.end(); it != it_end; ++ it)
+ {
+ const QuestItemText &data = *it;
+ switch (data.type)
+ {
+ case 0:
+ default:
+ mText->addRow(translator->getStr(data.text));
+ break;
+ case 1:
+ mText->addRow("[" + translator->getStr(data.text) + "]");
+ break;
+ }
+ }
+}
diff --git a/src/gui/questswindow.h b/src/gui/questswindow.h
new file mode 100644
index 000000000..c35e395bd
--- /dev/null
+++ b/src/gui/questswindow.h
@@ -0,0 +1,81 @@
+/*
+ * The ManaPlus Client
+ * Copyright (C) 2012 The ManaPlus developers
+ *
+ * This file is part of The ManaPlus Client.
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef QUESTS_WINDOW_H
+#define QUESTS_WINDOW_H
+
+#include "localconsts.h"
+
+#include "gui/widgets/window.h"
+
+#include "utils/xml.h"
+
+#include <guichan/actionlistener.hpp>
+
+#include <map>
+
+class Button;
+class BrowserBox;
+class ExtendedListBox;
+class ScrollArea;
+class QuestsModel;
+
+struct QuestItem;
+
+class QuestsWindow : public Window, public gcn::ActionListener
+{
+ public:
+ /**
+ * Constructor.
+ *
+ * @see Window::Window
+ */
+ QuestsWindow();
+
+ /**
+ * Called when receiving actions from the widgets.
+ */
+ void action(const gcn::ActionEvent &event);
+
+ void updateQuest(int var, int val);
+
+ void rebuild();
+
+ void showQuest(QuestItem *quest);
+
+ private:
+ void loadXml();
+
+ void loadQuest(int var, XmlNodePtr node);
+
+ QuestsModel *mQuestsModel;
+ ExtendedListBox *mQuestsListBox;
+ ScrollArea *mQuestScrollArea;
+ BrowserBox *mText;
+ ScrollArea *mTextScrollArea;
+ Button *mCloseButton;
+ std::map<int, int> mVars;
+ std::map<int, std::vector<QuestItem*>> mQuests;
+ std::vector<QuestItem*> mQuestLinks;
+};
+
+extern QuestsWindow *questsWindow;
+
+#endif
diff --git a/src/gui/setupactiondata.h b/src/gui/setupactiondata.h
index 9c516c83f..ee85f6ca5 100644
--- a/src/gui/setupactiondata.h
+++ b/src/gui/setupactiondata.h
@@ -403,6 +403,11 @@ static SetupActionData setupActionData2[] =
"",
},
{
+ N_("Quests Window"),
+ Input::KEY_WINDOW_QUESTS,
+ "",
+ },
+ {
N_("Previous Social Tab"),
Input::KEY_PREV_SOCIAL_TAB,
"",
diff --git a/src/gui/widgets/extendednamesmodel.cpp b/src/gui/widgets/extendednamesmodel.cpp
new file mode 100644
index 000000000..1cc6561dc
--- /dev/null
+++ b/src/gui/widgets/extendednamesmodel.cpp
@@ -0,0 +1,70 @@
+/*
+ * The ManaPlus Client
+ * Copyright (C) 2012 The ManaPlus Developers
+ *
+ * This file is part of The ManaPlus Client.
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "gui/widgets/extendednamesmodel.h"
+
+#include "logger.h"
+
+#include "utils/gettext.h"
+
+#include <guichan/exception.hpp>
+#include <guichan/font.hpp>
+
+#include "debug.h"
+
+ExtendedNamesModel::ExtendedNamesModel()
+{
+}
+
+ExtendedNamesModel::~ExtendedNamesModel()
+{
+ clear();
+}
+
+int ExtendedNamesModel::getNumberOfElements()
+{
+ return static_cast<int>(mNames.size());
+}
+
+std::string ExtendedNamesModel::getElementAt(int i)
+{
+ if (i >= getNumberOfElements() || i < 0)
+ return _("???");
+
+ return mNames[i];
+}
+
+Image *ExtendedNamesModel::getImageAt(int i)
+{
+ if (i >= static_cast<int>(mImages.size()) || i < 0)
+ return nullptr;
+
+ return mImages[i];
+}
+
+void ExtendedNamesModel::clear()
+{
+ mNames.clear();
+ for (std::vector<Image*>::iterator it = mImages.begin(),
+ it_end = mImages.end(); it != it_end; ++ it)
+ {
+ (*it)->decRef();
+ }
+}
diff --git a/src/gui/widgets/extendednamesmodel.h b/src/gui/widgets/extendednamesmodel.h
new file mode 100644
index 000000000..5a15949a9
--- /dev/null
+++ b/src/gui/widgets/extendednamesmodel.h
@@ -0,0 +1,59 @@
+/*
+ * The ManaPlus Client
+ * Copyright (C) 2012 The ManaPlus Developers
+ *
+ * This file is part of The ManaPlus Client.
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GUI_WIDGETS_EXTENDEDNAMESMODEL_H
+#define GUI_WIDGETS_EXTENDEDNAMESMODEL_H
+
+#include "utils/stringvector.h"
+
+#include "gui/widgets/extendedlistmodel.h"
+
+#include "resources/image.h"
+
+class ExtendedNamesModel : public ExtendedListModel
+{
+ public:
+ ExtendedNamesModel();
+
+ virtual ~ExtendedNamesModel();
+
+ virtual int getNumberOfElements();
+
+ virtual std::string getElementAt(int i);
+
+ virtual Image *getImageAt(int i);
+
+ StringVect &getNames()
+ { return mNames; }
+
+ std::vector<Image*> &getImages()
+ { return mImages; }
+
+ size_t size()
+ { return mNames.size(); }
+
+ void clear();
+
+ protected:
+ StringVect mNames;
+ std::vector<Image*> mImages;
+};
+
+#endif
diff --git a/src/gui/widgets/namesmodel.h b/src/gui/widgets/namesmodel.h
index e1dbf0a89..1064a8875 100644
--- a/src/gui/widgets/namesmodel.h
+++ b/src/gui/widgets/namesmodel.h
@@ -42,6 +42,9 @@ class NamesModel : public gcn::ListModel
size_t size()
{ return mNames.size(); }
+ void clear()
+ { mNames.clear(); }
+
protected:
StringVect mNames;
};