author | unc0rr |
Fri, 16 Oct 2009 13:37:37 +0000 | |
changeset 2485 | 89568e997baa |
parent 2471 | 1cfc9f897616 |
child 2860 | 13a53315ae18 |
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> |
2467 | 32 |
#include <math.h> |
624 | 33 |
|
629 | 34 |
QImage getAmmoImage(int num) |
35 |
{ |
|
1509 | 36 |
static QImage ammo(":Ammos.png"); |
2471
1cfc9f897616
properly do offsets into mult-column ammo image (forgot y)
nemo
parents:
2467
diff
changeset
|
37 |
int x = floor(num/(ammo.height()/32)); |
1cfc9f897616
properly do offsets into mult-column ammo image (forgot y)
nemo
parents:
2467
diff
changeset
|
38 |
int y = (num-((ammo.height()/32)*x))*32; |
1cfc9f897616
properly do offsets into mult-column ammo image (forgot y)
nemo
parents:
2467
diff
changeset
|
39 |
x*=32; |
1cfc9f897616
properly do offsets into mult-column ammo image (forgot y)
nemo
parents:
2467
diff
changeset
|
40 |
return ammo.copy(x, y, 32, 32); |
629 | 41 |
} |
42 |
||
2369
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
43 |
SelWeaponItem::SelWeaponItem(bool allowInfinite, int iconNum, int wNum, QWidget* parent) : |
1509 | 44 |
QWidget(parent) |
629 | 45 |
{ |
1509 | 46 |
QHBoxLayout* hbLayout = new QHBoxLayout(this); |
47 |
hbLayout->setSpacing(1); |
|
48 |
hbLayout->setMargin(1); |
|
2377 | 49 |
|
1509 | 50 |
QLabel* lbl = new QLabel(this); |
51 |
lbl->setPixmap(QPixmap::fromImage(getAmmoImage(iconNum))); |
|
52 |
lbl->setMaximumWidth(30); |
|
53 |
lbl->setGeometry(0, 0, 30, 30); |
|
54 |
hbLayout->addWidget(lbl); |
|
639 | 55 |
|
1509 | 56 |
item = new WeaponItem(QImage(":/res/ammopic.png"), this); |
57 |
item->setItemsNum(wNum); |
|
2369
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
58 |
item->setInfinityState(allowInfinite); |
1509 | 59 |
hbLayout->addWidget(item); |
639 | 60 |
|
1509 | 61 |
hbLayout->setStretchFactor(lbl, 1); |
62 |
hbLayout->setStretchFactor(item, 99); |
|
63 |
hbLayout->setAlignment(lbl, Qt::AlignLeft | Qt::AlignVCenter); |
|
64 |
hbLayout->setAlignment(item, Qt::AlignLeft | Qt::AlignVCenter); |
|
629 | 65 |
} |
612
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
66 |
|
683 | 67 |
void SelWeaponItem::setItemsNum(const unsigned char num) |
68 |
{ |
|
1509 | 69 |
item->setItemsNum(num); |
683 | 70 |
} |
71 |
||
681 | 72 |
unsigned char SelWeaponItem::getItemsNum() const |
73 |
{ |
|
1509 | 74 |
return item->getItemsNum(); |
681 | 75 |
} |
76 |
||
683 | 77 |
SelWeaponWidget::SelWeaponWidget(int numItems, QWidget* parent) : |
1904 | 78 |
QFrame(parent), |
79 |
m_numItems(numItems) |
|
612
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
80 |
{ |
1509 | 81 |
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
|
82 |
|
1967 | 83 |
for(int i = 0; i < cDefaultAmmos.size(); ++i) |
84 |
wconf->setValue(cDefaultAmmos[i].first, cDefaultAmmos[i].second); |
|
694 | 85 |
|
1576
a02353129a41
Check for deprecated ammo schemes at startup and delete them
unc0rr
parents:
1509
diff
changeset
|
86 |
QStringList keys = wconf->allKeys(); |
a02353129a41
Check for deprecated ammo schemes at startup and delete them
unc0rr
parents:
1509
diff
changeset
|
87 |
for(int i = 0; i < keys.size(); i++) |
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 |
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
|
90 |
wconf->remove(keys[i]); |
a02353129a41
Check for deprecated ammo schemes at startup and delete them
unc0rr
parents:
1509
diff
changeset
|
91 |
} |
a02353129a41
Check for deprecated ammo schemes at startup and delete them
unc0rr
parents:
1509
diff
changeset
|
92 |
|
a02353129a41
Check for deprecated ammo schemes at startup and delete them
unc0rr
parents:
1509
diff
changeset
|
93 |
QString currentState = *cDefaultAmmoStore; |
683 | 94 |
|
2369
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
95 |
QTabWidget * tbw = new QTabWidget(this); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
96 |
QWidget * page1 = new QWidget(this); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
97 |
p1Layout = new QGridLayout(page1); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
98 |
p1Layout->setSpacing(1); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
99 |
p1Layout->setMargin(1); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
100 |
QWidget * page2 = new QWidget(this); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
101 |
p2Layout = new QGridLayout(page2); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
102 |
p2Layout->setSpacing(1); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
103 |
p2Layout->setMargin(1); |
2377 | 104 |
|
2369
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
105 |
tbw->addTab(page1, tr("Weapon set")); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
106 |
tbw->addTab(page2, tr("Probabilities")); |
2377 | 107 |
|
2369
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
108 |
QGridLayout * pageLayout = new QGridLayout(this); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
109 |
pageLayout->addWidget(tbw); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
110 |
|
629 | 111 |
|
1509 | 112 |
int j = -1; |
113 |
int i = 0, k = 0; |
|
114 |
for(; i < m_numItems; ++i) { |
|
115 |
if (i == 6) continue; |
|
116 |
if (k % 4 == 0) ++j; |
|
2369
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
117 |
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
|
118 |
weaponItems[i].append(swi); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
119 |
p1Layout->addWidget(swi, j, k % 4); |
2377 | 120 |
|
2369
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
121 |
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
|
122 |
weaponItems[i].append(pwi); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
123 |
p2Layout->addWidget(pwi, j, k % 4); |
2377 | 124 |
|
1509 | 125 |
++k; |
126 |
} |
|
694 | 127 |
|
1509 | 128 |
//pLayout->setRowStretch(5, 100); |
129 |
m_name = new QLineEdit(this); |
|
2369
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
130 |
pageLayout->addWidget(m_name, i, 0, 1, 5); |
612
333d095319de
abstract class for items container (hedgehogs num, bullets, etc.)
displacer
parents:
598
diff
changeset
|
131 |
} |
681 | 132 |
|
694 | 133 |
void SelWeaponWidget::setWeapons(const QString& ammo) |
683 | 134 |
{ |
1509 | 135 |
for(int i = 0; i < m_numItems; ++i) { |
136 |
twi::iterator it = weaponItems.find(i); |
|
137 |
if (it == weaponItems.end()) continue; |
|
2369
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
138 |
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
|
139 |
it.value()[1]->setItemsNum(ammo[m_numItems + i].digitValue()); |
1509 | 140 |
} |
141 |
update(); |
|
683 | 142 |
} |
143 |
||
144 |
void SelWeaponWidget::setDefault() |
|
145 |
{ |
|
1576
a02353129a41
Check for deprecated ammo schemes at startup and delete them
unc0rr
parents:
1509
diff
changeset
|
146 |
setWeapons(*cDefaultAmmoStore); |
683 | 147 |
} |
148 |
||
149 |
void SelWeaponWidget::save() |
|
150 |
{ |
|
1509 | 151 |
if (m_name->text() == "Default") { |
152 |
QMessageBox impossible(QMessageBox::Warning, QMessageBox::tr("Weapons"), QMessageBox::tr("Can not edit default weapon set")); |
|
153 |
impossible.exec(); |
|
154 |
return; |
|
155 |
} |
|
2377 | 156 |
|
1509 | 157 |
if (m_name->text() == "") return; |
2377 | 158 |
|
2369
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
159 |
QString state1; |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
160 |
QString state2; |
2377 | 161 |
|
1509 | 162 |
for(int i = 0; i < m_numItems; ++i) { |
163 |
twi::const_iterator it = weaponItems.find(i); |
|
2371 | 164 |
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
|
165 |
state1.append(QString::number(num)); |
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
166 |
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
|
167 |
state2.append(QString::number(prob)); |
1509 | 168 |
} |
169 |
if (curWeaponsName != "") { |
|
170 |
// remove old entry |
|
171 |
wconf->remove(curWeaponsName); |
|
172 |
} |
|
2369
c3eb11f1ab3a
Implement probability editor for weapon schemes (engine doesn't support that yet)
unc0rr
parents:
1967
diff
changeset
|
173 |
wconf->setValue(m_name->text(), state1 + state2); |
1509 | 174 |
emit weaponsChanged(); |
683 | 175 |
} |
176 |
||
681 | 177 |
int SelWeaponWidget::operator [] (unsigned int weaponIndex) const |
178 |
{ |
|
1509 | 179 |
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
|
180 |
return it == weaponItems.end() ? 9 : it.value()[0]->getItemsNum(); |
681 | 181 |
} |
182 |
||
696 | 183 |
QString SelWeaponWidget::getWeaponsString(const QString& name) const |
184 |
{ |
|
1509 | 185 |
return wconf->value(name).toString(); |
696 | 186 |
} |
187 |
||
718 | 188 |
void SelWeaponWidget::deleteWeaponsName() |
189 |
{ |
|
1509 | 190 |
if (curWeaponsName == "") return; |
724 | 191 |
|
1509 | 192 |
if (curWeaponsName == "Default") { |
193 |
QMessageBox impossible(QMessageBox::Warning, QMessageBox::tr("Weapons"), QMessageBox::tr("Can not delete default weapon set")); |
|
194 |
impossible.exec(); |
|
195 |
return; |
|
196 |
} |
|
197 |
||
198 |
QMessageBox reallyDelete(QMessageBox::Question, QMessageBox::tr("Weapons"), QMessageBox::tr("Really delete this weapon set?"), QMessageBox::Ok | QMessageBox::Cancel); |
|
2377 | 199 |
|
1509 | 200 |
if (reallyDelete.exec() == QMessageBox::Ok) { |
201 |
wconf->remove(curWeaponsName); |
|
202 |
emit weaponsDeleted(); |
|
203 |
} |
|
718 | 204 |
} |
205 |
||
1509 | 206 |
void SelWeaponWidget::setWeaponsName(const QString& name) |
694 | 207 |
{ |
1509 | 208 |
if(name != "" && wconf->contains(name)) { |
209 |
setWeapons(wconf->value(name).toString()); |
|
210 |
} |
|
694 | 211 |
|
1509 | 212 |
curWeaponsName = name; |
717 | 213 |
|
1509 | 214 |
m_name->setText(name); |
694 | 215 |
} |
216 |
||
217 |
QStringList SelWeaponWidget::getWeaponNames() const |
|
218 |
{ |
|
1509 | 219 |
return wconf->allKeys(); |
694 | 220 |
} |