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

0001 /* GCompris - bargame.qml
0002  *
0003  * SPDX-FileCopyrightText: 2016 UTKARSH TIWARI <iamutkarshtiwari@kde.org>
0004  *
0005  * Authors:
0006  *   Yves Combe (GTK+ version)
0007  *   UTKARSH TIWARI <iamutkarshtiwari@kde.org> (Qt Quick port)
0008  *
0009  *   SPDX-License-Identifier: GPL-3.0-or-later
0010  */
0011 import QtQuick 2.12
0012 import GCompris 1.0
0013 
0014 import "../../core"
0015 import "bargame.js" as Activity
0016 
0017 ActivityBase {
0018     id: activity
0019     property int gameMode: 1
0020 
0021     onStart: focus = true
0022     onStop: {}
0023 
0024     pageComponent: Image {
0025         id: rootWindow
0026         source: Activity.url + "background.svg"
0027         sourceSize.width: width
0028         sourceSize.height: height
0029         fillMode: Image.PreserveAspectCrop
0030         signal start
0031         signal stop
0032         property bool horizontalLayout: background.width >= background.height
0033 
0034         Component.onCompleted: {
0035             dialogActivityConfig.initialize()
0036             activity.start.connect(start)
0037             activity.stop.connect(stop)
0038         }
0039 
0040         // Add here the QML items you need to access in javascript
0041         QtObject {
0042             id: items
0043             property Item main: activity.main
0044             property int numberOfBalls: 1
0045             property alias answerBallsPlacement: answerBallsPlacement
0046             property int currentLevel: activity.currentLevel
0047             property alias bonus: bonus
0048             property alias okArea: okArea
0049 
0050             property alias trigTuxMove: trigTuxMove
0051 
0052             property alias player1score: player1score
0053             property alias player2score: player2score
0054 
0055             property int mode: 1
0056             property int gridSize: (horizontalLayout ? rootWindow.width : (rootWindow.height - bar.height * 1.2)) / Activity.levelsProperties[items.mode - 1].boardSize
0057             property bool isPlayer1Beginning: true
0058             property bool isPlayer1Turn: true
0059         }
0060 
0061         onStart: {
0062             Activity.start(items, gameMode);
0063             Activity.calculateWinPlaces();
0064         }
0065         onStop: { Activity.stop() }
0066 
0067         // Tux move delay
0068         Timer {
0069             id: trigTuxMove
0070             repeat: false
0071             interval: 1500
0072             onTriggered: Activity.machinePlay()
0073         }
0074 
0075         // Tux image
0076         Image {
0077             id: tux
0078             visible: gameMode == 1
0079             source: "qrc:/gcompris/src/activities/ballcatch/resource/tux.svg"
0080             height: rootWindow.height * 0.2
0081             width: tux.height
0082             y: rootWindow.height * 0.4
0083             anchors {
0084                 right: rootWindow.right
0085                 rightMargin: 23 * ApplicationInfo.ratio
0086 
0087             }
0088             MouseArea {
0089                 id: tuxArea
0090                 hoverEnabled: enabled
0091                 enabled: gameMode == 1 && !answerBallsPlacement.children[0].visible
0092                 anchors.fill: parent
0093                 onClicked: {
0094                     items.isPlayer1Turn = false;
0095                     Activity.machinePlay();
0096                 }
0097             }
0098             states: State {
0099                 name: "tuxHover"
0100                 when: tuxArea.containsMouse
0101                 PropertyChanges {
0102                     target: tux
0103                     scale: 1.1
0104                 }
0105             }
0106         }
0107 
0108         // Box row
0109         Item {
0110             id: boxModel
0111 
0112             states: [
0113                 State {
0114                     name: "horizontalBar"
0115                     when: horizontalLayout
0116                     PropertyChanges { target: boxModel; x: 0; y: rootWindow.height - bar.height * 2}
0117                 },
0118                 State {
0119                     name: "verticalBar"
0120                     when: !horizontalLayout
0121                     PropertyChanges { target: boxModel; x: rootWindow.width * 0.5; y: 0}
0122                 }
0123             ]
0124 
0125             transform: Rotation {
0126                 origin.x: 0;
0127                 origin.y: 0;
0128                 angle: horizontalLayout ? 0 : 90
0129             }
0130 
0131             // The empty boxes grid
0132             Grid {
0133                 id: boxes
0134                 rows: 1
0135                 columns: Activity.levelsProperties[items.mode - 1].boardSize
0136                 anchors.centerIn: boxModel.Center
0137                 Repeater {
0138                     id: startCase
0139                     model: boxes.columns
0140                     Image {
0141                         id: greenCase
0142                         source: Activity.url + ((index == boxes.columns - 1) ? "case_last.svg" : "case.svg")
0143                         sourceSize.width: items.gridSize
0144                         width: sourceSize.width
0145                         height: sourceSize.width
0146                         visible: true
0147                     }
0148                 }
0149             }
0150 
0151             // Hidden Answer Balls
0152             Grid {
0153                 // All green balls placement
0154                 id: answerBallsPlacement
0155                 anchors.centerIn: boxModel.Center
0156                 columns: Activity.levelsProperties[items.mode - 1].boardSize
0157                 rows: 1
0158                 Repeater {
0159                     model: answerBallsPlacement.columns
0160                     Image {
0161                         source: Activity.url + "ball_1.svg"
0162                         sourceSize.width: items.gridSize
0163                         width: sourceSize.width
0164                         height: sourceSize.width
0165                         visible: false
0166                     }
0167                 }
0168             }
0169 
0170             // Masks
0171             Grid {
0172                 id: masks
0173                 anchors.centerIn: boxModel.Center
0174                 rows: 1
0175                 columns: Activity.levelsProperties[items.mode - 1].boardSize
0176                 Repeater {
0177                     id: startMask
0178                     model: masks.columns
0179                     Image {
0180                         id: greenMask
0181                         source: Activity.url + ((index == boxes.columns - 1) ? "mask_last.svg" : "mask.svg")
0182                         sourceSize.width: items.gridSize
0183                         width: sourceSize.width
0184                         height: sourceSize.width
0185                         // Numbering label
0186                         Rectangle {
0187                             id: bgNbTxt
0188                             visible: ((index + 1) % 5 == 0 && index > 0) ? true : false
0189                             color: "#42FFFFFF"
0190                             height: numberText.height * 1.2
0191                             width: height
0192                             radius: height / 2
0193                             anchors {
0194                                 horizontalCenter: parent.horizontalCenter
0195                                 bottom: parent.top
0196                                 bottomMargin: (horizontalLayout ? 4 * ApplicationInfo.ratio : -16 * ApplicationInfo.ratio)
0197                             }
0198                             GCText {
0199                                 id: numberText
0200                                 text: index + 1
0201                                 color: "#373737"
0202                                 fontSize: smallSize
0203                                 font.bold: true
0204                                 visible: ((index + 1) % 5 == 0 && index > 0) ? true : false
0205                                 anchors {
0206                                     horizontalCenter: bgNbTxt.horizontalCenter
0207                                     verticalCenter: bgNbTxt.verticalCenter
0208                                 }
0209                             }
0210                             transform: Rotation {
0211                                 angle: horizontalLayout ? 0 : -90
0212                             }
0213                         }
0214                     }
0215                 }
0216             }
0217         }
0218 
0219         // ok button
0220         Image {
0221             id: playLabel
0222             width: ballNumberPlate.height
0223             height: width
0224             sourceSize.width: width
0225             sourceSize.height: width
0226             source: Activity.url + "bar_ok.svg"
0227             anchors {
0228                 left: ballNumberPlate.right
0229                 verticalCenter: ballNumberPlate.verticalCenter
0230                 leftMargin: width / 4
0231             }
0232 
0233             MouseArea {
0234                 id: okArea
0235                 anchors.fill: parent
0236                 hoverEnabled: enabled
0237                 enabled: true
0238                 onClicked: {
0239                     var value = items.numberOfBalls
0240                     if (gameMode == 1 || items.isPlayer1Turn) {
0241                         Activity.play(1, value);
0242                     } else {
0243                         Activity.play(2, value);
0244                     }
0245                     // reset next action
0246                     items.numberOfBalls = Activity.levelsProperties[items.mode - 1].minNumberOfBalls
0247                 }
0248             }
0249             states: State {
0250                 name: "mouseHover"
0251                 when: okArea.containsMouse
0252                 PropertyChanges {
0253                     target: playLabel
0254                     scale: 1.2
0255                 }
0256             }
0257         }
0258 
0259         // Number of balls to be placed
0260         Image {
0261             id: ballNumberPlate
0262             y: rootWindow.height * 0.32
0263             source: items.isPlayer1Turn ? Activity.url + "score_1.svg" :
0264                                           Activity.url + "score_2.svg"
0265             width: bar.height
0266             height: bar.height * 0.7
0267             sourceSize.width: width
0268             sourceSize.height: height
0269 
0270             anchors {
0271                 left: rootWindow.left
0272                 leftMargin: 16 * ApplicationInfo.ratio
0273             }
0274 
0275             MouseArea {
0276                 id: numberPlateArea
0277                 anchors.fill: parent
0278                 hoverEnabled: enabled
0279                 enabled: (gameMode == 1 && items.isPlayer1Turn == false) ? false : true
0280                 onClicked: {
0281                     items.numberOfBalls ++;
0282                     var max = Activity.levelsProperties[items.mode - 1].maxNumberOfBalls
0283                     if (items.numberOfBalls > max) {
0284                         items.numberOfBalls = Activity.levelsProperties[items.mode - 1].minNumberOfBalls;
0285                     }
0286                 }
0287             }
0288             states: State {
0289                 name: "numberHover"
0290                 when: numberPlateArea.containsMouse
0291                 PropertyChanges {
0292                     target: ballNumberPlate
0293                     scale: 1.1
0294                 }
0295             }
0296 
0297             // Ball Icon
0298             Image {
0299                 id: ballIcon
0300                 source: items.isPlayer1Turn ? Activity.url + "ball_1b.svg" :
0301                                               Activity.url + "ball_2b.svg"
0302                 sourceSize.width: parent.height * 0.8
0303                 width: sourceSize.width
0304                 height: sourceSize.width
0305                 anchors {
0306                     verticalCenter: ballNumberPlate.verticalCenter
0307                     left: ballNumberPlate.left
0308                     leftMargin: 10
0309                 }
0310             }
0311             // Number label
0312             GCText {
0313                 id: numberLabel
0314                 text: items.numberOfBalls
0315                 color: "#C04040"
0316                 font.bold: true
0317                 fontSize: smallSize
0318                 anchors {
0319                     right: ballNumberPlate.right
0320                     rightMargin: 10
0321                     verticalCenter: ballNumberPlate.verticalCenter
0322                 }
0323             }
0324         }
0325 
0326         ScoreItem {
0327             id: player1score
0328             player: 1
0329             height: Math.min(rootWindow.height/7, Math.min(rootWindow.width/7, bar.height * 1.05))
0330             width: height*11/8
0331             anchors {
0332                 top: rootWindow.top
0333                 topMargin: 5
0334                 left: rootWindow.left
0335                 leftMargin: 5
0336             }
0337             playerImageSource: "qrc:/gcompris/src/core/resource/player_1.svg"
0338             backgroundImageSource: Activity.url + "score_1.svg"
0339             playerItem.source: Activity.url + "ball_1b.svg"
0340             playerItem.height: playerItem.parent.height * 0.3
0341             playerItem.anchors.leftMargin: playerItem.parent.height * 0.05
0342             playerItem.anchors.bottomMargin: playerItem.parent.height * 0.05
0343         }
0344 
0345         ScoreItem {
0346             id: player2score
0347             player: 2
0348             height: Math.min(rootWindow.height/7, Math.min(rootWindow.width/7, bar.height * 1.05))
0349             width: height*11/8
0350             anchors {
0351                 top: rootWindow.top
0352                 topMargin: 5
0353                 right: rootWindow.right
0354                 rightMargin: 5
0355             }
0356             playerImageSource: "qrc:/gcompris/src/core/resource/player_2.svg"
0357             backgroundImageSource: Activity.url + "score_2.svg"
0358             playerScaleOriginX: player2score.width
0359             playerItem.source: Activity.url + "ball_2b.svg"
0360             playerItem.height: playerItem.parent.height * 0.3
0361             playerItem.anchors.leftMargin: playerItem.parent.height * 0.05
0362             playerItem.anchors.bottomMargin: playerItem.parent.height * 0.10
0363         }
0364 
0365         DialogHelp {
0366             id: dialogHelp
0367             onClose: home()
0368         }
0369 
0370         DialogChooseLevel {
0371             id: dialogActivityConfig
0372             currentActivity: activity.activityInfo
0373 
0374             onClose: home()
0375 
0376             onLoadData: {
0377                 if(activityData && activityData["mode"]) {
0378                     items.mode = activityData["mode"];
0379                 }
0380             }
0381             onStartActivity: {
0382                 rootWindow.stop()
0383                 rootWindow.start()
0384             }
0385         }
0386 
0387         Bar {
0388             id: bar
0389             level: items.currentLevel + 1
0390             content: BarEnumContent { value: help | home | level | reload | activityConfig }
0391             onHelpClicked: {
0392                 displayDialog(dialogHelp)
0393             }
0394             onPreviousLevelClicked: Activity.previousLevel()
0395             onNextLevelClicked: Activity.nextLevel()
0396             onHomeClicked: activity.home()
0397             onActivityConfigClicked: {
0398                 displayDialog(dialogActivityConfig)
0399             }
0400             onReloadClicked: Activity.restartLevel()
0401         }
0402 
0403         Bonus {
0404             id: bonus
0405         }
0406     }
0407 }