3
# Time-stamp: <2007-08-22 01:26:07 bruno>
5
# Copyright (C) 2003 Jose Jorge
7
# This program is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
17
# You should have received a copy of the GNU General Public License
18
# along with this program; if not, see <http://www.gnu.org/licenses/>.
31
from gcompris import gcompris_gettext as _
33
class Gcompris_melody:
34
"""The melody activity"""
37
def __init__(self, gcomprisBoard):
38
self.gcomprisBoard = gcomprisBoard
40
self.gcomprisBoard.disable_im_context = True
42
#print("Gcompris_melody __init__.")
44
# These are used to let us restart only after the bonux is displayed.
45
# When the bonus is displayed, it call us first with pause(1) and then with pause(0)
46
self.board_paused = 0;
51
self.saved_policy = gcompris.sound.policy_get()
52
gcompris.sound.policy_set(gcompris.sound.PLAY_AND_INTERRUPT)
54
self.gcomprisBoard.level=1
55
self.gcomprisBoard.sublevel=1
56
self.gcomprisBoard.number_of_sublevel=1
60
gcompris.sound.pause()
69
# This list contains the 'theme' for each melody level.
75
{'theme': "xylofon", 'background': "melody/xylofon/background.png", 'hittool': "melody/xylofon/cursor.png", 'hitofset_x': 50, 'hitofset_y': 50},
76
[ {'x': 150.0, 'y': 101.0, 'image': "melody/xylofon/son1.png", 'sound': "sounds/melody/xylofon/son1.wav"},
77
{'x': 284.0, 'y': 118.0, 'image': "melody/xylofon/son2.png", 'sound': "sounds/melody/xylofon/son2.wav"},
78
{'x': 412.0, 'y': 140.0, 'image': "melody/xylofon/son3.png", 'sound': "sounds/melody/xylofon/son3.wav"},
79
{'x': 546.0, 'y': 157.0, 'image': "melody/xylofon/son4.png", 'sound': "sounds/melody/xylofon/son4.wav"} ] ],
83
{'theme': "guitar", 'background': "melody/guitar/background.jpg", 'hittool': "melody/guitar/cursor.png", 'hitofset_x': 400, 'hitofset_y': -5},
84
[ {'x': 0, 'y': 170.0, 'image': "melody/guitar/son1.png", 'sound': "sounds/melody/guitar/son1.wav"},
85
{'x': 0, 'y': 230.0, 'image': "melody/guitar/son2.png", 'sound': "sounds/melody/guitar/son2.wav"},
86
{'x': 0, 'y': 290.0, 'image': "melody/guitar/son3.png", 'sound': "sounds/melody/guitar/son3.wav"},
87
{'x': 0, 'y': 350.0, 'image': "melody/guitar/son4.png", 'sound': "sounds/melody/guitar/son4.wav"} ] ],
91
{'theme': "tachos", 'background': "melody/tachos/background.jpg", 'hittool': "melody/tachos/cursor.png", 'hitofset_x': 50, 'hitofset_y': 50},
92
[ {'x': 150.0, 'y': 50.0, 'image': "melody/tachos/son1.png", 'sound': "sounds/melody/tachos/son1.wav"},
93
{'x': 550.0, 'y': 50.0, 'image': "melody/tachos/son2.png", 'sound': "sounds/melody/tachos/son2.wav"},
94
{'x': 150.0, 'y': 250.0, 'image': "melody/tachos/son3.png", 'sound': "sounds/melody/tachos/son3.wav"},
95
{'x': 550.0, 'y': 250.0, 'image': "melody/tachos/son4.png", 'sound': "sounds/melody/tachos/son4.wav"} ] ] ]
97
self.maxtheme = len(self.melodylist)-1
98
self.gcomprisBoard.maxlevel = 9
101
pixmap = gcompris.utils.load_pixmap(gcompris.skin.image_to_skin("button_reload.png"))
103
gcompris.bar_set_repeat_icon(pixmap)
104
gcompris.bar_set(gcompris.BAR_LEVEL|gcompris.BAR_REPEAT_ICON)
106
gcompris.bar_set(gcompris.BAR_LEVEL|gcompris.BAR_REPEAT)
110
self.display_current_level()
112
# Play an intro sound
113
gcompris.sound.play_ogg_cb("sounds/melody/" + self.melodylist[self.theme][0]['theme'] + "/melody.wav",
116
Prop = gcompris.get_properties()
119
gcompris.utils.dialog(_("Error: this activity cannot be played with the\nsound effects disabled.\nGo to the configuration dialogue to\nenable the sound"), stop_board)
123
#print("Gcompris_melody end.")
124
gcompris.sound.policy_set(self.saved_policy)
125
gcompris.sound.resume()
129
#print("Gcompris_melody ok.")
135
for i in self.timers :
136
gobject.source_remove(i)
142
# Remove the root item removes all the others inside it
143
self.rootitem.destroy()
146
def display_current_level(self):
148
gcompris.set_background(self.gcomprisBoard.canvas.root(),
149
self.melodylist[self.theme][0]['background'])
150
gcompris.bar_set_level(self.gcomprisBoard)
152
gcompris.sound.policy_set(gcompris.sound.PLAY_AND_INTERRUPT)
154
# Create our rootitem. We put each canvas item in it so at the end we
155
# only have to kill it. The canvas deletes all the items it contains automaticaly.
156
self.rootitem = self.gcomprisBoard.canvas.root().add(
157
gnomecanvas.CanvasGroup,
162
# Put the theme switcher button
163
self.switch_item = self.rootitem.add(
164
gnomecanvas.CanvasPixbuf,
165
pixbuf = gcompris.utils.load_pixmap("melody/switch.png"),
169
self.switch_item.connect("event", self.switch_item_event)
170
# This item is clickeable and it must be seen
171
self.switch_item.connect("event", gcompris.utils.item_event_focus)
174
# Put the sound buttons
175
self.sound_list = self.melodylist[self.theme][1]
177
for i in self.sound_list:
178
self.sound_item = self.rootitem.add(
179
gnomecanvas.CanvasPixbuf,
180
pixbuf = gcompris.utils.load_pixmap(i['image']),
184
self.sound_item.connect("event", self.sound_item_event, i)
185
# This item is clickeable and it must be seen
186
self.sound_item.connect("event", gcompris.utils.item_event_focus)
189
self.bang_item = self.rootitem.add(
190
gnomecanvas.CanvasPixbuf,
191
pixbuf = gcompris.utils.load_pixmap(self.melodylist[self.theme][0]['hittool']),
195
self.bang_item.hide()
197
self.hitofset_x = self.melodylist[self.theme][0]['hitofset_x']
198
self.hitofset_y = self.melodylist[self.theme][0]['hitofset_y']
200
self.populate(self.sound_list)
203
# records the try of the child
205
if len(self.kidstry) >= len(self.solution) :
207
self.kidstry.append(a)
209
if self.kidstry == self.solution :
210
if (self.increment_level() == 1):
212
gcompris.sound.policy_set(self.saved_policy)
213
gcompris.bonus.display(1, gcompris.bonus.FLOWER)
216
# Ready to play for the kid
223
# Shows and plays the thing clicked
224
def show_bang_stop(self, a):
226
if self.board_paused or self.rootitem == None:
229
self.bang_item.hide()
232
# Shows and plays the thing clicked
233
def show_bang(self, a):
235
if self.board_paused or self.rootitem == None:
238
self.bang_item.set(x=a['x'] + self.hitofset_x, y=a['y'] + self.hitofset_y)
240
self.bang_item.show()
242
gcompris.sound.play_ogg_cb(a['sound'], self.sound_played)
247
#print("Gcompris_melody repeat.")
248
# Important to use a timer here to keep self.timers up todate
249
self.timers.append(gobject.timeout_add(50, self.repeat_it))
252
#print("Gcompris_melody repeat it.")
256
# We are like paused until the last sound is played
265
for i in self.solution:
266
self.timers.append(gobject.timeout_add(timer, self.show_bang, i))
268
self.timers.append(gobject.timeout_add(timer, self.show_bang_stop, i))
271
self.timers.append(gobject.timeout_add(timer, self.ready))
275
#print("Gcompris_melody config.")
278
#randomize the sequence and plays it one first time
279
def populate(self, sound_struct):
280
#print("Gcompris_melody populate.")
283
for i in range(self.gcomprisBoard.level+2):
284
self.solution.append(sound_struct[random.randint(0,len(sound_struct)-1)])
286
self.timers.append(gobject.timeout_add(1300, self.repeat_it))
288
def key_press(self, keyval, commit_str, preedit_str):
289
#print("got key %i" % keyval)
290
# Play sounds with the keys
291
if ((keyval == gtk.keysyms.KP_1) or (keyval == gtk.keysyms._1)):
293
self.sound_play(self.melodylist[self.theme][1][0])
295
if ((keyval == gtk.keysyms.KP_2) or (keyval == gtk.keysyms._2)):
297
self.sound_play(self.melodylist[self.theme][1][1])
299
if ((keyval == gtk.keysyms.KP_3) or (keyval == gtk.keysyms._3)):
301
self.sound_play(self.melodylist[self.theme][1][2])
303
if ((keyval == gtk.keysyms.KP_4) or (keyval == gtk.keysyms._4)):
305
self.sound_play(self.melodylist[self.theme][1][3])
311
def pause(self, pause):
312
self.board_paused = pause
314
# When the bonus is displayed, it call us first with pause(1) and then with pause(0)
316
if(self.gamewon == 1 and pause == 0):
318
self.display_current_level()
324
def set_level(self, level):
325
#print("Gcompris_melody set level. %i" % level)
326
self.gcomprisBoard.level=level;
327
self.gcomprisBoard.sublevel=1;
329
self.display_current_level()
331
# Code that increments the sublevel and level
332
# And bail out if no more levels are available
333
# return 1 if continue, 0 if bail out
334
def increment_level(self):
335
self.gcomprisBoard.sublevel += 1
337
if(self.gcomprisBoard.sublevel>self.gcomprisBoard.number_of_sublevel):
339
self.gcomprisBoard.sublevel=1
340
self.gcomprisBoard.level += 1
341
if(self.gcomprisBoard.level>self.gcomprisBoard.maxlevel):
342
# the current board is finished : bail out
343
gcompris.bonus.board_finished(gcompris.bonus.FINISHED_RANDOM)
349
# ---------------- sound on click events -----------------------
350
def sound_item_event(self, widget, event, sound_struct):
352
if self.board_paused or self.in_repeat:
355
if event.type == gtk.gdk.BUTTON_PRESS:
356
if event.button == 1:
357
self.sound_play(sound_struct)
360
# ---------------- sound is effectively played -----------------------
361
def sound_play(self, sound_struct):
362
gcompris.sound.play_ogg_cb(sound_struct['sound'], self.sound_played)
363
self.tried(sound_struct)
366
# ---------------- theme change on switch events -----------------------
367
def switch_item_event(self, widget, event):
369
if self.board_paused or self.in_repeat:
372
if event.type == gtk.gdk.BUTTON_PRESS:
373
if event.button == 1:
374
if self.theme < self.maxtheme:
379
#print("New melody theme : " + self.melodylist[self.theme][0]['theme'] + ".")
382
self.display_current_level()
387
def sound_played(self, file):
388
#print "python sound played :", file
391
def intro_cb(self, file):
392
#print "intro passed. go play"
394
self.populate(self.sound_list)
397
gcompris.bonus.board_finished(gcompris.bonus.FINISHED_RANDOM)