diff --git a/src/activities/railroad/Railroad.qml b/src/activities/railroad/Railroad.qml index 7fcfe76d9..c393cc000 100644 --- a/src/activities/railroad/Railroad.qml +++ b/src/activities/railroad/Railroad.qml @@ -1,365 +1,472 @@ /* GCompris - railroad.qml * * Copyright (C) 2016 Utkarsh Tiwari * * Authors: * Pascal Georges (GTK+ version) * Utkarsh Tiwari (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 GCompris 1.0 import "../../core" import "railroad.js" as Activity ActivityBase { id: activity onStart: focus = true onStop: {} pageComponent: Image { id: background source: Activity.resourceURL + "railroad-bg.svg" height: activity.height / 2 width: activity.width anchors.fill: parent signal start signal stop Component.onCompleted: { activity.start.connect(start) activity.stop.connect(stop) } // 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 alias score: score property alias timer: timer property alias sampleList: sampleList property alias listModel: listModel property alias answerZone: answerZone property alias animateFlow: animateFlow property alias introMessage: introMessage property bool memoryMode: false property bool mouseEnabled: true } onStart: { Activity.start(items) } onStop: { Activity.stop() } + Keys.enabled: !timer.running && !animateFlow.running + Keys.onPressed: (answerZone.keyNavigation) ? answerZone.handleKeys(event) : sampleList.handleKeys(event) + // Countdown timer Timer { id: timer repeat: false interval: 4000 onTriggered: { items.animateFlow.start() activity.audioEffects.play(Activity.resourceURL + 'sounds/train.wav') } } // Intro message IntroMessage { id: introMessage y: (background.height / 4.7) anchors { right: parent.right rightMargin: 5 left: parent.left leftMargin: 5 } z: 100 onIntroDone: { timer.start() } intro: [ qsTr("Observe and remember the train before the timer ends and then drag the items to set up a similar train.") ] } // Top Display Area Rectangle { id: topDisplayArea width: background.width height: background.height / 4.5 y: background.height / 12.7 color: 'transparent' z: 1 GridView { id: answerZone readonly property int levelCellWidth: (background.width > background.height) ? background.width / (listModel.count > 5 ? 7.2 : 5.66) : background.height / ((listModel.count > 5) ? 8.8 : 6.2) readonly property int levelCellHeight: background.height / 8 width: parent.width height: background.height / 8 cellWidth: levelCellWidth cellHeight: levelCellHeight x: parent.x interactive: false - focus: true model: listModel delegate: Image { id: wagon source: listModel.get(index).source height: answerZone.levelCellHeight width: answerZone.levelCellWidth sourceSize.width: width function checkDrop(dragItem) { // Checks the drop location of this wagon var globalCoordinates = dragItem.mapToItem(answerZone, 0, 0) if(globalCoordinates.y <= ((background.height / 12.5) + (background.height / 8))) { var dropIndex = Activity.getDropIndex(globalCoordinates.x) if(dropIndex > (listModel.count - 1)) { // Handles index overflow dropIndex = listModel.count - 1 } listModel.move(listModel.count - 1, dropIndex, 1) opacity = 1 } if(globalCoordinates.y > (background.height / 8)){ // Remove it if dropped in the lower section activity.audioEffects.play('qrc:/gcompris/src/core/resource/sounds/smudge.wav') listModel.remove(listModel.count - 1) } } function createNewItem() { var component = Qt.createComponent("Loco.qml"); if(component.status === Component.Ready) { var newItem = component.createObject(parent, {"x":x, "y":y, "z": 10 ,"imageIndex": listModel.get(index).id}); } return newItem } MouseArea { id: displayWagonMouseArea hoverEnabled: true enabled: (introMessage.visible ? false : true) && items.mouseEnabled anchors.fill: parent onPressed: { if(items.memoryMode == true) { drag.target = parent.createNewItem(); parent.opacity = 0 listModel.move(index, listModel.count - 1, 1) } } onReleased: { if(items.memoryMode == true) { var dragItem = drag.target parent.checkDrop(dragItem) dragItem.destroy(); parent.Drag.cancel() Activity.isAnswer() } } onClicked: { //skips memorization time. if(!items.memoryMode) { bar.hintClicked() } } } states: State { name: "wagonHover" when: displayWagonMouseArea.containsMouse && (items.memoryMode === true) PropertyChanges { target: wagon scale: 1.1 } } } onXChanged: { if(answerZone.x >= background.width) { timer.stop() animateFlow.stop(); answerZone.x = 2; listModel.clear(); items.memoryMode = true; } } PropertyAnimation { id: animateFlow target: answerZone properties: "x" from: answerZone.x to: background.width duration: 4000 easing.type: Easing.InExpo loops: 1 onStopped: answerZone.x = 2; } + + function handleKeys(event) { + if(event.key === Qt.Key_Down) { + keyNavigation = false + answerZone.currentIndex = -1 + sampleList.currentIndex = 0 + } + if(event.key === Qt.Key_Up) { + keyNavigation = false + answerZone.currentIndex = -1 + sampleList.currentIndex = 0 + } + if(event.key === Qt.Key_Left) { + keyNavigation = true + answerZone.moveCurrentIndexLeft() + } + if(event.key === Qt.Key_Right) { + keyNavigation = true + answerZone.moveCurrentIndexRight() + } + } + + property bool keyNavigation: true + Keys.enabled: true + focus: true + keyNavigationWraps: true + highlightRangeMode: GridView.ApplyRange + highlight: Rectangle { + width: answerZone.cellWidth + height: answerZone.cellHeight + color: "blue" + opacity: 0.8 + radius: 5 + visible: answerZone.keyNavigation && (!timer.running && !animateFlow.running) + x: (answerZone.currentIndex >= 0) ? answerZone.currentItem.x : 0 + y: (answerZone.currentIndex >= 0) ? answerZone.currentItem.y : 0 + Behavior on x { + SpringAnimation { + spring: 3 + damping: 0.2 + } + } + Behavior on y { + SpringAnimation { + spring: 3 + damping: 0.2 + } + } + } + highlightFollowsCurrentItem: false } ListModel { id: listModel } } // Lower Sample Wagon Display Area GridView { id: sampleList visible: items.memoryMode y: (background.height / 4.7) z: 5 width: background.width height: background.height - topDisplayArea.height anchors.margins: 20 cellWidth: width / 5 cellHeight: background.height / 7 model: 20 interactive: false delegate: Image { id: loco readonly property int uniqueID: index property real originX property real originY source: Activity.resourceURL + "loco" + (uniqueID + 1) + ".svg" height: background.height / 7.5 width: ((background.width > background.height) ? background.width/5.66 : background.height/6.2) sourceSize.width: width visible: true function initDrag() { originX = x originY = y } function replace() { x = originX y = originY } function checkDrop() { // Checks the drop location of this wagon var globalCoordinates = loco.mapToItem(answerZone, 0, 0) // checks if the wagon is dropped in correct zone and no. of wagons in answer row are less than // total no. of wagons in correct answer + 2, before dropping the wagon. if(globalCoordinates.y <= (background.height / 12.5) && listModel.count <= Activity.currentLevel + 2) { activity.audioEffects.play('qrc:/gcompris/src/core/resource/sounds/smudge.wav') var dropIndex = Activity.getDropIndex(globalCoordinates.x) Activity.addWagon(uniqueID + 1, dropIndex); } Activity.isAnswer() } MouseArea { id: mouseArea hoverEnabled: true anchors.fill: parent drag.target: parent drag.axis: (parent.y >= 0 && parent.y <= background.height / 7.5) ? Drag.YAxis : Drag.XAndYAxis enabled: items.mouseEnabled onPressed: { parent.initDrag() } onReleased: { parent.Drag.cancel() parent.checkDrop() parent.replace() } } Component.onCompleted: initDrag(); states: State { name: "carHover" when: mouseArea.containsMouse PropertyChanges { target: loco scale: 1.1 } } } + + function handleKeys(event) { + if(event.key === Qt.Key_Up) { + keyNavigation = true + if(sampleList.currentIndex <= 4 && listModel.count >= 1) { + answerZone.keyNavigation = true + answerZone.currentIndex = 0 + sampleList.currentIndex = -1 + } + else { + sampleList.moveCurrentIndexUp() + } + } + if(event.key === Qt.Key_Down) { + keyNavigation = true + sampleList.moveCurrentIndexDown() + } + if(event.key === Qt.Key_Left) { + keyNavigation = true + sampleList.moveCurrentIndexLeft() + } + if(event.key === Qt.Key_Right) { + keyNavigation = true + sampleList.moveCurrentIndexRight() + } + } + + property bool keyNavigation: !answerZone.keyNavigation + Keys.enabled: true + focus: true + keyNavigationWraps: true + highlightRangeMode: GridView.ApplyRange + highlight: Rectangle { + width: sampleList.cellWidth + height: sampleList.cellHeight + color: "#AA41AAC4" + opacity: 0.8 + radius: 5 + visible: sampleList.keyNavigation + x: (sampleList.currentIndex >= 0) ? sampleList.currentItem.x : 0 + y: (sampleList.currentIndex >= 0) ? sampleList.currentItem.y : 0 + Behavior on x { + SpringAnimation { + spring: 3 + damping: 0.2 + } + } + Behavior on y { + SpringAnimation { + spring: 3 + damping: 0.2 + } + } + } + highlightFollowsCurrentItem: false } // Lower level wagons shelves Repeater { id: railSupporter model: 4 Rectangle { x: 0 y: sampleList.y + ((index + 1) * (background.height / 7.5)) + (index * 5) z: 1 width: background.width height: 6 border.color: "#808180" color: "transparent" border.width: 4 } } DialogHelp { id: dialogHelp onClose: home() } Score { id: score fontSize: background.width > background.height ? internalTextComponent.smallSize : internalTextComponent.tinySize anchors.top: parent.top anchors.topMargin: 10 * ApplicationInfo.ratio anchors.right: parent.right anchors.leftMargin: 10 * ApplicationInfo.ratio anchors.bottom: undefined anchors.left: undefined } Bar { id: bar content: BarEnumContent { value: help | home | level | hint } onHelpClicked: { displayDialog(dialogHelp) } onPreviousLevelClicked: Activity.previousLevel() onNextLevelClicked: Activity.nextLevel() onHomeClicked: activity.home() onHintClicked: { if(!introMessage.visible && items.mouseEnabled) { if(items.memoryMode == false) { timer.stop() animateFlow.stop(); listModel.clear(); for(var index = 0; index < Activity.backupListModel.length; index++) { Activity.addWagon(Activity.backupListModel[index], index); } items.memoryMode = true; } else { Activity.restoreLevel(); } } } } Bonus { id: bonus Component.onCompleted: win.connect(Activity.nextSubLevel) } } } diff --git a/src/activities/railroad/railroad.js b/src/activities/railroad/railroad.js index e0742207c..460188a92 100644 --- a/src/activities/railroad/railroad.js +++ b/src/activities/railroad/railroad.js @@ -1,161 +1,163 @@ /* GCompris - railroad.js * * Copyright (C) 2016 Utkarsh Tiwari * * Authors: * (GTK+ version) * "Utkarsh Tiwari" (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 var currentLevel = 0 var numberOfLevel = 5 var solutionArray = [] var backupListModel = [] var memoryTime = [4, 6, 7, 8, 10] var isNewLevel = true var resourceURL = "qrc:/gcompris/src/activities/railroad/resource/" var numberOfSubLevels = 3 var items function start(items_) { items = items_ currentLevel = 0 items.score.numberOfSubLevels = numberOfSubLevels; items.score.currentSubLevel = 1; initLevel() } function stop() { } function initLevel() { var index = 0; items.mouseEnabled = true; items.memoryMode = false; items.timer.stop(); items.animateFlow.stop(); // Stops any previous animations items.listModel.clear(); + items.answerZone.currentIndex = 0; + items.sampleList.currentIndex = 0; if(isNewLevel) { // Initiates a new level backupListModel = []; solutionArray = []; for(var i = 0; i < currentLevel + 1; i++) { if(i == currentLevel) { // Selects the last carriage do { index = Math.floor(Math.random() * 9) + 1; } while (solutionArray.indexOf(index) != -1) // Ensures non-repeative wagons setup } else { // Selects the follow up wagons do { index = Math.floor(Math.random() * 11) + 10; } while (solutionArray.indexOf(index) != -1) } solutionArray.push(index); addWagon(index, i); } } else { // Re-setup the same level for(var i = 0; i < solutionArray.length; i++) { addWagon(solutionArray[i], i); } } if(items.introMessage.visible == false && isNewLevel) { items.timer.start() } items.bar.level = currentLevel + 1; items.timer.interval = memoryTime[currentLevel] * 1000 } function nextLevel() { if(numberOfLevel <= ++currentLevel) { currentLevel = 0 } items.score.currentSubLevel = 1; isNewLevel = true; initLevel(); } function previousLevel() { if(--currentLevel < 0) { currentLevel = numberOfLevel - 1 } items.score.currentSubLevel = 1; isNewLevel = true; initLevel(); } function restoreLevel() { backupListModel = []; for (var index = 0; index < items.listModel.count; index++) { backupListModel.push(items.listModel.get(index).id); } isNewLevel = false; initLevel(); } function nextSubLevel() { /* Sets up the next sublevel */ items.score.currentSubLevel ++; if(items.score.currentSubLevel > numberOfSubLevels) { nextLevel(); } else { isNewLevel = true; initLevel(); } } function isAnswer() { /* Checks if the top level setup equals the solutions */ if(items.listModel.count === solutionArray.length) { var isSolution = true; for (var index = 0; index < items.listModel.count; index++) { if(items.listModel.get(index).id != solutionArray[index]) { isSolution = false; break; } } if(isSolution == true) { items.mouseEnabled = false; // Disables the touch items.bonus.good("flower"); } } } function addWagon(index, dropIndex) { /* Appends wagons to the display area */ items.listModel.insert(dropIndex, {"id": index, "source": (resourceURL + "loco" + (index) + ".svg")}); } function getDropIndex(x) { var count = items.listModel.count; for (var index = 0; index < count; index++) { var xVal = items.answerZone.cellWidth * index var itemWidth = items.answerZone.cellWidth if(x < xVal && index == 0) { return 0; } else if((xVal + itemWidth + items.background.width * 0.0025) <= x && index == (count - 1)) { return count; } else if(xVal <= x && x < (xVal + itemWidth + items.background.width * 0.0025)) { return index + 1; } } return 0; }