Sharedwww / talks / 2006-05-09-sage-digipen / tutorial / pudding-menu-1.pyOpen in CoCalc
Author: William A. Stein
1
#!/usr/bin/env python
2
3
"""
4
a test showing how simple components can be used to build more complex
5
controls
6
"""
7
8
import sys, os
9
10
import soya
11
import soya.pudding as pudding
12
13
soya.init(width = 320, height = 200)
14
soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data"))
15
16
# initialise pudding
17
pudding.init()
18
19
scene = soya.World()
20
21
sword_model = soya.Shape.get("sword")
22
sword = soya.Volume(scene, sword_model)
23
sword.y = -1
24
sword.x = -1.5
25
sword.rotate_vertical(90.)
26
27
# one line animation :)
28
sword.advance_time = lambda p: sword.rotate_lateral(5.*p)
29
30
light = soya.Light(scene)
31
light.set_xyz( .5, 0., 2.)
32
33
camera = soya.Camera(scene)
34
camera.z = 3.
35
36
# here we construct a simple menu item widget that highlights
37
# when it gets focus and calls a function when its clicked
38
39
class MenuOption(pudding.control.Label):
40
def __init__(self, label, function ):
41
pudding.control.Label.__init__(self, label = label)
42
43
self.function = function
44
45
self.color1 = (1., 1., 1., 1.)
46
self.color2 = (1., 0., 0., 1.)
47
48
def on_focus(self):
49
self.color = self.color2
50
51
def on_loose_focus(self):
52
self.color = self.color1
53
54
def on_mouse_up(self, button, x, y):
55
print "click on %s" % self.label
56
57
self.function()
58
59
# we must return True here to stop even propgation
60
return True
61
62
# some pretend menu functions
63
64
def go_game():
65
print "starting game..."
66
67
def go_menu():
68
print "starting menu..."
69
70
def go_quit():
71
print "quitting..."
72
73
sys.exit()
74
75
w = pudding.core.RootWidget(width = 640,height = 480)
76
77
# make a vertical container to put our menu items in
78
vc = pudding.container.VerticalContainer(w, width = 10, height = 50)
79
print vc.child
80
81
# make use of the right and bottom properties so we dont need
82
# to think about width and height
83
vc.right = 10
84
vc.bottom = 10
85
86
# anchor the whole container so it stays the same size whatever
87
# the window size
88
vc.anchors = pudding.ANCHOR_ALL
89
90
# add some padding so our menu items are not too close together
91
vc.padding = 15
92
93
# what we have now is a big invisible column covering the whole screen
94
# we will add the menu items, telling the container that they are a
95
# fixed size and should not be expanded. This will result in all
96
# the items being neatly underneath each other. Behind the scenes
97
# the Label control by default will adjust its width and height
98
# based on its content. So to center the text we simply need to
99
# pass the CENTER_HORIZ flag to the container
100
101
op1 = vc.add_child( MenuOption('Game', go_game ), pudding.CENTER_HORIZ)
102
op2 = vc.add_child( MenuOption('Menu', go_menu ), pudding.CENTER_HORIZ)
103
op3 = vc.add_child( MenuOption('Quit', go_quit ), pudding.CENTER_HORIZ)
104
105
# you can get and set options after creation too
106
print vc.get_child_options(op3)
107
108
w.add_child(camera)
109
110
soya.set_root_widget(w)
111
112
pudding.idler.Idler(scene).idle()
113
114