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

0001 /* GCompris - target.qml
0002  *
0003  * SPDX-FileCopyrightText: 2014 Bruno coudoin <bruno.coudoin@gcompris.net>
0004  *
0005  * Authors:
0006  *   Bruno Coudoin <bruno.coudoin@gcompris.net> (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 "target.js" as Activity
0016 
0017 ActivityBase {
0018     id: activity
0019 
0020     onStart: focus = true
0021     onStop: {}
0022 
0023     pageComponent: Item {
0024         id: background
0025         anchors.fill: parent
0026 
0027         signal start
0028         signal stop
0029         signal targetReached
0030 
0031         Keys.onPressed: {
0032             if(items.currentArrow != items.nbArrow)
0033                 return
0034 
0035             if(bonus.isPlaying || items.inputLocked)
0036                 return
0037 
0038             if(event.key === Qt.Key_Backspace) {
0039                 backspace()
0040                 event.accepted = true
0041             }
0042             else if(event.key === Qt.Key_Enter || event.key === Qt.Key_Return) {
0043                 Activity.checkAnswer();
0044                 event.accepted = true
0045             }
0046             else if(appendText(event.text)) {
0047                 event.accepted = true
0048             }
0049         }
0050 
0051         Component.onCompleted: {
0052             dialogActivityConfig.initialize()
0053             activity.start.connect(start)
0054             activity.stop.connect(stop)
0055         }
0056 
0057         // Add here the QML items you need to access in javascript
0058         QtObject {
0059             id: items
0060             property Item main: activity.main
0061             property alias background: background
0062             property GCSfx audioEffects: activity.audioEffects
0063             property int currentLevel: activity.currentLevel
0064             property alias bonus: bonus
0065             property alias score: score
0066             property alias errorRectangle: errorRectangle
0067             property alias targetModel: targetItem.model
0068             readonly property var levels: activity.datasetLoader.data
0069             property alias targetItem: targetItem
0070             property alias userEntry: userEntry
0071             property int currentArrow
0072             property int nbArrow
0073             property int currentSubLevel
0074             property int numberOfSubLevel
0075             property bool arrowFlying
0076             property bool inputLocked: true
0077 
0078             onNbArrowChanged: {
0079                 arrowRepeater.init(nbArrow)
0080             }
0081         }
0082 
0083         onStart: {
0084             keyboard.populate();
0085             Activity.start(items)
0086         }
0087         onStop: { Activity.stop() }
0088 
0089         TargetItem {
0090             id: targetItem
0091         }
0092 
0093         onTargetReached: {
0094             items.arrowFlying = false
0095             if(items.currentArrow == items.nbArrow) {
0096                 targetItem.stop()
0097                 targetItem.scoreText += " = "
0098                 userEntry.text = "?"
0099                 items.inputLocked = false
0100             }
0101         }
0102 
0103         Arrow {
0104             id: arrowRepeater
0105         }
0106 
0107         Image {
0108             id: cross
0109             anchors.centerIn: parent
0110             source: Activity.url + "cross.svg"
0111             opacity: items.currentArrow != items.nbArrow ? 1 : 0
0112             sourceSize.width: 50 * ApplicationInfo.ratio
0113         }
0114 
0115         MouseArea {
0116             id: mouseArea
0117             anchors.fill: parent
0118             enabled: items.currentArrow != items.nbArrow && !items.arrowFlying && !items.inputLocked
0119             onClicked: {
0120                 activity.audioEffects.play(Activity.url + 'arrow.wav')
0121                 items.arrowFlying = true
0122                 if(items.currentArrow != items.nbArrow) {
0123                     arrowRepeater.itemAt(items.currentArrow).opacity = 1
0124                     arrowRepeater.itemAt(items.currentArrow++).scale = 1
0125                 }
0126             }
0127         }
0128 
0129         GCText {
0130             id: scoreItem
0131             anchors.horizontalCenter: parent.horizontalCenter
0132             width: parent.width
0133             text: targetItem.scoreText
0134             fontSize: 22
0135             font.bold: true
0136             style: Text.Outline
0137             styleColor: "black"
0138             color: "white"
0139             wrapMode: Text.WordWrap
0140             horizontalAlignment: Text.AlignHCenter
0141         }
0142 
0143         function backspace() {
0144             userEntry.text = userEntry.text.slice(0, -1)
0145             if(userEntry.text.length === 0) {
0146                 userEntry.text = "?"
0147             }
0148         }
0149 
0150         function appendText(text) {
0151             if(items.inputLocked)
0152                 return false
0153 
0154             if(text === keyboard.backspace) {
0155                 backspace()
0156                 return true
0157             }
0158 
0159             var number = parseInt(text)
0160             if(isNaN(number))
0161                 return false
0162 
0163             if(userEntry.text === "?") {
0164                 userEntry.text = ""
0165             }
0166 
0167             if(userEntry.text.length > ('' + targetItem.scoreTotal).length) {
0168                 return false
0169             }
0170 
0171             userEntry.text += text
0172             return true
0173         }
0174 
0175         GCText {
0176             id: userEntry
0177             anchors.top: scoreItem.bottom
0178             width: parent.width
0179             fontSize: 22
0180             font.bold: true
0181             style: Text.Outline
0182             styleColor: "black"
0183             color: "white"
0184             wrapMode: Text.WordWrap
0185             horizontalAlignment: Text.AlignHCenter
0186         }
0187 
0188         BarButton {
0189             id: okButton
0190             source: "qrc:/gcompris/src/core/resource/bar_ok.svg"
0191             sourceSize.width: 60 * ApplicationInfo.ratio
0192             anchors.bottom: bar.top
0193             anchors.bottomMargin: 20 * ApplicationInfo.ratio
0194             anchors.right: parent.right
0195             anchors.rightMargin: 10 * ApplicationInfo.ratio
0196             mouseArea.enabled: !items.inputLocked
0197             visible: items.currentArrow == items.nbArrow
0198             onClicked: {
0199                 Activity.checkAnswer();
0200             }
0201         }
0202 
0203         ErrorRectangle {
0204             id: errorRectangle
0205             anchors.fill: userEntry
0206             imageSize: height * 0.75
0207             function releaseControls() { items.inputLocked = false; }
0208         }
0209 
0210         VirtualKeyboard {
0211             id: keyboard
0212             anchors.bottom: parent.bottom
0213             anchors.horizontalCenter: parent.horizontalCenter
0214             hide: items.currentArrow == items.nbArrow ? false : true
0215             enabled: visible && !items.inputLocked
0216 
0217             function populate() {
0218                 layout = [
0219                 [
0220                     { label: "0" },
0221                     { label: "1" },
0222                     { label: "2" },
0223                     { label: "3" },
0224                     { label: "4" },
0225                     { label: "5" },
0226                     { label: "6" },
0227                     { label: "7" },
0228                     { label: "8" },
0229                     { label: "9" },
0230                     { label: keyboard.backspace }
0231                 ] ]
0232             }
0233             onKeypress: background.appendText(text)
0234 
0235             onError: console.log("VirtualKeyboard error: " + msg);
0236         }
0237 
0238         DialogChooseLevel {
0239             id: dialogActivityConfig
0240             currentActivity: activity.activityInfo
0241 
0242             onSaveData: {
0243                 levelFolder = dialogActivityConfig.chosenLevels
0244                 currentActivity.currentLevels = dialogActivityConfig.chosenLevels
0245                 ApplicationSettings.setCurrentLevels(currentActivity.name, dialogActivityConfig.chosenLevels)
0246             }
0247             onClose: {
0248                 home()
0249             }
0250             onStartActivity: {
0251                 background.stop()
0252                 background.start()
0253             }
0254         }
0255 
0256         DialogHelp {
0257             id: dialogHelp
0258             onClose: home()
0259         }
0260 
0261         Bar {
0262             id: bar
0263             level: items.currentLevel + 1
0264             anchors.bottom: keyboard.top
0265             content: BarEnumContent { value: help | home | level | activityConfig }
0266             onHelpClicked: {
0267                 displayDialog(dialogHelp)
0268             }
0269             onActivityConfigClicked: {
0270                 displayDialog(dialogActivityConfig)
0271             }
0272             onPreviousLevelClicked: Activity.previousLevel()
0273             onNextLevelClicked: Activity.nextLevel()
0274             onHomeClicked: activity.home()
0275         }
0276 
0277         Score {
0278             id: score
0279             anchors.right: parent.right
0280             anchors.top: parent.top
0281             anchors.bottom: undefined
0282             currentSubLevel: 0
0283             numberOfSubLevels: items.numberOfSubLevel
0284             onStop: Activity.nextSubLevel()
0285         }
0286 
0287         Bonus {
0288             id: bonus
0289             Component.onCompleted: win.connect(Activity.nextLevel)
0290         }
0291     }
0292 }