summaryrefslogtreecommitdiffstats
path: root/animals/Animal.qml
blob: 7955aa1fe9fa92527ff5a4a9599a8f5ce04f9caa (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
import Qt 4.7

import Qt.labs.gestures 2.0


Rectangle {
    id: animalRectangle;
    property string image;

    color: "transparent"
    width: 100; height: 100;
    property int imageSize: width - 10;

    // to identify pairs of animals
    property int type;

    state: "back";

    states: [
        State {
            name: "back"
        },
        State {
            name: "front"
        },
        State {
            name: "solved";
            extend: "front";
        }
    ]

    transitions: [
        Transition {
            from: "*"
            to: "solved";
            ParallelAnimation {
            NumberAnimation {
                target: flipable;
                property: "angle";
                to: 180;
                duration: 1000
            }
            NumberAnimation { target: animalRectangle; property: "opacity"; to: 0.9; duration: 1000;}
        }
        },

        Transition {
            from: "back";
            to: "*";
            NumberAnimation {
                target: flipable;
                property: "angle";
                from: 0;
                to: 180;
                duration: 1000
            }
        },

        Transition {
            from: "front";
            to: "back";
            SequentialAnimation {
                ScriptAction {
                    script: console.log("transition");
                }
                NumberAnimation {
                    target: flipable;
                    property: "angle";
                    to: 180;
                    duration: 1000
                }
                PauseAnimation { duration: 1000 }
                NumberAnimation {
                    target: flipable;
                    property: "angle";
                    to: 0;
                    duration: 1000
                }
            }
        }
    ]

    Flipable {
        id: flipable;
        property int angle: 0;

        anchors.fill: parent;

        Rectangle {
            radius: 10;
            color: 'white';
            anchors.fill: parent;
            z: -1
        }
        front: Image {
            id: backImage;
            width: imageSize; height: imageSize; anchors.centerIn: parent;
            fillMode: "PreserveAspectFit";
            source: "images/qt-logo.png";
        }
        back: Image {
            id: animalImage;
            width: imageSize; height: imageSize; anchors.centerIn: parent;
            fillMode: "PreserveAspectFit";
            source: animalRectangle.image;
        }

        transform: Rotation {
            origin.x: flipable.width/2; origin.y: flipable.height/2
            axis.x: 0; axis.y: 1; axis.z: 0     // rotate around y-axis
            angle: flipable.angle
        }
    }

    GestureArea {
        anchors.fill: parent

        Tap {
            onFinished: animalRectangle.parent.animalTapped(animalRectangle);
        }

        TapAndHold {
            onStarted: console.log("tap-and-hold started");
            onFinished: console.log("tap-and-hold finished");
        }

        Pan {
            onStarted: animalRectangle.z = 1;
            onUpdated: {
                animalRectangle.x += gesture.delta.x;
                animalRectangle.y += gesture.delta.y;
            }
            onFinished: {
                animalRectangle.z = 0;

                console.log("velo: " + gesture.horizontalVelocity);
            }
        }

        Pinch {
            onStarted: animalRectangle.z = 1;
            onUpdated: {
                animalRectangle.rotation += gesture.rotationAngle - gesture.lastRotationAngle
                animalRectangle.scale = animalRectangle.scale * gesture.scaleFactor;
                animalRectangle.x += gesture.centerPoint.x - gesture.lastCenterPoint.x
                animalRectangle.y += gesture.centerPoint.y - gesture.lastCenterPoint.y
            }
            onFinished: animalRectangle.z = 0;
        }
    }
}