Warning, /education/gcompris/src/activities/share/DropChild.qml is written in an unsupported language. File is not indexed.

0001 /* GCompris - DropChild.qml
0002  *
0003  * SPDX-FileCopyrightText: 2016 Stefan Toncu <stefan.toncu29@gmail.com>
0004  *
0005  *   SPDX-License-Identifier: GPL-3.0-or-later
0006  */
0007 
0008 import QtQuick 2.12
0009 import GCompris 1.0
0010 
0011 import "../../core"
0012 
0013 Rectangle {
0014     id: dropChild
0015     width: items.cellSize * 3
0016     height: items.cellSize * 4.5
0017     color: "transparent"
0018     radius: 0.2
0019     z: 5
0020 
0021     property string name
0022     property alias childImage: childImage
0023     property alias area: area
0024     property int indexS: index
0025     property alias candyCount: candyCount
0026 
0027     Image {
0028         id: childImage
0029         width: items.cellSize
0030         sourceSize.width: width
0031         anchors.bottom: area.top
0032         anchors.left: parent.left
0033         anchors.leftMargin: 20
0034         source: "resource/images/" + name + ".svg"
0035         fillMode: Image.PreserveAspectFit
0036         mipmap: true
0037     }
0038 
0039     //displays the number of candies each child has
0040     GCText {
0041         id: candyCount
0042         color: "#373737"
0043         anchors.bottom: area.top
0044         anchors.right: parent.right
0045         anchors.rightMargin: 20
0046 
0047         //"listModel.get(index) ? ... " because of an error received at startup of each level
0048         text: (listModel.get(index)) ? listModel.get(index).countS : ""
0049     }
0050 
0051     Rectangle {
0052         id: area
0053         width: items.cellSize * 3
0054         height: items.cellSize * 3
0055         anchors.bottom: parent.bottom
0056         radius: width * 0.07
0057 
0058         color: "#f2f2f2"
0059 
0060         property var childCoordinate: repeaterDropAreas.mapToItem(background, dropChild.x, dropChild.y)
0061         property var candyCoord: candyWidget.mapToItem(background, candyWidget.element.x, candyWidget.element.y)
0062 
0063         opacity: candyCoord.x > childCoordinate.x &&
0064                  candyCoord.y > childCoordinate.y + childImage.height &&
0065                  candyCoord.x < childCoordinate.x + childCoordinate.width &&
0066                  candyCoord.y < childCoordinate.y + childCoordinate.height ? 0.5 : 1
0067 
0068         MouseArea {
0069             anchors.fill: parent
0070             enabled: !items.buttonsBlocked
0071 
0072             onClicked: {
0073                 if (items.acceptCandy) {
0074                     // Easy mode
0075                     if (background.easyMode) {
0076                         if (background.currentCandies < items.candyWidget.total) {
0077                             if (listModel.get(index).countS + 1 <= items.maxNumberOfCandiesPerWidget) {
0078                                 //add candies in the first rectangle
0079                                 repeaterDropAreas.itemAt(index).candyCount.text = listModel.get(index).countS + 1
0080                                 listModel.setProperty(index, "countS", listModel.get(index).countS + 1)
0081                                 //the current number of candies increases
0082                                 background.currentCandies ++
0083                                 //on the last one, the candy image from top goes away (destroy)
0084                                 if (background.currentCandies === items.candyWidget.total) {
0085                                     background.resetCandy()
0086                                     candyWidget.element.opacity = 0.6
0087                                 }
0088                             }
0089                             else {
0090                                 background.wrongMove.visible = true
0091                             }
0092                         }
0093                         else {
0094                             background.resetCandy()
0095                             candyWidget.element.opacity = 0.6
0096                         }
0097                     }
0098                     // Hard mode
0099                     else {
0100                         if (background.currentCandies < items.candyWidget.total) {
0101                             if (listModel.get(index).countS + 1 <= items.maxNumberOfCandiesPerWidget) {
0102                                 //add candies in the first rectangle
0103                                 repeaterDropAreas.itemAt(index).candyCount.text = listModel.get(index).countS + 1
0104                                 listModel.setProperty(index, "countS", listModel.get(index).countS + 1)
0105                                 //the current number of candies increases
0106                                 background.currentCandies ++
0107                                 if (background.currentCandies === items.candyWidget.total) {
0108                                     background.resetCandy()
0109                                     candyWidget.element.opacity = 0.6
0110                                 }
0111                             }
0112                             else {
0113                                 background.wrongMove.visible = true
0114                             }
0115                         }
0116                     }
0117                 }
0118             }
0119         }
0120 
0121         Flow {
0122             id: candyDropArea
0123             spacing: 5
0124             width: parent.width
0125             height: parent.height
0126 
0127             Repeater {
0128                 id: repeaterCandyDropArea
0129                 model: countS
0130 
0131                 Image {
0132                     id: candyArea
0133                     sourceSize.width: items.cellSize * 0.6
0134                     sourceSize.height: items.cellSize * 1.2
0135                     source: "resource/images/candy.svg"
0136                     fillMode: Image.PreserveAspectFit
0137 
0138                     property int lastX
0139                     property int lastY
0140 
0141                     MouseArea {
0142                         anchors.fill: parent
0143 
0144                         //enables dragging the candy after placed
0145                         drag.target: parent
0146                         enabled: !items.buttonsBlocked
0147 
0148                         onPressed: {
0149                             instruction.hide()
0150                             //set the initial position
0151                             candyArea.lastX = candyArea.x
0152                             candyArea.lastY = candyArea.y
0153                             //move this rectangle/grid on top of everything
0154                             dropChild.z++
0155                         }
0156 
0157                         function childContainsCandy(currentChild, candy) {
0158                             //coordinates of "boy/girl rectangle" in background coordinates
0159                             var child = dropAreas.mapToItem(items.background, currentChild.x, currentChild.y)
0160                             return (candy.x > child.x &&
0161                                 candy.x < child.x + currentChild.area.width &&
0162                                 candy.y > child.y + currentChild.childImage.height &&
0163                                 candy.y < child.y + currentChild.childImage.height + currentChild.area.height)
0164                         }
0165 
0166                         onReleased: {
0167                             //move this rectangle/grid to its previous state
0168                             dropChild.z--
0169 
0170                             var candyCoordinate = candyArea.parent.mapToItem(background, candyArea.x, candyArea.y)
0171 
0172                             //check where the candy is being dropped
0173                             for (var i = 0 ; i < listModel.count ; i++) {
0174                                 var currentChild = repeaterDropAreas.itemAt(i)
0175 
0176                                 if (currentChild !== dropChild) {
0177                                     //check if the user wants to put a candy to another rectangle
0178                                     if (childContainsCandy(currentChild, candyCoordinate)) {
0179                                         // don't drop more than the maximum of allowed candies per widget
0180                                         if(listModel.get(currentChild.indexS).countS >= items.maxNumberOfCandiesPerWidget) {
0181                                             background.wrongMove.visible = true
0182                                             break;
0183                                         }
0184 
0185                                         //add the candy to the i-th rectangle
0186                                         repeaterDropAreas.itemAt(i).candyCount.text = listModel.get(i).countS + 1
0187                                         listModel.setProperty(i, "countS", listModel.get(i).countS + 1)
0188                                         //remove the candy from current rectangle
0189                                         repeaterDropAreas.itemAt(rect2.indexS).candyCount.text = listModel.get(rect2.indexS).countS - 1
0190                                         listModel.setProperty(rect2.indexS, "countS", listModel.get(rect2.indexS).countS - 1);
0191                                         break;
0192                                     }
0193                                 }
0194                             }
0195 
0196                             //restore the candy to its initial position
0197                             candyArea.x = candyArea.lastX
0198                             candyArea.y = candyArea.lastY
0199                         }
0200 
0201                         //when clicked, it will restore the candy
0202                         onClicked: {
0203                             repeaterDropAreas.itemAt(rect2.indexS).candyCount.text = listModel.get(rect2.indexS).countS - 1
0204                             background.currentCandies--
0205                             candyWidget.element.opacity = 1
0206                             items.candyWidget.canDrag = true
0207                             listModel.setProperty(rect2.indexS, "countS", listModel.get(rect2.indexS).countS - 1);
0208                         }
0209                     }
0210                 }
0211             }
0212         }
0213     }
0214 }