0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the examples of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
#include "mouse.h"
|
|
43 |
|
|
44 |
#include <QGraphicsScene>
|
|
45 |
#include <QPainter>
|
|
46 |
#include <QStyleOption>
|
|
47 |
|
|
48 |
#include <math.h>
|
|
49 |
|
|
50 |
static const double Pi = 3.14159265358979323846264338327950288419717;
|
|
51 |
static double TwoPi = 2.0 * Pi;
|
|
52 |
|
|
53 |
static qreal normalizeAngle(qreal angle)
|
|
54 |
{
|
|
55 |
while (angle < 0)
|
|
56 |
angle += TwoPi;
|
|
57 |
while (angle > TwoPi)
|
|
58 |
angle -= TwoPi;
|
|
59 |
return angle;
|
|
60 |
}
|
|
61 |
|
|
62 |
//! [0]
|
|
63 |
Mouse::Mouse()
|
|
64 |
: angle(0), speed(0), mouseEyeDirection(0),
|
|
65 |
color(qrand() % 256, qrand() % 256, qrand() % 256)
|
|
66 |
{
|
|
67 |
setRotation(qrand() % (360 * 16));
|
|
68 |
}
|
|
69 |
//! [0]
|
|
70 |
|
|
71 |
//! [1]
|
|
72 |
QRectF Mouse::boundingRect() const
|
|
73 |
{
|
|
74 |
qreal adjust = 0.5;
|
|
75 |
return QRectF(-18 - adjust, -22 - adjust,
|
|
76 |
36 + adjust, 60 + adjust);
|
|
77 |
}
|
|
78 |
//! [1]
|
|
79 |
|
|
80 |
//! [2]
|
|
81 |
QPainterPath Mouse::shape() const
|
|
82 |
{
|
|
83 |
QPainterPath path;
|
|
84 |
path.addRect(-10, -20, 20, 40);
|
|
85 |
return path;
|
|
86 |
}
|
|
87 |
//! [2]
|
|
88 |
|
|
89 |
//! [3]
|
|
90 |
void Mouse::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
|
|
91 |
{
|
|
92 |
// Body
|
|
93 |
painter->setBrush(color);
|
|
94 |
painter->drawEllipse(-10, -20, 20, 40);
|
|
95 |
|
|
96 |
// Eyes
|
|
97 |
painter->setBrush(Qt::white);
|
|
98 |
painter->drawEllipse(-10, -17, 8, 8);
|
|
99 |
painter->drawEllipse(2, -17, 8, 8);
|
|
100 |
|
|
101 |
// Nose
|
|
102 |
painter->setBrush(Qt::black);
|
|
103 |
painter->drawEllipse(QRectF(-2, -22, 4, 4));
|
|
104 |
|
|
105 |
// Pupils
|
|
106 |
painter->drawEllipse(QRectF(-8.0 + mouseEyeDirection, -17, 4, 4));
|
|
107 |
painter->drawEllipse(QRectF(4.0 + mouseEyeDirection, -17, 4, 4));
|
|
108 |
|
|
109 |
// Ears
|
|
110 |
painter->setBrush(scene()->collidingItems(this).isEmpty() ? Qt::darkYellow : Qt::red);
|
|
111 |
painter->drawEllipse(-17, -12, 16, 16);
|
|
112 |
painter->drawEllipse(1, -12, 16, 16);
|
|
113 |
|
|
114 |
// Tail
|
|
115 |
QPainterPath path(QPointF(0, 20));
|
|
116 |
path.cubicTo(-5, 22, -5, 22, 0, 25);
|
|
117 |
path.cubicTo(5, 27, 5, 32, 0, 30);
|
|
118 |
path.cubicTo(-5, 32, -5, 42, 0, 35);
|
|
119 |
painter->setBrush(Qt::NoBrush);
|
|
120 |
painter->drawPath(path);
|
|
121 |
}
|
|
122 |
//! [3]
|
|
123 |
|
|
124 |
//! [4]
|
|
125 |
void Mouse::advance(int step)
|
|
126 |
{
|
|
127 |
if (!step)
|
|
128 |
return;
|
|
129 |
//! [4]
|
|
130 |
// Don't move too far away
|
|
131 |
//! [5]
|
|
132 |
QLineF lineToCenter(QPointF(0, 0), mapFromScene(0, 0));
|
|
133 |
if (lineToCenter.length() > 150) {
|
|
134 |
qreal angleToCenter = ::acos(lineToCenter.dx() / lineToCenter.length());
|
|
135 |
if (lineToCenter.dy() < 0)
|
|
136 |
angleToCenter = TwoPi - angleToCenter;
|
|
137 |
angleToCenter = normalizeAngle((Pi - angleToCenter) + Pi / 2);
|
|
138 |
|
|
139 |
if (angleToCenter < Pi && angleToCenter > Pi / 4) {
|
|
140 |
// Rotate left
|
|
141 |
angle += (angle < -Pi / 2) ? 0.25 : -0.25;
|
|
142 |
} else if (angleToCenter >= Pi && angleToCenter < (Pi + Pi / 2 + Pi / 4)) {
|
|
143 |
// Rotate right
|
|
144 |
angle += (angle < Pi / 2) ? 0.25 : -0.25;
|
|
145 |
}
|
|
146 |
} else if (::sin(angle) < 0) {
|
|
147 |
angle += 0.25;
|
|
148 |
} else if (::sin(angle) > 0) {
|
|
149 |
angle -= 0.25;
|
|
150 |
//! [5] //! [6]
|
|
151 |
}
|
|
152 |
//! [6]
|
|
153 |
|
|
154 |
// Try not to crash with any other mice
|
|
155 |
//! [7]
|
|
156 |
QList<QGraphicsItem *> dangerMice = scene()->items(QPolygonF()
|
|
157 |
<< mapToScene(0, 0)
|
|
158 |
<< mapToScene(-30, -50)
|
|
159 |
<< mapToScene(30, -50));
|
|
160 |
foreach (QGraphicsItem *item, dangerMice) {
|
|
161 |
if (item == this)
|
|
162 |
continue;
|
|
163 |
|
|
164 |
QLineF lineToMouse(QPointF(0, 0), mapFromItem(item, 0, 0));
|
|
165 |
qreal angleToMouse = ::acos(lineToMouse.dx() / lineToMouse.length());
|
|
166 |
if (lineToMouse.dy() < 0)
|
|
167 |
angleToMouse = TwoPi - angleToMouse;
|
|
168 |
angleToMouse = normalizeAngle((Pi - angleToMouse) + Pi / 2);
|
|
169 |
|
|
170 |
if (angleToMouse >= 0 && angleToMouse < Pi / 2) {
|
|
171 |
// Rotate right
|
|
172 |
angle += 0.5;
|
|
173 |
} else if (angleToMouse <= TwoPi && angleToMouse > (TwoPi - Pi / 2)) {
|
|
174 |
// Rotate left
|
|
175 |
angle -= 0.5;
|
|
176 |
//! [7] //! [8]
|
|
177 |
}
|
|
178 |
//! [8] //! [9]
|
|
179 |
}
|
|
180 |
//! [9]
|
|
181 |
|
|
182 |
// Add some random movement
|
|
183 |
//! [10]
|
|
184 |
if (dangerMice.size() > 1 && (qrand() % 10) == 0) {
|
|
185 |
if (qrand() % 1)
|
|
186 |
angle += (qrand() % 100) / 500.0;
|
|
187 |
else
|
|
188 |
angle -= (qrand() % 100) / 500.0;
|
|
189 |
}
|
|
190 |
//! [10]
|
|
191 |
|
|
192 |
//! [11]
|
|
193 |
speed += (-50 + qrand() % 100) / 100.0;
|
|
194 |
|
|
195 |
qreal dx = ::sin(angle) * 10;
|
|
196 |
mouseEyeDirection = (qAbs(dx / 5) < 1) ? 0 : dx / 5;
|
|
197 |
|
|
198 |
setRotation(rotation() + dx);
|
|
199 |
setPos(mapToParent(0, -(3 + sin(speed) * 3)));
|
|
200 |
}
|
|
201 |
//! [11]
|