Beispiel #1
0
 public static XElement ToXml(this Microsoft.Xna.Framework.Color value, string name)
 {
     return(new XElement(name,
                         new XAttribute("A", value.A),
                         new XAttribute("R", value.R),
                         new XAttribute("G", value.G),
                         new XAttribute("B", value.B)));
 }
Beispiel #2
0
 public static Microsoft.Xna.Framework.Color FromXml(this Microsoft.Xna.Framework.Color value, XElement xEle)
 {
     return(new Microsoft.Xna.Framework.Color(
                Byte.Parse(xEle.Attribute("A").Value, CultureInfo.InvariantCulture),
                Byte.Parse(xEle.Attribute("R").Value, CultureInfo.InvariantCulture),
                Byte.Parse(xEle.Attribute("G").Value, CultureInfo.InvariantCulture),
                Byte.Parse(xEle.Attribute("B").Value, CultureInfo.InvariantCulture)
                ));
 }
Beispiel #3
0
        /// <summary>
        ///     Will perform some reflection to load the specified ttf file into the resource service. This method will first
        ///     rasterize the font using System.Drawing, and will then construct a SpriteFont, as a result this method is very
        ///     slow.
        /// </summary>
        /// <param name="path"></param>
        /// <param name="name"></param>
        public static void LoadFont(string path, int size, string style, char start, char end, string name)
        {
            //Get SpriteFont Constructor
            var constructor = typeof(SpriteFont).GetConstructor(
                BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public,
                null, new[]
            {
                typeof(Texture2D),        //texture
                typeof(List <Rectangle>), //glyphBounds
                typeof(List <Rectangle>), //cropping
                typeof(List <char>),      //characters
                typeof(int),              //lineSpacing
                typeof(float),            //spacing
                typeof(List <Vector3>),   //kerning
                typeof(char?)             //defaultChar
            }, new ParameterModifier[0]);

            //constructor.Invoke(new object[] { null, null, null, null, null, null, null, null });

            var pfcoll = new PrivateFontCollection();

            pfcoll.AddFontFile(path);
            var ff         = pfcoll.Families[0];
            var targetSize = (int)Math.Sqrt(end - start);
            var width      = (targetSize + 10) * size;
            var height     = (targetSize + 10) * size;

            var target = new Bitmap(width, height);

            //for (int i = 0; i < width; i++)
            //{
            //    for (int o = 0; o < height; o++)
            //    {
            //        //Debug.WriteLine(target.GetPixel(i, o));
            //    }
            //}
            target.MakeTransparent();


            var bounds      = new List <Rectangle>();
            var cropping    = new List <Rectangle>();
            var chars       = new List <char>();
            var kerning     = new List <Vector3>();
            var rect        = new System.Drawing.RectangleF(0, 0, 0, 0);
            var lineSpacing = 0;
            var spacing     = 0f;
            var transparent = Color.FromArgb(0, 0, 0, 0);

            using (var graphics = System.Drawing.Graphics.FromImage(target))
            {
                graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
                graphics.SmoothingMode     = SmoothingMode.None;
                using (var font = new Font(ff, size, GraphicsUnit.Pixel))
                {
                    lineSpacing = font.FontFamily.GetLineSpacing(FontStyle.Regular);
                    for (int i = start; i <= end; i++)
                    {
                        var measure = TextRenderer.MeasureText(((char)i).ToString(), font);
                        rect.Width  = measure.Width;
                        rect.Height = measure.Height;
                        if (rect.Right > width)
                        {
                            rect.X  = 0;
                            rect.Y += measure.Height + 10;
                        }

                        TextRenderer.DrawText(graphics, ((char)i).ToString(), font,
                                              new Point((int)rect.X, (int)rect.Y), Color.White);
                        var minX        = (int)rect.Right;
                        var minY        = (int)rect.Top;
                        var maxX        = (int)rect.Left;
                        var maxY        = (int)rect.Bottom;
                        var solidPixels = 0;
                        for (var x = (int)rect.X; x < Math.Min((int)rect.Right, target.Width); x++)
                        {
                            for (var y = (int)rect.Y; y < Math.Min((int)rect.Bottom, target.Width); y++)
                            {
                                var pixel = target.GetPixel(x, y);
                                if (pixel != transparent)
                                {
                                    if (pixel != Color.White)
                                    {
                                        target.SetPixel(x, y, Color.Transparent);
                                    }
                                    if (x > maxX)
                                    {
                                        maxX = x;
                                    }
                                    if (x < minX)
                                    {
                                        minX = x;
                                    }
                                    solidPixels++;
                                }
                            }
                        }

                        if (solidPixels == 0)
                        {
                            maxX = (int)(rect.Right / 2f);
                            minX = (int)rect.Left;
                        }

                        chars.Add((char)i);
                        bounds.Add(new Rectangle((int)rect.X, (int)rect.Y, (int)rect.Width, (int)rect.Height));
                        var crop = new Rectangle(minX - (int)rect.X, minY - (int)rect.Y, maxX - minX, maxY - minY);
                        Console.WriteLine("Crop for {0} is {1}", ((char)i).ToString(), crop);
                        cropping.Add(crop);
                        if (maxX - minX > spacing)
                        {
                            spacing = maxX - minX;
                        }
                        rect.X = rect.X + measure.Width + 10;
                    }
                }
            }

            var j = 0;

            foreach (var b in cropping)
            {
                var letterWidth  = (int)Math.Max(b.Width + size / 10, spacing / 3);
                var kerningLeft  = 0;
                var kerningRight = 0;
                var k            = new Vector3(kerningLeft, letterWidth, kerningRight);
                kerning.Add(k);
                j++;
            }

            var texture = new Texture2D(ServiceLocator.Get <GraphicsDeviceManager>().GraphicsDevice, target.Width,
                                        target.Height);
            var data = new Microsoft.Xna.Framework.Color[target.Width * target.Height];

            for (var x = 0; x < target.Width; x++)
            {
                for (var y = 0; y < target.Height; y++)
                {
                    var pixel = target.GetPixel(x, y);
                    data[target.Width * y + x] = new Microsoft.Xna.Framework.Color(pixel.R, pixel.G, pixel.B, pixel.A);
                }
            }

            target.Dispose();
            texture.SetData(data);
            using (var stream = new FileStream("test.png", FileMode.Create))
            {
                texture.SaveAsPng(stream, texture.Width, texture.Height);
            }

            var f = (SpriteFont)constructor.Invoke(
                new object[] { texture, bounds, cropping, chars, 0, 0, kerning, null });

            ServiceLocator.Get <IResourceService>().Register(f, name);
            GC.Collect();
        }
