diff --git a/src/activities/penalty/GoalZone.qml b/src/activities/penalty/GoalZone.qml new file mode 100644 --- /dev/null +++ b/src/activities/penalty/GoalZone.qml @@ -0,0 +1,100 @@ +/* GCompris - Zone.qml +* +* Copyright (C) 2017 Rohit Das +* +* 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.0 + +Rectangle { + anchors.left: parent.left + anchors.right: parent.right + anchors.bottom: parent.bottom + anchors.top: parent.top + anchors.topMargin: parent.height * 0.07 + state: "INITIAL" + property var progress: undefined + states: [ + State { + name: "LEFT" + }, + State { + name: "RIGHT" + }, + State { + name: "CENTER" + } + ] + color: "transparent" + + MouseArea { + anchors.fill: parent + acceptedButtons: Qt.LeftButton | Qt.RightButton | Qt.MidButton + enabled: { + isEnabled(this.parent) + } + + onClicked: { + changeBallState(this.parent) + } + } + + function isEnabled (MyRectangle) { + return (items.saveBallState !== "INITIAL") ? + (items.saveBallState === MyRectangle.state) + :(ball.x === items.ballX && ball.y === items.ballY) + } + + function changeBallState(MyRectangle) { + instruction.text = "" + + //var saveBallState = MyRectangle.state + if(ball.state === "FAIL") { + Activity.resetLevel() + return + } + + /* This is a shoot */ + var progress = items.progress + if (items.saveBallState === "INITIAL" && progress === undefined) { + items.saveBallState = MyRectangle.state + progress = MyRectangle.progress + } + items.progress = progress + + if(progress.ratio > 0) { + /* Second click, stop animation */ + progress.anim.running = false; + + /* Play sound */ + activity.audioEffects.play("qrc:/gcompris/src/core/resource/sounds/brick.wav") + + /* Success or not */ + if(progress.ratio < 100) { + /* Success */ + ball.state = items.saveBallState + } else { + /* failure */ + ball.state = "FAIL" + } + timerBonus.start() + } else { + /* First click, start animation*/ + progress.anim.running = true; + + /* Play sound */ + activity.audioEffects.play("qrc:/gcompris/src/core/resource/sounds/flip.wav") + } + } +} diff --git a/src/activities/penalty/Penalty.qml b/src/activities/penalty/Penalty.qml --- a/src/activities/penalty/Penalty.qml +++ b/src/activities/penalty/Penalty.qml @@ -45,113 +45,34 @@ activity.stop.connect(stop) } - function changeBallState(saveBallState) { - instruction.text = "" - - if(ball.state === "FAIL") { - Activity.resetLevel() - return - } - - /* This is a shoot */ - var progress = progressTop - if (saveBallState == "LEFT") { - progress = progressLeft - } - else if(saveBallState == "RIGHT") { - progress = progressRight - } - - if(progress.ratio > 0) { - /* Second click, stop animation */ - progress.anim.running = false; - - /* Play sound */ - activity.audioEffects.play("qrc:/gcompris/src/core/resource/sounds/brick.wav") - - /* Success or not */ - if(progress.ratio < 100) { - /* Success */ - ball.state = saveBallState - } else { - /* failure */ - ball.state = "FAIL" - } - timerBonus.start() - } else { - /* First click, start animation*/ - progress.anim.running = true; - - /* Play sound */ - activity.audioEffects.play("qrc:/gcompris/src/core/resource/sounds/flip.wav") - } - } - // To enable tapping/clicking on left side of goal - Rectangle { + GoalZone { id: rectLeft - anchors.left: parent.left - anchors.top: parent.top + state: "LEFT" + progress: progressLeft anchors.right: player.left - anchors.bottom: parent.bottom anchors.leftMargin: parent.width * 0.08 - anchors.topMargin: parent.height * 0.07 anchors.bottomMargin: parent.height * 0.45 - color: "transparent" - - MouseArea { - anchors.fill: parent - acceptedButtons: Qt.LeftButton | Qt.RightButton | Qt.MidButton - onClicked: { - if(mouse.button) { - changeBallState("LEFT") - } - } - } } // To enable tapping/clicking on top of goal - Rectangle { + GoalZone { id: rectTop - anchors.top: parent.top - anchors.left: rectLeft.right - anchors.right: rectRight.left + state: "CENTER" + progress: progressTop + anchors.left: player.left + anchors.right: player.right anchors.bottom: player.top - anchors.topMargin: parent.height * 0.07 - color: "transparent" - - MouseArea { - anchors.fill: parent - acceptedButtons: Qt.LeftButton | Qt.RightButton | Qt.MidButton - onClicked: { - if(mouse.button) { - changeBallState("CENTER") - } - } - } } // To enable tapping/clicking on right side of goal - Rectangle { + GoalZone { id: rectRight + state: "RIGHT" + progress: progressRight anchors.left: player.right - anchors.right: parent.right - anchors.top: parent.top - anchors.bottom: parent.bottom anchors.rightMargin: parent.width * 0.06 - anchors.topMargin: parent.height * 0.07 anchors.bottomMargin: parent.height * 0.45 - color: "transparent" - - MouseArea { - anchors.fill: parent - acceptedButtons: Qt.LeftButton | Qt.RightButton | Qt.MidButton - onClicked: { - if(mouse.button) { - changeBallState("RIGHT") - } - } - } } // Add here the QML items you need to access in javascript @@ -167,6 +88,10 @@ property alias bonus: bonus property int duration : 0 property int progressBarOpacity : 40 + property string saveBallState: "INITIAL" + property var progress: undefined + property double ballX: ball.parent.width/2 - ball.width/2; + property double ballY: ball.parent.height*0.77 - ball.height/2 } onStart: { Activity.start(items) } @@ -217,116 +142,26 @@ /* The progress bars */ - Rectangle { + Progress { id: progressLeft - property int ratio: 0 - property ParallelAnimation anim: animationLeft - - opacity: items.progressBarOpacity anchors.left: parent.left anchors.leftMargin: parent.width / parent.implicitWidth * 62 - anchors.top: parent.top - anchors.topMargin: parent.height / parent.implicitHeight * 100 - width: ratio / 100 * parent.width / parent.implicitWidth * 200 - height: parent.height / parent.implicitHeight * 20 - ParallelAnimation { - id: animationLeft - onRunningChanged: { - if (!animationLeft.running) { - timerBonus.start() - } - } - PropertyAnimation - { - target: progressLeft - property: "ratio" - from: 0 - to: 100 - duration: items.duration - } - PropertyAnimation - { - target: progressLeft - property: "color" - from: "#00FF00" - to: "#FF0000" - duration: items.duration - } - } } - Rectangle { + Progress { id: progressRight - property int ratio: 0 - property ParallelAnimation anim: animationRight - - opacity: items.progressBarOpacity anchors.right: parent.right anchors.rightMargin: parent.width/parent.implicitWidth * 50 - anchors.top: parent.top - anchors.topMargin: parent.height/parent.implicitHeight * 100 - width: ratio / 100 * parent.width / parent.implicitWidth * 200 - height: parent.height / parent.implicitHeight * 20 - ParallelAnimation { - id: animationRight - onRunningChanged: { - if (!animationRight.running) { - timerBonus.start() - } - } - PropertyAnimation - { - target: progressRight - property: "ratio" - from: 0 - to: 100 - duration: items.duration - } - PropertyAnimation - { - target: progressRight - property: "color" - from: "#00FF00" - to: "#FF0000" - duration: items.duration - } - } } - Rectangle { + Progress { id: progressTop - property int ratio: 0 - property ParallelAnimation anim: animationTop - - opacity: items.progressBarOpacity - anchors.top: parent.top anchors.topMargin: parent.width / parent.implicitWidth * 40 anchors.horizontalCenter: parent.horizontalCenter width: parent.height / parent.implicitHeight * 20 height: ratio / 100 * parent.width / parent.implicitWidth * 100 - ParallelAnimation { - id: animationTop - onRunningChanged: { - if (!animationTop.running) { - timerBonus.start() - } - } - PropertyAnimation { - target: progressTop - property: "ratio" - from: 0 - to: 100 - duration: items.duration - } - PropertyAnimation { - target: progressTop - property: "color" - from: "#00FF00" - to: "#FF0000" - duration: items.duration - } - } } + /* The player */ Image { id: player @@ -432,9 +267,11 @@ acceptedButtons: Qt.LeftButton | Qt.RightButton | Qt.MidButton onClicked: { ball.state = "INITIAL" + items.saveBallState = "INITIAL" progressRight.ratio = 0 progressLeft.ratio = 0 progressTop.ratio = 0 + items.progress = undefined } } } diff --git a/src/activities/penalty/Progress.qml b/src/activities/penalty/Progress.qml new file mode 100644 --- /dev/null +++ b/src/activities/penalty/Progress.qml @@ -0,0 +1,34 @@ +import QtQuick 2.0 + +Rectangle { + id: progress + property int ratio: 0 + property ParallelAnimation anim: animation + opacity: items.progressBarOpacity + anchors.top: parent.top + anchors.topMargin: parent.height / parent.implicitHeight * 100 + width: ratio / 100 * parent.width / parent.implicitWidth * 200 + height: parent.height / parent.implicitHeight * 20 + ParallelAnimation { + id: animation + onRunningChanged: { + if (!animation.running) { + timerBonus.start() + } + } + PropertyAnimation { + target: progress + property: "ratio" + from: 0 + to: 100 + duration: items.duration + } + PropertyAnimation { + target: progress + property: "color" + from: "#00FF00" + to: "#FF0000" + duration: items.duration + } + } +} diff --git a/src/activities/penalty/penalty.js b/src/activities/penalty/penalty.js --- a/src/activities/penalty/penalty.js +++ b/src/activities/penalty/penalty.js @@ -31,6 +31,7 @@ function start(items_) { items = items_ currentLevel = 0 + initLevel() } @@ -54,9 +55,11 @@ function resetLevel() { items.ball.state = "INITIAL" + items.saveBallState = "INITIAL" items.progressRight.ratio = 0 items.progressLeft.ratio = 0 items.progressTop.ratio = 0 + items.progress = undefined } function nextLevel() {