def loadItemLabel(self, i, preload=False): # Load the item label if it isn't yet loaded item = self.items[i] if self.itemLabels[i] is None: if isinstance(item, song.SongInfo): # CD covers if self.labelType == 1: f = "label.png" else: f = "album.png" if self.texturedLabels: label = self.engine.resource.fileName( item.libraryNam, item.songName, f) if os.path.exists(label): self.itemLabels[i] = Texture(label) else: self.itemLabels[i] = False else: self.itemLabels[i] = self.engine.loadImgDrawing( None, "label", os.path.join(item.libraryNam, item.songName, f)) elif isinstance(item, song.LibraryInfo): if self.texturedLabels: label = self.engine.resource.fileName( item.libraryName, "label.png") if os.path.exists(label): self.itemLabels[i] = Texture(label) else: self.itemLabels[i] = False else: self.itemLabels[i] = self.engine.loadImgDrawing( None, "label", os.path.join(item.libraryName, "label.png")) elif isinstance(item, song.RandomSongInfo): self.itemLabels[i] = "Random" else: return if preload: if time.time() - self.loadStartTime > 3: self.loadStartTime = time.time() percent = (i * 100) / len(self.items) Dialogs.changeLoadingSplashScreenText( self.engine, self.splash, _("Loading Album Artwork...") + " %d%%" % percent)
def __init__(self, context, ImgData): self.ImgData = None self.texture = None self.context = context self.cache = None self.filename = ImgData # Detect the type of data passed in if isinstance(ImgData, file): self.ImgData = ImgData.read() elif isinstance(ImgData, basestring): self.texture = Texture(ImgData) elif isinstance(ImgData, Image.Image): #stump: let a PIL image be passed in self.texture = Texture() self.texture.loadImage(ImgData) # Make sure we have a valid texture if not self.texture: if isinstance(ImgData, basestring): e = "Unable to load texture for %s." % ImgData else: e = "Unable to load texture for SVG file." Log.error(e) raise RuntimeError(e) self.pixelSize = self.texture.pixelSize #the size of the image in pixels (from texture) self.position = [0.0, 0.0] #position of the image in the viewport self.scale = [1.0, 1.0] #percentage scaling self.angle = 0 #angle of rotation (degrees) self.color = (1.0, 1.0, 1.0, 1.0) #glColor rgba self.rect = (0, 1, 0, 1) #texture mapping coordinates self.shift = -.5 #horizontal alignment self.vshift = -.5 #vertical alignment self.path = self.texture.name #path of the image file self.texArray = np.zeros((4, 2), dtype=np.float32) self.createTex()
def __init__(self, context, ImgData): self.ImgData = None self.texture = None self.context = context self.cache = None self.filename = ImgData # Detect the type of data passed in if isinstance(ImgData, file): self.ImgData = ImgData.read() elif isinstance(ImgData, basestring): self.texture = Texture(ImgData) elif isinstance(ImgData, Image.Image): # stump: let a PIL image be passed in self.texture = Texture() self.texture.loadImage(ImgData) # Make sure we have a valid texture if not self.texture: if isinstance(ImgData, basestring): e = "Unable to load texture for %s." % ImgData else: e = "Unable to load texture for SVG file." Log.error(e) raise RuntimeError(e) self.pixelSize = self.texture.pixelSize # the size of the image in pixels (from texture) self.position = [0.0, 0.0] # position of the image in the viewport self.scale = [1.0, 1.0] # percentage scaling self.angle = 0 # angle of rotation (degrees) self.color = (1.0, 1.0, 1.0, 1.0) # glColor rgba self.rect = (0, 1, 0, 1) # texture mapping coordinates self.shift = -0.5 # horizontal alignment self.vshift = -0.5 # vertical alignment self.path = self.texture.name # path of the image file self.texArray = np.zeros((4, 2), dtype=np.float32) self.createTex()
class ImgDrawing(object): # hard-coded quad for drawing textures onto VTX_ARRAY = np.array([[0.0, 1.0], [1.0, 1.0], [1.0, 0.0], [0.0, 0.0]], dtype=np.float32) def __init__(self, context, ImgData): self.ImgData = None self.texture = None self.context = context self.cache = None self.filename = ImgData # Detect the type of data passed in if isinstance(ImgData, file): self.ImgData = ImgData.read() elif isinstance(ImgData, basestring): self.texture = Texture(ImgData) elif isinstance(ImgData, Image.Image): # let a PIL image be passed in self.texture = Texture() self.texture.loadImage(ImgData) # Make sure we have a valid texture if not self.texture: if isinstance(ImgData, basestring): e = "Unable to load texture for %s." % ImgData else: e = "Unable to load texture for SVG file." log.error(e) raise RuntimeError(e) self.pixelSize = self.texture.pixelSize # the size of the image in pixels (from texture) self.position = [0.0, 0.0] # position of the image in the viewport self.scale = [1.0, 1.0] # percentage scaling self.angle = 0 # angle of rotation (degrees) self.color = (1.0, 1.0, 1.0, 1.0) # glColor rgba self.rect = (0, 1, 0, 1) # texture mapping coordinates self.shift = -.5 # horizontal alignment self.vshift = -.5 # vertical alignment self.path = self.texture.name # path of the image file self.texArray = np.zeros((4, 2), dtype=np.float32) self.createTex() def createTex(self): tA = self.texArray rect = self.rect #topLeft, topRight, bottomRight, bottomLeft tA[0, 0] = rect[0] tA[0, 1] = rect[3] tA[1, 0] = rect[1] tA[1, 1] = rect[3] tA[2, 0] = rect[1] tA[2, 1] = rect[2] tA[3, 0] = rect[0] tA[3, 1] = rect[2] def width1(self): """ Return the width of the texture in pixels """ width = self.pixelSize[0] if width: return width else: return 0 def height1(self): """ Return the height of the texture in pixels """ height = self.pixelSize[1] if height is not None: return height else: return 0 def widthf(self, pixelw): """ :param pixelw: a width in pixels :return: the scaled ratio of the pixelw divided by the pixel width of the texture """ width = self.pixelSize[0] if width is not None: wfactor = pixelw / width return wfactor else: return 0 def setPosition(self, x, y, fit=CENTER): """ Sets position of this image on screen :param fit: Adjusts the texture so the coordinate for the y-axis placement can be on the top side (1), bottom side (2), or center point (any other value) of the image """ # fit gives the y position if fit == CENTER: pass elif fit == TOP: y = y - ((self.pixelSize[1] * abs(self.scale[1])) * .5 * (self.context.geometry[3] / SCREEN_HEIGHT)) elif fit == BOTTOM: y = y + ((self.pixelSize[1] * abs(self.scale[1])) * .5 * (self.context.geometry[3] / SCREEN_HEIGHT)) self.position = [x, y] def setScale(self, width, height, stretched=0): """ :param stretched: Bitmask stretching the image according to the following values 0) does not stretch the image 1) fits it to the width of the viewport 2) fits it to the height of the viewport 3) stretches it so it fits the viewport 4) preserves the aspect ratio while stretching """ if stretched & FULL_SCREEN: # FULL_SCREEN is FIT_WIDTH | FIT_HEIGHT xStretch = 1 yStretch = 1 if stretched & FIT_WIDTH: xStretch = float(self.context.geometry[2]) / self.pixelSize[0] if stretched & FIT_HEIGHT: yStretch = float(self.context.geometry[3]) / self.pixelSize[1] if stretched & KEEP_ASPECT: if stretched & FULL_SCREEN == FULL_SCREEN: #Note that on FULL_SCREEN | KEEP_ASPECT we will scale to the larger and clip. if xStretch > yStretch: yStretch = xStretch else: xStretch = yStretch else: if stretched & FIT_WIDTH: yStretch = xStretch else: xStretch = yStretch width *= xStretch height *= yStretch self.scale = [width, height] def setAngle(self, angle): self.angle = angle def setRect(self, rect): """ :param rect: The surface rectangle, this is used for cropping the texture """ if not rect == self.rect: self.rect = rect self.createTex() def setAlignment(self, alignment): """ :param alignment: Adjusts the texture so the coordinate for x-axis placement can either be on the left side (0), center point (1), or right(2) side of the image """ if alignment == CENTER: self.shift = -.5 elif alignment == LEFT: self.shift = 0 elif alignment == RIGHT: self.shift = -1.0 def setVAlignment(self, alignment): """ :param valignment: Adjusts the texture so the coordinate for y-axis placement can either be on the bottom side (0), center point (1), or top(2) side of the image """ if alignment == CENTER: self.vshift = -.5 if alignment == TOP: self.vshift = 0 elif alignment == BOTTOM: self.vshift = -1.0 def setColor(self, color): if len(color) == 3: color = (color[0], color[1], color[2], 1.0) self.color = color def draw(self): with cmgl.PushedSpecificMatrix(GL_TEXTURE): with cmgl.PushedSpecificMatrix(GL_PROJECTION): with cmgl.MatrixMode(GL_PROJECTION): self.context.setProjection() with cmgl.PushedMatrix(): glLoadIdentity() glTranslate(self.position[0], self.position[1], 0.0) glRotatef(-self.angle, 0, 0, 1) glScalef(self.scale[0], self.scale[1], 1.0) glScalef(self.pixelSize[0], self.pixelSize[1], 1) glTranslatef(self.shift, self.vshift, 0) glColor4f(*self.color) glEnable(GL_TEXTURE_2D) self.texture.bind() cmgl.drawArrays(GL_QUADS, vertices=ImgDrawing.VTX_ARRAY, texcoords=self.texArray) glDisable(GL_TEXTURE_2D)
def render(self, text, pos=(0, 0), rotate=0, scale=DEFAULT_SCALE, shadowoffset=(.0022, .0005), align=LEFT, shadow=False, outline=False, shadowOpacity=1.0): """ Draw some text. :param text: Text to draw :param pos: Text coordinate tuple (x, y) :param rotate: Angle to rotate text, in degrees :param scale: Scale factor :param shadowoffset: Shadow offset (x, y) :param align: how to align the text :param shadow: if there will be shadow (default: False) :param outline: if there will be outline (default: False) :param shadowOpacity: opacity of the shadow (default: 1.0) """ if not text: return try: t, w, h = self.stringsCache.get(text) except KeyError: s = self.font.render(text, True, (255, 255, 255)) t = Texture() t.setFilter(GL_LINEAR, GL_LINEAR) t.setRepeat(GL_CLAMP, GL_CLAMP) t.loadSurface(s, alphaChannel=True) del s w, h = self.font.size(text) self.stringsCache.add(text, (t, w, h)) x, y = pos scale *= self.scale w, h = w * scale * self.aspectRatioFactor, h * scale if align == CENTER: x -= (w / 2) elif align == RIGHT: x -= w y -= (h / 2) tw, th = t.size glEnable(GL_TEXTURE_2D) with cmgl.PushedMatrix(): if rotate: if not isinstance(rotate, tuple): glRotatef(rotate, 0, 0, 1.0) else: glRotatef(0, *rotate) glTranslatef(x, y, 0) t.bind() # Outline if self.outline or outline: with cmgl.PushedAttrib(GL_CURRENT_BIT): glColor4f(0, 0, 0, .25 * glGetDoublev(GL_CURRENT_COLOR)[3]) blur = 2 * DEFAULT_SCALE for offset in [(-.7, -.7), (0, -1), (.7, -.7), (-1, 0), (1, 0), (-.7, .7), (0, 1), (.7, .7)]: with cmgl.PushedMatrix(): glTranslatef(blur * offset[0], blur * offset[1], 0) self.drawSquare(w, h, tw, th) # Shadow if self.shadow or shadow: with cmgl.PushedAttrib(GL_CURRENT_BIT): glColor4f(0, 0, 0, glGetDoublev(GL_CURRENT_COLOR)[3] * shadowOpacity) with cmgl.PushedMatrix(): glTranslatef(shadowoffset[0], shadowoffset[1], 0) self.drawSquare(w, h, tw, th) self.drawSquare(w,h,tw,th) glDisable(GL_TEXTURE_2D)
class ImgDrawing(object): VTX_ARRAY = np.array( [[0.0, 1.0], [1.0, 1.0], [1.0, 0.0], [0.0, 0.0]], dtype=np.float32 ) # hard-coded quad for drawing textures onto def __init__(self, context, ImgData): self.ImgData = None self.texture = None self.context = context self.cache = None self.filename = ImgData # Detect the type of data passed in if isinstance(ImgData, file): self.ImgData = ImgData.read() elif isinstance(ImgData, basestring): self.texture = Texture(ImgData) elif isinstance(ImgData, Image.Image): # stump: let a PIL image be passed in self.texture = Texture() self.texture.loadImage(ImgData) # Make sure we have a valid texture if not self.texture: if isinstance(ImgData, basestring): e = "Unable to load texture for %s." % ImgData else: e = "Unable to load texture for SVG file." Log.error(e) raise RuntimeError(e) self.pixelSize = self.texture.pixelSize # the size of the image in pixels (from texture) self.position = [0.0, 0.0] # position of the image in the viewport self.scale = [1.0, 1.0] # percentage scaling self.angle = 0 # angle of rotation (degrees) self.color = (1.0, 1.0, 1.0, 1.0) # glColor rgba self.rect = (0, 1, 0, 1) # texture mapping coordinates self.shift = -0.5 # horizontal alignment self.vshift = -0.5 # vertical alignment self.path = self.texture.name # path of the image file self.texArray = np.zeros((4, 2), dtype=np.float32) self.createTex() def createTex(self): tA = self.texArray rect = self.rect # topLeft, topRight, bottomRight, bottomLeft tA[0, 0] = rect[0] tA[0, 1] = rect[3] tA[1, 0] = rect[1] tA[1, 1] = rect[3] tA[2, 0] = rect[1] tA[2, 1] = rect[2] tA[3, 0] = rect[0] tA[3, 1] = rect[2] def width1(self): """ @return the width of the texture in pixels """ width = self.pixelSize[0] if width: return width else: return 0 def height1(self): """ @return the height of the texture in pixels """ height = self.pixelSize[1] if height is not None: return height else: return 0 def widthf(self, pixelw): """ @param pixelw - a width in pixels @return the scaled ratio of the pixelw divided by the pixel width of the texture """ width = self.pixelSize[0] if width is not None: wfactor = pixelw / width return wfactor else: return 0 def setPosition(self, x, y, fit=CENTER): """ Sets position of this image on screen @param fit: Adjusts the texture so the coordinate for the y-axis placement can be on the top side (1), bottom side (2), or center point (any other value) of the image """ if fit == CENTER: # y is center pass elif fit == TOP: # y is on top (not center) y = y - ((self.pixelSize[1] * abs(self.scale[1])) * 0.5 * (self.context.geometry[3] / SCREEN_HEIGHT)) elif fit == BOTTOM: # y is on bottom y = y + ((self.pixelSize[1] * abs(self.scale[1])) * 0.5 * (self.context.geometry[3] / SCREEN_HEIGHT)) self.position = [x, y] def setScale(self, width, height, stretched=0): """ @param stretched: Bitmask stretching the image according to the following values 0) does not stretch the image 1) fits it to the width of the viewport 2) fits it to the height of the viewport 3) stretches it so it fits the viewport 4) preserves the aspect ratio while stretching """ if stretched & FULL_SCREEN: # FULL_SCREEN is FIT_WIDTH | FIT_HEIGHT xStretch = 1 yStretch = 1 if stretched & FIT_WIDTH: xStretch = float(self.context.geometry[2]) / self.pixelSize[0] if stretched & FIT_HEIGHT: yStretch = float(self.context.geometry[3]) / self.pixelSize[1] if stretched & KEEP_ASPECT: if ( stretched & FULL_SCREEN == FULL_SCREEN ): # Note that on FULL_SCREEN | KEEP_ASPECT we will scale to the larger and clip. if xStretch > yStretch: yStretch = xStretch else: xStretch = yStretch else: if stretched & FIT_WIDTH: yStretch = xStretch else: xStretch = yStretch width *= xStretch height *= yStretch self.scale = [width, height] def setAngle(self, angle): self.angle = angle def setRect(self, rect): """ @param rect: The surface rectangle, this is used for cropping the texture """ if not rect == self.rect: self.rect = rect self.createTex() def setAlignment(self, alignment): """ @param alignment: Adjusts the texture so the coordinate for x-axis placement can either be on the left side (0), center point (1), or right(2) side of the image """ if alignment == CENTER: # center self.shift = -0.5 elif alignment == LEFT: # left self.shift = 0 elif alignment == RIGHT: # right self.shift = -1.0 def setVAlignment(self, alignment): """ @param valignment: Adjusts the texture so the coordinate for y-axis placement can either be on the bottom side (0), center point (1), or top(2) side of the image """ if alignment == CENTER: # center self.vshift = -0.5 if alignment == TOP: # bottom self.vshift = 0 elif alignment == BOTTOM: # top self.vshift = -1.0 def setColor(self, color): if len(color) == 3: color = (color[0], color[1], color[2], 1.0) self.color = color def draw(self): with cmgl.PushedSpecificMatrix(GL_TEXTURE): with cmgl.PushedSpecificMatrix(GL_PROJECTION): with cmgl.MatrixMode(GL_PROJECTION): self.context.setProjection() with cmgl.PushedMatrix(): glLoadIdentity() glTranslate(self.position[0], self.position[1], 0.0) glRotatef(-self.angle, 0, 0, 1) glScalef(self.scale[0], self.scale[1], 1.0) glScalef(self.pixelSize[0], self.pixelSize[1], 1) glTranslatef(self.shift, self.vshift, 0) glColor4f(*self.color) glEnable(GL_TEXTURE_2D) self.texture.bind() cmgl.drawArrays(GL_QUADS, vertices=ImgDrawing.VTX_ARRAY, texcoords=self.texArray) glDisable(GL_TEXTURE_2D)
def render(self, text, pos = (0, 0), rotate = 0, scale = DEFAULT_SCALE, shadowoffset = (.0022, .0005), align = LEFT, new = False, shadow = False, outline = False, shadowOpacity = 1.0): """ Draw some text. @param text: Text to draw @param pos: Text coordinate tuple (x, y) @param rotate: Angle to rotate text, in degrees @param scale: Scale factor """ # deufeufeu : new drawing relaying only on pygame.font.render # : I know me miss special unicodes characters, but the gain # : is really important. # evilynux : Use arrays to increase performance def drawSquare(w,h,tw,th): self.square_prim[1,0] = self.square_prim[3,0] = w self.square_prim[2,1] = self.square_prim[3,1] = h self.square_tex[0,1] = self.square_tex[1,1] = th self.square_tex[1,0] = self.square_tex[3,0] = tw cmgl.drawArrays(GL_TRIANGLE_STRIP, vertices=self.square_prim, texcoords=self.square_tex) if not text: return try: t,w,h = self.stringsCache.get(text) except KeyError: s = self.font.render(text, True, (255,255,255)) t = Texture() t.setFilter(GL_LINEAR, GL_LINEAR) t.setRepeat(GL_CLAMP, GL_CLAMP) t.loadSurface(s, alphaChannel = True) del s w, h = self.font.size(text) self.stringsCache.add(text,(t,w,h)) x, y = pos scale *= self.scale w, h = w*scale*self.aspectRatioFactor, h*scale if align == CENTER: #we have already done all the calculating. Why not add this? - akedrou x -= (w/2) elif align == RIGHT: x -= w y -= (h/2) tw,th = t.size glEnable(GL_TEXTURE_2D) with cmgl.PushedMatrix(): if rotate: if not isinstance(rotate, tuple): glRotatef(rotate, 0, 0, 1.0) else: glRotatef(0, *rotate) glTranslatef(x,y,0) t.bind() if self.outline or outline: with cmgl.PushedAttrib(GL_CURRENT_BIT): glColor4f(0, 0, 0, .25 * glGetDoublev(GL_CURRENT_COLOR)[3]) blur = 2 * DEFAULT_SCALE for offset in [(-.7, -.7), (0, -1), (.7, -.7), (-1, 0), (1, 0), (-.7, .7), (0, 1), (.7, .7)]: with cmgl.PushedMatrix(): glTranslatef(blur * offset[0], blur * offset[1], 0) drawSquare(w,h,tw,th) if self.shadow or shadow: with cmgl.PushedAttrib(GL_CURRENT_BIT): glColor4f(0, 0, 0, glGetDoublev(GL_CURRENT_COLOR)[3]*shadowOpacity) with cmgl.PushedMatrix(): glTranslatef(shadowoffset[0], shadowoffset[1], 0) drawSquare(w,h,tw,th) drawSquare(w,h,tw,th) glDisable(GL_TEXTURE_2D)
def render(self, text, pos=(0, 0), rotate=0, scale=DEFAULT_SCALE, shadowoffset=(.0022, .0005), align=LEFT, new=False, shadow=False, outline=False, shadowOpacity=1.0): """ Draw some text. @param text: Text to draw @param pos: Text coordinate tuple (x, y) @param rotate: Angle to rotate text, in degrees @param scale: Scale factor """ # deufeufeu : new drawing relaying only on pygame.font.render # : I know me miss special unicodes characters, but the gain # : is really important. # evilynux : Use arrays to increase performance def drawSquare(w, h, tw, th): self.square_prim[1, 0] = self.square_prim[3, 0] = w self.square_prim[2, 1] = self.square_prim[3, 1] = h self.square_tex[0, 1] = self.square_tex[1, 1] = th self.square_tex[1, 0] = self.square_tex[3, 0] = tw cmgl.drawArrays(GL_TRIANGLE_STRIP, vertices=self.square_prim, texcoords=self.square_tex) if not text: return try: t, w, h = self.stringsCache.get(text) except KeyError: s = self.font.render(text, True, (255, 255, 255)) t = Texture() t.setFilter(GL_LINEAR, GL_LINEAR) t.setRepeat(GL_CLAMP, GL_CLAMP) t.loadSurface(s, alphaChannel=True) del s w, h = self.font.size(text) self.stringsCache.add(text, (t, w, h)) x, y = pos scale *= self.scale w, h = w * scale * self.aspectRatioFactor, h * scale if align == CENTER: #we have already done all the calculating. Why not add this? - akedrou x -= (w / 2) elif align == RIGHT: x -= w y -= (h / 2) tw, th = t.size glEnable(GL_TEXTURE_2D) with cmgl.PushedMatrix(): if rotate: if not isinstance(rotate, tuple): glRotatef(rotate, 0, 0, 1.0) else: glRotatef(0, *rotate) glTranslatef(x, y, 0) t.bind() if self.outline or outline: with cmgl.PushedAttrib(GL_CURRENT_BIT): glColor4f(0, 0, 0, .25 * glGetDoublev(GL_CURRENT_COLOR)[3]) blur = 2 * DEFAULT_SCALE for offset in [(-.7, -.7), (0, -1), (.7, -.7), (-1, 0), (1, 0), (-.7, .7), (0, 1), (.7, .7)]: with cmgl.PushedMatrix(): glTranslatef(blur * offset[0], blur * offset[1], 0) drawSquare(w, h, tw, th) if self.shadow or shadow: with cmgl.PushedAttrib(GL_CURRENT_BIT): glColor4f( 0, 0, 0, glGetDoublev(GL_CURRENT_COLOR)[3] * shadowOpacity) with cmgl.PushedMatrix(): glTranslatef(shadowoffset[0], shadowoffset[1], 0) drawSquare(w, h, tw, th) drawSquare(w, h, tw, th) glDisable(GL_TEXTURE_2D)