author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 04 Oct 2010 01:19:32 +0300 | |
changeset 37 | 758a864f9613 |
parent 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 |
** |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
9 |
** $QT_BEGIN_LICENSE:BSD$ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
10 |
** You may use this file under the terms of the BSD license as follows: |
0 | 11 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
12 |
** "Redistribution and use in source and binary forms, with or without |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
13 |
** modification, are permitted provided that the following conditions are |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
14 |
** met: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
15 |
** * Redistributions of source code must retain the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
16 |
** notice, this list of conditions and the following disclaimer. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
17 |
** * Redistributions in binary form must reproduce the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
18 |
** notice, this list of conditions and the following disclaimer in |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
19 |
** the documentation and/or other materials provided with the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
20 |
** distribution. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
21 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
22 |
** the names of its contributors may be used to endorse or promote |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
23 |
** products derived from this software without specific prior written |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
24 |
** permission. |
0 | 25 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
26 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
27 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
28 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
29 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
30 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
31 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
32 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
33 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
34 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
35 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
36 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
0 | 37 |
** $QT_END_LICENSE$ |
38 |
** |
|
39 |
****************************************************************************/ |
|
40 |
||
41 |
#include "mouse.h" |
|
42 |
||
43 |
#include <QGraphicsScene> |
|
44 |
#include <QPainter> |
|
45 |
#include <QStyleOption> |
|
46 |
||
47 |
#include <math.h> |
|
48 |
||
49 |
static const double Pi = 3.14159265358979323846264338327950288419717; |
|
50 |
static double TwoPi = 2.0 * Pi; |
|
51 |
||
52 |
static qreal normalizeAngle(qreal angle) |
|
53 |
{ |
|
54 |
while (angle < 0) |
|
55 |
angle += TwoPi; |
|
56 |
while (angle > TwoPi) |
|
57 |
angle -= TwoPi; |
|
58 |
return angle; |
|
59 |
} |
|
60 |
||
61 |
//! [0] |
|
62 |
Mouse::Mouse() |
|
63 |
: angle(0), speed(0), mouseEyeDirection(0), |
|
64 |
color(qrand() % 256, qrand() % 256, qrand() % 256) |
|
65 |
{ |
|
66 |
setRotation(qrand() % (360 * 16)); |
|
67 |
} |
|
68 |
//! [0] |
|
69 |
||
70 |
//! [1] |
|
71 |
QRectF Mouse::boundingRect() const |
|
72 |
{ |
|
73 |
qreal adjust = 0.5; |
|
74 |
return QRectF(-18 - adjust, -22 - adjust, |
|
75 |
36 + adjust, 60 + adjust); |
|
76 |
} |
|
77 |
//! [1] |
|
78 |
||
79 |
//! [2] |
|
80 |
QPainterPath Mouse::shape() const |
|
81 |
{ |
|
82 |
QPainterPath path; |
|
83 |
path.addRect(-10, -20, 20, 40); |
|
84 |
return path; |
|
85 |
} |
|
86 |
//! [2] |
|
87 |
||
88 |
//! [3] |
|
89 |
void Mouse::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) |
|
90 |
{ |
|
91 |
// Body |
|
92 |
painter->setBrush(color); |
|
93 |
painter->drawEllipse(-10, -20, 20, 40); |
|
94 |
||
95 |
// Eyes |
|
96 |
painter->setBrush(Qt::white); |
|
97 |
painter->drawEllipse(-10, -17, 8, 8); |
|
98 |
painter->drawEllipse(2, -17, 8, 8); |
|
99 |
||
100 |
// Nose |
|
101 |
painter->setBrush(Qt::black); |
|
102 |
painter->drawEllipse(QRectF(-2, -22, 4, 4)); |
|
103 |
||
104 |
// Pupils |
|
105 |
painter->drawEllipse(QRectF(-8.0 + mouseEyeDirection, -17, 4, 4)); |
|
106 |
painter->drawEllipse(QRectF(4.0 + mouseEyeDirection, -17, 4, 4)); |
|
107 |
||
108 |
// Ears |
|
109 |
painter->setBrush(scene()->collidingItems(this).isEmpty() ? Qt::darkYellow : Qt::red); |
|
110 |
painter->drawEllipse(-17, -12, 16, 16); |
|
111 |
painter->drawEllipse(1, -12, 16, 16); |
|
112 |
||
113 |
// Tail |
|
114 |
QPainterPath path(QPointF(0, 20)); |
|
115 |
path.cubicTo(-5, 22, -5, 22, 0, 25); |
|
116 |
path.cubicTo(5, 27, 5, 32, 0, 30); |
|
117 |
path.cubicTo(-5, 32, -5, 42, 0, 35); |
|
118 |
painter->setBrush(Qt::NoBrush); |
|
119 |
painter->drawPath(path); |
|
120 |
} |
|
121 |
//! [3] |
|
122 |
||
123 |
//! [4] |
|
124 |
void Mouse::advance(int step) |
|
125 |
{ |
|
126 |
if (!step) |
|
127 |
return; |
|
128 |
//! [4] |
|
129 |
// Don't move too far away |
|
130 |
//! [5] |
|
131 |
QLineF lineToCenter(QPointF(0, 0), mapFromScene(0, 0)); |
|
132 |
if (lineToCenter.length() > 150) { |
|
133 |
qreal angleToCenter = ::acos(lineToCenter.dx() / lineToCenter.length()); |
|
134 |
if (lineToCenter.dy() < 0) |
|
135 |
angleToCenter = TwoPi - angleToCenter; |
|
136 |
angleToCenter = normalizeAngle((Pi - angleToCenter) + Pi / 2); |
|
137 |
||
138 |
if (angleToCenter < Pi && angleToCenter > Pi / 4) { |
|
139 |
// Rotate left |
|
140 |
angle += (angle < -Pi / 2) ? 0.25 : -0.25; |
|
141 |
} else if (angleToCenter >= Pi && angleToCenter < (Pi + Pi / 2 + Pi / 4)) { |
|
142 |
// Rotate right |
|
143 |
angle += (angle < Pi / 2) ? 0.25 : -0.25; |
|
144 |
} |
|
145 |
} else if (::sin(angle) < 0) { |
|
146 |
angle += 0.25; |
|
147 |
} else if (::sin(angle) > 0) { |
|
148 |
angle -= 0.25; |
|
149 |
//! [5] //! [6] |
|
150 |
} |
|
151 |
//! [6] |
|
152 |
||
153 |
// Try not to crash with any other mice |
|
154 |
//! [7] |
|
155 |
QList<QGraphicsItem *> dangerMice = scene()->items(QPolygonF() |
|
156 |
<< mapToScene(0, 0) |
|
157 |
<< mapToScene(-30, -50) |
|
158 |
<< mapToScene(30, -50)); |
|
159 |
foreach (QGraphicsItem *item, dangerMice) { |
|
160 |
if (item == this) |
|
161 |
continue; |
|
162 |
||
163 |
QLineF lineToMouse(QPointF(0, 0), mapFromItem(item, 0, 0)); |
|
164 |
qreal angleToMouse = ::acos(lineToMouse.dx() / lineToMouse.length()); |
|
165 |
if (lineToMouse.dy() < 0) |
|
166 |
angleToMouse = TwoPi - angleToMouse; |
|
167 |
angleToMouse = normalizeAngle((Pi - angleToMouse) + Pi / 2); |
|
168 |
||
169 |
if (angleToMouse >= 0 && angleToMouse < Pi / 2) { |
|
170 |
// Rotate right |
|
171 |
angle += 0.5; |
|
172 |
} else if (angleToMouse <= TwoPi && angleToMouse > (TwoPi - Pi / 2)) { |
|
173 |
// Rotate left |
|
174 |
angle -= 0.5; |
|
175 |
//! [7] //! [8] |
|
176 |
} |
|
177 |
//! [8] //! [9] |
|
178 |
} |
|
179 |
//! [9] |
|
180 |
||
181 |
// Add some random movement |
|
182 |
//! [10] |
|
183 |
if (dangerMice.size() > 1 && (qrand() % 10) == 0) { |
|
184 |
if (qrand() % 1) |
|
185 |
angle += (qrand() % 100) / 500.0; |
|
186 |
else |
|
187 |
angle -= (qrand() % 100) / 500.0; |
|
188 |
} |
|
189 |
//! [10] |
|
190 |
||
191 |
//! [11] |
|
192 |
speed += (-50 + qrand() % 100) / 100.0; |
|
193 |
||
194 |
qreal dx = ::sin(angle) * 10; |
|
195 |
mouseEyeDirection = (qAbs(dx / 5) < 1) ? 0 : dx / 5; |
|
196 |
||
197 |
setRotation(rotation() + dx); |
|
198 |
setPos(mapToParent(0, -(3 + sin(speed) * 3))); |
|
199 |
} |
|
200 |
//! [11] |