Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/olpcgames/pausescreen.py
diff options
context:
space:
mode:
authorManuel Kaufmann <humitos@gmail.com>2012-03-27 13:15:10 (GMT)
committer Rafael Ortiz <rafael@activitycentral.com>2012-03-28 18:05:22 (GMT)
commit335ad73456ba3ec8f56811abddcaca4650199db1 (patch)
treedb788baba57c7656c9bdb73eb9003a70dc87d59a /olpcgames/pausescreen.py
parent6deeb3f569e6c9a1c02a32a011b7a96a58fa8443 (diff)
Save and restore state of the game
Ability to 'save' (when the user closes the Activity) and 'restore' (when the user launch it from the Journal or the Home without holding Alt) the state of the game. For this ability I had to upgrade 'olpcgames' to 1.6 because 'olpcgames.FILE_READ_REQUEST' and 'olpcgames.FILE_WRITE_REQUEST' events are added in that version and those events are needed for this. The data is saved (as JSON, with json module) in the 'event.metadata["state"]' and the timestamp state is saved in 'event.filename'. This commit solves ticket #2393: * http://bugs.sugarlabs.org/ticket/2393 Signed-off-by: Manuel Kaufmann <humitos@gmail.com> Signed-off-by: Rafael Ortiz <rafael@activitycentral.com>
Diffstat (limited to 'olpcgames/pausescreen.py')
-rw-r--r--olpcgames/pausescreen.py53
1 files changed, 40 insertions, 13 deletions
diff --git a/olpcgames/pausescreen.py b/olpcgames/pausescreen.py
index 95f1a7a..113a0ea 100644
--- a/olpcgames/pausescreen.py
+++ b/olpcgames/pausescreen.py
@@ -7,12 +7,39 @@ we have more involved activities using the code.
We use svgsprite to render a graphic which is stored in the
olpcgames data directory over a dimmed version of the current
screen contents.
+
+_LAST_EVENT_TIME -- tracks the last time that we saw an event
+ come across the wire.
"""
import logging
-log = logging.getLogger('olpcgames.pausescreen')
+log = logging.getLogger( 'olpcgames.pausescreen' )
import pygame
from pygame import sprite
+_LAST_EVENT_TIME = 0
+
+def _set_last_event_time( time=None ):
+ """Set time as the last event time
+
+ time -- if None, pygame.time.get_ticks() is used
+
+ returns time set
+ """
+ global _LAST_EVENT_TIME
+ if time is None:
+ time = pygame.time.get_ticks()
+ _LAST_EVENT_TIME = time
+ return time
+
+def last_event_time( ):
+ """Return the duration since last event for pausing operations
+
+ returns time in seconds
+ """
+ global _LAST_EVENT_TIME
+ return (pygame.time.get_ticks() - _LAST_EVENT_TIME)/1000.
+
+
def get_events( sleep_timeout = 10, pause=None, **args ):
"""Retrieve the set of pending events or sleep
@@ -21,7 +48,7 @@ def get_events( sleep_timeout = 10, pause=None, **args ):
by taking the current screen and modifying it in some way. Defaults
to pauseScreen in this module. If you return nothing from this
function then no restoration or display-flipping will occur
- *args -- if present, passed to pause to configuration operation (e.g.
+ *args -- if present, passed to 'pause' to configuration operation (e.g.
to specify a different overlaySVG file)
returns set of pending events (potentially empty)
@@ -30,25 +57,25 @@ def get_events( sleep_timeout = 10, pause=None, **args ):
pause = pauseScreen
events = pygame.event.get( )
if not events:
- log.info('No events in queue')
+ log.info( 'No events in queue' )
old_screen = None
- if hasattr(pygame.event, 'last_event_time') and pygame.event.last_event_time() > sleep_timeout:
+ if last_event_time() > sleep_timeout:
# we've been waiting long enough, go to sleep visually
- log.warn('Pausing activity after %s with function %s', sleep_timeout, pause)
+ log.warn( 'Pausing activity after %s with function %s', sleep_timeout, pause )
old_screen = pause( )
if old_screen:
pygame.display.flip()
# now we wait until there *are* some events (efficiently)
# and retrieve any extra events that are waiting...
events = [ pygame.event.wait() ] + pygame.event.get()
- log.warn('Activity restarted')
+ log.warn( 'Activity restarted')
if old_screen:
- restoreScreen(old_screen)
- else:
- log.info('Not running under OLPCGames')
+ restoreScreen( old_screen )
+ if events:
+ _set_last_event_time()
return events
-def pauseScreen(overlaySVG=None):
+def pauseScreen( overlaySVG=None ):
"""Display a "Paused" screen and suspend
This default implementation will not do anything to shut down your
@@ -69,7 +96,7 @@ def pauseScreen(overlaySVG=None):
)
pause_sprite.rect.center = screen.get_rect().center
group = sprite.RenderUpdates( )
- group.add(pause_sprite)
+ group.add( pause_sprite )
# dim the screen and display the 'paused' message in the center.
BLACK = (0,0,0)
@@ -79,10 +106,10 @@ def pauseScreen(overlaySVG=None):
screen.fill(BLACK)
screen.blit(dimmed, (0,0))
- group.draw(screen)
+ group.draw( screen )
return old_screen
-def restoreScreen(old_screen):
+def restoreScreen( old_screen ):
"""Restore the original screen and return"""
screen = pygame.display.get_surface()
screen.blit(old_screen, (0,0))