Sharedwww / talks / 2006-05-09-sage-digipen / tutorial / pudding-2.pyOpen in CoCalc
Author: William A. Stein
1
#!/usr/bin/env python
2
3
"""
4
this is basic pudding script demonstrating anchors and containers
5
"""
6
7
import sys, os
8
9
import soya
10
import soya.pudding as pudding
11
12
soya.init()
13
soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))
14
15
# initialise pudding
16
pudding.init()
17
18
scene = soya.World()
19
20
sword_model = soya.Shape.get("sword")
21
sword = soya.Volume(scene, sword_model)
22
sword.x = 1
23
sword.rotate_lateral(90.)
24
25
light = soya.Light(scene)
26
light.set_xyz( .5, 0., 2.)
27
28
camera = soya.Camera(scene)
29
camera.z = 2.
30
31
# we use a special root widget as a small compatability layer
32
33
# the width and height can actually be anything you want if using
34
# anchors as the widgets will be resized.
35
# basically, if you set here the width and height at 640,480
36
# and then layout your components based on that
37
# when the app is run the components should be positined correctly
38
# whatever the window size
39
40
w = pudding.core.RootWidget(width = 640,height = 480)
41
42
# create a vertical container
43
c = pudding.container.VerticalContainer(w, left=10, top=10, width=300, height=200)
44
c.right = 10
45
46
# here we' set the anchors. anchors make resizable screen components a doddle
47
# here we anchor all but the bottom to make a sort of title bar
48
c.anchors = pudding.ANCHOR_RIGHT | pudding.ANCHOR_TOP | pudding.ANCHOR_LEFT
49
50
# add a button
51
# the last argument indicates if the object is free to expand with the container
52
d = c.add_child(pudding.control.Button(label = 'Button'), 1)
53
# set the background color for show
54
d.background_color = (0., 1., 0., 1.)
55
56
# add another button but this time specifiy that its not free to grow excpet in the
57
# horizontal
58
d = c.add_child(pudding.control.Input(height = 40, initial = 'input'), pudding.EXPAND_HORIZ)
59
d.background_color = (1., 0., 0., 1.)
60
61
# add another container
62
# this time with one exapanding button and one button that doesnt expand at all
63
c = pudding.container.HorizontalContainer(w, left=10, top=210, width=300, height=200)
64
c.bottom = 10
65
66
# this time we just anchor one side
67
c.anchors = pudding.ANCHOR_BOTTOM
68
69
d = c.add_child(pudding.control.Button(label = 'Button'), pudding.EXPAND_BOTH)
70
d.background_color = (0., 1., 0., 1.)
71
d = c.add_child(pudding.control.Button(width = 20,height = 20, label = ''))
72
d.background_color = (1., 0., 0., 1.)
73
74
w.add_child(camera)
75
76
soya.set_root_widget(w)
77
78
pudding.idler.Idler(scene).idle()
79
80