~neon/kolf/master

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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
/*
    Copyright (C) 2002-2005, Jason Katz-Brown <jasonkb@mit.edu>
    Copyright 2010 Stefan Majewsky <majewsky@gmx.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "obstacles.h"
#include "ball.h"
#include "game.h"
#include "shape.h"

#include <QCheckBox>
#include <QGridLayout>
#include <QLabel>
#include <QSlider>
#include <QTimer>
#include <KConfigGroup>
#include <KLineEdit>
#include <KRandom>
#include <KLocalizedString>
//BEGIN Kolf::Bumper

Kolf::Bumper::Bumper(QGraphicsItem* parent, b2World* world)
	: EllipticalCanvasItem(false, QStringLiteral("bumper_off"), parent, world)
{
	const int diameter = 20;
	setSize(QSizeF(diameter, diameter));
	setZBehavior(CanvasItem::IsRaisedByStrut, 4);
	setSimulationType(CanvasItem::NoSimulation);
}

bool Kolf::Bumper::collision(Ball* ball)
{
	const double maxSpeed = ball->getMaxBumperBounceSpeed();
	const double speed = qMin(maxSpeed, 1.8 + Vector(ball->velocity()).magnitude() * .9);
	ball->reduceMaxBumperBounceSpeed();

	Vector betweenVector(ball->pos() - pos());
	betweenVector.setMagnitudeDirection(speed,
		// add some randomness so we don't go indefinitely
		betweenVector.direction() + deg2rad((KRandom::random() % 3) - 1)
	);

	ball->setVelocity(betweenVector);
	ball->setState(Rolling);

	game->playSound(Sound::Bumper);

	setSpriteKey(QStringLiteral("bumper_on"));
	QTimer::singleShot(100, this, &Kolf::Bumper::turnBumperOff);
	return true;
}

void Kolf::Bumper::turnBumperOff()
{
	setSpriteKey(QStringLiteral("bumper_off"));
}

Kolf::Overlay* Kolf::Bumper::createOverlay()
{
	return new Kolf::Overlay(this, this);
}

//END Kolf::Bumper
//BEGIN Kolf::Wall

Kolf::Wall::Wall(QGraphicsItem* parent, b2World* world)
	: QGraphicsLineItem(QLineF(-15, 10, 15, -5), parent)
	, CanvasItem(world)
{
	setPen(QPen(Qt::darkRed, 3));
	setData(0, Rtti_NoCollision);
	//see also KolfGame::addBorderWall()
	setZBehavior(CanvasItem::FixedZValue, 5);

	m_shape = new Kolf::LineShape(line());
	addShape(m_shape);
}

void Kolf::Wall::load(KConfigGroup* cfgGroup)
{
	const QPoint start = cfgGroup->readEntry("startPoint", QPoint(-15, 10));
	const QPoint end = cfgGroup->readEntry("endPoint", QPoint(15, -5));
	setLine(QLineF(start, end));
}

void Kolf::Wall::save(KConfigGroup* cfgGroup)
{
	const QLineF line = this->line();
	cfgGroup->writeEntry("startPoint", line.p1().toPoint());
	cfgGroup->writeEntry("endPoint", line.p2().toPoint());
}

void Kolf::Wall::setVisible(bool visible)
{
	QGraphicsLineItem::setVisible(visible);
	setSimulationType(visible ? CanvasItem::CollisionSimulation : CanvasItem::NoSimulation);
}

void Kolf::Wall::setLine(const QLineF& line)
{
	QGraphicsLineItem::setLine(line);
	m_shape->setLine(line);
	propagateUpdate();
}

void Kolf::Wall::moveBy(double dx, double dy)
{
	QGraphicsLineItem::moveBy(dx, dy);
	CanvasItem::moveBy(dx, dy);
}

QPointF Kolf::Wall::getPosition() const
{
	return QGraphicsItem::pos();
}

Kolf::Overlay* Kolf::Wall::createOverlay()
{
	return new Kolf::WallOverlay(this);
}

//END Kolf::Wall
//BEGIN Kolf::WallOverlay

Kolf::WallOverlay::WallOverlay(Kolf::Wall* wall)
	: Kolf::Overlay(wall, wall)
	, m_handle1(new Kolf::OverlayHandle(Kolf::OverlayHandle::SquareShape, this))
	, m_handle2(new Kolf::OverlayHandle(Kolf::OverlayHandle::SquareShape, this))
{
	addHandle(m_handle1);
	addHandle(m_handle2);
	connect(m_handle1, &Kolf::OverlayHandle::moveRequest, this, &Kolf::WallOverlay::moveHandle);
	connect(m_handle2, &Kolf::OverlayHandle::moveRequest, this, &Kolf::WallOverlay::moveHandle);
}

void Kolf::WallOverlay::update()
{
	Kolf::Overlay::update();
	const QLineF line = dynamic_cast<Kolf::Wall*>(qitem())->line();
	m_handle1->setPos(line.p1());
	m_handle2->setPos(line.p2());
}

void Kolf::WallOverlay::moveHandle(const QPointF& handleScenePos)
{
	//TODO: code duplication to Kolf::FloaterOverlay
	QPointF handlePos = mapFromScene(handleScenePos);
	const QObject* handle = sender();
	//get handle positions
	QPointF handle1Pos = m_handle1->pos();
	QPointF handle2Pos = m_handle2->pos();
	if (handle == m_handle1)
		handle1Pos = handlePos;
	else if (handle == m_handle2)
		handle2Pos = handlePos;
	//ensure minimum length
	static const qreal minLength = Kolf::Overlay::MinimumObjectDimension;
	const QPointF posDiff = handle1Pos - handle2Pos;
	const qreal length = QLineF(QPointF(), posDiff).length();
	if (length < minLength)
	{
		const QPointF additionalExtent = posDiff * (minLength / length - 1);
		if (handle == m_handle1)
			handle1Pos += additionalExtent;
		else if (handle == m_handle2)
			handle2Pos -= additionalExtent;
	}
	//apply to item
	dynamic_cast<Kolf::Wall*>(qitem())->setLine(QLineF(handle1Pos, handle2Pos));
}

//END Kolf::WallOverlay
//BEGIN Kolf::RectangleItem

Kolf::RectangleItem::RectangleItem(const QString& type, QGraphicsItem* parent, b2World* world)
	: Tagaro::SpriteObjectItem(Kolf::renderer(), type, parent)
	, CanvasItem(world)
	, m_wallPen(QColor("#92772D").darker(), 3)
	, m_wallAllowed(Kolf::RectangleWallCount, true)
	, m_walls(Kolf::RectangleWallCount, 0)
	, m_shape(new Kolf::RectShape(QRectF(0, 0, 1, 1)))
{
	addShape(m_shape);
	setSimulationType(CanvasItem::NoSimulation);
	//default size
	setSize(type == QLatin1String("sign") ? QSize(110, 40) : QSize(80, 40));
}

Kolf::RectangleItem::~RectangleItem()
{
	qDeleteAll(m_walls);
}

bool Kolf::RectangleItem::hasWall(Kolf::WallIndex index) const
{
	return (bool) m_walls[index];
}

bool Kolf::RectangleItem::isWallAllowed(Kolf::WallIndex index) const
{
	return m_wallAllowed[index];
}

void Kolf::RectangleItem::setWall(Kolf::WallIndex index, bool hasWall)
{
	const bool oldHasWall = (bool) m_walls[index];
	if (oldHasWall == hasWall)
		return;
	if (hasWall && !m_wallAllowed[index])
		return;
	if (hasWall)
	{
		Kolf::Wall* wall = m_walls[index] = new Kolf::Wall(parentItem(), world());
		wall->setPos(pos());
		applyWallStyle(wall);
		updateWallPosition();
	}
	else
	{
		delete m_walls[index];
		m_walls[index] = 0;
	}
	propagateUpdate();
	emit wallChanged(index, hasWall, m_wallAllowed[index]);
}

void Kolf::RectangleItem::setWallAllowed(Kolf::WallIndex index, bool wallAllowed)
{
	m_wallAllowed[index] = wallAllowed;
	//delete wall if one exists at this position currently
	if (!wallAllowed)
		setWall(index, false);
	emit wallChanged(index, hasWall(index), wallAllowed);
}

void Kolf::RectangleItem::updateWallPosition()
{
	const QRectF rect(QPointF(), size());
	Kolf::Wall* const topWall = m_walls[Kolf::TopWallIndex];
	Kolf::Wall* const leftWall = m_walls[Kolf::LeftWallIndex];
	Kolf::Wall* const rightWall = m_walls[Kolf::RightWallIndex];
	Kolf::Wall* const bottomWall = m_walls[Kolf::BottomWallIndex];
	if (topWall)
		topWall->setLine(QLineF(rect.topLeft(), rect.topRight()));
	if (leftWall)
		leftWall->setLine(QLineF(rect.topLeft(), rect.bottomLeft()));
	if (rightWall)
		rightWall->setLine(QLineF(rect.topRight(), rect.bottomRight()));
	if (bottomWall)
		bottomWall->setLine(QLineF(rect.bottomLeft(), rect.bottomRight()));
}

void Kolf::RectangleItem::setSize(const QSizeF& size)
{
	Tagaro::SpriteObjectItem::setSize(size);
	m_shape->setRect(QRectF(QPointF(), size));
	updateWallPosition();
	propagateUpdate();
}

QPointF Kolf::RectangleItem::getPosition() const
{
	return QGraphicsItem::pos();
}

void Kolf::RectangleItem::moveBy(double dx, double dy)
{
	Tagaro::SpriteObjectItem::moveBy(dx, dy);
	//move myself
	const QPointF pos = this->pos();
	foreach (Kolf::Wall* wall, m_walls)
		if (wall)
			wall->setPos(pos);
	//update Z order
	CanvasItem::moveBy(dx, dy);
	foreach (QGraphicsItem* qitem, collidingItems())
	{
		CanvasItem* citem = dynamic_cast<CanvasItem*>(qitem);
		if (citem)
			citem->updateZ(qitem);
	}
}

void Kolf::RectangleItem::setWallColor(const QColor& color)
{
	m_wallPen = QPen(color.darker(), 3);
	foreach (Kolf::Wall* wall, m_walls)
		applyWallStyle(wall);
}

void Kolf::RectangleItem::applyWallStyle(Kolf::Wall* wall, bool adjustPainting)
{
	if (!wall) //explicitly allowed, see e.g. setWallColor()
		return;
	if (adjustPainting)
		wall->setPen(m_wallPen);
	wall->setZBehavior(CanvasItem::IsRaisedByStrut, 3);
	wall->setStaticStrut(this);
}

static const char* wallPropNames[] = { "topWallVisible", "leftWallVisible", "rightWallVisible", "botWallVisible" };

void Kolf::RectangleItem::load(KConfigGroup* group)
{
	QSize size = Tagaro::SpriteObjectItem::size().toSize();
	size.setWidth(group->readEntry("width", size.width()));
	size.setHeight(group->readEntry("height", size.height()));
	setSize(size);
	for (int i = 0; i < Kolf::RectangleWallCount; ++i)
	{
		bool hasWall = this->hasWall((Kolf::WallIndex) i);
		hasWall = group->readEntry(wallPropNames[i], hasWall);
		setWall((Kolf::WallIndex) i, hasWall);
	}
}

void Kolf::RectangleItem::save(KConfigGroup* group)
{
	const QSize size = Tagaro::SpriteObjectItem::size().toSize();
	group->writeEntry("width", size.width());
	group->writeEntry("height", size.height());
	for (int i = 0; i < Kolf::RectangleWallCount; ++i)
	{
		const bool hasWall = this->hasWall((Kolf::WallIndex) i);
		group->writeEntry(wallPropNames[i], hasWall);
	}
}

Config* Kolf::RectangleItem::config(QWidget* parent)
{
	return new Kolf::RectangleConfig(this, parent);
}

Kolf::Overlay* Kolf::RectangleItem::createOverlay()
{
	return new Kolf::RectangleOverlay(this);
}

//END Kolf::RectangleItem
//BEGIN Kolf::RectangleOverlay

Kolf::RectangleOverlay::RectangleOverlay(Kolf::RectangleItem* item)
	: Kolf::Overlay(item, item)
{
	//TODO: code duplication to Kolf::LandscapeOverlay and Kolf::SlopeOverlay
	for (int i = 0; i < 4; ++i)
	{
		Kolf::OverlayHandle* handle = new Kolf::OverlayHandle(Kolf::OverlayHandle::CircleShape, this);
		m_handles << handle;
		addHandle(handle);
		connect(handle, &Kolf::OverlayHandle::moveRequest, this, &Kolf::RectangleOverlay::moveHandle);
	}
}

void Kolf::RectangleOverlay::update()
{
	Kolf::Overlay::update();
	const QRectF rect = qitem()->boundingRect();
	m_handles[0]->setPos(rect.topLeft());
	m_handles[1]->setPos(rect.topRight());
	m_handles[2]->setPos(rect.bottomLeft());
	m_handles[3]->setPos(rect.bottomRight());
}

void Kolf::RectangleOverlay::moveHandle(const QPointF& handleScenePos)
{
	Kolf::OverlayHandle* handle = qobject_cast<Kolf::OverlayHandle*>(sender());
	const int handleIndex = m_handles.indexOf(handle);
	Kolf::RectangleItem* item = dynamic_cast<Kolf::RectangleItem*>(qitem());
	const QPointF handlePos = mapFromScene(handleScenePos);
	//modify bounding rect using new handlePos
	QRectF rect(QPointF(), item->size());
	if (handleIndex % 2 == 0)
		rect.setLeft(qMin(handlePos.x(), rect.right()));
	else
		rect.setRight(qMax(handlePos.x(), rect.left()));
	if (handleIndex < 2)
		rect.setTop(qMin(handlePos.y(), rect.bottom()));
	else
		rect.setBottom(qMax(handlePos.y(), rect.top()));
	item->moveBy(rect.x(), rect.y());
	item->setSize(rect.size());
}

//END Kolf::RectangleOverlay
//BEGIN Kolf::RectangleConfig

Kolf::RectangleConfig::RectangleConfig(Kolf::RectangleItem* item, QWidget* parent)
	: Config(parent)
	, m_layout(new QGridLayout(this))
	, m_wallCheckBoxes(Kolf::RectangleWallCount, 0)
	, m_item(item)
{
	static const char* captions[] = { I18N_NOOP("&Top"), I18N_NOOP("&Left"), I18N_NOOP("&Right"), I18N_NOOP("&Bottom") };
	for (int i = 0; i < Kolf::RectangleWallCount; ++i)
	{
		QCheckBox* checkBox = m_wallCheckBoxes[i] = new QCheckBox(i18n(captions[i]), this);
		checkBox->setEnabled(item->isWallAllowed((Kolf::WallIndex) i));
		checkBox->setChecked(item->hasWall((Kolf::WallIndex) i));
		connect(checkBox, &QCheckBox::toggled, this, &Kolf::RectangleConfig::setWall);
	}
	connect(item, &Kolf::RectangleItem::wallChanged, this, &Kolf::RectangleConfig::wallChanged);
	m_layout->addWidget(new QLabel(i18n("Walls on:")), 0, 0);
	m_layout->addWidget(m_wallCheckBoxes[0], 0, 1);
	m_layout->addWidget(m_wallCheckBoxes[1], 1, 0);
	m_layout->addWidget(m_wallCheckBoxes[2], 1, 2);
	m_layout->addWidget(m_wallCheckBoxes[3], 1, 1);
	m_layout->setRowStretch(2, 10);
	//Kolf::Sign does not have a special Config class
	Kolf::Sign* sign = qobject_cast<Kolf::Sign*>(item);
	if (sign)
	{
		m_layout->addWidget(new QLabel(i18n("Sign HTML:")), 3, 0, 1, 3);
		KLineEdit* edit = new KLineEdit(sign->text(), this);
		m_layout->addWidget(edit, 4, 0, 1, 3);
		connect(edit, &KLineEdit::textChanged, sign, &Kolf::Sign::setText);
	}
	//Kolf::Windmill does not have a special Config class
	Kolf::Windmill* windmill = qobject_cast<Kolf::Windmill*>(item);
	if (windmill)
	{
		QCheckBox* checkBox = new QCheckBox(i18n("Windmill on top"), this);
		m_layout->addWidget(checkBox, 4, 0, 1, 3);
		checkBox->setChecked(windmill->guardAtTop());
		connect(checkBox, &QCheckBox::toggled, windmill, &Kolf::Windmill::setGuardAtTop);
		QHBoxLayout* hlayout = new QHBoxLayout;
		m_layout->addLayout(hlayout, 5, 0, 1, 3);
		QLabel* label1 = new QLabel(i18n("Slow"), this);
		hlayout->addWidget(label1);
		QSlider* slider = new QSlider(Qt::Horizontal, this);
		hlayout->addWidget(slider);
		QLabel* label2 = new QLabel(i18n("Fast"), this);
		hlayout->addWidget(label2);
		slider->setRange(1, 10);
		slider->setPageStep(1);
		slider->setValue(windmill->speed());
		connect(slider, &QSlider::valueChanged, windmill, &Kolf::Windmill::setSpeed);
	}
	//Kolf::Floater does not have a special Config class
	Kolf::Floater* floater = qobject_cast<Kolf::Floater*>(item);
	if (floater)
	{
		m_layout->addWidget(new QLabel(i18n("Moving speed"), this), 4, 0, 1, 3);
		QHBoxLayout* hlayout = new QHBoxLayout;
		m_layout->addLayout(hlayout, 5, 0, 1, 3);
		QLabel* label1 = new QLabel(i18n("Slow"), this);
		hlayout->addWidget(label1);
		QSlider* slider = new QSlider(Qt::Horizontal, this);
		hlayout->addWidget(slider);
		QLabel* label2 = new QLabel(i18n("Fast"), this);
		hlayout->addWidget(label2);
		slider->setRange(0, 20);
		slider->setPageStep(2);
		slider->setValue(floater->speed());
		connect(slider, &QSlider::valueChanged, floater, &Kolf::Floater::setSpeed);
	}
}

void Kolf::RectangleConfig::setWall(bool hasWall)
{
	const int wallIndex = m_wallCheckBoxes.indexOf(qobject_cast<QCheckBox*>(sender()));
	if (wallIndex >= 0)
	{
		m_item->setWall((Kolf::WallIndex) wallIndex, hasWall);
		changed();
	}
}

void Kolf::RectangleConfig::wallChanged(Kolf::WallIndex index, bool hasWall, bool wallAllowed)
{
	m_wallCheckBoxes[index]->setEnabled(wallAllowed);
	m_wallCheckBoxes[index]->setChecked(hasWall);
}

//END Kolf::RectangleConfig
//BEGIN Kolf::Bridge

Kolf::Bridge::Bridge(QGraphicsItem* parent, b2World* world)
	: Kolf::RectangleItem(QStringLiteral("bridge"), parent, world)
{
	setZBehavior(CanvasItem::IsStrut, 0);
}

bool Kolf::Bridge::collision(Ball* ball)
{
	ball->setFrictionMultiplier(.63);
	return false;
}

//END Kolf::Bridge
//BEGIN Kolf::Floater

Kolf::Floater::Floater(QGraphicsItem* parent, b2World* world)
	: Kolf::RectangleItem(QStringLiteral("floater"), parent, world)
	, m_motionLine(QLineF(200, 200, 100, 100))
	, m_speed(0)
	, m_velocity(0)
	, m_position(0)
	, m_moveByMovesMotionLine(true)
	, m_animated(true)
{
	setMlPosition(m_position);
	setZBehavior(CanvasItem::IsStrut, 0);
}

void Kolf::Floater::editModeChanged(bool editing)
{
	Kolf::RectangleItem::editModeChanged(editing);
	m_animated = !editing;
	if (editing)
		setMlPosition(0);
}

void Kolf::Floater::moveBy(double dx, double dy)
{
	moveItemsOnStrut(QPointF(dx, dy));
	Kolf::RectangleItem::moveBy(dx, dy);
	if (m_moveByMovesMotionLine)
		m_motionLine.translate(dx, dy);
	propagateUpdate();
}

QLineF Kolf::Floater::motionLine() const
{
	return m_motionLine;
}

void Kolf::Floater::setMotionLine(const QLineF& motionLine)
{
	m_motionLine = motionLine;
	setMlPosition(m_position);
	propagateUpdate();
}

void Kolf::Floater::setMlPosition(qreal position)
{
	m_moveByMovesMotionLine = false;
	setPosition(m_motionLine.pointAt(position));
	m_position = position;
	m_moveByMovesMotionLine = true;
}

int Kolf::Floater::speed() const
{
	return m_speed;
}

void Kolf::Floater::setSpeed(int speed)
{
	m_speed = speed;
	const qreal velocity = speed / 3.5;
	m_velocity = (m_velocity < 0) ? -velocity : velocity;
	propagateUpdate();
}

void Kolf::Floater::advance(int phase)
{
	if (phase != 1 || !m_animated)
		return;
	//determine movement step
	const qreal mlLength = m_motionLine.length();
	const qreal parameterDiff = m_velocity / mlLength;
	//determine new position (mirror on end point if end point passed)
	m_position += parameterDiff;
	if (m_position < 0)
	{
		m_velocity = qAbs(m_velocity);
		m_position = -m_position;
	}
	else if (m_position > 1)
	{
		m_velocity = -qAbs(m_velocity);
		m_position = 2 - m_position;
	}
	//apply position
	setMlPosition(m_position);
}

void Kolf::Floater::load(KConfigGroup* group)
{
	Kolf::RectangleItem::load(group);
	QLineF motionLine = m_motionLine;
	motionLine.setP1(group->readEntry("startPoint", m_motionLine.p1()));
	motionLine.setP2(group->readEntry("endPoint", m_motionLine.p2()));
	setMotionLine(motionLine);
	setSpeed(group->readEntry("speed", m_speed));
}

void Kolf::Floater::save(KConfigGroup* group)
{
	Kolf::RectangleItem::save(group);
	group->writeEntry("startPoint", m_motionLine.p1());
	group->writeEntry("endPoint", m_motionLine.p2());
	group->writeEntry("speed", m_speed);
}

Kolf::Overlay* Kolf::Floater::createOverlay()
{
	return new Kolf::FloaterOverlay(this);
}

//END Kolf::Floater
//BEGIN Kolf::FloaterOverlay

Kolf::FloaterOverlay::FloaterOverlay(Kolf::Floater* floater)
	: Kolf::RectangleOverlay(floater)
	, m_handle1(new Kolf::OverlayHandle(Kolf::OverlayHandle::SquareShape, this))
	, m_handle2(new Kolf::OverlayHandle(Kolf::OverlayHandle::SquareShape, this))
	, m_motionLineItem(new QGraphicsLineItem(this))
{
	addHandle(m_handle1);
	addHandle(m_handle2);
	connect(m_handle1, &Kolf::OverlayHandle::moveRequest, this, &Kolf::FloaterOverlay::moveMotionLineHandle);
	connect(m_handle2, &Kolf::OverlayHandle::moveRequest, this, &Kolf::FloaterOverlay::moveMotionLineHandle);
	addHandle(m_motionLineItem);
	QPen pen = m_motionLineItem->pen();
	pen.setStyle(Qt::DashLine);
	m_motionLineItem->setPen(pen);
}

void Kolf::FloaterOverlay::update()
{
	Kolf::RectangleOverlay::update();
	const QLineF line = dynamic_cast<Kolf::Floater*>(qitem())->motionLine().translated(-qitem()->pos());
	m_handle1->setPos(line.p1());
	m_handle2->setPos(line.p2());
	m_motionLineItem->setLine(line);
}

void Kolf::FloaterOverlay::moveMotionLineHandle(const QPointF& handleScenePos)
{
	//TODO: code duplication to Kolf::WallOverlay
	QPointF handlePos = mapFromScene(handleScenePos) + qitem()->pos();
	const QObject* handle = sender();
	//get handle positions
	QPointF handle1Pos = m_handle1->pos() + qitem()->pos();
	QPointF handle2Pos = m_handle2->pos() + qitem()->pos();
	if (handle == m_handle1)
		handle1Pos = handlePos;
	else if (handle == m_handle2)
		handle2Pos = handlePos;
	//ensure minimum length
	static const qreal minLength = Kolf::Overlay::MinimumObjectDimension;
	const QPointF posDiff = handle1Pos - handle2Pos;
	const qreal length = QLineF(QPointF(), posDiff).length();
	if (length < minLength)
	{
		const QPointF additionalExtent = posDiff * (minLength / length - 1);
		if (handle == m_handle1)
			handle1Pos += additionalExtent;
		else if (handle == m_handle2)
			handle2Pos -= additionalExtent;
	}
	//apply to item
	dynamic_cast<Kolf::Floater*>(qitem())->setMotionLine(QLineF(handle1Pos, handle2Pos));
}

//END Kolf::FloaterOverlay
//BEGIN Kolf::Sign

Kolf::Sign::Sign(QGraphicsItem* parent, b2World* world)
	: Kolf::RectangleItem(QStringLiteral("sign"), parent, world)
	, m_text(i18n("New Text"))
	, m_textItem(new QGraphicsTextItem(m_text, this))
{
	setZBehavior(CanvasItem::FixedZValue, 3);
	setWallColor(Qt::black);
	for (int i = 0; i < Kolf::RectangleWallCount; ++i)
		setWall((Kolf::WallIndex) i, true);
	//Z value 1 should be enough to keep text above overlay
	m_textItem->setZValue(1);
	m_textItem->setAcceptedMouseButtons(0);
	//TODO: activate QGraphicsItem::ItemClipsChildrenToShape flag after
	//refactoring (only after it is clear that the text is the only child)
}

QString Kolf::Sign::text() const
{
	return m_text;
}

void Kolf::Sign::setText(const QString& text)
{
	m_text = text;
	m_textItem->setHtml(text);
}

void Kolf::Sign::setSize(const QSizeF& size)
{
	Kolf::RectangleItem::setSize(size);
	m_textItem->setTextWidth(size.width());
}

void Kolf::Sign::load(KConfigGroup* group)
{
	Kolf::RectangleItem::load(group);
	setText(group->readEntry("Comment", m_text));
}

void Kolf::Sign::save(KConfigGroup* group)
{
	Kolf::RectangleItem::save(group);
	group->writeEntry("Comment", m_text);
}

//END Kolf::Sign
//BEGIN Kolf::Windmill

Kolf::Windmill::Windmill(QGraphicsItem* parent, b2World* world)
	: Kolf::RectangleItem(QStringLiteral("windmill"), parent, world)
	  , m_leftWall(new Kolf::Wall(parent, world))
	  , m_rightWall(new Kolf::Wall(parent, world))
	  , m_guardWall(new Kolf::Wall(parent, world))
	  , m_guardAtTop(false)
	  , m_speed(0), m_velocity(0)
{
	setZBehavior(CanvasItem::IsStrut, 0);
	setSpeed(5); //initialize m_speed and m_velocity properly
	applyWallStyle(m_leftWall);
	applyWallStyle(m_rightWall);
	applyWallStyle(m_guardWall, false); //Z-ordering!
	m_guardWall->setPen(QPen(Qt::black, 5));
	setWall(Kolf::TopWallIndex, false);
	setWall(Kolf::LeftWallIndex, true);
	setWall(Kolf::RightWallIndex, true);
	setWallAllowed(Kolf::BottomWallIndex, false);
	m_guardWall->setLine(QLineF());
	updateWallPosition();
}

Kolf::Windmill::~Windmill()
{
	delete m_leftWall;
	delete m_rightWall;
	delete m_guardWall;
}

bool Kolf::Windmill::guardAtTop() const
{
	return m_guardAtTop;
}

void Kolf::Windmill::setGuardAtTop(bool guardAtTop)
{
	if (m_guardAtTop == guardAtTop)
		return;
	m_guardAtTop = guardAtTop;
	//exchange top and bottom walls
	if (guardAtTop)
	{
		const bool hasWall = this->hasWall(Kolf::TopWallIndex);
		setWallAllowed(Kolf::BottomWallIndex, true);
		setWallAllowed(Kolf::TopWallIndex, false);
		setWall(Kolf::BottomWallIndex, hasWall);
	}
	else
	{
		const bool hasWall = this->hasWall(Kolf::BottomWallIndex);
		setWallAllowed(Kolf::BottomWallIndex, false);
		setWallAllowed(Kolf::TopWallIndex, true);
		setWall(Kolf::TopWallIndex, hasWall);
	}
	//recalculate position of guard walls etc.
	updateWallPosition();
	propagateUpdate();
}

int Kolf::Windmill::speed() const
{
	return m_speed;
}

void Kolf::Windmill::setSpeed(int speed)
{
	m_speed = speed;
	const qreal velocity = speed / 3.0;
	m_velocity = (m_velocity < 0) ? -velocity : velocity;
	propagateUpdate();
}

void Kolf::Windmill::advance(int phase)
{
	if (phase == 1)
	{
		QLineF guardLine = m_guardWall->line().translated(m_velocity, 0);
		const qreal maxX = qMax(guardLine.x1(), guardLine.x2());
		const qreal minX = qMin(guardLine.x1(), guardLine.x2());
		QRectF rect(QPointF(), size());
		if (minX < rect.left())
		{
			guardLine.translate(rect.left() - minX, 0);
			m_velocity = qAbs(m_velocity);
		}
		else if (maxX > rect.right())
		{
			guardLine.translate(rect.right() - maxX, 0);
			m_velocity = -qAbs(m_velocity);
		}
		m_guardWall->setLine(guardLine);
	}
}

void Kolf::Windmill::moveBy(double dx, double dy)
{
	Kolf::RectangleItem::moveBy(dx, dy);
	const QPointF pos = this->pos();
	m_leftWall->setPos(pos);
	m_rightWall->setPos(pos);
	m_guardWall->setPos(pos);
}

void Kolf::Windmill::updateWallPosition()
{
	Kolf::RectangleItem::updateWallPosition();
	//parametrize position of guard relative to old rect
	qreal t = 0.5;
	if (!m_guardWall->line().isNull())
	{
		//this branch is taken unless this method gets called from the ctor
		const qreal oldLeft = m_leftWall->line().x1();
		const qreal oldRight = m_rightWall->line().x1();
		const qreal oldGCenter = m_guardWall->line().pointAt(0.5).x();
		t = (oldGCenter - oldLeft) / (oldRight - oldLeft);
	}
	//set new positions
	const QRectF rect(QPointF(), size());
	const QPointF leftEnd = m_guardAtTop ? rect.topLeft() : rect.bottomLeft();
	const QPointF rightEnd = m_guardAtTop ? rect.topRight() : rect.bottomRight();
	const QPointF wallExtent(rect.width() / 4, 0);
	m_leftWall->setLine(QLineF(leftEnd, leftEnd + wallExtent));
	m_rightWall->setLine(QLineF(rightEnd, rightEnd - wallExtent));
	//set position of guard to the same relative coordinate as before
	const qreal gWidth = wallExtent.x() / 1.07 - 2;
	const qreal gY = m_guardAtTop ? rect.top() - 4 : rect.bottom() + 4;
	QLineF gLine(rect.left(), gY, rect.left() + gWidth, gY);
	const qreal currentGCenter = gLine.pointAt(0.5).x();
	const qreal desiredGCenter = rect.left() + t * rect.width();
	gLine.translate(desiredGCenter - currentGCenter, 0);
	m_guardWall->setLine(gLine);
}

void Kolf::Windmill::load(KConfigGroup* group)
{
	Kolf::RectangleItem::load(group);
	setSpeed(group->readEntry("speed", m_speed));
	setGuardAtTop(!group->readEntry("bottom", !m_guardAtTop));
}

void Kolf::Windmill::save(KConfigGroup* group)
{
	Kolf::RectangleItem::save(group);
	group->writeEntry("speed", m_speed);
	group->writeEntry("bottom", !m_guardAtTop);
}

//END Kolf::Windmill