Add layout manager and a circle layout
This commit is contained in:
parent
a02313d85a
commit
fe69904b6c
@ -7,6 +7,7 @@ import gtk
|
||||
from sugar.scene.Stage import Stage
|
||||
from sugar.scene.Group import Group
|
||||
from sugar.scene.PixbufActor import PixbufActor
|
||||
from sugar.scene.CircleLayout import CircleLayout
|
||||
|
||||
def drawing_area_expose_cb(widget, event, stage):
|
||||
stage.render(widget.window)
|
||||
@ -24,6 +25,9 @@ while i <= 5:
|
||||
icons_group.add(PixbufActor(pixbuf))
|
||||
i += 1
|
||||
|
||||
layout = CircleLayout(100)
|
||||
icons_group.set_layout_manager(layout)
|
||||
|
||||
stage.add(icons_group)
|
||||
|
||||
window = gtk.Window()
|
||||
|
21
sugar/scene/CircleLayout.py
Normal file
21
sugar/scene/CircleLayout.py
Normal 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))
|
@ -3,12 +3,24 @@ from sugar.scene.Actor import Actor
|
||||
class Group(Actor):
|
||||
def __init__(self):
|
||||
self._actors = []
|
||||
self._layout_manager = None
|
||||
|
||||
def add(self, actor):
|
||||
self._actors.append(actor)
|
||||
if self._layout_manager:
|
||||
self._layout_manager.layout_group(slef)
|
||||
|
||||
def remove(self, 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):
|
||||
for actor in self._actors:
|
||||
|
7
sugar/scene/LayoutManager.py
Normal file
7
sugar/scene/LayoutManager.py
Normal file
@ -0,0 +1,7 @@
|
||||
class LayoutManager:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def layout_group(self, group):
|
||||
pass
|
||||
|
Loading…
Reference in New Issue
Block a user