~alan-griffiths/miral/debug

« back to all changes in this revision

Viewing changes to miral-qt/demos/qml-demo-client/MovingRect.qml

  • Committer: Gerry Boland
  • Date: 2016-06-01 22:06:51 UTC
  • mto: This revision was merged to the branch mainline in revision 178.
  • Revision ID: gerry.boland@canonical.com-20160601220651-ge508tffql4e7u7c
Import QtMir code into miral-qt subdirectory. Disabled by default, use -DMIRAL_ENABLE_QT=1 to build.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import QtQuick 2.0
 
2
 
 
3
Rectangle {
 
4
    id: movingRect
 
5
    width: 80
 
6
    height: 80
 
7
    color: "red"
 
8
 
 
9
    property real maxX: parent.width - width
 
10
    property real targetX: maxX
 
11
 
 
12
    function flipTargetX() {
 
13
        if (targetX == 0) {
 
14
            targetX = maxX
 
15
        } else {
 
16
            targetX = 0
 
17
        }
 
18
    }
 
19
 
 
20
    Timer {
 
21
        property real step: 4
 
22
        repeat: true
 
23
        running: true
 
24
        interval: 1000 / 60
 
25
        onTriggered: {
 
26
            if (x < targetX) {
 
27
                if (x + step > targetX) {
 
28
                    x = targetX;
 
29
                } else {
 
30
                    x += step;
 
31
                }
 
32
            } else {
 
33
                if (x - step < targetX) {
 
34
                    x = targetX;
 
35
                } else {
 
36
                    x -= step;
 
37
                }
 
38
            }
 
39
        }
 
40
    }
 
41
 
 
42
    onXChanged: {
 
43
        if (x == targetX) {
 
44
            flipTargetX();
 
45
        }
 
46
    }
 
47
    onWidthChanged: {
 
48
        if (targetX > 0) {
 
49
            targetX = maxX;
 
50
        }
 
51
    }
 
52
 
 
53
    MouseArea {
 
54
        anchors.fill: parent
 
55
        onPressed: {
 
56
            parent.flipTargetX();
 
57
        }
 
58
    }
 
59
}