1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
#include <qlabel.h>
#include <qslider.h>
#include <kconfig.h>
#include "floater.h"
void FloaterGuide::aboutToDelete()
{
game->removeItem(floater);
aboutToDie();
floater->aboutToDie();
delete floater;
almostDead = true;
}
void FloaterGuide::aboutToDie()
{
if (almostDead)
return;
else
Wall::aboutToDie();
}
void FloaterGuide::moveBy(double dx, double dy)
{
Wall::moveBy(dx, dy);
if (floater)
floater->reset();
}
void FloaterGuide::setPoints(int xa, int ya, int xb, int yb)
{
if (fabs(xa - xb) > 0 || fabs(ya - yb) > 0)
{
Wall::setPoints(xa, ya, xb, yb);
if (floater)
floater->reset();
}
}
Config *FloaterGuide::config(QWidget *parent)
{
return floater->config(parent);
}
/////////////////////////
Floater::Floater(QRect rect, QCanvas *canvas)
: Bridge(rect, canvas), speedfactor(16)
{
wall = 0;
setEnabled(true);
noUpdateZ = false;
haventMoved = true;
wall = new FloaterGuide(this, canvas);
wall->setPoints(100, 100, 200, 200);
wall->setPen(QPen(wall->pen().color().light(), wall->pen().width() - 1));
move(wall->endPoint().x(), wall->endPoint().y());
setTopWallVisible(false);
setBotWallVisible(false);
setLeftWallVisible(false);
setRightWallVisible(false);
newSize(width(), height());
moveBy(0, 0);
setSpeed(0);
editModeChanged(false);
reset();
}
void Floater::setGame(KolfGame *game)
{
Bridge::setGame(game);
wall->setGame(game);
}
void Floater::editModeChanged(bool changed)
{
if (changed)
wall->editModeChanged(true);
Bridge::editModeChanged(changed);
wall->setVisible(changed);
}
void Floater::advance(int phase)
{
if (!isEnabled())
return;
Bridge::advance(phase);
if (phase == 1 && (xVelocity() || yVelocity()))
{
if (Vector(origin, QPoint(x(), y())).magnitude() > vector.magnitude())
{
vector.setDirection(vector.direction() + M_PI);
origin = (origin == wall->startPoint()? wall->endPoint() : wall->startPoint());
setVelocity(-xVelocity(), -yVelocity());
}
}
}
void Floater::reset()
{
QPoint start = wall->startPoint() + QPoint(wall->x(), wall->y());
QPoint end = wall->endPoint() + QPoint(wall->x(), wall->y());
vector = Vector(end, start);
origin = end;
move(origin.x(), origin.y());
setSpeed(speed);
}
QPtrList<QCanvasItem> Floater::moveableItems() const
{
QPtrList<QCanvasItem> ret(wall->moveableItems());
ret.append(wall);
ret.append(point);
return ret;
}
void Floater::aboutToDie()
{
if (wall)
wall->setVisible(false);
Bridge::aboutToDie();
setEnabled(false);
}
void Floater::setSpeed(int news)
{
if (!wall || news < 0)
return;
speed = news;
if (news == 0)
{
setVelocity(0, 0);
return;
}
const double factor = (double)speed / 3.5;
setVelocity(-cos(vector.direction()) * factor, -sin(vector.direction()) * factor);
}
void Floater::aboutToSave()
{
setVelocity(0, 0);
noUpdateZ = true;
move(wall->endPoint().x() + wall->x(), wall->endPoint().y() + wall->y());
noUpdateZ = false;
}
void Floater::savingDone()
{
setSpeed(speed);
}
void Floater::moveBy(double dx, double dy)
{
if (!isEnabled())
return;
QCanvasItemList l = collisions(false);
for (QCanvasItemList::Iterator it = l.begin(); it != l.end(); ++it)
{
CanvasItem *item = dynamic_cast<CanvasItem *>(*it);
if (!noUpdateZ && item && item->canBeMovedByOthers())
item->updateZ(this);
if ((*it)->z() >= z())
{
if (item && item->canBeMovedByOthers() && collidesWith(*it))
{
if ((*it)->rtti() == Rtti_Ball)
{
//((Ball *)(*it))->setState(Rolling);
(*it)->moveBy(dx, dy);
if (game && game->hasFocus() && !game->isEditing() && game->curBall() == (Ball *)(*it))
game->ballMoved();
}
else if ((*it)->rtti() != Rtti_Putter)
(*it)->moveBy(dx, dy);
}
}
}
point->dontMove();
point->move(x() + width(), y() + height());
// this call must come after we have tested for collisions, otherwise we skip them when saving!
// that's a bad thing
QCanvasRectangle::moveBy(dx, dy);
// because we don't do Bridge::moveBy();
topWall->move(x(), y());
botWall->move(x(), y() - 1);
leftWall->move(x(), y());
rightWall->move(x(), y());
if (game && game->isEditing())
game->updateHighlighter();
}
void Floater::saveState(StateDB *db)
{
db->setPoint(QPoint(x(), y()));
}
void Floater::loadState(StateDB *db)
{
const QPoint moveTo = db->point();
move(moveTo.x(), moveTo.y());
}
void Floater::save(KConfig *cfg)
{
cfg->writeEntry("speed", speed);
cfg->writeEntry("startPoint", QPoint(wall->startPoint().x() + wall->x(), wall->startPoint().y() + wall->y()));
cfg->writeEntry("endPoint", QPoint(wall->endPoint().x() + wall->x(), wall->endPoint().y() + wall->y()));
doSave(cfg);
}
void Floater::load(KConfig *cfg)
{
move(firstPoint.x(), firstPoint.y());
QPoint start(wall->startPoint() + QPoint(wall->x(), wall->y()));
start = cfg->readPointEntry("startPoint", &start);
QPoint end(wall->endPoint() + QPoint(wall->x(), wall->y()));
end = cfg->readPointEntry("endPoint", &end);
wall->setPoints(start.x(), start.y(), end.x(), end.y());
wall->move(0, 0);
setSpeed(cfg->readNumEntry("speed", -1));
doLoad(cfg);
reset();
}
void Floater::firstMove(int x, int y)
{
firstPoint = QPoint(x, y);
}
/////////////////////////
FloaterConfig::FloaterConfig(Floater *floater, QWidget *parent)
: BridgeConfig(floater, parent)
{
this->floater = floater;
m_vlayout->addStretch();
m_vlayout->addWidget(new QLabel(i18n("Moving speed"), this));
QHBoxLayout *hlayout = new QHBoxLayout(m_vlayout, spacingHint());
hlayout->addWidget(new QLabel(i18n("Slow"), this));
QSlider *slider = new QSlider(0, 20, 2, floater->curSpeed(), Qt::Horizontal, this);
hlayout->addWidget(slider);
hlayout->addWidget(new QLabel(i18n("Fast"), this));
connect(slider, SIGNAL(valueChanged(int)), this, SLOT(speedChanged(int)));
}
void FloaterConfig::speedChanged(int news)
{
floater->setSpeed(news);
changed();
}
#include "floater.moc"
|