Debian Codegraph @

src/activities/graduated_line_read/graduated_line_read.js

raw
1 /* GCompris - graduated_line_read.js
2 *
3 * SPDX-FileCopyrightText: 2023 Bruno ANSELME <[email protected]>
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 *
6 */
7 .pragma library
8 .import QtQuick as Quick
9 .import core 1.0 as GCompris // for ApplicationInfo
10 .import "qrc:/gcompris/src/core/core.js" as Core
11
12 var numberOfLevel
13 var items
14 var activityMode
15 var maxSolutionSize = 0
16 var mapToPad = {} // Maps keyboard charcodes to numPad's indexes to animate graphics from computer's numpad
17 var exercises = []
18
19 function randInt(max) { return Math.floor(Math.random() * max) }
20
21 function longInt(val) {
22 // we allow to have up to 5 numbers after the separator
23 var str = Core.convertNumberToLocaleString(val, GCompris.ApplicationInfo.localeShort, 'f', 5)
24 // Remove extra 0 only for decimal numbers
25 var isDecimalNumber = false;
26 for(var i = 0; i < str.length; ++i) {
27 if(isNaN(str[i])) {
28 isDecimalNumber = true;
29 break;
30 }
31 }
32 if(isDecimalNumber) {
33 str = str.replace(/0+$/,'');
34 }
35 // handle if last character is a separator, remove it
36 if(isNaN(str[str.length-1])) {
37 str = str.substr(0, str.length-1)
38 }
39 return str
40 }
41
42 function createExercise(idx, rules, s) {
43 var nbSeg = 2
44 var step = rules.steps[s]
45 var maxOffset = 0
46 if (rules.fitLimits) {
47 nbSeg = Math.floor((rules.range[1] - rules.range[0]) / step) + 1
48 } else {
49 nbSeg = 1 + rules.segments[0] + randInt(1 + rules.segments[1] - rules.segments[0])
50 maxOffset = rules.range[1] - (nbSeg * step)
51 while (maxOffset < 0) { // if data is not valid, reduce the number of segment
52 nbSeg--
53 maxOffset = rules.range[1] - (nbSeg * step)
54 }
55 maxOffset = rules.range[1] - ((nbSeg - 1) * step)
56 }
57 if (rules.fitLimits) {
58 idx = 1 + (idx % (nbSeg - 2))
59 } else {
60 idx = randInt(nbSeg - 2) + 1
61 }
62 var start = rules.range[0] + randInt(maxOffset)
63 return {
64 "solution": idx,
65 "step": step,
66 "nbSeg": nbSeg,
67 "start": start,
68 "rangeMin": rules.range[0],
69 "rangeMax": rules.range[1]
70 }
71 }
72
73 function createLevel() { // Create an array of exercise
74 exercises = []
75 var levelRules = items.levels[items.currentLevel].rules
76 for(var s = 0; s < levelRules.steps.length; s++) {
77 var nbTicks = Math.floor((levelRules.range[1] - levelRules.range[0]) / levelRules.steps[s]) -1
78 for(var i = 0; i < nbTicks; i++) {
79 var exo = createExercise(i, levelRules, s)
80 exercises.push(exo)
81 }
82 }
83 }
84
85 function buildRuler() { // Read from exercises with currentSubLevel index
86 items.solutionGrad = 0
87 items.rulerModel.clear()
88 if (items.currentSubLevel % exercises.length === 0) // if first time or all exercises already done
89 Core.shuffle(exercises) // shuffle exercises before restarting
90 var exo = exercises[items.currentSubLevel % exercises.length]
91 var start = exo.start
92 var thickStep = Math.floor(exo.rangeMax / 10)
93 if (thickStep < 10)
94 thickStep = 10
95 var i = 0
96 for (i = 0; i < exo.nbSeg; i++) { // Create rulerModel
97 var thick = items.segmentThickness
98 if (start % thickStep === 0)
99 thick = 2 * items.segmentThickness
100 if ((i === 0) || (i === exo.nbSeg - 1))
101 thick = 3 * items.segmentThickness
102 items.rulerModel.append({ "value_": start
103 , "thickness_": thick })
104 start += exo.step
105 }
106 var min = (items.orientation === Qt.LeftToRight) ? 0 : items.rulerModel.count -1
107 var max = (items.orientation === Qt.LeftToRight) ? items.rulerModel.count -1 : 0
108 var denominator = 1
109 if (items.levels[items.currentLevel].rules.denominator && items.levels[items.currentLevel].rules.denominator != 1) {
110 denominator = items.levels[items.currentLevel].rules.denominator
111 }
112 items.leftLimit.text = longInt(items.rulerModel.get(min).value_ / denominator)
113 items.rightLimit.text = longInt(items.rulerModel.get(max).value_ / denominator)
114
115 items.solutionGrad = exo.solution
116 items.answer = items.rulerModel.get(items.solutionGrad).value_.toString()
117 maxSolutionSize = String(longInt(items.answer) / denominator).length
118 if (activityMode === "number2tick") // Choose an other starting tick
119 items.solutionGrad = randInt(exo.nbSeg - 2) + 1
120 }
121
122 function createRuler() {
123 var levelRules = items.levels[items.currentLevel].rules
124 items.numberOfSubLevel = levelRules.nbOfQuestions
125 buildRuler()
126 if(levelRules.denominator) {
127 items.denominator = levelRules.denominator
128 }
129 else {
130 items.denominator = 1
131 }
132 var title = items.levels[items.currentLevel].title
133 if (!levelRules.fitLimits)
134 title = title.substr(0, title.length - 1) + " " + qsTr("(variable boundaries)") + title.substr(title.length - 1)
135 }
136
137 function updateNumpadWithDecimalPoint() {
138 // We only want to display the decimal point on levels with decimal numbers
139 var decimalPoint = Qt.locale(GCompris.ApplicationInfo.localeShort).decimalPoint;
140 // Arabic has a special decimal point but as we are displaying arabic numbers as Hebrew numbers, we use the . as separator
141 if(GCompris.ApplicationInfo.localeShort == "ar") {
142 decimalPoint = ".";
143 }
144 if(items.denominator == 1 && items.padModel.get(items.padModel.count-3).label == decimalPoint) {
145 items.padModel.set(items.padModel.count-3, {"label": "", "visible": false});
146 }
147 else if(items.denominator != 1 && items.padModel.get(items.padModel.count-3).label != decimalPoint) {
148 items.padModel.set(items.padModel.count-3, {"label": decimalPoint, "visible": true});
149 }
150 }
151
152 function moveLeft() {
153 if (items.score.isWinAnimationPlaying || items.buttonsBlocked)
154 return
155 if (items.solutionGrad > 1) {
156 items.clickSound.play()
157 items.solutionGrad--
158 }
159 }
160
161 function moveRight() {
162 if (items.score.isWinAnimationPlaying || items.buttonsBlocked)
163 return
164 if (items.solutionGrad < items.rulerModel.count - 2) {
165 items.clickSound.play()
166 items.solutionGrad++
167 }
168 }
169
170 function checkResult() {
171 if (items.score.isWinAnimationPlaying || items.buttonsBlocked)
172 return
173 items.buttonsBlocked = true;
174 var success = false;
175 switch (activityMode) {
176 case "tick2number":
177 var locale = GCompris.ApplicationInfo.localeShort
178 if(locale == "ar") {
179 locale = "he";
180 }
181 var value = Number.fromLocaleString(Qt.locale(locale), items.cursor.children[items.solutionGrad].textValue) * items.denominator
182 success = (String(value) === items.answer)
183 break
184 case "number2tick":
185 success = (items.rulerModel.get(items.solutionGrad).value_.toString() === items.answer)
186 if (success)
187 items.cursor.children[items.solutionGrad].textValue = items.boxText.text
188 break
189 }
190 items.client.sendToServer(success)
191 if (success) {
192 items.goodAnswerSound.play();
193 items.currentSubLevel ++;
194 items.score.playWinAnimation();
195 } else {
196 items.badAnswerSound.play();
197 items.buttonsBlocked = true;
198 items.errorRectangle.startAnimation();
199 }
200 }
201
202 function start(items_, activityMode_) {
203 items = items_;
204 activityMode = activityMode_
205 items.orientation = (Core.isLeftToRightLocale(GCompris.ApplicationSettings.locale)) ? Qt.LeftToRight : Qt.RightToLeft
206 // items.orientation = Qt.RightToLeft // Force RightToLeft here
207 // Make sure numberOfLevel is initialized before calling Core.getInitialLevel
208 numberOfLevel = items.levels.length
209 items.currentLevel = Core.getInitialLevel(numberOfLevel)
210 initLevel();
211 }
212
213 function stop() {
214 }
215
216 function initLevel() {
217 items.errorRectangle.resetState();
218 items.buttonsBlocked = false;
219 items.currentSubLevel = 0;
220 createLevel();
221 createRuler();
222 updateNumpadWithDecimalPoint();
223 items.client.startTiming() // for server version
224 }
225
226 function nextLevel() {
227 items.score.stopWinAnimation()
228 items.currentLevel = Core.getNextLevel(items.currentLevel, numberOfLevel);
229 initLevel();
230 }
231
232 function previousLevel() {
233 items.score.stopWinAnimation()
234 items.currentLevel = Core.getPreviousLevel(items.currentLevel, numberOfLevel);
235 initLevel();
236 }
237
238 function nextSubLevel() {
239 if(items.currentSubLevel >= items.numberOfSubLevel)
240 items.bonus.good("sun");
241 else {
242 items.buttonsBlocked = false;
243 createRuler(); // initLevel();
244 }
245 }
246
247 function previousSubLevel() {
248 if( --items.currentSubLevel < 0)
249 previousLevel();
250 else {
251 items.buttonsBlocked = false;
252 createRuler(); // initLevel();
253 }
254 }
255
256 function handleKeys(key) {
257 if (items.score.isWinAnimationPlaying || items.buttonsBlocked)
258 return
259 if (items.orientation === Qt.RightToLeft) {
260 switch (key) {
261 case Qt.Key_Left:
262 key = Qt.Key_Right
263 break
264 case Qt.Key_Right:
265 key = Qt.Key_Left
266 break
267 }
268 }
269 // Handle separators the same (maybe, we'll need to add more here...)
270 if(key == Qt.Key_Comma) {
271 key = Qt.Key_Period
272 }
273
274 switch (key) {
275 case Qt.Key_Space:
276 case Qt.Key_Return:
277 case Qt.Key_Enter:
278 if (activityMode === "tick2number") {
279 if (items.cursor.children[items.solutionGrad].textValue !== "")
280 checkResult()
281 } else {
282 checkResult()
283 }
284
285 break
286 case Qt.Key_Left:
287 if (activityMode === "number2tick")
288 moveLeft()
289 break
290 case Qt.Key_Right:
291 if (activityMode === "number2tick")
292 moveRight()
293 break
294 case Qt.Key_0:
295 case Qt.Key_1:
296 case Qt.Key_2:
297 case Qt.Key_3:
298 case Qt.Key_4:
299 case Qt.Key_5:
300 case Qt.Key_6:
301 case Qt.Key_7:
302 case Qt.Key_8:
303 case Qt.Key_9:
304 if (activityMode === "number2tick")
305 return
306 if (items.cursor.children[items.solutionGrad].textValue.length < maxSolutionSize) {
307 items.cursor.children[items.solutionGrad].textValue += key - '0'.charCodeAt(0)
308 items.numPad.currentIndex = mapToPad[key]
309 items.numPad.currentItem.state = "pressed"
310 } else {
311 items.smudgeSound.play()
312 }
313
314 break
315 case Qt.Key_Period:
316 if (activityMode === "number2tick" || items.denominator == 1)
317 return
318 var decimalPoint = Qt.locale(GCompris.ApplicationInfo.localeShort).decimalPoint
319 if(GCompris.ApplicationInfo.localeShort == "ar") {
320 decimalPoint = ".";
321 }
322
323 // Prevent to have multiple times the decimal point
324 if (items.cursor.children[items.solutionGrad].textValue.length < maxSolutionSize && items.cursor.children[items.solutionGrad].textValue.indexOf(decimalPoint) == -1) {
325 items.cursor.children[items.solutionGrad].textValue += decimalPoint
326 items.numPad.currentIndex = mapToPad[key]
327 items.numPad.currentItem.state = "pressed"
328 } else {
329 items.smudgeSound.play()
330 }
331
332 break
333 case Qt.Key_Backspace:
334 if (activityMode === "number2tick")
335 return
336 items.cursor.children[items.solutionGrad].textValue = items.cursor.children[items.solutionGrad].textValue.slice(0, -1)
337 items.numPad.currentIndex = mapToPad[key]
338 items.numPad.currentItem.state = "pressed"
339 break
340 case Qt.Key_Delete:
341 if (activityMode === "number2tick")
342 return
343 items.cursor.children[items.solutionGrad].textValue = ""
344 items.numPad.currentIndex = mapToPad[key]
345 items.numPad.currentItem.state = "pressed"
346 break
347 }
348 }
349
350 function handleEvents(event) {
351 handleKeys(event.key)
352 }