1
1
# -*- coding: utf-8 -*-
2
- # Copyright (c) 2012 Walter Bender
2
+ # Copyright (c) 2012 Walter Bender <walter.bender@gmail.com>
3
3
# Copyright (c) 2013 Aneesh Dogra <lionaneesh@gmail.com>
4
+ # Copyright (c) 2013 Ignacio Rodríguez <ignacio@sugarlabs.org>
4
5
# This program is free software; you can redistribute it and/or modify
5
6
# it under the terms of the GNU General Public License as published by
6
7
# the Free Software Foundation; either version 3 of the License, or
11
12
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
12
13
# Boston, MA 02111-1307, USA.
13
14
14
- import gtk
15
- import gobject
15
+
16
+ from gi . repository import Gtk , Gdk , GObject , GdkPixbuf
16
17
import os
17
18
import codecs
18
19
from random import uniform , choice
19
20
20
21
from gettext import gettext as _
21
22
22
- from sugar .datastore import datastore
23
+ from sugar3 .datastore import datastore
23
24
from utils .play_audio import play_audio_from_file
24
25
25
26
import logging
26
27
_logger = logging .getLogger ('lettermatch-activity' )
27
28
28
- try :
29
- from sugar .graphics import style
30
- GRID_CELL_SIZE = style .GRID_CELL_SIZE
31
- except ImportError :
32
- GRID_CELL_SIZE = 0
29
+ from sugar3 .graphics import style
30
+ GRID_CELL_SIZE = style .GRID_CELL_SIZE
33
31
34
32
from genpieces import generate_card
35
33
from utils .sprites import Sprites , Sprite
@@ -66,18 +64,17 @@ def __init__(self, canvas, lessons_path, images_path, sounds_path,
66
64
self ._canvas = canvas
67
65
self ._activity .show_all ()
68
66
69
- self ._canvas .set_flags (gtk .CAN_FOCUS )
70
- self ._canvas .add_events (gtk .gdk .BUTTON_PRESS_MASK )
71
- self ._canvas .add_events (gtk .gdk .BUTTON_RELEASE_MASK )
72
- self ._canvas .connect ("expose-event" , self ._expose_cb )
67
+ self ._canvas .add_events (Gdk .EventMask .BUTTON_PRESS_MASK )
68
+ self ._canvas .add_events (Gdk .EventMask .BUTTON_RELEASE_MASK )
69
+ self ._canvas .connect ("draw" , self .__draw_cb )
73
70
self .button_release_event_id = \
74
71
self ._canvas .connect ("button-release-event" , self ._button_release_cb )
75
72
self .button_press_event_id = \
76
73
self ._canvas .connect ("button-press-event" , self ._button_press_cb )
77
74
78
75
self ._canvas .connect ("key_press_event" , self ._keypress_cb )
79
- self ._width = gtk . gdk . screen_width ()
80
- self ._height = gtk . gdk . screen_height ()
76
+ self ._width = Gdk . Screen . width ()
77
+ self ._height = Gdk . Screen . height ()
81
78
self ._card_height = int ((self ._height - GRID_CELL_SIZE ) / YDIM ) \
82
79
- GUTTER * 2
83
80
self ._card_width = int (self ._card_height * 4 / 3. )
@@ -106,7 +103,7 @@ def __init__(self, canvas, lessons_path, images_path, sounds_path,
106
103
def new_page (self ):
107
104
''' Load a page of cards '''
108
105
if self .timeout is not None :
109
- gobject .source_remove (self .timeout )
106
+ GObject .source_remove (self .timeout )
110
107
self ._hide_cards ()
111
108
self .new_target ()
112
109
x = self ._grid_x_offset + self ._card_width + GUTTER * 3
@@ -173,7 +170,7 @@ def _alpha_cards(self):
173
170
h2 = 1.0 - h1
174
171
bot .composite (top , 0 , int (h1 * top .get_height ()),
175
172
top .get_width (), int (h2 * top .get_height ()),
176
- 0 , 0 , 1 , 1 , gtk . gdk . INTERP_NEAREST , 255 )
173
+ 0 , 0 , 1 , 1 , GdkPixbuf . InterpType . NEAREST , 255 )
177
174
self ._cards .append (Sprite (self ._sprites , 0 , 0 , top ))
178
175
else :
179
176
stroke = self ._test_for_stroke ()
@@ -207,8 +204,8 @@ def new_target(self):
207
204
self .answers [i ] = self .target
208
205
209
206
if self .timeout is not None :
210
- gobject .source_remove (self .timeout )
211
- self .timeout = gobject .timeout_add (1000 , self ._play_target_sound )
207
+ GObject .source_remove (self .timeout )
208
+ self .timeout = GObject .timeout_add (1000 , self ._play_target_sound )
212
209
213
210
def _bad_answer (self , i ):
214
211
''' Make sure answer is unique '''
@@ -275,18 +272,29 @@ def _button_release_cb(self, win, event):
275
272
276
273
if self .current_card == self .target :
277
274
self ._activity .status .set_text (_ ('Very good!' ))
275
+ self ._play (True )
278
276
if self .timeout is not None :
279
- gobject .source_remove (self .timeout )
280
- self .timeout = gobject .timeout_add (1000 , self .new_page )
277
+ GObject .source_remove (self .timeout )
278
+ self .timeout = GObject .timeout_add (1000 , self .new_page )
281
279
else :
282
280
self ._activity .status .set_text (_ ('Please try again.' ))
281
+ self ._play (False )
283
282
self ._play_target_sound ()
284
-
283
+
284
+ def _play (self , great ):
285
+ if great :
286
+ play_audio_from_file (os .getcwd () + '/sounds/great.ogg' )
287
+ else :
288
+ play_audio_from_file (os .getcwd () + '/sounds/bad.ogg' )
289
+
285
290
def _keypress_cb (self , area , event ):
286
291
''' No keyboard shortcuts at the moment. Perhaps jump to the page
287
292
associated with the key pressed? '''
288
293
return True
289
-
294
+
295
+ def __draw_cb (self , canvas , cr ):
296
+ self ._sprites .redraw_sprites (cr = cr )
297
+
290
298
def _expose_cb (self , win , event ):
291
299
''' Callback to handle window expose events '''
292
300
self .do_expose_event (event )
@@ -308,12 +316,16 @@ def do_expose_event(self, event):
308
316
309
317
def _destroy_cb (self , win , event ):
310
318
''' Make a clean exit. '''
311
- gtk .main_quit ()
319
+ Gtk .main_quit ()
312
320
313
321
def invalt (self , x , y , w , h ):
314
322
''' Mark a region for refresh '''
315
- self ._canvas .window .invalidate_rect (
316
- gtk .gdk .Rectangle (int (x ), int (y ), int (w ), int (h )), False )
323
+ rectangle = Gdk .Rectangle ()
324
+ rectangle .x = x
325
+ rectangle .y = y
326
+ rectangle .width = w
327
+ rectangle .height = h
328
+ self ._canvas .window .invalidate_rect (rectangle )
317
329
318
330
def load_level (self , path ):
319
331
''' Load a level (CSV) from path: letter, word, color, image,
@@ -371,14 +383,14 @@ def _clear_all(self):
371
383
372
384
def svg_str_to_pixbuf (svg_string ):
373
385
''' Load pixbuf from SVG string. '''
374
- pl = gtk .gdk .PixbufLoader ('svg' )
386
+ pl = GdkPixbuf .PixbufLoader .new_with_type ('svg' )
387
+ if type (svg_string ) == unicode :
388
+ svg_string = svg_string .encode ('ascii' , 'replace' )
375
389
pl .write (svg_string )
376
390
pl .close ()
377
- pixbuf = pl .get_pixbuf ()
378
- return pixbuf
391
+ return pl .get_pixbuf ()
379
392
380
393
381
394
def image_file_to_pixbuf (file_path , w , h ):
382
395
''' Load pixbuf from file '''
383
- return gtk .gdk .pixbuf_new_from_file_at_size (file_path , int (w ), int (h ))
384
-
396
+ return GdkPixbuf .Pixbuf .new_from_file_at_size (file_path , int (w ), int (h ))
0 commit comments