public bool UpdateWithBatchNode(CCSpriteBatchNode batchnode, CCRect rect, bool rotated, CCRect capInsets) { var opacity = Opacity; var color = Color; // Release old sprites RemoveAllChildrenWithCleanup(true); _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; float h = rect.Size.Height; float w = rect.Size.Width; // If there is no specified center region if (m_capInsetsInternal.Equals(CCRect.Zero)) { m_capInsetsInternal = new CCRect(w / 3, h / 3, w / 3, h / 3); } float left_w = m_capInsetsInternal.Origin.X; float center_w = m_capInsetsInternal.Size.Width; float right_w = rect.Size.Width - (left_w + center_w); float top_h = m_capInsetsInternal.Origin.Y; float center_h = m_capInsetsInternal.Size.Height; float bottom_h = rect.Size.Height - (top_h + center_h); // calculate rects // ... top row float x = 0.0f; float y = 0.0f; // top left CCRect lefttopbounds = new CCRect(x, y, left_w, top_h); // top center x += left_w; CCRect centertopbounds = new CCRect(x, y, center_w, top_h); // top right x += center_w; CCRect righttopbounds = new CCRect(x, y, right_w, top_h); // ... center row x = 0.0f; y = 0.0f; y += top_h; // center left CCRect leftcenterbounds = new CCRect(x, y, left_w, center_h); // center center x += left_w; CCRect centerbounds = new CCRect(x, y, center_w, center_h); // center right x += center_w; CCRect rightcenterbounds = new CCRect(x, y, right_w, center_h); // ... bottom row x = 0.0f; y = 0.0f; y += top_h; y += center_h; // bottom left CCRect leftbottombounds = new CCRect(x, y, left_w, bottom_h); // bottom center x += left_w; CCRect centerbottombounds = new CCRect(x, y, center_w, bottom_h); // bottom right x += center_w; CCRect rightbottombounds = new CCRect(x, y, right_w, bottom_h); if (!rotated) { // CCLog("!rotated"); CCAffineTransform t = CCAffineTransform.Identity; t = CCAffineTransform.Translate(t, rect.Origin.X, rect.Origin.Y); centerbounds = CCAffineTransform.Transform(centerbounds, t); rightbottombounds = CCAffineTransform.Transform(rightbottombounds, t); leftbottombounds = CCAffineTransform.Transform(leftbottombounds, t); righttopbounds = CCAffineTransform.Transform(righttopbounds, t); lefttopbounds = CCAffineTransform.Transform(lefttopbounds, t); rightcenterbounds = CCAffineTransform.Transform(rightcenterbounds, t); leftcenterbounds = CCAffineTransform.Transform(leftcenterbounds, t); centerbottombounds = CCAffineTransform.Transform(centerbottombounds, t); centertopbounds = CCAffineTransform.Transform(centertopbounds, t); // Centre _centre = new CCSprite(); _centre.InitWithTexture(_scale9Image.Texture, centerbounds); _scale9Image.AddChild(_centre, 0, (int)Positions.Centre); // Top _top = new CCSprite(); _top.InitWithTexture(_scale9Image.Texture, centertopbounds); _scale9Image.AddChild(_top, 1, (int)Positions.Top); // Bottom _bottom = new CCSprite(); _bottom.InitWithTexture(_scale9Image.Texture, centerbottombounds); _scale9Image.AddChild(_bottom, 1, (int)Positions.Bottom); // Left _left = new CCSprite(); _left.InitWithTexture(_scale9Image.Texture, leftcenterbounds); _scale9Image.AddChild(_left, 1, (int)Positions.Left); // Right _right = new CCSprite(); _right.InitWithTexture(_scale9Image.Texture, rightcenterbounds); _scale9Image.AddChild(_right, 1, (int)Positions.Right); // Top left _topLeft = new CCSprite(); _topLeft.InitWithTexture(_scale9Image.Texture, lefttopbounds); _scale9Image.AddChild(_topLeft, 2, (int)Positions.TopLeft); // Top right _topRight = new CCSprite(); _topRight.InitWithTexture(_scale9Image.Texture, righttopbounds); _scale9Image.AddChild(_topRight, 2, (int)Positions.TopRight); // Bottom left _bottomLeft = new CCSprite(); _bottomLeft.InitWithTexture(_scale9Image.Texture, leftbottombounds); _scale9Image.AddChild(_bottomLeft, 2, (int)Positions.BottomLeft); // Bottom right _bottomRight = new CCSprite(); _bottomRight.InitWithTexture(_scale9Image.Texture, rightbottombounds); _scale9Image.AddChild(_bottomRight, 2, (int)Positions.BottomRight); } else { // set up transformation of coordinates // to handle the case where the sprite is stored rotated // in the spritesheet // CCLog("rotated"); CCAffineTransform t = CCAffineTransform.Identity; CCRect rotatedcenterbounds = centerbounds; CCRect rotatedrightbottombounds = rightbottombounds; CCRect rotatedleftbottombounds = leftbottombounds; CCRect rotatedrighttopbounds = righttopbounds; CCRect rotatedlefttopbounds = lefttopbounds; CCRect rotatedrightcenterbounds = rightcenterbounds; CCRect rotatedleftcenterbounds = leftcenterbounds; CCRect rotatedcenterbottombounds = centerbottombounds; CCRect rotatedcentertopbounds = centertopbounds; t = CCAffineTransform.Translate(t, rect.Size.Height + rect.Origin.X, rect.Origin.Y); t = CCAffineTransform.Rotate(t, 1.57079633f); centerbounds = CCAffineTransform.Transform(centerbounds, t); rightbottombounds = CCAffineTransform.Transform(rightbottombounds, t); leftbottombounds = CCAffineTransform.Transform(leftbottombounds, t); righttopbounds = CCAffineTransform.Transform(righttopbounds, t); lefttopbounds = CCAffineTransform.Transform(lefttopbounds, t); rightcenterbounds = CCAffineTransform.Transform(rightcenterbounds, t); leftcenterbounds = CCAffineTransform.Transform(leftcenterbounds, t); centerbottombounds = CCAffineTransform.Transform(centerbottombounds, t); centertopbounds = CCAffineTransform.Transform(centertopbounds, t); rotatedcenterbounds.Origin = centerbounds.Origin; rotatedrightbottombounds.Origin = rightbottombounds.Origin; rotatedleftbottombounds.Origin = leftbottombounds.Origin; rotatedrighttopbounds.Origin = righttopbounds.Origin; rotatedlefttopbounds.Origin = lefttopbounds.Origin; rotatedrightcenterbounds.Origin = rightcenterbounds.Origin; rotatedleftcenterbounds.Origin = leftcenterbounds.Origin; rotatedcenterbottombounds.Origin = centerbottombounds.Origin; rotatedcentertopbounds.Origin = centertopbounds.Origin; // Centre _centre = new CCSprite(); _centre.InitWithTexture(_scale9Image.Texture, rotatedcenterbounds, true); _scale9Image.AddChild(_centre, 0, (int)Positions.Centre); // Top _top = new CCSprite(); _top.InitWithTexture(_scale9Image.Texture, rotatedcentertopbounds, true); _scale9Image.AddChild(_top, 1, (int)Positions.Top); // Bottom _bottom = new CCSprite(); _bottom.InitWithTexture(_scale9Image.Texture, rotatedcenterbottombounds, true); _scale9Image.AddChild(_bottom, 1, (int)Positions.Bottom); // Left _left = new CCSprite(); _left.InitWithTexture(_scale9Image.Texture, rotatedleftcenterbounds, true); _scale9Image.AddChild(_left, 1, (int)Positions.Left); // Right _right = new CCSprite(); _right.InitWithTexture(_scale9Image.Texture, rotatedrightcenterbounds, true); _scale9Image.AddChild(_right, 1, (int)Positions.Right); // Top left _topLeft = new CCSprite(); _topLeft.InitWithTexture(_scale9Image.Texture, rotatedlefttopbounds, true); _scale9Image.AddChild(_topLeft, 2, (int)Positions.TopLeft); // Top right _topRight = new CCSprite(); _topRight.InitWithTexture(_scale9Image.Texture, rotatedrighttopbounds, true); _scale9Image.AddChild(_topRight, 2, (int)Positions.TopRight); // Bottom left _bottomLeft = new CCSprite(); _bottomLeft.InitWithTexture(_scale9Image.Texture, rotatedleftbottombounds, true); _scale9Image.AddChild(_bottomLeft, 2, (int)Positions.BottomLeft); // Bottom right _bottomRight = new CCSprite(); _bottomRight.InitWithTexture(_scale9Image.Texture, rotatedrightbottombounds, true); _scale9Image.AddChild(_bottomRight, 2, (int)Positions.BottomRight); } ContentSize = rect.Size; AddChild(_scale9Image); if (m_bSpritesGenerated) { // Restore color and opacity Opacity = opacity; Color = color; } m_bSpritesGenerated = true; return(true); }
public virtual CCAffineTransform NodeToParentTransform() { if (m_bTransformDirty) { // Translate values float x = m_obPosition.X; float y = m_obPosition.Y; if (m_bIgnoreAnchorPointForPosition) { x += m_obAnchorPointInPoints.X; y += m_obAnchorPointInPoints.Y; } // Rotation values // Change rotation code to handle X and Y // If we skew with the exact same value for both x and y then we're simply just rotating float cx = 1, sx = 0, cy = 1, sy = 0; if (m_fRotationX != 0 || m_fRotationY != 0) { float radiansX = -CCMacros.CCDegreesToRadians(m_fRotationX); float radiansY = -CCMacros.CCDegreesToRadians(m_fRotationY); cx = (float)Math.Cos(radiansX); sx = (float)Math.Sin(radiansX); cy = (float)Math.Cos(radiansY); sy = (float)Math.Sin(radiansY); } bool needsSkewMatrix = (m_fSkewX != 0f || m_fSkewY != 0f); // optimization: // inline anchor point calculation if skew is not needed if (!needsSkewMatrix && !m_obAnchorPointInPoints.Equals(CCPoint.Zero)) { x += cy * -m_obAnchorPointInPoints.X * m_fScaleX + -sx * -m_obAnchorPointInPoints.Y * m_fScaleY; y += sy * -m_obAnchorPointInPoints.X * m_fScaleX + cx * -m_obAnchorPointInPoints.Y * m_fScaleY; } // Build Transform Matrix // Adjusted transform calculation for rotational skew m_sTransform.a = cy * m_fScaleX; m_sTransform.b = sy * m_fScaleX; m_sTransform.c = -sx * m_fScaleY; m_sTransform.d = cx * m_fScaleY; m_sTransform.tx = x; m_sTransform.ty = y; // XXX: Try to inline skew // If skew is needed, apply skew and then anchor point if (needsSkewMatrix) { var skewMatrix = new CCAffineTransform( 1.0f, (float)Math.Tan(CCMacros.CCDegreesToRadians(m_fSkewY)), (float)Math.Tan(CCMacros.CCDegreesToRadians(m_fSkewX)), 1.0f, 0.0f, 0.0f); m_sTransform = CCAffineTransform.Concat(skewMatrix, m_sTransform); // adjust anchor point if (!m_obAnchorPointInPoints.Equals(CCPoint.Zero)) { m_sTransform = CCAffineTransform.Translate(m_sTransform, -m_obAnchorPointInPoints.X, -m_obAnchorPointInPoints.Y); } } if (m_bAdditionalTransformDirty) { m_sTransform.Concat(ref m_sAdditionalTransform); m_bAdditionalTransformDirty = false; } m_bTransformDirty = false; } return(m_sTransform); }
public virtual CCAffineTransform NodeToParentTransform() { if (m_bIsTransformDirty) { // Translate values float x = m_tPosition.X; float y = m_tPosition.Y; if (m_bIgnoreAnchorPointForPosition) { x += m_tAnchorPointInPoints.X; y += m_tAnchorPointInPoints.Y; } // Rotation values float c = 1, s = 0; if (m_fRotation != 0.0f) { float radians = -CCMacros.CCDegreesToRadians(m_fRotation); c = (float)Math.Cos(radians); s = (float)Math.Sin(radians); } bool needsSkewMatrix = (m_fSkewX != 0f || m_fSkewY != 0f); // optimization: // inline anchor point calculation if skew is not needed if (!needsSkewMatrix && !m_tAnchorPointInPoints.Equals(CCPoint.Zero)) { x += c * -m_tAnchorPointInPoints.X * m_fScaleX + -s * -m_tAnchorPointInPoints.Y * m_fScaleY; y += s * -m_tAnchorPointInPoints.X * m_fScaleX + c * -m_tAnchorPointInPoints.Y * m_fScaleY; } // Build Transform Matrix //m_tTransform = new CCAffineTransform( // c * m_fScaleX, s * m_fScaleX, // -s * m_fScaleY, c * m_fScaleY, // x, y); // Build Transform Matrix m_tTransform.a = c * m_fScaleX; m_tTransform.b = s * m_fScaleX; m_tTransform.c = -s * m_fScaleY; m_tTransform.d = c * m_fScaleY; m_tTransform.tx = x; m_tTransform.ty = y; // XXX: Try to inline skew // If skew is needed, apply skew and then anchor point if (needsSkewMatrix) { var skewMatrix = new CCAffineTransform( 1.0f, (float)Math.Tan(CCMacros.CCDegreesToRadians(m_fSkewY)), (float)Math.Tan(CCMacros.CCDegreesToRadians(m_fSkewX)), 1.0f, 0.0f, 0.0f); m_tTransform = CCAffineTransform.Concat(skewMatrix, m_tTransform); // adjust anchor point if (!m_tAnchorPointInPoints.Equals(CCPoint.Zero)) { m_tTransform = CCAffineTransform.Translate(m_tTransform, -m_tAnchorPointInPoints.X, -m_tAnchorPointInPoints.Y); } } m_bIsTransformDirty = false; } return(m_tTransform); }