diff --git a/src/activities/hangman/Hangman.qml b/src/activities/hangman/Hangman.qml index b352e0aa3..e88fb66ef 100644 --- a/src/activities/hangman/Hangman.qml +++ b/src/activities/hangman/Hangman.qml @@ -1,409 +1,409 @@ /* GCompris - hangman.qml * * Copyright (C) 2015 Rajdeep Kaur * * Authors: * Bruno Coudoin (GTK+ version) * Rajdeep kaur (Qt Quick port) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, see . */ import QtQuick 2.1 import GCompris 1.0 import QtGraphicalEffects 1.0 import "../../core" import "hangman.js" as Activity import "qrc:/gcompris/src/core/core.js" as Core ActivityBase { id: activity // Overload this in your activity to change it // Put you default-.json files in it property string dataSetUrl: "qrc:/gcompris/src/activities/hangman/resource/" onStart: focus = true onStop: { } // When going on configuration, it steals the focus and re set it to the activity. // We need to set it back to the textinput item in order to have key events. onFocusChanged: { if(focus) { Activity.focusTextInput() } } pageComponent: Image { id: background source: activity.dataSetUrl+"background.svg" fillMode: Image.PreserveAspectCrop anchors.fill: parent sourceSize.width: Math.max(parent.width, parent.height) // system locale by default property string locale: "system" readonly property string wordsResource: "data2/words/words.rcc" property bool englishFallback: false signal start signal stop Component.onCompleted: { dialogActivityConfig.getInitialConfiguration() activity.start.connect(start) activity.stop.connect(stop) } // Add here the QML items you need to access in javascript QtObject { id: items property Item main: activity.main property alias background: background property Item ourActivity: activity property alias bar: bar property alias bonus: bonus property alias keyboard: keyboard property alias hidden: hidden property alias textinput: textinput property alias wordImage: wordImage property alias score: score property alias parser: parser property alias locale: background.locale property alias ok: ok - property int remainingLife + property int remainingLife property variant goodWord property int goodWordIndex property alias englishFallbackDialog: englishFallbackDialog function playWord() { var locale = ApplicationInfo.getVoicesLocale(items.locale) if(activity.audioVoices.append( ApplicationInfo.getAudioFilePathForLocale(goodWord.voice, locale))) bonus.interval = 2500 else bonus.interval = 500 } onRemainingLifeChanged: { if(remainingLife == 3) { playWord(); } } } onStart: { focus = true Activity.start(items) Activity.focusTextInput() } onStop: { Activity.stop(); } GCText { id: hidden fontSize: largeSize color: "#4d4d4d" font.letterSpacing: 0.5 width: parent.width * 0.9 wrapMode: Text.WordWrap horizontalAlignment: Text.AlignHCenter anchors { horizontalCenter: parent.horizontalCenter bottom: bar.top bottomMargin: 5 * ApplicationInfo.ratio } z: 11 } TextInput { // Helper element to capture composed key events like french ô which // are not available via Keys.onPressed() on linux. Must be // disabled on mobile! id: textinput enabled: !ApplicationInfo.isMobile visible: false focus: true onTextChanged: { if (text != "") { Activity.processKeyPress(text); text = ""; } } onAccepted: if(items.remainingLife === 0) Activity.nextSubLevel() } Item { id: imageframe width: Math.min(300 * ApplicationInfo.ratio, background.width * 0.8, hidden.y) height: width anchors.horizontalCenter: parent.horizontalCenter y: 5 * ApplicationInfo.ratio z: 10 Image { id: wordImage smooth: true visible: false anchors.fill: parent property string nextSource function changeSource(nextSource_) { nextSource = nextSource_ animImage.start() } SequentialAnimation { id: animImage PropertyAnimation { target: wordImage property: "opacity" to: 0 duration: 100 } PropertyAction { target: wordImage property: "source" value: wordImage.nextSource } PropertyAnimation { target: wordImage property: "opacity" to: 1 duration: 100 } } } Image { id: threshmask smooth: true visible: false width: 1.3*parent.width height: 1.2*parent.height source: dataSetUrl + "fog.png" } ThresholdMask { id: thresh anchors.fill: wordImage source: wordImage maskSource: threshmask spread: 0.4 // remainingLife between 0 and 6 => threshold between 0 and 0.9 threshold: 0.15 * items.remainingLife } } DialogActivityConfig { id: dialogActivityConfig currentActivity: activity content: Component { Item { property alias localeBox: localeBox height: column.height property alias availableLangs: langs.languages LanguageList { id: langs } Column { id: column spacing: 10 width: parent.width Flow { spacing: 5 width: dialogActivityConfig.width GCComboBox { id: localeBox model: langs.languages background: dialogActivityConfig label: qsTr("Select your locale") } } } } } onClose: home() onLoadData: { if(dataToSave && dataToSave["locale"]) { background.locale = dataToSave["locale"]; } } onSaveData: { var oldLocale = background.locale; var newLocale = dialogActivityConfig.configItem.availableLangs[dialogActivityConfig.loader.item.localeBox.currentIndex].locale; // Remove .UTF-8 if(newLocale.indexOf('.') != -1) { newLocale = newLocale.substring(0, newLocale.indexOf('.')) } dataToSave = {"locale": newLocale } background.locale = newLocale; // Restart the activity with new information if(oldLocale !== newLocale) { background.stop(); background.start(); } } function setDefaultValues() { var localeUtf8 = background.locale; if(background.locale != "system") { localeUtf8 += ".UTF-8"; } for(var i = 0 ; i < dialogActivityConfig.configItem.availableLangs.length ; i ++) { if(dialogActivityConfig.configItem.availableLangs[i].locale === localeUtf8) { dialogActivityConfig.loader.item.localeBox.currentIndex = i; break; } } } } DialogHelp { id: dialogHelp onClose: home() } Bar { id: bar anchors.bottom: keyboard.top content: BarEnumContent { value: help | home | level | config } onHelpClicked: { displayDialog(dialogHelp) } onPreviousLevelClicked: Activity.previousLevel() onNextLevelClicked: Activity.nextLevel() onHomeClicked: activity.home() onConfigClicked: { dialogActivityConfig.active = true dialogActivityConfig.setDefaultValues() displayDialog(dialogActivityConfig) } } Score { id: score anchors.top: undefined anchors.topMargin: 10 * ApplicationInfo.ratio anchors.right: parent.right anchors.rightMargin: 10 * ApplicationInfo.ratio anchors.bottom: keyboard.top } BarButton { id: ok source: "qrc:/gcompris/src/core/resource/bar_ok.svg"; sourceSize.width: 75 * ApplicationInfo.ratio visible: false anchors { bottom: score.top horizontalCenter: score.horizontalCenter bottomMargin: 10 * ApplicationInfo.ratio } onClicked: Activity.nextSubLevel() } JsonParser { id: parser onError: console.error("Hangman: Error parsing json: " + msg); } Image { id: clock anchors { left: parent.left top: parent.top margins: 10 } sourceSize.width: 66 * bar.barZoom property int remainingLife: items.remainingLife onRemainingLifeChanged: if(remainingLife >= 0) clockAnim.restart() SequentialAnimation { id: clockAnim alwaysRunToEnd: true ParallelAnimation { NumberAnimation { target: clock; properties: "opacity"; to: 0; duration: 800; easing.type: Easing.OutCubic } NumberAnimation { target: clock; properties: "rotation"; from: 0; to: 180; duration: 800; easing.type: Easing.OutCubic } } PropertyAction { target: clock; property: 'source'; value: "qrc:/gcompris/src/activities/reversecount/resource/" + "flower" + items.remainingLife + ".svg" } ParallelAnimation { NumberAnimation { target: clock; properties: "opacity"; to: 1; duration: 800; easing.type: Easing.OutCubic } NumberAnimation { target: clock; properties: "rotation"; from: 180; to: 0; duration: 800; easing.type: Easing.OutCubic } } } } VirtualKeyboard { id: keyboard anchors.bottom: parent.bottom anchors.horizontalCenter: parent.horizontalCenter width: parent.width onKeypress: Activity.processKeyPress(text); onError: console.log("VirtualKeyboard error: " + msg); } Bonus { id: bonus interval: 2000 onLoose: ok.visible = true onWin: Activity.nextSubLevel() } Loader { id: englishFallbackDialog sourceComponent: GCDialog { parent: activity.main message: qsTr("We are sorry, we don't have yet a translation for your language.") + " " + qsTr("GCompris is developed by the KDE community, you can translate GCompris by joining a translation team on %2").arg("http://l10n.kde.org/") + "

" + qsTr("We switched to English for this activity but you can select another language in the configuration dialog.") onClose: background.englishFallback = false } anchors.fill: parent focus: true active: background.englishFallback onStatusChanged: if (status == Loader.Ready) item.start() } } } diff --git a/src/activities/letter-in-word/Card.qml b/src/activities/letter-in-word/Card.qml index 6746d9ac8..0d97ac7bc 100755 --- a/src/activities/letter-in-word/Card.qml +++ b/src/activities/letter-in-word/Card.qml @@ -1,171 +1,178 @@ /* GCompris - Card.qml * * Copyright (C) 2016 Akshat Tandon * * Authors: * Akshat Tandon * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, see . */ import QtQuick 2.1 import GCompris 1.0 import QtGraphicalEffects 1.0 import "../../core" import "letter-in-word.js" as Activity Item { id: cardItem //width: cardImage.width height: wordPic.height + cardImage.height - 30 * ApplicationInfo.ratio property bool mouseActive: true Image { id: wordPic sourceSize.width: cardItem.width -6 sourceSize.height: cardItem.width -5 fillMode: Image.PreserveAspectFit source: imgurl z: -5 //visible: index % 2 != 0 ? false : true } Image { id: cardImage anchors.top: wordPic.bottom anchors.topMargin: -30 * ApplicationInfo.ratio sourceSize.width: cardItem.width - 10 fillMode: Image.PreserveAspectFit source: Activity.resUrl2 + "cloud.svg" z: (state == 'scaled') ? 1 : -1 //visible: index % 2 != 0 ? false : true GCText { id: textItem z: 11 // textFound is the rich text with letter found, spelling is the text in the dataset text:"" + (selected ? textFound : spelling) + "" property string textFound: spelling textFormat: Text.RichText font.pointSize: NaN // need to clear font.pointSize explicitly font.pixelSize: spelling.length > 5 ? (spelling.length > 7 ? cardImage.width * 0.19 : cardImage.width * 0.25): cardImage.width * 0.30 font.bold: true style: Text.Outline width: cardImage.width height: cardImage.height wrapMode: spelling.indexOf(' ') === -1 ? Text.WrapAnywhere : Text.WordWrap verticalAlignment: Text.AlignVCenter horizontalAlignment: Text.AlignHCenter styleColor: "#2a2a2a" } ParticleSystemStarLoader { id: particle clip: false } states: State { name: "scaled"; when: mouseArea.containsMouse && mouseActive PropertyChanges { target: cardItem scale: /*carriageImage.scale * */ 1.2 z: 2 } } transitions: Transition { NumberAnimation { properties: "scale"; easing.type: Easing.OutCubic } } SequentialAnimation { id: successAnimation running: selected loops: 2 NumberAnimation { target: cardImage easing.type: Easing.InOutQuad property: "rotation" to: 20; duration: 500 } NumberAnimation { target: cardImage easing.type: Easing.InOutQuad property: "rotation"; to: -20 duration: 500 } onRunningChanged: { if(!running && selected) { rotationStop.restart() } } } SequentialAnimation { id: failureAnimation NumberAnimation { target: colorCardImage property: "opacity" to: 1; duration: 400 } NumberAnimation { target: colorCardImage property: "opacity" to: 0; duration: 200 } } NumberAnimation { id: rotationStop running: !selected target: cardImage property: "rotation" to: 0 duration: 500 easing.type: Easing.InOutQuad } } Colorize { id: colorCardImage z: 5 anchors.fill: cardImage source: cardImage hue: 0.0 saturation: 1 opacity: 0 } MouseArea { id: mouseArea anchors.fill: parent hoverEnabled: ApplicationInfo.isMobile ? false : true onClicked: { select(); } } + function playWord() { + var locale = ApplicationInfo.getVoicesLocale(items.locale) + activity.audioVoices.append( + ApplicationInfo.getAudioFilePathForLocale(voice, locale)) + } + function select() { if(mouseActive && !successAnimation.running) { if (Activity.checkWord(index)) { successAnimation.restart(); particle.burst(30); textItem.textFound = spelling.replace(RegExp(Activity.currentLetter, "g"), ""+Activity.currentLetter+""); + playWord(); } else { failureAnimation.restart() } } } } diff --git a/src/activities/letter-in-word/letter-in-word.js b/src/activities/letter-in-word/letter-in-word.js index 91fc37b66..6f99c9ec3 100755 --- a/src/activities/letter-in-word/letter-in-word.js +++ b/src/activities/letter-in-word/letter-in-word.js @@ -1,259 +1,260 @@ /* GCompris - letter-in-word.js * * Copyright (C) 2014 Holger Kaelberer * 2016 Akshat Tandon * * Authors: * Holger Kaelberer (Qt Quick port of click-on-letter) * Akshat Tandon * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, see . */ .pragma library .import QtQuick 2.0 as Quick .import GCompris 1.0 as GCompris //for ApplicationInfo .import "qrc:/gcompris/src/core/core.js" as Core .import "qrc:/gcompris/src/activities/lang/lang_api.js" as Lang var url = "qrc:/gcompris/src/activities/letter-in-word/resource/" var resUrl = "qrc:/gcompris/src/activities/braille_fun/resource/" var resUrl2 = "qrc:/gcompris/src/activities/click_on_letter/resource/" var levels; var currentLevel; var maxLevel; var currentSubLevel; var currentLetter; var maxSubLevel; var questions; var words; var items; var dataset = null; var frequency; var incorrectFlag = false; var locale; function start(_items) { Core.checkForVoices(_items.main); items = _items; // register the voices for the locale locale = GCompris.ApplicationInfo.getVoicesLocale(items.locale) GCompris.DownloadManager.updateResource(GCompris.DownloadManager.getVoicesResourceForLocale(locale)) loadDataset(); levels = Lang.getAllLessons(dataset) currentLevel = 0; currentSubLevel = 0; maxLevel = levels.length; initLevel(); } function loadDataset() { var resourceUrl = "qrc:/gcompris/src/activities/lang/resource/" dataset = Lang.load(items.parser, resourceUrl, GCompris.ApplicationSettings.wordset ? "words.json" : "words_sample.json", "content-"+ locale +".json") // If dataset is empty, we try to load from short locale // and if not present again, we switch to default one var localeUnderscoreIndex = locale.indexOf('_') if(!dataset) { var localeShort; // We will first look again for locale xx (without _XX if exist) if(localeUnderscoreIndex > 0) { localeShort = locale.substring(0, localeUnderscoreIndex) } else { localeShort = locale; } dataset = Lang.load(items.parser, resourceUrl, GCompris.ApplicationSettings.wordset ? "words.json" : "words_sample.json", "content-"+localeShort+ ".json") } if(!dataset) { // English fallback dataset = Lang.load(items.parser, resourceUrl, GCompris.ApplicationSettings.wordset ? "words.json" : "words_sample.json", "content-en.json") } } function stop() { items.animateX.stop() } function shuffleString(s) { var a = s; var n = a.length; for(var i = n-1; i > 0; --i) { var j = Math.floor(Math.random() * (i + 1)); var tmp = a[i]; a[i] = a[j]; a[j] = tmp; } return a.join(""); } function initLevel() { items.bar.level = currentLevel + 1; if (currentSubLevel == 0 && !incorrectFlag) { var level = levels[currentLevel]; words = Lang.getLessonWords(dataset, level); Core.shuffle(words); var limit = Math.min(11, words.length) words = words.slice(0, limit) frequency = calculateFrequency(); var tempQuestions = generateQuestions(); maxSubLevel = tempQuestions.length; items.score.numberOfSubLevels = maxSubLevel; items.score.currentSubLevel = 1; questions = shuffleString(tempQuestions); items.wordsModel.clear(); for (var i = 0; i < words.length; i++) { items.wordsModel.append({ "spelling": words[i].translatedTxt, "imgurl": words[i].image, - "selected": false + "selected": false, + "voice": words[i].voice }); } } else { items.score.currentSubLevel = currentSubLevel + 1; } incorrectFlag = false; for(var i = 0; i < words.length; i++) { items.wordsModel.setProperty(i, "selected", false); } currentLetter = questions[currentSubLevel]; items.question = currentLetter items.animateX.restart(); if (GCompris.ApplicationSettings.isAudioVoicesEnabled && GCompris.DownloadManager.haveLocalResource( GCompris.DownloadManager.getVoicesResourceForLocale(locale))) { items.audioVoices.silence(100) playLetter(currentLetter) } } function calculateFrequency() { var freq = []; //regex pattern to detect whether the character is an english alphabet or some accented latin chacarcter var pattern = /[A-Za-z\u00C0-\u017F]/; for(var i = 0; i < words.length; i++) { var currentWord = words[i].translatedTxt; for(var j = 0; j < currentWord.length; j++) { var character = currentWord.charAt(j); //consider the character if and only if it is an alphabet or an accented latin character if(pattern.test(character)) { if(freq[character]) { freq[character]++; } else { freq[character] = 1; } } } } return freq; } function generateQuestions() { var freqArr = []; var ques = []; for(var character in frequency) { freqArr.push([character, frequency[character]]); } freqArr.sort(function(a, b) {return b[1] - a[1]}); var limit = Math.min(10, freqArr.length); for(var i = 0; i < limit; i++) { ques.push(freqArr[i][0]) } return ques; } function playLetter(letter) { items.audioVoices.append(GCompris.ApplicationInfo.getAudioFilePath("voices-$CA/"+locale+"/alphabet/" + Core.getSoundFilenamForChar(letter))) } function nextLevel() { items.audioVoices.clearQueue() if(maxLevel <= ++currentLevel) { currentLevel = 0 } currentSubLevel = 0; initLevel(); } function previousLevel() { items.audioVoices.clearQueue() if(--currentLevel < 0) { currentLevel = maxLevel - 1 } currentSubLevel = 0; initLevel(); } function nextSubLevel() { if( ++currentSubLevel >= maxSubLevel) { currentSubLevel = 0 nextLevel() } initLevel(); } function checkAnswer() { var hasWordNotFound = false; var modelEntry; for(var i = 0; i < words.length; i++) { modelEntry = items.wordsModel.get(i); if(modelEntry.spelling.indexOf(currentLetter) != -1 && modelEntry.selected == false) { hasWordNotFound = true; break; } } if(hasWordNotFound == false) { items.bonus.good("flower"); } } function checkWord(index) { var modelEntry = items.wordsModel.get(index); if(modelEntry.spelling.indexOf(currentLetter) != -1) { items.wordsModel.setProperty(index, "selected", true); checkAnswer(); return true; } else { items.bonus.bad("flower"); return false; } } function incorrectSelection() { incorrectFlag = true; var quesLen = questions.length; questions = questions.slice(0, currentSubLevel) + questions.slice(currentSubLevel+1, quesLen) + questions.charAt(currentSubLevel); currentSubLevel--; nextSubLevel(); }