|
1 /* |
|
2 * Hedgewars, a free turn based strategy game |
|
3 * Copyright (c) 2006-2008 Igor Ulyanov <iulyanov@gmail.com> |
|
4 * Copyright (c) 2008-2011 Andrey Korotaev <unC0Rr@gmail.com> |
|
5 * |
|
6 * This program is free software; you can redistribute it and/or modify |
|
7 * it under the terms of the GNU General Public License as published by |
|
8 * the Free Software Foundation; version 2 of the License |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License |
|
16 * along with this program; if not, write to the Free Software |
|
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
18 */ |
|
19 |
|
20 #include "selectWeapon.h" |
|
21 #include "weaponItem.h" |
|
22 #include "hwconsts.h" |
|
23 |
|
24 #include <QPushButton> |
|
25 #include <QGridLayout> |
|
26 #include <QHBoxLayout> |
|
27 #include <QLabel> |
|
28 #include <QBitmap> |
|
29 #include <QLineEdit> |
|
30 #include <QSettings> |
|
31 #include <QMessageBox> |
|
32 #include <QTabWidget> |
|
33 #include <math.h> |
|
34 |
|
35 QImage getAmmoImage(int num) |
|
36 { |
|
37 static QImage ammo(":Ammos.png"); |
|
38 int x = num/(ammo.height()/32); |
|
39 int y = (num-((ammo.height()/32)*x))*32; |
|
40 x*=32; |
|
41 return ammo.copy(x, y, 32, 32); |
|
42 } |
|
43 |
|
44 SelWeaponItem::SelWeaponItem(bool allowInfinite, int iconNum, int wNum, QImage image, QImage imagegrey, QWidget* parent) : |
|
45 QWidget(parent) |
|
46 { |
|
47 QHBoxLayout* hbLayout = new QHBoxLayout(this); |
|
48 hbLayout->setSpacing(1); |
|
49 hbLayout->setMargin(1); |
|
50 |
|
51 QLabel* lbl = new QLabel(this); |
|
52 lbl->setPixmap(QPixmap::fromImage(getAmmoImage(iconNum))); |
|
53 lbl->setMaximumWidth(30); |
|
54 lbl->setGeometry(0, 0, 30, 30); |
|
55 hbLayout->addWidget(lbl); |
|
56 |
|
57 item = new WeaponItem(image, imagegrey, this); |
|
58 item->setItemsNum(wNum); |
|
59 item->setInfinityState(allowInfinite); |
|
60 hbLayout->addWidget(item); |
|
61 |
|
62 hbLayout->setStretchFactor(lbl, 1); |
|
63 hbLayout->setStretchFactor(item, 99); |
|
64 hbLayout->setAlignment(lbl, Qt::AlignLeft | Qt::AlignVCenter); |
|
65 hbLayout->setAlignment(item, Qt::AlignLeft | Qt::AlignVCenter); |
|
66 } |
|
67 |
|
68 void SelWeaponItem::setItemsNum(const unsigned char num) |
|
69 { |
|
70 item->setItemsNum(num); |
|
71 } |
|
72 |
|
73 unsigned char SelWeaponItem::getItemsNum() const |
|
74 { |
|
75 return item->getItemsNum(); |
|
76 } |
|
77 |
|
78 void SelWeaponItem::setEnabled(bool value) |
|
79 { |
|
80 item->setEnabled(value); |
|
81 } |
|
82 |
|
83 SelWeaponWidget::SelWeaponWidget(int numItems, QWidget* parent) : |
|
84 QFrame(parent), |
|
85 m_numItems(numItems) |
|
86 { |
|
87 wconf = new QSettings(cfgdir->absolutePath() + "/weapons.ini", QSettings::IniFormat, this); |
|
88 |
|
89 for(int i = 0; i < cDefaultAmmos.size(); ++i) |
|
90 wconf->setValue(cDefaultAmmos[i].first, cDefaultAmmos[i].second); |
|
91 |
|
92 QStringList keys = wconf->allKeys(); |
|
93 for(int i = 0; i < keys.size(); i++) |
|
94 { |
|
95 if (wconf->value(keys[i]).toString().size() != cDefaultAmmoStore->size()) |
|
96 wconf->remove(keys[i]); |
|
97 } |
|
98 |
|
99 QString currentState = *cDefaultAmmoStore; |
|
100 |
|
101 QTabWidget * tbw = new QTabWidget(this); |
|
102 QWidget * page1 = new QWidget(this); |
|
103 p1Layout = new QGridLayout(page1); |
|
104 p1Layout->setSpacing(1); |
|
105 p1Layout->setMargin(1); |
|
106 QWidget * page2 = new QWidget(this); |
|
107 p2Layout = new QGridLayout(page2); |
|
108 p2Layout->setSpacing(1); |
|
109 p2Layout->setMargin(1); |
|
110 QWidget * page3 = new QWidget(this); |
|
111 p3Layout = new QGridLayout(page3); |
|
112 p3Layout->setSpacing(1); |
|
113 p3Layout->setMargin(1); |
|
114 QWidget * page4 = new QWidget(this); |
|
115 p4Layout = new QGridLayout(page4); |
|
116 p4Layout->setSpacing(1); |
|
117 p4Layout->setMargin(1); |
|
118 |
|
119 tbw->addTab(page1, tr("Weapon set")); |
|
120 tbw->addTab(page2, tr("Probabilities")); |
|
121 tbw->addTab(page4, tr("Ammo in boxes")); |
|
122 tbw->addTab(page3, tr("Delays")); |
|
123 |
|
124 QGridLayout * pageLayout = new QGridLayout(this); |
|
125 pageLayout->addWidget(tbw); |
|
126 |
|
127 |
|
128 int j = -1; |
|
129 int i = 0, k = 0; |
|
130 for(; i < m_numItems; ++i) { |
|
131 if (i == 6) continue; |
|
132 if (i == 52) continue; // Disable structures for now |
|
133 if (k % 4 == 0) ++j; |
|
134 SelWeaponItem * swi = new SelWeaponItem(true, i, currentState[i].digitValue(), QImage(":/res/ammopic.png"), QImage(":/res/ammopicgrey.png"), this); |
|
135 weaponItems[i].append(swi); |
|
136 p1Layout->addWidget(swi, j, k % 4); |
|
137 |
|
138 SelWeaponItem * pwi = new SelWeaponItem(false, i, currentState[numItems + i].digitValue(), QImage(":/res/ammopicbox.png"), QImage(":/res/ammopicboxgrey.png"), this); |
|
139 weaponItems[i].append(pwi); |
|
140 p2Layout->addWidget(pwi, j, k % 4); |
|
141 |
|
142 SelWeaponItem * dwi = new SelWeaponItem(false, i, currentState[numItems*2 + i].digitValue(), QImage(":/res/ammopicdelay.png"), QImage(":/res/ammopicdelaygrey.png"), this); |
|
143 weaponItems[i].append(dwi); |
|
144 p3Layout->addWidget(dwi, j, k % 4); |
|
145 |
|
146 SelWeaponItem * awi = new SelWeaponItem(false, i, currentState[numItems*3 + i].digitValue(), QImage(":/res/ammopic.png"), QImage(":/res/ammopicgrey.png"), this); |
|
147 weaponItems[i].append(awi); |
|
148 p4Layout->addWidget(awi, j, k % 4); |
|
149 |
|
150 ++k; |
|
151 } |
|
152 |
|
153 //pLayout->setRowStretch(5, 100); |
|
154 m_name = new QLineEdit(this); |
|
155 pageLayout->addWidget(m_name, i, 0, 1, 5); |
|
156 } |
|
157 |
|
158 void SelWeaponWidget::setWeapons(const QString& ammo) |
|
159 { |
|
160 bool enable = true; |
|
161 for(int i = 0; i < cDefaultAmmos.size(); i++) |
|
162 if (!cDefaultAmmos[i].first.compare(m_name->text())) { |
|
163 enable = false; |
|
164 } |
|
165 for(int i = 0; i < m_numItems; ++i) { |
|
166 twi::iterator it = weaponItems.find(i); |
|
167 if (it == weaponItems.end()) continue; |
|
168 it.value()[0]->setItemsNum(ammo[i].digitValue()); |
|
169 it.value()[1]->setItemsNum(ammo[m_numItems + i].digitValue()); |
|
170 it.value()[2]->setItemsNum(ammo[m_numItems*2 + i].digitValue()); |
|
171 it.value()[3]->setItemsNum(ammo[m_numItems*3 + i].digitValue()); |
|
172 it.value()[0]->setEnabled(enable); |
|
173 it.value()[1]->setEnabled(enable); |
|
174 it.value()[2]->setEnabled(enable); |
|
175 it.value()[3]->setEnabled(enable); |
|
176 } |
|
177 m_name->setEnabled(enable); |
|
178 } |
|
179 |
|
180 void SelWeaponWidget::setDefault() |
|
181 { |
|
182 for(int i = 0; i < cDefaultAmmos.size(); i++) |
|
183 if (!cDefaultAmmos[i].first.compare(m_name->text())) { |
|
184 return; |
|
185 } |
|
186 setWeapons(*cDefaultAmmoStore); |
|
187 } |
|
188 |
|
189 void SelWeaponWidget::save() |
|
190 { |
|
191 for(int i = 0; i < cDefaultAmmos.size(); i++) |
|
192 if (!cDefaultAmmos[i].first.compare(m_name->text())) { |
|
193 QMessageBox::warning(0, QMessageBox::tr("Weapons"), QMessageBox::tr("Can not overwrite default weapon set '%1'!").arg(cDefaultAmmos[i].first)); |
|
194 return; |
|
195 } |
|
196 |
|
197 if (m_name->text() == "") return; |
|
198 |
|
199 QString state1; |
|
200 QString state2; |
|
201 QString state3; |
|
202 QString state4; |
|
203 |
|
204 for(int i = 0; i < m_numItems; ++i) { |
|
205 twi::const_iterator it = weaponItems.find(i); |
|
206 int num = it == weaponItems.end() ? 9 : it.value()[0]->getItemsNum(); // 9 is for 'skip turn' |
|
207 state1.append(QString::number(num)); |
|
208 int prob = it == weaponItems.end() ? 0 : it.value()[1]->getItemsNum(); |
|
209 state2.append(QString::number(prob)); |
|
210 int del = it == weaponItems.end() ? 0 : it.value()[2]->getItemsNum(); |
|
211 state3.append(QString::number(del)); |
|
212 int am = it == weaponItems.end() ? 0 : it.value()[3]->getItemsNum(); |
|
213 state4.append(QString::number(am)); |
|
214 } |
|
215 if (curWeaponsName != "") { |
|
216 // remove old entry |
|
217 wconf->remove(curWeaponsName); |
|
218 } |
|
219 wconf->setValue(m_name->text(), state1 + state2 + state3 + state4); |
|
220 emit weaponsChanged(); |
|
221 } |
|
222 |
|
223 int SelWeaponWidget::operator [] (unsigned int weaponIndex) const |
|
224 { |
|
225 twi::const_iterator it = weaponItems.find(weaponIndex); |
|
226 return it == weaponItems.end() ? 9 : it.value()[0]->getItemsNum(); |
|
227 } |
|
228 |
|
229 QString SelWeaponWidget::getWeaponsString(const QString& name) const |
|
230 { |
|
231 return wconf->value(name).toString(); |
|
232 } |
|
233 |
|
234 void SelWeaponWidget::deleteWeaponsName() |
|
235 { |
|
236 if (curWeaponsName == "") return; |
|
237 |
|
238 for(int i = 0; i < cDefaultAmmos.size(); i++) |
|
239 if (!cDefaultAmmos[i].first.compare(m_name->text())) { |
|
240 QMessageBox::warning(0, QMessageBox::tr("Weapons"), QMessageBox::tr("Can not delete default weapon set '%1'!").arg(cDefaultAmmos[i].first)); |
|
241 return; |
|
242 } |
|
243 |
|
244 QMessageBox reallyDelete(QMessageBox::Question, QMessageBox::tr("Weapons"), QMessageBox::tr("Really delete this weapon set?"), QMessageBox::Ok | QMessageBox::Cancel); |
|
245 |
|
246 if (reallyDelete.exec() == QMessageBox::Ok) { |
|
247 wconf->remove(curWeaponsName); |
|
248 emit weaponsDeleted(); |
|
249 } |
|
250 } |
|
251 |
|
252 void SelWeaponWidget::newWeaponsName() |
|
253 { |
|
254 QString newName = tr("new"); |
|
255 if(wconf->contains(newName)) { |
|
256 //name already used -> look for an appropriate name: |
|
257 int i=2; |
|
258 while(wconf->contains(newName = tr("new")+QString::number(i++))); |
|
259 } |
|
260 setWeaponsName(newName); |
|
261 } |
|
262 |
|
263 void SelWeaponWidget::setWeaponsName(const QString& name) |
|
264 { |
|
265 m_name->setText(name); |
|
266 |
|
267 curWeaponsName = name; |
|
268 |
|
269 if(name != "" && wconf->contains(name)) { |
|
270 setWeapons(wconf->value(name).toString()); |
|
271 } else { |
|
272 setWeapons(*cDefaultAmmoStore); |
|
273 } |
|
274 } |
|
275 |
|
276 QStringList SelWeaponWidget::getWeaponNames() const |
|
277 { |
|
278 return wconf->allKeys(); |
|
279 } |
|
280 |
|
281 void SelWeaponWidget::copy() |
|
282 { |
|
283 if(wconf->contains(curWeaponsName)) { |
|
284 QString ammo = getWeaponsString(curWeaponsName); |
|
285 QString newName = tr("copy of") + " " + curWeaponsName; |
|
286 if(wconf->contains(newName)) { |
|
287 //name already used -> look for an appropriate name: |
|
288 int i=2; |
|
289 while(wconf->contains(newName = tr("copy of") + " " + curWeaponsName+QString::number(i++))); |
|
290 } |
|
291 setWeaponsName(newName); |
|
292 setWeapons(ammo); |
|
293 } |
|
294 } |