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

0001 /* GCompris - redraw.js
0002  *
0003  * SPDX-FileCopyrightText: 2014 Bruno Coudoin <bruno.coudoin@gcompris.net>
0004  *
0005  * Authors:
0006  *   Bruno Coudoin <bruno.coudoin@gcompris.net> (GTK+ version)
0007  *   Bruno Coudoin <bruno.coudoin@gcompris.net> (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 url = "qrc:/gcompris/src/activities/redraw/resource/"
0016 var colorShortcut = {
0017     0: 'white',
0018     1: 'red',
0019     2: 'orange',
0020     3: 'green',
0021     4: 'blue',
0022     5: 'yellow',
0023     6: 'black'
0024 }
0025 var colors = {
0026     0: '#33FFFFFF',
0027     1: '#FFCC0000',
0028     2: '#FFFCAE3D',
0029     3: '#FF73D216',
0030     4: '#FF3465A4',
0031     5: '#FFEDD400',
0032     6: '#FF2E3436'
0033 }
0034 
0035 
0036 var dataset
0037 var numberOfLevel
0038 var items
0039 
0040 function start(items_) {
0041     items = items_
0042     dataset = items.levels
0043     numberOfLevel = dataset.length
0044     items.currentLevel = Core.getInitialLevel(numberOfLevel)
0045     initLevel()
0046 }
0047 
0048 function stop() {
0049 }
0050 
0051 function initLevel() {
0052     items.numberOfColumn = dataset[items.currentLevel].columns
0053     items.targetModelData = dataset[items.currentLevel].image
0054     items.numberOfColor = getNumberOfColors(items.targetModelData)
0055     items.colorSelector = 0
0056     items.userModel.reset()
0057     if(items.currentLevel == 0) {
0058         // To help determine the puzzle mirroring type set a color
0059         // at first level
0060         items.userModel.itemAt(0).paint(items.targetModelData[0])
0061     }
0062     items.colorSelector = 1
0063     items.buttonsBlocked = false
0064 }
0065 
0066 function nextLevel() {
0067     items.currentLevel = Core.getNextLevel(items.currentLevel, numberOfLevel);
0068     initLevel();
0069 }
0070 
0071 function previousLevel() {
0072     items.currentLevel = Core.getPreviousLevel(items.currentLevel, numberOfLevel);
0073     initLevel();
0074 }
0075 
0076 function getNumberOfColors(model) {
0077     var nbColor = 0
0078     for(var i=0; i < model.length; ++i) {
0079         nbColor = Math.max(nbColor, model[i])
0080     }
0081     return nbColor + 1
0082 }
0083 
0084 function checkModel() {
0085     for(var i=0; i < items.userModel.count; ++i) {
0086         if(items.userModel.itemAt(i).color !== items.targetModel.itemAt(i).color)
0087             return false
0088     }
0089     items.buttonsBlocked = true
0090     items.checkTimer.stop()
0091     items.bonus.good("flower")
0092 }
0093 
0094 // Dump the user drawing in the format we use for drawing definition
0095 // Can be used to create content
0096 function dump() {
0097     var line = "["
0098     for(var i=0; i < items.userModel.count; ++i) {
0099         if(i % items.numberOfColumn == 0) {
0100             print(line)
0101             line = "   "
0102         }
0103         line += items.userModel.itemAt(i).colorIndex + ","
0104     }
0105     print(line)
0106     print("]")
0107 }