Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIgnacio Rodríguez <ignaciorodriguez@sugarlabs.org>2013-11-20 16:41:03 (GMT)
committer Ignacio Rodríguez <ignaciorodriguez@sugarlabs.org>2013-11-20 16:41:03 (GMT)
commit48d87969bc0685f9e4de6c198dcc0d42008c4d5f (patch)
tree7818d2a2df14f11f79002447aaa470211080c95c
parent11f7abe8d042bf91c403d6c5548e23726fc4c6a6 (diff)
Hello World Skeleton - SugarGame
-rwxr-xr-xSugarGame/HelloWorld.py52
-rw-r--r--SugarGame/activity/activity-helloworld.svg26
-rw-r--r--SugarGame/activity/activity.info6
-rwxr-xr-xSugarGame/game.py48
-rwxr-xr-xSugarGame/setup.py4
-rwxr-xr-xSugarGame/sugargame/__init__.py1
-rwxr-xr-xSugarGame/sugargame/canvas.py71
-rwxr-xr-xSugarGame/sugargame/event.py257
8 files changed, 465 insertions, 0 deletions
diff --git a/SugarGame/HelloWorld.py b/SugarGame/HelloWorld.py
new file mode 100755
index 0000000..7e42e72
--- /dev/null
+++ b/SugarGame/HelloWorld.py
@@ -0,0 +1,52 @@
+#! /usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import sugargame
+import sugargame.canvas
+import gtk
+
+from sugar.activity import activity
+from sugar.graphics.toolbarbox import ToolbarBox
+from sugar.activity.widgets import ActivityToolbarButton
+from sugar.activity.widgets import StopButton
+
+import game
+
+class HelloWorld(activity.Activity):
+
+ def __init__(self, handle):
+ activity.Activity.__init__(self, handle)
+
+ self.max_participants = 1
+ self.build_toolbar()
+ self.actividad = game.MiJuego()
+ self._pygamecanvas = sugargame.canvas.PygameCanvas(self)
+ self.set_canvas(self._pygamecanvas)
+ self._pygamecanvas.grab_focus()
+ self._pygamecanvas.run_pygame(self.actividad.juego_loop)
+
+ def read_file(self, file_path):
+ pass
+
+ def write_file(self, file_path):
+ pass
+
+ def build_toolbar(self):
+ toolbar_box = ToolbarBox()
+ toolbar_box.show()
+
+ activity_button = ActivityToolbarButton(self)
+ toolbar_box.toolbar.insert(activity_button, -1)
+ activity_button.show()
+
+ separator = gtk.SeparatorToolItem()
+ separator.props.draw = False
+ separator.set_expand(True)
+
+ toolbar_box.toolbar.insert(separator, -1)
+ stop_button = StopButton(self)
+ toolbar_box.toolbar.insert(stop_button, -1)
+ stop_button.show()
+
+ self.set_toolbar_box(toolbar_box)
+ toolbar_box.show_all()
diff --git a/SugarGame/activity/activity-helloworld.svg b/SugarGame/activity/activity-helloworld.svg
new file mode 100644
index 0000000..8da7c63
--- /dev/null
+++ b/SugarGame/activity/activity-helloworld.svg
@@ -0,0 +1,26 @@
+<?xml version="1.0" ?><!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN' 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd' [
+ <!ENTITY stroke_color "#010101">
+ <!ENTITY fill_color "#FFFFFF">
+]>
+<svg enable-background="new 0 0 55 55" height="55px" version="1.1" viewBox="0 0 55 55" width="55px" x="0px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" y="0px">
+ <g display="block" id="activity-helloworld">
+ <path d="M9.263,48.396c0.682,1.152,6.027,0.059,8.246-1.463 c2.102-1.432,3.207-2.596,4.336-2.596c1.133,0,12.54,0.92,20.935-5.715c7.225-5.707,9.773-13.788,4.52-21.437 c-5.252-7.644-13.832-9.08-20.878-8.56C16.806,9.342,4.224,16.91,4.677,28.313c0.264,6.711,3.357,9.143,4.922,10.703 c1.562,1.566,4.545,1.566,2.992,5.588C11.981,46.183,8.753,47.522,9.263,48.396z" display="inline" fill="&fill_color;" stroke="&stroke_color;" stroke-width="3.5"/>
+ </g>
+ <circle cx="27.375" cy="27.5" r="19.903"
+ transform="matrix(0.6,0,0,0.6,10.95,11)"
+ id="circle4" fill="&fill_color;" stroke="&stroke_color;" stroke-width="3.5" display="inline" />
+ <g transform="matrix(0.6,0,0,0.6,10.95,11)" id="g6" style="display:inline">
+ <path d="m 27.376,7.598 c 0,0 -11.205,8.394 -11.205,19.976 0,11.583 11.205,19.829 11.205,19.829"
+ id="path8" fill="&fill_color;" stroke="&stroke_color;" stroke-width="3.5" />
+ <path d="m 27.376,7.598 c 0,0 11.066,9.141 11.066,19.976 0,10.839 -11.066,19.829 -11.066,19.829"
+ id="path10" fill="&fill_color;" stroke="&stroke_color;" stroke-width="3.5" />
+ <line x1="27.375999" x2="27.375999" y1="7.598" y2="47.402"
+ id="line12" fill="&fill_color;" stroke="&stroke_color;" stroke-width="3.5" />
+ <line x1="27.375999" x2="27.375999" y1="7.598" y2="47.402"
+ id="line14" fill="&fill_color;" stroke="&stroke_color;" stroke-width="3.5" />
+ <line x1="27.375999" x2="27.375999" y1="7.598" y2="47.402"
+ id="line16" fill="&fill_color;" stroke="&stroke_color;" stroke-width="3.5" />
+ <line x1="7.4720001" x2="47.278" y1="27.5" y2="27.5"
+ id="line18" fill="&fill_color;" stroke="&stroke_color;" stroke-width="3.5" />
+ </g>
+</svg>
diff --git a/SugarGame/activity/activity.info b/SugarGame/activity/activity.info
new file mode 100644
index 0000000..7db05aa
--- /dev/null
+++ b/SugarGame/activity/activity.info
@@ -0,0 +1,6 @@
+[Activity]
+name = Hello World
+activity_version = 1
+bundle_id = org.sugarlabs.HelloWorld
+icon = activity-helloworld
+exec = sugar-activity HelloWorld.HelloWorld
diff --git a/SugarGame/game.py b/SugarGame/game.py
new file mode 100755
index 0000000..c117193
--- /dev/null
+++ b/SugarGame/game.py
@@ -0,0 +1,48 @@
+#! /usr/bin/env python
+
+import pygame
+from pygame.locals import *
+
+import gtk, sys
+
+BLANCO = (255, 255, 255)
+NEGRO = (0, 0, 0)
+
+class MiJuego():
+ def __init__(self):
+ pass
+
+ def juego_loop(self):
+ pygame.init()
+ global x, y, fuente, texto
+ x = gtk.gdk.screen_width()
+ y = gtk.gdk.screen_height() - 55
+ pygame.display.set_caption('Hello world!')
+
+ fuente = pygame.font.SysFont(None, 48)
+ texto = fuente.render('Hello world!', True, BLANCO, NEGRO)
+
+ reloj = pygame.time.Clock()
+ pantalla = pygame.display.get_surface()
+
+ while 1:
+ while gtk.events_pending():
+ gtk.main_iteration()
+ for event in pygame.event.get():
+ if event.type == pygame.QUIT:
+ exit("Juego finalizado")
+ elif event.type == pygame.VIDEORESIZE:
+ pygame.display.set_mode(event.size, pygame.RESIZABLE)
+
+
+ pantalla.fill(NEGRO)
+
+ pantalla.blit(texto, ((x / 2) - (x / 10), (y / 2) - (y / 10)))
+ pygame.display.flip()
+
+ # Try to stay at 30 FPS
+ reloj.tick(30)
+
+
+if __name__ == "__main__":
+ MiJuego()
diff --git a/SugarGame/setup.py b/SugarGame/setup.py
new file mode 100755
index 0000000..77fda74
--- /dev/null
+++ b/SugarGame/setup.py
@@ -0,0 +1,4 @@
+#!/usr/bin/env python
+from sugar.activity import bundlebuilder
+if __name__ == "__main__":
+ bundlebuilder.start()
diff --git a/SugarGame/sugargame/__init__.py b/SugarGame/sugargame/__init__.py
new file mode 100755
index 0000000..439eb0c
--- /dev/null
+++ b/SugarGame/sugargame/__init__.py
@@ -0,0 +1 @@
+__version__ = '1.1'
diff --git a/SugarGame/sugargame/canvas.py b/SugarGame/sugargame/canvas.py
new file mode 100755
index 0000000..a43bf55
--- /dev/null
+++ b/SugarGame/sugargame/canvas.py
@@ -0,0 +1,71 @@
+import os
+import gtk
+import gobject
+import pygame
+import event
+
+CANVAS = None
+
+class PygameCanvas(gtk.EventBox):
+
+ """
+ mainwindow is the activity intself.
+ """
+ def __init__(self, mainwindow, pointer_hint = True):
+ gtk.EventBox.__init__(self)
+
+ global CANVAS
+ assert CANVAS == None, "Only one PygameCanvas can be created, ever."
+ CANVAS = self
+
+ # Initialize Events translator before widget gets "realized".
+ self.translator = event.Translator(mainwindow, self)
+
+ self._mainwindow = mainwindow
+
+ self.set_flags(gtk.CAN_FOCUS)
+
+ self._socket = gtk.Socket()
+ self.add(self._socket)
+
+ self._initialized = False
+
+ self.show_all()
+
+ def run_pygame(self, main_fn):
+ # Run the main loop after a short delay. The reason for the delay is that the
+ # 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)
+
+ def _run_pygame_cb(self, main_fn):
+ # PygameCanvas.run_pygame can only be called once
+ if self._initialized:
+ return
+
+ # Preinitialize Pygame with the X window ID.
+ os.environ['SDL_WINDOWID'] = str(self._socket.get_id())
+ if pygame.display.get_surface() is not None:
+ pygame.display.quit()
+ pygame.init()
+
+ # Restore the default cursor.
+ self._socket.window.set_cursor(None)
+
+ # Initialize the Pygame window.
+ r = self.get_allocation()
+ pygame.display.set_mode((r.width, r.height), pygame.RESIZABLE)
+
+ # Hook certain Pygame functions with GTK equivalents.
+ self.translator.hook_pygame()
+
+ # Run the Pygame main loop.
+ main_fn()
+
+ self._initialized = True
+
+ return False
+
+ def get_pygame_widget(self):
+ return self._socket
diff --git a/SugarGame/sugargame/event.py b/SugarGame/sugargame/event.py
new file mode 100755
index 0000000..299c277
--- /dev/null
+++ b/SugarGame/sugargame/event.py
@@ -0,0 +1,257 @@
+import gtk
+import gobject
+import pygame
+import pygame.event
+
+class _MockEvent(object):
+ def __init__(self, keyval):
+ self.keyval = keyval
+
+class Translator(object):
+ key_trans = {
+ 'Alt_L': pygame.K_LALT,
+ 'Alt_R': pygame.K_RALT,
+ 'Control_L': pygame.K_LCTRL,
+ 'Control_R': pygame.K_RCTRL,
+ 'Shift_L': pygame.K_LSHIFT,
+ 'Shift_R': pygame.K_RSHIFT,
+ 'Super_L': pygame.K_LSUPER,
+ 'Super_R': pygame.K_RSUPER,
+ 'KP_Page_Up' : pygame.K_KP9,
+ 'KP_Page_Down' : pygame.K_KP3,
+ 'KP_End' : pygame.K_KP1,
+ 'KP_Home' : pygame.K_KP7,
+ 'KP_Up' : pygame.K_KP8,
+ 'KP_Down' : pygame.K_KP2,
+ 'KP_Left' : pygame.K_KP4,
+ 'KP_Right' : pygame.K_KP6,
+
+ }
+
+ mod_map = {
+ pygame.K_LALT: pygame.KMOD_LALT,
+ pygame.K_RALT: pygame.KMOD_RALT,
+ pygame.K_LCTRL: pygame.KMOD_LCTRL,
+ pygame.K_RCTRL: pygame.KMOD_RCTRL,
+ pygame.K_LSHIFT: pygame.KMOD_LSHIFT,
+ pygame.K_RSHIFT: pygame.KMOD_RSHIFT,
+ }
+
+ def __init__(self, mainwindow, inner_evb):
+ """Initialise the Translator with the windows to which to listen"""
+ self._mainwindow = mainwindow
+ self._inner_evb = inner_evb
+
+ # 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 | \
+ gtk.gdk.VISIBILITY_NOTIFY_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
+ )
+
+ self._mainwindow.set_flags(gtk.CAN_FOCUS)
+ self._inner_evb.set_flags(gtk.CAN_FOCUS)
+
+ # Callback functions to link the event systems
+ self._mainwindow.connect('unrealize', self._quit_cb)
+ self._mainwindow.connect('visibility_notify_event', self._visibility_cb)
+ self._inner_evb.connect('key_press_event', self._keydown_cb)
+ self._inner_evb.connect('key_release_event', self._keyup_cb)
+ 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('configure-event', self._resize_cb)
+ self._inner_evb.connect('screen-changed', self._screen_changed_cb)
+
+ # Internal data
+ self.__stopped = False
+ self.__keystate = [0] * 323
+ self.__button_state = [0,0,0]
+ self.__mouse_pos = (0,0)
+ self.__repeat = (None, None)
+ self.__held = set()
+ self.__held_time_left = {}
+ self.__held_last_time = {}
+ self.__tick_id = None
+
+ def hook_pygame(self):
+ pygame.key.get_pressed = self._get_pressed
+ pygame.key.set_repeat = self._set_repeat
+ pygame.mouse.get_pressed = self._get_mouse_pressed
+ pygame.mouse.get_pos = self._get_mouse_pos
+
+ def update_display(self):
+ pygame.event.post(pygame.event.Event(pygame.VIDEOEXPOSE))
+
+ def _expose_cb(self, widget, event):
+ if pygame.display.get_init():
+ pygame.event.post(pygame.event.Event(pygame.VIDEOEXPOSE))
+ return True
+
+ def _resize_cb(self, widget, event):
+ evt = pygame.event.Event(pygame.VIDEORESIZE,
+ size=(event.width,event.height), width=event.width, height=event.height)
+ pygame.event.post(evt)
+ return False # continue processing
+
+ def _screen_changed_cb(self, widget, screen):
+ if pygame.display.get_init():
+ self.update_display()
+
+ def _quit_cb(self, data=None):
+ self.__stopped = True
+ pygame.event.post(pygame.event.Event(pygame.QUIT))
+
+ def _visibility_cb(self, widget, event):
+ if pygame.display.get_init():
+ self.update_display()
+ return False
+
+ def _keydown_cb(self, widget, event):
+ key = event.keyval
+ if key in self.__held:
+ return True
+ else:
+ if self.__repeat[0] is not None:
+ self.__held_last_time[key] = pygame.time.get_ticks()
+ self.__held_time_left[key] = self.__repeat[0]
+ self.__held.add(key)
+
+ return self._keyevent(widget, event, pygame.KEYDOWN)
+
+ def _keyup_cb(self, widget, event):
+ key = event.keyval
+ if self.__repeat[0] is not None:
+ if key in self.__held:
+ # This is possibly false if set_repeat() is called with a key held
+ del self.__held_time_left[key]
+ del self.__held_last_time[key]
+ self.__held.discard(key)
+
+ return self._keyevent(widget, event, pygame.KEYUP)
+
+ def _keymods(self):
+ mod = 0
+ for key_val, mod_val in self.mod_map.iteritems():
+ mod |= self.__keystate[key_val] and mod_val
+ return mod
+
+ def _keyevent(self, widget, event, type):
+ key = gtk.gdk.keyval_name(event.keyval)
+ if key is None:
+ # No idea what this key is.
+ return False
+
+ keycode = None
+ if key in self.key_trans:
+ keycode = self.key_trans[key]
+ elif hasattr(pygame, 'K_'+key.upper()):
+ keycode = getattr(pygame, 'K_'+key.upper())
+ elif hasattr(pygame, 'K_'+key.lower()):
+ keycode = getattr(pygame, 'K_'+key.lower())
+ elif key == 'XF86Start':
+ # view source request, specially handled...
+ self._mainwindow.view_source()
+ else:
+ print 'Key %s unrecognized' % key
+
+ if keycode is not None:
+ if type == pygame.KEYDOWN:
+ mod = self._keymods()
+ self.__keystate[keycode] = type == pygame.KEYDOWN
+ if type == pygame.KEYUP:
+ mod = self._keymods()
+ ukey = unichr(gtk.gdk.keyval_to_unicode(event.keyval))
+ if ukey == '\000':
+ ukey = ''
+ evt = pygame.event.Event(type, key=keycode, unicode=ukey, mod=mod)
+ self._post(evt)
+
+ return True
+
+ def _get_pressed(self):
+ return self.__keystate
+
+ def _get_mouse_pressed(self):
+ return self.__button_state
+
+ def _mousedown_cb(self, widget, event):
+ self.__button_state[event.button-1] = 1
+ return self._mouseevent(widget, event, pygame.MOUSEBUTTONDOWN)
+
+ def _mouseup_cb(self, widget, event):
+ self.__button_state[event.button-1] = 0
+ return self._mouseevent(widget, event, pygame.MOUSEBUTTONUP)
+
+ def _mouseevent(self, widget, event, type):
+ evt = pygame.event.Event(type, button=event.button, pos=(event.x, event.y))
+ self._post(evt)
+ return True
+
+ def _mousemove_cb(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
+ # information, if not it's all we need.
+ if event.is_hint:
+ x, y, state = event.window.get_pointer()
+ else:
+ x = event.x
+ y = event.y
+ state = event.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,
+ ]
+
+ evt = pygame.event.Event(pygame.MOUSEMOTION,
+ pos=self.__mouse_pos, rel=rel, buttons=self.__button_state)
+ self._post(evt)
+ return True
+
+ def _tick_cb(self):
+ cur_time = pygame.time.get_ticks()
+ for key in self.__held:
+ delta = cur_time - self.__held_last_time[key]
+ self.__held_last_time[key] = cur_time
+
+ self.__held_time_left[key] -= delta
+ if self.__held_time_left[key] <= 0:
+ self.__held_time_left[key] = self.__repeat[1]
+ self._keyevent(None, _MockEvent(key), pygame.KEYDOWN)
+
+ return True
+
+ 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)
+ elif delay is None and self.__repeat[0] is not None:
+ gobject.source_remove(self.__tick_id)
+ self.__repeat = (delay, interval)
+
+ def _get_mouse_pos(self):
+ return self.__mouse_pos
+
+ def _post(self, evt):
+ try:
+ pygame.event.post(evt)
+ except pygame.error, e:
+ if str(e) == 'Event queue full':
+ print "Event queue full!"
+ pass
+ else:
+ raise e