The following example demonstrates the implementation of a new triangle Actor type.
File: main.py
import sys
import clutter
from triangle import Triangle
def main():
""""""
stage_color = clutter.Color(0, 0, 0, 255)
actor_color = clutter.Color(255, 255, 255, 153)
# Get the stage and set its size and color
stage = clutter.Stage()
stage.set_size(200, 200)
stage.set_color(stage_color)
# Add our custom actor to the stage
actor = Triangle(actor_color)
actor.set_size(100, 100)
actor.set_position(20, 20)
stage.add(actor)
actor.show()
# Show the stage
stage.show()
stage.connect('destroy', clutter.main_quit)
#actor.set_color('Red')
# Start the main loop, so we can respond to events
clutter.main()
return 0
if __name__ == '__main__':
sys.exit(main())
File: triangle.py
import gobject
import clutter
from clutter import cogl
class Triangle(clutter.Actor):
__gtype_name__ = 'Triangle'
__gproperties__ = {
'color' : (
str, 'color', 'Color', 'The color of the triangle', gobject.PARAM_READWRITE,
),
}
def __init__(self, color=None):
super(Triangle, self).__init__()
if color is not None:
self._color = color
def do_set_property (self, pspec, value):
if pspec.name == 'color':
self._color = clutter.color_from_string(value)
else:
raise TypeError('Unknown property %s' % pspec.name)
def do_get_property (self, pspec):
if pspec.name == 'color':
return self._color
else:
raise TypeError('Unknown property %s' % pspec.name)
def do_paint (self):
# Paint the triangle with the actor's color
paint_color = self._color
real_alpha = self.get_paint_opacity() * paint_color.alpha / 255
paint_color.alpha = real_alpha
self.__paint_triangle(paint_color)
def __paint_triangle (self, color):
cogl.push_matrix()
cogl.set_source_color(color)
width, height = self.get_geometry()[2:]
# Paint a triangle
cogl.path_polygon(0, 0, 0, height, width, height)
cogl.path_fill()
cogl.pop_matrix()
def do_pick (self, pick_color):
if self.should_pick_paint():
self.__paint_triangle(pick_color)
def get_color(self):
return self.color
def set_color(self, color):
self._color = clutter.color_from_string(color)
self.set_opacity(self._color.alpha)
if self.props.visible:
clutter.Actor.queue_redraw(self)
gobject.type_register(Triangle)