Warning, /education/gcompris/src/activities/traffic/Car.qml is written in an unsupported language. File is not indexed.

0001 /* GCompris - Car.qml
0002  *
0003  * SPDX-FileCopyrightText: 2014 Holger Kaelberer <holger.k@elberer.de>
0004  * 
0005  * Authors:
0006  *   Bruno Coudoin <bruno.coudoin@gcompris.net> (GTK+ Mostly full rewrite)
0007  *   Holger Kaelberer <holger.k@elberer.de> (Qt Quick port)
0008  *
0009  *   SPDX-License-Identifier: GPL-3.0-or-later
0010  */
0011 
0012 import QtQuick 2.12
0013 import GCompris 1.0
0014 import QtGraphicalEffects 1.0
0015 import "../../core"
0016 import "traffic.js" as Activity
0017 
0018 Item {
0019     id: car
0020 
0021     property int xPos
0022     property int yPos
0023     property int size
0024     property bool goal: false
0025     property bool isHorizontal: false
0026     property alias color: carRect.color
0027     property alias source: carImage.source
0028     
0029     property real blockSize: parent.width / 6
0030     property var xBounds: undefined
0031     property var yBounds: undefined
0032 
0033     property string mode
0034     property bool isMoving: false
0035 
0036     Component.onCompleted: {
0037         mode = parent.mode
0038         connection.target = parent
0039     }
0040     // Connect the jamGrid.mode to car.mode to automatically change the wrapped object
0041     Connections {
0042         id: connection
0043         onModeChanged: {
0044             car.mode = parent.mode;
0045         }
0046     }
0047 
0048     x: (mode == "COLOR" || car.isHorizontal) ? (xPos * blockSize) : ((xPos+1) * blockSize)
0049     y: (mode == "COLOR" || car.isHorizontal) ? (yPos * blockSize) : ((yPos) * blockSize)
0050 
0051     // track effective coordinates (needed for transformed image):
0052     property real effX: car.xPos * car.blockSize
0053     property real effY: car.yPos * car.blockSize
0054     property real effWidth: (mode == "COLOR" || car.isHorizontal) ? car.width : car.height
0055     property real effHeight: (mode == "COLOR" || car.isHorizontal) ? car.height : car.width
0056     property GCSfx audioEffects
0057 
0058     width: (mode == "IMAGE" || isHorizontal) ? (size * blockSize) : blockSize
0059     height: (mode == "IMAGE" || isHorizontal) ? blockSize : (size * blockSize)
0060 
0061     Item {
0062         id: carWrapper
0063         
0064         anchors.fill: parent
0065         width: parent.width
0066         height: parent.height
0067         
0068         Rectangle {
0069             id: carRect
0070             visible: (mode == "COLOR")
0071             
0072             z: 11
0073             anchors.fill: parent
0074             width: parent.width
0075             height: parent.height
0076             
0077             border.width: 2
0078             border.color: "white"
0079             
0080             MultiPointTouchArea {
0081                 id: rectTouch
0082                 anchors.fill: parent
0083                 touchPoints: [ TouchPoint { id: point1 } ]
0084                 property real startX;
0085                 property real startY;
0086             
0087                 onPressed: {
0088                     if (!Activity.isMoving) {
0089                         Activity.isMoving = true;
0090                         car.isMoving = true;
0091                         car.audioEffects.play(Activity.baseUrl + "car.wav")
0092                         rectTouch.startX = point1.x;
0093                         rectTouch.startY = point1.y;
0094                     }
0095                 }
0096                 
0097                 onUpdated: {
0098                     if (car.isMoving && !Activity.haveWon) {
0099                         var deltaX = point1.x - startX;
0100                         var deltaY = point1.y - startY;
0101                         Activity.updateCarPosition(car, car.x + deltaX, car.y + deltaY);
0102                     }
0103                 }
0104 
0105                 onReleased: {
0106                     if (car.isMoving) {
0107                         car.isMoving = false;
0108                         Activity.isMoving = false;
0109                         if (!Activity.haveWon)
0110                             Activity.snapCarToGrid(car);
0111                     }
0112                 }
0113             }
0114         }
0115         
0116         Image {
0117             id: carImage
0118             visible: (mode == "IMAGE")
0119             
0120             fillMode: Image.PreserveAspectFit
0121             anchors.fill: parent
0122             sourceSize.width: parent.width
0123             sourceSize.height: parent.height
0124             
0125             rotation: car.isHorizontal ? 0 : 90
0126             transformOrigin: Item.TopLeft
0127             
0128             MultiPointTouchArea {
0129                 id: imageTouch
0130                 anchors.fill: parent
0131                 touchPoints: [ TouchPoint { id: imagePoint } ]
0132                 property real startX;
0133                 property real startY;
0134             
0135                 onPressed: {
0136                     if (!Activity.isMoving) {
0137                         Activity.isMoving = true;
0138                         car.isMoving = true;
0139                         car.audioEffects.play(Activity.baseUrl + "car.wav")
0140                         imageTouch.startX = imagePoint.x;
0141                         imageTouch.startY = imagePoint.y;
0142                     }
0143                 }
0144                 
0145                 onUpdated: {
0146                     if (car.isMoving && !Activity.haveWon) {
0147                         var deltaX = imagePoint.x - startX;
0148                         var deltaY = imagePoint.y - startY;
0149                         if (!car.isHorizontal) {
0150                             var w = deltaX;
0151                             deltaX = deltaY;
0152                             deltaY = w;
0153                         }
0154                         Activity.updateCarPosition(car, car.effX + deltaX, car.effY + deltaY);
0155                     }
0156                 }
0157 
0158                 onReleased: {
0159                     if (car.isMoving) {
0160                         Activity.isMoving = false;
0161                         car.isMoving = false;
0162                         if (!Activity.haveWon)
0163                             Activity.snapCarToGrid(car);
0164                     }
0165                 }
0166             }
0167         }
0168     }
0169     
0170     // note: the following leads to delayed dragging, therefore deactivated:
0171     //Behavior on x { PropertyAnimation { duration: 50 } }
0172     //Behavior on y { PropertyAnimation { duration: 50 } }
0173 }