diff --git a/examples/scene/scene.py b/examples/scene/scene.py index 7a6fcdbe..b54c40c9 100755 --- a/examples/scene/scene.py +++ b/examples/scene/scene.py @@ -33,6 +33,7 @@ pixbuf = gtk.gdk.pixbuf_new_from_file('background.png') stage.add(PixbufActor(pixbuf)) icons_group = Group() +icons_group.set_position(100, 100) i = 1 while i <= 5: @@ -55,7 +56,7 @@ drawing_area.show() window.show() -timeline = Timeline(stage, 300) +timeline = Timeline(stage, 200) timeline.connect('next-frame', __next_frame_cb, icons_group) timeline.connect('completed', __completed_cb, icons_group) timeline.start() diff --git a/sugar/scene/Actor.py b/sugar/scene/Actor.py index 1ee7e2d5..24074da4 100644 --- a/sugar/scene/Actor.py +++ b/sugar/scene/Actor.py @@ -13,5 +13,5 @@ class Actor: self._width = width self._height = height - def render(self, window): + def render(self, window, transf): pass diff --git a/sugar/scene/Group.py b/sugar/scene/Group.py index 8a2a5623..3cfa5e4d 100644 --- a/sugar/scene/Group.py +++ b/sugar/scene/Group.py @@ -1,9 +1,15 @@ from sugar.scene.Actor import Actor +from sugar.scene.Transformation import Transformation class Group(Actor): def __init__(self): self._actors = [] self._layout = None + self._transf = Transformation() + + def set_position(self, x, y): + Actor.set_position(self, x, y) + self._transf.set_translation(x, y) def add(self, actor): self._actors.append(actor) @@ -27,6 +33,7 @@ class Group(Actor): if self._layout: self._layout.layout_group(self) - def render(self, drawable): + def render(self, drawable, transf): + transf = transf.compose(self._transf) for actor in self._actors: - actor.render(drawable) + actor.render(drawable, transf) diff --git a/sugar/scene/PixbufActor.py b/sugar/scene/PixbufActor.py index 00ed5871..59b3dcea 100644 --- a/sugar/scene/PixbufActor.py +++ b/sugar/scene/PixbufActor.py @@ -8,6 +8,7 @@ class PixbufActor(Actor): self._pixbuf = pixbuf - def render(self, drawable): + def render(self, drawable, transf): + (x, y) = transf.get_position(self._x, self._y) gc = gtk.gdk.GC(drawable) - drawable.draw_pixbuf(gc, self._pixbuf, 0, 0, self._x, self._y) + drawable.draw_pixbuf(gc, self._pixbuf, 0, 0, x, y) diff --git a/sugar/scene/Stage.py b/sugar/scene/Stage.py index 1707f845..2573d026 100644 --- a/sugar/scene/Stage.py +++ b/sugar/scene/Stage.py @@ -1,4 +1,5 @@ from sugar.scene.Group import Group +from sugar.scene.Transformation import Transformation class Stage(Group): def __init__(self): @@ -7,3 +8,8 @@ class Stage(Group): def get_fps(self): return self._fps + + def render(self, drawable, transf = None): + if transf == None: + transf = Transformation() + Group.render(self, drawable, transf) diff --git a/sugar/scene/Transformation.py b/sugar/scene/Transformation.py new file mode 100644 index 00000000..452c599f --- /dev/null +++ b/sugar/scene/Transformation.py @@ -0,0 +1,21 @@ +import copy + +class Transformation: + def __init__(self): + self._translation_x = 0 + self._translation_y = 0 + + def set_translation(self, x, y): + self._translation_x = x + self._translation_y = y + + def get_position(self, x, y): + translated_x = x + self._translation_x + translated_y = y + self._translation_y + return (translated_x, translated_y) + + def compose(self, transf): + composed = copy.copy(transf) + composed._translation_x += transf._translation_x + composed._translation_y += transf._translation_y + return composed