7
by Jono Bacon
Mergin in our first snippet, a clutter example! :-) |
1 |
# [SNIPPET_NAME: Load an Image and detect click events]
|
6.1.1
by Oly
clutter example |
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]
|
|
6 |
||
7
by Jono Bacon
Mergin in our first snippet, a clutter example! :-) |
7 |
import clutter |
8 |
||
9 |
#create a clutter stage and set the display size
|
|
10 |
stage = clutter.Stage() |
|
11 |
stage.set_size(400, 400) |
|
12 |
||
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) |
|
15 |
||
16 |
#example create button from class start
|
|
17 |
||
18 |
class button(clutter.Texture): |
|
19 |
def __init__(self,id,row=1,padding=10): |
|
20 |
clutter.Texture.__init__(self) |
|
21 |
self.row=row |
|
22 |
self.id=id |
|
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) |
|
29 |
||
30 |
def clicked(self,stage, event): |
|
31 |
print "class click="+str(self.id)+" row "+str(self.row) |
|
32 |
||
33 |
||
34 |
#list of button class
|
|
35 |
buttonlist1=[] |
|
36 |
for i in range(0,10): |
|
37 |
buttonlist1.append(button(i)) |
|
38 |
||
39 |
#add the buttons to the stage
|
|
40 |
for b in buttonlist1: |
|
41 |
stage.add(b) |
|
42 |
||
43 |
#example create button from class end
|
|
44 |
||
45 |
#example class with collection of button class
|
|
46 |
class buttons: |
|
47 |
def __init__(self): |
|
48 |
self.buttonlist=[] |
|
49 |
self.count=0 |
|
50 |
for i in range(0,10): |
|
51 |
self.buttonlist.append(button(self.count,row=2)) |
|
52 |
#stage.add(self.buttonlist[self.count])
|
|
53 |
self.count+=1 |
|
54 |
||
55 |
#iter method so we can step through all buttons in a for loop
|
|
56 |
def __iter__(self): |
|
57 |
for i in self.buttonlist: |
|
58 |
yield i |
|
59 |
||
60 |
#append a new button
|
|
61 |
def append(self,btton): |
|
62 |
self.buttonlist.append(self.count) |
|
63 |
self.count+=1 |
|
64 |
||
65 |
#crate instance of buttons class
|
|
66 |
#append buttons class to stage
|
|
67 |
buttonlist2=buttons() |
|
68 |
for b in buttonlist2: |
|
69 |
stage.add(b) |
|
70 |
||
71 |
#example class with collection of button class end
|
|
72 |
||
73 |
#example of creating button with function calls start
|
|
74 |
||
75 |
#function call back on hit of button
|
|
76 |
def button_click(stage, event): |
|
77 |
print "no class test button press" |
|
78 |
||
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) |
|
85 |
||
86 |
#add non class button
|
|
87 |
stage.add(rectangle_actor) |
|
88 |
||
89 |
#example of creating button with function calls end
|
|
90 |
||
91 |
||
92 |
#show everything in the stage
|
|
93 |
stage.show_all() |
|
94 |
stage.connect("destroy",clutter.main_quit) |
|
95 |
#main clutter loop
|
|
96 |
clutter.main() |