diff options
author | Daniel Francis <francis@sugarlabs.org> | 2012-12-23 22:27:06 (GMT) |
---|---|---|
committer | Daniel Francis <francis@sugarlabs.org> | 2012-12-23 22:27:06 (GMT) |
commit | ccbfabb4fcbd8799d1a68729ab5268a09542c879 (patch) | |
tree | 7bd8bec93c9cfbdf8ba094b5f6def2639aed6da8 | |
parent | 9674738abc8984d15b55418e52718ac28669cc7d (diff) |
Initial port
Signed-off-by: Daniel Francis <francis@sugarlabs.org>
-rw-r--r-- | activity.py | 118 | ||||
-rw-r--r-- | physics.py | 34 | ||||
-rwxr-xr-x | setup.py | 2 | ||||
-rw-r--r-- | sugargame/canvas.py | 16 | ||||
-rw-r--r-- | sugargame/event.py | 45 |
5 files changed, 108 insertions, 107 deletions
diff --git a/activity.py b/activity.py index 3a97890..5771b99 100644 --- a/activity.py +++ b/activity.py @@ -20,30 +20,26 @@ import tools import sugargame import sugargame.canvas import pygame -from sugar.graphics.radiotoolbutton import RadioToolButton -from sugar.graphics.toolbutton import ToolButton -from sugar.activity import activity +from sugar3.graphics.radiotoolbutton import RadioToolButton +from sugar3.graphics.toolbutton import ToolButton +from sugar3.activity import activity from gettext import gettext as _ -import gtk - +from gi.repository import Gtk +from gi.repository import Gdk +from gi.repository import GdkPixbuf import physics -try: - # >= 0.86 toolbars - from sugar.graphics.toolbarbox import ToolbarBox - from sugar.activity.widgets import ActivityToolbarButton - from sugar.activity.widgets import StopButton -except ImportError: - # <= 0.84 toolbars - pass +from sugar3.graphics.toolbarbox import ToolbarBox +from sugar3.activity.widgets import ActivityToolbarButton +from sugar3.activity.widgets import StopButton class PhysicsActivity(activity.Activity): def __init__(self, handle): super(PhysicsActivity, self).__init__(handle) self.metadata['mime_type'] = 'application/x-physics-activity' - self.add_events(gtk.gdk.ALL_EVENTS_MASK | - gtk.gdk.VISIBILITY_NOTIFY_MASK) + self.add_events(Gdk.EventMask.ALL_EVENTS_MASK | + Gdk.EventMask.VISIBILITY_NOTIFY_MASK) self.connect('visibility-notify-event', self._focus_event) self.connect('window-state-event', self._window_event) # Build the Pygame canvas. @@ -68,12 +64,12 @@ class PhysicsActivity(activity.Activity): """ surface = pygame.display.get_surface() width, height = surface.get_width(), surface.get_height() - pixbuf = gtk.gdk.pixbuf_new_from_data(pygame.image.tostring(surface, + pixbuf = GdkPixbuf.Pixbuf.new_from_data(pygame.image.tostring(surface, "RGB"), - gtk.gdk.COLORSPACE_RGB, 0, 8, + GdkPixbuf.Colorspace.RGB, 0, 8, width, height, - 3 * width) - pixbuf = pixbuf.scale_simple(300, 225, gtk.gdk.INTERP_BILINEAR) + 3 * width, (lambda: None), None) + pixbuf = pixbuf.scale_simple(300, 225, GdkPixbuf.InterpType.BILINEAR) preview_data = [] def save_func(buf, data): @@ -86,51 +82,32 @@ class PhysicsActivity(activity.Activity): # Setup the toolbar def build_toolbar(self): - try: - # Use new >= 0.86 toolbar - self.max_participants = 1 - toolbar_box = ToolbarBox() - activity_button = ActivityToolbarButton(self) - toolbar_box.toolbar.insert(activity_button, 0) - activity_button.show() - - separator = gtk.SeparatorToolItem() - toolbar_box.toolbar.insert(separator, -1) - separator.show() - - self._insert_create_tools(toolbar_box.toolbar) - - separator = gtk.SeparatorToolItem() - separator.props.draw = False - separator.set_size_request(0, -1) - separator.set_expand(True) - toolbar_box.toolbar.insert(separator, -1) - separator.show() - - stop_button = StopButton(self) - toolbar_box.toolbar.insert(stop_button, -1) - stop_button.show() - - self.set_toolbar_box(toolbar_box) - toolbar_box.show() - return toolbar_box - - except NameError: - # Use old <= 0.84 toolbar design - toolbox = activity.ActivityToolbox(self) - activity_toolbar = toolbox.get_activity_toolbar() - activity_toolbar.share.props.visible = False - - create_toolbar = gtk.Toolbar() - self._insert_create_tools(create_toolbar) - - toolbox.add_toolbar(_("Create"), create_toolbar) - create_toolbar.show() - toolbox.set_current_toolbar(1) - - toolbox.show() - self.set_toolbox(toolbox) - return activity_toolbar + self.max_participants = 1 + toolbar_box = ToolbarBox() + activity_button = ActivityToolbarButton(self) + toolbar_box.toolbar.insert(activity_button, 0) + activity_button.show() + + separator = Gtk.SeparatorToolItem() + toolbar_box.toolbar.insert(separator, -1) + separator.show() + + self._insert_create_tools(toolbar_box.toolbar) + + separator = Gtk.SeparatorToolItem() + separator.props.draw = False + separator.set_size_request(0, -1) + separator.set_expand(True) + toolbar_box.toolbar.insert(separator, -1) + separator.show() + + stop_button = StopButton(self) + toolbar_box.toolbar.insert(stop_button, -1) + stop_button.show() + + self.set_toolbar_box(toolbar_box) + toolbar_box.show() + return toolbar_box def _insert_create_tools(self, create_toolbar): # Stop/play button @@ -142,7 +119,7 @@ class PhysicsActivity(activity.Activity): create_toolbar.insert(self.stop_play, -1) self.stop_play.show() - separator = gtk.SeparatorToolItem() + separator = Gtk.SeparatorToolItem() create_toolbar.insert(separator, -1) separator.show() @@ -150,11 +127,10 @@ class PhysicsActivity(activity.Activity): self.radioList = {} firstButton = None for c in tools.allTools: - button = RadioToolButton(named_icon=c.icon) + button = RadioToolButton(icon_name=c.icon) if firstButton: - button.set_group(firstButton) + button.set_group(firstButton.get_group()) else: - button.set_group(None) firstButton = button button.set_tooltip(c.toolTip) button.set_accelerator(c.toolAccelerator) @@ -182,7 +158,7 @@ class PhysicsActivity(activity.Activity): 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: + if data.state == Gdk.VisibilityState.FULLY_OBSCURED: pygame.event.post(pygame.event.Event(pygame.USEREVENT, action="focus_out")) else: @@ -193,6 +169,6 @@ class PhysicsActivity(activity.Activity): 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: + if event.changed_mask & Gdk.WindowState.ICONIFIED: pygame.event.post(pygame.event.Event(pygame.USEREVENT, - action="focus_out")) + action="focus_out")) @@ -25,6 +25,9 @@ Code: git://git.sugarlabs.org/physics/mainline.git """ +import logging +logger = logging.getLogger('physics') + import sys import math import pygame @@ -40,7 +43,9 @@ import Box2D as box2d import elements import tools from helpers import * -import gtk +from gi.repository import Gtk +from gi.repository import Gdk + class PhysicsGame: def __init__(self, activity): @@ -73,6 +78,7 @@ class PhysicsGame: self.opening_queue = path def run(self): + logger.debug('Run') self.screen = pygame.display.get_surface() pygame.display.init() self.world = elements.Elements(self.screen.get_size()) @@ -91,12 +97,25 @@ class PhysicsGame: self.switch_on_fake_pygame_cursor_cb) self.canvas.connect("leave_notify_event", self.switch_off_fake_pygame_cursor_cb) - self.canvas.add_events(gtk.gdk.ENTER_NOTIFY_MASK - | gtk.gdk.LEAVE_NOTIFY_MASK) + self.canvas.add_events(Gdk.EventMask.ENTER_NOTIFY_MASK + | Gdk.EventMask.LEAVE_NOTIFY_MASK) while True: - while gtk.events_pending(): - gtk.main_iteration() + logger.debug('Loop') + + #FIXME: I crash :P + while True: + logger.debug('Need to iterate in GTK!') + pending = Gtk.events_pending() + logger.debug('Pending, I can crash...') + if pending: + Gtk.main_iteration() + logger.debug('Main iteration, I wont crash now') + else: + logger.debug('No more iteration') + break + + logger.debug('GTK OK') if self.opening_queue: self.world.json_load(self.opening_queue) @@ -107,6 +126,8 @@ class PhysicsGame: if event.button == 1: self.show_fake_cursor = True + logger.debug('Events detected') + if self.in_focus: # Drive motors if self.world.run_physics: @@ -136,8 +157,11 @@ class PhysicsGame: # Flip Display pygame.display.flip() + logger.debug('No focus') + # Stay < 30 FPS to help keep the rest of the platform responsive self.clock.tick(30) # Originally 50 + logger.debug('Running again') def setTool(self, tool): self.currentTool.cancel() @@ -1,4 +1,4 @@ #!/usr/bin/env python -from sugar.activity import bundlebuilder +from sugar3.activity import bundlebuilder bundlebuilder.start() diff --git a/sugargame/canvas.py b/sugargame/canvas.py index 980cb73..1ce0250 100644 --- a/sugargame/canvas.py +++ b/sugargame/canvas.py @@ -1,18 +1,18 @@ import os -import gtk -import gobject +from gi.repository import Gtk +from gi.repository import GObject import pygame import event CANVAS = None -class PygameCanvas(gtk.EventBox): +class PygameCanvas(Gtk.EventBox): """ mainwindow is the activity intself. """ def __init__(self, mainwindow, pointer_hint = True): - gtk.EventBox.__init__(self) + GObject.GObject.__init__(self) global CANVAS assert CANVAS == None, "Only one PygameCanvas can be created, ever." @@ -23,9 +23,9 @@ class PygameCanvas(gtk.EventBox): self._mainwindow = mainwindow - self.set_flags(gtk.CAN_FOCUS) + self.set_can_focus(True) - self._socket = gtk.Socket() + self._socket = Gtk.Socket() self.add(self._socket) self.show_all() @@ -34,7 +34,7 @@ class PygameCanvas(gtk.EventBox): # Sugar activity is not properly created until after its constructor returns. # If the Pygame main loop is called from the activity constructor, the # constructor never returns and the activity freezes. - gobject.idle_add(self._run_pygame_cb, main_fn) + GObject.idle_add(self._run_pygame_cb, main_fn) def _run_pygame_cb(self, main_fn): assert pygame.display.get_surface() is None, "PygameCanvas.run_pygame can only be called once." @@ -45,7 +45,7 @@ class PygameCanvas(gtk.EventBox): pygame.init() # Restore the default cursor. - self._socket.window.set_cursor(None) + self._socket.props.window.set_cursor(None) # Initialize the Pygame window. r = self.get_allocation() diff --git a/sugargame/event.py b/sugargame/event.py index 4cc3be8..431a600 100644 --- a/sugargame/event.py +++ b/sugargame/event.py @@ -1,5 +1,6 @@ -import gtk -import gobject +from gi.repository import Gtk +from gi.repository import Gdk +from gi.repository import GObject import pygame import pygame.event import logging @@ -46,20 +47,20 @@ class Translator(object): # Enable events # (add instead of set here because the main window is already realized) self._mainwindow.add_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 ) - self._mainwindow.set_flags(gtk.CAN_FOCUS) - self._inner_evb.set_flags(gtk.CAN_FOCUS) + self._mainwindow.set_can_focus(True) + self._inner_evb.set_can_focus(True) # Callback functions to link the event systems self._mainwindow.connect('unrealize', self._quit_cb) @@ -68,7 +69,7 @@ class Translator(object): self._inner_evb.connect('button_press_event', self._mousedown_cb) self._inner_evb.connect('button_release_event', self._mouseup_cb) self._inner_evb.connect('motion-notify-event', self._mousemove_cb) - self._inner_evb.connect('expose-event', self._expose_cb) + self._inner_evb.connect('draw', self._draw_cb) self._inner_evb.connect('configure-event', self._resize_cb) # Internal data @@ -88,7 +89,7 @@ class Translator(object): pygame.mouse.get_pressed = self._get_mouse_pressed pygame.mouse.get_pos = self._get_mouse_pos - def _expose_cb(self, event, widget): + def _draw_cb(self, widget, event): if pygame.display.get_init(): pygame.event.post(pygame.event.Event(pygame.VIDEOEXPOSE)) return True @@ -133,7 +134,7 @@ class Translator(object): return mod def _keyevent(self, widget, event, type): - key = gtk.gdk.keyval_name(event.keyval) + key = Gdk.keyval_name(event.keyval) if key is None: # No idea what this key is. return False @@ -157,7 +158,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(Gdk.keyval_to_unicode(event.keyval)) if ukey == '\000': ukey = '' evt = pygame.event.Event(type, key=keycode, unicode=ukey, mod=mod) @@ -189,19 +190,19 @@ class Translator(object): # 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() + win, x, y, state = event.window.get_device_position(event.device) else: x = event.x y = event.y - state = event.state + state = event.get_state() rel = (x - self.__mouse_pos[0], y - self.__mouse_pos[1]) 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.ModifierType.BUTTON1_MASK and 1 or 0, + state & Gdk.ModifierType.BUTTON2_MASK and 1 or 0, + state & Gdk.ModifierType.BUTTON3_MASK and 1 or 0, ] evt = pygame.event.Event(pygame.MOUSEMOTION, @@ -224,9 +225,9 @@ class Translator(object): def _set_repeat(self, delay=None, interval=None): if delay is not None and self.__repeat[0] is None: - self.__tick_id = gobject.timeout_add(10, self._tick_cb) + self.__tick_id = GObject.timeout_add(10, self._tick_cb) 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): |