~ml-launchpad/ubuntu/natty/gcompris/fix-for-777349

« back to all changes in this revision

Viewing changes to src/boards/python/melody.py

  • Committer: Bazaar Package Importer
  • Author(s): Marc Gariepy, Marc Gariepy, Stephane Graber
  • Date: 2010-01-04 17:42:49 UTC
  • mfrom: (1.1.14 upstream)
  • Revision ID: james.westby@ubuntu.com-20100104174249-7bupatd9dtxyhvs4
Tags: 9.0-0ubuntu1
[Marc Gariepy]
* New upstream release (9.0).
* Remove cache.c from POTFILES to avoid FTBFS
* Remove unneeded rm in debian/rules (file no longer exists upstream)

[Stephane Graber]
* Bump Debian standards to 3.8.3
* Add patch system (dpatch)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#  gcompris - melody
2
 
#
3
 
# Time-stamp: <2007-08-22 01:26:07 bruno>
4
 
#
5
 
# Copyright (C) 2003 Jose Jorge
6
 
#
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.
11
 
#
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.
16
 
#
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/>.
19
 
#
20
 
import gobject
21
 
import gnomecanvas
22
 
import gcompris
23
 
import gcompris.utils
24
 
import gcompris.skin
25
 
import gcompris.sound
26
 
import gcompris.bonus
27
 
import gtk
28
 
import gtk.gdk
29
 
import random
30
 
 
31
 
from gcompris import gcompris_gettext as _
32
 
 
33
 
class Gcompris_melody:
34
 
  """The melody activity"""
35
 
 
36
 
 
37
 
  def __init__(self, gcomprisBoard):
38
 
    self.gcomprisBoard = gcomprisBoard
39
 
 
40
 
    self.gcomprisBoard.disable_im_context = True
41
 
 
42
 
    #print("Gcompris_melody __init__.")
43
 
 
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;
47
 
    self.gamewon       = 0;
48
 
 
49
 
 
50
 
  def start(self):
51
 
    self.saved_policy = gcompris.sound.policy_get()
52
 
    gcompris.sound.policy_set(gcompris.sound.PLAY_AND_INTERRUPT)
53
 
 
54
 
    self.gcomprisBoard.level=1
55
 
    self.gcomprisBoard.sublevel=1
56
 
    self.gcomprisBoard.number_of_sublevel=1
57
 
 
58
 
    # pause the bg music
59
 
    #print "pause sound"
60
 
    gcompris.sound.pause()
61
 
 
62
 
    self.timers = []
63
 
    self.solution = []
64
 
    self.kidstry = []
65
 
    self.in_repeat = 0
66
 
    self.theme = 0
67
 
 
68
 
    #
69
 
    # This list contains the 'theme' for each melody level.
70
 
    #
71
 
    self.melodylist = \
72
 
                      [
73
 
      # xylophon
74
 
      [
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"} ] ],
80
 
 
81
 
      # guitar
82
 
      [
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"} ] ],
88
 
 
89
 
      # Kitchen
90
 
      [
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"} ] ] ]
96
 
 
97
 
    self.maxtheme = len(self.melodylist)-1
98
 
    self.gcomprisBoard.maxlevel = 9
99
 
 
100
 
    #
101
 
    pixmap = gcompris.utils.load_pixmap(gcompris.skin.image_to_skin("button_reload.png"))
102
 
    if(pixmap):
103
 
      gcompris.bar_set_repeat_icon(pixmap)
104
 
      gcompris.bar_set(gcompris.BAR_LEVEL|gcompris.BAR_REPEAT_ICON)
105
 
    else:
106
 
      gcompris.bar_set(gcompris.BAR_LEVEL|gcompris.BAR_REPEAT)
107
 
 
108
 
 
109
 
    self.pause(1);
110
 
    self.display_current_level()
111
 
 
112
 
    # Play an intro sound
113
 
    gcompris.sound.play_ogg_cb("sounds/melody/" + self.melodylist[self.theme][0]['theme'] + "/melody.wav",
114
 
                               self.intro_cb)
115
 
 
116
 
    Prop = gcompris.get_properties()
117
 
 
118
 
    if(not Prop.fx):
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)
120
 
 
121
 
  def end(self):
122
 
    self.cleanup()
123
 
    #print("Gcompris_melody end.")
124
 
    gcompris.sound.policy_set(self.saved_policy)
125
 
    gcompris.sound.resume()
126
 
 
127
 
 
128
 
  def ok(self):
129
 
    #print("Gcompris_melody ok.")
130
 
    pass
131
 
 
132
 
  def cleanup(self):
133
 
 
134
 
    # Clear all timer
135
 
    for i in self.timers :
136
 
      gobject.source_remove(i)
137
 
 
138
 
    self.timers = []
139
 
 
140
 
    self.in_repeat = 0;
141
 
 
142
 
    # Remove the root item removes all the others inside it
143
 
    self.rootitem.destroy()
144
 
    self.rootitem = None
145
 
 
146
 
  def display_current_level(self):
147
 
 
148
 
    gcompris.set_background(self.gcomprisBoard.canvas.root(),
149
 
                            self.melodylist[self.theme][0]['background'])
150
 
    gcompris.bar_set_level(self.gcomprisBoard)
151
 
 
152
 
    gcompris.sound.policy_set(gcompris.sound.PLAY_AND_INTERRUPT)
153
 
 
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,
158
 
      x=0.0,
159
 
      y=0.0
160
 
      )
161
 
 
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"),
166
 
        x=10,
167
 
        y=10
168
 
        )
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)
172
 
 
173
 
 
174
 
    # Put the sound buttons
