Warning, /education/gcompris/src/activities/gnumch-equality/Gnumch.qml is written in an unsupported language. File is not indexed.

0001 /* GCompris - Gnumch.qml
0002 *
0003 * SPDX-FileCopyrightText: 2014 Manuel Tondeur <manueltondeur@gmail.com>
0004 *
0005 * Authors:
0006 *   Joe Neeman (spuzzzzzzz@gmail.com) (GTK+ version)
0007 *   Manuel Tondeur <manueltondeur@gmail.com> (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 "gnumch-equality.js" as Activity
0016 
0017 ActivityBase {
0018     id: activity
0019 
0020     property string type
0021     property bool useMultipleDataset: false
0022 
0023     focus: true
0024 
0025     onStart: {}
0026     onStop: {}
0027 
0028     pageComponent: Image {
0029         id: background
0030         anchors.fill: parent
0031         source: "qrc:/gcompris/src/activities/guesscount/resource/backgroundW01.svg"
0032         property bool withMonsters: false
0033 
0034         function checkAnswer() {
0035             if (!muncher.movable)
0036                 return
0037 
0038             // Case already discovered
0039             if (!modelCells.get(muncher.index).show) {
0040                 return
0041             }
0042 
0043             muncher.eating = true
0044             // Set the cell invisible if it's the correct answer.
0045             if (Activity.isAnswerCorrect(muncher.index)) {
0046                 modelCells.get(muncher.index).show = false
0047                 if (gridPart.isLevelDone()) {
0048                     stopLevel();
0049                 }
0050             } else {
0051                 modelCells.get(muncher.index).show = false
0052                 muncher.getCaught(muncher.index)
0053             }
0054         }
0055 
0056         function nextLevel() {
0057             Activity.nextLevel();
0058             initLevel();
0059         }
0060 
0061         function previousLevel() {
0062             Activity.previousLevel();
0063             initLevel();
0064         }
0065 
0066         function initLevel() {
0067             topPanel.life.opacity = 1;
0068             forceActiveFocus();
0069             Activity.initLevel();
0070             items.operator = Activity.operator;
0071             topPanel.goal = Activity.getGoal();
0072             stopLevel();
0073 
0074             if(useMultipleDataset) {
0075                 if(items.levels[items.currentLevel].spawnMonsters) {
0076                     withMonsters = true;
0077                     spawningMonsters.restart();
0078                 }
0079             }
0080             else if (items.currentLevel !== 0) {
0081                 withMonsters = true;
0082                 spawningMonsters.restart();
0083             }
0084             else {
0085                 withMonsters = false;
0086             }
0087         }
0088 
0089         function stopLevel() {
0090             monsters.destroyAll();
0091             spawningMonsters.stop();
0092             timerActivateWarn.stop();
0093         }
0094 
0095         signal start
0096         signal stop
0097 
0098         Component.onCompleted: {
0099             dialogActivityConfig.initialize()
0100             activity.start.connect(start)
0101             activity.stop.connect(stop)
0102         }
0103 
0104         QtObject {
0105             id: items
0106             property var levels: activity.datasetLoader ? activity.datasetLoader.data : ""
0107             property alias modelCells: modelCells
0108             property alias bonus: bonus
0109             property int currentLevel: activity.currentLevel
0110             property string operator
0111         }
0112 
0113         onStart: {
0114             Activity.start(items, type, useMultipleDataset);
0115             initLevel();
0116         }
0117         onStop: {
0118             spawningMonsters.stop()
0119             positionTimer.stop()
0120             timerActivateWarn.stop()
0121             monsters.destroyAll()
0122             Activity.stop()
0123         }
0124 
0125         Keys.enabled: !bonus.isPlaying
0126         Keys.onRightPressed: muncher.moveTo(muncher.moveRight)
0127         Keys.onLeftPressed: muncher.moveTo(muncher.moveLeft)
0128         Keys.onDownPressed: muncher.moveTo(muncher.moveDown)
0129         Keys.onUpPressed: muncher.moveTo(muncher.moveUp)
0130 
0131         Keys.onSpacePressed: {
0132             checkAnswer()
0133         }
0134 
0135         Keys.onReturnPressed: {
0136             warningRect.hideWarning()
0137         }
0138 
0139         // Debug utility.
0140 //        Keys.onAsteriskPressed: {
0141 //            nextLevel()
0142 //        }
0143 
0144         onWidthChanged: {
0145             positionTimer.restart()
0146         }
0147 
0148         Timer {
0149             id: positionTimer
0150 
0151             interval: 100
0152 
0153             onTriggered: {
0154                 muncher.updatePosition()
0155                 var children = monsters.children
0156                 for (var it = 0; it < children.length; it++) {
0157                     children[it].updatePosition()
0158                 }
0159             }
0160         }
0161 
0162         Connections {
0163             target: warningRect.mArea
0164             onClicked: warningRect.hideWarning()
0165         }
0166 
0167         Item {
0168             id: gridPart
0169 
0170             width: background.width
0171             height: background.height / 7 * 6
0172             anchors.right: parent.right
0173             anchors.top: parent.top
0174             anchors.bottom: topPanel.top
0175 
0176             function isLevelDone() {
0177                 for (var it = 0; it < modelCells.count; it++) {
0178                     if (Activity.isAnswerCorrect(it) && modelCells.get(it).show) {
0179                         return false
0180                     }
0181                 }
0182                 bonus.good("gnu")
0183                 return true
0184             }
0185 
0186             MultiPointTouchArea {
0187                 anchors.fill: parent
0188                 touchPoints: [ TouchPoint { id: point1 } ]
0189                 property real startX
0190                 property real startY
0191                 // Workaround to avoid having 2 times the onReleased event
0192                 property bool started
0193 
0194                 onPressed: {
0195                     startX = point1.x
0196                     startY = point1.y
0197                     started = true
0198                 }
0199 
0200                 onReleased: {
0201                     if(!started)
0202                         return false
0203                     var moveX = point1.x - startX
0204                     var moveY = point1.y - startY
0205                     // Find the direction with the most move
0206                     if(Math.abs(moveX) * ApplicationInfo.ratio > 10 &&
0207                             Math.abs(moveX) > Math.abs(moveY)) {
0208                         if(moveX > 10 * ApplicationInfo.ratio)
0209                             muncher.moveTo(muncher.moveRight)
0210                         else if(moveX < -10 * ApplicationInfo.ratio)
0211                             muncher.moveTo(muncher.moveLeft)
0212                         else
0213                             background.checkAnswer()
0214                     } else if(Math.abs(moveY) * ApplicationInfo.ratio > 10 &&
0215                               Math.abs(moveX) < Math.abs(moveY)) {
0216                         if(moveY > 10 * ApplicationInfo.ratio)
0217                             muncher.moveTo(muncher.moveDown)
0218                         else if(moveY < -10 * ApplicationInfo.ratio)
0219                             muncher.moveTo(muncher.moveUp)
0220                         else
0221                             background.checkAnswer()
0222                     } else {
0223                         // No move, just a tap or mouse click
0224                         if(point1.x > muncher.x + muncher.width)
0225                             muncher.moveTo(muncher.moveRight)
0226                         else if(point1.x < muncher.x)
0227                             muncher.moveTo(muncher.moveLeft)
0228                         else if(point1.y < muncher.y)
0229                             muncher.moveTo(muncher.moveUp)
0230                         else if(point1.y > muncher.y + muncher.height)
0231                             muncher.moveTo(muncher.moveDown)
0232                         else
0233                             background.checkAnswer()
0234                     }
0235                     started = false
0236                 }
0237             }
0238 
0239             GridView {
0240                 id: gridBackground
0241 
0242                 anchors.fill: parent
0243                 cellHeight: (parent.height - 2) / 6
0244                 cellWidth: (parent.width - 2) / 6
0245                 interactive: false
0246                 focus: false
0247 
0248                 model: modelCells
0249                 delegate: Rectangle {
0250                     width: gridBackground.cellWidth
0251                     height: gridBackground.cellHeight
0252                     border.color: "#373737"
0253                     border.width: 2
0254                     radius: 5
0255                     color: "#80ffffff"
0256                 }
0257             }
0258 
0259             Muncher {
0260                 id: muncher
0261                 audioEffects: activity.audioEffects
0262             }
0263 
0264             Item {
0265                 id: monsters
0266 
0267                 function setMovable(movable) {
0268                     var children = monsters.children
0269                     for (var it = 0; it < children.length; it++) {
0270                         children[it].movable = movable
0271                     }
0272                 }
0273 
0274                 function destroyAll() {
0275                     var children = monsters.children
0276                     for (var it = 0; it < children.length; it++) {
0277                         children[it].destroy()
0278                     }
0279                 }
0280 
0281                 function isThereAMonster(position) {
0282                     var children = monsters.children
0283                     for (var it = 0; it < children.length; it++) {
0284                         if (children[it].index == position) {
0285                             children[it].eating = true
0286                             return true
0287                         }
0288                     }
0289                     return false
0290                 }
0291 
0292                 function checkOtherMonster(position) {
0293                     var children = monsters.children
0294                     var count = 0
0295                     for (var it = 0; it < children.length; it++) {
0296                         if (children[it].index == position
0297                                 && !children[it].movingOn) {
0298                             count++
0299                             if (count > 1) {
0300                                 children[it].opacity = 0
0301                             }
0302                         }
0303                     }
0304                 }
0305             }
0306 
0307             Timer {
0308                 id: spawningMonsters
0309 
0310                 interval: Activity.genTime()
0311                 running: false
0312                 repeat: true
0313 
0314                 onTriggered: {
0315                     interval = Activity.genTime()
0316                     timerActivateWarn.start()
0317                     var comp = Qt.createComponent("qrc:/gcompris/src/activities/gnumch-equality/" +
0318                                                   Activity.genMonster(
0319                                                       ) + ".qml")
0320                     if (comp.status === Component.Ready) {
0321                         var direction = Math.floor(Math.random() * 4)
0322                         var result = Activity.genPosition(direction,
0323                                                           grid.cellWidth,
0324                                                           grid.cellHeight)
0325                         var reggie = comp.createObject(monsters, {
0326                                                            audioEffects: activity.audioEffects,
0327                                                            direction: direction,
0328                                                            player: muncher,
0329                                                            index: result[0],
0330                                                            x: result[1],
0331                                                            y: result[2]
0332                                                        })
0333                         reggie.opacity = 1
0334                     }
0335                 }
0336             }
0337 
0338             Timer {
0339                 id: timerActivateWarn
0340 
0341                 interval: spawningMonsters.interval - 2000
0342                 running: spawningMonsters.running
0343 
0344                 onTriggered: {
0345                     warnMonsters.opacity = 1
0346                 }
0347             }
0348 
0349             GridView {
0350                 id: grid
0351 
0352                 anchors.fill: parent
0353                 cellHeight: (parent.height - 2) / 6
0354                 cellWidth: (parent.width - 2) / 6
0355                 interactive: false
0356                 focus: false
0357 
0358                 model: modelCells
0359                 delegate: gridDelegate.delegate
0360             }
0361 
0362             CellDelegate {
0363                 id: gridDelegate
0364             }
0365 
0366             ListModel {
0367                 id: modelCells
0368 
0369                 function regenCell(position) {
0370                     if (type === "equality" || type === "inequality") {
0371                         var terms
0372                         if (items.operator === " + ") {
0373                             terms = Activity.splitPlusNumber(
0374                                         Activity.genNumber())
0375                         } else if (items.operator === " - ") {
0376                             terms = Activity.splitMinusNumber(
0377                                         Activity.genNumber())
0378                         } else if (items.operator === " * ") {
0379                             terms = Activity.splitMultiplicationNumber(
0380                                         Activity.genNumber())
0381                         } else if (items.operator === " / ") {
0382                             terms = Activity.splitDivisionNumber(
0383                                         Activity.genNumber())
0384                         }
0385                         modelCells.setProperty(position, "number1", terms[0])
0386                         modelCells.setProperty(position, "number2", terms[1])
0387                     } else if (type == "multiples") {
0388                         modelCells.setProperty(position, "number1", Activity.genMultiple())
0389                     } else if (type == "factors") {
0390                         modelCells.setProperty(position, "number1", Activity.genFactor())
0391                     }
0392 
0393                     modelCells.setProperty(position, "show", true)
0394                     gridPart.isLevelDone()
0395                 }
0396             }
0397         }
0398 
0399         DialogChooseLevel {
0400             id: dialogActivityConfig
0401             currentActivity: activity.activityInfo
0402             onClose: {
0403                 home()
0404             }
0405 
0406             onSaveData: {
0407                 levelFolder = dialogActivityConfig.chosenLevels
0408                 currentActivity.currentLevels = dialogActivityConfig.chosenLevels
0409                 ApplicationSettings.setCurrentLevels(currentActivity.name, dialogActivityConfig.chosenLevels)
0410             }
0411 
0412             onStartActivity: {
0413                 background.stop()
0414                 background.start()
0415             }
0416         }
0417 
0418         TopPanel {
0419             id: topPanel
0420             goal: Activity.getGoal()
0421         }
0422 
0423         WarnMonster {
0424             id: warnMonsters
0425         }
0426 
0427         DialogHelp {
0428             id: dialogHelp
0429             onClose: home()
0430         }
0431 
0432         Warning {
0433             id: warningRect
0434         }
0435 
0436         Bonus {
0437             id: bonus
0438 
0439             onStop: {
0440                 if(isWin === true)
0441                     parent.nextLevel();
0442             }
0443         }
0444     }
0445 }