summaryrefslogtreecommitdiffstats
path: root/examples/sensors/maze/Mouse.qml
blob: 155f0bd682db5f7153ee9d30ea856622dc2d2d58 (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
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

//Import the declarative plugins
import QtQuick

//Import the javascript functions for this game
import "lib.js" as Lib

//Implementation of the Mouse control.
Item {
    id: mouse
    x: 0
    y: 0
    width: Lib.cellDimension
    height: Lib.cellDimension
    property int angle
    readonly property double radians_to_degrees: 180 / Math.PI

    AnimatedImage {
        id: img
        source: "content/mouse_down.gif"
        anchors.fill: parent
        visible:  true
    }

    //Function for moving the mouse
    function move(newx, newy)
    {
        if (mouse.x === newx && mouse.y === newy)
            return
        // somehow this actually works
//! [0]
        var a = newy - mouse.y
        var b = newx - mouse.x
        angle = Math.atan2(-b, a) * mouse.radians_to_degrees
        if (angle < 0)
            angle = 360 + angle

        img.rotation = angle
        mouse.x = newx;
        mouse.y = newy;
//! [0]
    }
}