home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.5)
-
- from util import print_timing
- from util import to_storage, Storage
- import wx
- from gui.toolbox import TransparentBitmap
- from logging import getLogger
- log = getLogger('MultiImage')
- info = log.info
- error = log.error
-
- class MultiImage(object):
-
- def __init__(self, images):
- self.images = images
- self.tags = { }
- self.drawrects = { }
- for image in self.images:
- if hasattr(image, 'anchors'):
- for anchor in image.anchors:
- if 'tag' in anchor and anchor['tag'] is not None:
- self.tags[anchor['tag']] = image
- continue
-
-
- self.cached_result = Storage(bitmap = None, size = (0, 0))
-
-
- def __repr__(self):
- pformat = pformat
- import pprint
- return '<MultiImage:' + pformat(self.drawrects) + '>'
-
-
- def __contains__(self, tagname):
- return tagname in self.tags
-
-
- def tag_rect(self, tagname):
- if tagname not in self.tags:
- raise ValueError('tag %s not in this MultiImage' % tagname)
-
- return self.drawrects[self.tags[tagname]]
-
-
- def Draw(self, dc, rect):
- position = rect[:2]
- size = rect[2:]
- (self.w, self.h) = size
- if not (self.cached_result.bitmap) or self.cached_result.size != size:
- info('new size, making bitmap')
- self.recalc()
- self.cached_result.size = size
- self.cached_result.bitmap = self.make_bitmap(*size)
-
- dc.DrawBitmapPoint(self.cached_result.bitmap, position)
-
-
- def make_bitmap(self, width, height):
- size = (width, height)
- bitmap = TransparentBitmap(size)
- if not hasattr(self, 'temp_dc'):
- self.temp_dc = wx.MemoryDC()
-
- self.temp_dc.SelectObject(bitmap)
- self.drawtodc(self.temp_dc, wx.Rect(0, 0, *size))
- self.temp_dc.SelectObject(wx.NullBitmap)
- self.region = wx.RegionFromBitmapColour(bitmap, (0, 0, 0, 0))
- return bitmap
-
-
- def recalc(self):
- self.drawrects.clear()
- for i in self.images:
- if i not in self.drawrects:
- self.compute_rect(i, wx.Rect(0, 0, self.w, self.h))
- continue
-
-
-
- def drawtodc(self, dc, rect):
- for image in self.images:
- drect = wx.Rect(*self.drawrects[image])
- drect.Offset((rect.x, rect.y))
- image.Draw(dc, drect)
-
-
-
- def compute_rect(self, image, dcrect):
- myanchors = image.get_anchors_to()
- numanchors = len(myanchors)
- if image.style != 'static':
- if not numanchors == 1 or numanchors == 2:
- raise AssertionError('%r is not static but has anchors %r' % (image, image.anchors))
-
- if numanchors == 1:
- anchorfrom = to_storage(myanchors[0])
- tag = anchorfrom.to
- imageto = self.tags[tag]
- anchorto = [](_[1][0])
- rectto1 = self.drawrects[imageto]
- rectto2 = (0, 0, dcrect.width, dcrect.height)
- positionto1 = compute_anchor_position(anchorto, rectto1[2:], rectto1[:2])
- positionto2 = compute_anchor_position(get_SI2_anchor(image), rectto2[2:], rectto2[:2])
- positionfrom1 = compute_anchor_position(anchorfrom, [
- image.width,
- image.height], [
- 0,
- 0])
- positionfrom2 = (0, 0)
- diffxto = abs(positionto1[0] - positionto2[0])
- diffyto = abs(positionto1[1] - positionto2[1])
- diffxlocal = abs(positionfrom1[0] - positionfrom2[0])
- diffylocal = abs(positionfrom1[1] - positionfrom2[1])
- increasex = diffxto - diffxlocal
- increasey = diffyto - diffylocal
- newsizew = image.width + increasex
- newsizeh = image.height + increasey
- newlocalanchorposition = positionto2
- positiontodrawat = (positionfrom2[0] + newlocalanchorposition[0], positionfrom2[1] + newlocalanchorposition[1])
- self.drawrects[image] = (positiontodrawat[0], positiontodrawat[1], newsizew, newsizeh)
- return None
- elif numanchors == 2:
- (anchorfrom1, anchorfrom2) = myanchors
- tag1 = anchorfrom1['to']
- tag2 = anchorfrom2['to']
- imageto1 = self.tags[tag1]
- imageto2 = self.tags[tag2]
- anchorto1 = _[2]
- anchorto2 = _[3]
- rectto1 = self.drawrects[imageto1]
- rectto2 = self.drawrects[imageto2]
- positionto1 = compute_anchor_position(anchorto1, rectto1[2:], rectto1[:2])
- positionto2 = compute_anchor_position(anchorto2, rectto2[2:], rectto2[:2])
- positionfrom1 = compute_anchor_position(anchorfrom1, [
- image.imgw,
- image.imgh], [
- 0,
- 0])
- positionfrom2 = compute_anchor_position(anchorfrom2, [
- image.imgw,
- image.imgh], [
- 0,
- 0])
- diffxto = abs(positionto1[0] - positionto2[0])
- diffyto = abs(positionto1[1] - positionto2[1])
- diffxlocal = abs(positionfrom1[0] - positionfrom2[0])
- diffylocal = abs(positionfrom1[1] - positionfrom2[1])
- increasex = diffxto - diffxlocal
- increasey = diffyto - diffylocal
- newsizew = image.imgw + increasex
- newsizeh = image.imgh + increasey
- newlocalanchorposition = compute_anchor_position(anchorfrom1, newsizew, newsizeh, [
- 0,
- 0])
- positiontodrawat = (positionto1[0] - newlocalanchorposition[0], positionto1[1] - newlocalanchorposition[1])
- self.drawrects[image] = (positiontodrawat[0], positiontodrawat[1], newsizew, newsizeh)
- return None
- else:
- raise AssertionError('invalid skin, wrong number (%d) of anchors for image of type %s!' % (numanchors, image.image_dictionary['style']))
- elif numanchors == 0:
- positiontodrawat = compute_anchor_position(get_SI2_anchor(image), (dcrect.width, dcrect.height), [
- 0,
- 0])
- self.drawrects[image] = (positiontodrawat[0], positiontodrawat[1], image.width, image.height)
- return None
- elif numanchors == 1:
- anchorfrom = image.get_anchors_to()[0]
- tag = anchorfrom['to']
- imageto = self.tags[tag]
- anchorto = _[4]
- rectto = self.drawrects[imageto]
- positionto = compute_anchor_position(anchorto, rectto[2:], rectto[:2])
- positionfrom = compute_anchor_position(anchorfrom, [
- image.width,
- image.height], [
- 0,
- 0])
- positiontodrawat = (positionto[0] - positionfrom[0], positionto[1] - positionfrom[1])
- self.drawrects[image] = (positiontodrawat[0], positiontodrawat[1], image.width, image.height)
- return None
- else:
- raise AssertionError('invalid skin, wrong number (%d) of anchors for image of type %s!' % (numanchors, image.image_dictionary['style']))
-
-
-
- def compute_anchor_position(anchor, size, offset):
- if 'halign' in anchor:
- halign = anchor.halign
- else:
- halign = 'left'
- if 'valign' in anchor:
- valign = anchor.valign
- else:
- valign = 'top'
- if 'offset' in anchor:
- off = anchor.offset
- else:
- off = [
- 0,
- 0]
- if isinstance(off[0], int):
- myoffsetx = off[0]
- else:
- myoffsetx = int(str(off[0])[:-1]) * size[0] / 100
- if isinstance(off[1], int):
- myoffsety = off[1]
- else:
- myoffsety = int(str(off[1])[:-1]) * size[1] / 100
- off = [
- myoffsetx,
- myoffsety]
- tup = (offset[0], offset[1], size[0], size[1])
- if halign == 'left':
- x = 0
- elif halign == 'right':
- x = tup[2]
- else:
- x = tup[2] / 2
- x = x + tup[0] + off[0]
- if valign == 'top':
- y = 0
- elif valign == 'bottom':
- y = tup[3]
- else:
- y = tup[3] / 2
- y = y + tup[1] + off[1]
- return (x, y)
-
-
- def get_SI2_anchor(image):
- retval = Storage()
- if hasattr(image, 'offset'):
- retval['offset'] = image.offset
-
- if hasattr(image, 'valign'):
- retval['valign'] = image.valign
-
- if hasattr(image, 'halign'):
- retval['halign'] = image.halign
-
- return retval
-
-
- def main(images):
- temp_dc = wx.MemoryDC()
- temp_dc.SelectObject(destbitmap)
- mimg = MultiImage(images)
- drawrect = wx.Rect(10, 10, 220, 440)
- mimg.draw(temp_dc, drawrect)
- temp_dc.SelectObject(wx.NullBitmap)
-
- if __name__ == '__main__':
- import util
- import syck
- from skins import images as imgmngr
- from skins import skins
- app = wx.PySimpleApp()
- skins.res_path = '../../res/'
- destbitmap = imgmngr.get('skins/default/blue-flower.jpg')
- f = file('../../res/skins/skinExample')
- images = to_storage(syck.load(f)).Images
- f.close()
- util.profile(main, images)
- destbitmap.SaveFile('C:/workspace/Digsby/res/skins/default/output.png', wx.BITMAP_TYPE_PNG)
-
-