1 # Written by Jelle Roozenburg, Maarten ten Brinke
2 # see LICENSE.txt for license information
4 from traceback import print_exc
8 class PlayerButton(wx.Panel):
10 Button that changes the image shown if you move your mouse over it.
11 It redraws the background of the parent Panel, if this is an imagepanel with
12 a variable self.bitmap.
15 def __init__(self, parent, imagedir, imagename):
16 self.imagedir = imagedir
17 self.imagename = imagename
24 self.backgroundColor = wx.WHITE
25 wx.Panel.__init__(self, self.parent)
28 self.Bind(wx.EVT_MOUSE_EVENTS, self.mouseAction)
32 self.createBackgroundImage()
34 #<mluc> on mac, the button doesn't get a size
35 #if self.bitmaps[0] and self.GetSize()==(0,0):
37 self.SetSize(self.bitmaps[0].GetSize())
48 def GetImageName(self):
52 def searchBitmaps(self):
53 self.bitmaps = [None, None ,None]
54 self.parentBitmap = None
55 self.mouseOver = False
57 if not os.path.isdir(self.imagedir):
58 print 'Error: no image directory found in %s' % self.imagedir
61 # find a file with same name as this panel
62 self.bitmapPath = [os.path.join(self.imagedir, self.imagename+'.png'),
63 os.path.join(self.imagedir, self.imagename+'_hover.png'),
64 os.path.join(self.imagedir, self.imagename+'_dis.png')]
67 for img in self.bitmapPath:
68 if os.path.isfile(img):
69 self.bitmaps[i] = wx.Bitmap(img, wx.BITMAP_TYPE_ANY)
72 print 'Could not find image: %s' % img
77 def createBackgroundImage(self):
79 wx.EVT_PAINT(self, self.OnPaint)
80 self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnErase)
83 def OnErase(self, event):
87 def setSelected(self, sel):
94 def mouseAction(self, event):
100 elif event.Leaving():
101 self.mouseOver = False
106 def getParentBitmap(self):
108 parent = self.GetParent()
109 bitmap = parent.bitmap
115 location = self.GetPosition()
116 #location[0] -= parent.GetPosition()[0]
117 #location[1] -= parent.GetPosition()[1]
119 # print '(button %s) Mypos: %s, Parentpos: %s' % (self.GetName(), self.GetPosition(), parent.GetPosition())
120 rect = [location[0], location[1], self.GetClientSize()[0], self.GetClientSize()[1]]
122 # print '(button %s) Slicing rect(%d,%d) size(%s) from parent image size(%s)' % (self.GetName(), location[0], location[1], str(self.GetClientSize()), str(bitmap.GetSize()))
123 bitmap = self.getBitmapSlice(bitmap, rect)
128 def joinImage(self, im1,im2,offsetx=0,offsety=0):
130 stopx = im2.GetWidth()
131 if stopx > (im1.GetWidth()-offsetx):
132 stopx = im1.GetWidth()-offsetx
133 stopy = im2.GetHeight()
134 if stopy > (im1.GetHeight()-offsety):
135 stopy = im1.GetHeight()-offsety
136 if stopx>0 and stopy>0:
137 for x in range(0,stopx):
138 for y in range(0,stopy):
139 rgb2 = (im2.GetRed(x,y),im2.GetGreen(x,y),im2.GetBlue(x,y))
140 if rgb2 !=(255,0,255):
141 im1.SetRGB(x+offsetx,y+offsety,rgb2[0],rgb2[1],rgb2[2])
144 def getBitmapSlice(self, bitmap, rect):
147 bitmapSize = bitmap.GetSize()
148 rect[0] %= bitmapSize[0]
149 rect[1] %= bitmapSize[1]
151 if rect[0]+rect[2] > bitmapSize[0]:
152 rect1 = (rect[0], rect[1], bitmapSize[0]-rect[0], rect[3])
153 rect2 = (0, rect[1], rect[0]+rect[2] - bitmapSize[0], rect[3])
154 rects = [rect1, rect2]
155 if rect[1]+ rect[3] > bitmapSize[1]:
158 r1 = (r[0], r[1], r[2], bitmapSize[1] - r[3])
159 r2 = (r[0], 0, r[2], r[1]+r[3] - bitmapSize[1])
166 print "(button %s) Result: %s" % (self.GetName(), rects)
167 image = wx.EmptyImage(rect[2], rect[3])
169 rect = wx.Rect(r[0], r[1], r[2], r[3])
171 print '(button %s) Trying to get rect: %s from bitmap: %s' % (self.GetName(), rect, bitmap.GetSize())
172 subBitmap = bitmap.GetSubBitmap(rect)
173 subImage = subBitmap.ConvertToImage()
178 place = (rects[0][2], 0)
179 elif len(rects) == 4:
183 place = (0, rects[0][3])
185 place = (rects[0][2],0)
187 place = (rects[0][2], rects[0][3])
189 print "(button %s) Place subbitmap: %s" % (self.GetName(), str(place))
190 self.joinImage(image, subImage, place[0], place[1])
192 print '(button %s) Result img size: %s' % (self.GetName(), str(image.GetSize()))
193 return image.ConvertToBitmap()
195 return bitmap.GetSubBitmap(wx.Rect(rect[0], rect[1], rect[2], rect[3]))
201 def setEnabled(self, e):
204 self.SetToolTipString('')
207 # self.SetToolTipString(self.tooltip)
213 def setBackground(self, wxColor):
214 self.backgroundColor = wxColor
217 def OnPaint(self, evt):
218 dc = wx.BufferedPaintDC(self)
219 dc.SetBackground(wx.Brush(self.backgroundColor))
222 if self.parentBitmap:
223 dc.DrawBitmap(self.parentBitmap, 0,0, True)
225 self.parentBitmap = self.getParentBitmap()
226 if self.parentBitmap:
227 dc.DrawBitmap(self.parentBitmap, 0,0, True)
233 if self.selected == 2:
234 dc.DrawBitmap(self.bitmaps[2], 0,0, True)
239 dc.DrawBitmap(self.bitmaps[0], 0,0, True)
240 if (self.mouseOver or self.selected) and self.bitmaps[1]:
241 dc.DrawBitmap(self.bitmaps[1], 0,0, True)
244 class PlayerSwitchButton(PlayerButton):
246 def __init__(self, parent, imagedir, imagename, imagename2):
247 self.imagedir = imagedir
248 self.imagename = imagename
249 self.imagename2 = imagename2
253 def searchBitmaps(self):
255 self.allBitmaps = [None, None, None, None, None, None]
256 self.parentBitmap = None
257 self.mouseOver = False
260 if not os.path.isdir(self.imagedir):
261 print >>sys.stderr,'PlayerSwitchButton: Error: no image directory found in',self.imagedir
264 # find a file with same name as this panel
265 self.bitmapPath = [os.path.join(self.imagedir, self.imagename+'.png'),
266 os.path.join(self.imagedir, self.imagename+'_hover.png'),
267 os.path.join(self.imagedir, self.imagename+'_dis.png'),
268 os.path.join(self.imagedir, self.imagename2+'.png'),
269 os.path.join(self.imagedir, self.imagename2+'_hover.png'),
270 os.path.join(self.imagedir, self.imagename2+'_dis.png')
274 for img in self.bitmapPath:
275 if os.path.isfile(img):
276 self.allBitmaps[i] = wx.Bitmap(img, wx.BITMAP_TYPE_ANY)
279 print 'Could not find image: %s' % img
283 self.bitmaps = self.allBitmaps[3:]
285 self.bitmaps = self.allBitmaps[:3]
287 def setToggled(self, b, tooltip = { "enabled": "", "disabled": ""}):
290 if not self.initDone:
294 self.bitmaps=self.allBitmaps[3:]
296 self.SetToolTipString(tooltip["enabled"])
298 self.bitmaps=self.allBitmaps[:3]
300 self.SetToolTipString(tooltip["disabled"])
302 #print 'Bitmaps is now: %s' % self.bitmaps
311 ##class VolumeButton(PlayerButton):