class ImgDrawing(object): def __init__(self, context, ImgData): self.ImgData = None self.texture = None self.context = context self.cache = None self.transform = SvgTransform() self.filename = ImgData self.triangVtx = np.zeros((4,2), dtype=float32) self.textriangVtx = np.zeros((4,2), dtype=float32) # Detect the type of data passed in if type(ImgData) == file: self.ImgData = ImgData.read() elif type(ImgData) == str: self.texture = Texture(ImgData) # bitmapFile = ImgData.replace(".svg", ".png") # # Load PNG files directly # if ImgData.endswith(".png"): # self.texture = Texture(ImgData) # elif ImgData.endswith(".jpg"): # self.texture = Texture(ImgData) # elif ImgData.endswith(".jpeg"): # self.texture = Texture(ImgData) # Check whether we have a prerendered bitmap version of the SVG file # elif ImgData.endswith(".svg") and os.path.exists(bitmapFile): # Log.debug("Loading cached bitmap '%s' instead of '%s'." % (bitmapFile, ImgData)) # self.texture = Texture(bitmapFile) # else: # if not haveAmanith: # e = "PyAmanith support is deprecated and you are trying to load an SVG file." # Log.error(e) # raise RuntimeError(e) # Log.debug("Loading SVG file '%s'." % (ImgData)) # self.ImgData = open(ImgData).read() 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 type(ImgData) == str: e = "Unable to load texture for %s." % ImgData else: e = "Unable to load texture for SVG file." Log.error(e) raise RuntimeError(e) def convertToTexture(self, width, height): if self.texture: return e = "SVG drawing does not have a valid texture image." Log.error(e) raise RuntimeError(e) def _getEffectiveTransform(self): transform = SvgTransform(self.transform) transform.transform(self.context.transform) return transform def width1(self): width = self.texture.pixelSize[0] if not width == None: return width else: return 0 #myfingershurt: def height1(self): height = self.texture.pixelSize[1] if not height == None: return height else: return 0 def widthf(self, pixelw): width = self.texture.pixelSize[0] wfactor = pixelw/width if not width == None: return wfactor else: return 0 def draw(self, color = (1, 1, 1, 1), rect = (0,1,0,1), lOffset = 0.0, rOffset = 0.0): glMatrixMode(GL_TEXTURE) glPushMatrix() glMatrixMode(GL_PROJECTION) glPushMatrix() self.context.setProjection() glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() self._getEffectiveTransform().applyGL() glScalef(self.texture.pixelSize[0], self.texture.pixelSize[1], 1) glTranslatef(-.5, -.5, 0) glColor4f(*color) glEnable(GL_TEXTURE_2D) self.texture.bind() self.triangVtx[0,0] = 0.0-lOffset self.triangVtx[0,1] = 1.0 self.triangVtx[1,0] = 1.0-rOffset self.triangVtx[1,1] = 1.0 self.triangVtx[2,0] = 0.0+lOffset self.triangVtx[2,1] = 0.0 self.triangVtx[3,0] = 1.0+rOffset self.triangVtx[3,1] = 0.0 self.textriangVtx[0,0] = rect[0] self.textriangVtx[0,1] = rect[3] self.textriangVtx[1,0] = rect[1] self.textriangVtx[1,1] = rect[3] self.textriangVtx[2,0] = rect[0] self.textriangVtx[2,1] = rect[2] self.textriangVtx[3,0] = rect[1] self.textriangVtx[3,1] = rect[2] glEnableClientState(GL_TEXTURE_COORD_ARRAY) glEnableClientState(GL_VERTEX_ARRAY) glVertexPointerf(self.triangVtx) glTexCoordPointerf(self.textriangVtx) glDrawArrays(GL_TRIANGLE_STRIP, 0, self.triangVtx.shape[0]) glDisableClientState(GL_VERTEX_ARRAY) glDisableClientState(GL_TEXTURE_COORD_ARRAY) glDisable(GL_TEXTURE_2D) glPopMatrix() glMatrixMode(GL_TEXTURE) glPopMatrix() glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW)
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 drawImage(image, scale = (1.0, -1.0), coord = (0, 0), rot = 0, \ color = (1,1,1,1), rect = (0,1,0,1), stretched = 0, fit = CENTER, \ alignment = CENTER, valignment = CENTER): """ Draws the image/surface to screen """ if not isinstance(image, ImgDrawing): return False image.setRect(rect) image.setScale(scale[0], scale[1], stretched) image.setPosition(coord[0], coord[1], fit) image.setAlignment(alignment) image.setVAlignment(valignment) image.setAngle(rot) image.setColor(color) image.draw() return True 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 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]))*.5*(self.context.geometry[3]/SCREEN_HEIGHT)) elif fit == BOTTOM: #y is on 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:#center self.shift = -.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 = -.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)
class ImgDrawing(object): 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 type(ImgData) == file: self.ImgData = ImgData.read() elif type(ImgData) == str: 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 type(ImgData) == str: 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.createArrays() def createArrays(self): self.vtxArray = np.zeros((4,2), dtype=np.float32) self.texArray = np.zeros((4,2), dtype=np.float32) self.createVtx() self.createTex() def createVtx(self): vA = self.vtxArray #short hand variable casting #topLeft, topRight, bottomRight, bottomLeft vA[0,0] = 0.0; vA[0,1] = 1.0 vA[1,0] = 1.0; vA[1,1] = 1.0 vA[2,0] = 1.0; vA[2,1] = 0.0 vA[3,0] = 0.0; vA[3,1] = 0.0 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): width = self.pixelSize[0] if width: return width else: return 0 #myfingershurt: def height1(self): height = self.pixelSize[1] if height: return height else: return 0 def widthf(self, pixelw): width = self.pixelSize[0] if width: wfactor = pixelw/width return wfactor else: return 0 def setPosition(self, x, y): self.position = [x,y] def setScale(self, width, height): self.scale = [width, height] def setAngle(self, angle): self.angle = angle def setRect(self, rect): if not rect == self.rect: self.rect = rect self.createTex() def setAlignment(self, alignment): if alignment == LEFT: #left self.shift = 0 elif alignment == CENTER:#center self.shift = -.5 elif alignment == RIGHT:#right self.shift = -1.0 def setVAlignment(self, alignment): if alignment == 0: #bottom self.vshift = 0 elif alignment == 1:#center self.vshift = -.5 elif alignment == 2:#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) glScalef(self.scale[0], self.scale[1], 1.0) glRotatef(self.angle, 0, 0, 1) 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=self.vtxArray, texcoords=self.texArray) glDisable(GL_TEXTURE_2D)
class ImgDrawing: def __init__(self, context, ImgData): self.ImgData = None self.texture = None self.context = context self.cache = None self.transform = SvgTransform() # Detect the type of data passed in if type(ImgData) == file: self.ImgData = ImgData.read() elif type(ImgData) == str: bitmapFile = ImgData.replace(".svg", ".png") # Load PNG files directly if ImgData.endswith(".png"): self.texture = Texture(ImgData) elif ImgData.endswith(".jpg"): self.texture = Texture(ImgData) elif ImgData.endswith(".jpeg"): self.texture = Texture(ImgData) # Check whether we have a prerendered bitmap version of the SVG file elif ImgData.endswith(".svg") and os.path.exists(bitmapFile): Log.debug("Loading cached bitmap '%s' instead of '%s'." % (bitmapFile, ImgData)) self.texture = Texture(bitmapFile) else: if not haveAmanith: e = "PyAmanith support is deprecated and you are trying to load an SVG file." Log.error(e) raise RuntimeError(e) Log.debug("Loading SVG file '%s'." % (ImgData)) self.ImgData = open(ImgData).read() 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 type(ImgData) == str: e = "Unable to load texture for %s." % ImgData else: e = "Unable to load texture for SVG file." Log.error(e) raise RuntimeError(e) def _cacheDrawing(self, drawBoard): self.cache.beginCaching() parser = sax.make_parser() sax.parseString(self.ImgData, SvgHandler(drawBoard, self.cache)) self.cache.endCaching() del self.ImgData def convertToTexture(self, width, height): if self.texture: return e = "SVG drawing does not have a valid texture image." Log.error(e) raise RuntimeError(e) def _getEffectiveTransform(self): transform = SvgTransform(self.transform) transform.transform(self.context.transform) return transform def width1(self): width = self.texture.pixelSize[0] if not width == None: return width else: return 0 #myfingershurt: def height1(self): height = self.texture.pixelSize[1] if not height == None: return height else: return 0 def widthf(self, pixelw): width = self.texture.pixelSize[0] wfactor = pixelw/width if not width == None: return wfactor else: return 0 def _render(self, transform): glMatrixMode(GL_TEXTURE) glPushMatrix() glMatrixMode(GL_MODELVIEW) glPushAttrib(GL_ENABLE_BIT | GL_TEXTURE_BIT | GL_STENCIL_BUFFER_BIT | GL_TRANSFORM_BIT | GL_COLOR_BUFFER_BIT | GL_POLYGON_BIT | GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT) if not self.cache: self.cache = SvgCache(self.context.drawBoard) self._cacheDrawing(self.context.drawBoard) self.cache.draw(transform) glPopAttrib() glMatrixMode(GL_TEXTURE) glPopMatrix() glMatrixMode(GL_MODELVIEW) def draw(self, color = (1, 1, 1, 1), rect = (0,1,0,1), lOffset = 0.0, rOffset = 0.0): glMatrixMode(GL_TEXTURE) glPushMatrix() glMatrixMode(GL_PROJECTION) glPushMatrix() self.context.setProjection() glMatrixMode(GL_MODELVIEW) glPushMatrix() transform = self._getEffectiveTransform() if self.texture: glLoadIdentity() transform.applyGL() glScalef(self.texture.pixelSize[0], self.texture.pixelSize[1], 1) glTranslatef(-.5, -.5, 0) glColor4f(*color) glEnable(GL_TEXTURE_2D) self.texture.bind() triangVtx = array( [[0.0-lOffset, 1.0], [1.0-rOffset, 1.0], [0.0+lOffset, 0.0], [1.0+rOffset, 0.0]], dtype=float32) textriangVtx = array( [[rect[0], rect[3]], [rect[1], rect[3]], [rect[0], rect[2]], [rect[1], rect[2]]], dtype=float32) glEnableClientState(GL_TEXTURE_COORD_ARRAY) glEnableClientState(GL_VERTEX_ARRAY) glTexCoordPointerf(textriangVtx) glVertexPointerf(triangVtx) glDrawArrays(GL_TRIANGLE_STRIP, 0, triangVtx.shape[0]) glDisableClientState(GL_VERTEX_ARRAY) glDisableClientState(GL_TEXTURE_COORD_ARRAY) glDisable(GL_TEXTURE_2D) else: self._render(transform) glMatrixMode(GL_TEXTURE) glPopMatrix() glMatrixMode(GL_MODELVIEW) glPopMatrix() glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW)
class ImgDrawing(object): 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 type(ImgData) == file: self.ImgData = ImgData.read() elif type(ImgData) == str: 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 type(ImgData) == str: 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 self.position = [0.0,0.0] self.scale = [1.0,1.0] self.angle = 0 self.color = (1.0,1.0,1.0) self.rect = (0,1,0,1) self.shift = -.5 self.createArrays() def createArrays(self): self.vtxArray = np.zeros((4,2), dtype=float32) self.texArray = np.zeros((4,2), dtype=float32) self.createVtx() self.createTex() def createVtx(self): vA = self.vtxArray #short hand variable casting #topLeft, topRight, bottomRight, bottomLeft vA[0,0] = 0.0; vA[0,1] = 1.0 vA[1,0] = 1.0; vA[1,1] = 1.0 vA[2,0] = 1.0; vA[2,1] = 0.0 vA[3,0] = 0.0; vA[3,1] = 0.0 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 convertToTexture(self, width, height): if self.texture: return e = "SVG drawing does not have a valid texture image." Log.error(e) raise RuntimeError(e) def width1(self): width = self.pixelSize[0] if width: return width else: return 0 #myfingershurt: def height1(self): height = self.pixelSize[1] if height: return height else: return 0 def widthf(self, pixelw): width = self.pixelSize[0] if width: wfactor = pixelw/width return wfactor else: return 0 def setPosition(self, x, y): self.position = [x,y] def setScale(self, width, height): self.scale = [width, height] def setAngle(self, angle): self.angle = angle def setRect(self, rect): if not rect == self.rect: self.rect = rect self.createTex() def setAlignment(self, alignment): if alignment == 0: #left self.shift = 0 elif alignment == 1:#center self.shift = -.5 elif alignment == 2:#right self.shift = -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): glMatrixMode(GL_TEXTURE) glPushMatrix() glMatrixMode(GL_PROJECTION) glPushMatrix() self.context.setProjection() glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() glTranslate(self.position[0], self.position[1], 0.0) glScalef(self.scale[0], self.scale[1], 1.0) glRotatef(self.angle, 0, 0, 1) glScalef(self.pixelSize[0], self.pixelSize[1], 1) glTranslatef(self.shift, -.5, 0) glColor4f(*self.color) glEnable(GL_TEXTURE_2D) self.texture.bind() glEnableClientState(GL_TEXTURE_COORD_ARRAY) glEnableClientState(GL_VERTEX_ARRAY) glVertexPointerf(self.vtxArray) glTexCoordPointerf(self.texArray) glDrawArrays(GL_QUADS, 0, self.vtxArray.shape[0]) glDisableClientState(GL_VERTEX_ARRAY) glDisableClientState(GL_TEXTURE_COORD_ARRAY) glDisable(GL_TEXTURE_2D) glPopMatrix() glMatrixMode(GL_TEXTURE) glPopMatrix() glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW)
def loadFromFile(self, name): texture = Texture(name) return texture.loadImage()
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 drawImage(image, scale = (1.0, -1.0), coord = (0, 0), rot = 0, \ color = (1,1,1,1), rect = (0,1,0,1), stretched = 0, fit = CENTER, \ alignment = CENTER, valignment = CENTER): """ Draws the image/surface to screen """ if not isinstance(image, ImgDrawing): return False image.setRect(rect) image.setScale(scale[0], scale[1], stretched) image.setPosition(coord[0], coord[1], fit) image.setAlignment(alignment) image.setVAlignment(valignment) image.setAngle(rot) image.setColor(color) image.draw() return True 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 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])) * .5 * (self.context.geometry[3] / SCREEN_HEIGHT)) elif fit == BOTTOM: #y is on 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: #center self.shift = -.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 = -.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)