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

0001 /* GCompris - Animal.qml
0002  *
0003  * SPDX-FileCopyrightText: 2022 Samarth Raj <mailforsamarth@gmail.com>
0004  * SPDX-FileCopyrightText: 2022 Timothée Giet <animtim@gmail.com>
0005  * SPDX-License-Identifier: GPL-3.0-or-later
0006  */
0007 import QtQuick 2.12
0008 
0009 import "../../core"
0010 import "left_right_click.js" as Activity
0011 
0012 Item {
0013     id: animal
0014     z: 10
0015     visible: !animalInvisible ? true : false
0016     property bool clicked: false
0017 
0018     Rectangle {
0019         id: animalRect
0020         anchors.horizontalCenter: parent.horizontalCenter
0021         anchors.verticalCenter: parent.verticalCenter
0022         width: parent.width * 0.8
0023         height: parent.height * 0.8
0024         color: "transparent"
0025         border.color: "transparent"
0026         border.width: 5
0027         radius: 10
0028         Image {
0029             width: parent.width * 0.9
0030             height: parent.height * 0.9
0031             sourceSize.width: width
0032             source: imageSource
0033             anchors.horizontalCenter: parent.horizontalCenter
0034             anchors.verticalCenter: parent.verticalCenter
0035             fillMode: Image.PreserveAspectFit
0036         }
0037         PropertyAnimation {
0038             id: disappearAnimal
0039             target: animal
0040             property: 'visible'
0041             to: false
0042             duration: 1000
0043         }
0044         PropertyAnimation {
0045             id: borderHighlight
0046             target: animalRect
0047             property: 'border.color'
0048             to: "#F2F2F2"
0049             duration: 0
0050         }
0051         MouseArea {
0052             id: mouseArea
0053             anchors.fill: parent
0054             acceptedButtons: Qt.LeftButton | Qt.RightButton
0055             enabled: !animal.clicked
0056             onClicked: {
0057                 // if left target animal is clicked with left click
0058                 if((mouse.button === Qt.LeftButton) && (animalIdentifier === Activity.Position.left)) {
0059                     animal.clicked = true
0060                     animal.state = "toLeftTarget"
0061                     Activity.incrementCounter()
0062                     disappearAnimal.running = true
0063                 }
0064                 else if((mouse.button === Qt.RightButton) && (animalIdentifier === Activity.Position.right)) {
0065                     animal.clicked = true
0066                     animal.state = "toRightTarget"
0067                     Activity.incrementCounter()
0068                     disappearAnimal.running = true
0069                 }
0070                 else {
0071                     wrongClick()
0072                 }
0073                 triggerClick(mouse.button)
0074             }
0075 
0076             function triggerClick(mouseButton) {
0077                 if(mouseButton === Qt.LeftButton)
0078                     leftClickTrigger()
0079                 else
0080                     rightClickTrigger()
0081             }
0082 
0083             hoverEnabled: enabled
0084             onEntered: {
0085                 animalRect.border.color = "#F2F2F2"
0086             }
0087             onExited: {
0088                 animalRect.border.color = "transparent"
0089             }
0090         }
0091     }
0092 
0093     states: [
0094         State {
0095             name: "toLeftTarget"
0096             ParentChange {
0097                 target: animal
0098                 parent: leftArea
0099             }
0100             PropertyChanges {
0101                 target: animal
0102                 x: (leftArea.width - animal.width) * 0.5
0103                 y: (leftArea.height - animal.height) * 0.75
0104             }
0105         },
0106         State {
0107             name: "toRightTarget"
0108             ParentChange {
0109                 target: animal
0110                 parent: rightArea
0111             }
0112             PropertyChanges {
0113                 target: animal
0114                 x: (leftArea.width - animal.width) * 0.5
0115                 y: (leftArea.height - animal.height) * 0.5
0116             }
0117         }
0118     ]
0119 
0120     Behavior on x {
0121         PropertyAnimation { easing.type: Easing.OutQuad; duration: 1000 }
0122     }
0123     Behavior on y {
0124         PropertyAnimation { easing.type: Easing.OutQuad; duration: 1000 }
0125     }
0126 }