~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
/***************************************************************************
 *   Copyright 2008, 2009, 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 "overlay.h"
#include "canvasitem.h"
#include "game.h"
#include "shape.h"
#include "utils-animateditem.h"

#include <QGraphicsSceneEvent>
#include <QPen>
#include <QtMath>

const qreal Kolf::Overlay::MinimumObjectDimension = 10;

static const qreal OverlayHandleSize = 5;
static const QPen HandlePen(Qt::blue);
static const QBrush ActiveHandleBrush(QColor::fromHsv(240, 200, 255, 128));
static const QBrush NormalHandleBrush(QColor::fromHsv(240, 255, 255, 128));

//BEGIN Kolf::OverlayHandle

Kolf::OverlayHandle::OverlayHandle(Kolf::OverlayHandle::Shape shape, QGraphicsItem* parent)
	: QGraphicsPathItem(parent)
{
	setAcceptHoverEvents(true);
	setAcceptedMouseButtons(Qt::LeftButton);
	setPen(HandlePen);
	setBrush(NormalHandleBrush);
	//create shape
	QPainterPath path;
	switch (shape)
	{
		case SquareShape:
			path.addRect(QRectF(-OverlayHandleSize, -OverlayHandleSize, 2 * OverlayHandleSize, 2 * OverlayHandleSize));
			break;
		case CircleShape:
			path.addEllipse(QPointF(), OverlayHandleSize, OverlayHandleSize);
			break;
		case TriangleShape:
			path.moveTo(QPointF(OverlayHandleSize, 0.0));
			path.lineTo(QPointF(-OverlayHandleSize, OverlayHandleSize));
			path.lineTo(QPointF(-OverlayHandleSize, -OverlayHandleSize));
			path.closeSubpath();
			break;
	}
	setPath(path);
}

void Kolf::OverlayHandle::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
{
	event->accept();
	setBrush(ActiveHandleBrush);
}

void Kolf::OverlayHandle::hoverLeaveEvent(QGraphicsSceneHoverEvent* event)
{
	event->accept();
	setBrush(NormalHandleBrush);
}

void Kolf::OverlayHandle::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
	if (event->button() == Qt::LeftButton)
	{
		event->accept();
		emit moveStarted();
	}
}

void Kolf::OverlayHandle::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
{
	if (event->buttons() & Qt::LeftButton)
	{
		event->accept();
		emit moveRequest(event->scenePos());
	}
}

void Kolf::OverlayHandle::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
{
	if (event->button() == Qt::LeftButton)
	{
		event->accept();
		emit moveEnded();
	}
}

//END Kolf::OverlayHandle
//BEGIN Kolf::OverlayAreaItem

Kolf::OverlayAreaItem::OverlayAreaItem(Features features, QGraphicsItem* parent)
	: QGraphicsPathItem(parent)
	, m_features(features)
{
	if (m_features & (Clickable | Draggable))
		setAcceptedMouseButtons(Qt::LeftButton | Qt::RightButton);
	if (m_features & Draggable)
		setCursor(Qt::OpenHandCursor);
	if (m_features & Hoverable)
		setAcceptHoverEvents(true);
	//start invisible
	setPen(Qt::NoPen);
	setBrush(Qt::transparent);
}

void Kolf::OverlayAreaItem::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
{
	if (m_features & Hoverable)
	{
		event->accept();
		emit hoverEntered();
	}
	else
		QGraphicsPathItem::hoverEnterEvent(event);
}

void Kolf::OverlayAreaItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* event)
{
	if (m_features & Hoverable)
	{
		event->accept();
		emit hoverLeft();
	}
	else
		QGraphicsPathItem::hoverLeaveEvent(event);
}

void Kolf::OverlayAreaItem::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
	if (m_features & Clickable)
	{
		event->accept();
		emit clicked(event->button());
	}
	if (m_features & Draggable)
	{
		event->accept();
		setCursor(Qt::ClosedHandCursor);
	}
	if (!event->isAccepted())
		QGraphicsPathItem::mousePressEvent(event);
}

void Kolf::OverlayAreaItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
{
	if (m_features & Draggable)
		emit dragged(event->scenePos() - event->lastScenePos());
	else
		QGraphicsItem::mouseMoveEvent(event);
}

void Kolf::OverlayAreaItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
{
	if (m_features & Draggable)
		setCursor(Qt::OpenHandCursor);
	else
		QGraphicsItem::mouseReleaseEvent(event);
}

//END Kolf::OverlayAreaItem
//BEGIN Kolf::Overlay

Kolf::Overlay::Overlay(CanvasItem* citem, QGraphicsItem* qitem, bool hack_addQitemShapeToOutlines)
	: QGraphicsItem(qitem)
	, m_citem(citem), m_qitem(qitem)
	, m_state(Kolf::Overlay::Passive)
	, m_addQitemShapeToOutlines(hack_addQitemShapeToOutlines)
	, m_activatorItem(new Kolf::OverlayAreaItem(Kolf::OverlayAreaItem::Clickable | Kolf::OverlayAreaItem::Hoverable, this))
	, m_interactorAnimator(new Utils::AnimatedItem(this))
	, m_interactorItem(new Kolf::OverlayAreaItem(Kolf::OverlayAreaItem::Clickable | Kolf::OverlayAreaItem::Draggable, m_interactorAnimator))
	, m_handleAnimator(new Utils::AnimatedItem(this))
{
	//overlays have to be shown explicitly
	hide();
	//overlays themselves do not have mouse interaction, and do not paint anything itself
	setAcceptedMouseButtons(0);
	setFlag(QGraphicsItem::ItemHasNoContents);
	//initialize activator area item
	m_activatorItem->setZValue(1);
	connect(m_activatorItem, &Kolf::OverlayAreaItem::hoverEntered, this, &Overlay::activatorEntered);
	connect(m_activatorItem, &Kolf::OverlayAreaItem::hoverLeft, this, &Overlay::activatorLeft);
	connect(m_activatorItem, &Kolf::OverlayAreaItem::clicked, this, &Overlay::activatorClicked);
	//initialize interactor area item
	m_interactorAnimator->setZValue(2);
	m_interactorAnimator->setOpacity(0); //not visible at first
	m_interactorItem->setBrush(Qt::green);
	connect(m_interactorItem, &Kolf::OverlayAreaItem::clicked, this, &Overlay::activatorClicked);
	connect(m_interactorItem, &Kolf::OverlayAreaItem::dragged, this, &Overlay::interactorDragged);
	//initialize handle manager
	m_handleAnimator->setZValue(3);
	m_handleAnimator->setHideWhenInvisible(true);
	m_handleAnimator->setOpacity(0); //not visible at first
	//apply passive state - we need to change to some other state to prevent the setState method from returning early
	m_state = Active;
	setState(Passive);
}

CanvasItem* Kolf::Overlay::citem() const
{
	return m_citem;
}

QGraphicsItem* Kolf::Overlay::qitem() const
{
	return m_qitem;
}

void Kolf::Overlay::update()
{
	//update geometry outlines
	QPainterPath activationOutline, interactionOutline;
	foreach (Kolf::Shape* shape, m_citem->shapes())
	{
		activationOutline.addPath(shape->activationOutline());
		interactionOutline.addPath(shape->interactionOutline());
	}
	//HACK for Kolf::Shape
	if (m_addQitemShapeToOutlines)
	{
		const QPainterPath shape = m_qitem->shape();
		activationOutline.addPath(shape);
		interactionOutline.addPath(shape);
	}
	activationOutline.setFillRule(Qt::WindingFill);
	interactionOutline.setFillRule(Qt::WindingFill);
	m_activatorItem->setPath(activationOutline);
	m_interactorItem->setPath(interactionOutline);
}

void Kolf::Overlay::addHandle(QGraphicsItem* handle)
{
	handle->setParentItem(m_handleAnimator);
	handle->show();
}

Kolf::Overlay::State Kolf::Overlay::state() const
{
	return m_state;
}

void Kolf::Overlay::setState(Kolf::Overlay::State state)
{
	if (m_state == state)
		return;
	m_state = state;
	//apply new inner properties
	switch (state)
	{
		case Kolf::Overlay::Passive:
			m_interactorAnimator->setOpacityAnimated(0.0);
			break;
		case Kolf::Overlay::Hovered:
			m_interactorAnimator->setOpacityAnimated(0.3);
			break;
		case Kolf::Overlay::Active:
			m_interactorAnimator->setOpacityAnimated(0.6);
			break;
	}
	m_handleAnimator->setOpacityAnimated(state == Active ? 1.0 : 0.0);
	//propagate changes
	emit stateChanged();
	if (state == Kolf::Overlay::Active)
	{
		KolfGame* game = m_citem->game;
		if (game)
			game->setSelectedItem(m_citem);
	}
}

void Kolf::Overlay::activatorEntered()
{
	if (m_state == Kolf::Overlay::Passive)
		setState(Kolf::Overlay::Hovered);
}

void Kolf::Overlay::activatorLeft()
{
	if (m_state == Kolf::Overlay::Hovered)
		setState(Kolf::Overlay::Passive);
}

void Kolf::Overlay::activatorClicked(int button)
{
	Q_UNUSED(button)
	setState(Kolf::Overlay::Active);
}

void Kolf::Overlay::interactorDragged(const QPointF& distance)
{
	if (m_state == Kolf::Overlay::Active)
		m_citem->moveBy(distance.x(), distance.y());
}

//default implementation for QGraphicsItem

QRectF Kolf::Overlay::boundingRect() const
{
	return QRectF();
}

void Kolf::Overlay::paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*)
{
}

//END Kolf::Overlay