~baltix/gcompris-qt/0.97.1

« back to all changes in this revision

Viewing changes to src/activities/share/DropChild.qml

  • Committer: Mantas Kriaučiūnas
  • Date: 2020-07-06 18:07:11 UTC
  • Revision ID: baltix@gmail.com-20200706180711-g254osu02cn8bc8p
GCompris-QT 0.97.1 with Lithuanian translation

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GCompris - DropChild.qml
 
2
 *
 
3
 * Copyright (C) 2016 Stefan Toncu <stefan.toncu29@gmail.com>
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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/>.
 
17
 */
 
18
 
 
19
import QtQuick 2.6
 
20
import GCompris 1.0
 
21
 
 
22
import "../../core"
 
23
 
 
24
Rectangle {
 
25
    id: dropChild
 
26
    width: items.cellSize * 3
 
27
    height: items.cellSize * 4.5
 
28
    color: "transparent"
 
29
    radius: 0.2
 
30
    z: 5
 
31
 
 
32
    property string name
 
33
    property alias childImage: childImage
 
34
    property alias area: area
 
35
    property int indexS: index
 
36
    property alias candyCount: candyCount
 
37
 
 
38
    Image {
 
39
        id: childImage
 
40
        width: items.cellSize
 
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
 
47
        mipmap: true
 
48
    }
 
49
 
 
50
    //displays the number of candies each child has
 
51
    GCText {
 
52
        id: candyCount
 
53
        color: "#373737"
 
54
        anchors.bottom: area.top
 
55
        anchors.right: parent.right
 
56
        anchors.rightMargin: 20
 
57
 
 
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 : ""
 
60
    }
 
61
 
 
62
    Rectangle {
 
63
        id: area
 
64
        width: items.cellSize * 3
 
65
        height: items.cellSize * 3
 
66
        anchors.bottom: parent.bottom
 
67
        radius: width * 0.07
 
68
 
 
69
        color: "#f2f2f2"
 
70
 
 
71
        property var childCoordinate: repeaterDropAreas.mapToItem(background, dropChild.x, dropChild.y)
 
72
        property var candyCoord: candyWidget.mapToItem(background, candyWidget.element.x, candyWidget.element.y)
 
73
 
 
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
 
78
 
 
79
        MouseArea {
 
80
            anchors.fill: parent
 
81
 
 
82
            onClicked: {
 
83
                if (items.acceptCandy) {
 
84
                    // Easy mode
 
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
 
97
                                }
 
98
                            }
 
99
                        }
 
100
                        else {
 
101
                            background.resetCandy()
 
102
                            candyWidget.element.opacity = 0.6
 
103
                        }
 
104
                    }
 
105
                    // Hard mode
 
106
                    else {
 
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 ++
 
114
 
 
115
                                if (background.currentCandies + 1 === items.candyWidget.total) {
 
116
                                    background.resetCandy()
 
117
                                }
 
118
                            }
 
119
                            else {
 
120
                                background.wrongMove.visible = true
 
121
                            }
 
122
                        }
 
123
                    }
 
124
                }
 
125
            }
 
126
        }
 
127
 
 
128
        Flow {
 
129
            id: candyDropArea
 
130
            spacing: 5
 
131
            width: parent.width
 
132
            height: parent.height
 
133
 
 
134
            Repeater {
 
135
                id: repeaterCandyDropArea
 
136
                model: countS
 
137
 
 
138
                Image {
 
139
                    id: candyArea
 
140
                    sourceSize.width: items.cellSize * 0.6
 
141
                    sourceSize.height: items.cellSize * 1.2
 
142
                    source: "resource/images/candy.svg"
 
143
                    fillMode: Image.PreserveAspectFit
 
144
 
 
145
                    property int lastX
 
146
                    property int lastY
 
147
 
 
148
                    MouseArea {
 
149
                        anchors.fill: parent
 
150
 
 
151
                        //enables dragging the candy after placed
 
152
                        drag.target: parent
 
153
 
 
154
                        onPressed: {
 
155
                            instruction.hide()
 
156
                            //set the initial position
 
157
                            candyArea.lastX = candyArea.x
 
158
                            candyArea.lastY = candyArea.y
 
159
                            //move this rectangle/grid on top of everything
 
160
                            dropChild.z++
 
161
                        }
 
162
 
 
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)
 
170
                        }
 
171
 
 
172
                        onReleased: {
 
173
                            //move this rectangle/grid to its previous state
 
174
                            dropChild.z--
 
175
 
 
176
                            var candyCoordinate = candyArea.parent.mapToItem(background, candyArea.x, candyArea.y)
 
177
 
 
178
                            //check where the candy is being dropped
 
179
                            for (var i = 0 ; i < listModel.count ; i++) {
 
180
                                var currentChild = repeaterDropAreas.itemAt(i)
 
181
 
 
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
 
188
                                            break;
 
189
                                        }
 
190
 
 
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);
 
197
                                        break;
 
198
                                    }
 
199
                                }
 
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);
 
209
                                    break;
 
210
                                }
 
211
                            }
 
212
 
 
213
                            //restore the candy to its initial position
 
214
                            candyArea.x = candyArea.lastX
 
215
                            candyArea.y = candyArea.lastY
 
216
                        }
 
217
 
 
218
                        //when clicked, it will restore the candy
 
219
                        onClicked: {
 
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);
 
225
                        }
 
226
                    }
 
227
                }
 
228
            }
 
229
        }
 
230
    }
 
231
}