def addPattern(self, textureName, textures, scale): self.getPatterns() width = 92.0 height = 92.0 textureName = textureName.strip(' \n') patternId = 'pattern_%s' % textureName if patternId not in self.patterns.keys(): if textureName not in textures.keys(): msg = 'The texture %s is not an existing one.' % textureName log.outMsg(msg) raise Exception(msg) textureFilename = textures[textureName]['file'] pattern = Element(addNS('pattern', 'svg')) for name, value in [('patternUnits', 'userSpaceOnUse'), ('width', str(width)), ('height', str(height)), ('id', patternId)]: pattern.set(name, value) imageId = self.addImage(textureName, textures, width, height) if imageId is None: return use = newUseNode('use_%s' % patternId, 0, 0, imageId) pattern.append(use) self.patterns[patternId] = pattern self.defs.append(pattern) if scale != 1.0: def inkscape2xmoto(scale): if scale == 0.0: return 0.0 else: return 1.0 / scale scaledPatternId = 'pattern_%s_%.2f' % (textureName, scale) scale = inkscape2xmoto(scale) if scaledPatternId not in self.patterns.keys(): pattern = Element(addNS('pattern', 'svg')) for name, value in [('id', scaledPatternId), ('patternTransform', 'scale(%f, %f)' % (scale, scale)), (addNS('href', 'xlink'), '#' + patternId)]: pattern.set(name, value) self.patterns[scaledPatternId] = pattern self.defs.append(pattern) patternId = scaledPatternId return patternId
def addColoredChildren(self, node, label, style, coloredStyle): nbChildren = len(self.getchildren()) if nbChildren == 2: self.getchildren()[0].set('style', coloredStyle) self.getchildren()[1].set('style', style) elif nbChildren == 1: colored = node.duplicate(node.get('id') + 'colored') colored.set('style', coloredStyle) # the colored block has to be under the textured one self.insert(0, colored._node) else: log.outMsg("The sublayer %s has a wrong number of \ children: %d" % (self.get('id', ''), nbChildren))
def effectHook(self): """ we need to manipulate the two selected items """ if len(self.selected) != 2: msg = "You have to select the two objects to join together." log.outMsg(msg) return False # check that the objects are paths or rectangles for node in self.selected.values(): if node.tag not in [addNS('path', 'svg'), addNS('rect', 'svg')]: msg = "You need to select path and rectangle only." log.outMsg(msg) return False label = XmNode(node).getParsedLabel() createIfAbsent(label, 'position') if 'physics' not in label['position']: msg = "The selected objects has to be Xmoto physics blocks." log.outMsg(msg) return False block1Id = self.options.ids[0] block2Id = self.options.ids[1] anchorId = block1Id + block2Id + '_joint_' + self.jointType parentNode = self.selected[block1Id].getparent() anchorNode = self.createJointNode(parentNode, anchorId, self.selected[block1Id], self.selected[block2Id]) label, style = self.getLabelAndStyle(block1Id, block2Id) self.updateNodeSvgAttributes(anchorNode, label, style) return False
""" import log, logging import sys try: import os if os.name == 'nt': # we want to reuse inkscape dll under Windows gtkDllPath = os.path.join(os.getcwd()) if os.path.exists(gtkDllPath) == True: os.environ['PATH'] += gtkDllPath import gtk import gtk.glade except Exception, e: log.outMsg("You need to install PyGtk\nError::[%s]" % str(e)) sys.exit(1) from os.path import join, exists from xmotoTools import getSystemDir, getExistingImageFullPath from xmotoTools import alphabeticSortOfKeys, getHomeDir from availableElements import AvailableElements TEXTURES = AvailableElements()['TEXTURES'] EDGETEXTURES = AvailableElements()['EDGETEXTURES'] SPRITES = AvailableElements()['SPRITES'] PARTICLESOURCES = AvailableElements()['PARTICLESOURCES'] EDGETEXTURES['_None_'] = {'file': 'none.png'} TEXTURES['_None_'] = {'file': 'none.png'} SPRITES['_None_'] = {'file': 'none.png'}
def updateNodeSvgAttributes(self, node, label, style): node = convertToXmNode(node, self.svg) labelValue = LabelParser().unparse(label) styleValue = StyleParser().unparse(style) # if the user select the an element in the sublayer using the # 'circle tool' for example, the selected node will be that # element, not the sublayer (the sublayer is selected when you # use the 'selection tool'). # in this case, use the sublayer instead of the selected child if node.belongsToSubLayer() == True: node = convertToXmNode(node.getparent()) # update node shape # ugly and clumsy but will be refactored with xmObjects later if 'typeid' in label: # entity or zone typeid = label['typeid'] if typeid in [ 'PlayerStart', 'EndOfLevel', 'Strawberry', 'Wrecker', 'Checkpoint' ]: if typeid == 'EndOfLevel': typeid = 'Flower' metadata = self.svg.getMetaDataValue() metadata = LabelParser().parse(metadata) if typeid == 'Checkpoint': # the checkpoint sprite is called with _1 createIfAbsent(metadata, 'remplacement') metadata['remplacement']['Checkpoint'] = 'Checkpoint_1' texName = getValue(metadata, 'remplacement', typeid, default=typeid) scale = float( getValue(metadata, 'remplacement', typeid + 'Scale', 1.0)) _reversed = getBoolValue(label, 'position', 'reversed') rotation = float(getValue(label, 'position', 'angle', 0.0)) radius = ENTITY_RADIUS[typeid] / SVG2LVL_RATIO node.setNodeAsBitmap(self.svg, texName, radius, SPRITES, labelValue, styleValue, scale, _reversed, rotation) elif typeid == 'ParticleSource': texName = getValue(label, 'param', 'type', '') radius = ENTITY_RADIUS[typeid] / SVG2LVL_RATIO node.setNodeAsBitmap(self.svg, texName, radius, PARTICLESOURCES, labelValue, styleValue) elif typeid == 'Sprite': texName = getValue(label, 'param', 'name', '') scale = float(getValue(label, 'size', 'scale', 1.0)) _reversed = getBoolValue(label, 'position', 'reversed') rotation = float(getValue(label, 'position', 'angle', 0.0)) radius = ENTITY_RADIUS['Sprite'] / SVG2LVL_RATIO node.setNodeAsBitmap(self.svg, texName, radius, SPRITES, labelValue, styleValue, scale, _reversed, rotation) elif typeid == 'Zone': (node, aabb) = node.subLayerElementToSingleNode() node.setNodeAsRectangle(aabb) # we may have set the label and still to a child of # 'g', and now node is the 'g', so we have to set it # to it too. node.setStyleLabel(labelValue, styleValue) elif typeid == 'Joint': # the addJoint extension already create the joints # with the right shape pass else: raise Exception("typeid=%s not handled by \ updateNodeSvgAttributes" % typeid) else: # block if node.isSubLayer(type=XmNode.BITMAP) == True: log.outMsg("Can't convert an entity to a block") return # elif (getValue(label, 'usetexture', 'color_r', 255) != 255 # or getValue(label, 'usetexture', 'color_g', 255) != 255 # or getValue(label, 'usetexture', 'color_b', 255) != 255): # # a color is not 255, we have to set two blocks, one # # textured and one colored # coloredStyle = self.generateStyle(label, coloredBlock=True) # coloredStyleValue = StyleParser().unparse(coloredStyle) # # g = node.getSubLayerNode() # g.addColoredChildren(node, labelValue, styleValue, coloredStyleValue) else: if node.isSubLayer(type=XmNode.BLOCK) == True: # remove sublayer and colored block node.removeColoredChildren(labelValue, styleValue) else: # nothing to do pass node.setStyleLabel(labelValue, styleValue)