diff --git a/src/activities/railroad/Railroad.qml b/src/activities/railroad/Railroad.qml index ba80db294..25a07300e 100644 --- a/src/activities/railroad/Railroad.qml +++ b/src/activities/railroad/Railroad.qml @@ -1,318 +1,336 @@ /* 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: {} property variant barAtStart 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 sampleList: sampleList property alias listModel: listModel property alias displayList: displayList property alias animateFlow: animateFlow property alias displayFlow: displayFlow property alias railCollection: railCollection property alias introMessage: introMessage } onStart: { Activity.start(items) } onStop: { Activity.stop() } // Top Display Area Rectangle { + width: background.width + height: background.height + color: 'transparent' + x: 2 + y : background.height / 12.5 + + MouseArea { + id: topMouseArea + enabled: true + hoverEnabled: true + anchors.fill: parent + } Flickable { id: flickTop - width: background.width - height: background.height + width: parent.width + height: parent.height contentWidth: displayFlow.width contentHeight: width flickableDirection: Flickable.HorizontalFlick - x: 2 - y : background.height / 12.5 + anchors.fill: parent + + Row { id: displayFlow x: flickTop.x y: 0 width: childrenRect.width spacing: background.width * 0.0025 - rotation: 0 Repeater { id: displayList model: listModel - Image { + + delegate : Image { id: wagon source: Activity.resourceURL + "loco1.svg" height: background.height / 8.0 width: ((background.width > background.height) ? background.width : background.height) / 4.66 visible: true - rotation: displayFlow.rotation MouseArea { id: displayWagonMouseArea hoverEnabled: true enabled: introMessage.visible ? false : true anchors.fill: parent onClicked: { if (Activity.memoryMode == true) { listModel.remove(index); Activity.isAnswer(); } else { animateFlow.stop(); displayFlow.x = 2; listModel.clear(); Activity.memoryMode = true; - Activity.items.displayFlow.rotation = 180; Activity.items.railCollection.visible = true } } } states: State { name: "waganHover" when: displayWagonMouseArea.containsMouse && (Activity.memoryMode === true) PropertyChanges { target: wagon scale: 1.1 } } } } onXChanged: { if (displayFlow.x >= background.width) { animateFlow.stop(); displayFlow.x = 2; listModel.clear(); Activity.memoryMode = true; - displayFlow.rotation = 180; items.railCollection.visible = true; } } PropertyAnimation { id: animateFlow target: displayFlow properties: "x" from: 2 to: background.width duration: 14000 easing.type: Easing.InExpo loops: 1 } } + ListModel { id: listModel } } } // Lower Sample Wagon Display Area Rectangle { id: railCollection color: "transparent" visible: false Repeater { id: sampleList model: 4 Flickable { x: 2 y: (background.height / 4.7) + (index * (background.height / 6.5)) height: background.height / 7.5 width: background.width contentWidth: railCarriages.childrenRect.width contentHeight: height flickableDirection: Flickable.HorizontalFlick Row { id: railCarriages property real rowNo: index anchors.margins: 1 anchors.bottomMargin: 10 spacing: background.width * 0.0025 y: 0 height: background.height / 7.5 width: childrenRect.width Repeater { id: eachRow model: Activity.noOfCarriages[parent.rowNo] + Image { id: loco readonly property int uniqueID: Activity.sum(parent.rowNo) + index - property real originXAxis - property real originYAxis + property real originX + property real originY + property real minY: (background.height / 4.7) + (parent.rowNo * (background.height / 6.5)) + property real maxY: minY + background.height / 7.5 source: Activity.resourceURL + "loco" + (uniqueID + 1) + ".svg" height: background.height / 7.5 width: ((background.width > background.height) ? background.width : background.height) / 4.66 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(background, 0, 0) + if(globalCoordinates.y <= ((background.height / 8.0) + (background.height / 12.5))) { + if(listModel.count == 0) { + Activity.addWagon(uniqueID + 1, 0); + } else { + var dropIndex = Activity.getDropIndex(globalCoordinates.x) + Activity.addWagon(uniqueID + 1, dropIndex); + } + } + Activity.isAnswer() + } + MouseArea { id: mouseArea hoverEnabled: true - enabled: true anchors.fill: parent drag.target: parent - // onClicked: { - // Activity.addWagon(parent.uniqueID + 1); - // Activity.isAnswer(); - // } + drag.axis: (parent.y >= 0 && parent.y <= background.height / 7.5) ? Drag.YAxis : Drag.XAndYAxis + enabled: true onPressed: { - // tileImage.anchors.centerIn = undefined - parent.originXAxis = parent.x - parent.originYAxis = parent.y + parent.initDrag() } - - // onUpdated: { - // var moveX = point1.x - startX - // var moveY = point1.y - startY - // parent.x = parent.x + moveX - // parent.y = parent.y + moveY - // } - onReleased: { - // if (pressedOnce) { - // pressedOnce = false - // item.selected = false - // var coord = backgroundContainer.mapFromItem(tileImage.parent, parent.x, parent.y) - // if(coord.x > 0 && (backgroundContainer.width - coord.x > tileImage.fullWidth)) - // Activity.createComponent(coord.x, coord.y, index) - // tileImage.anchors.centerIn = tile - // tileImage.toSmall() - // toolTip.show("") - // } - parent.x = parent.originXAxis - parent.y = parent.originYAxis + parent.Drag.cancel() + parent.checkDrop() + parent.replace() } + } + Component.onCompleted: initDrag(); - } states: State { name: "carHover" when: mouseArea.containsMouse PropertyChanges { target: loco scale: 1.1 } } } } } } } } // Lower level wagons shelves Repeater { id: railSupporter model: 4 Rectangle { x: 0 y: (background.height / 2.9) + (index * (background.height / 6.5)) width: background.width height: 5 border.color: "#808180" color: "transparent" border.width: 5 } } IntroMessage { id: introMessage y: (background.height / 4.7) anchors { right: parent.right rightMargin: 5 left: parent.left leftMargin: 5 } z: 100 onIntroDone: { items.animateFlow.start(); } intro: [ qsTr("Swap left-right to view all the carriages outside display area!") ] } DialogHelp { id: dialogHelp onClose: home() } Score { id: score 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 | reload} onHelpClicked: { displayDialog(dialogHelp) } onPreviousLevelClicked: Activity.previousLevel() onNextLevelClicked: Activity.nextLevel() onHomeClicked: activity.home() onReloadClicked: { Activity.reset() } } Bonus { id: bonus Component.onCompleted: win.connect(Activity.advanceSubLevel) } } } diff --git a/src/activities/railroad/railroad.js b/src/activities/railroad/railroad.js index a082f74d5..5cd8c1534 100644 --- a/src/activities/railroad/railroad.js +++ b/src/activities/railroad/railroad.js @@ -1,151 +1,165 @@ /* 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 = 4 var noOfCarriages = [5, 6, 5, 6] var rowWidth = [0.95, 0.1, 0.1, 0.1] var memoryMode = false var solutionArray = [] var isReset = false var resourceURL = "qrc:/gcompris/src/activities/railroad/resource/" var maxSubLevel = 3 var items function start(items_) { items = items_ currentLevel = 0 items.score.numberOfSubLevels = maxSubLevel; items.score.currentSubLevel = 1; initLevel() } function stop() { } function initLevel() { var index = 0; memoryMode = false; - items.displayFlow.rotation = 0; items.railCollection.visible = false; items.animateFlow.stop(); // Stops any previous animations items.listModel.clear(); items.displayList.model = items.listModel; if (isReset == false) { // Initiates a new level solutionArray = []; for (var i = 0; i < currentLevel + 2; i++) { if (i == (currentLevel + 1)) { // 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() * 12) + 10; } while (solutionArray.indexOf(index) != -1) } solutionArray.push(index); items.listModel.append({"id" : index}); (items.displayList.itemAt(items.listModel.count - 1)).source = resourceURL + "loco" + (index) + ".svg"; } } else { // Re-setup the same level for ( var i = 0; i < solutionArray.length; i++) { items.listModel.append({"id" : solutionArray[i]}); (items.displayList.itemAt(items.listModel.count - 1)).source = resourceURL + "loco" + (solutionArray[i]) + ".svg"; } isReset = false; } if (items.introMessage.visible === false) { items.animateFlow.start() } items.bar.level = currentLevel + 1; } function nextLevel() { if(numberOfLevel <= ++currentLevel ) { currentLevel = 0 } items.score.currentSubLevel = 1; initLevel(); } function previousLevel() { if(--currentLevel < 0) { currentLevel = numberOfLevel - 1 } items.score.currentSubLevel = 1; initLevel(); } function reset() { isReset = true; initLevel(); } function advanceSubLevel() { /* Sets up the next sublevel */ items.score.currentSubLevel++; if (items.score.currentSubLevel > maxSubLevel) { nextLevel(); items.score.currentSubLevel = 1; } else { initLevel(); } } function isAnswer() { /* Checks if the top level setup equals the solutions */ if (items.listModel.count === solutionArray.length) { var flag = true; - for (var index = items.listModel.count - 1; index > 0; index--) { - if (items.listModel.get(index).id != solutionArray[items.listModel.count - 1 - index]) { + for (var index = 0; index < items.listModel.count; index++) { + if (items.listModel.get(index).id != solutionArray[index]) { flag = false; break; } } if (flag == true) { items.bonus.good("flower"); } } } function sum(index) { /* Returns the sum up till the specified index */ var total = 0 for (var i = 0; i