175
 
    self.sound_list = self.melodylist[self.theme][1]
176
 
 
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']),
181
 
        x=i['x'],
182
 
        y=i['y']
183
 
        )
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)
187
 
 
188
 
 
189
 
    self.bang_item = self.rootitem.add(
190
 
      gnomecanvas.CanvasPixbuf,
191
 
      pixbuf = gcompris.utils.load_pixmap(self.melodylist[self.theme][0]['hittool']),
192
 
      x=0,
193
 
      y=0
194
 
      )
195
 
    self.bang_item.hide()
196
 
 
197
 
    self.hitofset_x = self.melodylist[self.theme][0]['hitofset_x']
198
 
    self.hitofset_y = self.melodylist[self.theme][0]['hitofset_y']
199
 
 
200
 
    self.populate(self.sound_list)
201
 
 
202
 
 
203
 
  # records the try of the child
204
 
  def tried(self, a):
205
 
    if len(self.kidstry) >= len(self.solution) :
206
 
      self.kidstry.pop(0)
207
 
    self.kidstry.append(a)
208
 
    #level finished?
209
 
    if self.kidstry == self.solution :
210
 
      if (self.increment_level() == 1):
211
 
        self.gamewon = 1
212
 
        gcompris.sound.policy_set(self.saved_policy)
213
 
        gcompris.bonus.display(1, gcompris.bonus.FLOWER)
214
 
 
215
 
 
216
 
  # Ready to play for the kid
217
 
  def ready(self):
218
 
 
219
 
    self.in_repeat = 0
220
 
    self.timers.pop(0)
221
 
 
222
 
 
223
 
  # Shows and plays the thing clicked
224
 
  def show_bang_stop(self, a):
225
 
 
226
 
    if self.board_paused or self.rootitem == None:
227
 
      return
228
 
 
229
 
    self.bang_item.hide()
230
 
    self.timers.pop(0)
231
 
 
232
 
  # Shows and plays the thing clicked
233
 
  def show_bang(self, a):
234
 
 
235
 
    if self.board_paused or self.rootitem == None:
236
 
      return
237
 
 
238
 
    self.bang_item.set(x=a['x'] + self.hitofset_x, y=a['y'] + self.hitofset_y)
239
 
 
240
 
    self.bang_item.show()
241
 
 
242
 
    gcompris.sound.play_ogg_cb(a['sound'], self.sound_played)
243
 
    self.timers.pop(0)
244
 
 
245
 
 
246
 
  def repeat(self):
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))
250
 
 
251
 
  def repeat_it(self):
252
 
    #print("Gcompris_melody repeat it.")
253
 
    if self.in_repeat:
254
 
      return
255
 
 
256
 
    # We are like paused until the last sound is played
257
 
    self.in_repeat = 1;
258
 
 
259
 
    if self.timers :
260
 
      self.timers.pop(0)
261
 
 
262
 
    self.kidstry = []
263
 
    timer = 0
264
 
 
265
 
    for i in self.solution:
266
 
      self.timers.append(gobject.timeout_add(timer, self.show_bang, i))
267
 
      timer = timer + 1000
