author | unc0rr |
Thu, 10 Sep 2009 12:23:31 +0000 | |
changeset 2371 | a20b6e26044b |
parent 2369 | c3eb11f1ab3a |
child 2377 | f3fab2b09e0c |
permissions | -rw-r--r-- |
612
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
1 |
/* |
1066 | 2 |
* Hedgewars, a free turn based strategy game |
883 | 3 |
* Copyright (c) 2006-2008 Ulyanov Igor <iulyanov@gmail.com> |
612
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
4 |
* |
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
5 |
* This program is free software; you can redistribute it and/or modify |
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
6 |
* it under the terms of the GNU General Public License as published by |
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
7 |
* the Free Software Foundation; version 2 of the License |
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
8 |
* |
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, |
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
12 |
* GNU General Public License for more details. |
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
13 |
* |
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
15 |
* along with this program; if not, write to the Free Software |
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
17 |
*/ |
1509 | 18 |
|
624 | 19 |
#include "selectWeapon.h" |
629 | 20 |
#include "weaponItem.h" |
681 | 21 |
#include "hwconsts.h" |
612
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
22 |
|
624 | 23 |
#include <QPushButton> |
24 |
#include <QGridLayout> |
|
629 | 25 |
#include <QHBoxLayout> |
26 |
#include <QLabel> |
|
630 | 27 |
#include <QBitmap> |
694 | 28 |
#include <QLineEdit> |
29 |
#include <QSettings> |
|
724 | 30 |
#include <QMessageBox> |
2369
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
31 |
#include <QTabWidget> |
624 | 32 |
|
629 | 33 |
QImage getAmmoImage(int num) |
34 |
{ |
|
1509 | 35 |
static QImage ammo(":Ammos.png"); |
36 |
return ammo.copy(0, num*32, 32, 32); |
|
629 | 37 |
} |
38 |
||
2369
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
39 |
SelWeaponItem::SelWeaponItem(bool allowInfinite, int iconNum, int wNum, QWidget* parent) : |
1509 | 40 |
QWidget(parent) |
629 | 41 |
{ |
1509 | 42 |
QHBoxLayout* hbLayout = new QHBoxLayout(this); |
43 |
hbLayout->setSpacing(1); |
|
44 |
hbLayout->setMargin(1); |
|
45 |
||
46 |
QLabel* lbl = new QLabel(this); |
|
47 |
lbl->setPixmap(QPixmap::fromImage(getAmmoImage(iconNum))); |
|
48 |
lbl->setMaximumWidth(30); |
|
49 |
lbl->setGeometry(0, 0, 30, 30); |
|
50 |
hbLayout->addWidget(lbl); |
|
639 | 51 |
|
1509 | 52 |
item = new WeaponItem(QImage(":/res/ammopic.png"), this); |
53 |
item->setItemsNum(wNum); |
|
2369
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
54 |
item->setInfinityState(allowInfinite); |
1509 | 55 |
hbLayout->addWidget(item); |
639 | 56 |
|
1509 | 57 |
hbLayout->setStretchFactor(lbl, 1); |
58 |
hbLayout->setStretchFactor(item, 99); |
|
59 |
hbLayout->setAlignment(lbl, Qt::AlignLeft | Qt::AlignVCenter); |
|
60 |
hbLayout->setAlignment(item, Qt::AlignLeft | Qt::AlignVCenter); |
|
629 | 61 |
} |
612
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
62 |
|
683 | 63 |
void SelWeaponItem::setItemsNum(const unsigned char num) |
64 |
{ |
|
1509 | 65 |
item->setItemsNum(num); |
683 | 66 |
} |
67 |
||
681 | 68 |
unsigned char SelWeaponItem::getItemsNum() const |
69 |
{ |
|
1509 | 70 |
return item->getItemsNum(); |
681 | 71 |
} |
72 |
||
683 | 73 |
SelWeaponWidget::SelWeaponWidget(int numItems, QWidget* parent) : |
1904 | 74 |
QFrame(parent), |
75 |
m_numItems(numItems) |
|
612
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
76 |
{ |
1509 | 77 |
wconf = new QSettings(cfgdir->absolutePath() + "/weapons.ini", QSettings::IniFormat, this); |
1004
4cbd91296df7
Don't let updated hedgewars version to make errors due to old weapons.ini
unc0rr
parents:
883
diff
changeset
|
78 |
|
1967 | 79 |
for(int i = 0; i < cDefaultAmmos.size(); ++i) |
80 |
wconf->setValue(cDefaultAmmos[i].first, cDefaultAmmos[i].second); |
|
694 | 81 |
|
1576
a02353129a41
Check for deprecated ammo schemes at startup and delete them
unc0rr
parents:
1509
diff
changeset
|
82 |
QStringList keys = wconf->allKeys(); |
a02353129a41
Check for deprecated ammo schemes at startup and delete them
unc0rr
parents:
1509
diff
changeset
|
83 |
for(int i = 0; i < keys.size(); i++) |
a02353129a41
Check for deprecated ammo schemes at startup and delete them
unc0rr
parents:
1509
diff
changeset
|
84 |
{ |
a02353129a41
Check for deprecated ammo schemes at startup and delete them
unc0rr
parents:
1509
diff
changeset
|
85 |
if (wconf->value(keys[i]).toString().size() != cDefaultAmmoStore->size()) |
a02353129a41
Check for deprecated ammo schemes at startup and delete them
unc0rr
parents:
1509
diff
changeset
|
86 |
wconf->remove(keys[i]); |
a02353129a41
Check for deprecated ammo schemes at startup and delete them
unc0rr
parents:
1509
diff
changeset
|
87 |
} |
a02353129a41
Check for deprecated ammo schemes at startup and delete them
unc0rr
parents:
1509
diff
changeset
|
88 |
|
a02353129a41
Check for deprecated ammo schemes at startup and delete them
unc0rr
parents:
1509
diff
changeset
|
89 |
QString currentState = *cDefaultAmmoStore; |
683 | 90 |
|
2369
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
91 |
QTabWidget * tbw = new QTabWidget(this); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
92 |
QWidget * page1 = new QWidget(this); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
93 |
p1Layout = new QGridLayout(page1); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
94 |
p1Layout->setSpacing(1); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
95 |
p1Layout->setMargin(1); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
96 |
QWidget * page2 = new QWidget(this); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
97 |
p2Layout = new QGridLayout(page2); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
98 |
p2Layout->setSpacing(1); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
99 |
p2Layout->setMargin(1); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
100 |
|
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
101 |
tbw->addTab(page1, tr("Weapon set")); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
102 |
tbw->addTab(page2, tr("Probabilities")); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
103 |
|
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
104 |
QGridLayout * pageLayout = new QGridLayout(this); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
105 |
pageLayout->addWidget(tbw); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
106 |
|
629 | 107 |
|
1509 | 108 |
int j = -1; |
109 |
int i = 0, k = 0; |
|
110 |
for(; i < m_numItems; ++i) { |
|
111 |
if (i == 6) continue; |
|
112 |
if (k % 4 == 0) ++j; |
|
2369
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
113 |
SelWeaponItem * swi = new SelWeaponItem(true, i, currentState[i].digitValue(), this); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
114 |
weaponItems[i].append(swi); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
115 |
p1Layout->addWidget(swi, j, k % 4); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
116 |
|
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
117 |
SelWeaponItem * pwi = new SelWeaponItem(false, i, currentState[numItems + i].digitValue(), this); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
118 |
weaponItems[i].append(pwi); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
119 |
p2Layout->addWidget(pwi, j, k % 4); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
120 |
|
1509 | 121 |
++k; |
122 |
} |
|
694 | 123 |
|
1509 | 124 |
//pLayout->setRowStretch(5, 100); |
125 |
m_name = new QLineEdit(this); |
|
2369
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
126 |
pageLayout->addWidget(m_name, i, 0, 1, 5); |
612
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
127 |
} |
681 | 128 |
|
694 | 129 |
void SelWeaponWidget::setWeapons(const QString& ammo) |
683 | 130 |
{ |
1509 | 131 |
for(int i = 0; i < m_numItems; ++i) { |
132 |
twi::iterator it = weaponItems.find(i); |
|
133 |
if (it == weaponItems.end()) continue; |
|
2369
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
134 |
it.value()[0]->setItemsNum(ammo[i].digitValue()); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
135 |
it.value()[1]->setItemsNum(ammo[m_numItems + i].digitValue()); |
1509 | 136 |
} |
137 |
update(); |
|
683 | 138 |
} |
139 |
||
140 |
void SelWeaponWidget::setDefault() |
|
141 |
{ |
|
1576
a02353129a41
Check for deprecated ammo schemes at startup and delete them
unc0rr
parents:
1509
diff
changeset
|
142 |
setWeapons(*cDefaultAmmoStore); |
683 | 143 |
} |
144 |
||
145 |
void SelWeaponWidget::save() |
|
146 |
{ |
|
1509 | 147 |
if (m_name->text() == "Default") { |
148 |
QMessageBox impossible(QMessageBox::Warning, QMessageBox::tr("Weapons"), QMessageBox::tr("Can not edit default weapon set")); |
|
149 |
impossible.exec(); |
|
150 |
return; |
|
151 |
} |
|
152 |
||
153 |
if (m_name->text() == "") return; |
|
154 |
||
2369
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
155 |
QString state1; |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
156 |
QString state2; |
1509 | 157 |
|
158 |
for(int i = 0; i < m_numItems; ++i) { |
|
159 |
twi::const_iterator it = weaponItems.find(i); |
|
2371 | 160 |
int num = it == weaponItems.end() ? 9 : it.value()[0]->getItemsNum(); // 9 is for 'skip turn' |
2369
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
161 |
state1.append(QString::number(num)); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
162 |
int prob = it == weaponItems.end() ? 0 : it.value()[1]->getItemsNum(); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
163 |
state2.append(QString::number(prob)); |
1509 | 164 |
} |
165 |
if (curWeaponsName != "") { |
|
166 |
// remove old entry |
|
167 |
wconf->remove(curWeaponsName); |
|
168 |
} |
|
2369
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
169 |
wconf->setValue(m_name->text(), state1 + state2); |
1509 | 170 |
emit weaponsChanged(); |
683 | 171 |
} |
172 |
||
681 | 173 |
int SelWeaponWidget::operator [] (unsigned int weaponIndex) const |
174 |
{ |
|
1509 | 175 |
twi::const_iterator it = weaponItems.find(weaponIndex); |
2369
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
176 |
return it == weaponItems.end() ? 9 : it.value()[0]->getItemsNum(); |
681 | 177 |
} |
178 |
||
696 | 179 |
QString SelWeaponWidget::getWeaponsString(const QString& name) const |
180 |
{ |
|
1509 | 181 |
return wconf->value(name).toString(); |
696 | 182 |
} |
183 |
||
718 | 184 |
void SelWeaponWidget::deleteWeaponsName() |
185 |
{ |
|
1509 | 186 |
if (curWeaponsName == "") return; |
724 | 187 |
|
1509 | 188 |
if (curWeaponsName == "Default") { |
189 |
QMessageBox impossible(QMessageBox::Warning, QMessageBox::tr("Weapons"), QMessageBox::tr("Can not delete default weapon set")); |
|
190 |
impossible.exec(); |
|
191 |
return; |
|
192 |
} |
|
193 |
||
194 |
QMessageBox reallyDelete(QMessageBox::Question, QMessageBox::tr("Weapons"), QMessageBox::tr("Really delete this weapon set?"), QMessageBox::Ok | QMessageBox::Cancel); |
|
195 |
||
196 |
if (reallyDelete.exec() == QMessageBox::Ok) { |
|
197 |
wconf->remove(curWeaponsName); |
|
198 |
emit weaponsDeleted(); |
|
199 |
} |
|
718 | 200 |
} |
201 |
||
1509 | 202 |
void SelWeaponWidget::setWeaponsName(const QString& name) |
694 | 203 |
{ |
1509 | 204 |
if(name != "" && wconf->contains(name)) { |
205 |
setWeapons(wconf->value(name).toString()); |
|
206 |
} |
|
694 | 207 |
|
1509 | 208 |
curWeaponsName = name; |
717 | 209 |
|
1509 | 210 |
m_name->setText(name); |
694 | 211 |
} |
212 |
||
213 |
QStringList SelWeaponWidget::getWeaponNames() const |
|
214 |
{ |
|
1509 | 215 |
return wconf->allKeys(); |
694 | 216 |
} |