From c9bb66d595927bd14871c10b216ed05ffb5e3b12 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Fri, 27 Jul 2012 02:32:32 +0300 Subject: Add support for images in npc menu. --- src/CMakeLists.txt | 3 ++ src/Makefile.am | 3 ++ src/defaults.cpp | 1 + src/gui/npcdialog.cpp | 50 ++++++++++++++++-- src/gui/npcdialog.h | 13 +++-- src/gui/widgets/extendedlistbox.cpp | 101 ++++++++++++++++++++++++++++++++++++ src/gui/widgets/extendedlistbox.h | 50 ++++++++++++++++++ src/gui/widgets/extendedlistmodel.h | 34 ++++++++++++ 8 files changed, 248 insertions(+), 7 deletions(-) create mode 100644 src/gui/widgets/extendedlistbox.cpp create mode 100644 src/gui/widgets/extendedlistbox.h create mode 100644 src/gui/widgets/extendedlistmodel.h (limited to 'src') 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::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::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: /** @@ -118,6 +119,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. */ @@ -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 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 . + */ + +#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 +#include +#include +#include +#include + +#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(mListModel); + Graphics *g = static_cast(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(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 . + */ + +#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 . + */ + +#ifndef EXTENDEDLISTMODEL_H +#define EXTENDEDLISTMODEL_H + +#include "resources/image.h" + +#include + +class ExtendedListModel : public gcn::ListModel +{ + public: + virtual const Image *getImageAt(int i) = 0; +}; + +#endif -- cgit v1.2.3-70-g09d2