268
 
      self.timers.append(gobject.timeout_add(timer, self.show_bang_stop, i))
269
 
      timer = timer + 500
270
 
 
271
 
    self.timers.append(gobject.timeout_add(timer, self.ready))
272
 
 
273
 
 
274
 
  def config(self):
275
 
    #print("Gcompris_melody config.")
276
 
    pass
277
 
 
278
 
  #randomize the sequence and plays it one first time
279
 
  def populate(self, sound_struct):
280
 
    #print("Gcompris_melody populate.")
281
 
    self.solution = []
282
 
 
283
 
    for i in range(self.gcomprisBoard.level+2):
284
 
      self.solution.append(sound_struct[random.randint(0,len(sound_struct)-1)])
285
 
 
286
 
    self.timers.append(gobject.timeout_add(1300, self.repeat_it))
287
 
 
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)):
292
 
      #print "son1"
293
 
      self.sound_play(self.melodylist[self.theme][1][0])
294
 
      return True
295
 
    if ((keyval == gtk.keysyms.KP_2) or (keyval == gtk.keysyms._2)):
296
 
      #print "son2"
297
 
      self.sound_play(self.melodylist[self.theme][1][1])
298
 
      return True
299
 
    if ((keyval == gtk.keysyms.KP_3) or (keyval == gtk.keysyms._3)):
300
 
      #print "son3"
301
 
      self.sound_play(self.melodylist[self.theme][1][2])
302
 
      return True
303
 
    if ((keyval == gtk.keysyms.KP_4) or (keyval == gtk.keysyms._4)):
304
 
      #print "son4"
305
 
      self.sound_play(self.melodylist[self.theme][1][3])
306
 
      return True
307
 
 
308
 
    return False
309
 
 
310
 
 
311
 
  def pause(self, pause):
312
 
    self.board_paused = pause
313
 
 
314
 
    # When the bonus is displayed, it call us first with pause(1) and then with pause(0)
315
 
    # the game is won
316
 
    if(self.gamewon == 1 and pause == 0):
317
 
      self.cleanup()
318
 
      self.display_current_level()
319
 
      self.gamewon = 0
320
 
 
321
 
    return
322
 
 
323
 
 
324
 
  def set_level(self, level):
325
 
    #print("Gcompris_melody set level. %i" % level)
326
 
    self.gcomprisBoard.level=level;
327
 
    self.gcomprisBoard.sublevel=1;
328
 
    self.cleanup()
329
 
    self.display_current_level()
330
 
 
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
336
 
 
337
 
    if(self.gcomprisBoard.sublevel>self.gcomprisBoard.number_of_sublevel):
338
 
      # Try the next level
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)
344
 
        return 0
345
 
 
346
 
    return 1
347
 
 
348
 
 
349
 
  # ---------------- sound on click events -----------------------
350
 
  def sound_item_event(self, widget, event, sound_struct):
351
 
 
352
 
    if self.board_paused or self.in_repeat:
353
 
      return
354
 
 
355
 
    if event.type == gtk.gdk.BUTTON_PRESS:
356
 
      if event.button == 1:
357
 
          self.sound_play(sound_struct)
358
 
    return False
359
 
 
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)
364
 
    return
365
 
 
366
 
  # ---------------- theme change on switch events -----------------------
367
 
  def switch_item_event(self, widget, event):
368
 
 
369
 
    if self.board_paused or self.in_repeat:
370
 
      return
371
 
    # switch the theme
372
 
    if event.type == gtk.gdk.BUTTON_PRESS:
373
 
      if event.button == 1:
374
 
        if self.theme < self.maxtheme:
375
 
          self.theme += 1
376
 
        else:
377
 
          self.theme = 0
378
 
 
379
 
        #print("New melody theme : " + self.melodylist[self.theme][0]['theme'] + ".")
380
 
    # Apply the changes
381
 
        self.cleanup()
382
 
        self.display_current_level()
383
 
 
384
 
 
385
 
    return False
386
 
 
387
 
  def sound_played(self, file):
388
 
    #print "python sound played :", file
389
 
    pass
390
 
 
391
 
  def intro_cb(self, file):
392
 
    #print "intro passed. go play"
393
 
    self.pause(0)
394
 
    self.populate(self.sound_list)
395
 
 
396
 
def stop_board():
397
 
  gcompris.bonus.board_finished(gcompris.bonus.FINISHED_RANDOM)
398