Mana
Loading...
Searching...
No Matches
shoplistbox.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
23
24#include "configuration.h"
25#include "graphics.h"
26#include "shopitem.h"
27
28#include "gui/itempopup.h"
29#include "gui/viewport.h"
30
32
33#include "resources/image.h"
34#include "resources/theme.h"
35
36#include <guichan/font.hpp>
37#include <guichan/listmodel.hpp>
38
39ShopListBox::ShopListBox(gcn::ListModel *listModel):
40 ListBox(listModel)
41{
42 mRowHeight = getFont()->getHeight();
43 mPriceCheck = true;
44
46}
47
48ShopListBox::ShopListBox(gcn::ListModel *listModel, ShopItems *shopListModel):
49 ListBox(listModel),
50 mShopItems(shopListModel)
51{
52 mRowHeight = std::max(getFont()->getHeight(), ITEM_ICON_SIZE);
53 mPriceCheck = true;
54
56}
57
62
64{
65 mPlayerMoney = money;
66}
67
68void ShopListBox::draw(gcn::Graphics *gcnGraphics)
69{
70 if (!mListModel)
71 return;
72
73 const int alpha = (int)(config.guiAlpha * 255.0f);
74 auto highlightColor = Theme::getThemeColor(Theme::HIGHLIGHT);
75 auto warningColor = Theme::getThemeColor(Theme::SHOP_WARNING);
76 auto textColor = Theme::getThemeColor(Theme::TEXT);
77 auto highlightTextColor = Theme::getThemeColor(Theme::HIGHLIGHT_TEXT);
78 highlightColor.a = alpha;
79 warningColor.a = alpha;
80
81 auto *graphics = static_cast<Graphics*>(gcnGraphics);
82
83 graphics->setFont(getFont());
84 const int fontHeight = getFont()->getHeight();
85
86 // Draw the list elements
87 for (int i = 0, y = 0;
88 i < mListModel->getNumberOfElements();
89 ++i, y += mRowHeight)
90 {
91 ShopItem *shopItem = mShopItems ? mShopItems->at(i) : nullptr;
92
93 if (shopItem && mPlayerMoney < shopItem->getPrice() && mPriceCheck)
94 {
95 if (i != mSelected)
96 {
97 graphics->setColor(warningColor);
98 }
99 else
100 {
101 gcn::Color blend = warningColor;
102 blend.r = (blend.r + highlightColor.r) / 2;
103 blend.g = (blend.g + highlightColor.g) / 2;
104 blend.b = (blend.b + highlightColor.b) / 2;
105 blend.a = alpha;
106 graphics->setColor(blend);
107 }
108 graphics->fillRectangle(gcn::Rectangle(0, y, getWidth(), mRowHeight));
109 }
110 else if (i == mSelected)
111 {
112 graphics->setColor(highlightColor);
113 graphics->fillRectangle(gcn::Rectangle(0, y, getWidth(), mRowHeight));
114 }
115
116 if (shopItem)
117 {
118 if (Image *icon = shopItem->getImage())
119 {
120 icon->setAlpha(1.0f);
121 graphics->drawImage(icon, 1, y);
122 }
123
124 // Draw the item quantity when it's not just a single item
125 if (shopItem->getQuantity() > 1)
126 {
127 graphics->setColor(textColor);
129 1 + ITEM_ICON_SIZE,
130 y + ITEM_ICON_SIZE - fontHeight,
131 Graphics::RIGHT);
132 }
133 }
134
135 graphics->setColor(i == mSelected ? highlightTextColor : textColor);
136 graphics->drawText(mListModel->getElementAt(i),
137 ITEM_ICON_SIZE + 5,
138 y + (ITEM_ICON_SIZE - fontHeight) / 2);
139 }
140}
141
143{
144 if (mListModel)
145 setHeight(mRowHeight * mListModel->getNumberOfElements());
146}
147
149{
150 mPriceCheck = check;
151}
152
153void ShopListBox::mouseMoved(gcn::MouseEvent &event)
154{
155 if (!mShopItems)
156 return;
157
158 int index = event.getY() / mRowHeight;
159
160 if (index < 0 || index >= mShopItems->getNumberOfElements())
161 {
162 mItemPopup->setVisible(false);
163 }
164 else
165 {
166 if (Item *item = mShopItems->at(index))
167 {
168 mItemPopup->setItem(item->getInfo());
170 }
171 else
172 {
173 mItemPopup->setVisible(false);
174 }
175 }
176}
177
178void ShopListBox::mouseExited(gcn::MouseEvent &event)
179{
180 mItemPopup->setVisible(false);
181}
A central point of control for graphics.
Definition graphics.h:78
int getHeight() const
Returns the logical height of the screen.
Definition graphics.h:226
void drawText(const std::string &text, int x, int y, gcn::Graphics::Alignment alignment, const gcn::Color &color, gcn::Font *font, bool outline=false, bool shadow=false, const std::optional< gcn::Color > &outlineColor={}, const std::optional< gcn::Color > &shadowColor={})
Definition graphics.cpp:176
void setColor(const gcn::Color &color) override
Definition graphics.h:255
bool drawImage(const Image *image, int x, int y)
Blits an image onto the screen.
Definition graphics.cpp:36
Defines a class for loading and storing images.
Definition image.h:45
A popup that displays information about an item.
Definition itempopup.h:39
void setItem(const ItemInfo &item, bool showImage=false)
Sets the info to be displayed given a particular item.
Represents one or more instances of a certain item type.
Definition item.h:35
int getQuantity() const
Returns the number of items.
Definition item.h:69
Image * getImage() const
Returns the item image.
Definition item.h:54
A list box, meant to be used inside a scroll area.
Definition listbox.h:36
void position(int x, int y)
Sets the location to display the popup.
Definition popup.cpp:183
Represents an item in a shop inventory.
Definition shopitem.h:33
This class handles the list of items available in a shop.
Definition shopitems.h:41
ShopItem * at(int i) const
Returns the item number i in the shop.
Definition shopitems.cpp:72
int getNumberOfElements() override
Returns the number of items in the shop.
Definition shopitems.cpp:38
ShopListBox(gcn::ListModel *listModel)
bool mPriceCheck
Definition shoplistbox.h:97
ItemPopup * mItemPopup
Definition shoplistbox.h:93
void draw(gcn::Graphics *graphics) override
Draws the list box.
void setPriceCheck(bool check)
Set on/off the disabling of too expensive items.
void setPlayersMoney(int money)
gives information about the current player's money
void mouseMoved(gcn::MouseEvent &event) override
Show ItemTooltip.
~ShopListBox() override
void mouseExited(gcn::MouseEvent &event) override
Hide ItemTooltip.
unsigned int mRowHeight
Row Height.
Definition shoplistbox.h:95
ShopItems * mShopItems
Keeps another pointer to the same listModel, permitting to use the ShopItems specific functions.
Definition shoplistbox.h:91
void adjustSize()
Adjust List draw size.
static const gcn::Color & getThemeColor(int type)
Gets the color associated with the type in the default palette (0).
Definition theme.cpp:313
@ HIGHLIGHT_TEXT
Definition theme.h:232
@ HIGHLIGHT
Definition theme.h:231
@ SHOP_WARNING
Definition theme.h:234
@ TEXT
Definition theme.h:214
int getMouseY() const
Returns mouse y in pixels.
Definition viewport.h:136
int getMouseX() const
Returns mouse x in pixels.
Definition viewport.h:131
Config config
Global settings (config.xml)
Definition client.cpp:97
Graphics * graphics
Definition client.cpp:104
Viewport * viewport
Viewport on the map.
Definition game.cpp:115
const int ITEM_ICON_SIZE
Definition item.h:29
std::string toString(const T &arg)
Converts the given value to a string using std::stringstream.
Definition stringutils.h:68
float guiAlpha