public bool UpdateWithBatchNode(CCSpriteBatchNode batchnode, CCRect rect, bool rotated, CCRect capInsets) { byte opacity = m_cOpacity; CCColor3B color = m_tColor; // Release old sprites RemoveAllChildrenWithCleanup(true); if (scale9Image != batchnode) { scale9Image = batchnode; } scale9Image.RemoveAllChildrenWithCleanup(true); m_capInsets = capInsets; // If there is no given rect if (rect.Equals(CCRect.Zero)) { // Get the texture size as original CCSize textureSize = scale9Image.TextureAtlas.Texture.ContentSize; rect = new CCRect(0, 0, textureSize.Width, textureSize.Height); } // Set the given rect's size as original size m_spriteRect = rect; m_originalSize = rect.Size; m_preferredSize = m_originalSize; m_capInsetsInternal = capInsets; // Get the image edges float l = rect.Origin.X; float t = rect.Origin.Y; float h = rect.Size.Height; float w = rect.Size.Width; // If there is no specified center region if (m_capInsetsInternal.Equals(CCRect.Zero)) { // Apply the 3x3 grid format if (rotated) { m_capInsetsInternal = new CCRect(l + h / 3, t + w / 3, w / 3, h / 3); } else { m_capInsetsInternal = new CCRect(l + w / 3, t + h / 3, w / 3, h / 3); } } // // Set up the image // if (rotated) { // Sprite frame is rotated // Centre centre = new CCSprite(); centre.InitWithTexture(scale9Image.Texture, m_capInsetsInternal, true); scale9Image.AddChild(centre, 0, (int) Positions.pCentre); // Bottom bottom = new CCSprite(); bottom.InitWithTexture(scale9Image.Texture, new CCRect(l, m_capInsetsInternal.Origin.Y, m_capInsetsInternal.Size.Width, m_capInsetsInternal.Origin.X - l), rotated ); scale9Image.AddChild(bottom, 1, (int) Positions.pBottom); // Top top = new CCSprite(); top.InitWithTexture(scale9Image.Texture, new CCRect(m_capInsetsInternal.Origin.X + m_capInsetsInternal.Size.Height, m_capInsetsInternal.Origin.Y, m_capInsetsInternal.Size.Width, h - m_capInsetsInternal.Size.Height - (m_capInsetsInternal.Origin.X - l)), rotated ); scale9Image.AddChild(top, 1, (int) Positions.pTop); // Right right = new CCSprite(); right.InitWithTexture(scale9Image.Texture, new CCRect(m_capInsetsInternal.Origin.X, m_capInsetsInternal.Origin.Y + m_capInsetsInternal.Size.Width, w - (m_capInsetsInternal.Origin.Y - t) - m_capInsetsInternal.Size.Width, m_capInsetsInternal.Size.Height), rotated ); scale9Image.AddChild(right, 1, (int) Positions.pRight); // Left left = new CCSprite(); left.InitWithTexture(scale9Image.Texture, new CCRect(m_capInsetsInternal.Origin.X, t, m_capInsetsInternal.Origin.Y - t, m_capInsetsInternal.Size.Height), rotated ); scale9Image.AddChild(left, 1, (int) Positions.pLeft); // Top right topRight = new CCSprite(); topRight.InitWithTexture(scale9Image.Texture, new CCRect(m_capInsetsInternal.Origin.X + m_capInsetsInternal.Size.Height, m_capInsetsInternal.Origin.Y + m_capInsetsInternal.Size.Width, w - (m_capInsetsInternal.Origin.Y - t) - m_capInsetsInternal.Size.Width, h - m_capInsetsInternal.Size.Height - (m_capInsetsInternal.Origin.X - l)), rotated ); scale9Image.AddChild(topRight, 2, (int) Positions.pTopRight); // Top left topLeft = new CCSprite(); topLeft.InitWithTexture(scale9Image.Texture, new CCRect(m_capInsetsInternal.Origin.X + m_capInsetsInternal.Size.Height, t, m_capInsetsInternal.Origin.Y - t, h - m_capInsetsInternal.Size.Height - (m_capInsetsInternal.Origin.X - l)), rotated ); scale9Image.AddChild(topLeft, 2, (int) Positions.pTopLeft); // Bottom right bottomRight = new CCSprite(); bottomRight.InitWithTexture(scale9Image.Texture, new CCRect(l, m_capInsetsInternal.Origin.Y + m_capInsetsInternal.Size.Width, w - (m_capInsetsInternal.Origin.Y - t) - m_capInsetsInternal.Size.Width, m_capInsetsInternal.Origin.X - l), rotated ); scale9Image.AddChild(bottomRight, 2, (int) Positions.pBottomRight); // Bottom left bottomLeft = new CCSprite(); bottomLeft.InitWithTexture(scale9Image.Texture, new CCRect(l, t, m_capInsetsInternal.Origin.Y - t, m_capInsetsInternal.Origin.X - l), rotated ); scale9Image.AddChild(bottomLeft, 2, (int) Positions.pBottomLeft); } else { // Sprite frame is not rotated // Centre centre = new CCSprite(); centre.InitWithTexture(scale9Image.Texture, m_capInsetsInternal, rotated); scale9Image.AddChild(centre, 0, (int) Positions.pCentre); // Top top = new CCSprite(); top.InitWithTexture(scale9Image.Texture, new CCRect(m_capInsetsInternal.Origin.X, t, m_capInsetsInternal.Size.Width, m_capInsetsInternal.Origin.Y - t), rotated ); scale9Image.AddChild(top, 1, (int) Positions.pTop); // Bottom bottom = new CCSprite(); bottom.InitWithTexture(scale9Image.Texture, new CCRect(m_capInsetsInternal.Origin.X, m_capInsetsInternal.Origin.Y + m_capInsetsInternal.Size.Height, m_capInsetsInternal.Size.Width, h - (m_capInsetsInternal.Origin.Y - t + m_capInsetsInternal.Size.Height)), rotated); scale9Image.AddChild(bottom, 1, (int) Positions.pBottom); // Left left = new CCSprite(); left.InitWithTexture(scale9Image.Texture, new CCRect( l, m_capInsetsInternal.Origin.Y, m_capInsetsInternal.Origin.X - l, m_capInsetsInternal.Size.Height), rotated); scale9Image.AddChild(left, 1, (int) Positions.pLeft); // Right right = new CCSprite(); right.InitWithTexture(scale9Image.Texture, new CCRect( m_capInsetsInternal.Origin.X + m_capInsetsInternal.Size.Width, m_capInsetsInternal.Origin.Y, w - (m_capInsetsInternal.Origin.X - l + m_capInsetsInternal.Size.Width), m_capInsetsInternal.Size.Height), rotated); scale9Image.AddChild(right, 1, (int) Positions.pRight); // Top left topLeft = new CCSprite(); topLeft.InitWithTexture(scale9Image.Texture, new CCRect( l, t, m_capInsetsInternal.Origin.X - l, m_capInsetsInternal.Origin.Y - t), rotated); scale9Image.AddChild(topLeft, 2, (int) Positions.pTopLeft); // Top right topRight = new CCSprite(); topRight.InitWithTexture(scale9Image.Texture, new CCRect( m_capInsetsInternal.Origin.X + m_capInsetsInternal.Size.Width, t, w - (m_capInsetsInternal.Origin.X - l + m_capInsetsInternal.Size.Width), m_capInsetsInternal.Origin.Y - t), rotated); scale9Image.AddChild(topRight, 2, (int) Positions.pTopRight); // Bottom left bottomLeft = new CCSprite(); bottomLeft.InitWithTexture(scale9Image.Texture, new CCRect( l, m_capInsetsInternal.Origin.Y + m_capInsetsInternal.Size.Height, m_capInsetsInternal.Origin.X - l, h - (m_capInsetsInternal.Origin.Y - t + m_capInsetsInternal.Size.Height)), rotated); scale9Image.AddChild(bottomLeft, 2, (int) Positions.pBottomLeft); // Bottom right bottomRight = new CCSprite(); bottomRight.InitWithTexture(scale9Image.Texture, new CCRect( m_capInsetsInternal.Origin.X + m_capInsetsInternal.Size.Width, m_capInsetsInternal.Origin.Y + m_capInsetsInternal.Size.Height, w - (m_capInsetsInternal.Origin.X - l + m_capInsetsInternal.Size.Width), h - (m_capInsetsInternal.Origin.Y - t + m_capInsetsInternal.Size.Height)), rotated); scale9Image.AddChild(bottomRight, 2, (int) Positions.pBottomRight); } ContentSize = rect.Size; AddChild(scale9Image); if (m_bSpritesGenerated) { // Restore color and opacity Opacity = opacity; Color = color; } m_bSpritesGenerated = true; return true; }
/** returns the tile (CCSprite) at a given a tile coordinate. The returned CCSprite will be already added to the CCTMXLayer. Don't add it again. The CCSprite can be treated like any other CCSprite: rotated, scaled, translated, opacity, color, etc. You can remove either by calling: - layer.removeChild(sprite, cleanup); - or layer.removeTileAt(ccp(x,y)); */ public CCSprite TileAt(CCPoint pos) { Debug.Assert(pos.X < m_tLayerSize.Width && pos.Y < m_tLayerSize.Height && pos.X >= 0 && pos.Y >= 0, "TMXLayer: invalid position"); Debug.Assert(m_pTiles != null && m_pAtlasIndexArray != null, "TMXLayer: the tiles map has been released"); CCSprite tile = null; uint gid = TileGIDAt(pos); // if GID == 0, then no tile is present if (gid != 0) { var z = (int) (pos.X + pos.Y * m_tLayerSize.Width); tile = (CCSprite) GetChildByTag(z); // tile not created yet. create it if (tile == null) { CCRect rect = m_pTileSet.RectForGID(gid); rect = CCMacros.CCRectanglePixelsToPoints(rect); tile = new CCSprite(); // // do the init AFTER the batch node is set so that the tile is set to // draw in batch mode instead of self draw mode. // tile.InitWithTexture(Texture, rect); tile.BatchNode = this; tile.Position = PositionAt(pos); tile.VertexZ = VertexZForPos(pos); tile.AnchorPoint = CCPoint.Zero; tile.Opacity = m_cOpacity; // tile.InitWithTexture(Texture, rect); // tile.BatchNode = this; int indexForZ = AtlasIndexForExistantZ(z); AddSpriteWithoutQuad(tile, indexForZ, z); } } return tile; }
private CCSprite ReusedTileWithRect(CCRect rect) { if (m_pReusedTile == null) { m_pReusedTile = new CCSprite(); m_pReusedTile.InitWithTexture(m_pobTextureAtlas.Texture, rect, false); m_pReusedTile.BatchNode = this; } else { // XXX: should not be re-init. Potential memeory leak. Not following best practices // XXX: it shall call directory [setRect:rect] m_pReusedTile.InitWithTexture(m_pobTextureAtlas.Texture, rect, false); // Since initWithTexture resets the batchNode, we need to re add it. // but should be removed once initWithTexture is not called again m_pReusedTile.BatchNode = this; } return m_pReusedTile; }
public void CreateFontChars() { int nextFontPositionX = 0; int nextFontPositionY = 0; //unsigned short prev = -1; int kerningAmount = 0; CCSize tmpSize = CCSize.Zero; int longestLine = 0; int totalHeight = 0; int quantityOfLines = 1; int stringLen = m_sString.Length; if (stringLen == 0) { return; } for (int i = 0; i < stringLen - 1; ++i) { if (m_sString[i] == '\n') { quantityOfLines++; } } totalHeight = m_pConfiguration.m_nCommonHeight * quantityOfLines; nextFontPositionY = 0 - (m_pConfiguration.m_nCommonHeight - m_pConfiguration.m_nCommonHeight * quantityOfLines); for (int i = 0; i < stringLen; i++) { char c = m_sString[i]; if (c == '\n') { nextFontPositionX = 0; nextFontPositionY -= m_pConfiguration.m_nCommonHeight; continue; } // unichar is a short, and an int is needed on HASH_FIND_INT CCBMFontConfiguration.ccBMFontDef fontDef = m_pConfiguration.m_pFontDefDictionary[c]; CCRect rect = fontDef.rect; rect = CCMacros.CCRectanglePixelsToPoints(rect); rect.Origin.X += m_tImageOffset.X; rect.Origin.Y += m_tImageOffset.Y; CCSprite fontChar; fontChar = (CCSprite) (GetChildByTag(i)); if (fontChar == null) { fontChar = new CCSprite(); fontChar.InitWithTexture(m_pobTextureAtlas.Texture, rect); AddChild(fontChar, 0, i); } else { // reusing fonts fontChar.SetTextureRect(rect, false, rect.Size); // restore to default in case they were modified fontChar.Visible = true; fontChar.Opacity = 255; } // See issue 1343. cast( signed short + unsigned integer ) == unsigned integer (sign is lost!) int yOffset = m_pConfiguration.m_nCommonHeight - fontDef.yOffset; var fontPos = new CCPoint((float) nextFontPositionX + fontDef.xOffset + fontDef.rect.Size.Width * 0.5f + kerningAmount, (float) nextFontPositionY + yOffset - rect.Size.Height * 0.5f * CCMacros.CCContentScaleFactor()); fontChar.Position = CCMacros.CCPointPixelsToPoints(fontPos); // update kerning nextFontPositionX += fontDef.xAdvance + kerningAmount; //prev = c; // Apply label properties fontChar.IsOpacityModifyRGB = m_bIsOpacityModifyRGB; // Color MUST be set before opacity, since opacity might change color if OpacityModifyRGB is on fontChar.Color = m_tColor; // only apply opacity if it is different than 255 ) // to prevent modifying the color too (issue #610) if (m_cOpacity != 255) { fontChar.Opacity = m_cOpacity; } if (longestLine < nextFontPositionX) { longestLine = nextFontPositionX; } } tmpSize.Width = longestLine; tmpSize.Height = totalHeight; ContentSize = CCMacros.CCSizePixelsToPoints(tmpSize); }