[gcompris] music activities, fixes to let intro music play



commit 2d29b7530c9caff687dfabb9f7c9a2141333138e
Author: Bruno Coudoin <bruno coudoin gcompris net>
Date:   Fri Nov 2 19:54:16 2012 +0100

    music activities, fixes to let intro music play
    
    In the music activities, a sound was played as we enter
    the activity thus cancelling the intro voice.
    
    Reworked the textButton API to create a TextButton class.
    Updated all the activities to use the new API.

 src/explore-activity/explore.py                    |    6 +-
 src/note_names-activity/note_names.py              |   13 +--
 src/piano_composition-activity/gcomprismusic.py    |  103 ++++++++++----------
 .../piano_composition.py                           |    8 +-
 src/play_piano-activity/play_piano.py              |   22 +++-
 src/play_rhythm-activity/play_rhythm.py            |   15 +++-
 6 files changed, 99 insertions(+), 68 deletions(-)
---
diff --git a/src/explore-activity/explore.py b/src/explore-activity/explore.py
index e4d0b45..2c6c9c9 100644
--- a/src/explore-activity/explore.py
+++ b/src/explore-activity/explore.py
@@ -67,6 +67,8 @@ class Gcompris_explore:
         self.progressBar = None
         self.next_action = None
 
+        self.first_run = True
+
     def start(self):
         '''
         method called to create 'home-page', the world map with all the locations.
@@ -98,7 +100,9 @@ dialogue to\nenable the sound."), None)
                                         self.gcomprisBoard.canvas.get_root_item())
 
         # silence any currently playing music
-        gcompris.sound.play_ogg('boards/sounds/silence1s.ogg')
+        if not self.first_run:
+            gcompris.sound.play_ogg('boards/sounds/silence1s.ogg')
+            self.first_run = False
 
         level = self.gcomprisBoard.level
 
diff --git a/src/note_names-activity/note_names.py b/src/note_names-activity/note_names.py
index 7e93ecc..240d796 100644
--- a/src/note_names-activity/note_names.py
+++ b/src/note_names-activity/note_names.py
@@ -67,7 +67,6 @@ class Gcompris_note_names:
         self.saved_policy = gcompris.sound.policy_get()
         gcompris.sound.policy_set(gcompris.sound.PLAY_AND_INTERRUPT)
         gcompris.sound.pause()
-        gcompris.sound.play_ogg('boards/sounds/silence1s.ogg')
         # Set a background image
         gcompris.set_default_background(self.gcomprisBoard.canvas.get_root_item())
 
@@ -141,21 +140,21 @@ They also form the C Major Scale. Notice that the note positions are different t
               alignment=pango.ALIGN_CENTER
               )
 
-            self.playScaleButton = textButton(400, 136, _("Play Scale"),
+            playScaleButton = TextButton(400, 136, _("Play Scale"),
                                               self.rootitem, 0xCC0033FFL)
 
-            self.playScaleButton.connect("button_press_event", self.staff.playComposition)
-            gcompris.utils.item_focus_init(self.playScaleButton, None)
+            playScaleButton.getBackground().connect("button_press_event", 
+                                                    self.staff.playComposition)
 
             if level == 1:
                 text = _("Play Treble Clef Game")
             elif level == 11:
                 text = _("Play Bass Clef Game")
 
-            self.playScaleGameButton = textButton(400, 410, text,
+            playScaleGameButton = TextButton(400, 410, text,
                                                   self.rootitem, 0x00AA33FFL)
-            self.playScaleGameButton.connect("button_press_event", self.play_scale_game)
-            gcompris.utils.item_focus_init(self.playScaleGameButton, None)
+            playScaleGameButton.getBackground().connect("button_press_event",
+                                                        self.play_scale_game)
 
         if level != 1 and level != 11:
             if level in [2, 5, 8, 12, 15, 18]:
diff --git a/src/piano_composition-activity/gcomprismusic.py b/src/piano_composition-activity/gcomprismusic.py
index a790574..acbc761 100644
--- a/src/piano_composition-activity/gcomprismusic.py
+++ b/src/piano_composition-activity/gcomprismusic.py
@@ -1338,57 +1338,60 @@ def pianokeyBindings(keyval, keyboard_click):
 # General UTILITY FUNCTIONS
 #
 # ---------------------------------------------------------------------------
-
-def textButton(x, y, text, rootitem, color_rgba=0x666666AAL,
-               width=100000, includeText=False):
-    '''
-    Add a text button to the screen with the following parameters:
-    1. x: the x position of the button
-    2. y: the y position of the button
-    3. text: the text of the button
-    4. rootitem: the item to draw the button in
-    5. color: the color of button background
-    6. width: the width of the button
-
-    textButton(200, 300, 'Hello World!', self, color_rgba=0x6600FFFFL)
-    '''
-    textbox = goocanvas.Text(
-        parent = rootitem,
-        x=x, y=y,
-        width=width,
-        text=text,
-        fill_color="white", anchor=gtk.ANCHOR_CENTER,
-        alignment=pango.ALIGN_CENTER,
-        pointer_events="GOO_CANVAS_EVENTS_NONE"
-        )
-    TG = 15
-    bounds = textbox.get_bounds()
-
-    goocanvas.Rect(parent = rootitem,
-                   x = bounds.x1 - TG,
-                   y = bounds.y1 - TG,
-                   height = bounds.y2 - bounds.y1 + TG * 2,
-                   width = bounds.x2 - bounds.x1 + TG * 2,
-                   stroke_color = "black",
-                   fill_color_rgba = color_rgba,
-                   radius_x = 3, radius_y = 3,
-                   line_width = 2.0)
-
-    img = goocanvas.Image(
-            parent = rootitem,
-            x = bounds.x1 - TG,
-            y = bounds.y1 - TG,
-            height = bounds.y2 - bounds.y1 + TG * 2,
-            width = bounds.x2 - bounds.x1 + TG * 2,
-            pixbuf = gcompris.utils.load_pixmap('piano_composition/button_front.svg')
+class TextButton:
+
+    def __init__(self, x, y, text, rootitem, color_rgba=0x666666AAL):
+        '''
+        Add a text button to the screen with the following parameters:
+        1. x: the x position of the button
+        2. y: the y position of the button
+        3. text: the text of the button
+        4. rootitem: the item to draw the button in
+        5. color: the color of button background
+
+        TextButton(200, 300, 'Hello World!', self, color_rgba=0x6600FFFFL)
+        '''
+        width = -1
+        self.rootitem = goocanvas.Group(parent=rootitem, x=0, y=0)
+        textbox = goocanvas.Text(
+            parent = self.rootitem,
+            x=x, y=y,
+            width=width,
+            text=text,
+            fill_color="white", anchor=gtk.ANCHOR_CENTER,
+            alignment=pango.ALIGN_CENTER,
+            pointer_events="GOO_CANVAS_EVENTS_NONE"
             )
-
-    gcompris.utils.item_focus_init(img, None)
-    textbox.raise_(img)
-    if includeText:
-        return img, textbox
-    else:
-        return img
+        TG = 15
+        bounds = textbox.get_bounds()
+
+        self.back = goocanvas.Rect(parent = self.rootitem,
+                       x = bounds.x1 - TG,
+                       y = bounds.y1 - TG,
+                       height = bounds.y2 - bounds.y1 + TG * 2,
+                       width = bounds.x2 - bounds.x1 + TG * 2,
+                       stroke_color = "black",
+                       fill_color_rgba = color_rgba,
+                       radius_x = 3, radius_y = 3,
+                       line_width = 2.0)
+
+        self.img = goocanvas.Image(
+                parent = self.rootitem,
+                x = bounds.x1 - TG,
+                y = bounds.y1 - TG,
+                height = bounds.y2 - bounds.y1 + TG * 2,
+                width = bounds.x2 - bounds.x1 + TG * 2,
+                pixbuf = gcompris.utils.load_pixmap('piano_composition/button_front.svg')
+                )
+
+        gcompris.utils.item_focus_init(self.img, None)
+        textbox.raise_(self.img)
+
+    def getBackground(self):
+        return self.img
+
+    def destroy(self):
+        return self.rootitem.remove()
 
 def textBox(text, x, y , rootitem, width=10000,
             fill_color_rgba = None, stroke_color_rgba = None,
diff --git a/src/piano_composition-activity/piano_composition.py b/src/piano_composition-activity/piano_composition.py
index ac93013..f4b10eb 100644
--- a/src/piano_composition-activity/piano_composition.py
+++ b/src/piano_composition-activity/piano_composition.py
@@ -321,10 +321,10 @@ class Gcompris_piano_composition:
             gcompris.utils.item_focus_init(self.makeSharpButton, None)
             self.makeSharpButton.props.visibility = goocanvas.ITEM_INVISIBLE
 
-            self.loadSongsButton = textButton(280, 430, _("Load Music"),
-                                              self.rootitem, 0xE768ABFFL, 100)
-            self.loadSongsButton.connect("button_press_event", self.load_songs_event)
-            gcompris.utils.item_focus_init(self.loadSongsButton, None)
+            loadSongsButton = TextButton(280, 430, _("Load Music"),
+                                              self.rootitem, 0xE768ABFFL)
+            loadSongsButton.getBackground().connect("button_press_event",
+                                                    self.load_songs_event)
 
             textBox(_("Change Accidental Style:"), 100, 430, self.rootitem, width=150, noRect=True)
 
diff --git a/src/play_piano-activity/play_piano.py b/src/play_piano-activity/play_piano.py
index d80d30b..b218752 100644
--- a/src/play_piano-activity/play_piano.py
+++ b/src/play_piano-activity/play_piano.py
@@ -51,12 +51,13 @@ class Gcompris_play_piano:
         # Used to skip double clicks
         self.record_click_time = 0
 
+        self.first_run = True
+
     def start(self):
         self.recordedHits = []
         self.saved_policy = gcompris.sound.policy_get()
         gcompris.sound.policy_set(gcompris.sound.PLAY_AND_INTERRUPT)
         gcompris.sound.pause()
-        gcompris.sound.play_ogg('boards/sounds/silence1s.ogg')
 
         # Set the buttons we want in the bar
         gcompris.bar_set(gcompris.BAR_LEVEL)
@@ -78,6 +79,11 @@ class Gcompris_play_piano:
 played with the\nsound effects disabled.\nGo to the configuration \
 dialogue to\nenable the sound."), None)
 
+        readyButton = TextButton(400, 455, ' ' * 20 + _('I am Ready') + ' ' * 20,
+                                 self.rootitem, 0x11AA11FFL)
+        readyButton.getBackground().connect("button_press_event",
+                                            self.ready_event, readyButton)
+
     def display_level(self, level):
         self.score = 0
         if hasattr(self, 'staff'):
@@ -114,11 +120,11 @@ dialogue to\nenable the sound."), None)
         self.staff.endx = 200
 
         if level not in [6, 12]:
-            self.colorCodeNotesButton = textButton(100, 215, _("Color code notes?"),
+            colorCodeNotesButton = TextButton(100, 215, _("Color code notes?"),
                                                    self.rootitem, 0x990011FFL)
 
-            self.colorCodeNotesButton.connect("button_press_event", self.color_code_notes)
-            gcompris.utils.item_focus_init(self.colorCodeNotesButton, None)
+            colorCodeNotesButton.getBackground().connect("button_press_event",
+                                                        self.color_code_notes)
         else:
             self.staff.colorCodeNotes = False
 
@@ -126,7 +132,8 @@ dialogue to\nenable the sound."), None)
         self.staff.rootitem.scale(2.0, 2.0)
 
         self.givenOption = []
-        self.show_melody()
+        if not self.first_run:
+            self.show_melody()
         self.kidsNoteList = []
         self.piano = PianoKeyboard(250, 305, self.rootitem)
         if level in [4, 5, 6, 12, 11, 10]:
@@ -343,3 +350,8 @@ dialogue to\nenable the sound."), None)
         else:
             self.staff.colorCodeNotes = True
             self.staff.colorCodeAllNotes()
+
+    def ready_event(self, widget, target, event, button):
+        button.destroy()
+        self.show_melody()
+        self.first_run = False
diff --git a/src/play_rhythm-activity/play_rhythm.py b/src/play_rhythm-activity/play_rhythm.py
index 2d609f2..ae15c34 100644
--- a/src/play_rhythm-activity/play_rhythm.py
+++ b/src/play_rhythm-activity/play_rhythm.py
@@ -56,6 +56,8 @@ class Gcompris_play_rhythm:
         self.metronomeTimer = 0
         self.updateTimer = 0
 
+        self.first_run = True 
+
     def start(self):
 
         self.running = True
@@ -82,6 +84,11 @@ class Gcompris_play_rhythm:
 played with the\nsound effects disabled.\nGo to the configuration \
 dialogue to\nenable the sound."), None)
 
+        readyButton = TextButton(400, 455, ' ' * 20 + _('I am Ready') + ' ' * 20,
+                                 self.rootitem, 0x11AA11FFL)
+        readyButton.getBackground().connect("button_press_event",
+                                            self.ready_event, readyButton)
+
     def display_level(self, level):
 
         self.recordedHits = []
@@ -200,7 +207,8 @@ dialogue to\nenable the sound."), None)
 
         self.readyForFirstDrumBeat = True
 
-        self.show_rhythm()
+        if not self.first_run:
+            self.show_rhythm()
         self.updateBoard(1)
 
         self.playButton.connect("button_press_event", self.compositionIsPlaying)
@@ -501,3 +509,8 @@ dialogue to\nenable the sound."), None)
         self.gcomprisBoard.level = level
         gcompris.bar_set_level(self.gcomprisBoard)
         self.display_level(self.gcomprisBoard.level)
+
+    def ready_event(self, widget, target, event, button):
+        button.destroy()
+        self.show_rhythm()
+        self.first_run = False



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]