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

0001 /* GCompris - Share.qml
0002  *
0003  * SPDX-FileCopyrightText: 2016 Stefan Toncu <stefan.toncu29@gmail.com>
0004  *
0005  *   SPDX-License-Identifier: GPL-3.0-or-later
0006  */
0007 
0008 import QtQuick 2.12
0009 import GCompris 1.0
0010 
0011 import "../../core"
0012 import "share.js" as Activity
0013 
0014 ActivityBase {
0015     id: activity
0016 
0017     onStart: focus = true
0018     onStop: {}
0019 
0020     pageComponent: Rectangle {
0021         id: background
0022         anchors.fill: parent
0023         color: "#abcdef"
0024         signal start
0025         signal stop
0026 
0027         Component.onCompleted: {
0028             dialogActivityConfig.initialize()
0029             activity.start.connect(start)
0030             activity.stop.connect(stop)
0031         }
0032 
0033         // Add here the QML items you need to access in javascript
0034         QtObject {
0035             id: items
0036             property Item main: activity.main
0037             property alias background: background
0038             property int currentLevel: activity.currentLevel
0039             property alias bonus: bonus
0040             property alias score: score
0041             property alias errorRectangle: errorRectangle
0042             property alias instruction: instruction
0043             property int nbSubLevel
0044             property alias listModel: listModel
0045             property bool acceptCandy: false
0046             property alias girlWidget: girlWidget
0047             property alias boyWidget: boyWidget
0048             property alias candyWidget: candyWidget
0049             property alias basketWidget: basketWidget
0050             property alias leftWidget: leftWidget
0051             property int totalBoys
0052             property int totalGirls
0053             property int totalCandies
0054             property int totalChildren: totalBoys + totalGirls
0055             readonly property var levels: activity.datasetLoader.data
0056             property int barHeightAddon: ApplicationSettings.isBarHidden ? 1 : 3
0057             property int cellSize: Math.round(Math.min(background.width / 12, background.height / (11 + barHeightAddon)))
0058             property alias repeaterDropAreas: repeaterDropAreas
0059             property int maxNumberOfCandiesPerWidget: 6
0060             property bool buttonsBlocked: false
0061         }
0062 
0063         onStart: { Activity.start(items) }
0064         onStop: { Activity.stop() }
0065 
0066         property bool vert: background.width >= background.height
0067         property int currentBoys: 0
0068         property int currentGirls: 0
0069         property int currentCandies: 0
0070         property int rest
0071         property int placedInGirls
0072         property int placedInBoys
0073         property bool easyMode: true
0074         property alias wrongMove: wrongMove
0075 
0076         //returns true if the x and y is in the "dest" area
0077         function contains(x, y, dest) {
0078             return (x > dest.x && x < dest.x + dest.width &&
0079                     y > dest.y && y < dest.y + dest.height)
0080         }
0081 
0082         //stop the candy rotation
0083         function resetCandy() {
0084             items.acceptCandy = false;
0085             candyWidget.element.rotation = 0
0086         }
0087 
0088         //check if the answer is correct
0089         function check() {
0090             background.resetCandy()
0091             items.buttonsBlocked = true
0092 
0093             var ok = 0
0094             var okRest = 0
0095 
0096             if (listModel.count >= items.totalChildren) {
0097                 for (var i = 0 ; i < listModel.count ; i++) {
0098                     if (listModel.get(i).nameS === "basket")
0099                         okRest = listModel.get(i).countS
0100                     else if (listModel.get(i).countS === Math.floor(items.totalCandies/items.totalChildren))
0101                         ok ++
0102                 }
0103 
0104                 //condition without or with rest
0105                 if ((rest == 0 && ok == items.totalChildren) || (rest == okRest && ok == items.totalChildren))  {
0106                     score.currentSubLevel++
0107                     score.playWinAnimation()
0108                     activity.audioEffects.play("qrc:/gcompris/src/core/resource/sounds/completetask.wav")
0109                     return
0110                 }
0111             }
0112 
0113             //else => bad
0114             errorRectangle.startAnimation()
0115             activity.audioEffects.play("qrc:/gcompris/src/core/resource/sounds/crash.wav")
0116         }
0117 
0118         //center zone
0119         Rectangle {
0120             id: grid
0121             z: 4
0122 
0123             //map the coordinates from widgets to grid
0124             property var boy: leftWidget.mapFromItem(boyWidget, boyWidget.element.x, boyWidget.element.y)
0125             property var girl: leftWidget.mapFromItem(girlWidget, girlWidget.element.x, girlWidget.element.y)
0126             property var basket: leftWidget.mapFromItem(basketWidget, basketWidget.element.x, basketWidget.element.y)
0127 
0128             //show that the widget can be dropped here
0129             color: background.contains(boy.x, boy.y, grid) ||
0130                    background.contains(girl.x, girl.y, grid) ||
0131                    background.contains(basket.x, basket.y, grid) ? "#d5e6f7" : "transparent"
0132 
0133             anchors {
0134                 top: background.vert ? parent.top : leftWidget.bottom
0135                 left: background.vert ? leftWidget.right : parent.left
0136                 topMargin: 20
0137                 leftMargin: 20
0138             }
0139 
0140             width: background.vert ?
0141                        background.width - leftWidget.width - 40 : background.width - 40
0142             height: ApplicationSettings.isBarHidden ?
0143                         background.height : background.vert ?
0144                             background.height - (bar.height * 1.1) :
0145                             background.height - (bar.height * 1.1) - leftWidget.height
0146 
0147             //shows/hides the Instruction
0148             MouseArea {
0149                 anchors.fill: parent
0150                 enabled: !items.buttonsBlocked
0151                 // first hide the wrong move if visible, then show/hide instruction
0152                 onClicked: wrongMove.visible ? wrongMove.visible = false :
0153                            (instruction.opacity === 0) ?
0154                                instruction.show() : instruction.hide()
0155             }
0156 
0157             ListModel {
0158                 id: listModel
0159             }
0160 
0161             Flow {
0162                 id: dropAreas
0163                 spacing: 10
0164 
0165                 width: parent.width
0166                 height: parent.height
0167 
0168                 Repeater {
0169                     id: repeaterDropAreas
0170                     model: listModel
0171 
0172                     DropChild {
0173                         id: rect2
0174                         //"nameS" from listModel
0175                         name: nameS
0176                     }
0177                 }
0178             }
0179         }
0180 
0181         ErrorRectangle {
0182             id: errorRectangle
0183             z: 20
0184             anchors.fill: grid
0185             imageSize: 60 * ApplicationInfo.ratio
0186             function releaseControls() { items.buttonsBlocked = false; }
0187         }
0188 
0189         //instruction rectangle
0190         Rectangle {
0191             id: instruction
0192             anchors.fill: instructionTxt
0193             opacity: 0.8
0194             radius: 10
0195             border.width: 2
0196             z: 20
0197             border.color: "#DDD"
0198             color: "#373737"
0199             
0200             property alias text: instructionTxt.text
0201 
0202             Behavior on opacity { PropertyAnimation { duration: 200 } }
0203 
0204             //shows/hides the Instruction
0205             MouseArea {
0206                 anchors.fill: parent
0207                 onClicked: instruction.hide()
0208                 enabled: instruction.opacity !== 0 && !items.buttonsBlocked
0209             }
0210 
0211             function show() {
0212                 if(text)
0213                     opacity = 1
0214             }
0215             function hide() {
0216                 opacity = 0
0217             }
0218         }
0219 
0220         //instruction for playing the game
0221         GCText {
0222             id: instructionTxt
0223             anchors {
0224                 top: background.vert ? parent.top : leftWidget.bottom
0225                 topMargin: 10
0226                 horizontalCenter: grid.horizontalCenter
0227             }
0228             opacity: instruction.opacity
0229             z: instruction.z
0230             fontSize: background.vert ? regularSize : smallSize
0231             color: "white"
0232             horizontalAlignment: Text.AlignHCenter
0233             width: Math.max(Math.min(parent.width * 0.8, text.length * 8), parent.width * 0.3)
0234             wrapMode: TextEdit.WordWrap
0235         }
0236 
0237         //left widget, with girl/boy/candy/basket widgets in a grid
0238         Rectangle {
0239             id: leftWidget
0240             width: background.vert ?
0241                        items.cellSize * 2.04 : background.width
0242             height: background.vert ?
0243                         background.height : items.cellSize * 2.04
0244             color: "#5a9de0"
0245             border.color: "#3f81c4"
0246             border.width: 4
0247             z: 4
0248 
0249             //grid with ok button and images of a boy, a girl, a candy, a basket and the button to display the instructions
0250             Grid {
0251                 id: view
0252                 x: 10
0253                 y: 10
0254 
0255                 width: background.vert ? leftWidget.width : 3 * bar.height
0256                 height: background.vert ? background.height - 2 * bar.height : bar.height
0257                 spacing: 10
0258                 columns: background.vert ? 1 : 6
0259 
0260                 //ok button
0261                 Image {
0262                     id: okButton
0263                     source:"qrc:/gcompris/src/core/resource/bar_ok.svg"
0264                     sourceSize.width: items.cellSize * 1.5 - view.x / 2
0265                     fillMode: Image.PreserveAspectFit
0266 
0267                     MouseArea {
0268                         id: mouseArea
0269                         anchors.fill: parent
0270                         enabled: !items.buttonsBlocked
0271                         onPressed: okButton.opacity = 0.6
0272                         onReleased: okButton.opacity = 1
0273                         onClicked: background.check()
0274                     }
0275                 }
0276 
0277                 ChildWidget {
0278                     id: girlWidget
0279                     name: "girl"
0280                     total: items.totalGirls
0281                     visible: items.totalGirls !== 0
0282                     current: background.currentGirls
0283                     placedInChild: background.placedInGirls
0284                 }
0285 
0286                 ChildWidget {
0287                     id: boyWidget
0288                     name: "boy"
0289                     visible: items.totalBoys !== 0
0290                     total: items.totalBoys
0291                     current: background.currentBoys
0292                     placedInChild: background.placedInBoys
0293                 }
0294                 
0295                 BasketWidget {
0296                     id: basketWidget
0297                 }
0298 
0299                 CandyWidget {
0300                     id: candyWidget
0301                     total: items.totalCandies
0302                     current: background.currentCandies
0303                     element.opacity: background.easyMode ? 1 : 0
0304                 }
0305 
0306                 Image {
0307                     id: showInstruction
0308                     source:"qrc:/gcompris/src/core/resource/bar_hint.svg"
0309                     sourceSize.width: items.cellSize * 1.5 - view.x / 2
0310                     fillMode: Image.PreserveAspectFit
0311 
0312                     MouseArea {
0313                         anchors.fill: parent
0314                         enabled: !items.buttonsBlocked
0315                         onPressed: showInstruction.opacity = 0.6
0316                         onReleased: showInstruction.opacity = 1
0317                         onClicked: items.instruction.opacity == 0 ? items.instruction.show() : items.instruction.hide()
0318                     }
0319                 }
0320             }
0321         }
0322 
0323         // show message warning for placing too many candies in one area
0324         Rectangle {
0325             id: wrongMove
0326             z: 5
0327             color: "orange"
0328             radius: width / height * 10
0329             visible: false
0330 
0331             width: grid.width
0332             height: grid.height / 3
0333             anchors.centerIn: grid
0334 
0335             MouseArea {
0336                 anchors.fill: parent
0337                 onClicked: parent.visible = false && !items.buttonsBlocked
0338             }
0339             GCText {
0340                 id: wrongMoveText
0341                 horizontalAlignment: Text.AlignHCenter
0342                 verticalAlignment: Text.AlignVCenter
0343                 width: parent.width - 2 // -2 for margin
0344                 height: parent.height
0345                 fontSizeMode: Text.Fit
0346                 wrapMode: Text.WordWrap
0347                 color: "#404040"
0348                 text: qsTr("You can't put more than %1 pieces of candy in the same rectangle").arg(items.maxNumberOfCandiesPerWidget)
0349             }
0350         }
0351 
0352         DialogChooseLevel {
0353             id: dialogActivityConfig
0354             currentActivity: activity.activityInfo
0355             onSaveData: {
0356                 levelFolder = dialogActivityConfig.chosenLevels
0357                 currentActivity.currentLevels = dialogActivityConfig.chosenLevels
0358                 ApplicationSettings.setCurrentLevels(currentActivity.name, dialogActivityConfig.chosenLevels)
0359             }
0360             onLoadData: {
0361                 if(activityData && activityData["mode"]) {
0362                     background.easyMode = (activityData["mode"] === "true");
0363                 }
0364             }
0365             onStartActivity: {
0366                 background.stop();
0367                 background.start()
0368             }
0369 
0370             onClose: home()
0371         }
0372 
0373         //bar buttons
0374         DialogHelp {
0375             id: dialogHelp
0376             onClose: home()
0377         }
0378 
0379         Bar {
0380             id: bar
0381             level: items.currentLevel + 1
0382             content: BarEnumContent { value: help | home | level | reload | activityConfig}
0383             onHelpClicked: {
0384                 displayDialog(dialogHelp)
0385             }
0386             onPreviousLevelClicked: Activity.previousLevel()
0387             onNextLevelClicked: Activity.nextLevel()
0388             onHomeClicked: activity.home()
0389             onReloadClicked: Activity.reloadRandom()
0390             onActivityConfigClicked: {
0391                  displayDialog(dialogActivityConfig)
0392             }
0393         }
0394 
0395         Bonus {
0396             id: bonus
0397             Component.onCompleted: {
0398                 win.connect(Activity.nextLevel)
0399             }
0400         }
0401 
0402         Score {
0403             id: score
0404             anchors {
0405                 left: undefined
0406                 right: leftWidget.right
0407                 bottom: background.vert ? bar.top : leftWidget.bottom
0408                 margins: 3 * ApplicationInfo.ratio
0409             }
0410             width: girlWidget.width
0411             height: background.vert ? (girlWidget.height * 0.8) : girlWidget.height
0412             numberOfSubLevels: items.nbSubLevel
0413             currentSubLevel: 0
0414             onStop: Activity.nextSubLevel()
0415         }
0416     }
0417 
0418 }