コード例 #1
0
ファイル: CCFontAtlasCache.cs プロジェクト: swindy/CocosSharp
        public static CCFontAtlas GetFontAtlasFNT(string fontFileName, CCVector2 imageOffset = default(CCVector2))
        {
            string atlasName          = GenerateFontName(fontFileName, 0, GlyphCollection.Custom, false);
            var    atlasAlreadyExists = atlasMap.ContainsKey(atlasName);

            if (!atlasAlreadyExists)
            {
                var font = new CCFontFNT(fontFileName, imageOffset);
                if (font != null)
                {
                    var tempAtlas = font.CreateFontAtlas();
                    if (tempAtlas != null)
                    {
                        atlasMap[atlasName] = tempAtlas;
                        return(atlasMap[atlasName]);
                    }
                }
            }
            else
            {
                return(atlasMap[atlasName]);
            }


            return(null);
        }
コード例 #2
0
        public override void DrawCircle(b2Vec2 center, float radius, b2Color color)
        {
            if (!primitiveBatch.IsReady())
            {
                throw new InvalidOperationException("BeginCustomDraw must be called before drawing anything.");
            }
            const double increment = Math.PI * 2.0 / CircleSegments;
            double       theta     = 0.0;

            var       col   = color.ToCCColor4B();
            CCVector2 centr = center.ToCCVector2();

            for (int i = 0, count = CircleSegments; i < count; i++)
            {
                CCVector2 v1 = (centr + radius * new CCVector2((float)Math.Cos(theta), (float)Math.Sin(theta))) * PTMRatio;
                CCVector2 v2 = (centr +
                                radius *
                                new CCVector2((float)Math.Cos(theta + increment), (float)Math.Sin(theta + increment))) * PTMRatio;

                primitiveBatch.AddVertex(ref v1, col, PrimitiveType.LineList);
                primitiveBatch.AddVertex(ref v2, col, PrimitiveType.LineList);

                theta += increment;
            }
        }
コード例 #3
0
        public void AddVertex(ref CCVector2 vertex, CCColor4B color, PrimitiveType primitiveType)
        {
            if (!hasBegun)
            {
                throw new InvalidOperationException("Begin must be called before AddVertex can be called.");
            }

            if (primitiveType == PrimitiveType.LineStrip || primitiveType == PrimitiveType.TriangleStrip)
            {
                throw new NotSupportedException("The specified primitiveType is not supported by PrimitiveBatch.");
            }

            if (primitiveType == PrimitiveType.TriangleList)
            {
                if (triangleVertsCount >= triangleVertices.Length)
                {
                    FlushTriangles();
                }
                triangleVertices[triangleVertsCount].Position = new Vector3(vertex.ToVector2(), -0.1f);
                triangleVertices[triangleVertsCount].Color    = color.ToColor();
                triangleVertsCount++;
            }

            if (primitiveType == PrimitiveType.LineList)
            {
                if (lineVertsCount >= lineVertices.Length)
                {
                    FlushLines();
                }
                lineVertices[lineVertsCount].Position = new Vector3(vertex.ToVector2(), 0f);
                lineVertices[lineVertsCount].Color    = color.ToColor();
                lineVertsCount++;
            }
        }
コード例 #4
0
        public override void DrawSolidCircle(b2Vec2 center, float radius, b2Vec2 axis, b2Color color)
        {
            if (!primitiveBatch.IsReady())
            {
                throw new InvalidOperationException("BeginCustomDraw must be called before drawing anything.");
            }
            const double increment = Math.PI * 2.0 / CircleSegments;
            double       theta     = 0.0;

            var colorFill = color.ToCCColor4B() * 0.5f;
            var centr     = center.ToCCVector2();

            CCVector2 v0 = center.ToCCVector2() + radius * new CCVector2((float)Math.Cos(theta), (float)Math.Sin(theta));

            theta += increment;

            for (int i = 1; i < CircleSegments - 1; i++)
            {
                var v1 = centr + radius * new CCVector2((float)Math.Cos(theta), (float)Math.Sin(theta));
                var v2 = centr +
                         radius * new CCVector2((float)Math.Cos(theta + increment), (float)Math.Sin(theta + increment));

                primitiveBatch.AddVertex(ref v0, colorFill, PrimitiveType.TriangleList);
                primitiveBatch.AddVertex(ref v1, colorFill, PrimitiveType.TriangleList);
                primitiveBatch.AddVertex(ref v2, colorFill, PrimitiveType.TriangleList);

                theta += increment;
            }
            DrawCircle(center, radius, color);

            DrawSegment(center, center + axis * radius, color);
        }
