Add layout manager and a circle layout

This commit is contained in:
Marco Pesenti Gritti 2006-07-22 12:28:59 +02:00
parent a02313d85a
commit fe69904b6c
4 changed files with 44 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import gtk
from sugar.scene.Stage import Stage from sugar.scene.Stage import Stage
from sugar.scene.Group import Group from sugar.scene.Group import Group
from sugar.scene.PixbufActor import PixbufActor from sugar.scene.PixbufActor import PixbufActor
from sugar.scene.CircleLayout import CircleLayout
def drawing_area_expose_cb(widget, event, stage): def drawing_area_expose_cb(widget, event, stage):
stage.render(widget.window) stage.render(widget.window)
@ -24,6 +25,9 @@ while i <= 5:
icons_group.add(PixbufActor(pixbuf)) icons_group.add(PixbufActor(pixbuf))
i += 1 i += 1
layout = CircleLayout(100)
icons_group.set_layout_manager(layout)
stage.add(icons_group) stage.add(icons_group)
window = gtk.Window() window = gtk.Window()

View File

@ -0,0 +1,21 @@
import math
from sugar.scene.LayoutManager import LayoutManager
class CircleLayout(LayoutManager):
def __init__(self, radium):
LayoutManager.__init__(self)
self._radium = radium
def layout_group(self, group):
step = 2 * math.pi / len(group.get_actors())
angle = 2 * math.pi
for actor in group.get_actors():
self._update_position(actor, angle)
angle -= step
def _update_position(self, actor, angle):
x = math.cos(angle) * self._radium + self._radium
y = math.sin(angle) * self._radium + self._radium
actor.set_position(int(x), int(y))

View File

@ -3,12 +3,24 @@ from sugar.scene.Actor import Actor
class Group(Actor): class Group(Actor):
def __init__(self): def __init__(self):
self._actors = [] self._actors = []
self._layout_manager = None
def add(self, actor): def add(self, actor):
self._actors.append(actor) self._actors.append(actor)
if self._layout_manager:
self._layout_manager.layout_group(slef)
def remove(self, actor): def remove(self, actor):
self._actors.remove(actor) self._actors.remove(actor)
if self._layout_manager:
self._layout_manager.layout_group(self)
def get_actors(self):
return self._actors
def set_layout_manager(self, layout_manager):
self._layout_manager = layout_manager
self._layout_manager.layout_group(self)
def render(self, drawable): def render(self, drawable):
for actor in self._actors: for actor in self._actors:

View File

@ -0,0 +1,7 @@
class LayoutManager:
def __init__(self):
pass
def layout_group(self, group):
pass