/
Button.py
296 lines (242 loc) · 10.8 KB
/
Button.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2010 British Broadcasting Corporation and Kamaelia Contributors(1)
#
# (1) Kamaelia Contributors are listed in the AUTHORS file and at
# http://www.kamaelia.org/AUTHORS - please extend this file,
# not this notice.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -------------------------------------------------------------------------
"""\
====================
OpenGL Button Widget
====================
A button widget for the OpenGL display service. Sends a message when
clicked or an assigned key is pressed.
This component is a subclass of OpenGLComponent and therefore uses the
OpenGL display service.
Example Usage
-------------
4 Buttons which could be used for playback control (output to the console)::
Graphline(
BUTTON1 = Button(caption="<<", msg="Previous", position=(-3,0,-10)),
BUTTON2 = Button(caption=">>", msg="Next", position=(3,0,-10)),
BUTTON3 = Button(caption="Play", msg="Play", position=(-1,0,-10)),
BUTTON4 = Button(caption="Stop", msg="Stop", position=(1,0,-10)),
ECHO = ConsoleEchoer(),
linkages = {
(BUTTON1, "outbox") : (ECHO, "inbox"),
(BUTTON2, "outbox") : (ECHO, "inbox"),
(BUTTON3, "outbox") : (ECHO, "inbox"),
(BUTTON4, "outbox") : (ECHO, "inbox"),
}
).run()
How does it work?
-----------------
This component is a subclass of OpenGLComponent (for OpenGLComponent
functionality see its documentation). It overrides __init__(), setup(),
draw(), handleEvents() and frame().
In setup() it requests to receive mouse events and calls buildCaption()
where the set caption is rendered on a pygame surface. This surface is
then set as OpenGL texture.
In draw() a flat cuboid is drawn (if size is not specified) with the
caption texture on both the front and the back surface.
In handleEvents() the component reacts to mouse events and, if a key is
assigned, to the keydown event. If a mouse click happens when the mouse
pointer is over the button it gets "grabbed", which is visualised by
shrinking the button by a little amount, until the button is released
again. Only if the mouse button is released over the button widget it
gets activated. On activation the button gets rotated by 360 degrees
around the X axis.
In frame() the button gets rotated when it has been activated.
"""
import Axon
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGLComponent import OpenGLComponent
from Vector import Vector
from math import *
class Button(OpenGLComponent):
"""
Button(...) -> A new Button component.
A button widget for the OpenGL display service. Sends a message when
clicked or an assigned key is pressed.
Keyword arguments:
- caption -- Button caption (default="Button")
- bgcolour -- Colour of surfaces behind caption (default=(244,244,244))
- fgcolour -- Colour of the caption text (default=(0,0,0)
- sidecolour -- Colour of side planes (default=(200,200,244))
- margin -- Margin size in pixels (default=8)
- key -- Key to activate button (default=None)
- fontsize -- Font size for caption text (default=50)
- pixelscaling -- Factor to convert pixels to units in 3d, ignored if size is specified (default=100)
- thickness -- Thickness of button widget, ignored if size is specified (default=0.3)
- msg -- Message which gets sent when button is activated (default="CLICK")
"""
def __init__(self, **argd):
"""x.__init__(...) initializes x; see x.__class__.__doc__ for signature"""
super(Button, self).__init__(**argd)
self.grabbed = 0
# Button initialisation
self.caption = argd.get("caption", "Button")
self.backgroundColour = argd.get("bgcolour", (244,244,244))
self.foregroundColour = argd.get("fgcolour", (0,0,0))
self.sideColour = argd.get("sidecolour", (200,200,244))
self.margin = argd.get("margin", 8)
self.key = argd.get("key", None)
self.fontsize = argd.get("fontsize", 50)
self.pixelscaling = argd.get("pixelscaling", 100)
self.thickness = argd.get("thickness", 0.3)
self.eventMsg = argd.get("msg", "CLICK")
self.activated = False
self.actrot = 0
def setup(self):
""" Build caption and request reception of events."""
self.buildCaption()
self.addListenEvents( [pygame.MOUSEBUTTONDOWN, pygame.MOUSEBUTTONUP, pygame.KEYDOWN ])
def draw(self):
""" Draw button cuboid."""
hs = self.size/2.0
# draw faces
glBegin(GL_QUADS)
glColor4f(self.sideColour[0]/256.0, self.sideColour[1]/256.0, self.sideColour[2]/256.0, 0.5)
glVertex3f(hs.x,hs.y,hs.z)
glVertex3f(hs.x,-hs.y,hs.z)
glVertex3f(hs.x,-hs.y,-hs.z)
glVertex3f(hs.x,hs.y,-hs.z)
glVertex3f(-hs.x,hs.y,hs.z)
glVertex3f(-hs.x,-hs.y,hs.z)
glVertex3f(-hs.x,-hs.y,-hs.z)
glVertex3f(-hs.x,hs.y,-hs.z)
glVertex3f(hs.x,hs.y,hs.z)
glVertex3f(-hs.x,hs.y,hs.z)
glVertex3f(-hs.x,hs.y,-hs.z)
glVertex3f(hs.x,hs.y,-hs.z)
glVertex3f(hs.x,-hs.y,hs.z)
glVertex3f(-hs.x,-hs.y,hs.z)
glVertex3f(-hs.x,-hs.y,-hs.z)
glVertex3f(hs.x,-hs.y,-hs.z)
glEnd()
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, self.texID)
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE)
glBegin(GL_QUADS)
# back plane
glTexCoord2f(self.tex_w, 1.0-self.tex_h)
glVertex3f(hs.x,hs.y,-hs.z)
glTexCoord2f(0.0, 1.0-self.tex_h)
glVertex3f(-hs.x,hs.y,-hs.z)
glTexCoord2f(0.0, 1.0)
glVertex3f(-hs.x,-hs.y,-hs.z)
glTexCoord2f(self.tex_w, 1.0)
glVertex3f(hs.x,-hs.y,-hs.z)
# front plane
glTexCoord2f(0.0, 1.0-self.tex_h)
glVertex3f(-hs.x,-hs.y,hs.z)
glTexCoord2f(self.tex_w, 1.0-self.tex_h)
glVertex3f(hs.x,-hs.y,hs.z)
glTexCoord2f(self.tex_w, 1.0)
glVertex3f(hs.x,hs.y,hs.z)
glTexCoord2f(0.0, 1.0)
glVertex3f(-hs.x,hs.y,hs.z)
glEnd()
glDisable(GL_TEXTURE_2D)
def frame(self):
""" Rotate button if it has been activated. """
self.activationMovement()
def handleEvents(self):
""" Handle events. """
while self.dataReady("events"):
event = self.recv("events")
if event.type == pygame.KEYDOWN:
if event.key == self.key:
activate = True
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1 and self.identifier in event.hitobjects:
self.grabbed = event.button
self.scaling = Vector(0.9,0.9,0.9)
if event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
self.grabbed = 0
self.scaling = Vector(1,1,1)
#activate
if self.identifier in event.hitobjects:
self.send( self.eventMsg, "outbox" )
self.activated = True
def buildCaption(self):
"""Pre-render the text to go on the label."""
# Text is rendered to self.image
pygame.font.init()
font = pygame.font.Font(None, self.fontsize)
self.image = font.render(self.caption,True, self.foregroundColour, )
if self.size != Vector(0,0,0):
texsize = (self.size.x*self.pixelscaling, self.size.y*self.pixelscaling)
else:
texsize = ( self.image.get_width()+2*self.margin, self.image.get_height()+2*self.margin )
self.size=Vector(texsize[0]/float(self.pixelscaling), texsize[1]/float(self.pixelscaling), self.thickness)
# create power of 2 dimensioned surface
pow2size = (int(2**(ceil(log(texsize[0]+2*self.margin, 2)))), int(2**(ceil(log(texsize[1]+2*self.margin, 2)))))
textureSurface = pygame.Surface(pow2size)
textureSurface.fill( self.backgroundColour )
# determine texture coordinates
self.tex_w = float(texsize[0])/pow2size[0]
self.tex_h = float(texsize[1])/pow2size[1]
# copy image data to pow2surface
dest = ( max((texsize[0]-self.image.get_width())/2, 0), max((texsize[1]-self.image.get_height())/2, 0) )
textureSurface.blit(self.image, dest)
# textureSurface.set_alpha(128)
textureSurface = textureSurface.convert_alpha()
# read pixel data
textureData = pygame.image.tostring(textureSurface, "RGBX", 1)
self.texID = glGenTextures(1)
# create texture
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, self.texID)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, textureSurface.get_width(), textureSurface.get_height(), 0,
GL_RGBA, GL_UNSIGNED_BYTE, textureData );
glDisable(GL_TEXTURE_2D)
def activationMovement(self):
""" Rotate button stepwise by 360 degrees when it has been activated. """
if self.activated:
amount = 800.0*self.frametime
self.rotation.x += amount
self.rotation.x %= 360.0
self.actrot += amount
if self.actrot >= 360.0:
self.rotation.x -= (self.actrot-360.0)
self.actrot = 0.0
self.activated = False
__kamaelia_components__ = ( Button, )
if __name__=='__main__':
from Kamaelia.Util.Console import ConsoleEchoer
from Kamaelia.Chassis.Graphline import Graphline
Graphline(
BUTTON1 = Button(caption="<<", msg="Previous", position=(-3,0,-10)),
BUTTON2 = Button(caption=">>", msg="Next", position=(3,0,-10)),
BUTTON3 = Button(caption="Play", msg="Play", position=(-1,0,-10)),
BUTTON4 = Button(caption="Stop", msg="Stop", position=(1,0,-10)),
ECHO = ConsoleEchoer(),
linkages = {
("BUTTON1", "outbox") : ("ECHO", "inbox"),
("BUTTON2", "outbox") : ("ECHO", "inbox"),
("BUTTON3", "outbox") : ("ECHO", "inbox"),
("BUTTON4", "outbox") : ("ECHO", "inbox"),
}
).run()
# Licensed to the BBC under a Contributor Agreement: THF