コード例 #5
0
        /// <summary>
        /// Constructor to create a Bitmap Font configuration object from a .FNT configuration file
        /// string to be parsed and a Stream object of the Atlas Texture to be used.
        /// </summary>
        /// <param name="configInfo">String representation read from a .FNT configuration</param>
        /// <param name="atlasTexture">CCTexture2D Atlas to be used.</param>
        /// <param name="imageOffset">Image Offset</param>
        public CCFontFNT(string configInfo, CCTexture2D atlasTexture, CCVector2?imageOffset = null)
        {
            try
            {
                Configuration = new CCBMFontConfiguration(configInfo, string.Empty);
            }
            catch (Exception exc)
            {
                throw new ContentLoadException("Bitmap Font Configuration is invalid: " + exc.Message);
            }

            this.imageOffset = CCVector2.Zero;

            if (imageOffset.HasValue)
            {
                this.imageOffset = imageOffset.Value;
            }

            AtlasTexture = atlasTexture;

            if (Configuration != null)
            {
                IsFontConfigValid = true;
            }
        }
コード例 #6
0
ファイル: CCFontFNT.cs プロジェクト: zhuruvl/CocosSharp
        public CCFontFNT(string fntFilePath, CCVector2 imageOffset = default(CCVector2))
        {
            Configuration = CCBMFontConfiguration.FontConfigurationWithFile(fntFilePath);
            _imageOffset  = imageOffset;

            if (Configuration != null)
            {
                IsFontConfigValid = true;
            }
        }
コード例 #7
0
        public CCFontSpriteFont(string fntFilePath, float fontSize, CCVector2?imageOffset = null)
        {
            fontName      = fntFilePath;
            this.fontSize = fontSize;

            this.imageOffset = CCVector2.Zero;

            if (imageOffset.HasValue)
            {
                this.imageOffset = imageOffset.Value;
            }

            fontScale = 1.0f;
        }
コード例 #8
0
        /// <summary>
        /// Constructor to create a Bitmap Font configuration object from a file path within the
        /// Content directory
        /// </summary>
        /// <param name="fntFilePath">Path to the configuration file</param>
        /// <param name="imageOffset">Image Offset</param>
        public CCFontFNT(string fntFilePath, CCVector2?imageOffset = null)
        {
            Configuration    = CCBMFontConfiguration.FontConfigurationWithFile(fntFilePath);
            this.imageOffset = CCVector2.Zero;

            if (imageOffset.HasValue)
            {
                this.imageOffset = imageOffset.Value;
            }

            if (Configuration != null)
            {
                IsFontConfigValid = true;
            }
        }
コード例 #9
0
ファイル: CCFontAtlasCache.cs プロジェクト: swindy/CocosSharp
        public static CCFontAtlas GetFontAtlasFNT(CCFontFNT font, CCVector2 imageOffset = default(CCVector2))
        {
            if (font != null)
            {
                var atlasName = font.Configuration.AtlasName;
                var tempAtlas = font.CreateFontAtlas();
                if (tempAtlas != null)
                {
                    atlasMap[atlasName] = tempAtlas;
                    return(atlasMap[atlasName]);
                }
            }


            return(null);
        }
