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

0001 /* GCompris - Align42players.qml
0002  *
0003  * SPDX-FileCopyrightText: 2014 Bharath M S <brat.197@gmail.com>
0004  *
0005  * Authors:
0006  *   Laurent Lacheny <laurent.lacheny@wanadoo.fr> (GTK+ version)
0007  *   Bharath M S <brat.197@gmail.com> (Qt Quick port)
0008  *
0009  *   SPDX-License-Identifier: GPL-3.0-or-later
0010  */
0011 import QtQuick 2.12
0012 
0013 import "../../core"
0014 import "align4.js" as Activity
0015 
0016 import GCompris 1.0
0017 
0018 ActivityBase {
0019     id: activity
0020 
0021     property bool twoPlayer: true
0022 
0023     onStart: focus = true
0024     onStop: {}
0025 
0026     pageComponent: Image {
0027         id: background
0028         anchors.fill: parent
0029         source: Activity.url + "background.svg"
0030         sourceSize.width: width
0031         sourceSize.height: height
0032         fillMode: Image.PreserveAspectCrop
0033         signal start
0034         signal stop
0035 
0036         Component.onCompleted: {
0037             activity.start.connect(start)
0038             activity.stop.connect(stop)
0039         }
0040 
0041         // Add here the QML items you need to access in javascript
0042         QtObject {
0043             id: items
0044             property Item main: activity.main
0045             property alias background: background
0046             property alias fallingPiece: fallingPiece
0047             property alias pieces: pieces
0048             property alias dynamic: dynamic
0049             property alias drop: drop
0050             property alias player1score: player1score
0051             property alias player2score: player2score
0052             property int currentLevel: activity.currentLevel
0053             property alias bonus: bonus
0054             property alias repeater: repeater
0055             property alias columns: grid.columns
0056             property alias rows: grid.rows
0057             property alias trigTuxMove: trigTuxMove
0058             property int cellSize: background.width <= background.height ? (background.width / (columns + 3)) : (background.height / (rows + 4))
0059             property bool gameDone: false
0060             property int counter
0061             property int nextPlayerStart: 1
0062         }
0063 
0064         onStart: { Activity.start(items, twoPlayer) }
0065         onStop: { Activity.stop() }
0066 
0067         Keys.onPressed: {
0068             if(drop.running || bonus.isPlaying || (items.counter % 2 != 0 && !twoPlayer))
0069                 return
0070             if(items.gameDone && !bonus.isPlaying)
0071                 Activity.reset();
0072             if(event.key === Qt.Key_Right)
0073                 Activity.moveCurrentIndexRight();
0074             else if(event.key === Qt.Key_Left)
0075                 Activity.moveCurrentIndexLeft();
0076             else if(event.key === Qt.Key_Down || event.key === Qt.Key_Space)
0077                 Activity.handleDrop(Activity.currentLocation);
0078         }
0079 
0080         ListModel {
0081             id: pieces
0082         }
0083 
0084         // Tux move delay
0085         Timer {
0086             id: trigTuxMove
0087             repeat: false
0088             interval: 1500
0089             onTriggered: {
0090                 Activity.doMove()
0091                 items.player2score.endTurn()
0092                 items.player1score.beginTurn()
0093             }
0094         }
0095 
0096         Grid {
0097             id: grid
0098             z: 2
0099             anchors.horizontalCenter: parent.horizontalCenter
0100             anchors {
0101                 verticalCenter: parent.verticalCenter
0102                 horizontalCenter: parent.horizontalCenter
0103             }
0104 
0105             spacing: 5
0106             columns: 7
0107             rows: 6
0108 
0109             Repeater {
0110                 id: repeater
0111                 model: pieces
0112                 delegate: blueSquare
0113 
0114                 Component {
0115                     id: blueSquare
0116                     Rectangle {
0117                         color: "#DDAAAAAA";
0118                         width: items.cellSize
0119                         height: items.cellSize
0120                         radius: width / 2
0121                         border.color: "#FFFFFFFF"
0122                         border.width: 0
0123                         Piece {
0124                             anchors.fill: parent
0125                             state: stateTemp
0126                             sourceSize.width: items.cellSize
0127                         }
0128                     }
0129                 }
0130             }
0131 
0132             Piece {
0133                 id: fallingPiece
0134                 state: items.counter % 2 ? "2": "1"
0135                 sourceSize.width: items.cellSize
0136 
0137                 Behavior on x { PropertyAnimation { duration: 200 } }
0138             }
0139 
0140         }
0141 
0142         PropertyAnimation {
0143             id: drop
0144             target: fallingPiece
0145             properties: "y"
0146             duration: 720
0147             onStarted: activity.audioEffects.play(Activity.url + 'slide.wav')
0148             onStopped: {
0149                 dynamic.display()
0150                 Activity.continueGame()
0151             }
0152         }
0153 
0154         MouseArea {
0155             id: dynamic
0156             anchors.fill: parent
0157             enabled: hoverEnabled
0158             hoverEnabled: (!drop.running && !items.gameDone && (items.counter % 2 == 0 || twoPlayer))
0159 
0160             property bool holdMode: true
0161             function display() {
0162                 var coord = grid.mapFromItem(background, mouseX, mouseY)
0163                 Activity.setPieceLocation(coord.x, coord.y)
0164             }
0165 
0166             onPositionChanged: items.dynamic.enabled ? display() : ''
0167             onPressed: holdMode = false
0168             onPressAndHold: holdMode = true
0169             onClicked: {
0170                 display()
0171                 if(!holdMode) {
0172                     var coord = grid.mapFromItem(background, mouseX, mouseY)
0173                     var column = Activity.whichColumn(coord.x, coord.y)
0174                     Activity.handleDrop(column)
0175                 }
0176             }
0177         }
0178 
0179         DialogHelp {
0180             id: dialogHelp
0181             onClose: home()
0182         }
0183 
0184         Bar {
0185             id: bar
0186             level: items.currentLevel + 1
0187             content: BarEnumContent { value: help | home | reload | (twoPlayer ? 0 : level) }
0188             onHelpClicked: {
0189                 displayDialog(dialogHelp)
0190             }
0191             onHomeClicked: activity.home()
0192             onReloadClicked: {
0193                 Activity.reset()
0194             }
0195             onPreviousLevelClicked: Activity.previousLevel()
0196             onNextLevelClicked: Activity.nextLevel()
0197         }
0198 
0199         ScoreItem {
0200             id: player1score
0201             z: 1
0202             player: 1
0203             height: Math.min(background.height/7, Math.min(background.width/7, bar.height * 1.05))
0204             width: height*11/8
0205             anchors {
0206                 top: background.top
0207                 topMargin: 5
0208                 left: background.left
0209                 leftMargin: 5
0210             }
0211             playerImageSource: "qrc:/gcompris/src/core/resource/player_1.svg"
0212             backgroundImageSource: Activity.url + "score_1.svg"
0213         }
0214 
0215         ScoreItem {
0216             id: player2score
0217             z: 1
0218             player: 2
0219             height: Math.min(background.height/7, Math.min(background.width/7, bar.height * 1.05))
0220             width: height*11/8
0221             anchors {
0222                 top: background.top
0223                 topMargin: 5
0224                 right: background.right
0225                 rightMargin: 5
0226             }
0227             playerImageSource: "qrc:/gcompris/src/core/resource/player_2.svg"
0228             backgroundImageSource: Activity.url + "score_2.svg"
0229             playerScaleOriginX: player2score.width
0230         }
0231 
0232         Bonus {
0233             id: bonus
0234         }
0235     }
0236 }