Warning, /education/gcompris/src/core/ScoreItem.qml is written in an unsupported language. File is not indexed.

0001 /* GCompris - ScoreItem.qml
0002  *
0003  * SPDX-FileCopyrightText: 2017 Johnny Jazeix <jazeix@gmail.com>
0004  *
0005  * Authors:
0006  *   Pulkit Gupta <pulkitgenius@gmail.com> (main code)
0007  *   Johnny Jazeix <jazeix@gmail.com> (refactorisation)
0008  *
0009  *   SPDX-License-Identifier: GPL-3.0-or-later
0010  */
0011 import QtQuick 2.12
0012 import GCompris 1.0
0013 
0014 /**
0015  * A QML component to visualize number of wins.
0016  * @ingroup components
0017  *
0018  * ScoreItem consists of player score (@ref playerScore)
0019  * and player image (@ref playerImageSource).
0020  * Mostly used in multi-player activities.
0021  *
0022  * @inherit QtQuick.Item
0023  */
0024 Item {
0025     id: scoreItem
0026 
0027     /**
0028      * type:int
0029      * Id of the player.
0030      */
0031     property int player: 1
0032 
0033     /**
0034      * type:string
0035      * Source of background image to display.
0036      *
0037      * @sa backgroundImage.source
0038      */
0039     property string backgroundImageSource
0040 
0041     /**
0042      * type:string
0043      * Source of player image to display.
0044      *
0045      * @sa playerImage.source
0046      */
0047     property string playerImageSource
0048 
0049     /**
0050      * type:int
0051      * Count of score(i.e. number of wins).
0052      *
0053      * @sa scoreLabel.text
0054      */
0055     property int playerScore
0056 
0057     /**
0058      * type:int
0059      * Holds the point from which the player image
0060      * is scaled on x-axis.
0061      *
0062      * @sa scaleTransform.origin.x
0063      */
0064     property int playerScaleOriginX
0065 
0066     /**
0067      * type:int
0068      * Holds the point from which the player image
0069      * is scaled on y-axis.
0070      *
0071      * @sa scaleTransform.origin.y
0072      */
0073     property int playerScaleOriginY
0074 
0075     /**
0076      * Emitted when the win animation should be started.
0077      *
0078      * Triggers scale, rotation animation and increases playerScore count.
0079      */
0080     signal win
0081 
0082     /**
0083      * Emitted when the player turn should be started.
0084      *
0085      * Triggers scale and rotation animation.
0086      */
0087     signal beginTurn
0088 
0089     /**
0090      * Emitted when the player turn should be ended.
0091      *
0092      * Triggers shrink and rotation animation on player image.
0093      */
0094     signal endTurn
0095 
0096     /**
0097      * type:bool
0098      * Wether it is player's turn.
0099      */
0100     property bool playersTurn: false
0101 
0102     /**
0103      * type:alias
0104      * allow to access properties of playerItem
0105      * Usually you'll need set its source, height, anchors.leftMargin and anchors.bottom.margin
0106      */
0107     property alias playerItem: playerItem
0108 
0109     onBeginTurn: {
0110         scaleAnimation.start()
0111         playersTurn = true
0112     }
0113 
0114     onEndTurn: {
0115         playersTurn = false
0116         scaleAnimation.stop()
0117         playerImageRotate.stop()
0118         playerImage.rotation = 0
0119         shrinkAnimation.start()
0120         backgroundRectangle.state = "second"
0121     }
0122 
0123     onWin: {
0124         scaleAnimation.start()
0125         backgroundRectangle.state = "win"
0126         playerScore ++;
0127     }
0128 
0129     PropertyAnimation {
0130         id: scaleAnimation
0131         target: scaleTransform
0132         properties: "scale"
0133         from: 1.0
0134         to: 1.4
0135         duration: 500
0136         onStarted: {
0137             backgroundRectangle.state = "first"
0138             playerImageRotate.start()
0139         }
0140     }
0141 
0142     PropertyAnimation {
0143         id: shrinkAnimation
0144         target: scaleTransform
0145         properties: "scale"
0146         from: 1.4
0147         to: 1.0
0148         duration: 500
0149     }
0150 
0151     SequentialAnimation {
0152         id: playerImageRotate
0153         loops: Animation.Infinite
0154         NumberAnimation {
0155             target: playerImage
0156             property: "rotation"
0157             from: -30; to: 30
0158             duration: 750
0159             easing.type: Easing.InOutQuad
0160         }
0161         NumberAnimation {
0162             target: playerImage
0163             property: "rotation"
0164             from: 30; to: -30
0165             duration: 750
0166             easing.type: Easing.InOutQuad
0167         }
0168     }
0169 
0170     Rectangle {
0171         id: backgroundRectangle
0172         anchors.fill: parent
0173         radius: 15
0174         state: "second"
0175 
0176         Image {
0177             id: backgroundImage
0178             source: backgroundImageSource
0179             sourceSize.height: height * 1.4
0180             sourceSize.width: width * 1.4
0181             anchors.fill: parent
0182             anchors.margins: parent.height * 0.04
0183 
0184             Image {
0185                 id: playerImage
0186                 source: playerImageSource
0187                 fillMode: Image.PreserveAspectFit
0188                 height: parent.height*0.8
0189                 sourceSize.height: height * 1.4
0190                 x: parent.width*0.06
0191                 anchors.verticalCenter: parent.verticalCenter
0192                 Image {
0193                     id: playerItem
0194                     source: "qrc:/gcompris/src/core/resource/empty.svg"
0195                     fillMode: Image.PreserveAspectFit
0196                     height: 0
0197                     sourceSize.height: height * 1.4
0198                     anchors.bottom: parent.bottom
0199                     anchors.left: parent.left
0200                     anchors.leftMargin: 0
0201                     anchors.bottomMargin: 0
0202                 }
0203             }
0204             GCText {
0205                 id: scoreLabel
0206                 x: parent.width * 0.65
0207                 anchors.verticalCenter: parent.verticalCenter
0208                 height: parent.height * 0.8
0209                 width: parent.width * 0.3
0210                 horizontalAlignment: Text.AlignHCenter
0211                 verticalAlignment: Text.AlignVCenter
0212                 color: "#2a2a2a"
0213                 fontSizeMode: Text.Fit
0214                 text: playerScore
0215             }
0216         }
0217 
0218         states: [
0219         State {
0220             name: "first"
0221             PropertyChanges {
0222                 target: backgroundRectangle
0223                 color: "#80ffffff"
0224             }
0225             PropertyChanges {
0226                 target: playerImage
0227                 source: playerImageSource
0228             }
0229             PropertyChanges {
0230                 target: playerItem
0231                 visible: true
0232             }
0233         },
0234         State {
0235             name: "second"
0236             PropertyChanges {
0237                 target: backgroundRectangle
0238                 color: "transparent"
0239             }
0240             PropertyChanges {
0241                 target: playerImage
0242                 source: playerImageSource
0243             }
0244             PropertyChanges {
0245                 target: playerItem
0246                 visible: true
0247             }
0248         },
0249         State {
0250             name: "win"
0251             PropertyChanges {
0252                 target: playerImage
0253                 source: "qrc:/gcompris/src/core/resource/win.svg"
0254             }
0255             PropertyChanges {
0256                 target: playerItem
0257                 visible: false
0258             }
0259             PropertyChanges {
0260                 target: backgroundRectangle
0261                 color: "#80ffffff"
0262             }
0263         }
0264         ]
0265 
0266         transform: Scale {
0267             id: scaleTransform
0268             property real scale: 1
0269             origin.x: playerScaleOriginX
0270             origin.y: playerScaleOriginY
0271             xScale: scale
0272             yScale: scale
0273         }
0274     }
0275 }