File indexing completed on 2024-05-05 15:53:17

0001 /* GCompris - piano_composition.js
0002  *
0003  * SPDX-FileCopyrightText: 2016 Johnny Jazeix <jazeix@gmail.com>
0004  * SPDX-FileCopyrightText: 2018 Aman Kumar Gupta <gupta2140@gmail.com>
0005  *
0006  * Authors:
0007  *   Beth Hadley <bethmhadley@gmail.com> (GTK+ version)
0008  *   Johnny Jazeix <jazeix@gmail.com> (Qt Quick port)
0009  *   Aman Kumar Gupta <gupta2140@gmail.com> (Qt Quick port)
0010  *
0011  *   SPDX-License-Identifier: GPL-3.0-or-later
0012  */
0013 .pragma library
0014 .import QtQuick 2.12 as Quick
0015 .import GCompris 1.0 as GCompris
0016 .import "qrc:/gcompris/src/core/core.js" as Core
0017 
0018 var numberOfLevel = 7
0019 var items
0020 var userDir = "file://" + GCompris.ApplicationSettings.userDataPath + "/" + "piano_composition"
0021 var userFile = userDir + "/melodies.json"
0022 var undoStack = []
0023 var instructions = [{
0024         "text": qsTr("This is the treble clef staff for high pitched notes.")
0025     },
0026     {
0027         "text": qsTr("This is the bass clef staff for low pitched notes.")
0028     },
0029     {
0030         "text": qsTr("The black keys are sharp and flat keys. Sharp notes have a ♯ sign.")
0031     },
0032     {
0033         "text": qsTr("Each black key has two names: flat and sharp. Flat notes have ♭ sign.")
0034     },
0035     {
0036         "text": qsTr("Click on the note symbol to write different length notes such as whole notes, half notes, quarter notes and eighth notes.")
0037     },
0038     {
0039         "text": qsTr("Rests are equivalent to notes during which silence is maintained. Click on the rest symbol to select the rest length and then click on the add button to enter it to the staff.")
0040     },
0041     {
0042         "text": qsTr("Now you can load music and also save your compositions.")
0043     }
0044 ]
0045 
0046 function start(items_) {
0047     items = items_
0048     items.currentLevel = Core.getInitialLevel(numberOfLevel)
0049     initLevel()
0050 }
0051 
0052 function saveMelody() {
0053     var notes = items.multipleStaff.getAllNotes()
0054     if (!items.file.exists(userDir)) {
0055         if (!items.file.mkpath(userDir))
0056             console.error("Could not create directory " + userDir);
0057         else
0058             console.debug("Created directory " + userDir);
0059     }
0060 
0061     if (!items.file.append(JSON.stringify(notes), userFile)) {
0062         Core.showMessageDialog(items.background,
0063             qsTr("Error saving melody to your file (%1)")
0064             .arg(userFile),
0065             "", null, "", null, null);
0066     } else {
0067         Core.showMessageDialog(items.background,
0068             qsTr("Melody saved to your file (%1)")
0069             .arg(userFile),
0070             "", null, "", null, null);
0071     }
0072 }
0073 
0074 function stop() {
0075     items.multipleStaff.stopAudios()
0076 }
0077 
0078 function initLevel() {
0079     items.multipleStaff.bpmValue = 120
0080 
0081     if(items.currentLevel === 1) {
0082         items.background.clefType = "Bass"
0083         items.piano.currentOctaveNb = 0
0084     }
0085     else {
0086         items.background.clefType = "Treble"
0087         items.piano.currentOctaveNb = 1
0088     }
0089 
0090     items.multipleStaff.initClefs(items.background.clefType)
0091 
0092     if(items.currentLevel === 3)
0093         items.piano.useSharpNotation = false
0094     else
0095         items.piano.useSharpNotation = true
0096 
0097     items.multipleStaff.nbStaves = 2
0098     items.optionsRow.noteOptionsIndex = 2
0099     items.background.currentType = "Quarter"
0100     items.lyricsArea.resetLyricsArea()
0101     undoStack = []
0102 }
0103 
0104 function pushToStack(data) {
0105     undoStack.push(data)
0106     // Maintain most recent 5 changes. Remove older ones (stack behaves as queue here).
0107     if(undoStack.length > 5)
0108         undoStack.shift()
0109 }
0110 
0111 function undoChange() {
0112     if(undoStack.length > 0) {
0113         var undoState = undoStack[undoStack.length - 1]
0114         undoStack.pop()
0115         items.multipleStaff.redraw(undoState)
0116     }
0117 }
0118 
0119 function nextLevel() {
0120     items.multipleStaff.eraseAllNotes()
0121     items.currentLevel = Core.getNextLevel(items.currentLevel, numberOfLevel);
0122     initLevel()
0123 }
0124 
0125 function previousLevel() {
0126     items.multipleStaff.eraseAllNotes()
0127     items.currentLevel = Core.getPreviousLevel(items.currentLevel, numberOfLevel);
0128     initLevel()
0129 }