Changeset View
Changeset View
Standalone View
Standalone View
src/activities/play_piano/play_piano.js
- This file was added.
1 | /* GCompris - play_piano.js | ||||
---|---|---|---|---|---|
2 | * | ||||
3 | * Copyright (C) 2018 Aman Kumar Gupta <gupta2140@gmail.com> | ||||
4 | * | ||||
5 | * Authors: | ||||
6 | * Beth Hadley <bethmhadley@gmail.com> (GTK+ version) | ||||
7 | * Aman Kumar Gupta <gupta2140@gmail.com> (Qt Quick port) | ||||
8 | * | ||||
9 | * This program is free software; you can redistribute it and/or modify | ||||
10 | * it under the terms of the GNU General Public License as published by | ||||
11 | * the Free Software Foundation; either version 3 of the License, or | ||||
12 | * (at your option) any later version. | ||||
13 | * | ||||
14 | * This program is distributed in the hope that it will be useful, | ||||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
17 | * GNU General Public License for more details. | ||||
18 | * | ||||
19 | * You should have received a copy of the GNU General Public License | ||||
20 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | ||||
21 | */ | ||||
22 | .pragma library | ||||
23 | .import QtQuick 2.6 as Quick | ||||
24 | .import "qrc:/gcompris/src/core/core.js" as Core | ||||
25 | | ||||
26 | var currentLevel = 0 | ||||
27 | var currentSubLevel = 0 | ||||
28 | var numberOfLevel = 10 | ||||
29 | var noteIndexAnswered | ||||
30 | var items | ||||
31 | var levels | ||||
32 | var incorrectAnswers = [] | ||||
33 | var isIntroductoryAudioPlaying = false | ||||
34 | | ||||
35 | function start(items_) { | ||||
36 | items = items_ | ||||
37 | currentLevel = 0 | ||||
38 | levels = items.parser.parseFromUrl("qrc:/gcompris/src/activities/play_piano/dataset.json").levels | ||||
39 | items.introductoryAudioTimer.start() | ||||
40 | initLevel() | ||||
41 | } | ||||
42 | | ||||
43 | function stop() { | ||||
44 | items.multipleStaff.stopAudios() | ||||
45 | } | ||||
46 | | ||||
47 | function initLevel() { | ||||
48 | items.bar.level = currentLevel + 1 | ||||
49 | currentSubLevel = 0 | ||||
50 | Core.shuffle(levels[currentLevel]) | ||||
51 | nextSubLevel() | ||||
52 | } | ||||
53 | | ||||
54 | function initSubLevel() { | ||||
55 | var currentSubLevelMelody = levels[currentLevel][currentSubLevel - 1] | ||||
56 | noteIndexAnswered = -1 | ||||
57 | items.multipleStaff.loadFromData(currentSubLevelMelody) | ||||
58 | | ||||
59 | if(!isIntroductoryAudioPlaying && !items.iAmReady.visible) | ||||
60 | items.multipleStaff.play() | ||||
61 | } | ||||
62 | | ||||
63 | function nextSubLevel() { | ||||
64 | currentSubLevel++ | ||||
65 | incorrectAnswers = [] | ||||
66 | items.score.currentSubLevel = currentSubLevel | ||||
67 | if(currentSubLevel > levels[currentLevel].length) | ||||
68 | nextLevel() | ||||
69 | else | ||||
70 | initSubLevel() | ||||
71 | } | ||||
72 | | ||||
73 | function undoPreviousAnswer() { | ||||
74 | if(noteIndexAnswered >= 0) { | ||||
75 | items.multipleStaff.revertAnswer(noteIndexAnswered + 1) | ||||
76 | if(incorrectAnswers.indexOf(noteIndexAnswered) != -1) | ||||
77 | incorrectAnswers.pop() | ||||
78 | | ||||
79 | noteIndexAnswered-- | ||||
80 | } | ||||
81 | } | ||||
82 | | ||||
83 | function checkAnswer(noteName) { | ||||
84 | var currentSubLevelNotes = levels[currentLevel][currentSubLevel - 1].split(' ') | ||||
85 | if(noteIndexAnswered < (currentSubLevelNotes.length - 2)) { | ||||
86 | noteIndexAnswered++ | ||||
87 | var currentNote = currentSubLevelNotes[noteIndexAnswered + 1] | ||||
88 | if((noteName + "Quarter") === currentNote) | ||||
89 | items.multipleStaff.indicateAnsweredNote(true, noteIndexAnswered + 1) | ||||
90 | else { | ||||
91 | incorrectAnswers.push(noteIndexAnswered) | ||||
92 | items.multipleStaff.indicateAnsweredNote(false, noteIndexAnswered + 1) | ||||
93 | } | ||||
94 | | ||||
95 | if(noteIndexAnswered === (currentSubLevelNotes.length - 2)) { | ||||
96 | if(incorrectAnswers.length === 0) | ||||
97 | items.bonus.good("flower") | ||||
98 | else | ||||
99 | items.bonus.bad("flower") | ||||
100 | } | ||||
101 | } | ||||
102 | } | ||||
103 | | ||||
104 | function nextLevel() { | ||||
105 | if(numberOfLevel <= ++currentLevel) { | ||||
106 | currentLevel = 0 | ||||
107 | } | ||||
108 | initLevel() | ||||
109 | } | ||||
110 | | ||||
111 | function previousLevel() { | ||||
112 | if(--currentLevel < 0) { | ||||
113 | currentLevel = numberOfLevel - 1 | ||||
114 | } | ||||
115 | initLevel() | ||||
116 | } |