Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Saludame.activity/activity.py
blob: 590e6a0c33010c19d3ed1fbb6834fec8233c1348 (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# -*- coding: utf-8 -*-

from sugar.activity.activity import Activity, ActivityToolbox
from sugar.datastore import datastore
from sugargame.canvas import PygameCanvas

import gc

import gtk
import gobject

import gettext
gettextold = gettext.gettext

def _(string):
    string = gettextold(string)
    if isinstance(string, unicode):
        return string.upper()
    else:
        return unicode(string.decode("utf-8")).upper()
gettext.gettext = _

from gettext import gettext as _

import startup_window
import game
import credits
import content_window
import guides_window
import logging

class SaludameActivity(Activity):
    ''' Clase llamada por sugar cuando se ejecuta la actividad.
        El nombre de esta clase está señalada en el archivo activity/activity.info '''
        
    def __init__(self, handle):
        
        self.game_init = False          # Tells if game engine was initialized
        self.loaded_game = None
        
        Activity.__init__(self, handle)
        
        # Creates the activiy box for toolbars
        self.toolbox = ActivityToolbox(self)
        self.set_toolbox(self.toolbox)
        self.toolbox.show()
        
        # Retrieves the Activity standard toolbar
        self.activity_toolbar = self.toolbox.get_activity_toolbar()

        # Creates other toolbars
        # Game toolbar gets created on demand
        # Library toolbar gets created on demand
        self.guides_toolbar = gtk.Toolbar()
        self.credits_toolbar = gtk.Toolbar()
        
        self.indexes = ["activity"] # right now only activity toolbar

        # Create startup windows
        self.startup_window = startup_window.StartupWindow(self._start_cb, self._load_last_cb)
        
        # Create the canvas to embbed pygame
        self.pygame_canvas = PygameCanvas(self, False)
        
        # Create Health Library Window
        self.health_library = content_window.ContentWindow()
        
        # Create Guides Window
        self.guides = guides_window.GuidesWindow()
        
        # Create Credits Window
        self.credits = credits.Credits()
        
        self.startup_window.show()
        self.pygame_canvas.show()
        self.health_library.show()
        self.guides.show()
        self.credits.show()
        
        self.items = gtk.Notebook()
        self.items.set_show_tabs(False)
        self.items.set_show_border(False)
        self.items.append_page(self.startup_window)
        self.items.append_page(self.pygame_canvas)
        self.items.append_page(self.health_library)
        self.items.append_page(self.guides)
        self.items.append_page(self.credits)
        self.items.show()
        
        self.set_canvas(self.items)

        logging.debug("Create main")
        self.game = game.Main()
        self.game.set_game_over_callback(self.game_over_callback)
        
        self.toolbox.connect('current-toolbar-changed', self.change_mode)
        self.make_toolbox(False)
        self.toolbox.set_current_toolbar(0)     # Start in activity tab
        
        # force the toolbar change
        self.change_mode(None, self.toolbox.get_current_toolbar())
        
        game.set_library_function = self.set_library    # Sets the callback to put links in the library
        
        self.show()
    
    def make_toolbox(self, add_game):
        toolbars = len(self.indexes)
        for i in range(toolbars, 0, -1):
            self.toolbox.remove_toolbar(i)
        
        self.indexes = ["activity"]     # activity toolbar never gets removed
        
        if add_game:
            self.toolbox.add_toolbar(_("Game"), self.get_game_toolbar())
            self.indexes += ["game"]
        
        self.indexes += ["library", "guides", "credits"]
        self.toolbox.add_toolbar(_("Health Library"), self.health_library.get_toolbar())
        self.toolbox.add_toolbar(_("Guides"), self.guides_toolbar)
        self.toolbox.add_toolbar(_("Credits"), self.credits_toolbar)
        
    def change_mode(self, notebook, index):
        game.pause = True
        self.pygame_canvas.translator.unhook_pygame()
        self.health_library.ditch()
        self.guides.ditch()
        self.guides.ditch()
        
        gc.collect()    # Collects garbaje
        
        if self.indexes[index] == "activity":
            self.items.set_current_page(0)
            
        if self.indexes[index] == "game":
            game.pause = False
            self.show_game()
            self.items.set_current_page(1)

        elif self.indexes[index] == "library":
            self.items.set_current_page(2)
            
        elif self.indexes[index] == "guides":
            self.items.set_current_page(3)
            
        elif self.indexes[index] == "credits":
            self.credits.before_show()
            self.items.set_current_page(4)
    
    #Override activity.Activity's can_close method
    def can_close(self):
        game.running = False
        return True
    
    def _start_cb(self, gender, name):
        #self.create_in_journal()
        self.metadata['title'] = _("Saludame") + " " + name
        self.game.gender = gender
        self.game.name = name
        self.startup_window.set_welcome()
        self.make_toolbox(True)
        self.toolbox.set_current_toolbar(1)             # Move to game tab
    
    def show_game(self):
        if self.game.started:
            self.game.windows_controller.reload_main = True       # Repaints the whole screen
        
        if self.game_init:
            self.pygame_canvas.translator.hook_pygame()
        else:
            self.game_init = True
            # Start pygame
            self.pygame_canvas.run_pygame(lambda:self.game.main(True))    # Indico que llame a la función local para iniciar el juego pygame

    def set_library(self, link, anchor=None):
        self.toolbox.set_current_toolbar(2)
        self.health_library.set_url(link, anchor)
    
    def _load_last_cb(self, event):
        metadata = self.get_last_game()
        if metadata:
            self.metadata['title'] = metadata['title']
            self.make_toolbox(True)
            self.toolbox.set_current_toolbar(1)
    
    def game_over_callback(self):
        self.make_toolbox(False)
        self.toolbox.set_current_toolbar(0)             # Move to game tab
        
    def write_file(self, file_path):
        if self.game.started:
            try:
                self.game.save_game(file_path)
            except Exception,e:
                print "Error writting to file"
                print e
                raise e
        else:
            raise NotImplementedError
    
    def game_loaded(self):
        self.game.loaded_game = self.loaded_game
        self.make_toolbox(True)
        self.toolbox.set_current_toolbar(1)     # Loading an older game, move to game tab
    
    def read_file(self, file_path):
        logging.debug("Read file")
        # load data from file for this datastore entry in to the data variable
        fd = open(file_path, 'r')
        try:
            data = fd.read()
            self.loaded_game = data
            self.game_loaded()
        except Exception,e:
            print "Error loading data"
            print e
        finally:
            fd.close()
    
    def get_last_game(self):
        """ Make a query in the datastore to load last game """
        
        metadata = None
        
        # This query returns the last time activity instance with the saved flag on
        ds_objects, num_objects = datastore.find({'activity': 'org.ceibaljam.Saludame'})
        for entry in ds_objects:
            filepath = entry.get_file_path()
            if filepath:
                metadata = entry.get_metadata()
                filepath = entry.get_file_path()
                print "Last game is ", metadata['title'], " ", metadata['mtime']
                self.read_file(filepath)
                break
                
        for entry in ds_objects:
            entry.destroy()
            
        return metadata
        
        # This query returns the last activity instance that has a path
        #ds_objects, num_objects = datastore.find({'activity': 'org.ceibaljam.Saludame'}, sorting='mtime')
        
    def get_game_toolbar(self):
        
        toolbar = gtk.Toolbar()
        
        # Music Volume scale
        min = 0
        max = 10
        step = 1
        default = 3
        
        image = gtk.Image()
        image.set_from_file("assets/music/music_icon.png")
        image.show()
        
        tool_item = gtk.ToolItem()
        tool_item.set_expand(False)
        tool_item.add(image)
        tool_item.show()
        toolbar.insert(tool_item, -1)
        
        adj = gtk.Adjustment(default, min, max, step)
        scale = gtk.HScale(adj)
        scale.set_update_policy(gtk.UPDATE_DISCONTINUOUS)
        scale.set_size_request(240,15)
        scale.set_draw_value(False)
        scale.connect("value-changed", self.game.volume_changed)
        scale.show()
                
        tool_item = gtk.ToolItem()
        tool_item.set_expand(False)
        tool_item.add(scale)
        tool_item.show()
        toolbar.insert(tool_item, -1)
        
        toolbar.show()
        return toolbar