From 6288bac43a5ad215764349e9f093cc990fadd4d8 Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 26 Nov 2018 19:33:59 +0100 Subject: [PATCH] added range of TextFields --- Calculator.qml | 35 +++++++++------ CalculatorEndPage.qml | 97 ++++++++++++++++++++++++++++------------- CalculatorMainPage.qml | 2 +- CalculatorStartPage.qml | 16 ++++++- main.qml | 4 +- 5 files changed, 106 insertions(+), 48 deletions(-) diff --git a/Calculator.qml b/Calculator.qml index 819e631..cb618ff 100644 --- a/Calculator.qml +++ b/Calculator.qml @@ -8,12 +8,13 @@ Item { property int sum: 0 property int nextNum: 0 property int lastNum: 0 - property int actualNums: 0 - property int min: 0 + property int actualNumCount: 0 + property int min: 1 property int max: 9 property int tickInterval: 1000 property int numCount: 10 - property bool negate: false + property bool endPageVisibility: false + property string endPageLabelText: "You Lose!" signal pageOpened() @@ -21,10 +22,6 @@ Item { calculator.state = "starting" } - onNumCountChanged: { - initNums() - } - states: [ State { name: "starting" @@ -79,6 +76,7 @@ Item { Component { id: calculatorEndPageComp CalculatorEndPage { + id: calculatorEndPage } } @@ -117,18 +115,29 @@ Item { randNum = Math.floor((Math.random()*(range+1))+min) } nextNum = randNum - actualNums += 1 + calculator.sum += randNum + actualNumCount += 1 } - function start(min, max){ + function start(min, max) { calculator.nextNumber() calculator.state = "running" } - function initNums() { - while (nums.length < numCount) { - nums.push(0) - console.log(nums.length) + function reset() { + calculator.state = "starting" + calculator.sum = 0 + calculator.actualNumCount = 0 + calculator.endPageLabelText = "You Lose!" + calculator.endPageVisibility = false + calculator.lastNum = 0 + calculator.nextNum = 0 + } + + function checkSum(sumInputText) { + if (calculator.sum === parseInt(sumInputText)) { + calculator.endPageLabelText = "You Won!" } + calculator.endPageVisibility = true } } diff --git a/CalculatorEndPage.qml b/CalculatorEndPage.qml index 1a204fd..28e65ce 100644 --- a/CalculatorEndPage.qml +++ b/CalculatorEndPage.qml @@ -7,43 +7,80 @@ Page { signal pageOpened() - onPageOpened: { - rectAnim.start() - startRectAnim2.start() + TextField { + id: sumInput + placeholderText: "sum" + visible: !won.visible + validator: IntValidator {bottom: -1000000; top: 1000000;} + anchors { + horizontalCenter: parent.horizontalCenter + top: parent.top + topMargin: parent.height * 0.1 + } + Keys.onReturnPressed: calculator.checkSum(sumInput.text) + } + Label { + id: won + text: calculator.endPageLabelText + font.pixelSize: parent.width * 0.2 + visible: calculator.endPageVisibility + anchors { + horizontalCenter: parent.horizontalCenter + top: parent.top + topMargin: parent.height * 0.2 + } } - Rectangle { - id: rect - anchors.fill: parent - property string col: "darkgreen" - color: col + /* + Row { + anchors { + horizontalCenter: parent.horizontalCenter + bottom: parent.bottom + bottomMargin: parent.height * 0.2 + } + height: childrenRect.height + width: childrenRect.width + spacing: root.height * 0.1 - Timer { - id: startRectAnim2 - interval: 5000 - running: false - repeat: false - - onTriggered: { - rectAnim2.start() + RoundButton { + id: homeButton + height: parent.width * 0.2 + width: height + text: "home" + onClicked: { + game.state = "starting" + calculator.reset() } } - ColorAnimation { - id: rectAnim - target: rect - property: "color" - from: "darkGreen" - to: "yellow" - duration: 5000 + RoundButton { + id: startButtons + height: parent.width * 0.2 + width: height + text: "start" + onClicked: { + calculator.reset() + calculator.state = "starting" + } } - ColorAnimation { - id: rectAnim2 - target: rect - property: "color" - from: "yellow" - to: "darkRed" - duration: 5000 + } + */ + + RoundButton { + id: homeButton + height: parent.width * 0.2 + width: height + text: "home" + visible: endPageVisibility + anchors { + horizontalCenter: parent.horizontalCenter + bottom: parent.bottom + bottomMargin: parent.height * 0.1 + } + + onClicked: { + calculator.reset() + calculator.state = "starting" } } } diff --git a/CalculatorMainPage.qml b/CalculatorMainPage.qml index 1adbaf5..a36d50a 100644 --- a/CalculatorMainPage.qml +++ b/CalculatorMainPage.qml @@ -65,7 +65,7 @@ Page { running: calculator.state === "running" onTriggered: { - if(calculator.actualNums < calculator.numCount){ + if (calculator.actualNumCount < calculator.numCount) { nextNumber() tick.start() } diff --git a/CalculatorStartPage.qml b/CalculatorStartPage.qml index 69823d3..62a6a74 100644 --- a/CalculatorStartPage.qml +++ b/CalculatorStartPage.qml @@ -32,40 +32,48 @@ Page { id: tickInterval text: calculator.tickInterval placeholderText: "interval" + validator: IntValidator {bottom: 1; top: 100000;} horizontalAlignment: Qt.AlignHCenter Layout.fillWidth: true Layout.leftMargin: parent.width * 0.05 Layout.rightMargin: parent.width * 0.05 + Keys.onReturnPressed: root.start() } TextField { id: numCount text: calculator.numCount placeholderText: "numbercount" + validator: IntValidator {bottom: 1; top: 100000;} horizontalAlignment: Qt.AlignHCenter Layout.fillWidth: true Layout.leftMargin: parent.width * 0.05 Layout.rightMargin: parent.width * 0.05 + Keys.onReturnPressed: root.start() } TextField { id: min text: calculator.min placeholderText: "min" + validator: IntValidator {bottom: -100000; top: 100000;} horizontalAlignment: Qt.AlignHCenter Layout.fillWidth: true Layout.leftMargin: parent.width * 0.05 Layout.rightMargin: parent.width * 0.05 + Keys.onReturnPressed: root.start() } TextField { id: max text: calculator.max placeholderText: "max" + validator: IntValidator {bottom: -100000; top: 100000;} horizontalAlignment: Qt.AlignHCenter Layout.fillWidth: true Layout.leftMargin: parent.width * 0.05 Layout.rightMargin: parent.width * 0.05 + Keys.onReturnPressed: root.start() } RoundButton { @@ -78,11 +86,15 @@ Page { Layout.leftMargin: parent.width * 0.05 Layout.rightMargin: parent.width * 0.05 onClicked: { - updateVars() - calculator.start() + root.start() } } } + function start() { + root.updateVars() + calculator.start() + } + function updateVars() { calculator.tickInterval = tickInterval.text calculator.numCount = numCount.text diff --git a/main.qml b/main.qml index 4bd2d82..b61d54f 100644 --- a/main.qml +++ b/main.qml @@ -13,11 +13,11 @@ Window { id: game anchors.fill: parent - state: "idle" + state: "starting" states: [ State { - name: "idle" + name: "starting" PropertyChanges { target: mainStack currPage: startPageComp