コード例 #1
0
        public CCOrbitCameraState(CCOrbitCamera action, CCNode target)
            : base(action, target)
        {
            AngleX      = action.AngleX;
            AngleZ      = action.AngleZ;
            DeltaAngleX = action.DeltaAngleX;
            DeltaAngleZ = action.DeltaAngleZ;
            DeltaRadius = action.DeltaRadius;
            Radius      = action.Radius;

            // We are assuming the local camera will be centered directly at the target
            // => Add radius to the z position of local camera
            CCPoint3 fauxLocalCameraCenter = target.FauxLocalCameraCenter;

            fauxLocalCameraCenter.Z     += Radius;
            target.FauxLocalCameraCenter = fauxLocalCameraCenter;

            //CameraCenter = fauxLocalCameraCenter;

            RadDeltaZ = CCMacros.CCDegreesToRadians(DeltaAngleZ);
            RadDeltaX = CCMacros.CCDegreesToRadians(DeltaAngleX);

            // Only calculate the SpericalRadius when needed.
            if (float.IsNaN(Radius) || float.IsNaN(AngleZ) || float.IsNaN(AngleX))
            {
                float r, zenith, azimuth;
                SphericalRadius(out r, out zenith, out azimuth);

                if (float.IsNaN(Radius))
                {
                    Radius = r;
                }

                if (float.IsNaN(AngleZ))
                {
                    AngleZ = CCMacros.CCRadiansToDegrees(zenith);
                }

                if (float.IsNaN(AngleX))
                {
                    AngleX = CCMacros.CCRadiansToDegrees(azimuth);
                }
            }

            RadZ = CCMacros.CCDegreesToRadians(AngleZ);
            RadX = CCMacros.CCDegreesToRadians(AngleX);
        }
コード例 #2
0
        protected void UpdateSliderPosition(CCPoint location)
        {
            // Clamp the position of the icon within the circle
            CCRect BackgroundBox = Background.BoundingBox;

            float centerX = StartPos.X + BackgroundBox.Size.Width * 0.5f;
            float centerY = StartPos.Y + BackgroundBox.Size.Height * 0.5f;

            float dx = location.X - centerX;
            float dy = location.Y - centerY;

            // Update angle by using the direction of the location
            var   angle    = (float)Math.Atan2(dy, dx);
            float angleDeg = CCMacros.CCRadiansToDegrees(angle) + 180.0f;

            // use the position / Slider width to determin the percentage the dragger is at
            Hue = angleDeg;

            OnValueChanged();
        }
