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