1
# [SNIPPET_NAME: Load an Image and detect click events]
2
# [SNIPPET_CATEGORIES: Clutter]
3
# [SNIPPET_DESCRIPTION: Render a graphic using clutter and detect user click on the image, also demonstrates using the clutter library in a functional style and OO style]
4
# [SNIPPET_AUTHOR: Oliver Marks <oly@digitaloctave.com>]
5
# [SNIPPET_LICENSE: GPL]
9
#create a clutter stage and set the display size
10
stage = clutter.Stage()
11
stage.set_size(400, 400)
13
#load the image for the buttons
14
img=clutter.cogl.texture_new_from_file('button.png',clutter.cogl.TEXTURE_NO_SLICING, clutter.cogl.PIXEL_FORMAT_ANY)
16
#example create button from class start
18
class button(clutter.Texture):
19
def __init__(self,id,row=1,padding=10):
20
clutter.Texture.__init__(self)
23
self.set_size (100,100)
24
self.set_position (id*100,self.row*100)
25
self.set_cogl_texture(img)
26
self.set_reactive(True)
27
#call click method on button clicked
28
self.connect("button-press-event",self.clicked)
30
def clicked(self,stage, event):
31
print "class click="+str(self.id)+" row "+str(self.row)
37
buttonlist1.append(button(i))
39
#add the buttons to the stage
43
#example create button from class end
45
#example class with collection of button class
51
self.buttonlist.append(button(self.count,row=2))
52
#stage.add(self.buttonlist[self.count])
55
#iter method so we can step through all buttons in a for loop
57
for i in self.buttonlist:
61
def append(self,btton):
62
self.buttonlist.append(self.count)
65
#crate instance of buttons class
66
#append buttons class to stage
71
#example class with collection of button class end
73
#example of creating button with function calls start
75
#function call back on hit of button
76
def button_click(stage, event):
77
print "no class test button press"
79
rectangle_actor = clutter.Texture()
80
rectangle_actor.set_size (100,100)
81
rectangle_actor.set_cogl_texture(img)
82
rectangle_actor.set_reactive(True)
83
#call button_click function
84
rectangle_actor.connect("button-press-event", button_click)
87
stage.add(rectangle_actor)
89
#example of creating button with function calls end
92
#show everything in the stage
94
stage.connect("destroy",clutter.main_quit)