Implement translation

This commit is contained in:
Marco Pesenti Gritti
2006-07-24 11:01:25 +02:00
parent 1c35f8d92c
commit 1e3633baf7
6 changed files with 42 additions and 6 deletions
+1 -1
View File
@@ -13,5 +13,5 @@ class Actor:
self._width = width
self._height = height
def render(self, window):
def render(self, window, transf):
pass
+9 -2
View File
@@ -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)
+3 -2
View File
@@ -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)
+6
View File
@@ -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)
+21
View File
@@ -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