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

import Qt.labs.gestures 1.0


Rectangle {
    id: animalRectangle;
    property string image;

    width: 100; height: 100;
    property int imageSize: width - 10

    // to identify pairs of animals
    property int type;

    color: "transparent"

    signal rotated

    Flipable {
        id: flipable
        anchors.fill: parent

        property int angle: 0
        property bool flipped: false

        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
        }

        states: State {
            name: "back"
            PropertyChanges { target: flipable; angle: 180 }
            when: flipable.flipped
        }

        transitions: Transition {
            NumberAnimation { properties: "angle"; duration: 1000 }
        }
    }

    function hide() {
        hideTimer.start();
        console.log("hiding: " + image);
    }

    Timer {
        id: hideTimer
        interval: 2000; running: false; repeat: false
        onTriggered: flipable.flipped = false;
    }


    function showFront() {
        flipable.flipped = true;
        parent.animalFlipped(animalRectangle);
    }

    Behavior on scale {
        NumberAnimation {
            easing.type: Easing.OutBounce
            easing.amplitude: 100
            duration: 400
        }
    }
    Behavior on opacity {
        NumberAnimation {
            easing.type: Easing.OutBounce
            easing.amplitude: 100
            duration: 400
        }
    }

    GestureArea {
        anchors.fill: parent

        Tap {
            onStarted: { console.log("tap started");
                //animalRectangle.color = "red";
                //animalRectangle.toggleSide();
//                animalRectangle.scale = 1.1;
            }
            onCanceled: { console.log("tap canceled");
                //animalRectangle.color = "white";
//                animalRectangle.scale = 1.0;
            }
            onFinished: { console.log("tap finished");
                //animalRectangle.color = "white";
//                animalRectangle.scale = 1.0;
                animalRectangle.showFront();
            }
        }

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

            onFinished: {
                console.log("tap-and-hold finished");
//                animalRectangle.scale = 1.0;
//                animalRectangle.rotation = 0.0;
            }
        }

        Pan {
            onStarted: { console.log("pan started");
            }
            onUpdated: {
                //console.log("pan update dx:" + gesture.delta.x + " dy: " + gesture.delta.y
                //            + " ox: " + gesture.offset.x);
//                animalRectangle.scale = 0.8;
                //animalRectangle.x += gesture.offset.x;
//                animalRectangle.opacity = 0.7;
                animalRectangle.x += gesture.delta.x;
                animalRectangle.y += gesture.delta.y;
            }
            onFinished: { console.log("pan finished");
//                animalRectangle.scale = 1.0;
                animalRectangle.opacity = 1.0;

                console.log( "velocity: " + gesture.horizontalVelocity + ", " + gesture.verticalVelocity );
            }
        }

        Pinch {
            onStarted: 
            {
                console.log("pinch started");
                animalRectangle.tmpScale = eval(animalRectangle.scale);

            }
            onUpdated: {
                console.log("scale total:" + gesture.totalScaleFactor
                            +"scaleFactor:" + gesture.scaleFactor );
                //animalRectangle.scale = gesture.scaleFactor;
//                animalRectangle.scale *= 1.0 + gesture.totalScaleFactor;
                console.log("rotation total:" + gesture.totalRotationAngle
                            +"rotationAngle:" + gesture.rotationAngle );

                //animalRectangle.rotation = gesture.totalRotationAngle;
                animalRectangle.rotation += gesture.rotationAngle - gesture.lastRotationAngle

                console.log("tmpScale: " + animalRectangle.tmpScale);
                animalRectangle.scale += gesture.scaleFactor - gesture.lastScaleFactor;

            }
        }
    }
}