コード例 #10
0
 public CCFontSpriteFont(string fntFilePath, float fontSize, CCVector2 imageOffset = default(CCVector2))
 {
     fontName = fntFilePath;
     fontSize = fontSize;
 }
コード例 #11
0
        public virtual bool CollidesWith(CCMaskedSprite target, out CCPoint pt)
        {
            pt = CCPoint.Zero;
            CCAffineTransform affine1         = target.AffineLocalTransform;
            CCAffineTransform affine2         = target.AffineLocalTransform;
            CCRect            myBBInWorld     = Layer.VisibleBoundsWorldspace;
            CCRect            targetBBInWorld = target.BoundingBox;

            if (!myBBInWorld.IntersectsRect(targetBBInWorld))
            {
                return(false);
            }
            // Based upon http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series2D/Putting_CD_into_practice.php
            var affine1to2 = affine1 * CCAffineTransform.Invert(affine2);

            int width2  = (int)target.ContentSize.Width;
            int height2 = (int)target.ContentSize.Height;
            int width1  = (int)ContentSize.Width;
            int height1 = (int)ContentSize.Height;

            byte[] maskA = CollisionMask;
            byte[] maskB = target.CollisionMask;
            if (maskA == null || maskB == null)
            {
                return(false);
            }
            for (int x1 = 0; x1 < width1; x1++)
            {
                for (int y1 = 0; y1 < height1; y1++)
                {
                    var pos1 = new CCVector2(x1, y1);
                    var pos2 = CCVector2.Transform(pos1, affine1to2);

                    int x2 = (int)pos2.X;
                    int y2 = (int)pos2.Y;
                    if ((x2 >= 0) && (x2 < width2))
                    {
                        if ((y2 >= 0) && (y2 < height2))
                        {
                            int iA = x1 + (height1 - y1) * width1;
                            int iB = x2 + (height2 - y2) * width2;
                            if (iA >= maskA.Length || iB >= maskB.Length)
                            {
                                continue;
                            }
                            if (maskA[iA] > 0)
                            {
                                if (maskB[iB] > 0)
                                {
                                    CCVector2 screenPos = CCVector2.Transform(pos1, affine1);
                                    pt = new CCPoint(screenPos);
                                    return(true);
                                }
                            }
                        }
                    }
                }
            }

            return(false);
        }
