From e447f92baa4727b9fa246f2282f68803ee0e0c69 Mon Sep 17 00:00:00 2001 From: Daniel Francis Date: Fri, 21 Dec 2012 01:53:07 +0000 Subject: All the files Signed-off-by: Daniel Francis --- diff --git a/activity.py b/activity.py index f63e686..e54bbd1 100644 --- a/activity.py +++ b/activity.py @@ -17,7 +17,8 @@ """ import tools -import olpcgames +import sugargame +import sugargame.canvas import pygame from sugar.graphics.radiotoolbutton import RadioToolButton from sugar.graphics.toolbutton import ToolButton @@ -25,6 +26,7 @@ from sugar.activity import activity from gettext import gettext as _ import gtk +import physics try: # >= 0.86 toolbars @@ -36,11 +38,7 @@ except ImportError: pass -class PhysicsActivity(olpcgames.PyGameActivity): - game_name = 'physics' - game_title = _('Physics') - game_size = None # Olpcgame will choose size - +class PhysicsActivity(activity.Activity): def __init__(self, handle): super(PhysicsActivity, self).__init__(handle) self.metadata['mime_type'] = 'application/x-physics-activity' @@ -48,6 +46,17 @@ class PhysicsActivity(olpcgames.PyGameActivity): gtk.gdk.VISIBILITY_NOTIFY_MASK) self.connect('visibility-notify-event', self._focus_event) self.connect('window-state-event', self._window_event) + # Build the Pygame canvas. + self._canvas = sugargame.canvas.PygameCanvas(self) + self.game = physics.main(self) + self.build_toolbar() + + # Note that set_canvas implicitly calls read_file when resuming from the Journal. + self.set_canvas(self._canvas) + + # Start the game running. + self._canvas.run_pygame(self.game.run) + def get_preview(self): """Custom preview code to get image from pygame. @@ -150,8 +159,8 @@ class PhysicsActivity(olpcgames.PyGameActivity): self.radioList[button] = c.name def stop_play_cb(self, button): - pygame.event.post(olpcgames.eventwrap.Event(pygame.USEREVENT, - action="stop_start_toggle")) + pygame.event.post(sugargame.eventwrap.Event(pygame.USEREVENT, + action="stop_start_toggle")) self.stop_play_state = not self.stop_play_state # Update button if self.stop_play_state: @@ -162,22 +171,22 @@ class PhysicsActivity(olpcgames.PyGameActivity): self.stop_play.set_tooltip(_("Start")) def radioClicked(self, button): - pygame.event.post(olpcgames.eventwrap.Event(pygame.USEREVENT, + pygame.event.post(sugargame.eventwrap.Event(pygame.USEREVENT, action=self.radioList[button])) def _focus_event(self, event, data=None): """Send focus events to pygame to allow it to idle when in background. """ if data.state == gtk.gdk.VISIBILITY_FULLY_OBSCURED: - pygame.event.post(olpcgames.eventwrap.Event(pygame.USEREVENT, + pygame.event.post(pygame.event.Event(pygame.USEREVENT, action="focus_out")) else: - pygame.event.post(olpcgames.eventwrap.Event(pygame.USEREVENT, + pygame.event.post(pygame.event.Event(pygame.USEREVENT, action="focus_in")) def _window_event(self, window, event): """Send focus out event to pygame when switching to a desktop view. """ if event.changed_mask & gtk.gdk.WINDOW_STATE_ICONIFIED: - pygame.event.post(olpcgames.eventwrap.Event(pygame.USEREVENT, + pygame.event.post(pygame.event.Event(pygame.USEREVENT, action="focus_out")) diff --git a/physics.py b/physics.py index 47eb464..9298e11 100644 --- a/physics.py +++ b/physics.py @@ -30,11 +30,12 @@ import math import pygame from pygame.locals import * from pygame.color import * -import olpcgames +import sugargame sys.path.append("lib/") import pkg_resources -sys.path.append("lib/Elements-0.13-py2.5.egg") -sys.path.append("lib/Box2D-2.0.2b1-py2.5-linux-i686.egg") +# If your architecture is different, comment these lines and install the modules in your system. +#sys.path.append("lib/Elements-0.13-py2.5.egg") +#sys.path.append("lib/Box2D-2.0.2b1-py2.5-linux-i686.egg") import Box2D as box2d import elements import tools @@ -42,11 +43,10 @@ from helpers import * import gtk class PhysicsGame: - def __init__(self, screen): - self.screen = screen + def __init__(self, activity): # Get everything set up self.clock = pygame.time.Clock() - self.canvas = olpcgames.ACTIVITY.canvas + self.canvas = activity.canvas self.in_focus = True # Create the name --> instance map for components self.toolList = {} @@ -55,6 +55,16 @@ class PhysicsGame: self.currentTool = self.toolList[tools.allTools[0].name] # Set up the world (instance of Elements) self.box2d = box2d + + def switch_off_fake_pygame_cursor_cb(self, panel, event): + self.show_fake_cursor = False + + def switch_on_fake_pygame_cursor_cb(self, panel, event): + self.show_fake_cursor = True + + def run(self): + self.screen = pygame.display.get_surface() + pygame.display.init() self.world = elements.Elements(self.screen.get_size()) self.world.renderer.set_surface(self.screen) @@ -74,13 +84,6 @@ class PhysicsGame: self.canvas.add_events(gtk.gdk.ENTER_NOTIFY_MASK | gtk.gdk.LEAVE_NOTIFY_MASK) - def switch_off_fake_pygame_cursor_cb(self, panel, event): - self.show_fake_cursor = False - - def switch_on_fake_pygame_cursor_cb(self, panel, event): - self.show_fake_cursor = True - - def run(self): while True: for event in pygame.event.get(): self.currentTool.handleEvents(event) @@ -121,16 +124,9 @@ class PhysicsGame: self.currentTool.cancel() self.currentTool = self.toolList[tool] -def main(): - toolbarheight = 75 - tabheight = 45 - pygame.display.init() - video_info = pygame.display.Info() - x = video_info.current_w - y = video_info.current_h - screen = pygame.display.set_mode((x, y - toolbarheight - tabheight)) - game = PhysicsGame(screen) - game.run() +def main(activity): + game = PhysicsGame(activity) + return game # Make sure that main get's called if __name__ == '__main__': diff --git a/tools.py b/tools.py index fb118fc..c03130f 100644 --- a/tools.py +++ b/tools.py @@ -22,7 +22,7 @@ # By Alex Levenson #================================================================== import pygame -import olpcgames +import sugargame from pygame.locals import * from helpers import * from inspect import getmro @@ -56,11 +56,11 @@ class Tool(object): elif self.game.toolList.has_key(event.action): self.game.setTool(event.action) elif hasattr(event, "code"): - if event.code == olpcgames.FILE_WRITE_REQUEST: + if event.code == sugargame.FILE_WRITE_REQUEST: #Saving to journal self.game.world.add.remove_mouseJoint() self.game.world.json_save(event.filename) - elif event.code == olpcgames.FILE_READ_REQUEST: + elif event.code == sugargame.FILE_READ_REQUEST: #Loading from journal self.game.world.json_load(event.filename) elif event.type == MOUSEBUTTONDOWN and event.button == 1: -- cgit v0.9.1