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

0001  /* GCompris - hanoi_real.qml
0002  *
0003  * SPDX-FileCopyrightText: 2015 Amit Tomar <a.tomar@outlook.com>
0004  *
0005  * Authors:
0006  *   Bruno Coudoin <bruno.coudoin@gcompris.net> (GTK+ version)
0007  *   Amit Tomar <a.tomar@outlook.com> (Qt Quick port)
0008  *   Timothée Giet <animtim@gmail.com> (Graphics refactoring)
0009  *
0010  *   SPDX-License-Identifier: GPL-3.0-or-later
0011  */
0012 import QtQuick 2.12
0013 import GCompris 1.0
0014 
0015 import "../../core"
0016 import "hanoi_real.js" as Activity
0017 
0018 
0019 ActivityBase {
0020     id: activity
0021 
0022     onStart: focus = true
0023     onStop: {}
0024 
0025     property string activityMode: "real"
0026 
0027     pageComponent: Image {
0028         id: background
0029         source: Activity.url + "background.svg"
0030         sourceSize.width: width
0031         sourceSize.height: height
0032         fillMode: Image.PreserveAspectCrop
0033         signal start
0034         signal stop
0035 
0036         Component.onCompleted: {
0037             activity.start.connect(start)
0038             activity.stop.connect(stop)
0039         }
0040 
0041         // Add here the QML items you need to access in javascript
0042         QtObject {
0043             id: items
0044             property Item main: activity.main
0045             property alias background: background
0046             property int currentLevel: activity.currentLevel
0047             property alias bonus: bonus
0048             property alias discRepeater: discRepeater
0049             property alias towerModel: towerModel
0050             property bool hasWon: false
0051             property int numberOfDisc
0052         }
0053 
0054         onStart: { Activity.start(items, activityMode) }
0055         onStop: { Activity.stop() }
0056 
0057         onWidthChanged: Activity.sceneSizeChanged()
0058         onHeightChanged: Activity.sceneSizeChanged()
0059 
0060         Rectangle {
0061             id: instruction
0062             width: parent.width
0063             height: description.height + 5 * ApplicationInfo.ratio
0064             color: "#FFF"
0065             opacity: 0.8
0066             anchors {
0067                 bottom: bar.top
0068                 bottomMargin: 15 * ApplicationInfo.ratio
0069             }
0070             visible: bar.level == 1
0071         }
0072 
0073         GCText {
0074             id: description
0075             text: activityMode == "real" ? qsTr("Move the entire stack to the right peg, one disc at a time.") :
0076             qsTr("Build the same tower in the empty area as the one you see on the right-hand side")
0077             width: instruction.width
0078             fontSize: mediumSize
0079             color: "#373737"
0080             wrapMode: Text.WordWrap
0081             anchors.centerIn: instruction
0082             verticalAlignment: Text.AlignVCenter
0083             horizontalAlignment: Text.AlignHCenter
0084             visible: bar.level == 1
0085         }
0086 
0087         Repeater {
0088             id: discRepeater
0089 
0090             Rectangle {
0091                 id: disc
0092                 z: 4
0093                 width: Activity.getDiscWidth(index)
0094                 height: activityMode == "real"? towerModel.itemAt(0).height * 0.15: towerModel.itemAt(0).height / (Activity.nbMaxItemsByTower+1)
0095 
0096                 opacity: index < items.numberOfDisc ? 1 : 0
0097                 onHeightChanged: Activity.sceneSizeChanged()
0098                 property string baseColor: "#808080"
0099                 radius: height * 0.5
0100                 property bool mouseEnabled: true
0101                 property alias discMouseArea: discMouseArea
0102                 property Item towerImage
0103                 property int position // The position index on the tower
0104 
0105                 property alias text: textSimplified.text
0106 
0107                 color: Qt.darker(baseColor, 1.2)
0108                 anchors.horizontalCenter: parent.horizontalCenter
0109 
0110                 Behavior on y {
0111                     NumberAnimation {
0112                         id: bouncebehavior
0113                         easing {
0114                             type: Easing.OutElastic
0115                             amplitude: 1.0
0116                             period: 0.75
0117                         }
0118                     }
0119                 }
0120 
0121                 Rectangle {
0122                     id: inDisc
0123                     width: parent.width - 10 * ApplicationInfo.ratio
0124                     height: parent.height - 6 * ApplicationInfo.ratio
0125                     radius: width * 0.5
0126                     color: Qt.lighter(disc.baseColor, 1.2)
0127                     anchors.centerIn: parent
0128 
0129                     GCText {
0130                         id: textSimplified
0131                         visible: activityMode == "simplified"
0132                         color: "#373737"
0133                         anchors.verticalCenter: parent.verticalCenter
0134                         anchors.rightMargin: 10 * ApplicationInfo.ratio
0135                         anchors.right: parent.right
0136                     }
0137 
0138                 }
0139 
0140 
0141                 MouseArea {
0142                     id: discMouseArea
0143                     enabled: disc.mouseEnabled && !items.hasWon
0144                     anchors.centerIn: parent
0145                     width: Activity.getDiscWidth(0)
0146                     height: background.height
0147                     drag.target: parent
0148                     drag.axis: Drag.XandYAxis
0149                     hoverEnabled: true
0150 
0151                     onPressed: {
0152                         disc.anchors.horizontalCenter = undefined
0153                         // Need to higher the z tower for the disc to be above all other towers and disc
0154                         disc.towerImage.z ++
0155                         disc.z ++
0156                     }
0157 
0158                     onReleased: {
0159                         // Restore previous z before releasing the disc
0160                         disc.towerImage.z --
0161                         disc.z --
0162                         Activity.discReleased(index)
0163                         disc.anchors.horizontalCenter = disc.parent.horizontalCenter
0164                     }
0165                 }
0166             }
0167         }
0168 
0169         Grid {
0170             // do columns if mobile?
0171             rows: 1
0172             columnSpacing: (background.width - towerModel.model * towerModel.itemAt(0).width) / (items.towerModel.model+1)
0173 
0174             anchors {
0175                 bottom: instruction.top
0176                 horizontalCenter: parent.horizontalCenter
0177                 bottomMargin: 10 * ApplicationInfo.ratio
0178             }
0179             Repeater {
0180                 id: towerModel
0181                 model: 1 // will be dynamically set in js
0182                 delegate: Item {
0183                     id: towerImage
0184                     width: image.width
0185                     height: image.height
0186                     onHeightChanged: Activity.sceneSizeChanged()
0187                     Image {
0188                         id: image
0189                         source: 
0190                         if(activityMode == "simplified" && (modelData == towerModel.model-2))
0191                             //in simplified mode, the target tower
0192                             Activity.url + "disc_support-green.svg"
0193                         else if(activityMode == "simplified" && (modelData == towerModel.model-1))
0194                             // in simplified mode, the reference tower
0195                             Activity.url + "disc_support-red.svg"
0196                         else if(activityMode == "real" && (modelData == towerModel.model-1))
0197                             // in real mode, the target tower
0198                             Activity.url + "disc_support-green.svg"
0199                         else
0200                             Activity.url + "disc_support.svg"
0201                         width: background.width / (towerModel.model + 2.5)
0202                         fillMode: Image.Stretch
0203                         height: background.height - instruction.height - 2 * bar.height
0204                         sourceSize.width: width
0205                         sourceSize.height: height
0206                     }
0207                     z: 3
0208                 }
0209             }
0210         }
0211 
0212         DialogHelp {
0213             id: dialogHelpLeftRight
0214             onClose: home()
0215         }
0216 
0217         Bar {
0218             id: bar
0219             level: items.currentLevel + 1
0220             content: BarEnumContent { value: help | home | level | reload }
0221             onHelpClicked: {
0222                 displayDialog(dialogHelpLeftRight)
0223             }
0224             onPreviousLevelClicked: Activity.previousLevel()
0225             onNextLevelClicked: Activity.nextLevel()
0226             onHomeClicked: home()
0227             onReloadClicked: Activity.initLevel()
0228         }
0229 
0230         Bonus {
0231             id: bonus
0232             Component.onCompleted: win.connect(Activity.nextLevel)
0233         }
0234     }
0235 }