mathtrainingstuff/Calculator.qml

153 lines
3.4 KiB
QML
Raw Normal View History

2018-11-23 17:14:09 +01:00
import QtQuick 2.11
import QtQuick.Controls 2.4
import "./"
2018-11-23 17:14:09 +01:00
Item {
id: calculator
property int sum: 0
property int nextNum: 0
property int lastNum: 0
2018-11-26 19:33:59 +01:00
property int actualNumCount: 0
property int min: 1
property int max: 9
2018-11-23 17:14:09 +01:00
property int tickInterval: 1000
property int numCount: 10
2018-11-26 19:33:59 +01:00
property bool endPageVisibility: false
property string endPageLabelText: "You Lose!"
2018-11-23 17:14:09 +01:00
signal pageOpened()
onPageOpened: {
calculator.start()
2018-11-23 17:14:09 +01:00
}
states: [
State {
name: "starting"
PropertyChanges {
target: calculatorStack
currPage: calculatorStartPageComp
}
},
State {
name: "running"
PropertyChanges {
target: calculatorStack
currPage: calculatorRunningPageComp
}
},
State {
name: "gameOver"
PropertyChanges {
target: calculatorStack
2018-11-30 20:58:24 +01:00
currPage: calculatorGameOverPageComp
2018-11-23 17:14:09 +01:00
}
}
]
StackView {
id: calculatorStack
property var currPage;
anchors.fill: parent
onCurrPageChanged: {
calculatorStack.replace(currPage)
}
onCurrentItemChanged: {
calculatorStack.currentItem.pageOpened()
}
Component {
id: calculatorStartPageComp
CalculatorStartPage {
2018-11-23 17:14:09 +01:00
}
}
Component {
id: calculatorRunningPageComp
CalculatorRunningPage {
}
}
2018-11-23 17:14:09 +01:00
Component {
2018-11-30 20:58:24 +01:00
id: calculatorGameOverPageComp
CalculatorGameOverPage {
2018-11-23 17:14:09 +01:00
}
}
replaceExit: Transition {
NumberAnimation {
from: 1
to: 0
property: "opacity"
duration: 200
easing.type: Easing.InOutQuad
}
}
replaceEnter: Transition {
NumberAnimation {
from: 0
to: 1
property: "opacity"
duration: 200
easing.type: Easing.InOutQuad
}
}
}
/*-------------------------
---------functions---------
-------------------------*/
function start() {
calculator.reset()
calculator.state = "starting"
2018-11-23 17:14:09 +01:00
}
function run() {
2018-11-23 17:14:09 +01:00
calculator.nextNumber()
calculator.state = "running"
}
function gameOver() {
calculator.state = "gameOver"
}
2018-11-26 19:33:59 +01:00
function reset() {
calculator.sum = 0
calculator.actualNumCount = 0
calculator.endPageLabelText = "You Lose!"
calculator.endPageVisibility = false
calculator.lastNum = 0
calculator.nextNum = 0
}
function nextNumber(){
var randNum = 0
var range = calculator.max - calculator.min
lastNum = nextNum
while(randNum===0 || randNum === lastNum){
randNum = Math.floor((Math.random()*(range+1))+min)
}
nextNum = randNum
calculator.sum += randNum
actualNumCount += 1
}
2018-11-26 19:33:59 +01:00
function checkSum(sumInputText) {
if (sumInputText !== "") {
if (calculator.sum === parseInt(sumInputText)) {
calculator.endPageLabelText = "You Won!"
}
calculator.endPageVisibility = true
}
}
2018-11-23 17:14:09 +01:00
}