Web · Wiki · Activities · Blog · Lists · Chat · Meeting · Bugs · Git · Translate · Archive · People · Donate
1
from gettext import gettext as _
2
3
import sys
4
import gtk
5
import pygame
6
7
import sugar.activity.activity
8
import sugar.graphics.toolbutton
9
10
sys.path.append('..') # Import sugargame package from top directory.
11
import sugargame.canvas
12
13
import TestGame
14
15
class TestActivity(sugar.activity.activity.Activity):
16
    def __init__(self, handle):
17
        super(TestActivity, self).__init__(handle)
18
        
19
        self.paused = False
20
21
        # Create the game instance.
22
        self.game = TestGame.TestGame()
23
24
        # Build the activity toolbar.
25
        self.build_toolbar()
26
27
        # Build the Pygame canvas.
28
        self._pygamecanvas = sugargame.canvas.PygameCanvas(self)
29
30
        # Note that set_canvas implicitly calls read_file when resuming from the Journal.
31
        self.set_canvas(self._pygamecanvas)
32
        
33
        # Start the game running (self.game.run is called when the activity constructor returns).
34
        self._pygamecanvas.run_pygame(self.game.run)
35
        
36
    def build_toolbar(self):        
37
        stop_play = sugar.graphics.toolbutton.ToolButton('media-playback-stop')
38
        stop_play.set_tooltip(_("Stop"))
39
        stop_play.set_accelerator(_('<ctrl>space'))
40
        stop_play.connect('clicked', self._stop_play_cb)
41
42
        toolbar = gtk.Toolbar()
43
        toolbar.insert(stop_play, 0)
44
        
45
        toolbox = sugar.activity.activity.ActivityToolbox(self)
46
        toolbox.add_toolbar(_("Pygame"), toolbar)
47
        
48
        toolbox.show_all()
49
        self.set_toolbox(toolbox)
50
51
    def _stop_play_cb(self, button):
52
        # Pause or unpause the game.
53
        self.paused = not self.paused
54
        self.game.set_paused(self.paused)
55
        
56
        # Update the button to show the next action.
57
        if self.paused:
58
            button.set_icon('media-playback-start')
59
            button.set_tooltip(_("Start"))
60
        else:
61
            button.set_icon('media-playback-stop')
62
            button.set_tooltip(_("Stop"))
63
64
    def read_file(self, file_path):
65
        self.game.read_file(file_path)
66
        
67
    def write_file(self, file_path):
68
        self.game.write_file(file_path)