Changeset View
Changeset View
Standalone View
Standalone View
src/activities/analog_electricity/analog_electricity.js
- This file was added.
1 | /* GCompris - analog_electricity.js | ||||
---|---|---|---|---|---|
2 | * | ||||
3 | * Copyright (C) 2019 Deepak Kumar <pulkitnsit@gmail.com> | ||||
4 | * | ||||
5 | * Authors: | ||||
6 | * Bruno Coudoin <bruno.coudoin@gcompris.net> (GTK+ version) | ||||
7 | * Pulkit Gupta <pulkitnsit@gmail.com> (Qt Quick port) | ||||
8 | * Rudra Nil Basu <rudra.nil.basu.1996@gmail.com> (Qt Quick port) | ||||
9 | * Timothée Giet <animtim@gmail.com> (mouse drag refactoring) | ||||
10 | * | ||||
11 | * This program is free software; you can redistribute it and/or modify | ||||
12 | * it under the terms of the GNU General Public License as published by | ||||
13 | * the Free Software Foundation; either version 3 of the License, or | ||||
14 | * (at your option) any later version. | ||||
15 | * | ||||
16 | * This program is distributed in the hope that it will be useful, | ||||
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
19 | * GNU General Public License for more details. | ||||
20 | * | ||||
21 | * You should have received a copy of the GNU General Public License | ||||
22 | * along with this program; if not, see <https://www.gnu.org/licenses/>. | ||||
23 | */ | ||||
24 | .pragma library | ||||
25 | .import QtQuick 2.6 as Quick | ||||
26 | | ||||
27 | var currentLevel = 1 | ||||
28 | var numberOfLevel | ||||
29 | var items | ||||
30 | var url = "qrc:/gcompris/src/activities/analog_electricity/resource/" | ||||
31 | var toolDelete | ||||
32 | var toolDeleteSticky | ||||
33 | var selectedIndex | ||||
34 | var animationInProgress | ||||
35 | var selectedTerminal | ||||
36 | var deletedIndex = [] | ||||
37 | var components = [] | ||||
38 | var connected = [] | ||||
39 | var determiningComponents = [] | ||||
40 | var processingAnswer | ||||
41 | | ||||
42 | var currentZoom | ||||
43 | var maxZoom = 0.375 | ||||
44 | var minZoom = 0.125 | ||||
45 | var defaultZoom = 0.25 | ||||
46 | var zoomStep = 0.0625 | ||||
47 | | ||||
48 | var direction = { | ||||
49 | LEFT: -1, | ||||
50 | RIGHT: 1, | ||||
51 | UP: -2, | ||||
52 | DOWN: 2 | ||||
53 | } | ||||
54 | | ||||
55 | var viewPort = { | ||||
56 | leftExtreme: 0, | ||||
57 | rightExtreme: 1, | ||||
58 | topExtreme: 0, | ||||
59 | bottomExtreme: 1, | ||||
60 | leftEdge: 0, | ||||
61 | topEdge: 0 | ||||
62 | } | ||||
63 | | ||||
64 | function start(items_) { | ||||
65 | items = items_ | ||||
66 | currentLevel = 1 | ||||
67 | numberOfLevel = items.tutorialDataset.tutorialLevels.length | ||||
68 | initLevel() | ||||
69 | } | ||||
70 | | ||||
71 | function stop() { | ||||
72 | for(var i = 0 ; i < components.length ; ++i) { | ||||
73 | var j | ||||
74 | for(j = 0 ; j < deletedIndex.length ; ++j) { | ||||
75 | if(deletedIndex[j] == i) | ||||
76 | break | ||||
77 | } | ||||
78 | if(j == deletedIndex.length) | ||||
79 | removeComponent(i) | ||||
80 | } | ||||
81 | } | ||||
82 | | ||||
83 | function initLevel() { | ||||
84 | items.bar.level = currentLevel | ||||
85 | | ||||
86 | items.availablePieces.view.currentDisplayedGroup = 0 | ||||
87 | items.availablePieces.view.previousNavigation = 1 | ||||
88 | items.availablePieces.view.nextNavigation = 1 | ||||
89 | deletedIndex = [] | ||||
90 | components = [] | ||||
91 | connected = [] | ||||
92 | animationInProgress = false | ||||
93 | toolDelete = false | ||||
94 | toolDeleteSticky = false | ||||
95 | deselect() | ||||
96 | updateToolTip("") | ||||
97 | items.availablePieces.hideToolbar() | ||||
98 | | ||||
99 | currentZoom = defaultZoom | ||||
100 | items.availablePieces.zoomInBtn.state = "canZoomIn" | ||||
101 | items.availablePieces.zoomOutBtn.state = "canZoomOut" | ||||
102 | viewPort.leftEdge = 0 | ||||
103 | viewPort.topEdge = 0 | ||||
104 | items.playArea.x = items.mousePan.drag.maximumX | ||||
105 | items.playArea.y = items.mousePan.drag.maximumY | ||||
106 | | ||||
107 | if (!items.isTutorialMode) { | ||||
108 | items.tutorialInstruction.index = -1 | ||||
109 | loadFreeMode() | ||||
110 | } else { | ||||
111 | // load tutorial levels from dataset | ||||
112 | var levelProperties = items.tutorialDataset.tutorialLevels[currentLevel - 1] | ||||
113 | | ||||
114 | for (var i = 0; i < levelProperties.inputComponentList.length; i++) { | ||||
115 | var currentInputComponent = levelProperties.inputComponentList[i] | ||||
116 | items.availablePieces.model.append( { | ||||
117 | "imgName": currentInputComponent.imageName, | ||||
118 | "componentSrc": currentInputComponent.componentSource, | ||||
119 | "imgWidth": currentInputComponent.width, | ||||
120 | "imgHeight": currentInputComponent.height, | ||||
121 | "toolTipText": currentInputComponent.toolTipText | ||||
122 | }) | ||||
123 | } | ||||
124 | | ||||
125 | for (var i = 0; i < levelProperties.playAreaComponentList.length; i++) { | ||||
126 | var index = components.length | ||||
127 | var currentPlayAreaComponent = levelProperties.playAreaComponentList[i] | ||||
128 | var staticElectricalComponent = Qt.createComponent("qrc:/gcompris/src/activities/analog_electricity/" + currentPlayAreaComponent.componentSource) | ||||
129 | components[index] = staticElectricalComponent.createObject( | ||||
130 | items.playArea, { | ||||
131 | "index": index, | ||||
132 | "posX": levelProperties.playAreaComponentPositionX[i] * currentZoom, | ||||
133 | "posY": levelProperties.playAreaComponentPositionY[i] * currentZoom, | ||||
134 | "imgSrc": currentPlayAreaComponent.imageName, | ||||
135 | "toolTipTxt": currentPlayAreaComponent.toolTipText, | ||||
136 | "imgWidth": currentPlayAreaComponent.width * currentZoom, | ||||
137 | "imgHeight": currentPlayAreaComponent.height * currentZoom, | ||||
138 | "destructible": false | ||||
139 | }); | ||||
140 | } | ||||
141 | | ||||
142 | if (levelProperties.introMessage.length != 0) { | ||||
143 | items.tutorialInstruction.index = 0 | ||||
144 | items.tutorialInstruction.intro = levelProperties.introMessage | ||||
145 | } else { | ||||
146 | items.tutorialInstruction.index = -1 | ||||
147 | } | ||||
148 | } | ||||
149 | } | ||||
150 | | ||||
151 | function loadFreeMode() { | ||||
152 | | ||||
153 | var componentList = items.tutorialDataset.componentList | ||||
154 | for (var i = 0; i < componentList.length; i++) { | ||||
155 | items.availablePieces.model.append( { | ||||
156 | "imgName": componentList[i].imageName, | ||||
157 | "componentSrc": componentList[i].componentSource, | ||||
158 | "imgWidth": componentList[i].width, | ||||
159 | "imgHeight": componentList[i].height, | ||||
160 | "toolTipText": componentList[i].toolTipText | ||||
161 | }) | ||||
162 | } | ||||
163 | } | ||||
164 | | ||||
165 | function checkanswer() | ||||
166 | { | ||||
167 | // here the external library needs to be called | ||||
168 | } | ||||
169 | | ||||
170 | function zoomIn() { | ||||
171 | var previousZoom = currentZoom | ||||
172 | currentZoom += zoomStep | ||||
173 | if (currentZoom > maxZoom) | ||||
174 | currentZoom = maxZoom | ||||
175 | var zoomRatio = currentZoom / previousZoom | ||||
176 | updateComponentDimension(zoomRatio) | ||||
177 | | ||||
178 | if (currentZoom == maxZoom) { | ||||
179 | items.availablePieces.zoomInBtn.state = "cannotZoomIn" | ||||
180 | } else { | ||||
181 | items.availablePieces.zoomInBtn.state = "canZoomIn" | ||||
182 | } | ||||
183 | items.availablePieces.zoomOutBtn.state = "canZoomOut" | ||||
184 | | ||||
185 | if (items.zoomLvl < 0.5) { | ||||
186 | items.zoomLvl += 0.125 | ||||
187 | } | ||||
188 | } | ||||
189 | | ||||
190 | function zoomOut() { | ||||
191 | var previousZoom = currentZoom | ||||
192 | currentZoom -= zoomStep | ||||
193 | if (currentZoom < minZoom) | ||||
194 | currentZoom = minZoom | ||||
195 | var zoomRatio = currentZoom / previousZoom | ||||
196 | updateComponentDimension(zoomRatio) | ||||
197 | | ||||
198 | if (currentZoom == minZoom) { | ||||
199 | items.availablePieces.zoomOutBtn.state = "cannotZoomOut" | ||||
200 | } else { | ||||
201 | items.availablePieces.zoomOutBtn.state = "canZoomOut" | ||||
202 | } | ||||
203 | items.availablePieces.zoomInBtn.state = "canZoomIn" | ||||
204 | | ||||
205 | if (items.zoomLvl > 0) { | ||||
206 | items.zoomLvl -= 0.125 | ||||
207 | } | ||||
208 | } | ||||
209 | | ||||
210 | function updateComponentDimension(zoomRatio) { | ||||
211 | for (var i = 0; i < components.length; i++) { | ||||
212 | components[i].posX *= zoomRatio | ||||
213 | components[i].posY *= zoomRatio | ||||
214 | components[i].imgWidth *= zoomRatio | ||||
215 | components[i].imgHeight *= zoomRatio | ||||
216 | } | ||||
217 | } | ||||
218 | | ||||
219 | function nextLevel() { | ||||
220 | if(numberOfLevel < ++currentLevel) { | ||||
221 | currentLevel = 1 | ||||
222 | } | ||||
223 | reset(); | ||||
224 | } | ||||
225 | | ||||
226 | function previousLevel() { | ||||
227 | if(--currentLevel < 1) { | ||||
228 | currentLevel = numberOfLevel | ||||
229 | } | ||||
230 | reset(); | ||||
231 | } | ||||
232 | | ||||
233 | function reset() { | ||||
234 | stop() | ||||
235 | items.availablePieces.model.clear() | ||||
236 | initLevel() | ||||
237 | } | ||||
238 | | ||||
239 | // Creates component from ListWidget to the drawing board area | ||||
240 | function createComponent(x, y, componentIndex) { | ||||
241 | x = x / items.playArea.width | ||||
242 | y = y / items.playArea.height | ||||
243 | | ||||
244 | var index | ||||
245 | if(deletedIndex.length > 0) { | ||||
246 | index = deletedIndex[deletedIndex.length - 1] | ||||
247 | deletedIndex.pop() | ||||
248 | } | ||||
249 | else | ||||
250 | index = components.length | ||||
251 | | ||||
252 | var component = items.availablePieces.repeater.itemAt(componentIndex) | ||||
253 | var electricComponent = Qt.createComponent("qrc:/gcompris/src/activities/analog_electricity/" + | ||||
254 | component.source) | ||||
255 | | ||||
256 | //console.log("Error loading component:", electricComponent.errorString()) | ||||
257 | components[index] = electricComponent.createObject( | ||||
258 | items.playArea, { | ||||
259 | "index": index, | ||||
260 | "posX": x, | ||||
261 | "posY": y, | ||||
262 | "imgSrc": component.imageName, | ||||
263 | "toolTipTxt": component.toolTipTxt, | ||||
264 | "imgWidth": component.imageWidth * currentZoom, | ||||
265 | "imgHeight": component.imageHeight * currentZoom, | ||||
266 | "destructible": true | ||||
267 | }); | ||||
268 | | ||||
269 | toolDeleteSticky = false | ||||
270 | deselect() | ||||
271 | componentSelected(index) | ||||
272 | //updateComponent(index) | ||||
273 | } | ||||
274 | | ||||
275 | /* Creates wire between two terminals. Condition for creation of wire is that an input terminal | ||||
276 | * can only be connected to 1 wire, output terminals can be connected by any number of wires, and | ||||
277 | * an input terminal can be connected with an output terminal only. 'connected' variable is used | ||||
278 | * to make sure that an input is connected by only 1 wire. | ||||
279 | */ | ||||
280 | function terminalPointSelected(terminal) { | ||||
281 | if(selectedTerminal == -1 || selectedTerminal == terminal) | ||||
282 | selectedTerminal = terminal | ||||
283 | else if((selectedTerminal.type != terminal.type) && (selectedTerminal.parent != terminal.parent)) { | ||||
284 | var inTerminal = terminal.type == "In" ? terminal : selectedTerminal | ||||
285 | var outTerminal = terminal.type == "Out" ? terminal : selectedTerminal | ||||
286 | if(connected[inTerminal] == undefined || connected[inTerminal] == -1) { | ||||
287 | createWire(inTerminal, outTerminal, true) | ||||
288 | } | ||||
289 | deselect() | ||||
290 | } | ||||
291 | else { | ||||
292 | deselect() | ||||
293 | selectedTerminal = terminal | ||||
294 | terminal.selected = true | ||||
295 | } | ||||
296 | } | ||||
297 | | ||||
298 | | ||||
299 | function deselect() { | ||||
300 | if(toolDeleteSticky == false) { | ||||
301 | toolDelete = false | ||||
302 | items.availablePieces.toolDelete.state = "notSelected" | ||||
303 | } | ||||
304 | items.availablePieces.rotateLeft.state = "canNotBeSelected" | ||||
305 | items.availablePieces.rotateRight.state = "canNotBeSelected" | ||||
306 | items.availablePieces.info.state = "canNotBeSelected" | ||||
307 | items.infoTxt.visible = false | ||||
308 | selectedIndex = -1 | ||||
309 | selectedTerminal = -1 | ||||
310 | for(var i = 0 ; i < components.length ; ++i) { | ||||
311 | var component = components[i] | ||||
312 | for(var j = 0 ; j < component.noOfInputs ; ++j) | ||||
313 | component.inputTerminals.itemAt(j).selected = false | ||||
314 | for(var j = 0 ; j < component.noOfOutputs ; ++j) | ||||
315 | component.outputTerminals.itemAt(j).selected = false | ||||
316 | } | ||||
317 | } | ||||
318 | | ||||
319 | | ||||
320 | function componentSelected(index) { | ||||
321 | selectedIndex = index | ||||
322 | items.availablePieces.rotateLeft.state = "canBeSelected" | ||||
323 | items.availablePieces.rotateRight.state = "canBeSelected" | ||||
324 | items.availablePieces.info.state = "canBeSelected" | ||||
325 | } | ||||
326 | | ||||
327 | function rotateLeft() { | ||||
328 | components[selectedIndex].rotationAngle = -2 | ||||
329 | components[selectedIndex].rotateComponent.start() | ||||
330 | } | ||||
331 | | ||||
332 | function rotateRight() { | ||||
333 | components[selectedIndex].rotationAngle = 2 | ||||
334 | components[selectedIndex].rotateComponent.start() | ||||
335 | } | ||||
336 | | ||||
337 | function displayInfo() { | ||||
338 | var component = components[selectedIndex] | ||||
339 | var componentTruthTable = component.truthTable | ||||
340 | deselect() | ||||
341 | items.infoTxt.visible = true | ||||
342 | items.infoTxt.text = component.information | ||||
343 | | ||||
344 | if(component.infoImageSrc != undefined) { | ||||
345 | items.infoImage.imgVisible = true | ||||
346 | items.infoImage.source = url + component.infoImageSrc | ||||
347 | } | ||||
348 | else { | ||||
349 | items.infoImage.imgVisible = false | ||||
350 | items.infoImage.source = "" | ||||
351 | } | ||||
352 | } | ||||
353 | | ||||
354 | function updateToolTip(toolTipTxt) { | ||||
355 | items.toolTip.show(toolTipTxt) | ||||
356 | } |