1 |
|
|
/* |
2 |
|
|
* The ManaPlus Client |
3 |
|
|
* Copyright (C) 2007-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/shortcut/itemshortcut.h" |
24 |
|
|
|
25 |
|
|
#include "configuration.h" |
26 |
|
|
#include "spellmanager.h" |
27 |
|
|
|
28 |
|
|
#include "being/playerinfo.h" |
29 |
|
|
|
30 |
|
|
#include "const/spells.h" |
31 |
|
|
|
32 |
|
|
#include "const/resources/skill.h" |
33 |
|
|
|
34 |
|
|
#include "gui/windows/skilldialog.h" |
35 |
|
|
|
36 |
|
|
#include "resources/inventory/inventory.h" |
37 |
|
|
|
38 |
|
|
#include "resources/item/item.h" |
39 |
|
|
|
40 |
|
|
#include "debug.h" |
41 |
|
|
|
42 |
|
|
ItemShortcut *itemShortcut[SHORTCUT_TABS]; |
43 |
|
|
|
44 |
|
|
ItemShortcut::ItemShortcut(const size_t number) : |
45 |
|
|
mItems(), |
46 |
|
|
mItemColors(), |
47 |
|
|
mItemData(), |
48 |
|
|
mNumber(number), |
49 |
|
|
mItemSelected(-1), |
50 |
|
|
mItemColorSelected(ItemColor_one) |
51 |
|
|
{ |
52 |
|
|
load(); |
53 |
|
|
} |
54 |
|
|
|
55 |
|
|
ItemShortcut::~ItemShortcut() |
56 |
|
|
{ |
57 |
|
|
logger->log1("ItemShortcut::~ItemShortcut"); |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
void ItemShortcut::load() |
61 |
|
|
{ |
62 |
|
|
std::string name; |
63 |
|
|
std::string color; |
64 |
|
|
std::string data; |
65 |
|
|
if (mNumber == SHORTCUT_AUTO_TAB) |
66 |
|
|
return; |
67 |
|
|
|
68 |
|
|
const Configuration *cfg = &serverConfig; |
69 |
|
|
if (mNumber != 0) |
70 |
|
|
{ |
71 |
|
|
name = std::string("shortcut").append( |
72 |
|
|
toString(CAST_S32(mNumber))).append("_"); |
73 |
|
|
color = std::string("shortcutColor").append( |
74 |
|
|
toString(CAST_U32(mNumber))).append("_"); |
75 |
|
|
data = std::string("shortcutData").append( |
76 |
|
|
toString(CAST_U32(mNumber))).append("_"); |
77 |
|
|
} |
78 |
|
|
else |
79 |
|
|
{ |
80 |
|
|
name = "shortcut"; |
81 |
|
|
color = "shortcutColor"; |
82 |
|
|
data = "shortcutData"; |
83 |
|
|
} |
84 |
|
|
for (unsigned int i = 0; i < SHORTCUT_ITEMS; i++) |
85 |
|
|
{ |
86 |
|
|
const int itemId = cfg->getValue(name + toString(i), -1); |
87 |
|
|
const ItemColor itemColor = fromInt( |
88 |
|
|
cfg->getValue(color + toString(i), 1), |
89 |
|
|
ItemColor); |
90 |
|
|
|
91 |
|
|
mItems[i] = itemId; |
92 |
|
|
mItemColors[i] = itemColor; |
93 |
|
|
mItemData[i] = cfg->getValue(data + toString(i), std::string()); |
94 |
|
|
} |
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
void ItemShortcut::save() const |
98 |
|
|
{ |
99 |
|
|
std::string name; |
100 |
|
|
std::string color; |
101 |
|
|
std::string data; |
102 |
|
|
if (mNumber == SHORTCUT_AUTO_TAB) |
103 |
|
|
return; |
104 |
|
|
if (mNumber != 0) |
105 |
|
|
{ |
106 |
|
|
name = std::string("shortcut").append( |
107 |
|
|
toString(CAST_S32(mNumber))).append("_"); |
108 |
|
|
color = std::string("shortcutColor").append( |
109 |
|
|
toString(CAST_U32(mNumber))).append("_"); |
110 |
|
|
data = std::string("shortcutData").append( |
111 |
|
|
toString(CAST_U32(mNumber))).append("_"); |
112 |
|
|
} |
113 |
|
|
else |
114 |
|
|
{ |
115 |
|
|
name = "shortcut"; |
116 |
|
|
color = "shortcutColor"; |
117 |
|
|
data = "shortcutData"; |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
for (unsigned int i = 0; i < SHORTCUT_ITEMS; i++) |
121 |
|
|
{ |
122 |
|
|
const int itemId = mItems[i] != 0 ? mItems[i] : -1; |
123 |
|
|
const int itemColor = toInt(mItemColors[i], int); |
124 |
|
|
if (itemId != -1) |
125 |
|
|
{ |
126 |
|
|
const std::string itemData = mItemData[i]; |
127 |
|
|
serverConfig.setValue(name + toString(i), itemId); |
128 |
|
|
serverConfig.setValue(color + toString(i), itemColor); |
129 |
|
|
serverConfig.setValue(data + toString(i), itemData); |
130 |
|
|
} |
131 |
|
|
else |
132 |
|
|
{ |
133 |
|
|
serverConfig.deleteKey(name + toString(i)); |
134 |
|
|
serverConfig.deleteKey(color + toString(i)); |
135 |
|
|
serverConfig.deleteKey(data + toString(i)); |
136 |
|
|
} |
137 |
|
|
} |
138 |
|
|
} |
139 |
|
|
|
140 |
|
|
void ItemShortcut::clear() |
141 |
|
|
{ |
142 |
|
|
for (size_t i = 0; i < SHORTCUT_ITEMS; i++) |
143 |
|
|
{ |
144 |
|
|
mItems[i] = 0; |
145 |
|
|
mItemColors[i] = ItemColor_zero; |
146 |
|
|
mItemData[i].clear(); |
147 |
|
|
} |
148 |
|
|
} |
149 |
|
|
|
150 |
|
|
void ItemShortcut::useItem(const size_t index) const |
151 |
|
|
{ |
152 |
|
|
const Inventory *const inv = PlayerInfo::getInventory(); |
153 |
|
|
if (inv == nullptr) |
154 |
|
|
return; |
155 |
|
|
|
156 |
|
|
const int itemId = mItems[index]; |
157 |
|
|
const ItemColor itemColor = mItemColors[index]; |
158 |
|
|
if (itemId >= 0) |
159 |
|
|
{ |
160 |
|
|
if (itemId < SPELL_MIN_ID) |
161 |
|
|
{ |
162 |
|
|
const Item *const item = inv->findItem(itemId, itemColor); |
163 |
|
|
if (item != nullptr && item->getQuantity() != 0) |
164 |
|
|
PlayerInfo::useEquipItem(item, 0, Sfx_true); |
165 |
|
|
} |
166 |
|
|
else if (itemId < SKILL_MIN_ID && (spellManager != nullptr)) |
167 |
|
|
{ |
168 |
|
|
spellManager->useItem(itemId); |
169 |
|
|
} |
170 |
|
|
else if (skillDialog != nullptr) |
171 |
|
|
{ |
172 |
|
|
skillDialog->useItem(itemId, |
173 |
|
|
fromBool(config.getBoolValue("skillAutotarget"), AutoTarget), |
174 |
|
|
toInt(itemColor, int), |
175 |
|
|
mItemData[index]); |
176 |
|
|
} |
177 |
|
|
} |
178 |
|
|
} |
179 |
|
|
|
180 |
|
|
void ItemShortcut::equipItem(const size_t index) const |
181 |
|
|
{ |
182 |
|
|
const Inventory *const inv = PlayerInfo::getInventory(); |
183 |
|
|
if (inv == nullptr) |
184 |
|
|
return; |
185 |
|
|
|
186 |
|
|
const int itemId = mItems[index]; |
187 |
|
|
if (itemId != 0) |
188 |
|
|
{ |
189 |
|
|
const Item *const item = inv->findItem(itemId, mItemColors[index]); |
190 |
|
|
if ((item != nullptr) && (item->getQuantity() != 0)) |
191 |
|
|
{ |
192 |
|
|
if (item->isEquipment() == Equipm_true) |
193 |
|
|
{ |
194 |
|
|
if (item->isEquipped() == Equipped_false) |
195 |
|
|
PlayerInfo::equipItem(item, Sfx_true); |
196 |
|
|
} |
197 |
|
|
} |
198 |
|
|
} |
199 |
|
|
} |
200 |
|
|
void ItemShortcut::unequipItem(const size_t index) const |
201 |
|
|
{ |
202 |
|
|
const Inventory *const inv = PlayerInfo::getInventory(); |
203 |
|
|
if (inv == nullptr) |
204 |
|
|
return; |
205 |
|
|
|
206 |
|
|
const int itemId = mItems[index]; |
207 |
|
|
if (itemId != 0) |
208 |
|
|
{ |
209 |
|
|
const Item *const item = inv->findItem(itemId, mItemColors[index]); |
210 |
|
|
if ((item != nullptr) && (item->getQuantity() != 0)) |
211 |
|
|
{ |
212 |
|
|
if (item->isEquipment() == Equipm_true) |
213 |
|
|
{ |
214 |
|
|
if (item->isEquipped() == Equipped_true) |
215 |
|
|
PlayerInfo::unequipItem(item, Sfx_true); |
216 |
|
|
} |
217 |
|
|
} |
218 |
|
|
} |
219 |
|
|
} |
220 |
|
|
|
221 |
|
|
void ItemShortcut::setItemSelected(const Item *const item) |
222 |
|
|
{ |
223 |
|
|
if (item != nullptr) |
224 |
|
|
{ |
225 |
|
|
mItemSelected = item->getId(); |
226 |
|
|
mItemColorSelected = item->getColor(); |
227 |
|
|
} |
228 |
|
|
else |
229 |
|
|
{ |
230 |
|
|
mItemSelected = -1; |
231 |
|
|
mItemColorSelected = ItemColor_one; |
232 |
|
|
} |
233 |
|
|
} |
234 |
|
|
|
235 |
|
|
void ItemShortcut::setItem(const size_t index) |
236 |
|
|
{ |
237 |
|
|
mItems[index] = mItemSelected; |
238 |
|
|
mItemColors[index] = mItemColorSelected; |
239 |
|
|
save(); |
240 |
|
|
} |
241 |
|
|
|
242 |
|
|
void ItemShortcut::setItem(const size_t index, |
243 |
|
|
const int item, |
244 |
|
|
const ItemColor color) |
245 |
|
|
{ |
246 |
|
|
mItems[index] = item; |
247 |
|
|
mItemColors[index] = color; |
248 |
|
|
save(); |
249 |
|
|
} |
250 |
|
|
|
251 |
|
|
void ItemShortcut::setItemFast(const size_t index, |
252 |
|
|
const int item, |
253 |
|
|
const ItemColor color) |
254 |
|
|
{ |
255 |
|
|
mItems[index] = item; |
256 |
|
|
mItemColors[index] = color; |
257 |
|
|
} |
258 |
|
|
|
259 |
|
|
void ItemShortcut::swap(const size_t index1, |
260 |
|
|
const size_t index2) |
261 |
|
|
{ |
262 |
|
|
if (CAST_U32(index1) >= SHORTCUT_ITEMS || |
263 |
|
|
CAST_U32(index2) >= SHORTCUT_ITEMS) |
264 |
|
|
{ |
265 |
|
|
return; |
266 |
|
|
} |
267 |
|
|
|
268 |
|
|
const int tmpItem = mItems[index1]; |
269 |
|
|
mItems[index1] = mItems[index2]; |
270 |
|
|
mItems[index2] = tmpItem; |
271 |
|
|
const ItemColor tmpColor = mItemColors[index1]; |
272 |
|
|
mItemColors[index1] = mItemColors[index2]; |
273 |
|
|
mItemColors[index2] = tmpColor; |
274 |
|
|
|
275 |
|
|
const std::string tmpData = mItemData[index1]; |
276 |
|
|
mItemData[index1] = mItemData[index2]; |
277 |
|
|
mItemData[index2] = tmpData; |
278 |
|
|
save(); |
279 |
|
|
} |
280 |
|
|
|
281 |
|
|
size_t ItemShortcut::getFreeIndex() const |
282 |
|
|
{ |
283 |
|
|
for (size_t i = 0; i < SHORTCUT_ITEMS; i++) |
284 |
|
|
{ |
285 |
|
|
if (mItems[i] < 0) |
286 |
|
|
return i; |
287 |
|
|
} |
288 |
|
|
return SHORTCUT_ITEMS; |
289 |
|
|
} |