ManaPlus
shopitems.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2004-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2019 The ManaPlus Developers
6  *
7  * This file is part of The ManaPlus Client.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include "gui/models/shopitems.h"
24 
26 
27 #include "utils/dtor.h"
28 #include "utils/foreach.h"
29 
30 #include "debug.h"
31 
32 ShopItems::ShopItems(const bool mergeDuplicates,
33  const std::string &currency) :
34  ListModel(),
35  mAllShopItems(),
36  mShopItems(),
37  mCurrency(currency),
38  mMergeDuplicates(mergeDuplicates)
39 {
40 }
41 
43 {
44  clear();
45 }
46 
47 std::string ShopItems::getElementAt(int i)
48 {
49  if (i < 0 || CAST_U32(i)
50  >= CAST_U32(mShopItems.size()) || (mShopItems.at(i) == nullptr))
51  {
52  return "";
53  }
54 
55  return mShopItems.at(i)->getDisplayName();
56 }
57 
59  const ItemTypeT type,
60  const ItemColor color,
61  const int amount,
62  const int price)
63 {
64  ShopItem *const item = new ShopItem(-1,
65  id,
66  type,
67  color,
68  amount,
69  price,
70  mCurrency);
71  mShopItems.push_back(item);
72  mAllShopItems.push_back(item);
73  return item;
74 }
75 
77  const ItemTypeT type,
78  const ItemColor color,
79  const int amount,
80  const int price)
81 {
82  ShopItem *item = findItem(id, color);
83  if (item == nullptr)
84  {
85  item = new ShopItem(-1,
86  id,
87  type,
88  color,
89  amount,
90  price,
91  mCurrency);
92  mShopItems.push_back(item);
93  mAllShopItems.push_back(item);
94  }
95  return item;
96 }
97 
98 ShopItem *ShopItems::addItem2(const int inventoryIndex,
99  const int id,
100  const ItemTypeT type,
101  const ItemColor color,
102  const int quantity,
103  const int price)
104 {
105  ShopItem *item = nullptr;
106  if (mMergeDuplicates)
107  item = findItem(id, color);
108 
109  if (item != nullptr)
110  {
111  item->addDuplicate(inventoryIndex, quantity);
112  }
113  else
114  {
115  item = new ShopItem(inventoryIndex,
116  id,
117  type,
118  color,
119  quantity,
120  price,
121  mCurrency);
122  mShopItems.push_back(item);
123  mAllShopItems.push_back(item);
124  }
125  return item;
126 }
127 
128 ShopItem *ShopItems::at(const size_t i) const
129 {
130  if (i >= mShopItems.size())
131  return nullptr;
132 
133  return mShopItems.at(i);
134 }
135 
136 bool ShopItems::findInAllItems(STD_VECTOR<ShopItem*>::iterator &it,
137  const ShopItem *const item)
138 {
139  const STD_VECTOR<ShopItem*>::iterator it_end = mAllShopItems.end();
140  for (it = mAllShopItems.begin(); it != it_end; ++ it)
141  {
142  if (*it == item)
143  return true;
144  }
145  return false;
146 }
147 
148 void ShopItems::erase(const unsigned int i)
149 {
150  if (i >= CAST_U32(mShopItems.size()))
151  return;
152 
153  const ShopItem *const item = *(mShopItems.begin() + i);
154  STD_VECTOR<ShopItem*>::iterator it;
155  if (findInAllItems(it, item))
156  mAllShopItems.erase(it);
157  mShopItems.erase(mShopItems.begin() + i);
158 }
159 
160 void ShopItems::del(const unsigned int i)
161 {
162  if (i >= CAST_U32(mShopItems.size()))
163  return;
164 
165  ShopItem *item = *(mShopItems.begin() + i);
166  STD_VECTOR<ShopItem*>::iterator it;
167  if (findInAllItems(it, item))
168  mAllShopItems.erase(it);
169  mShopItems.erase(mShopItems.begin() + i);
170  delete item;
171 }
172 
174 {
176  mAllShopItems.clear();
177  mShopItems.clear();
178 }
179 
181  const ItemColor color) const
182 {
183  STD_VECTOR<ShopItem*>::const_iterator it = mShopItems.begin();
184  const STD_VECTOR<ShopItem*>::const_iterator e = mShopItems.end();
185  while (it != e)
186  {
187  ShopItem *const item = *it;
188  if (item->getId() == id && item->getColor() == color)
189  return item;
190 
191  ++it;
192  }
193 
194  return nullptr;
195 }
196 
198 {
199  mShopItems.clear();
200  FOR_EACH (STD_VECTOR<ShopItem*>::iterator, it, mAllShopItems)
201  {
202  ShopItem *const item = *it;
203  if ((item != nullptr) && item->isVisible())
204  mShopItems.push_back(item);
205  }
206 }
shopitems.h
ShopItems::mMergeDuplicates
bool mMergeDuplicates
Definition: shopitems.h:167
ShopItems::updateList
void updateList()
Definition: shopitems.cpp:197
ShopItems::at
ShopItem * at(const size_t i) const
Definition: shopitems.cpp:128
Item::getColor
ItemColor getColor() const
Definition: item.h:180
ShopItems::findItem
ShopItem * findItem(const int id, const ItemColor color) const
Definition: shopitems.cpp:180
shopitem.h
Item::getId
int getId() const
Definition: item.h:80
ItemTypeT
ItemType ::T ItemTypeT
Definition: itemtype.h:42
CAST_U32
#define CAST_U32
Definition: cast.h:30
ShopItems::mShopItems
std::vector< ShopItem * > mShopItems
Definition: shopitems.h:162
ShopItems::mAllShopItems
std::vector< ShopItem * > mAllShopItems
Definition: shopitems.h:160
ShopItems::del
void del(const unsigned int i)
Definition: shopitems.cpp:160
ListModel
Definition: listmodel.h:78
ShopItems::ShopItems
ShopItems(const bool mergeDuplicates, const std::string &currency)
Definition: shopitems.cpp:32
ShopItems::findInAllItems
bool findInAllItems(std::vector< ShopItem * >::iterator &it, const ShopItem *const item)
Definition: shopitems.cpp:136
ShopItems::mCurrency
std::string mCurrency
Definition: shopitems.h:164
ItemColor
uint16_t ItemColor
Definition: itemcolor.h:29
ShopItems::addItem2
ShopItem * addItem2(const int inventoryIndex, const int id, const ItemTypeT type, const ItemColor color, const int amount, const int price)
Definition: shopitems.cpp:98
ShopItems::addItemNoDup
ShopItem * addItemNoDup(const int id, const ItemTypeT type, const ItemColor color, const int amount, const int price)
Definition: shopitems.cpp:76
ShopItems::clear
void clear()
Definition: shopitems.cpp:173
delete_all
void delete_all(Container &c)
Definition: dtor.h:55
foreach.h
ShopItems::~ShopItems
~ShopItems()
Definition: shopitems.cpp:42
ShopItem::isVisible
bool isVisible() const
Definition: shopitem.h:145
ShopItem::addDuplicate
void addDuplicate(const int inventoryIndex, const int quantity)
Definition: shopitem.cpp:123
ShopItems::erase
void erase(const unsigned int i)
Definition: shopitems.cpp:148
dtor.h
ShopItems::getElementAt
std::string getElementAt(int i)
Definition: shopitems.cpp:47
ShopItems::addItem
ShopItem * addItem(const int id, const ItemTypeT type, const ItemColor color, const int amount, const int price)
Definition: shopitems.cpp:58
ShopItem
Definition: shopitem.h:36
FOR_EACH
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
debug.h