CCAffineTransform absoluteTransform()
        {
            CCAffineTransform transform = CCAffineTransform.Identity;

            LHScene scene = ((LHNodeProtocol)_node).getScene();

            b2Vec2  b2Pos     = _body.Position;
            CCPoint globalPos = new CCPoint(b2Pos.x, b2Pos.y);            //[scene pointFromMeters:b2Pos];

            transform = CCAffineTransform.Translate(transform, globalPos.X, globalPos.Y);
            transform = CCAffineTransform.Rotate(transform, _body.Angle);


            transform = CCAffineTransform.Translate(transform, -_node.ContentSize.Width * 0.5f, -_node.ContentSize.Height * 0.5f);

            return(transform);
        }
Example #2
0
        public GradientPens()
        {
            _gradWidth  = new GradientPen(Microsoft.Xna.Framework.Color.Lime, Microsoft.Xna.Framework.Color.Blue, 15);
            _gradLength = new PathGradientPen(Microsoft.Xna.Framework.Color.Lime, Microsoft.Xna.Framework.Color.Blue, 15);

            PathBuilder pathBuilder = new PathBuilder()
            {
                CalculateLengths = true
            };

            pathBuilder.AddPath(StarPoints(new CCVector2(325, 75), 5, 50, 25, 0, false));

            _widthStar  = pathBuilder.Stroke(_gradWidth, PathType.Open);
            _lengthStar = pathBuilder.Stroke(_gradLength,
                                             CCAffineTransform.Translate(CCAffineTransform.Identity, 0, 125, 0),
                                             PathType.Open);
        }
        void SaveImage(object sender)
        {
            RemoveChildByTag(spriteTag);
            var renderSize   = canvasNode.BoundingRect.Size;
            var renderOrigin = canvasNode.BoundingRect.Origin;

            // here we calculate the translation of the rendering.  It should be the offset of BoundingRect Origin
            // this will then be passed to the Visit(ref CCAffineTransform) method.
            var translate = CCAffineTransform.Translate(canvasNode.AffineWorldTransform, -renderOrigin.X, -renderOrigin.Y);

            var rtm = new CCRenderTexture(renderSize, renderSize);

            rtm.BeginWithClear(CCColor4B.Green);
            canvasNode.Visit(ref translate);
            rtm.End();

            rtm.Texture.IsAntialiased = true;
            rtm.Sprite.AnchorPoint    = CCPoint.AnchorLowerLeft;
            rtm.Sprite.Opacity        = 127;
            AddChild(rtm.Sprite, 50, spriteTag);;
        }
        void CreateSlicedSprites()
        {
            float height = originalSize.Height;
            float width  = originalSize.Width;

            CCPoint offsetPosition = CCPoint.Zero;

            offsetPosition.X = (float)System.Math.Ceiling(offset.X + (originalSize.Width - spriteRect.Size.Width) / 2);
            offsetPosition.Y = (float)System.Math.Ceiling(offset.Y + (originalSize.Height - spriteRect.Size.Height) / 2);

            // If there is no specified center region
            if (capInsetsInternal == CCRect.Zero)
            {
                capInsetsInternal = new CCRect(width / 3, height / 3, width / 3, height / 3);
            }

            var originalRect = spriteRect;

            if (spriteFrameRotated)
            {
                originalRect = new CCRect(spriteRect.Origin.X - offsetPosition.Y,
                                          spriteRect.Origin.Y - offsetPosition.X,
                                          originalSize.Width, originalSize.Height);
            }
            else
            {
                originalRect = new CCRect(spriteRect.Origin.X - offsetPosition.Y,
                                          spriteRect.Origin.Y - offsetPosition.Y,
                                          originalSize.Width, originalSize.Height);
            }

            float leftWidth   = capInsetsInternal.Origin.X;
            float centerWidth = capInsetsInternal.Size.Width;
            float rightWidth  = originalRect.Size.Width - (leftWidth + centerWidth);

            float topHeight    = capInsetsInternal.Origin.Y;
            float centerHeight = capInsetsInternal.Size.Height;
            float bottomHeight = originalRect.Size.Height - (topHeight + centerHeight);

            // calculate rects

            // ... top row
            float x = 0.0f;
            float y = 0.0f;
            //why do we need pixelRect?
            var pixelRect = new CCRect(offsetPosition.X, offsetPosition.Y,
                                       spriteRect.Size.Width, spriteRect.Size.Height);

            // top left
            var leftTopBoundsOriginal = new CCRect(x, y, leftWidth, topHeight);
            var leftTopBounds         = leftTopBoundsOriginal;

            // top center
            x += leftWidth;
            var centerTopBounds = new CCRect(x, y, centerWidth, topHeight);

            // top right
            x += centerWidth;
            var rightTopBounds = new CCRect(x, y, rightWidth, topHeight);

            // ... center row
            x  = 0.0f;
            y  = 0.0f;
            y += topHeight;

            // center left
            var leftCenterBounds = new CCRect(x, y, leftWidth, centerHeight);

            // center center
            x += leftWidth;
            var centerBoundsOriginal = new CCRect(x, y, centerWidth, centerHeight);
            var centerBounds         = centerBoundsOriginal;

            // center right
            x += centerWidth;
            var rightCenterBounds = new CCRect(x, y, rightWidth, centerHeight);

            // ... bottom row
            x  = 0.0f;
            y  = 0.0f;
            y += topHeight;
            y += centerHeight;

            // bottom left
            var leftBottomBounds = new CCRect(x, y, leftWidth, bottomHeight);

            // bottom center
            x += leftWidth;
            var centerBottomBounds = new CCRect(x, y, centerWidth, bottomHeight);

            // bottom right
            x += centerWidth;
            var rightBottomBoundsOriginal = new CCRect(x, y, rightWidth, bottomHeight);
            var rightBottomBounds         = rightBottomBoundsOriginal;

            if ((capInsetsInternal.Origin.X + capInsetsInternal.Size.Width) <= originalSize.Width ||
                (capInsetsInternal.Origin.Y + capInsetsInternal.Size.Height) <= originalSize.Height)
            //in general case it is error but for legacy support we will check it
            {
                leftTopBounds      = intersectRect(leftTopBounds, pixelRect);
                centerTopBounds    = intersectRect(centerTopBounds, pixelRect);
                rightTopBounds     = intersectRect(rightTopBounds, pixelRect);
                leftCenterBounds   = intersectRect(leftCenterBounds, pixelRect);
                centerBounds       = intersectRect(centerBounds, pixelRect);
                rightCenterBounds  = intersectRect(rightCenterBounds, pixelRect);
                leftBottomBounds   = intersectRect(leftBottomBounds, pixelRect);
                centerBottomBounds = intersectRect(centerBottomBounds, pixelRect);
                rightBottomBounds  = intersectRect(rightBottomBounds, pixelRect);
            }
            else
            {
                //it is error but for legacy turn off clip system
                CCLog.Log("Scale9Sprite capInsetsInternal > originalSize");
            }

            var rotatedLeftTopBoundsOriginal     = leftTopBoundsOriginal;
            var rotatedCenterBoundsOriginal      = centerBoundsOriginal;
            var rotatedRightBottomBoundsOriginal = rightBottomBoundsOriginal;

            var rotatedCenterBounds       = centerBounds;
            var rotatedRightBottomBounds  = rightBottomBounds;
            var rotatedLeftBottomBounds   = leftBottomBounds;
            var rotatedRightTopBounds     = rightTopBounds;
            var rotatedLeftTopBounds      = leftTopBounds;
            var rotatedRightCenterBounds  = rightCenterBounds;
            var rotatedLeftCenterBounds   = leftCenterBounds;
            var rotatedCenterBottomBounds = centerBottomBounds;
            var rotatedCenterTopBounds    = centerTopBounds;

            if (!spriteFrameRotated)
            {
                CCAffineTransform t = CCAffineTransform.Identity;
                t = CCAffineTransform.Translate(t, originalRect.Origin.X, originalRect.Origin.Y);

                rotatedLeftTopBoundsOriginal     = CCAffineTransform.Transform(rotatedLeftTopBoundsOriginal, t);
                rotatedCenterBoundsOriginal      = CCAffineTransform.Transform(rotatedCenterBoundsOriginal, t);
                rotatedRightBottomBoundsOriginal = CCAffineTransform.Transform(rotatedRightBottomBoundsOriginal, t);

                rotatedCenterBounds       = CCAffineTransform.Transform(rotatedCenterBounds, t);
                rotatedRightBottomBounds  = CCAffineTransform.Transform(rotatedRightBottomBounds, t);
                rotatedLeftBottomBounds   = CCAffineTransform.Transform(rotatedLeftBottomBounds, t);
                rotatedRightTopBounds     = CCAffineTransform.Transform(rotatedRightTopBounds, t);
                rotatedLeftTopBounds      = CCAffineTransform.Transform(rotatedLeftTopBounds, t);
                rotatedRightCenterBounds  = CCAffineTransform.Transform(rotatedRightCenterBounds, t);
                rotatedLeftCenterBounds   = CCAffineTransform.Transform(rotatedLeftCenterBounds, t);
                rotatedCenterBottomBounds = CCAffineTransform.Transform(rotatedCenterBottomBounds, t);
                rotatedCenterTopBounds    = CCAffineTransform.Transform(rotatedCenterTopBounds, t);
            }
            else
            {
                // set up transformation of coordinates
                // to handle the case where the sprite is stored rotated
                // in the spritesheet
                // log("rotated");

                CCAffineTransform t = CCAffineTransform.Identity;
                t = CCAffineTransform.Translate(t, originalRect.Size.Height + originalRect.Origin.X,
                                                originalRect.Origin.Y);

                t = CCAffineTransform.Rotate(t, 1.57079633f);

                leftTopBoundsOriginal     = CCAffineTransform.Transform(leftTopBoundsOriginal, t);
                centerBoundsOriginal      = CCAffineTransform.Transform(centerBoundsOriginal, t);
                rightBottomBoundsOriginal = CCAffineTransform.Transform(rightBottomBoundsOriginal, t);

                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);

                rotatedLeftTopBoundsOriginal.Origin     = leftTopBoundsOriginal.Origin;
                rotatedCenterBoundsOriginal.Origin      = centerBoundsOriginal.Origin;
                rotatedRightBottomBoundsOriginal.Origin = rightBottomBoundsOriginal.Origin;

                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;
            }

            topLeftSize     = rotatedLeftTopBoundsOriginal.Size;
            centerSize      = rotatedCenterBoundsOriginal.Size;
            bottomRightSize = rotatedRightBottomBoundsOriginal.Size;

            if (spriteFrameRotated)
            {
                float offsetX = (rotatedCenterBounds.Origin.X + rotatedCenterBounds.Size.Height / 2)
                                - (rotatedCenterBoundsOriginal.Origin.X + rotatedCenterBoundsOriginal.Size.Height / 2);
                float offsetY = (rotatedCenterBoundsOriginal.Origin.Y + rotatedCenterBoundsOriginal.Size.Width / 2)
                                - (rotatedCenterBounds.Origin.Y + rotatedCenterBounds.Size.Width / 2);
                centerOffset.X = -offsetY;
                centerOffset.Y = offsetX;
            }
            else
            {
                float offsetX = (rotatedCenterBounds.Origin.X + rotatedCenterBounds.Size.Width / 2)
                                - (rotatedCenterBoundsOriginal.Origin.X + rotatedCenterBoundsOriginal.Size.Width / 2);
                float offsetY = (rotatedCenterBoundsOriginal.Origin.Y + rotatedCenterBoundsOriginal.Size.Height / 2)
                                - (rotatedCenterBounds.Origin.Y + rotatedCenterBounds.Size.Height / 2);
                centerOffset.X = offsetX;
                centerOffset.Y = offsetY;
            }

            // Centre
            if (rotatedCenterBounds.Size.Width > 0 && rotatedCenterBounds.Size.Height > 0)
            {
                centerSprite = new CCSprite(scale9Image.Texture,
                                            rotatedCenterBounds,
                                            spriteFrameRotated);
                AddProtectedChild(centerSprite);
            }

            // Top
            if (rotatedCenterTopBounds.Size.Width > 0 && rotatedCenterTopBounds.Size.Height > 0)
            {
                topSprite = new CCSprite(scale9Image.Texture,
                                         rotatedCenterTopBounds,
                                         spriteFrameRotated);
                AddProtectedChild(topSprite);
            }

            // Bottom
            if (rotatedCenterBottomBounds.Size.Width > 0 && rotatedCenterBottomBounds.Size.Height > 0)
            {
                bottomSprite = new CCSprite(scale9Image.Texture,
                                            rotatedCenterBottomBounds,
                                            spriteFrameRotated);
                AddProtectedChild(bottomSprite);
            }

            // Left
            if (rotatedLeftCenterBounds.Size.Width > 0 && rotatedLeftCenterBounds.Size.Height > 0)
            {
                leftSprite = new CCSprite(scale9Image.Texture,
                                          rotatedLeftCenterBounds,
                                          spriteFrameRotated);
                AddProtectedChild(leftSprite);
            }

            // Right
            if (rotatedRightCenterBounds.Size.Width > 0 && rotatedRightCenterBounds.Size.Height > 0)
            {
                rightSprite = new CCSprite(scale9Image.Texture,
                                           rotatedRightCenterBounds,
                                           spriteFrameRotated);
                AddProtectedChild(rightSprite);
            }

            // Top left
            if (rotatedLeftTopBounds.Size.Width > 0 && rotatedLeftTopBounds.Size.Height > 0)
            {
                topLeftSprite = new CCSprite(scale9Image.Texture,
                                             rotatedLeftTopBounds,
                                             spriteFrameRotated);
                AddProtectedChild(topLeftSprite);
            }

            // Top right
            if (rotatedRightTopBounds.Size.Width > 0 && rotatedRightTopBounds.Size.Height > 0)
            {
                topRightSprite = new CCSprite(scale9Image.Texture,
                                              rotatedRightTopBounds,
                                              spriteFrameRotated);
                AddProtectedChild(topRightSprite);
            }

            // Bottom left
            if (rotatedLeftBottomBounds.Size.Width > 0 && rotatedLeftBottomBounds.Size.Height > 0)
            {
                bottomLeftSprite = new CCSprite(scale9Image.Texture,
                                                rotatedLeftBottomBounds,
                                                spriteFrameRotated);
                AddProtectedChild(bottomLeftSprite);
            }

            // Bottom right
            if (rotatedRightBottomBounds.Size.Width > 0 && rotatedRightBottomBounds.Size.Height > 0)
            {
                bottomRightSprite = new CCSprite(scale9Image.Texture,
                                                 rotatedRightBottomBounds,
                                                 spriteFrameRotated);
                AddProtectedChild(bottomRightSprite);
            }
        }