コード例 #12
0
        void InitWithString(string text, CCSize dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment, string fontName, float fontSize)
        {
            try
            {
                Debug.Assert(dimensions.Width >= 0 || dimensions.Height >= 0);

                if (string.IsNullOrEmpty(text))
                {
                    return;
                }

                float loadedSize = fontSize;

                SpriteFont font = CCSpriteFontCache.SharedInstance.TryLoadFont(fontName, fontSize, out loadedSize);

                if (font == null)
                {
                    CCLog.Log("Failed to load default font. No font supported.");
                    return;
                }

                float scale = 1f;

                if (loadedSize != 0)
                {
                    scale = fontSize / loadedSize * CCSpriteFontCache.FontScale;
                }

                if (dimensions.Equals(CCSize.Zero))
                {
                    CCVector2 temp = font.MeasureString(text).ToCCVector2();
                    dimensions.Width  = temp.X * scale;
                    dimensions.Height = temp.Y * scale;
                }

                var textList = new List <String>();
                var nextText = new StringBuilder();

                string[] lineList = text.Split('\n');

                float spaceWidth = font.MeasureString(" ").X *scale;

                for (int j = 0; j < lineList.Length; ++j)
                {
                    string[] wordList = lineList[j].Split(' ');

                    float lineWidth = 0;
                    bool  firstWord = true;

                    for (int i = 0; i < wordList.Length; ++i)
                    {
                        float wordWidth = font.MeasureString(wordList[i]).X *scale;

                        if ((lineWidth + wordWidth) > dimensions.Width)
                        {
                            lineWidth = wordWidth;

                            if (nextText.Length > 0)
                            {
                                firstWord = true;
                                textList.Add(nextText.ToString());
                                nextText.Length = 0;
                            }
                            else
                            {
                                lineWidth += wordWidth;
                                firstWord  = false;
                                textList.Add(wordList[i]);
                                continue;
                            }
                        }
                        else
                        {
                            lineWidth += wordWidth;
                        }
                        if (!firstWord)
                        {
                            nextText.Append(' ');
                            lineWidth += spaceWidth;
                        }

                        nextText.Append(wordList[i]);
                        firstWord = false;
                    }

                    textList.Add(nextText.ToString());

                    nextText.Clear();
                }

                if (dimensions.Height == 0)
                {
                    dimensions.Height = textList.Count * font.LineSpacing * scale;
                }

                //*  for render to texture
                RenderTarget2D renderTarget = CCDrawManager.SharedDrawManager.CreateRenderTarget(
                    (int)dimensions.Width, (int)dimensions.Height,
                    DefaultAlphaPixelFormat, CCRenderTargetUsage.DiscardContents
                    );

                CCDrawManager.SharedDrawManager.CurrentRenderTarget = renderTarget;
                CCDrawManager.SharedDrawManager.Clear(CCColor4B.Transparent);

                SpriteBatch sb = CCDrawManager.SharedDrawManager.SpriteBatch;
                sb.Begin();

                float textHeight = textList.Count * font.LineSpacing * scale;
                float nextY      = 0;

                if (vAlignment == CCVerticalTextAlignment.Bottom)
                {
                    nextY = dimensions.Height - textHeight;
                }
                else if (vAlignment == CCVerticalTextAlignment.Center)
                {
                    nextY = (dimensions.Height - textHeight) / 2.0f;
                }

                for (int j = 0; j < textList.Count; ++j)
                {
                    string line = textList[j];

                    var position = new CCVector2(0, nextY);

                    if (hAlignment == CCTextAlignment.Right)
                    {
                        position.X = dimensions.Width - font.MeasureString(line).X *scale;
                    }
                    else if (hAlignment == CCTextAlignment.Center)
                    {
                        position.X = (dimensions.Width - font.MeasureString(line).X *scale) / 2.0f;
                    }

                    sb.DrawString(font, line, position.ToVector2(), Color.White, 0f, Vector2.Zero, scale, SpriteEffects.None, 0);

                    nextY += font.LineSpacing * scale;
                }

                sb.End();

                CCDrawManager.SharedDrawManager.XnaGraphicsDevice.RasterizerState = RasterizerState.CullNone;
                CCDrawManager.SharedDrawManager.DepthStencilState = DepthStencilState.Default;

                CCDrawManager.SharedDrawManager.CurrentRenderTarget = (RenderTarget2D)null;

                InitWithTexture(renderTarget, (CCSurfaceFormat)renderTarget.Format, true, false);
                cacheInfo.CacheType = CCTextureCacheType.String;
                cacheInfo.Data      = new CCStringCache()
                {
                    Dimensions = dimensions,
                    Text       = text,
                    FontName   = fontName,
                    FontSize   = fontSize,
                    HAlignment = hAlignment,
                    VAlignment = vAlignment
                };
            }
            catch (Exception ex)
            {
                CCLog.Log(ex.ToString());
            }
        }
コード例 #13
0
ファイル: CCMacros.cs プロジェクト: ztaksoftware/CocosSharp
 internal static Microsoft.Xna.Framework.Vector2 ToVector2(this CCVector2 point)
 {
     return(new Microsoft.Xna.Framework.Vector2(point.X, point.Y));
 }
コード例 #14
0
 public void AddVertex(CCVector2 vertex, CCColor4B color, PrimitiveType primitiveType)
 {
     AddVertex(ref vertex, color, primitiveType);
 }
コード例 #15
0
 public CCPoint(CCVector2 v)
 {
     X = v.X;
     Y = v.Y;
 }