diff options
author | Andrei Karas <akaras@inbox.ru> | 2012-07-27 02:32:32 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2012-07-28 00:47:21 +0300 |
commit | c9bb66d595927bd14871c10b216ed05ffb5e3b12 (patch) | |
tree | aef6dc1911678dd813c64b3397ac13bbad7de622 /src | |
parent | 54f3d4ce98b81a744ef6c0068100f4eb36a6528b (diff) | |
download | plus-c9bb66d595927bd14871c10b216ed05ffb5e3b12.tar.gz plus-c9bb66d595927bd14871c10b216ed05ffb5e3b12.tar.bz2 plus-c9bb66d595927bd14871c10b216ed05ffb5e3b12.tar.xz plus-c9bb66d595927bd14871c10b216ed05ffb5e3b12.zip |
Add support for images in npc menu.
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/Makefile.am | 3 | ||||
-rw-r--r-- | src/defaults.cpp | 1 | ||||
-rw-r--r-- | src/gui/npcdialog.cpp | 50 | ||||
-rw-r--r-- | src/gui/npcdialog.h | 13 | ||||
-rw-r--r-- | src/gui/widgets/extendedlistbox.cpp | 101 | ||||
-rw-r--r-- | src/gui/widgets/extendedlistbox.h | 50 | ||||
-rw-r--r-- | src/gui/widgets/extendedlistmodel.h | 34 |
8 files changed, 248 insertions, 7 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 26a571c54..3c55c9632 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -127,6 +127,9 @@ SET(SRCS gui/widgets/dropdown.h gui/widgets/emoteshortcutcontainer.cpp gui/widgets/emoteshortcutcontainer.h + gui/widgets/extendedlistbox.cpp + gui/widgets/extendedlistbox.h + gui/widgets/extendedlistmodel.h gui/widgets/flowcontainer.cpp gui/widgets/flowcontainer.h gui/widgets/guildchattab.cpp diff --git a/src/Makefile.am b/src/Makefile.am index 2eac1be38..bfb0718cb 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -136,6 +136,9 @@ manaplus_SOURCES += gui/widgets/avatarlistbox.cpp \ gui/widgets/dropdown.h \ gui/widgets/emoteshortcutcontainer.cpp \ gui/widgets/emoteshortcutcontainer.h \ + gui/widgets/extendedlistbox.cpp \ + gui/widgets/extendedlistbox.h \ + gui/widgets/extendedlistmodel.h \ gui/widgets/flowcontainer.cpp \ gui/widgets/flowcontainer.h \ gui/widgets/guildchattab.cpp \ diff --git a/src/defaults.cpp b/src/defaults.cpp index 2b53dfb5a..09e65f530 100644 --- a/src/defaults.cpp +++ b/src/defaults.cpp @@ -310,6 +310,7 @@ DefaultsData* getPathsDefaults() AddDEF(pathsData, "unknownItemFile", "unknown-item.png"); AddDEF(pathsData, "sprites", "graphics/sprites/"); AddDEF(pathsData, "spriteErrorFile", "error.xml"); + AddDEF(pathsData, "guiIcons", "graphics/guiicons/"); AddDEF(pathsData, "particles", "graphics/particles/"); AddDEF(pathsData, "levelUpEffectFile", "levelup.particle.xml"); diff --git a/src/gui/npcdialog.cpp b/src/gui/npcdialog.cpp index aa86ffeec..04e0aa9fb 100644 --- a/src/gui/npcdialog.cpp +++ b/src/gui/npcdialog.cpp @@ -37,12 +37,14 @@ #include "gui/widgets/inttextfield.h" #include "gui/widgets/itemlinkhandler.h" #include "gui/widgets/layout.h" -#include "gui/widgets/listbox.h" +#include "gui/widgets/extendedlistbox.h" #include "gui/widgets/playerbox.h" #include "gui/widgets/scrollarea.h" #include "gui/widgets/textbox.h" #include "gui/widgets/textfield.h" +#include "resources/resourcemanager.h" + #include "net/net.h" #include "net/npchandler.h" @@ -103,12 +105,16 @@ NpcDialog::NpcDialog(int npcId) : mScrollArea->setVerticalScrollPolicy(gcn::ScrollArea::SHOW_ALWAYS); // Setup listbox - mItemList = new ListBox(this); + mItemList = new ExtendedListBox(this); mItemList->setWrappingEnabled(true); mItemList->setActionEventId("ok"); mItemList->addActionListener(this); mItemList->setDistributeMousePressed(false); mItemList->setFont(gui->getNpcFont()); + if (gui->getNpcFont()->getHeight() < 20) + mItemList->setRowHeight(20); + else + mItemList->setRowHeight(gui->getNpcFont()->getHeight()); setContentSize(260, 175); @@ -200,6 +206,15 @@ NpcDialog::~NpcDialog() delete mListScrollArea; mListScrollArea = nullptr; + for (std::vector<Image *>::iterator it = mImages.begin(), + it_end = mImages.end(); it != it_end; ++ it) + { + if (*it) + (*it)->decRef(); + } + + mImages.clear(); + instances.remove(this); } @@ -242,7 +257,6 @@ void NpcDialog::showCloseButton() void NpcDialog::action(const gcn::ActionEvent &event) { - logger->log(event.getId()); if (event.getId() == "ok") { if (mActionState == NPC_ACTION_NEXT) @@ -355,9 +369,21 @@ std::string NpcDialog::getElementAt(int i) return mItems[i]; } +const Image *NpcDialog::getImageAt(int i) +{ + return mImages[i]; +} + void NpcDialog::choiceRequest() { mItems.clear(); + for (std::vector<Image *>::iterator it = mImages.begin(), + it_end = mImages.end(); it != it_end; ++ it) + { + if (*it) + (*it)->decRef(); + } + mImages.clear(); mActionState = NPC_ACTION_INPUT; mInputState = NPC_INPUT_LIST; buildLayout(); @@ -366,15 +392,31 @@ void NpcDialog::choiceRequest() void NpcDialog::addChoice(const std::string &choice) { mItems.push_back(choice); + mImages.push_back(nullptr); } void NpcDialog::parseListItems(const std::string &itemString) { std::istringstream iss(itemString); + ResourceManager *resman = ResourceManager::getInstance(); std::string tmp; while (getline(iss, tmp, ':')) - mItems.push_back(tmp); + { + const size_t pos = tmp.find("|"); + if (pos == std::string::npos) + { + mItems.push_back(tmp); + mImages.push_back(nullptr); + } + else + { + mItems.push_back(tmp.substr(pos + 1)); + Image *img = resman->getImage(paths.getStringValue("guiIcons") + + tmp.substr(0, pos) + ".png"); + mImages.push_back(img); + } + } if (!mItems.empty()) { diff --git a/src/gui/npcdialog.h b/src/gui/npcdialog.h index 75c55ae96..e3cc274c8 100644 --- a/src/gui/npcdialog.h +++ b/src/gui/npcdialog.h @@ -25,6 +25,7 @@ #include "configlistener.h" +#include "gui/widgets/extendedlistmodel.h" #include "gui/widgets/window.h" #include "utils/stringvector.h" @@ -38,7 +39,7 @@ class Being; class BrowserBox; class ItemLinkHandler; class IntTextField; -class ListBox; +class ExtendedListBox; class PlayerBox; class TextBox; class TextField; @@ -55,7 +56,7 @@ namespace gcn * \ingroup Interface */ class NpcDialog : public Window, public gcn::ActionListener, - public gcn::ListModel, public ConfigListener + public ExtendedListModel, public ConfigListener { public: /** @@ -119,6 +120,11 @@ class NpcDialog : public Window, public gcn::ActionListener, std::string getElementAt(int i); /** + * Returns the image of item number i of the choices list. + */ + const Image *getImageAt(int i); + + /** * Makes this dialog request a choice selection from the user. */ void choiceRequest(); @@ -222,9 +228,10 @@ class NpcDialog : public Window, public gcn::ActionListener, std::string mNewText; // Used for choice input - ListBox *mItemList; + ExtendedListBox *mItemList; gcn::ScrollArea *mListScrollArea; StringVect mItems; + std::vector<Image *> mImages; ItemLinkHandler *mItemLinkHandler; // Used for string and integer input diff --git a/src/gui/widgets/extendedlistbox.cpp b/src/gui/widgets/extendedlistbox.cpp new file mode 100644 index 000000000..61f516d21 --- /dev/null +++ b/src/gui/widgets/extendedlistbox.cpp @@ -0,0 +1,101 @@ +/* + * 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/extendedlistbox.h" + +#include "client.h" +#include "configuration.h" +#include "keyevent.h" +#include "keydata.h" + +#include "gui/palette.h" +#include "gui/sdlinput.h" +#include "gui/theme.h" + +#include "gui/widgets/extendedlistmodel.h" + +#include "resources/image.h" + +#include <guichan/focushandler.hpp> +#include <guichan/font.hpp> +#include <guichan/graphics.hpp> +#include <guichan/key.hpp> +#include <guichan/listmodel.hpp> + +#include "debug.h" + +ExtendedListBox::ExtendedListBox(gcn::ListModel *listModel): + ListBox(listModel), + mRowHeight(13) +{ +} + +ExtendedListBox::~ExtendedListBox() +{ +} + +void ExtendedListBox::draw(gcn::Graphics *graphics) +{ + if (!mListModel) + return; + + ExtendedListModel *model = static_cast<ExtendedListModel*>(mListModel); + Graphics *g = static_cast<Graphics*>(graphics); + + updateAlpha(); + graphics->setFont(getFont()); + + const int height = getRowHeight(); + int textPos = (height - getFont()->getHeight()) / 2; + if (textPos < 0) + textPos = 0; + + // Draw filled rectangle around the selected list element + if (mSelected >= 0) + { + mHighlightColor.a = static_cast<int>(mAlpha * 255.0f); + graphics->setColor(mHighlightColor); + graphics->fillRectangle(gcn::Rectangle(0, height * mSelected, + getWidth(), height)); + } + + // Draw the list elements + graphics->setColor(getForegroundColor()); + for (int i = 0, y = 0; i < mListModel->getNumberOfElements(); + ++i, y += height) + { + const Image *image = model->getImageAt(i); + if (!image) + { + graphics->drawText(mListModel->getElementAt(i), 1, y + textPos); + } + else + { + g->drawImage(image, 1, y + (height - image->getHeight()) / 2); + graphics->drawText(mListModel->getElementAt(i), + image->getWidth() + 2, y + textPos); + } + } +} + +unsigned int ExtendedListBox::getRowHeight() const +{ + return mRowHeight; +} diff --git a/src/gui/widgets/extendedlistbox.h b/src/gui/widgets/extendedlistbox.h new file mode 100644 index 000000000..c6f3278fc --- /dev/null +++ b/src/gui/widgets/extendedlistbox.h @@ -0,0 +1,50 @@ +/* + * 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 EXTENDEDLISTBOX_H +#define EXTENDEDLISTBOX_H + +#include "gui/widgets/listbox.h" + +class ExtendedListBox : public ListBox +{ + public: + /** + * Constructor. + */ + ExtendedListBox(gcn::ListModel *listModel); + + ~ExtendedListBox(); + + /** + * Draws the list box. + */ + void draw(gcn::Graphics *graphics); + + unsigned int getRowHeight() const; + + void setRowHeight(unsigned int n) + { mRowHeight = n; } + + protected: + unsigned int mRowHeight; +}; + +#endif diff --git a/src/gui/widgets/extendedlistmodel.h b/src/gui/widgets/extendedlistmodel.h new file mode 100644 index 000000000..48b75dc95 --- /dev/null +++ b/src/gui/widgets/extendedlistmodel.h @@ -0,0 +1,34 @@ +/* + * 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 EXTENDEDLISTMODEL_H +#define EXTENDEDLISTMODEL_H + +#include "resources/image.h" + +#include <guichan/listmodel.hpp> + +class ExtendedListModel : public gcn::ListModel +{ + public: + virtual const Image *getImageAt(int i) = 0; +}; + +#endif |