コード例 #3
0
ファイル: CCSprite.cs プロジェクト: smitcham/CocosSharp
        void UpdateSpriteTextureQuads()
        {
            if (!Visible)
            {
                quad.BottomRight.Vertices =
                    quad.TopLeft.Vertices = quad.TopRight.Vertices = quad.BottomLeft.Vertices = new CCVertex3F(0, 0, 0);
            }
            else
            {
                CCPoint relativeOffset = unflippedOffsetPositionFromCenter;

                if (flipX)
                {
                    relativeOffset.X = -relativeOffset.X;
                }
                if (flipY)
                {
                    relativeOffset.Y = -relativeOffset.Y;
                }

                CCPoint centerPoint = UntrimmedSizeInPixels.Center + relativeOffset;
                CCPoint subRectOrigin;
                subRectOrigin.X = centerPoint.X - textureRectInPixels.Size.Width / 2.0f;
                subRectOrigin.Y = centerPoint.Y - textureRectInPixels.Size.Height / 2.0f;

                CCRect subRectRatio = CCRect.Zero;

                if (UntrimmedSizeInPixels.Width > 0 && UntrimmedSizeInPixels.Height > 0)
                {
                    subRectRatio = new CCRect(
                        subRectOrigin.X / UntrimmedSizeInPixels.Width,
                        subRectOrigin.Y / UntrimmedSizeInPixels.Height,
                        textureRectInPixels.Size.Width / UntrimmedSizeInPixels.Width,
                        textureRectInPixels.Size.Height / UntrimmedSizeInPixels.Height);
                }

                // Atlas: Vertex
                float x1 = subRectRatio.Origin.X * ContentSize.Width;
                float y1 = subRectRatio.Origin.Y * ContentSize.Height;
                float x2 = x1 + (subRectRatio.Size.Width * ContentSize.Width);
                float y2 = y1 + (subRectRatio.Size.Height * ContentSize.Height);

                // Don't set z-value: The node's transform will be set to include z offset
                quad.BottomLeft.Vertices  = new CCVertex3F(x1, y1, 0);
                quad.BottomRight.Vertices = new CCVertex3F(x2, y1, 0);
                quad.TopLeft.Vertices     = new CCVertex3F(x1, y2, 0);
                quad.TopRight.Vertices    = new CCVertex3F(x2, y2, 0);

                CCTexture2D tex = batchNode != null ? textureAtlas.Texture : texture;
                if (tex == null)
                {
                    return;
                }

                float atlasWidth  = tex.PixelsWide;
                float atlasHeight = tex.PixelsHigh;

                float left, right, top, bottom;

                if (IsTextureRectRotated)
                {
                    #if CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
                    left   = (2 * textureRectInPixels.Origin.X + 1) / (2 * atlasWidth);
                    right  = left + (textureRectInPixels.Size.Height * 2 - 2) / (2 * atlasWidth);
                    top    = (2 * textureRectInPixels.Origin.Y + 1) / (2 * atlasHeight);
                    bottom = top + (textureRectInPixels.Size.Width * 2 - 2) / (2 * atlasHeight);
                    #else
                    left   = textureRectInPixels.Origin.X / atlasWidth;
                    right  = (textureRectInPixels.Origin.X + textureRectInPixels.Size.Height) / atlasWidth;
                    top    = textureRectInPixels.Origin.Y / atlasHeight;
                    bottom = (textureRectInPixels.Origin.Y + textureRectInPixels.Size.Width) / atlasHeight;
                    #endif

                    if (flipX)
                    {
                        CCMacros.CCSwap(ref top, ref bottom);
                    }

                    if (flipY)
                    {
                        CCMacros.CCSwap(ref left, ref right);
                    }

                    quad.BottomLeft.TexCoords.U  = left;
                    quad.BottomLeft.TexCoords.V  = top;
                    quad.BottomRight.TexCoords.U = left;
                    quad.BottomRight.TexCoords.V = bottom;
                    quad.TopLeft.TexCoords.U     = right;
                    quad.TopLeft.TexCoords.V     = top;
                    quad.TopRight.TexCoords.U    = right;
                    quad.TopRight.TexCoords.V    = bottom;
                }
                else
                {
                    #if CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
                    left   = (2 * textureRectInPixels.Origin.X + 1) / (2 * atlasWidth);
                    right  = left + (textureRectInPixels.Size.Width * 2 - 2) / (2 * atlasWidth);
                    top    = (2 * textureRectInPixels.Origin.Y + 1) / (2 * atlasHeight);
                    bottom = top + (textureRectInPixels.Size.Height * 2 - 2) / (2 * atlasHeight);
                    #else
                    left   = textureRectInPixels.Origin.X / atlasWidth;
                    right  = (textureRectInPixels.Origin.X + textureRectInPixels.Size.Width) / atlasWidth;
                    top    = textureRectInPixels.Origin.Y / atlasHeight;
                    bottom = (textureRectInPixels.Origin.Y + textureRectInPixels.Size.Height) / atlasHeight;
                    #endif

                    if (flipX)
                    {
                        CCMacros.CCSwap(ref left, ref right);
                    }

                    if (flipY)
                    {
                        CCMacros.CCSwap(ref top, ref bottom);
                    }

                    quad.BottomLeft.TexCoords.U  = left;
                    quad.BottomLeft.TexCoords.V  = bottom;
                    quad.BottomRight.TexCoords.U = right;
                    quad.BottomRight.TexCoords.V = bottom;
                    quad.TopLeft.TexCoords.U     = left;
                    quad.TopLeft.TexCoords.V     = top;
                    quad.TopRight.TexCoords.U    = right;
                    quad.TopRight.TexCoords.V    = top;
                }
            }

            UpdateTransformedSpriteTextureQuads();
        }
コード例 #4
0
        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;
                    }
                }
            }
        }
