/// <summary>
        /// A Texture2D with coords relative to the screen. Generates the center of the 
        /// texture as well.
        /// </summary>
        /// <param name="tex">A delegate that returns a Texture2D.</param>
        /// <param name="position">Position of texture. Defined by point; e.g. if point is Center this value will be the center of the texture.</param>
        /// <param name="renderBox">If using a compiled texture, the rectangle that contains the
        /// desired texture. Use null for all of the given texture.</param>
        /// <param name="point">This is the point at which to render the sprite.</param>
        public Sprite(TextureDelegate d, Vector2 position, Rectangle? renderBox, RenderPoint point)
        {
            textureDelegate = d;
            if(renderBox.HasValue)
                TargetArea = renderBox.Value;
            else
                TargetArea = new Rectangle(0, 0, Texture.Width, Texture.Height);
            Point = point;

            if(point == RenderPoint.UpLeft)
            {
                upperLeft = position;
                lowerRight = new Vector2(upperLeft.X + Width, upperLeft.Y + Height);
                center = new Vector2((upperLeft.X + lowerRight.X) * 0.5f, (upperLeft.Y + lowerRight.Y) * 0.5f);
            }
            else // center
            {
                center = position;
                lowerRight = new Vector2(center.X + TargetArea.Width * 0.5f, center.Y + TargetArea.Height * 0.5f);
                upperLeft = new Vector2(center.X - TargetArea.Width * 0.5f, center.Y - TargetArea.Height * 0.5f);
                relativeOrigin = new Vector2(TargetArea.Width * 0.5f, TargetArea.Height * 0.5f);
            }

            originalCenter = Center;
            relativeScreenPosition = new Vector2(upperLeft.X / RenderingDevice.Width, upperLeft.Y / RenderingDevice.Height);

            RenderingDevice.GDM.DeviceReset += rebuildCenters;
        }
        /// <summary>
        /// This is a special SuperTextor used as a hitbox. 
        /// </summary>
        /// <param name="upLeft">Upper left corner (in screen coords) of the hitbox.</param>
        /// <param name="heightWidth">A vector determining the height/width of the hitbox.
        /// The X value of the vector will be used as the width, the Y value as the height.</param>
        public Sprite(Vector2 upLeft, Vector2 heightWidth)
        {
            textureDelegate = delegate { return null; };
            Point = RenderPoint.UpLeft;
            upperLeft = upLeft;
            lowerRight = new Vector2(upLeft.X + heightWidth.X, upLeft.Y + heightWidth.Y);
            center = new Vector2((upLeft.X + LowerRight.X) / 2, (upLeft.Y + LowerRight.Y) / 2);

            relativeScreenPosition = new Vector2(upperLeft.X / RenderingDevice.Width, upperLeft.Y / RenderingDevice.Height);

            RenderingDevice.GDM.DeviceReset += rebuildCenters;
        }
Example #3
0
        public unsafe AtlasData Serialize(
            AtlasPacker packer, DirectoryInfo output,
            TextureDelegate onTexture, ProgressDelegate onProgress)
        {
            int stateCount  = packer._states.Count;
            int singleCount = packer._singles.Count;
            var textures    = new string[stateCount + singleCount];

            int totalItemCount = packer.TotalItemCount;
            var items          = new List <AtlasData.Item>(totalItemCount);

            if (!output.Exists)
            {
                output.Create();
            }

            void AddItem(AtlasData.Item item)
            {
                items.Add(item);
                onProgress.Invoke(items.Count / (float)totalItemCount);
            }

            for (int textureIndex = 0; textureIndex < stateCount; textureIndex++)
            {
                AtlasPackerState state = packer._states[textureIndex];
                int width  = state.Width;
                int height = state.Height;

                using (var result = new Image <Rgba32>(width, height))
                {
                    Span <Rgba32> resultSpan = result.GetPixelSpan();
                    foreach (AtlasPackerState.Item item in state.Items)
                    {
                        using (var img = Image.Load <Rgba32>(item.AtlasImage.File.OpenRead()))
                        {
                            var srcRect = new Rect(0, 0, img.Width, img.Height);
                            var input32 = img.GetPixelSpan();

                            Copy(input32, inputStride: srcRect.W, srcRect,
                                 resultSpan, outputStride: width, item.Rect);

                            AddItem(new AtlasData.Item(item.AtlasImage.RelativePath, textureIndex, item.Rect));
                        }
                    }

                    using (var fs = GetFileStream(textures, textureIndex, output))
                        result.Save(fs, SaveFormat);

                    onTexture?.Invoke(resultSpan, new Size(width, height), textureIndex);
                }
            }

            for (int singleIndex = 0; singleIndex < singleCount; singleIndex++)
            {
                AtlasImage item = packer._singles[singleIndex];
                using (var img = Image.Load <Rgba32>(item.File.OpenRead()))
                {
                    int index = singleIndex + stateCount; // add amount of states as offset
                    using (var fs = GetFileStream(textures, index, output))
                        img.Save(fs, SaveFormat);

                    AddItem(new AtlasData.Item(item.RelativePath, index, 0, 0, item.Width, item.Height));
                    onTexture?.Invoke(img.GetPixelSpan(), new Size(item.Width, item.Height), index);
                }
            }

            return(new AtlasData(textures, items));
        }
Example #4
0
 public Dock(TextureDelegate dockTex, FontDelegate font)
 {
     texture = new Sprite(dockTex, new Vector2(-dockTex().Width, 120), null, Sprite.RenderPoint.UpLeft);
     fontDelegate = font;
 }