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

0001 /* GCompris - Scalesboard.qml
0002  *
0003  * SPDX-FileCopyrightText: 2014 Bruno Coudoin <bruno.coudoin@gcompris.net>
0004  *
0005  * Authors:
0006  *   miguel DE IZARRA <miguel2i@free.fr> (GTK+ version)
0007  *   Bruno Coudoin <bruno.coudoin@gcompris.net> (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 "scalesboard.js" as Activity
0016 import "."
0017 
0018 ActivityBase {
0019     id: activity
0020 
0021 
0022     onStart: focus = true
0023     onStop: {}
0024 
0025     pageComponent: Image {
0026         id: background
0027         anchors.fill: parent
0028         source: Activity.url + "background.svg"
0029         sourceSize.width: width
0030         sourceSize.height: height
0031         fillMode: Image.PreserveAspectCrop
0032         signal start
0033         signal stop
0034 
0035         property int scaleHeight: items.masseAreaLeft.weight == items.masseAreaRight.weight ? 0 :
0036                                  items.masseAreaLeft.weight > items.masseAreaRight.weight ? 20 : -20
0037 
0038         Component.onCompleted: {
0039             dialogActivityConfig.initialize()
0040             activity.start.connect(start)
0041             activity.stop.connect(stop)
0042         }
0043 
0044         // Add here the QML items you need to access in javascript
0045         QtObject {
0046             id: items
0047             property Item main: activity.main
0048             property alias background: background
0049             property GCSfx audioEffects: activity.audioEffects
0050             property alias errorRectangle: errorRectangle
0051             property int currentLevel: activity.currentLevel
0052             property alias bonus: bonus
0053             property alias score: score
0054             property int giftWeight
0055             property int scaleHeight: background.scaleHeight
0056             readonly property var levels: activity.datasetLoader.data
0057             property alias masseAreaCenter: masseAreaCenter
0058             property alias masseAreaLeft: masseAreaLeft
0059             property alias masseAreaRight: masseAreaRight
0060             property alias masseCenterModel: masseAreaCenter.masseModel
0061             property alias masseRightModel: masseAreaRight.masseModel
0062             property alias question: question
0063             property alias numpad: numpad
0064             property bool rightDrop
0065             property bool buttonsBlocked: false
0066         }
0067 
0068         onStart: { Activity.start(items) }
0069         onStop: { Activity.stop() }
0070 
0071         property bool isHorizontal: background.width > background.height
0072         property bool scoreAtBottom: bar.width * 6 + okButton.width * 1.5 + score.width < background.width
0073 
0074         Image {
0075             id: scaleBoard
0076             source: Activity.url + "scale.svg"
0077             sourceSize.width: isHorizontal ? Math.min(parent.width - okButton.height * 2,
0078                                                       (parent.height - okButton.height * 2) * 2) : parent.width
0079             anchors.centerIn: parent
0080             anchors.verticalCenterOffset: scoreAtBottom ? 0 : okButton.height * -0.5
0081         }
0082 
0083         Image {
0084             id: needle
0085             parent: scaleBoard
0086             source: Activity.url + "needle.svg"
0087             sourceSize.width: parent.width * 0.75
0088             z: -1
0089             property int angle: - background.scaleHeight * 0.35
0090             anchors {
0091                 horizontalCenter: parent.horizontalCenter
0092                 verticalCenter: parent.verticalCenter
0093                 verticalCenterOffset: - parent.paintedHeight * 0.15
0094             }
0095             transform: Rotation {
0096                 origin.x: needle.width / 2
0097                 origin.y: needle.height * 0.9
0098                 angle: needle.angle
0099             }
0100             Behavior on angle {
0101                 NumberAnimation {
0102                     duration: 500
0103                     easing.type: Easing.InOutQuad
0104                 }
0105             }
0106         }
0107 
0108         // === The Left plate ===
0109         Image {
0110             id: plateLeft
0111             parent: scaleBoard
0112             source: Activity.url + "plate.svg"
0113             sourceSize.width: parent.width * 0.35
0114             z: -10
0115 
0116             anchors {
0117                 horizontalCenter: parent.horizontalCenter
0118                 horizontalCenterOffset: - parent.paintedWidth * 0.3
0119                 verticalCenter: parent.verticalCenter
0120                 verticalCenterOffset: - parent.paintedHeight * 0.03 + background.scaleHeight
0121             }
0122             Behavior on anchors.verticalCenterOffset {
0123                 NumberAnimation {
0124                     duration: 500
0125                     easing.type: Easing.InOutQuad
0126                 }
0127             }
0128 
0129             // The Left Drop Area
0130             MasseArea {
0131                 id: masseAreaLeft
0132                 parent: scaleBoard
0133                 width: plateLeft.width
0134                 anchors {
0135                     horizontalCenter: parent.horizontalCenter
0136                     horizontalCenterOffset: - parent.paintedWidth * 0.3
0137                     verticalCenter: parent.verticalCenter
0138                     verticalCenterOffset: - parent.paintedHeight * 0.44 + background.scaleHeight
0139                 }
0140                 masseAreaCenter: masseAreaCenter
0141                 masseAreaLeft: masseAreaLeft
0142                 masseAreaRight: masseAreaRight
0143                 nbColumns: 3
0144                 audioEffects: activity.audioEffects
0145 
0146                 Behavior on anchors.verticalCenterOffset {
0147                     NumberAnimation {
0148                         duration: 500
0149                         easing.type: Easing.InOutQuad
0150                     }
0151                 }
0152             }
0153         }
0154 
0155         // === The Right plate ===
0156         Image {
0157             id: plateRight
0158             parent: scaleBoard
0159             source: Activity.url + "plate.svg"
0160             sourceSize.width: parent.width * 0.35
0161             z: -10
0162             anchors {
0163                 horizontalCenter: parent.horizontalCenter
0164                 horizontalCenterOffset: parent.paintedWidth * 0.3
0165                 verticalCenter: parent.verticalCenter
0166                 verticalCenterOffset: - parent.paintedHeight * 0.03 - background.scaleHeight
0167             }
0168             Behavior on anchors.verticalCenterOffset {
0169                 NumberAnimation {
0170                     duration: 500
0171                     easing.type: Easing.InOutQuad
0172                 }
0173             }
0174 
0175             // The Right Drop Area
0176             MasseArea {
0177                 id: masseAreaRight
0178                 parent: scaleBoard
0179                 width: plateRight.width
0180                 anchors {
0181                     horizontalCenter: parent.horizontalCenter
0182                     horizontalCenterOffset: parent.paintedWidth * 0.3
0183                     verticalCenter: parent.verticalCenter
0184                     verticalCenterOffset: - parent.paintedHeight * 0.44 - background.scaleHeight
0185                 }
0186                 masseAreaCenter: masseAreaCenter
0187                 masseAreaLeft: masseAreaLeft
0188                 masseAreaRight: masseAreaRight
0189                 nbColumns: 3
0190                 dropEnabledForThisLevel: items.rightDrop
0191                 audioEffects: activity.audioEffects
0192 
0193                 Behavior on anchors.verticalCenterOffset {
0194                     NumberAnimation {
0195                         duration: 500
0196                         easing.type: Easing.InOutQuad
0197                     }
0198                 }
0199             }
0200         }
0201 
0202         // === The Initial Masses List ===
0203         MasseArea {
0204             id: masseAreaCenter
0205             parent: scaleBoard
0206             x: parent.width * 0.05
0207             y: parent.height * 0.84 - height
0208             width: parent.width
0209             masseAreaCenter: masseAreaCenter
0210             masseAreaLeft: masseAreaLeft
0211             masseAreaRight: masseAreaRight
0212             nbColumns: masseModel.count
0213             audioEffects: activity.audioEffects
0214         }
0215 
0216         Message {
0217             id: message
0218             anchors {
0219                 top: parent.top
0220                 topMargin: 10
0221                 right: parent.right
0222                 rightMargin: 10
0223                 left: parent.left
0224                 leftMargin: 10
0225             }
0226         }
0227 
0228         Question {
0229             id: question
0230             parent: scaleBoard
0231             anchors.horizontalCenter: scaleBoard.horizontalCenter
0232             anchors.top: masseAreaCenter.top
0233             anchors.bottom: masseAreaCenter.bottom
0234             z: 1000
0235             width: isHorizontal ? parent.width * 0.5 : background.width - 160 * ApplicationInfo.ratio
0236             answer: items.giftWeight
0237             visible: (items.question.text && background.scaleHeight === 0) ? true : false
0238         }
0239 
0240         ErrorRectangle {
0241             id: errorRectangle
0242             z: 1010
0243             parent: scaleBoard
0244             height: parent.height * 0.5
0245             radius: 10 * ApplicationInfo.ratio
0246             imageSize: okButton.width
0247             function releaseControls() {
0248                 items.buttonsBlocked = false;
0249             }
0250             states: [
0251                 State {
0252                     when: question.visible
0253                     AnchorChanges {
0254                         target: errorRectangle
0255                         anchors.left: question.left
0256                         anchors.right: question.right
0257                         anchors.top: question.top
0258                         anchors.bottom: question.bottom
0259                     }
0260                 },
0261                 State {
0262                     when: !question.visible
0263                     AnchorChanges {
0264                         target: errorRectangle
0265                         anchors.left: plateLeft.right
0266                         anchors.right: plateRight.left
0267                         anchors.top: parent.top
0268                         anchors.bottom: undefined
0269                     }
0270                 }
0271             ]
0272         }
0273 
0274         DialogChooseLevel {
0275             id: dialogActivityConfig
0276             currentActivity: activity.activityInfo
0277 
0278             onSaveData: {
0279                 levelFolder = dialogActivityConfig.chosenLevels
0280                 currentActivity.currentLevels = dialogActivityConfig.chosenLevels
0281                 ApplicationSettings.setCurrentLevels(currentActivity.name, dialogActivityConfig.chosenLevels)
0282                 // restart activity on saving
0283                 background.start()
0284             }
0285             onClose: {
0286                 home()
0287             }
0288             onStartActivity: {
0289                 background.start()
0290             }
0291         }
0292 
0293         DialogHelp {
0294             id: dialogHelp
0295             onClose: home()
0296         }
0297 
0298         BarButton {
0299             id: okButton
0300             source: "qrc:/gcompris/src/core/resource/bar_ok.svg"
0301             sourceSize.width: 60 * ApplicationInfo.ratio
0302             enabled: !items.buttonsBlocked && (items.question.text ?  items.question.userEntry : masseAreaLeft.weight != 0)
0303             ParticleSystemStarLoader {
0304                 id: okButtonParticles
0305                 clip: false
0306             }
0307             onClicked: {
0308                 Activity.checkAnswer();
0309             }
0310         }
0311 
0312         Bar {
0313             id: bar
0314             level: items.currentLevel + 1
0315             content: BarEnumContent { value: help | home | level | activityConfig }
0316             onHelpClicked: {
0317                 displayDialog(dialogHelp)
0318             }
0319             onPreviousLevelClicked: Activity.previousLevel()
0320             onNextLevelClicked: Activity.nextLevel()
0321             onHomeClicked: activity.home()
0322             onActivityConfigClicked: {
0323                 displayDialog(dialogActivityConfig)
0324             }
0325             onLevelChanged: message.text = items.levels[bar.level - 1].message ? items.levels[bar.level - 1].message : ""
0326         }
0327 
0328         Score {
0329             id: score
0330             onStop: { Activity.nextSubLevel(); }
0331         }
0332 
0333         states: [
0334             State {
0335                 name: "horizontalLayout"; when: background.scoreAtBottom
0336                 AnchorChanges {
0337                     target: score
0338                     anchors.top: undefined
0339                     anchors.bottom: undefined
0340                     anchors.right: okButton.left
0341                     anchors.verticalCenter: okButton.verticalCenter
0342                 }
0343                 AnchorChanges {
0344                     target: okButton
0345                     anchors.horizontalCenter: undefined
0346                     anchors.verticalCenter: bar.verticalCenter
0347                     anchors.bottom: undefined
0348                     anchors.right: background.right
0349                     anchors.left: undefined
0350                 }
0351                 PropertyChanges {
0352                     target: okButton
0353                     anchors.bottomMargin: 0
0354                     anchors.rightMargin: okButton.width * 0.5
0355                     anchors.verticalCenterOffset: -10
0356                 }
0357             },
0358             State {
0359                 name: "verticalLayout"; when: !background.scoreAtBottom
0360                 AnchorChanges {
0361                     target: score
0362                     anchors.top: undefined
0363                     anchors.bottom: undefined
0364                     anchors.right: okButton.left
0365                     anchors.verticalCenter: okButton.verticalCenter
0366                 }
0367                 AnchorChanges {
0368                     target: okButton
0369                     anchors.horizontalCenter: undefined
0370                     anchors.verticalCenter: undefined
0371                     anchors.bottom: bar.top
0372                     anchors.right: undefined
0373                     anchors.left: background.horizontalCenter
0374                 }
0375                 PropertyChanges {
0376                     target: okButton
0377                     anchors.bottomMargin: okButton.height * 0.5
0378                     anchors.rightMargin: 0
0379                     anchors.verticalCenterOffset: 0
0380                 }
0381             }
0382         ]
0383 
0384         NumPad {
0385             id: numpad
0386             onAnswerChanged: question.userEntry = answer
0387             maxDigit: ('' + items.giftWeight).length + 1
0388             opacity: question.visible ? 1 : 0
0389             columnWidth: 60 * ApplicationInfo.ratio
0390             enableInput: !items.buttonsBlocked
0391         }
0392 
0393         Keys.enabled: !items.buttonsBlocked
0394         Keys.onPressed: {
0395             if(okButton.enabled && (event.key === Qt.Key_Enter || event.key === Qt.Key_Return)) {
0396                     Activity.checkAnswer()
0397             }
0398             else if(question.visible) {
0399                     numpad.updateAnswer(event.key, true);
0400             }
0401         }
0402 
0403         Keys.onReleased: {
0404             if(question.visible) {
0405                 numpad.updateAnswer(event.key, false);
0406             }
0407         }
0408 
0409         Bonus {
0410             id: bonus
0411             Component.onCompleted: win.connect(Activity.nextLevel)
0412         }
0413     }
0414 
0415 }