Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ColorPaint.py
blob: 5fa2dbc065cba2c2b9666f179f7971efa8395eb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import pygame, sys, os
from pygame.locals import *
from eduGames import *
from Paint import *

class ColorPaint(Game):   
    (INITIAL, WAITING, BAD_CHOICE, GOOD_CHOICE, WAITING_PAINT, PAINTING, END) = (0,1,2,3,4,5, 6)
    
    def __init__(self, screen, uiMgr, sndMgr, screenMgr, path):
        Game.__init__(self, screen, uiMgr, sndMgr, screenMgr, path)      
        self.finished = False     
        self.puzzleElements = None        
        self.paintBoxes = None
        self.currentElementIndex = 0
        self.state = ColorPaint.INITIAL
        self.puzzleElementControls = None
        self.selectedColor = ""
        self.soundIcon = None
        self.soundsDir = "" #set later by the Games class
        self.xMargin = 0
        self.yMargin = 0
        self.masterResourcesDir = None
        self.brush = None
        self.fakeMouse = None
        self.isHelp = False
        self.onNavigationPortion = False        
        self.xStartBrushZone = 0
        self.yStartBrushZone = 0
        self.xEndBrushZone = 1030 + self.xMargin
        self.yEndBrushZone = 0       
        
        
    def getCurrentPuzzleElement(self):
        return self.puzzleElements[self.currentElementIndex]
    
    def getCurrentPuzzleElementControl(self):
        return self.puzzleElementControls[self.currentElementIndex]
        
    def pause(self):
        self.getUiMgr().removeControl(self.brush)
        pygame.mouse.set_visible(True)
        
    def unPause(self):
        self.getUiMgr().addControl(self.brush)
        pygame.mouse.set_visible(False)
                        
    def initializeGameData(self):
        self.brush = Brush(self, "")
        self.alert = os.path.join(self.soundsDir, "chord.ogg")       
        self.ding = os.path.join(self.soundsDir, "ding.ogg")
        (self.puzzleElements, self.paintBoxes, backgroundPath, backgroundX, backgroundY) = self.readInfo()
        self.showElements()
        self.showPaintBoxes() 
        fakeMouse = [c for c in self.getUiMgr().controls if c.type == "fakeMouse"]
        self.fakeMouse = None
        if not len(fakeMouse) == 0:
            self.fakeMouse = fakeMouse[0]                       
            self.brush.trackControl(self.fakeMouse)         
            self.fakeMouse.setLayer(20)   
        self.getUiMgr().addControl(self.brush)
        if not self.isHelp:              
            pygame.mouse.set_visible(False) 
        
    def showElements(self):
        x = self.settings["xFirstElement"] + self.xMargin        
        y = self.settings["yFirstElement"] + self.yMargin
                
        distanceBetweenElements = -1
        if self.settings.has_key("distanceBetweenElements"):
            distanceBetweenElements = self.settings["distanceBetweenElements"]
        self.puzzleElementControls = []
        counter = 1

        for element in self.puzzleElements:
            if counter > self.settings["elementsPerRow"] and counter % self.settings["elementsPerRow"] == 1:
                xLastRow = self.settings["xFirstElementLastRow"] + self.xMargin
                x = xLastRow
                y = y + self.settings["spaceBetweenRows"]

            ic = GrowsAndShrinksAnimatedControl(self, x, y, os.path.join(self.resourcesDir, element.image), 
                              "", element.getNumberOfStates())     
            ic.setLayer(6)
            ic.type = "drawing"
            if distanceBetweenElements > -1:
                x += distanceBetweenElements
            else:
                x += ic.getWidth()
            self.getUiMgr().addControl(ic)
            self.puzzleElementControls.append(ic)
            counter += 1        
        
    def showPaintBoxes(self):
        x = self.settings["paintBoxesX"] + self.xMargin
        y = self.settings["paintBoxesY"] + self.yMargin
        watercolor = ImageControl(self, x, y, os.path.join(self.masterResourcesDir, "_acuarelas.png"))      
        watercolor.setLayer(5)           
        self.soundIcon = ImageControl(self, self.settings["soundIconX"] + self.xMargin, self.settings["soundIconY"] + self.yMargin, os.path.join(self.masterResourcesDir, "sound.png"), "", 2)                        
        self.soundIcon.setLayer(5)
        self.soundIcon.type = "soundIcon"
        self.getUiMgr().addControl(watercolor)
        self.getUiMgr().addControl(self.soundIcon)
        x = self.settings["paintBoxesX"] + self.xMargin + 75
        y = self.settings["paintBoxesY"] + self.yMargin + 25

        for element in self.paintBoxes:            
            ic = Control(self, x, y, 50, 50)                             
            ic.color = element
            ic.setLayer(4)
            ic.type = "paint"
            x += 7 + ic.getWidth()
            self.getUiMgr().addControl(ic)
            
        self.yEndBrushZone = watercolor.getY() + watercolor.getHeight()
                                
    def readInfo(self):        
        fileName = os.path.join(self.path, self.settings["infoFile"])        
        file = open(fileName, "r")
        fileText = file.read()
        reader = ScreensReader()
        file.close()        
        return reader.read(fileText)
    
    def on_mouse_hover(self, clickedControl):
        if clickedControl == self.soundIcon:            
            self.soundIcon.setImageDivisionIndex(1)
            self.onNavigationPortion = True
        else:            
            if self.soundIcon.getImageDivisionIndex() == 1: #might improve performance.
                self.soundIcon.setImageDivisionIndex(0)
            mousePos = pygame.mouse.get_pos()
            mousePosX = mousePos[0]
            mousePosY = mousePos[1]      
            
            if mousePosX < self.xStartBrushZone or mousePosY < self.yStartBrushZone or mousePosX > self.xEndBrushZone or mousePosY > self.yEndBrushZone:
                self.onNavigationPortion = True
            else:
                self.onNavigationPortion = False
        try:
            if self.onNavigationPortion:                
                if self.brush.isVisible():
                    self.brush.makeInvisible()
            else:
                if not self.brush.isVisible():
                    self.brush.makeVisible()
        except:
            pass           
                 
    def on_mouse_button_down(self, clickedControl):
        if self.state == Paint.WAITING:
            if clickedControl.type == "paint":            
                self.selectedColor = clickedControl.color
            elif clickedControl == self.soundIcon:
                self.state = Paint.INITIAL
            elif clickedControl == self.puzzleElementControls[self.currentElementIndex]:
                soundPath = os.path.join(self.soundsDir, self.getCurrentPuzzleElement().currentAudio())
                self.getSoundMgr().addSoundForPlayback(soundPath)
        if self.state == Paint.WAITING_PAINT:
            if clickedControl.type == "drawing" and clickedControl is self.getCurrentPuzzleElementControl():
                self.state = Paint.PAINTING    
                                
    def paintCurrentElement(self):   
        self.getCurrentPuzzleElementControl().setImageDivisionIndex(self.getCurrentPuzzleElement().getCurrentIndex()+1)
                                                            
    def playSound(self, sound):
        self.getSoundMgr().addSoundForPlayback(sound)                                                   
    
    def updateState(self):        
        mouseVisible = self.isHelp or self.isPaused or self.onNavigationPortion
        pygame.mouse.set_visible(mouseVisible)

        if self.state == Paint.INITIAL:
            soundPath = os.path.join(self.soundsDir, self.getCurrentPuzzleElement().currentAudio())
            self.getSoundMgr().addSoundForPlayback(soundPath)
            self.state = Paint.WAITING
            currentControl = self.getCurrentPuzzleElementControl()
            currentControl.playAnimation()     
            return
        if self.state == Paint.WAITING: #Actually two states: with and without a selected color.
            if self.selectedColor != "":
                if self.selectedColor == self.getCurrentPuzzleElement().currentColor():
                    self.state = Paint.GOOD_CHOICE
                    self.getUiMgr().removeControl(self.brush)
                    self.brush = Brush(self, self.selectedColor, self.fakeMouse)
                    self.getUiMgr().addControl(self.brush) 
                else:
                    self.state = Paint.BAD_CHOICE
                self.selectedColor = ""
            return
        if self.state == Paint.BAD_CHOICE:
            self.playSound(self.alert)
            self.state = Paint.WAITING            
            return        
        if self.state == Paint.GOOD_CHOICE: 
            self.playSound(self.ding)
            self.state = Paint.WAITING_PAINT
            return
        if self.state == Paint.PAINTING:           
            self.paintCurrentElement()            
            self.getCurrentPuzzleElement().moveOn()          
            self.getUiMgr().removeControl(self.brush)
            self.brush = Brush(self)
            self.brush.trackControl(self.fakeMouse)
            self.getUiMgr().addControl(self.brush)
            self.state = Paint.INITIAL
            if self.getCurrentPuzzleElement().solved():   
                self.getCurrentPuzzleElementControl().stopAnimation()         
                self.currentElementIndex += 1                 
                if self.currentElementIndex >= len(self.puzzleElements):
                    self.state = Paint.END              
            return
        if self.state == Paint.END:
            self.finished = True
            if not self.fakeMouse:
                pygame.mouse.set_visible(True)
            if not self.isHelp:
                self.saveAsDone()
            return
                    
    def endGame(self):        
        self.finished = True
        if not self.fakeMouse:
            pygame.mouse.set_visible(True)