File indexing completed on 2024-04-28 15:07:59

0001 /* GCompris - play_piano.js
0002  *
0003  * SPDX-FileCopyrightText: 2018 Aman Kumar Gupta <gupta2140@gmail.com>
0004  *
0005  * Authors:
0006  *   Beth Hadley <bethmhadley@gmail.com> (GTK+ version)
0007  *   Aman Kumar Gupta <gupta2140@gmail.com> (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 numberOfLevel = 10
0016 var noteIndexAnswered
0017 var items
0018 var levels
0019 var incorrectAnswers = []
0020 var isIntroductoryAudioPlaying = false
0021 
0022 function start(items_) {
0023     items = items_
0024     items.currentLevel = Core.getInitialLevel(numberOfLevel)
0025     levels = items.parser.parseFromUrl("qrc:/gcompris/src/activities/play_piano/dataset.json").levels
0026     items.introductoryAudioTimer.start()
0027     initLevel()
0028 }
0029 
0030 function stop() {
0031     items.introductoryAudioTimer.stop()
0032     items.multipleStaff.stopAudios()
0033 }
0034 
0035 function initLevel() {
0036     items.score.currentSubLevel = 0
0037     Core.shuffle(levels[items.currentLevel])
0038     nextSubLevel()
0039 }
0040 
0041 function initSubLevel() {
0042     var currentSubLevelMelody = levels[items.currentLevel][items.score.currentSubLevel]
0043     noteIndexAnswered = -1
0044     items.multipleStaff.loadFromData(currentSubLevelMelody)
0045 
0046     if(!isIntroductoryAudioPlaying && !items.iAmReady.visible)
0047         items.multipleStaff.play()
0048     items.buttonsBlocked = false
0049 }
0050 
0051 function nextSubLevel() {
0052     incorrectAnswers = []
0053     if(items.score.currentSubLevel >= levels[items.currentLevel].length)
0054         items.bonus.good("note")
0055     else
0056         initSubLevel()
0057 }
0058 
0059 function undoPreviousAnswer() {
0060     if(noteIndexAnswered >= 0) {
0061         items.multipleStaff.revertAnswer(noteIndexAnswered + 1)
0062         if(incorrectAnswers.indexOf(noteIndexAnswered) != -1)
0063             incorrectAnswers.pop()
0064 
0065         noteIndexAnswered--
0066     }
0067 }
0068 
0069 function checkAnswer(noteName) {
0070     var currentSubLevelNotes = levels[items.currentLevel][items.score.currentSubLevel].split(' ')
0071     if(noteIndexAnswered < (currentSubLevelNotes.length - 2)) {
0072         noteIndexAnswered++
0073         var currentNote = currentSubLevelNotes[noteIndexAnswered + 1]
0074         if((noteName + "Quarter") === currentNote)
0075             items.multipleStaff.indicateAnsweredNote(true, noteIndexAnswered + 1)
0076         else {
0077             incorrectAnswers.push(noteIndexAnswered)
0078             items.multipleStaff.indicateAnsweredNote(false, noteIndexAnswered + 1)
0079         }
0080 
0081         if(noteIndexAnswered === (currentSubLevelNotes.length - 2)) {
0082             items.buttonsBlocked = true
0083             items.answerFeedbackTimer.restart()
0084         }
0085     }
0086 }
0087 
0088 function answerFeedback() {
0089     if(incorrectAnswers.length === 0) {
0090         items.score.currentSubLevel += 1
0091         items.score.playWinAnimation()
0092         items.audioEffects.play("qrc:/gcompris/src/core/resource/sounds/completetask.wav")
0093     } else {
0094         items.audioEffects.play("qrc:/gcompris/src/core/resource/sounds/crash.wav")
0095         items.buttonsBlocked = false
0096     }
0097 }
0098 
0099 function nextLevel() {
0100     items.score.stopWinAnimation()
0101     items.currentLevel = Core.getNextLevel(items.currentLevel, numberOfLevel);
0102     initLevel();
0103 }
0104 
0105 function previousLevel() {
0106     items.score.stopWinAnimation()
0107     items.currentLevel = Core.getPreviousLevel(items.currentLevel, numberOfLevel);
0108     initLevel();
0109 }