Commit 31c94633 authored by Bruno Coudoin's avatar Bruno Coudoin
Browse files

followline, rewrite the drawing of the hose parts

Now this is done with createComponent which let us position items
more easily.
parent 93b82a46
/* GCompris - followline.qml
/* GCompris - FollowLine.qml
*
* Copyright (C) 2014 Bruno Coudoin
*
......@@ -49,13 +49,16 @@ ActivityBase {
// 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 fireman: fireman
property alias bar: bar
property alias bonus: bonus
property alias currentLock: hose.currentLock
property int currentLock: 0
property int lastLock: 0
}
onHeightChanged: Activity.initLevel()
onStart: { Activity.start(items) }
onStop: { Activity.stop() }
......@@ -110,6 +113,32 @@ ActivityBase {
onClose: home()
}
function win() {
fireflame.opacity = 0
water.opacity = 1
}
MultiPointTouchArea {
anchors.fill: parent
enabled: ApplicationInfo.isMobile
maximumTouchPoints: 1
z: 1000
onTouchUpdated: {
for(var i in touchPoints) {
var touch = touchPoints[i]
var part = background.childAt(touch.x, touch.y)
if(part) {
if(items.currentLock == part.index) {
items.currentLock++
if(items.currentLock == items.lastLock) {
background.win()
}
}
}
}
}
}
Bar {
id: bar
content: BarEnumContent { value: help | home | previous | next }
......@@ -134,117 +163,10 @@ ActivityBase {
anchors.fill: parent
enabled: !ApplicationInfo.isMobile
hoverEnabled: true
onPositionChanged: hose.currentLock > 0 && fireflame.opacity == 1 ?
hose.currentLock-- : false
}
function win() {
fireflame.opacity = 0
water.opacity = 1
}
MultiPointTouchArea {
anchors.fill: parent
enabled: ApplicationInfo.isMobile
maximumTouchPoints: 1
onTouchUpdated: {
for(var i in touchPoints) {
var touch = touchPoints[i]
var part = background.childAt(touch.x, touch.y)
if(part) {
if(part.i >= 0) {
if(hose.currentLock == part.i)
hose.currentLock++
if(hose.itemAt(hose.currentLock).opacity == 0) {
background.win()
}
} else {
hose.currentLock > 0 && fireflame.opacity == 1 ?
hose.currentLock-- : false
}
}
}
}
onPositionChanged: items.currentLock > 0 && fireflame.opacity == 1 ?
items.currentLock-- : false
}
Repeater {
id: hose
// We create more items than needed because
// we don't know how much we really need
model: Math.floor((activity.width - fireman.width - fire.width) / partWidth) * 4
anchors.left: fireman.right
property int partWidth: 50 * ApplicationInfo.ratio
property int period: 50 / 2
property int currentLock: 0
property double initX: fireman.x + fireman.width
Image {
source: index == hose.currentLock ?
Activity.url + "hose_lock.svg" :
index < hose.currentLock ?
Activity.url + "hose_water.svg" :
Activity.url + "hose_part.svg"
sourceSize.width: hose.partWidth
height: (50 + 5 * 8 / bar.level) * ApplicationInfo.ratio
// Do not display items when we reach the right target
opacity: x > fire.x ? 0 : 1
parent: background
x: getX()
y: getY()
z: index == hose.currentLock ? myparent.z + 1 : 100
transformOrigin: Item.Center
rotation: getAngleOfLineBetweenTwoPoints(myparent.x, myparent.y, x, y) *
(180 / Math.PI)
property int i: index
property double rotationRadian: getAngleOfLineBetweenTwoPoints(myparent.x, myparent.y, x, y)
property Item myparent: (index > 0) ? hose.itemAt(index - 1) : hose
function getX() {
if(index == 0) {
return hose.initX
}
// 0.7 to have overlapping items
return myparent.x + width * 0.7 * Math.cos(myparent.rotationRadian)
}
function getY() {
var Y = fireman.y + height / 2 +
Math.cos(((Math.PI * 2 * index) / hose.period)) *
20 * ApplicationInfo.ratio * bar.level -
20 * ApplicationInfo.ratio * bar.level
return Y < 0 ? 0 : Y
}
// Determines the angle of a straight line drawn between point one and two.
// The number returned, which is a float in radian,
// tells us how much we have to rotate a horizontal line clockwise
// for it to match the line between the two points.
function getAngleOfLineBetweenTwoPoints(x1, y1, x2, y2) {
if(index == 0)
return 0
var xDiff = x2 - x1;
var yDiff = y2 - y1;
return Math.atan2(yDiff, xDiff);
}
MouseArea {
anchors.fill: parent
enabled: !ApplicationInfo.isMobile
hoverEnabled: true
onEntered: {
if(hose.currentLock == index) hose.currentLock++
if(hose.itemAt(hose.currentLock).opacity == 0) {
background.win()
}
}
}
}
}
}
}
/* GCompris - LinePart.qml
*
* Copyright (C) 2014 Bruno Coudoin
*
* Authors:
* Bruno Coudoin <bruno.coudoin@gcompris.net>
*
* 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 <http://www.gnu.org/licenses/>.
*/
import QtQuick 2.0
Item {
id: part
property QtObject items
property int index
Rectangle {
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
anchors.verticalCenterOffset: - parent.height / 2
width: parent.width
height: parent.height * 0.2
radius: height / 8
color: "black"
z: 10
}
Rectangle {
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
anchors.horizontalCenterOffset: - parent.width * 0.1
anchors.verticalCenterOffset: 0
width: parent.width * 1.2
height: parent.height * 0.9
radius: height / 4
z: 5
color: index < part.items.currentLock
? "blue"
: index === part.items.currentLock
? "red"
: "grey"
MouseArea {
anchors.fill: parent
hoverEnabled: true
onEntered: {
if(part.items.currentLock == part.index) part.items.currentLock++
if(part.items.currentLock >= part.items.lastLock) {
items.background.win()
}
}
}
}
Rectangle {
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
anchors.verticalCenterOffset: parent.height / 2
width: parent.width
height: parent.height * 0.2
radius: height / 8
color: "black"
z: 10
}
}
......@@ -21,12 +21,14 @@
*/
.pragma library
.import QtQuick 2.0 as Quick
.import GCompris 1.0 as GCompris
var url = "qrc:/gcompris/src/activities/followline/resource/"
var currentLevel = 0
var numberOfLevel = 8
var items
var createdLineParts
function start(items_) {
items = items_
......@@ -35,11 +37,45 @@ function start(items_) {
}
function stop() {
destroyLineParts()
}
function initLevel() {
if(!items)
return
items.bar.level = currentLevel + 1
items.currentLock = 0
destroyLineParts()
createdLineParts = new Array()
var width = 40 * GCompris.ApplicationInfo.ratio
var height = 60 * GCompris.ApplicationInfo.ratio
var index = 0
var y = items.fireman.y
var x = items.fireman.x + items.fireman.width
var angle = 0
var directionStep = 0.01 * (currentLevel + 1)
var direction = directionStep
do {
var newy = y + Math.sin(angle) * width * 0.5
var newx = x + Math.cos(angle) * width * 0.5
angle += direction
if(angle > Math.PI / 4)
direction = - directionStep
else if(angle < - Math.PI / 4)
direction = directionStep
if(y > items.background.height * 0.6)
direction = - directionStep
else if(y < items.background.height * 0.4)
direction = directionStep
// console.log("angle", angle, "direction", direction, "y", y, "items.background.height", items.background.height)
createdLineParts[index] =
createLinePart(index, x, y, width, height,
getAngleOfLineBetweenTwoPoints(x, y, newx, newy) * (180 / Math.PI))
x = newx
y = newy
index++
} while(x < items.background.width * 0.9)
items.lastLock = index - 1
}
function nextLevel() {
......@@ -55,3 +91,45 @@ function previousLevel() {
}
initLevel();
}
function createLinePart(index, x, y, width, height, rotation) {
// console.log("createLinePart", x, y, width, rotation, items)
var component = Qt.createComponent("qrc:/gcompris/src/activities/followline/LinePart.qml");
var part = component.createObject(
items.background,
{
"x": x,
"y": y,
"width": width,
"height": height,
"rotation": rotation,
"z": index,
"items": items,
"index": index
});
if (part === null) {
// Error Handling
console.log("Error creating LinePart object");
}
return part;
}
// Determines the angle of a straight line drawn between point one and two.
// The number returned, which is a float in radian,
// tells us how much we have to rotate a horizontal line clockwise
// for it to match the line between the two points.
function getAngleOfLineBetweenTwoPoints(x1, y1, x2, y2) {
var xDiff = x2 - x1;
var yDiff = y2 - y1;
return Math.atan2(yDiff, xDiff);
}
function destroyLineParts() {
if (createdLineParts) {
for(var i = 0; i < createdLineParts.length; ++i) {
createdLineParts[i].destroy()
}
createdLineParts.length = 0
}
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment