Implémentation du clic sur le bouton B.
[minwii.git] / src / pgu / gui / basic.py
1 """These widgets are all grouped together because they are non-interactive widgets.
2 """
3
4 import pygame
5
6 from const import *
7 import widget
8
9 # Turns a descriptive string or a tuple into a pygame color
10 def parse_color(desc):
11 if (is_color(desc)):
12 # Already a color
13 return desc
14 elif (desc and desc[0] == "#"):
15 # Because of a bug in pygame 1.8.1 we need to explicitly define the
16 # alpha value otherwise it will default to transparent.
17 if (len(desc) == 7):
18 desc += "FF"
19 return pygame.Color(desc)
20
21 # Determines if the given object is a pygame-compatible color or not
22 def is_color(col):
23 # In every version of pygame (up to 1.8.1 so far) will interpret
24 # a tuple as a color.
25 if (type(col) == tuple):
26 return col
27 if (hasattr(pygame, "Color") and type(pygame.Color) == type):
28 # This is a recent version of pygame that uses a proper type
29 # instance for colors.
30 return (isinstance(col, pygame.Color))
31 # Otherwise, this version of pygame only supports tuple colors
32 return False
33
34 class Spacer(widget.Widget):
35 """A invisible space.
36
37 <pre>Spacer(width,height)</pre>
38
39 """
40 def __init__(self,width,height,**params):
41 params.setdefault('focusable',False)
42 widget.Widget.__init__(self,width=width,height=height,**params)
43
44
45 class Color(widget.Widget):
46 """A block of color.
47
48 <p>The color can be changed at run-time.</p>
49
50 <pre>Color(value=None)</pre>
51
52 <strong>Example</strong>
53 <code>
54 c = Color()
55 c.value = (255,0,0)
56 c.value = (0,255,0)
57 </code>
58 """
59
60
61 def __init__(self,value=None,**params):
62 params.setdefault('focusable',False)
63 if value != None: params['value']=value
64 widget.Widget.__init__(self,**params)
65
66 def paint(self,s):
67 if hasattr(self,'value'): s.fill(self.value)
68
69 def __setattr__(self,k,v):
70 if k == 'value' and type(v) == str:
71 v = parse_color(v)
72 _v = self.__dict__.get(k,NOATTR)
73 self.__dict__[k]=v
74 if k == 'value' and _v != NOATTR and _v != v:
75 self.send(CHANGE)
76 self.repaint()
77
78 class Label(widget.Widget):
79 """A text label.
80
81 <pre>Label(value)</pre>
82
83 <dl>
84 <dt>value<dd>text to be displayed
85 </dl>
86
87 <strong>Example</strong>
88 <code>
89 w = Label(value="I own a rubber chicken!")
90
91 w = Label("3 rubber chickens")
92 </code>
93 """
94 def __init__(self,value,**params):
95 params.setdefault('focusable',False)
96 params.setdefault('cls','label')
97 widget.Widget.__init__(self,**params)
98 self.value = value
99 self.font = self.style.font
100 self.style.width, self.style.height = self.font.size(self.value)
101
102 def paint(self,s):
103 s.blit(self.font.render(self.value, 1, self.style.color),(0,0))
104
105 class Image(widget.Widget):
106 """An image.
107
108 <pre>Image(value)</pre>
109
110 <dl>
111 <dt>value<dd>a file name or a pygame.Surface
112 </dl>
113
114 """
115 def __init__(self,value,**params):
116 params.setdefault('focusable',False)
117 widget.Widget.__init__(self,**params)
118 if type(value) == str: value = pygame.image.load(value)
119
120 ow,oh = iw,ih = value.get_width(),value.get_height()
121 sw,sh = self.style.width,self.style.height
122
123 if sw and not sh:
124 iw,ih = sw,ih*sw/iw
125 elif sh and not sw:
126 iw,ih = iw*sh/ih,sh
127 elif sw and sh:
128 iw,ih = sw,sh
129
130 if (ow,oh) != (iw,ih):
131 value = pygame.transform.scale(value,(iw,ih))
132 self.style.width,self.style.height = iw,ih
133 self.value = value
134
135 def paint(self,s):
136 s.blit(self.value,(0,0))