Beispiel #4
0
 /// <summary>
 /// Changes the color of the backbuffer when the background color of
 /// the control is changed.
 /// </summary>
 /// <param name="sender">The control element changed.</param>
 /// <param name="e">Empty event arguments.</param>
 private void SceneViewer_BackColorChanged(object sender, EventArgs e)
 {
     _backgroundColor = new Xna.Color(BackColor.R, BackColor.G, BackColor.B);
 }
Beispiel #5
0
 /// <summary>
 /// Allows derived classes to run initialization routines when the 
 /// control element is created.
 /// </summary>
 protected virtual void Initialize()
 {
     _backgroundColor = new Xna.Color(BackColor.R, BackColor.G, BackColor.B);
 }
Beispiel #6
0
        public override void Draw(GameTime gameTime)
        {
            var localPlayer = Storage.NetworkSession.LocalGamers[0].Tag as LocalPlayer;

            if(Storage.InTutorial)
            {
                if (_tool.Slot == ToolSlot.Mobility && (Storage.TutorialLevel != 3))
                    return;
                if ((_tool.Slot == ToolSlot.Utility || _tool.Slot == ToolSlot.Weapon) && (Storage.TutorialLevel != 5))
                    return;
                if ((_tool.Slot == ToolSlot.Primary && localPlayer.Class == Class.Defense) && (Storage.TutorialLevel != 7))
                    return;
                if ((_tool.Slot == ToolSlot.Primary && localPlayer.Class == Class.Support) && (Storage.TutorialLevel != 9))
                    return;
                if ((_tool.Slot == ToolSlot.Primary && localPlayer.Class == Class.Offense) && (Storage.TutorialLevel != 11))
                    return;
            }

            if(_tool.Energy != _drainValue)
            {
                UpdateDrainMeter();
            }

            var mulColorComponent = (Scale - 0.75f) * 3 + 0.25f;
            var mulColor = new Microsoft.Xna.Framework.Color(mulColorComponent, mulColorComponent, mulColorComponent);

            if(_drainTexture != null && _tool.Energy > 0.0f)
            {
                _spriteBatch.Draw(_drainTexture,
                                  Position,
                                  null, mulColor, 0.0f,
                                  new Vector2(_drainTexture.Width / 2.0f, _drainTexture.Height / 2.0f),
                                  Scale,
                                  SpriteEffects.None, 0);
            }

            _spriteBatch.Draw(_tool.Icon, Position, null, mulColor, 0.0f,
                new Vector2(_tool.Icon.Width / 2.0f, _tool.Icon.Height / 2.0f), Scale, SpriteEffects.None,
                              0);
        }
Beispiel #7
0
        /// <summary>
        /// Load a texture using one texture as the color channel and the other as the alpha
        /// </summary>
        /// <param name="TextureName"></param>
        /// <param name="AlphaName"></param>
        /// <returns></returns>
        public Texture2D LoadTextureWithAlpha(string TextureName, string AlphaName)
        {
            Texture2D ColorTexture = Content.Load<Texture2D>(TextureName);
            Texture2D AlphaTexture = Content.Load<Texture2D>(AlphaName);

            //            Texture2D CompositeTexture = new Texture2D(this.graphicsDeviceService.GraphicsDevice, ColorTexture.Width, ColorTexture.Height, false, SurfaceFormat.Color);

            int ArraySize = ColorTexture.Width * ColorTexture.Height;
            Microsoft.Xna.Framework.Color[] ColorTextureData = new Microsoft.Xna.Framework.Color[ArraySize];
            Microsoft.Xna.Framework.Color[] AlphaTextureData = new Microsoft.Xna.Framework.Color[ArraySize];

            ColorTexture.GetData<Microsoft.Xna.Framework.Color>(ColorTextureData);
            AlphaTexture.GetData<Microsoft.Xna.Framework.Color>(AlphaTextureData);

            for (int i = 0; i < ArraySize; i++)
            {
                ColorTextureData[i] = new Microsoft.Xna.Framework.Color(ColorTextureData[i].R,
                                                                        ColorTextureData[i].G,
                                                                        ColorTextureData[i].B,
                                                                        AlphaTextureData[i].R);
            }

            ColorTexture.SetData<Microsoft.Xna.Framework.Color>(ColorTextureData);

            //if (AlphaTexture != null)
            //{
             //   Content.Unload();
                //AlphaTexture.Dispose();
                //AlphaTexture = null;
            //}

            return ColorTexture;
        }