diff --git a/src/activities/guesscount/Guesscount.qml b/src/activities/guesscount/Guesscount.qml index 399728272..6142480db 100644 --- a/src/activities/guesscount/Guesscount.qml +++ b/src/activities/guesscount/Guesscount.qml @@ -1,423 +1,423 @@ /* GCompris - guesscount.qml * * Copyright (C) 2016 RAHUL YADAV * * Authors: * Pascal Georges (GTK+ version) * RAHUL YADAV (Qt Quick port) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, see . */ import QtQuick 2.1 import "../../core" import "guesscount.js" as Activity ActivityBase { id: activity onStart: focus = true onStop: {} property bool needRestart: true pageComponent: Image { id: background anchors.fill: parent source: Activity.baseUrl + "/backgroundW01.svg" signal start signal stop Component.onCompleted: { dialogActivityConfig.getInitialConfiguration() activity.start.connect(start) activity.stop.connect(stop) } MouseArea { anchors.fill: parent onClicked: { if(warningDialog.visible) warningDialog.visible = false } } // Add here the QML items you need to access in javascript QtObject { id: items property Item main: activity.main property alias background: background property alias bar: bar property alias bonus: bonus property int sublevel: 0 property alias operandRow: operandRow property var data property int result: data[sublevel-1][1] property alias timer: timer property alias warningDialog: warningDialog property GCAudio audioEffects: activity.audioEffects property bool solved property bool levelchanged: false property var levelArr property string mode property int currentlevel } onStart: if (activity.needRestart) { Activity.start(items); activity.needRestart = false; } else Activity.initLevel(); onStop: { Activity.stop() } JsonParser { id: parser onError: console.error("Guesscount: Error parsing JSON: " + msg); } Loader { id: admin active: false sourceComponent: Column { spacing: 10 width: parent.width height: parent.height Repeater { id:levels model: Activity.numberOfLevel Admin { id:level level: modelData+1 width: parent.width height: parent.height data: items.data } } } } Rectangle { id: top width: parent.width height: parent.height/10 anchors { top: parent.top topMargin: 20 } color: "transparent" Rectangle { id: questionNo width: parent.width*0.2 height: parent.height radius: 20 color: "steelblue" anchors { right: guessLabel.left rightMargin: 20 } GCText { color: "#E8E8E8" anchors.horizontalCenter: parent.horizontalCenter anchors.verticalCenter: parent.verticalCenter fontSize: mediumSize text: qsTr("%1/%2").arg(items.sublevel).arg(items.data.length) } } Rectangle { id: guessLabel width: parent.width*0.4 height: parent.height radius: 20 color: "orange" anchors { horizontalCenter: parent.horizontalCenter } Rectangle { id: insideFill width: parent.width - anchors.margins height: parent.height - anchors.margins anchors.verticalCenter: parent.verticalCenter anchors.horizontalCenter: parent.horizontalCenter anchors.margins: parent.height/4 radius: 10 color: "#E8E8E8" } GCText { id: guess anchors.horizontalCenter: parent.horizontalCenter anchors.verticalCenter: parent.verticalCenter fontSize: smallSize text: qsTr("Guesscount: %1").arg(items.result) } } } Column { id: col spacing: 10 anchors.top: top.bottom anchors.topMargin: 20 anchors.left: parent.left anchors.leftMargin: 5 width: parent.width height: parent.height-top.height-background.height/5 OperatorRow { id: operatorRow width: parent.width height: parent.height/7 mode: items.mode operators: items.levelArr level: items.currentlevel } OperandRow { id: operandRow width: parent.width height: parent.height/7 } Repeater { id: repeat model: operatorRow.repeater.model.length delegate: OperationRow { id: operationRow width: col.width height: col.height/7 property alias operationRow: operationRow noOfRows: operatorRow.repeater.model.length rowNo: modelData guesscount: items.result prevText: modelData ? repeat.itemAt(modelData-1).text : '' prevComplete: modelData ? repeat.itemAt(modelData-1).complete : false reparent: items.solved || items.levelchanged } } } DialogHelp { id: dialogHelp onClose: home() } DialogActivityConfig { id: dialogActivityConfig currentActivity: activity content: Component { Item { property alias modeBox: modeBox property var availableModes: [ { "text": qsTr("Admin"), "value": "admin" }, { "text": qsTr("BuiltIn"), "value": "builtin" } ] Rectangle { id: flow width: dialogActivityConfig.width height: background.height GCComboBox { id: modeBox anchors { top: parent.top topMargin: 5 } model: availableModes background: dialogActivityConfig label: qsTr("Select your mode") } Row { id: labels spacing: 20 anchors { top: modeBox.bottom topMargin: 5 } visible: modeBox.currentIndex == 0 Repeater { model: 2 Row { spacing: 10 Rectangle { id: label width: background.width*0.3 height: background.height/15 radius: 20.0; color: modelData ? "green" : "red" GCText { anchors.horizontalCenter: parent.horizontalCenter anchors.verticalCenter: parent.verticalCenter fontSize: smallSize text: modelData ? qsTr("Selected") : qsTr("Not Selected") } } } } } Rectangle { width: parent.width color: "transparent" height: parent.height/1.25-labels.height-modeBox.height anchors { top: labels.bottom topMargin: 5 } ListView { anchors.fill: parent visible: modeBox.currentIndex == 0 spacing: 5 model: Activity.numberOfLevel clip: true delegate: Admin { id: level level: modelData levelOperators: items.levelArr width: background.width height: background.height/10 } } } } } } onClose: { if(Activity.configDone(items.levelArr)){ Activity.initLevel() home() } } onLoadData: { if(dataToSave && dataToSave["mode"] ) { items.mode = dataToSave["mode"] if(dataToSave["levelArr"] == undefined) dataToSave["levelArr"] = Activity.defaultOperators if(dataToSave["levelArr"].length != Activity.numberOfLevel) items.levelArr = Activity.defaultOperators else items.levelArr = dataToSave["levelArr"] } else{ items.mode='builtin' items.levelArr = Activity.defaultOperators } } onSaveData: { items.mode = dialogActivityConfig.configItem.availableModes[dialogActivityConfig.configItem.modeBox.currentIndex].value dataToSave = {"mode": items.mode, "levelArr":items.levelArr} activity.needRestart = true } function setDefaultValues() { for(var i = 0 ; i < dialogActivityConfig.configItem.availableModes.length ; i ++) { if(dialogActivityConfig.configItem.availableModes[i].value === items.mode) { dialogActivityConfig.configItem.modeBox.currentIndex = i; break; } } } } Bar { id: bar content: BarEnumContent { value: help | home | level | config} onConfigClicked: { dialogActivityConfig.active = true dialogActivityConfig.setDefaultValues(); displayDialog(dialogActivityConfig) } onHelpClicked: { displayDialog(dialogHelp) } onPreviousLevelClicked: { items.levelchanged = true Activity.previousLevel() } onNextLevelClicked: { items.levelchanged = true Activity.nextLevel() } onHomeClicked: activity.home() } Bonus { id: bonus - Component.onCompleted: win.connect(Activity.nextLevel) + } Timer { id: timer interval: 1500 repeat: false onTriggered: { items.solved = true if(items.sublevel * * Authors: * Pascal Georges (GTK+ version) * RAHUL YADAV (Qt Quick port) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, see . */ .pragma library .import QtQuick 2.0 as Quick .import GCompris 1.0 as GCompris .import "dataset.js" as Data .import "qrc:/gcompris/src/core/core.js" as Core var url = "qrc:/gcompris/src/activities/guesscount/resource/" var defaultOperators = Data.defaultOperators var baseUrl = "qrc:/gcompris/src/activities/guesscount/resource"; var builtinFile = baseUrl + "/levels-default.json"; var dataset = [] +var bonusCharacters = ["flower","gnu","lion","note","tux"] var currentLevel var numberOfLevel = Data.levelSchema.length var items function start(items_) { items = items_ currentLevel = 0 initLevel() } function stop() { } function initLevel() { items.bar.level = currentLevel + 1 items.currentlevel = currentLevel items.sublevel = 1 items.data = buildDataset(Data.dataset, Data.levelSchema) items.operandRow.repeater.model = items.data[items.sublevel-1][0] items.levelchanged = false items.solved = false + Core.shuffle(bonusCharacters) if(items.warningDialog.visible) items.warningDialog.visible = false } function nextSublevel() { items.sublevel += 1 items.operandRow.repeater.model = items.data[items.sublevel-1][0] items.solved = false } function nextLevel() { if(numberOfLevel <= ++currentLevel) { currentLevel = 0 } initLevel(); } function previousLevel() { if(--currentLevel < 0) { currentLevel = numberOfLevel - 1 } initLevel(); } function calculate(operand1, operator, operand2, operationRow) { var repeat = operationRow.rowResult var result switch (operator) { case "+": result = operand1+operand2 break; case "-": result = operand1-operand2 break; case "/": result = operand1/operand2 break; case "*": result = operand1*operand2 } // result is positive integer if(Math.round(result)-result === 0 && result >= 0) { operationRow.rowResult = result operationRow.endResult.text = operationRow.rowResult.toString() operationRow.complete = true } else { if(result != repeat) { operationRow.endResult.text = "" operationRow.complete = false if(result < 0) items.warningDialog.dialogText.text = qsTr("result is not positive integer") else items.warningDialog.dialogText.text = qsTr("result is not an integer") items.warningDialog.visible = true } } } function childrenChange(item, operationRow) { if(item.children.length == 2 && item.count == 0) { item.count += 1 } else if(item.children.length == 3) { item.droppedItem.parent = item.droppedItem.reparent operationRow.complete=false if(items.warningDialog.visible) items.warningDialog.visible = false } else if(item.children.length == 1) { item.count -= 1 operationRow.complete = false operationRow.endResult.text = "" } } function checkAnswer(row) { if(items.sublevel < items.data.length) { - items.audioEffects.play("qrc:/gcompris/src/core/resource/sounds/win.wav") + items.bonus.good(bonusCharacters[(items.sublevel-1)%5]) items.timer.start() } else if(items.sublevel == items.data.length) { items.timer.start() items.bonus.good("smiley") + items.bonus.win.connect(nextLevel) } } function sync(array, level) { items.levelArr = array } function check(operator, array) { for (var i in array) { if(array[i] == operator) { return true } } return false } function configDone(array) { for(var i in array) { if(array[i].length == 0) { return false } } return true } function equal(levelOperators, array) { for(var i in levelOperators) { var found = false for(var j in array) { if(levelOperators[i] == array[j]) found = true } if(!found) return false } return true } function buildDataset(data, levelSchema) { var level = [] var levelArr = items.mode == 'builtin' ? defaultOperators : items.levelArr var noOfOperators = levelArr[currentLevel].length var questions for(var j in data[noOfOperators-1]) { if(equal(levelArr[currentLevel], data[noOfOperators-1][j][0])) { questions = data[noOfOperators-1][j][1] break } } var questions = Core.shuffle(questions) for(var m = 0 ; m < levelSchema[currentLevel] ; ++ m) { level.push(questions[m]) } return level }