Implement translation
This commit is contained in:
parent
1c35f8d92c
commit
1e3633baf7
@ -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()
|
||||
|
@ -13,5 +13,5 @@ class Actor:
|
||||
self._width = width
|
||||
self._height = height
|
||||
|
||||
def render(self, window):
|
||||
def render(self, window, transf):
|
||||
pass
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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
sugar/scene/Transformation.py
Normal file
21
sugar/scene/Transformation.py
Normal 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
|
Loading…
Reference in New Issue
Block a user