Warning, /education/gcompris/src/activities/target/TargetItem.qml is written in an unsupported language. File is not indexed.
0001 /* GCompris - TargetItem.qml 0002 * 0003 * SPDX-FileCopyrightText: 2014 Bruno coudoin <bruno.coudoin@gcompris.net> 0004 * 0005 * Authors: 0006 * Bruno Coudoin <bruno.coudoin@gcompris.net> (GTK+ version) 0007 * Bruno Coudoin <bruno.coudoin@gcompris.net> (Qt Quick port) 0008 * 0009 * SPDX-License-Identifier: GPL-3.0-or-later 0010 */ 0011 import QtQuick 2.12 0012 import GCompris 1.0 0013 0014 import "../../core" 0015 import "target.js" as Activity 0016 0017 Image { 0018 id: targetItem 0019 fillMode: Image.PreserveAspectCrop 0020 source: Activity.url + "target_background.svg" 0021 width: parent.width * zoom 0022 height: parent.height * zoom 0023 sourceSize.width: width 0024 sourceSize.height: height 0025 anchors.centerIn: parent 0026 0027 property int zoom: 2 0028 property alias model: targetModel 0029 property bool stopMe: false 0030 property var scores: [] 0031 property string scoreText 0032 property int scoreTotal 0033 0034 signal start 0035 signal stop 0036 signal attachArrow(Item arrow) 0037 0038 onStart: { 0039 scores = [] 0040 scoreText = "" 0041 scoreTotal = 0 0042 stopMe = false 0043 targetItem.anchors.horizontalCenterOffset = getRandomOffset(0) 0044 targetItem.anchors.verticalCenterOffset = getRandomOffset(0) 0045 } 0046 onStop: { 0047 stopMe = true 0048 targetItem.anchors.horizontalCenterOffset = 0 0049 targetItem.anchors.verticalCenterOffset = 0 0050 } 0051 0052 // Avoid taking the same value or the animation won't restart 0053 function getRandomOffset(oldValue) { 0054 if(oldValue != 0 && Math.random() < 0.5) 0055 return 0 0056 var maxSize = targetModel.get(0).size * ApplicationInfo.ratio 0057 do { 0058 var newValue = Math.floor(Math.random() * maxSize) - maxSize / 2 0059 } while(oldValue == newValue) 0060 return newValue 0061 } 0062 0063 onAttachArrow: { 0064 arrow.anchors.centerIn = undefined 0065 var coordArrow = parent.mapToItem(targetItem, arrow.x, arrow.y) 0066 arrow.parent = targetItem 0067 arrow.x = coordArrow.x 0068 arrow.y = coordArrow.y 0069 0070 var arrowCenterX = arrow.x + arrow.width / 2 0071 var arrowCenterY = arrow.y + arrow.height / 2 0072 0073 var centerX = targetItem.width / 2 0074 var centerY = targetItem.height / 2 0075 0076 // Calc the arrow score 0077 var dist = Math.sqrt(Math.pow(arrowCenterX - centerX, 2) + 0078 Math.pow(arrowCenterY - centerY, 2)) 0079 dist *= zoom / ApplicationInfo.ratio 0080 var score = 0 0081 for(var i = targetModel.count - 1; i >= 0; --i) { 0082 if(dist < targetModel.get(i).size) { 0083 score = targetModel.get(i).score 0084 break 0085 } 0086 } 0087 scores.push(score) 0088 scoreTotal += score 0089 scoreText = scores.join(" + ") 0090 parent.targetReached() 0091 } 0092 0093 Behavior on anchors.horizontalCenterOffset { 0094 id: horizontal 0095 NumberAnimation { 0096 id: anim1 0097 duration: 3000 0098 easing.type: Easing.InOutQuad 0099 onRunningChanged: { 0100 if(!anim1.running) { 0101 var newValue = getRandomOffset(targetItem.anchors.horizontalCenterOffset) 0102 if(!stopMe) 0103 targetItem.anchors.horizontalCenterOffset = newValue 0104 } 0105 } 0106 } 0107 } 0108 Behavior on anchors.verticalCenterOffset { 0109 id: vertical 0110 NumberAnimation { 0111 id: anim2 0112 duration: 3000 0113 easing.type: Easing.InOutQuad 0114 onRunningChanged: { 0115 if(!anim2.running) { 0116 var newValue = getRandomOffset(targetItem.anchors.verticalCenterOffset) 0117 if(!stopMe) 0118 targetItem.anchors.verticalCenterOffset = newValue 0119 } 0120 } 0121 } 0122 } 0123 0124 ListModel { 0125 id: targetModel 0126 } 0127 0128 Repeater { 0129 id: repeater 0130 model: targetModel 0131 0132 Rectangle { 0133 anchors.centerIn: targetItem 0134 width: size * ApplicationInfo.ratio 0135 height: size * ApplicationInfo.ratio 0136 color: model.color 0137 radius: width / 2 0138 border.width: 1 * ApplicationInfo.ratio 0139 border.color: "#40000000" 0140 0141 GCText { 0142 anchors.horizontalCenter: parent.horizontalCenter 0143 anchors.bottom: parent.bottom 0144 width: ApplicationInfo.ratio * 30 0145 horizontalAlignment: Text.AlignHCenter 0146 font.pointSize: 20 0147 minimumPointSize: 5 0148 fontSizeMode: Text.Fit 0149 text: score 0150 color: "#A0000000" 0151 } 0152 0153 } 0154 } 0155 }