public bool UpdateWithBatchNode(CCSpriteBatchNode batchnode, CCRect rect, bool rotated, CCRect capInsets) { var opacity = Opacity; var color = Color; // Release old sprites RemoveAllChildren(true); scale9Image = batchnode; scale9Image.RemoveAllChildren(true); this.capInsets = capInsets; spriteFrameRotated = rotated; // If there is no given rect if (rect.Equals(CCRect.Zero)) { // Get the texture size as original CCSize textureSize = scale9Image.TextureAtlas.Texture.ContentSizeInPixels; rect = new CCRect(0, 0, textureSize.Width, textureSize.Height); } // Set the given rect's size as original size spriteRect = rect; originalSize = rect.Size; preferredSize = originalSize; capInsetsInternal = capInsets; float h = rect.Size.Height; float w = rect.Size.Width; // If there is no specified center region if (capInsetsInternal.Equals(CCRect.Zero)) { capInsetsInternal = new CCRect(w / 3, h / 3, w / 3, h / 3); } float left_w = capInsetsInternal.Origin.X; float center_w = capInsetsInternal.Size.Width; float right_w = rect.Size.Width - (left_w + center_w); float top_h = capInsetsInternal.Origin.Y; float center_h = 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) { 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(scale9Image.Texture, centerbounds); scale9Image.AddChild(centre, 0, (int)Positions.Centre); // Top top = new CCSprite(scale9Image.Texture, centerbounds); scale9Image.AddChild(top, 1, (int)Positions.Top); // Bottom bottom = new CCSprite(scale9Image.Texture, centerbottombounds); scale9Image.AddChild(bottom, 1, (int)Positions.Bottom); // Left left = new CCSprite(scale9Image.Texture, leftcenterbounds); scale9Image.AddChild(left, 1, (int)Positions.Left); // Right right = new CCSprite(scale9Image.Texture, rightcenterbounds); scale9Image.AddChild(right, 1, (int)Positions.Right); // Top left topLeft = new CCSprite(scale9Image.Texture, lefttopbounds); scale9Image.AddChild(topLeft, 2, (int)Positions.TopLeft); // Top right topRight = new CCSprite(scale9Image.Texture, righttopbounds); scale9Image.AddChild(topRight, 2, (int)Positions.TopRight); // Bottom left bottomLeft = new CCSprite(scale9Image.Texture, leftbottombounds); scale9Image.AddChild(bottomLeft, 2, (int)Positions.BottomLeft); // Bottom right bottomRight = new CCSprite(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(scale9Image.Texture, rotatedcenterbounds, true); //centre.InitWithTexture(scale9Image.Texture, rotatedcenterbounds, true); scale9Image.AddChild(centre, 0, (int)Positions.Centre); // Top top = new CCSprite(scale9Image.Texture, rotatedcentertopbounds, true); //top.InitWithTexture(scale9Image.Texture, rotatedcentertopbounds, true); scale9Image.AddChild(top, 1, (int)Positions.Top); // Bottom bottom = new CCSprite(scale9Image.Texture, rotatedcenterbottombounds, true); //bottom.InitWithTexture(scale9Image.Texture, rotatedcenterbottombounds, true); scale9Image.AddChild(bottom, 1, (int)Positions.Bottom); // Left left = new CCSprite(scale9Image.Texture, rotatedleftcenterbounds, true); //left.InitWithTexture(scale9Image.Texture, rotatedleftcenterbounds, true); scale9Image.AddChild(left, 1, (int)Positions.Left); // Right right = new CCSprite(scale9Image.Texture, rotatedrightcenterbounds, true); //right.InitWithTexture(scale9Image.Texture, rotatedrightcenterbounds, true); scale9Image.AddChild(right, 1, (int)Positions.Right); // Top left topLeft = new CCSprite(scale9Image.Texture, rotatedlefttopbounds, true); //topLeft.InitWithTexture(scale9Image.Texture, rotatedlefttopbounds, true); scale9Image.AddChild(topLeft, 2, (int)Positions.TopLeft); // Top right topRight = new CCSprite(scale9Image.Texture, rotatedrighttopbounds, true); //topRight.InitWithTexture(scale9Image.Texture, rotatedrighttopbounds, true); scale9Image.AddChild(topRight, 2, (int)Positions.TopRight); // Bottom left bottomLeft = new CCSprite(scale9Image.Texture, rotatedleftbottombounds, true); //bottomLeft.InitWithTexture(scale9Image.Texture, rotatedleftbottombounds, true); scale9Image.AddChild(bottomLeft, 2, (int)Positions.BottomLeft); // Bottom right bottomRight = new CCSprite(scale9Image.Texture, rotatedrightbottombounds, true); //bottomRight.InitWithTexture(scale9Image.Texture, rotatedrightbottombounds, true); scale9Image.AddChild(bottomRight, 2, (int)Positions.BottomRight); } ContentSize = rect.Size; AddChild(scale9Image); if (spritesGenerated) { // Restore color and opacity Opacity = opacity; Color = color; } spritesGenerated = true; return(true); }
private void UpdatePositionTransform() { // Since we are extending SpriteBatchNode we will have to do our own transforms on the sprites. // Translate values float x = Position.X; float y = Position.Y; var affineLocalTransform = CCAffineTransform.Identity; if (IgnoreAnchorPointForPosition) { x += anchorPointInPoints.X; y += anchorPointInPoints.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 (RotationX != 0 || RotationY != 0) { float radiansX = -CCMacros.CCDegreesToRadians(RotationX); float radiansY = -CCMacros.CCDegreesToRadians(RotationY); cx = (float)Math.Cos(radiansX); sx = (float)Math.Sin(radiansX); cy = (float)Math.Cos(radiansY); sy = (float)Math.Sin(radiansY); } bool needsSkewMatrix = (SkewX != 0f || SkewY != 0f); // optimization: // inline anchor point calculation if skew is not needed if (!needsSkewMatrix && !anchorPointInPoints.Equals(CCPoint.Zero)) { x += cy * -anchorPointInPoints.X * ScaleX + -sx * -anchorPointInPoints.Y * ScaleY; y += sy * -anchorPointInPoints.X * ScaleX + cx * -anchorPointInPoints.Y * ScaleY; } // Build Transform Matrix // Adjusted transform calculation for rotational skew affineLocalTransform.A = cy * ScaleX; affineLocalTransform.B = sy * ScaleX; affineLocalTransform.C = -sx * ScaleY; affineLocalTransform.D = cx * ScaleY; affineLocalTransform.Tx = x; affineLocalTransform.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(SkewY)), (float)Math.Tan(CCMacros.CCDegreesToRadians(SkewX)), 1.0f, 0.0f, 0.0f); affineLocalTransform = CCAffineTransform.Concat(skewMatrix, affineLocalTransform); // adjust anchor point if (!anchorPointInPoints.Equals(CCPoint.Zero)) { affineLocalTransform = CCAffineTransform.Translate(affineLocalTransform, -anchorPointInPoints.X, -anchorPointInPoints.Y); } } if (Children != null && Children.Count != 0) { CCNode[] elements = Children.Elements; for (int i = 0, count = Children.Count; i < count; i++) { var sprite = elements[i] as CCSprite; if (sprite.Visible) { var pos = affineLocalTransform.Transform(sprite.Position); sprite.PositionX = pos.X; sprite.PositionY = pos.Y; sprite.ScaleX = ScaleX; sprite.ScaleY = ScaleY; sprite.SkewX = SkewX; sprite.SkewY = SkewY; } } } }