Mercurial > fife-parpg
comparison engine/extensions/pychan/widgets/buttons.py @ 248:a2d5e2721489
widgets.py split up.
author | phoku@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Thu, 26 Mar 2009 16:20:16 +0000 |
parents | |
children | 51cc05d862f2 |
comparison
equal
deleted
inserted
replaced
247:040387b7167f | 248:a2d5e2721489 |
---|---|
1 # -*- coding: utf-8 -*- | |
2 | |
3 from common import * | |
4 from basictextwidget import BasicTextWidget | |
5 | |
6 class Button(BasicTextWidget): | |
7 """ | |
8 A basic push button. | |
9 """ | |
10 def __init__(self,**kwargs): | |
11 self.real_widget = fife.Button("") | |
12 super(Button,self).__init__(**kwargs) | |
13 | |
14 class ImageButton(BasicTextWidget): | |
15 """ | |
16 A basic push button with three different images for the up, down and hover state. | |
17 | |
18 B{Work in progress.} | |
19 | |
20 New Attributes | |
21 ============== | |
22 | |
23 - up_image: String: The source location of the Image for the B{unpressed} state. | |
24 - down_image: String: The source location of the Image for the B{pressed} state. | |
25 - hover_image: String: The source location of the Image for the B{unpressed hovered} state. | |
26 """ | |
27 | |
28 ATTRIBUTES = BasicTextWidget.ATTRIBUTES + [Attr('up_image'),Attr('down_image'),PointAttr('offset'),Attr('hover_image')] | |
29 | |
30 def __init__(self,up_image="",down_image="",hover_image="",offset=(0,0),**kwargs): | |
31 self.real_widget = fife.TwoButton() | |
32 super(ImageButton,self).__init__(**kwargs) | |
33 | |
34 self.up_image = up_image | |
35 self.down_image = down_image | |
36 self.hover_image = hover_image | |
37 self.offset = offset | |
38 | |
39 def _setUpImage(self, source): | |
40 if isinstance(source,str): | |
41 self._upimage_source = source | |
42 try: | |
43 self._upimage = get_manager().loadImage(source) | |
44 self.real_widget.setUpImage( self._upimage ) | |
45 except: | |
46 self._upimage = DummyImage() | |
47 elif isinstance(source,fife.GuiImage): | |
48 self._upimage_source = None | |
49 self._upimage = source | |
50 self.real_widget.setUpImage( self._upimage ) | |
51 else: | |
52 raise RuntimeError("Icon.image only accepts GuiImage and python strings, not '%s'" % repr(source)) | |
53 | |
54 # Set minimum size accoriding to image | |
55 self.min_size = self.real_widget.getWidth(),self.real_widget.getHeight() | |
56 self.size = self.max_size = self.min_size | |
57 | |
58 def _getUpImage(self): return self._upimage_source | |
59 up_image = property(_getUpImage,_setUpImage) | |
60 | |
61 def _setDownImage(self, source): | |
62 if isinstance(source,str): | |
63 self._downimage_source = source | |
64 try: | |
65 self._downimage = get_manager().loadImage(source) | |
66 self.real_widget.setDownImage( self._downimage ) | |
67 except: | |
68 self._downimage = DummyImage() | |
69 elif isinstance(source,fife.GuiImage): | |
70 self._downimage_source = None | |
71 self._downimage = source | |
72 self.real_widget.setDownImage( self._downimage ) | |
73 else: | |
74 raise RuntimeError("Icon.image only accepts GuiImage and python strings, not '%s'" % repr(source)) | |
75 | |
76 def _getDownImage(self): return self._downimage_source | |
77 down_image = property(_getDownImage,_setDownImage) | |
78 | |
79 def _setHoverImage(self, source): | |
80 if isinstance(source, str): | |
81 self._hoverimage_source = source | |
82 try: | |
83 self._hoverimage = get_manager().loadImage(source) | |
84 self.real_widget.setHoverImage( self._hoverimage ) | |
85 except: | |
86 self._hoverimage = DummyImage() | |
87 elif isinstance(source,fife.GuiImage): | |
88 self._hoverimage_source = None | |
89 self._hoverimage = source | |
90 self.real_widget.setHoverImage( self._hoverimage ) | |
91 else: | |
92 raise RuntimeError("Icon.image only accepts GuiImage and python strings, not '%s'" % repr(source)) | |
93 | |
94 def _getHoverImage(self): return self._hoverimage_source | |
95 hover_image = property(_getHoverImage,_setHoverImage) | |
96 | |
97 def _setOffset(self, offset): | |
98 self.real_widget.setDownOffset(offset[0], offset[1]) | |
99 def _getOffset(self): | |
100 return (self.real_widget.getDownXOffset(), self.real_widget.getDownYOffset()) | |
101 offset = property(_getOffset,_setOffset) | |
102 | |
103 def resizeToContent(self): | |
104 self.height = max(self._upimage.getHeight(),self._downimage.getHeight(),self._hoverimage.getHeight()) + self.margins[1]*2 | |
105 self.width = max(self._upimage.getWidth(),self._downimage.getWidth(),self._hoverimage.getWidth()) + self.margins[1]*2 | |
106 | |
107 class ToggleButton(BasicTextWidget): | |
108 """ | |
109 A basic push button that can be toggled. | |
110 | |
111 Unfortunately a bit of code duplication from ImageButton. | |
112 | |
113 New Attributes | |
114 ============== | |
115 | |
116 - group: String: The group the button belongs to. Only one button in each group will be toggled at one time. | |
117 - toggled: Boolean: Whether the button is toggled or not. | |
118 """ | |
119 | |
120 ATTRIBUTES = BasicTextWidget.ATTRIBUTES + [ | |
121 Attr('up_image'),Attr('down_image'),Attr('hover_image'), | |
122 PointAttr('offset'),Attr('group') | |
123 ] | |
124 | |
125 def __init__(self,up_image="",down_image="",hover_image="",offset=(0,0),group="",**kwargs): | |
126 | |
127 self.real_widget = fife.ToggleButton() | |
128 super(ToggleButton,self).__init__(**kwargs) | |
129 self.group = group | |
130 self.up_image = up_image | |
131 self.down_image = down_image | |
132 self.hover_image = hover_image | |
133 self.offset = offset | |
134 | |
135 def _setGroup(self,group): | |
136 self.real_widget.setGroup( group ) | |
137 | |
138 def _getGroup(self): | |
139 return self.real_widget.getGroup() | |
140 group = property(_getGroup,_setGroup) | |
141 | |
142 def _setToggled(self, toggled): | |
143 self.real_widget.setToggled( toggled ) | |
144 | |
145 def _isToggled(self): | |
146 return self.real_widget.isToggled() | |
147 toggled = property(_isToggled, _setToggled) | |
148 | |
149 ### | |
150 # I didn't want to do this, but this is just cut and paste from the ImageButton class: | |
151 ### | |
152 | |
153 def _setUpImage(self,image): | |
154 self._upimage_source = image | |
155 try: | |
156 self._upimage = get_manager().loadImage(image) | |
157 self.real_widget.setUpImage( self._upimage ) | |
158 except: | |
159 self._upimage = DummyImage() | |
160 def _getUpImage(self): return self._upimage_source | |
161 up_image = property(_getUpImage,_setUpImage) | |
162 | |
163 def _setDownImage(self,image): | |
164 self._downimage_source = image | |
165 try: | |
166 self._downimage = get_manager().loadImage(image) | |
167 self.real_widget.setDownImage( self._downimage ) | |
168 except: | |
169 self._downimage = DummyImage() | |
170 def _getDownImage(self): return self._downimage_source | |
171 down_image = property(_getDownImage,_setDownImage) | |
172 | |
173 def _setHoverImage(self,image): | |
174 self._hoverimage_source = image | |
175 try: | |
176 self._hoverimage = get_manager().loadImage(image) | |
177 self.real_widget.setHoverImage( self._hoverimage ) | |
178 except: | |
179 self._hoverimage = DummyImage() | |
180 def _getHoverImage(self): return self._hoverimage_source | |
181 hover_image = property(_getHoverImage,_setHoverImage) | |
182 | |
183 def _setOffset(self, offset): | |
184 self.real_widget.setDownOffset(offset[0], offset[1]) | |
185 def _getOffset(self): | |
186 return (self.real_widget.getDownXOffset(), self.real_widget.getDownYOffset()) | |
187 offset = property(_getOffset,_setOffset) | |
188 | |
189 def resizeToContent(self): | |
190 self.height = max(self._upimage.getHeight(),self._downimage.getHeight(),self._hoverimage.getHeight()) + self.margins[1]*2 | |
191 self.width = max(self._upimage.getWidth(),self._downimage.getWidth(),self._hoverimage.getWidth()) + self.margins[1]*2 |