diff options
author | flavio <fdanesse@gmail.com> | 2012-07-01 18:37:58 (GMT) |
---|---|---|
committer | flavio <fdanesse@gmail.com> | 2012-07-01 18:37:58 (GMT) |
commit | 6b1703e85294c3d686dba1d77afef7453cd70bc2 (patch) | |
tree | 801f4885b8b85a858ca9e73b8e3bf778823adf6a | |
parent | 1b4a4011f483104064be9a98c966d8d30fb4dccd (diff) |
La Actividad Inicia
-rwxr-xr-x | activity.py | 24 | ||||
-rw-r--r-- | game.py | 2 | ||||
-rw-r--r-- | olpcgames/activity.py | 19 | ||||
-rw-r--r-- | olpcgames/canvas.py | 19 | ||||
-rw-r--r-- | olpcgames/eventwrap.py | 70 | ||||
-rw-r--r-- | olpcgames/gtkEvent.py | 77 | ||||
-rwxr-xr-x | setup.py | 2 |
7 files changed, 108 insertions, 105 deletions
diff --git a/activity.py b/activity.py index 4afbf56..2d7e0df 100755 --- a/activity.py +++ b/activity.py @@ -2,12 +2,12 @@ import olpcgames import pygame -import gtk +from gi.repository import Gtk -from sugar.activity.widgets import ActivityToolbarButton -from sugar.activity.widgets import StopButton -from sugar.graphics.toolbarbox import ToolbarBox -from sugar.graphics.toolbutton import ToolButton +from sugar3.activity.widgets import ActivityToolbarButton +from sugar3.activity.widgets import StopButton +from sugar3.graphics.toolbarbox import ToolbarBox +from sugar3.graphics.toolbutton import ToolButton from gettext import gettext as _ @@ -24,7 +24,7 @@ class MazeActivity(olpcgames.PyGameActivity): toolbar_box.toolbar.insert(activity_button, 0) activity_button.show() - separator = gtk.SeparatorToolItem() + separator = Gtk.SeparatorToolItem() toolbar_box.toolbar.insert(separator, -1) separator.show() @@ -38,7 +38,7 @@ class MazeActivity(olpcgames.PyGameActivity): harder_button.connect('clicked', self._harder_button_cb) toolbar_box.toolbar.insert(harder_button, -1) - separator = gtk.SeparatorToolItem() + separator = Gtk.SeparatorToolItem() separator.props.draw = False separator.set_size_request(0, -1) separator.set_expand(True) @@ -55,9 +55,11 @@ class MazeActivity(olpcgames.PyGameActivity): return toolbar_box def _easier_button_cb(self, button): - pygame.event.post(olpcgames.eventwrap.Event( - pygame.USEREVENT, action='easier_button')) + #pygame.event.post(olpcgames.eventwrap.Event( + # pygame.USEREVENT, action='easier_button')) + pass def _harder_button_cb(self, button): - pygame.event.post(olpcgames.eventwrap.Event( - pygame.USEREVENT, action='harder_button')) + #pygame.event.post(olpcgames.eventwrap.Event( + # pygame.USEREVENT, action='harder_button')) + pass @@ -40,7 +40,7 @@ log.setLevel(logging.DEBUG) import olpcgames.pausescreen as pausescreen import olpcgames.mesh as mesh from olpcgames.util import get_bundle_path -from sugar.presence import presenceservice +from sugar3.presence import presenceservice bundlepath = get_bundle_path() presenceService = presenceservice.get_instance() diff --git a/olpcgames/activity.py b/olpcgames/activity.py index 0f3ecd7..570f49a 100644 --- a/olpcgames/activity.py +++ b/olpcgames/activity.py @@ -27,16 +27,15 @@ logging.root.setLevel( logging.WARN ) log = logging.getLogger( 'olpcgames.activity' ) ##log.setLevel( logging.DEBUG ) -import pygtk -pygtk.require('2.0') -import gtk -import gtk.gdk +from gi.repository import Gtk +from gi.repository import Gdk import os from sugar3.activity import activity from sugar3.graphics import style from olpcgames.canvas import PygameCanvas -from olpcgames import mesh, util +from olpcgames import mesh +from olpcgames import util __all__ = ['PygameActivity'] @@ -98,7 +97,8 @@ class PygameActivity(activity.Activity): super(PygameActivity, self).__init__(handle) self.make_global() if self.game_size is None: - width,height = gtk.gdk.screen_width(), gtk.gdk.screen_height() + screen = self.get_screen() + width,height = (screen.get_width(), screen.get_height()) log.info( 'Total screen size: %s %s', width,height) # for now just fudge the toolbar size... self.game_size = width, height - (1*style.GRID_CELL_SIZE) @@ -140,7 +140,6 @@ class PygameActivity(activity.Activity): log.info( 'asserting focus' ) assert self._pgc.is_focus(), """Did not successfully set pygame canvas focus""" log.info( 'callback finished' ) - def joined_cb(*args, **kwargs): log.info( 'joined: %s, %s', args, kwargs ) mesh.activity_joined(self) @@ -169,7 +168,7 @@ class PygameActivity(activity.Activity): self._pgc.connect_game(self.game_handler or self.game_name) # XXX Bad coder, do not hide in a widely subclassed operation # map signal does not appear to show up on socket instances - gtk.gdk.threads_init() + Gdk.threads_init() return self._pgc else: # FIXME Es necesario quitar hippo de aquĆ @@ -239,8 +238,8 @@ class PygameActivity(activity.Activity): raise NotImplementedError( """Pygame Activity code did not produce a file for %s"""%( file_path, )) else: log.info( '''Stored file in %r''', file_path ) - + import olpcgames olpcgames.PyGameActivity = PygameActivity -PyGameActivity = PygameActivity +PyGameActivity = PygameActivity
\ No newline at end of file diff --git a/olpcgames/canvas.py b/olpcgames/canvas.py index 9dadeaa..4f0096e 100644 --- a/olpcgames/canvas.py +++ b/olpcgames/canvas.py @@ -7,17 +7,15 @@ log = logging.getLogger( 'olpcgames.canvas' ) import threading from pprint import pprint -import pygtk -pygtk.require('2.0') -import gtk -import gobject +from gi.repository import Gtk +from gi.repository import GObject import pygame from olpcgames import gtkEvent, util __all__ = ['PygameCanvas'] -class PygameCanvas(gtk.Layout): +class PygameCanvas(Gtk.Layout): """Canvas providing bridge methods to run Pygame in GTK The PygameCanvas creates a secondary thread in which the Pygame instance will @@ -40,12 +38,13 @@ class PygameCanvas(gtk.Layout): # Build the main widget log.info( 'Creating the pygame canvas' ) super(PygameCanvas,self).__init__() - self.set_flags(gtk.CAN_FOCUS) + #self.set_flags(Gtk.CAN_FOCUS) # Build the sub-widgets - self._align = gtk.Alignment(0.5, 0.5) - self._inner_evb = gtk.EventBox() - self._socket = gtk.Socket() + #self._align = Gtk.Alignment(0.5, 0.5) + self._align = Gtk.Alignment() + self._inner_evb = Gtk.EventBox() + self._socket = Gtk.Socket() # Add internal widgets @@ -134,7 +133,7 @@ class PygameCanvas(gtk.Layout): eventwrap.clear() finally: log.info( 'Main function finished, calling main_quit' ) - gtk.main_quit() + Gtk.main_quit() source_object_id = None def view_source(self): diff --git a/olpcgames/eventwrap.py b/olpcgames/eventwrap.py index 402109c..d20091c 100644 --- a/olpcgames/eventwrap.py +++ b/olpcgames/eventwrap.py @@ -1,25 +1,25 @@ """Provides substitute for Pygame's "event" module using gtkEvent -Provides methods which will be substituted into Pygame in order to +Provides methods which will be substituted into Pygame in order to provide the synthetic events that we will feed into the Pygame queue. These methods are registered by the "install" method. -This event queue does not support getting events only of a certain type. -You need to get all pending events at a time, or filter them yourself. You -can, however, block and unblock events of certain types, so that may be -useful to you. +This event queue does not support getting events only of a certain type. +You need to get all pending events at a time, or filter them yourself. You +can, however, block and unblock events of certain types, so that may be +useful to you. Set_grab doesn't do anything (you are not allowed to grab events). Sorry. Extensions: - wait( timeout=None ) -- allows you to wait for only a specified period - before you return to the application. Can be used to e.g. wait for a + wait( timeout=None ) -- allows you to wait for only a specified period + before you return to the application. Can be used to e.g. wait for a short period, then release some resources, then wait a bit more, then release a few more resources, then a bit more... """ import pygame -import gtk +from gi.repository import GObject import Queue import thread, threading import logging @@ -29,10 +29,11 @@ log = logging.getLogger( 'olpcgames.eventwrap' ) from pygame.event import Event, event_name, pump as pygame_pump, get as pygame_get -class Event(object): +class Event(GObject.Object): """Mock pygame events""" def __init__(self, type, dict=None,**named): """Initialise the new event variables from dictionary and named become attributes""" + GObject.Object.__init__(self) self.type = type if dict: self.__dict__.update( dict ) @@ -54,8 +55,8 @@ class Event(object): """Block until this event is finished processing Event process is only finalized on the *next* call to retrieve an event - after the processing operation in which the event is processed. In some - extremely rare cases we might actually see that happen, were the + after the processing operation in which the event is processed. In some + extremely rare cases we might actually see that happen, were the file-saving event (for example) causes the Pygame event loop to exit. In that case, the GTK event loop *could* hang. """ @@ -68,18 +69,19 @@ class Event(object): self.__lock.set() log.info( '''Released GTK thread on event: %s''', self ) except AttributeError, err: - pass + pass -class CallbackResult( object ): +class CallbackResult( GObject.Object ): def __init__( self, callable, args, named, callContext=None ): """Perform callback in Pygame loop with args and named - callContext is used to provide more information when there is + callContext is used to provide more information when there is a failure in the callback (for debugging purposes) """ + GObject.Object.__init__(self) self.callable = callable - self.args = args - self.named = named + self.args = args + self.named = named if callContext is None: callContext = util.get_traceback( None ) self.callContext = callContext @@ -90,7 +92,7 @@ class CallbackResult( object ): except Exception, err: log.error( """Failure in callback %s( *%s, **%s ): %s\n%s""", - getattr(self.callable, '__name__',self.callable), + getattr(self.callable, '__name__',self.callable), self.args, self.named, util.get_traceback( err ), self.callContext @@ -117,7 +119,7 @@ def _processCallbacks( events ): else: result.append( event ) if events and not result: - result.append( + result.append( Event( type=pygame.NOEVENT ) ) return result @@ -126,18 +128,18 @@ def _recordEvents( events ): """Record the set of events to retire on the next iteration""" global _EVENTS_TO_RETIRE events = _processCallbacks( events ) - _EVENTS_TO_RETIRE = events + _EVENTS_TO_RETIRE = events return events def install(): """Installs this module (eventwrap) as an in-place replacement for the pygame.event module. Use install() when you need to interact with Pygame code written - without reference to the olpcgames wrapper mechanisms to have the + without reference to the olpcgames wrapper mechanisms to have the code use this module's event queue. XXX Really, use it everywhere you want to use olpcgames, as olpcgames - registers the handler itself, so you will always wind up with it registered when + registers the handler itself, so you will always wind up with it registered when you use olpcgames (the gtkEvent.Translator.hook_pygame method calls it). """ log.info( 'Installing OLPCGames event wrapper' ) @@ -153,7 +155,7 @@ class _FilterQueue( Queue.Queue ): def get_type( self, filterFunction, block=True, timeout=None ): """Get events of a given type - Note: can raise Empty *even* when blocking if someone else + Note: can raise Empty *even* when blocking if someone else pops the event off the queue before we get around to it. """ self.not_empty.acquire() @@ -182,14 +184,14 @@ class _FilterQueue( Queue.Queue ): """Are we empty with respect to filterFunction?""" for element in self.queue: if filterFunction( element ): - return False + return False return True def _get_type( self, filterFunction ): """Get the first instance which matches filterFunction""" for element in self.queue: if filterFunction( element ): self.queue.remove( element ) - return element + return element # someone popped the event off the queue before we got to it! raise Queue.Empty def peek_type( self, filterFunction= lambda x: True ): @@ -236,14 +238,14 @@ def pump(): def get( types=None): """Get a list of all pending events - types -- either an integer event-type or a sequence of integer event types - which restrict the set of event-types returned from the queue. Keep in mind + types -- either an integer event-type or a sequence of integer event types + which restrict the set of event-types returned from the queue. Keep in mind that if you do not remove events you may wind up with an eternally growing - queue or a full queue. Normally you will want to remove all events in your + queue or a full queue. Normally you will want to remove all events in your top-level event-loop and propagate them yourself. Note: if you use types you lose all event ordering guarantees, events - may show up after events which were originally produced before them due to + may show up after events which were originally produced before them due to the re-ordering of the queue on filtering! """ pump() @@ -278,13 +280,13 @@ def poll(): def wait( timeout = None): """Get the next pending event, wait up to timeout if none - timeout -- if present, only wait up to timeout seconds, if we - do not find an event before then, return None. timeout + timeout -- if present, only wait up to timeout seconds, if we + do not find an event before then, return None. timeout is an OLPCGames-specific extension. """ pump() try: - result = None + result = None result = g_events.get(block=True, timeout=timeout) try: return _recordEvents( [result] )[0] @@ -296,8 +298,8 @@ def wait( timeout = None): def peek(types=None): """True if there is any pending event - types -- optional set of event-types used to check whether - an event is of interest. If specified must be either a sequence + types -- optional set of event-types used to check whether + an event is of interest. If specified must be either a sequence of integers/longs or an integer/long. """ if types: @@ -306,7 +308,7 @@ def peek(types=None): return not g_events.empty() def clear(): - """Clears the entire pending queue of events + """Clears the entire pending queue of events Rarely used """ diff --git a/olpcgames/gtkEvent.py b/olpcgames/gtkEvent.py index 6b20102..13304af 100644 --- a/olpcgames/gtkEvent.py +++ b/olpcgames/gtkEvent.py @@ -1,25 +1,25 @@ """gtkEvent.py: translate GTK events into Pygame events.""" -import pygtk -pygtk.require('2.0') -import gtk -import gobject +from gi.repository import Gtk +from gi.repository import Gdk +from gi.repository import GObject import pygame from olpcgames import eventwrap -import logging +import logging log = logging.getLogger( 'olpcgames.gtkevent' ) ##log.setLevel( logging.DEBUG ) -class _MockEvent(object): +class _MockEvent(GObject.Object): """Used to inject key-repeat events on the gtk side.""" def __init__(self, keyval): + GObject.Object.__init__(self) self.keyval = keyval -class Translator(object): - """Utility class to translate GTK events into Pygame events +class Translator(GObject.Object): + """Utility class to translate GTK events into Pygame events The Translator object interprets incoming GTK events and generates Pygame events in the eventwrap module's queue as a result. - It also handles generating Pygame style key-repeat events + It also handles generating Pygame style key-repeat events by synthesizing them via a GTK timer. """ key_trans = { @@ -31,9 +31,9 @@ class Translator(object): 'Shift_R': pygame.K_RSHIFT, 'Super_L': pygame.K_LSUPER, 'Super_R': pygame.K_RSUPER, - 'KP_Page_Up' : pygame.K_KP9, + 'KP_Page_Up' : pygame.K_KP9, 'KP_Page_Down' : pygame.K_KP3, - 'KP_End' : pygame.K_KP1, + 'KP_End' : pygame.K_KP1, 'KP_Home' : pygame.K_KP7, 'KP_Up' : pygame.K_KP8, 'KP_Down' : pygame.K_KP2, @@ -54,6 +54,7 @@ class Translator(object): def __init__(self, mainwindow, mouselistener=None): """Initialise the Translator with the windows to which to listen""" # _inner_evb is Mouselistener + GObject.Object.__init__(self) self._mainwindow = mainwindow if mouselistener is None: mouselistener = mainwindow @@ -62,16 +63,16 @@ class Translator(object): # Need to set our X event masks so we see mouse motion and stuff -- mainwindow.set_events( - gtk.gdk.KEY_PRESS_MASK | \ - gtk.gdk.KEY_RELEASE_MASK \ + Gdk.EventMask.KEY_PRESS_MASK | \ + Gdk.EventMask.KEY_RELEASE_MASK \ ) self._inner_evb.set_events( - gtk.gdk.POINTER_MOTION_MASK | \ - gtk.gdk.POINTER_MOTION_HINT_MASK | \ - gtk.gdk.BUTTON_MOTION_MASK | \ - gtk.gdk.BUTTON_PRESS_MASK | \ - gtk.gdk.BUTTON_RELEASE_MASK + Gdk.EventMask.POINTER_MOTION_MASK | \ + Gdk.EventMask.POINTER_MOTION_HINT_MASK | \ + Gdk.EventMask.BUTTON_MOTION_MASK | \ + Gdk.EventMask.BUTTON_PRESS_MASK | \ + Gdk.EventMask.BUTTON_RELEASE_MASK ) # Callback functions to link the event systems @@ -83,8 +84,8 @@ class Translator(object): self._inner_evb.connect('motion-notify-event', self._mousemove) # You might need to do this - mainwindow.set_flags(gtk.CAN_FOCUS) - self._inner_evb.set_flags(gtk.CAN_FOCUS) + #mainwindow.set_flags(Gtk.CAN_FOCUS) + #self._inner_evb.set_flags(Gtk.CAN_FOCUS) # Internal data self.__stopped = False @@ -98,13 +99,13 @@ class Translator(object): self.__tick_id = None #print "translator initialized" - self._inner_evb.connect( 'expose-event', self.do_expose_event ) + self._inner_evb.connect( 'draw', self.draw ) # screen = gtk.gdk.screen_get_default() # screen.connect( 'size-changed', self.do_resize_event ) self._inner_evb.connect( 'configure-event', self.do_resize_event ) - def do_expose_event(self, event, widget): + def draw(self, widget, ctx): """Handle exposure event (trigger redraw by gst)""" - log.info( 'Expose event: %s', event ) + #log.info( 'Expose event: %s', event ) from olpcgames import eventwrap eventwrap.post( eventwrap.Event( eventwrap.pygame.VIDEOEXPOSE )) return True @@ -114,10 +115,10 @@ class Translator(object): log.info( 'Event values: %s', (event.width,event.height) ) # from olpcgames import eventwrap # # shouldn't the activity's window have this information too? -# eventwrap.post( -# eventwrap.Event( -# eventwrap.pygame.VIDEORESIZE, -# dict(size=(event.width,event.height), width=event.width, height=event.height) +# eventwrap.post( +# eventwrap.Event( +# eventwrap.pygame.VIDEORESIZE, +# dict(size=(event.width,event.height), width=event.width, height=event.height) # ) # ) return False # continue processing @@ -168,10 +169,10 @@ class Translator(object): def _keyevent(self, widget, event, type): - key = gtk.gdk.keyval_name(event.keyval) + key = event.get_keyval() if key is None: # No idea what this key is. - return False + return False keycode = None if key in self.key_trans: @@ -192,7 +193,7 @@ class Translator(object): self.__keystate[keycode] = type == pygame.KEYDOWN if type == pygame.KEYUP: mod = self._keymods() - ukey = unichr(gtk.gdk.keyval_to_unicode(event.keyval)) + ukey = unichr(event.get_keycode()) if ukey == '\000': ukey = '' evt = eventwrap.Event(type, key=keycode, unicode=ukey, mod=mod) @@ -218,15 +219,15 @@ class Translator(object): def _mouseevent(self, widget, event, type): - evt = eventwrap.Event(type, - button=event.button, + evt = eventwrap.Event(type, + button=event.button, pos=(event.x, event.y)) self._post(evt) return True def _mousemove(self, widget, event): # From http://www.learningpython.com/2006/07/25/writing-a-custom-widget-using-pygtk/ - # if this is a hint, then let's get all the necessary + # if this is a hint, then let's get all the necessary # information, if not it's all we need. if event.is_hint: x, y, state = event.window.get_pointer() @@ -240,9 +241,9 @@ class Translator(object): self.__mouse_pos = (x, y) self.__button_state = [ - state & gtk.gdk.BUTTON1_MASK and 1 or 0, - state & gtk.gdk.BUTTON2_MASK and 1 or 0, - state & gtk.gdk.BUTTON3_MASK and 1 or 0, + state & Gdk.EventMask.BUTTON1_MASK and 1 or 0, + state & Gdk.EventMask.BUTTON2_MASK and 1 or 0, + state & Gdk.EventMask.BUTTON3_MASK and 1 or 0, ] evt = eventwrap.Event(pygame.MOUSEMOTION, @@ -256,7 +257,7 @@ class Translator(object): """Generate synthetic events for held-down keys""" cur_time = pygame.time.get_ticks() for key in self.__held: - delta = cur_time - self.__held_last_time[key] + delta = cur_time - self.__held_last_time[key] self.__held_last_time[key] = cur_time self.__held_time_left[key] -= delta @@ -271,7 +272,7 @@ class Translator(object): if delay is not None and self.__repeat[0] is None: self.__tick_id = gobject.timeout_add(10, self._tick) elif delay is None and self.__repeat[0] is not None: - gobject.source_remove(self.__tick_id) + GObject.source_remove(self.__tick_id) self.__repeat = (delay, interval) def _get_mouse_pos(self): @@ -1,3 +1,3 @@ #!/usr/bin/env python -from sugar.activity import bundlebuilder +from sugar3.activity import bundlebuilder bundlebuilder.start() |