summaryrefslogtreecommitdiffstats
path: root/examples/CollidingMice.qs
blob: a4d85cfa96b6d43d2013b6debf59e1ce7bb38161 (plain)
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
/****************************************************************************
**
** Copyright (C) 2008 Trolltech ASA. All rights reserved.
**
** This file is part of the Qt Script Generator project on Trolltech Labs.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.trolltech.com/products/qt/opensource.html
**
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://www.trolltech.com/products/qt/licensing.html or contact the
** sales department at sales@trolltech.com.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

function Mouse(parent) {
    QGraphicsItem.call(this, parent);
    this.angle = 0;
    this.speed = 0;
    this.mouseEyeDirection = 0;

    var adjust = 0.5;
    this._boundingRect = new QRectF(-20 - adjust, -22 - adjust,
                                    40 + adjust, 83 + adjust);
    this.boundingRect = function() { return this._boundingRect; };

    this._shape = new QPainterPath();
    this._shape.addRect(-10, -20, 20, 40);
    this.shape = function() { return this._shape; }

    this._brush = new QBrush(Qt.SolidPattern);
    this._tail = new QPainterPath(new QPointF(0, 20));
    this._tail.cubicTo(-5, 22, -5, 22, 0, 25);
    this._tail.cubicTo(5, 27, 5, 32, 0, 30);
    this._tail.cubicTo(-5, 32, -5, 42, 0, 35);

    this._pupilRect1 = new QRectF(-8 + this.mouseEyeDirection, -17, 4, 4);
    this._pupilRect2 = new QRectF(4 + this.mouseEyeDirection, -17, 4, 4);

    this.color = new QColor(Math.random()*256, Math.random()*256,
                            Math.random()*256);
    this.rotate(Math.random()*360);

    var timer = new QTimer(this);
    timer.singleShot = false;
    timer.timeout.connect(this, function() { this.move(); });
    timer.start(1000 / 33);
}

Mouse.prototype = new QGraphicsItem();

Mouse.prototype.paint = function(painter, styleOptionGraphicsItem, widget) {
    // Body
    painter.setBrush(new QBrush(this.color));
    painter.drawEllipse(-10, -20, 20, 40);

    // Eyes
    this._brush.setColor(Qt.white);
    painter.setBrush(this._brush);
    painter.drawEllipse(-10, -17, 8, 8);
    painter.drawEllipse(2, -17, 8, 8);

    // Nose
    this._brush.setColor(Qt.black);
    painter.setBrush(this._brush);
    painter.drawEllipse(-2, -22, 4, 4);

    // Pupils
    painter.drawEllipse(this._pupilRect1);
    painter.drawEllipse(this._pupilRect2);

    // Ears
    //    if (this.scene().collidingItems(this).length == 0) FIXME: const QGraphicsItem*
    if (this.scene().items(this.pos()).length == 1)
        this._brush.setColor(Qt.darkYellow);
    else
        this._brush.setColor(Qt.red);
    painter.setBrush(this._brush);

    painter.drawEllipse(-17, -12, 16, 16);
    painter.drawEllipse(1, -12, 16, 16);

    // Tail
    painter.setBrush(Qt.NoBrush);
    painter.drawPath(this._tail);
}

Mouse.prototype.move = function() {
    // Don't move too far away
    var lineToCenter = new QLineF(Mouse.origo, this.mapFromScene(0, 0));
    if (lineToCenter.length() > 150) {
        var angleToCenter = Math.acos(lineToCenter.dx()
                                      / lineToCenter.length());
        if (lineToCenter.dy() < 0)
            angleToCenter = Mouse.TWO_PI - angleToCenter;
        angleToCenter = Mouse.normalizeAngle((Math.PI - angleToCenter)
                                              + Math.PI / 2);

        if (angleToCenter < Math.PI && angleToCenter > Math.PI / 4) {
            // Rotate left
            this.angle += (this.angle < -Math.PI / 2) ? 0.25 : -0.25;
        } else if (angleToCenter >= Math.PI
                   && angleToCenter < (Math.PI + Math.PI / 2
                                       + Math.PI / 4)) {
            // Rotate right
            this.angle += (this.angle < Math.PI / 2) ? 0.25 : -0.25;
        }
    } else if (Math.sin(this.angle) < 0) {
        this.angle += 0.25;
    } else if (Math.sin(this.angle) > 0) {
        this.angle -= 0.25;
    }

    // Try not to crash with any other mice

    var polygon = new QPolygonF(
	[ this.mapToScene(0, 0),
	  this.mapToScene(-30, -50),
	  this.mapToScene(30, -50) ] );

    var dangerMice = this.scene().items(polygon);
    for (var i = 0; i < dangerMice.length; ++i) {
        var item = dangerMice[i];
        if (item == this)
            continue;

        var lineToMouse = new QLineF(Mouse.origo,
                                     this.mapFromItem(item, 0, 0));
        var angleToMouse = Math.acos(lineToMouse.dx()
                                     / lineToMouse.length());
        if (lineToMouse.dy() < 0)
            angleToMouse = Mouse.TWO_PI - angleToMouse;
        angleToMouse = Mouse.normalizeAngle((Math.PI - angleToMouse)
                                      + Math.PI / 2);

        if (angleToMouse >= 0 && angleToMouse < (Math.PI / 2)) {
            // Rotate right
            this.angle += 0.5;
        } else if (angleToMouse <= Mouse.TWO_PI
                   && angleToMouse > (Mouse.TWO_PI - Math.PI / 2)) {
            // Rotate left
            this.angle -= 0.5;
        }
    }

    // Add some random movement
    if (dangerMice.length < 1 && Math.random() < 0.1) {
        if (Math.random() > 0.5)
            this.angle += Math.random() / 5;
        else
            this.angle -= Math.random() / 5;
    }

    this.speed += (-50 + Math.random() * 100) / 100.0;

    var dx = Math.sin(this.angle) * 10;
    this.mouseEyeDirection = (Math.abs(dx / 5) < 1) ? 0 : dx / 5;

    this.rotate(dx);
    this.setPos(this.mapToParent(0, -(3 + Math.sin(this.speed) * 3)));
}

Mouse.normalizeAngle = function(angle) {
    while (angle < 0)
        angle += Mouse.TWO_PI;
    while (angle > Mouse.TWO_PI)
        angle -= Mouse.TWO_PI;
    return angle;
}

Mouse.TWO_PI = Math.PI * 2;
Mouse.origo = new QPointF(0, 0);



function CollidingMice(parent) {
    QWidget.call(this, parent);
    var scene = new QGraphicsScene(this);
    scene.setSceneRect(-300, -300, 600, 600);
    scene.itemIndexMethod = QGraphicsScene.NoIndex;

    for (var i = 0; i < CollidingMice.MOUSE_COUNT; ++i) {
        var mouse = new Mouse(this);
        mouse.setPos(Math.sin((i * 6.28) / CollidingMice.MOUSE_COUNT) * 200,
                     Math.cos((i * 6.28) / CollidingMice.MOUSE_COUNT) * 200);
        scene.addItem(mouse);
    }

    var view = new QGraphicsView(scene, this);
    view.setRenderHint(QPainter.Antialiasing);
    view.backgroundBrush = new QBrush(new QPixmap("images/cheese.png"));
    view.cacheMode = new QGraphicsView.CacheMode(QGraphicsView.CacheBackground);
    view.dragMode = QGraphicsView.ScrollHandDrag;
    view.viewportUpdateMode = QGraphicsView.FullViewportUpdate;

    var layout = new QGridLayout();
    layout.addWidget(view, 0, 0);
    this.setLayout(layout);

    this.setWindowTitle("Colliding Mice");
    this.resize(400, 300);
}

CollidingMice.prototype = new QWidget();

CollidingMice.MOUSE_COUNT = 7;



var collidingMice = new CollidingMice(null);
collidingMice.show();
QCoreApplication.exec();