1
/* GCompris - DropChild.qml
3
* Copyright (C) 2016 Stefan Toncu <stefan.toncu29@gmail.com>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 3 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, see <https://www.gnu.org/licenses/>.
26
width: items.cellSize * 3
27
height: items.cellSize * 4.5
33
property alias childImage: childImage
34
property alias area: area
35
property int indexS: index
36
property alias candyCount: candyCount
41
sourceSize.width: width
42
anchors.bottom: area.top
43
anchors.left: parent.left
44
anchors.leftMargin: 20
45
source: "resource/images/" + name + ".svg"
46
fillMode: Image.PreserveAspectFit
50
//displays the number of candies each child has
54
anchors.bottom: area.top
55
anchors.right: parent.right
56
anchors.rightMargin: 20
58
//"listModel.get(index) ? ... " because of an error received at startup of each level
59
text: (listModel.get(index) && background.showCount) ? listModel.get(index).countS : ""
64
width: items.cellSize * 3
65
height: items.cellSize * 3
66
anchors.bottom: parent.bottom
71
property var childCoordinate: repeaterDropAreas.mapToItem(background, dropChild.x, dropChild.y)
72
property var candyCoord: candyWidget.mapToItem(background, candyWidget.element.x, candyWidget.element.y)
74
opacity: candyCoord.x > childCoordinate.x &&
75
candyCoord.y > childCoordinate.y + childImage.height &&
76
candyCoord.x < childCoordinate.x + childCoordinate.width &&
77
candyCoord.y < childCoordinate.y + childCoordinate.height ? 0.5 : 1
83
if (items.acceptCandy) {
85
if (background.easyMode) {
86
if (background.currentCandies < items.candyWidget.total) {
87
if (listModel.get(index).countS + 1 <= items.maxNumberOfCandiesPerWidget) {
88
//add candies in the first rectangle
89
repeaterDropAreas.itemAt(index).candyCount.text = listModel.get(index).countS + 1
90
listModel.setProperty(index, "countS", listModel.get(index).countS + 1)
91
//the current number of candies increases
92
background.currentCandies ++
93
//on the last one, the candy image from top goes away (destroy)
94
if (background.currentCandies === items.candyWidget.total) {
95
background.resetCandy()
96
candyWidget.element.opacity = 0.6
101
background.resetCandy()
102
candyWidget.element.opacity = 0.6
107
if (background.currentCandies < items.candyWidget.total) {
108
if (listModel.get(index).countS + 1 <= items.maxNumberOfCandiesPerWidget) {
109
//add candies in the first rectangle
110
repeaterDropAreas.itemAt(index).candyCount.text = listModel.get(index).countS + 1
111
listModel.setProperty(index, "countS", listModel.get(index).countS + 1)
112
//the current number of candies increases
113
background.currentCandies ++
115
if (background.currentCandies + 1 === items.candyWidget.total) {
116
background.resetCandy()
120
background.wrongMove.visible = true
132
height: parent.height
135
id: repeaterCandyDropArea
140
sourceSize.width: items.cellSize * 0.6
141
sourceSize.height: items.cellSize * 1.2
142
source: "resource/images/candy.svg"
143
fillMode: Image.PreserveAspectFit
151
//enables dragging the candy after placed
156
//set the initial position
157
candyArea.lastX = candyArea.x
158
candyArea.lastY = candyArea.y
159
//move this rectangle/grid on top of everything
163
function childContainsCandy(currentChild, candy) {
164
//coordinates of "boy/girl rectangle" in background coordinates
165
var child = dropAreas.mapToItem(items.background, currentChild.x, currentChild.y)
166
return (candy.x > child.x &&
167
candy.x < child.x + currentChild.area.width &&
168
candy.y > child.y + currentChild.childImage.height &&
169
candy.y < child.y + currentChild.childImage.height + currentChild.area.height)
173
//move this rectangle/grid to its previous state
176
var candyCoordinate = candyArea.parent.mapToItem(background, candyArea.x, candyArea.y)
178
//check where the candy is being dropped
179
for (var i = 0 ; i < listModel.count ; i++) {
180
var currentChild = repeaterDropAreas.itemAt(i)
182
if (currentChild !== dropChild) {
183
//check if the user wants to put a candy to another rectangle
184
if (childContainsCandy(currentChild, candyCoordinate)) {
185
// don't drop more than the maximum of allowed candies per widget
186
if(listModel.get(currentChild.indexS).countS >= items.maxNumberOfCandiesPerWidget) {
187
background.wrongMove.visible = true
191
//add the candy to the i-th rectangle
192
repeaterDropAreas.itemAt(i).candyCount.text = listModel.get(i).countS + 1
193
listModel.setProperty(i, "countS", listModel.get(i).countS + 1)
194
//remove the candy from current rectangle
195
repeaterDropAreas.itemAt(rect2.indexS).candyCount.text = listModel.get(rect2.indexS).countS - 1
196
listModel.setProperty(rect2.indexS, "countS", listModel.get(rect2.indexS).countS - 1);
200
else if (childContainsCandy(currentChild, candyCoordinate)) {
201
//check if the user wants to put back the candy
202
repeaterDropAreas.itemAt(rect2.indexS).candyCount.text = listModel.get(rect2.indexS).countS - 1
203
//restore the candy to the leftWidget
204
background.currentCandies --
205
candyWidget.element.opacity = 1
206
items.candyWidget.canDrag = true
207
//remove the candy from current rectangle
208
listModel.setProperty(rect2.indexS, "countS", listModel.get(rect2.indexS).countS - 1);
213
//restore the candy to its initial position
214
candyArea.x = candyArea.lastX
215
candyArea.y = candyArea.lastY
218
//when clicked, it will restore the candy
220
repeaterDropAreas.itemAt(rect2.indexS).candyCount.text = listModel.get(rect2.indexS).countS - 1
221
background.currentCandies--
222
candyWidget.element.opacity = 1
223
items.candyWidget.canDrag = true
224
listModel.setProperty(rect2.indexS, "countS", listModel.get(rect2.indexS).countS - 1);