diff options
author | Gary Martin <gary@garycmartin.com> | 2010-10-06 17:56:32 (GMT) |
---|---|---|
committer | Gary Martin <gary@garycmartin.com> | 2010-10-06 17:56:32 (GMT) |
commit | 6be1dada2fde0e8e06220c9c509338c350123f0d (patch) | |
tree | 11843e9e6241b55e9b2666b9f46d7b851f0932e4 | |
parent | a45bd81c6e7bf30d7c598c291578960a2f9b2db8 (diff) |
Watch for focus events so that pygame run loop is more gracefully idle when in background.
-rw-r--r-- | activity.py | 8 | ||||
-rw-r--r-- | physics.py | 46 | ||||
-rw-r--r-- | tools.py | 4 |
3 files changed, 36 insertions, 22 deletions
diff --git a/activity.py b/activity.py index bf6ac0f..a718257 100644 --- a/activity.py +++ b/activity.py @@ -44,6 +44,7 @@ class PhysicsActivity(olpcgames.PyGameActivity): def __init__(self, handle): super(PhysicsActivity, self).__init__(handle) self.metadata['mime_type'] = 'application/x-physics-activity' + self.connect('visibility-notify-event', self._focus_event) def get_preview(self): """Custom preview code to get image from pygame. @@ -173,3 +174,10 @@ class PhysicsActivity(olpcgames.PyGameActivity): pygame.event.post(olpcgames.eventwrap.Event(pygame.USEREVENT, action=self.radioList[button])) + def _focus_event(self, event, data=None): + """Send focus events to pygame to allow it to more gracefully idle when in the background. + """ + if data.state == gtk.gdk.VISIBILITY_FULLY_OBSCURED: + pygame.event.post(olpcgames.eventwrap.Event(pygame.USEREVENT, action="focus_out")) + else: + pygame.event.post(olpcgames.eventwrap.Event(pygame.USEREVENT, action="focus_in")) @@ -78,32 +78,34 @@ class PhysicsGame: self.show_fake_cursor = True def run(self): + self.in_focus = True while True: for event in pygame.event.get(): self.currentTool.handleEvents(event) - # Drive motors - if self.world.run_physics: - for body in self.world.world.GetBodyList(): - if type(body.userData) == type({}): - if body.userData.has_key('rollMotor'): - diff = body.userData['rollMotor']['targetVelocity'] - body.GetAngularVelocity() - body.ApplyTorque(body.userData['rollMotor']['strength'] * diff * body.getMassData().I) - - # Update & Draw World - self.world.update() - self.screen.fill((255, 255, 255)) # 255 for white - self.world.draw() - - # Draw output from tools - self.currentTool.draw() - - # Show Sugar like cursor for UI consistancy - if self.show_fake_cursor: - self.screen.blit(self.cursor_picture, pygame.mouse.get_pos()) - - # Flip Display - pygame.display.flip() + if self.in_focus: + # Drive motors + if self.world.run_physics: + for body in self.world.world.GetBodyList(): + if type(body.userData) == type({}): + if body.userData.has_key('rollMotor'): + diff = body.userData['rollMotor']['targetVelocity'] - body.GetAngularVelocity() + body.ApplyTorque(body.userData['rollMotor']['strength'] * diff * body.getMassData().I) + + # Update & Draw World + self.world.update() + self.screen.fill((255, 255, 255)) # 255 for white + self.world.draw() + + # Draw output from tools + self.currentTool.draw() + + # Show Sugar like cursor for UI consistancy + if self.show_fake_cursor: + self.screen.blit(self.cursor_picture, pygame.mouse.get_pos()) + + # Flip Display + pygame.display.flip() # Stay under 30 FPS to help keep the rest of the platform responsive self.clock.tick(30) # Originally 50 @@ -48,6 +48,10 @@ class Tool(object): if event.action == "stop_start_toggle": # Stop/start simulation self.game.world.run_physics = not self.game.world.run_physics + elif event.action == "focus_in": + self.game.in_focus = True + elif event.action == "focus_out": + self.game.in_focus = False elif self.game.toolList.has_key(event.action): self.game.setTool(event.action) elif hasattr(event,"code"): |