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