File indexing completed on 2024-05-19 07:29:49
0001 /* GCompris - tangram.js 0002 * 0003 * SPDX-FileCopyrightText: 2016 Bruno Coudoin <bruno.coudoin@gcompris.net> 0004 * 0005 * Authors: 0006 * Yves Combe / Philippe Banwarth (GTK+ version) 0007 * Bruno Coudoin <bruno.coudoin@gcompris.net> (Qt Quick port) 0008 * 0009 * SPDX-License-Identifier: GPL-3.0-or-later 0010 */ 0011 .pragma library 0012 .import QtQuick 2.12 as Quick 0013 .import "qrc:/gcompris/src/core/core.js" as Core 0014 0015 var items 0016 0017 function start(items_) { 0018 items = items_ 0019 items.currentLevel = Core.getInitialLevel(items.numberOfLevel); 0020 initLevel() 0021 } 0022 0023 function stop() { 0024 } 0025 0026 function initLevel() { 0027 } 0028 0029 function nextLevel() { 0030 items.currentLevel = Core.getNextLevel(items.currentLevel, items.numberOfLevel); 0031 initLevel(); 0032 } 0033 0034 function previousLevel() { 0035 items.currentLevel = Core.getPreviousLevel(items.currentLevel, items.numberOfLevel); 0036 initLevel(); 0037 } 0038 0039 function getRandomInt(min, max) { 0040 return Math.floor(Math.random() * (max - min + 1) + min); 0041 } 0042 0043 // Determines the angle of a straight line drawn between point one and two. 0044 // The number returned, which is a float in radian, 0045 // tells us how much we have to rotate a horizontal line clockwise 0046 // for it to match the line between the two points. 0047 function getAngleOfLineBetweenTwoPoints(x1, y1, x2, y2) { 0048 var xDiff = x2 - x1; 0049 var yDiff = y2 - y1; 0050 return Math.atan2(yDiff, xDiff); 0051 } 0052 0053 function getDistance(ix, iy, jx, jy) { 0054 return Math.sqrt(Math.pow((ix - jx), 2) + Math.pow((iy - jy), 2)) 0055 } 0056 0057 function dumpTans(tans) { 0058 console.log("== tans ==") 0059 for(var i = 0; i < tans.length; i++) { 0060 console.log(tans[i].img, tans[i].x, tans[i].y, tans[i].rotation, tans[i].flipping) 0061 } 0062 } 0063 0064 /* Returns the [x, y] coordinate of the closest point */ 0065 function getClosest(point) { 0066 var nbpiece = items.currentTans.pieces.length 0067 var closestItem 0068 var closestDist = 1 0069 for(var i = 0; i < nbpiece; i++) { 0070 var p1 = items.currentTans.pieces[i] 0071 var dist = getDistance(p1.x, p1.y, point[0], point[1]) 0072 if(dist < closestDist) { 0073 closestDist = dist 0074 closestItem = p1 0075 } 0076 } 0077 0078 if(closestDist < 0.1) 0079 return [closestItem.x, closestItem.y] 0080 return 0081 } 0082 0083 function check() { 0084 var nbpiece = items.currentTans.pieces.length 0085 var userTans = items.userList.asTans() 0086 for(var i = 0; i < nbpiece; i++) { 0087 var p1 = items.currentTans.pieces[i] 0088 var ok = false 0089 for(var j = 0; j < nbpiece; j++) { 0090 var p2 = userTans[j] 0091 // Check type distance and rotation are close enough 0092 if(p1.img === p2.img && 0093 p1.flipping == p2.flipping && 0094 getDistance(p1.x, p1.y, p2.x, p2.y) < 0.01 && 0095 p1.rotation === p2.rotation ) { 0096 ok = true 0097 break 0098 } 0099 } 0100 if(!ok) 0101 return false 0102 } 0103 return true 0104 } 0105 0106 function toDataset() { 0107 var nbpiece = items.currentTans.pieces.length 0108 var userTans = items.userList.asTans() 0109 var tanss = ' {\n' + 0110 " 'bg': '',\n" + 0111 " 'name': '" + items.currentTans.name + "',\n" + 0112 " 'colorMask': '#999',\n" + 0113 " 'pieces': [\n" 0114 for(var i = 0; i < nbpiece; i++) { 0115 var p1 = items.currentTans.pieces[i] 0116 var p2 = userTans[i] 0117 tanss += ' {' + '\n' + 0118 " 'img': '" + p1.img + "',\n" + 0119 " 'flippable': " + p1.flippable + ',\n' + 0120 " 'flipping': " + p2.flipping + ',\n' + 0121 " 'height': " + p1.height + ',\n' + 0122 " 'initFlipping': " + p1.initFlipping + ',\n' + 0123 " 'initRotation': " + p1.initRotation + ',\n' + 0124 " 'initX': " + p1.initX + ',\n' + 0125 " 'initY': " + p1.initY + ',\n' + 0126 " 'moduloRotation': " + p1.moduloRotation + ',\n' + 0127 " 'rotation': " + p2.rotation + ',\n' + 0128 " 'width': " + p1.width + ',\n' + 0129 " 'x': " + p2.x + ',\n' + 0130 " 'y': " + p2.y + '\n' + 0131 " },\n"; 0132 } 0133 tanss += ' ]\n' + 0134 ' },\n' 0135 return(tanss) 0136 } 0137 0138 /* In edition mode arrow keys allow the move by 1 pixels in any direction */ 0139 function processPressedKey(event) { 0140 0141 if ( items.editionMode && items.selectedItem && items.selectedItem.selected) { 0142 /* Move the player */ 0143 switch (event.key) { 0144 case Qt.Key_Right: 0145 items.selectedItem.x += 1 0146 event.accepted = true 0147 break 0148 case Qt.Key_Left: 0149 items.selectedItem.x -= 1 0150 event.accepted = true 0151 break 0152 case Qt.Key_Up: 0153 items.selectedItem.y -= 1 0154 event.accepted = true 0155 break 0156 case Qt.Key_Down: 0157 items.selectedItem.y += 1 0158 event.accepted = true 0159 break 0160 } 0161 } 0162 } 0163