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

0001 /* GCompris - Piece.qml
0002  *
0003  * SPDX-FileCopyrightText: 2016 Pulkit Gupta <pulkitnsit@gmail.com>
0004  *
0005  * Authors:
0006  *   Pulkit Gupta <pulkitnsit@gmail.com>
0007  *
0008  *   SPDX-License-Identifier: GPL-3.0-or-later
0009  */
0010 import QtQuick 2.12
0011 
0012 import "../../core"
0013 import "nine_men_morris.js" as Activity
0014 
0015 import GCompris 1.0
0016 
0017 Image {
0018     id: piece
0019     property QtObject pieceParent
0020     property QtObject visualParent
0021     property double moveX
0022     property double moveY
0023     property int parentIndex: -1
0024     property bool canBeRemoved: false
0025     property bool firstPhase
0026     property bool isSelected: false
0027     property bool playSecond
0028     property bool gameDone
0029     property bool pieceBeingMoved
0030     property int chance
0031     opacity: 1.0
0032     sourceSize.height: height
0033     width: height
0034 
0035     ParallelAnimation {
0036         id: pieceAnimation
0037         NumberAnimation {
0038             target: piece
0039             easing.type: Easing.OutQuad
0040             property: "x"
0041             to: moveX
0042             duration: 430
0043         }
0044         NumberAnimation {
0045             target: piece
0046             easing.type: Easing.OutQuad
0047             property: "y"
0048             to: moveY
0049             duration: 430
0050         }
0051         onStarted: {
0052             piece.anchors.verticalCenter = undefined
0053             piece.anchors.centerIn = undefined
0054         }
0055         onStopped: {
0056             piece.parent = visualParent
0057             piece.anchors.centerIn = visualParent
0058             piece.pieceParent.state = piece.state
0059             piece.pieceParent.pieceIndex = index
0060             if (Activity.checkMill(piece.parentIndex,piece.state))
0061                 Activity.updateRemovablePiece()
0062             else if (firstPhase)
0063                 Activity.continueGame()
0064             else
0065                 Activity.checkGameWon()
0066         }
0067     }
0068 
0069     NumberAnimation {
0070         id: removePieceAnimation
0071         target: piece
0072         property: "opacity"
0073         to: 0
0074         duration: 430
0075         onStarted: { Activity.removePieceSelected(index) }
0076         onStopped: { Activity.removePiece(index) }
0077     }
0078 
0079     states: [
0080         State {
0081             name: "1" // Player 1
0082             PropertyChanges {
0083                 target: piece
0084                 source: playSecond ? Activity.url + "black_piece.svg" : Activity.url + "white_piece.svg"
0085             }
0086         },
0087         State {
0088             name: "2" // Player 2
0089             PropertyChanges {
0090                 target: piece
0091                 source: playSecond ? Activity.url + "white_piece.svg" : Activity.url + "black_piece.svg"
0092             }
0093         }
0094     ]
0095 
0096     MouseArea {
0097         id: area
0098         property bool turn: chance ? piece.state == "2" : piece.state == "1"
0099         enabled: ((canBeRemoved && !turn) || (!firstPhase && turn)) &&
0100                   (piece.parentIndex != -1) && !gameDone && (!pieceBeingMoved || canBeRemoved)
0101         anchors.centerIn: parent
0102         width: parent.width
0103         height: parent.height
0104         onClicked: {
0105             if (canBeRemoved)
0106                 removePieceAnimation.start()
0107             else {
0108                 isSelected = true
0109                 Activity.pieceSelected(index);
0110             }
0111         }
0112     }
0113 
0114     Rectangle {
0115         id: boundary
0116         anchors.centerIn: piece
0117         width: piece.width
0118         height: width
0119         visible: ((piece.visible && area.enabled && firstPhase) || isSelected) || canBeRemoved
0120         opacity: 1
0121         radius: width * 0.5
0122         border.width: width * 0.1
0123         border.color: canBeRemoved ? "#DC3D3D" : "#74F474" // red : green same as in DragPoint.qml
0124         color: "transparent"
0125         z: -1
0126     }
0127 
0128     function move(pieceChangeParent) {
0129         piece.pieceParent = pieceChangeParent
0130         piece.parentIndex = pieceChangeParent.index
0131         piece.visualParent = items.piecesLayout.itemAt(piece.parentIndex)
0132         piece.height = Qt.binding(function() { return pieceParent.width * 2.5 })
0133         var coord = piece.parent.mapFromItem(pieceChangeParent.parent, pieceChangeParent.x + pieceChangeParent.width / 2 -
0134                     piece.width / 2, pieceChangeParent.y + pieceChangeParent.height / 2 - piece.height / 2)
0135         piece.moveX = coord.x
0136         piece.moveY = coord.y
0137         pieceAnimation.start()
0138     }
0139 
0140     function remove() {
0141         removePieceAnimation.start()
0142     }
0143 }