コード例 #5
0
        void UpdateSpriteTextureQuadsCallback(ref CCV3F_C4B_T2F_Quad[] quads)
        {
            if (!Visible)
            {
                quads[0].BottomRight.Vertices             = quads[0].TopLeft.Vertices
                                                          = quads[0].TopRight.Vertices = quads[0].BottomLeft.Vertices = CCVertex3F.Zero;
            }
            else
            {
                CCPoint relativeOffset = unflippedOffsetPositionFromCenter;

                if (flipX)
                {
                    relativeOffset.X = -relativeOffset.X;
                }
                if (flipY)
                {
                    relativeOffset.Y = -relativeOffset.Y;
                }

                CCPoint centerPoint = UntrimmedSizeInPixels.Center + relativeOffset;
                CCPoint subRectOrigin;
                subRectOrigin.X = centerPoint.X - textureRectInPixels.Size.Width / 2.0f;
                subRectOrigin.Y = centerPoint.Y - textureRectInPixels.Size.Height / 2.0f;

                CCRect subRectRatio = CCRect.Zero;

                if (UntrimmedSizeInPixels.Width > 0 && UntrimmedSizeInPixels.Height > 0)
                {
                    subRectRatio = new CCRect(
                        subRectOrigin.X / UntrimmedSizeInPixels.Width,
                        subRectOrigin.Y / UntrimmedSizeInPixels.Height,
                        textureRectInPixels.Size.Width / UntrimmedSizeInPixels.Width,
                        textureRectInPixels.Size.Height / UntrimmedSizeInPixels.Height);
                }

                // Atlas: Vertex
                float x1 = subRectRatio.Origin.X * ContentSize.Width;
                float y1 = subRectRatio.Origin.Y * ContentSize.Height;
                float x2 = x1 + (subRectRatio.Size.Width * ContentSize.Width);
                float y2 = y1 + (subRectRatio.Size.Height * ContentSize.Height);

                // Don't set z-value: The node's transform will be set to include z offset
                quads[0].BottomLeft.Vertices  = new CCVertex3F(x1, y1, 0);
                quads[0].BottomRight.Vertices = new CCVertex3F(x2, y1, 0);
                quads[0].TopLeft.Vertices     = new CCVertex3F(x1, y2, 0);
                quads[0].TopRight.Vertices    = new CCVertex3F(x2, y2, 0);

                if (Texture == null)
                {
                    return;
                }

                float atlasWidth  = Texture.PixelsWide;
                float atlasHeight = Texture.PixelsHigh;

                float left, right, top, bottom;
                float offsetW = HalfTexelOffset ? 0.5f / atlasWidth : 0.0f;
                float offsetH = HalfTexelOffset ? 0.5f / atlasHeight : 0.0f;

                if (IsTextureRectRotated)
                {
                    left   = textureRectInPixels.Origin.X / atlasWidth + offsetW;
                    right  = (textureRectInPixels.Origin.X + textureRectInPixels.Size.Height) / atlasWidth - offsetW;
                    top    = textureRectInPixels.Origin.Y / atlasHeight + offsetH;
                    bottom = (textureRectInPixels.Origin.Y + textureRectInPixels.Size.Width) / atlasHeight - offsetH;

                    if (flipX)
                    {
                        CCMacros.CCSwap(ref top, ref bottom);
                    }

                    if (flipY)
                    {
                        CCMacros.CCSwap(ref left, ref right);
                    }

                    quads[0].BottomLeft.TexCoords.U  = left;
                    quads[0].BottomLeft.TexCoords.V  = top;
                    quads[0].BottomRight.TexCoords.U = left;
                    quads[0].BottomRight.TexCoords.V = bottom;
                    quads[0].TopLeft.TexCoords.U     = right;
                    quads[0].TopLeft.TexCoords.V     = top;
                    quads[0].TopRight.TexCoords.U    = right;
                    quads[0].TopRight.TexCoords.V    = bottom;
                }
                else
                {
                    left   = textureRectInPixels.Origin.X / atlasWidth + offsetW;
                    right  = (textureRectInPixels.Origin.X + textureRectInPixels.Size.Width) / atlasWidth - offsetW;
                    top    = textureRectInPixels.Origin.Y / atlasHeight + offsetH;
                    bottom = (textureRectInPixels.Origin.Y + textureRectInPixels.Size.Height) / atlasHeight - offsetH;

                    if (flipX)
                    {
                        CCMacros.CCSwap(ref left, ref right);
                    }

                    if (flipY)
                    {
                        CCMacros.CCSwap(ref top, ref bottom);
                    }

                    quads[0].BottomLeft.TexCoords.U  = left;
                    quads[0].BottomLeft.TexCoords.V  = bottom;
                    quads[0].BottomRight.TexCoords.U = right;
                    quads[0].BottomRight.TexCoords.V = bottom;
                    quads[0].TopLeft.TexCoords.U     = left;
                    quads[0].TopLeft.TexCoords.V     = top;
                    quads[0].TopRight.TexCoords.U    = right;
                    quads[0].TopRight.TexCoords.V    = top;
                }
            }
        }