Beispiel #1
0
 public FramePlacement XMLConvert(FramePlacement v, XAttribute attr)
 {
     if (attr == null) return v;
     FramePlacement f = FramePlacement.Any;
     f = f.Convert<FramePlacement>(attr);
     return f;
 }
Beispiel #2
0
        public FramePlacement XMLConvert(FramePlacement v, XAttribute attr)
        {
            if (attr == null)
            {
                return(v);
            }
            FramePlacement f = FramePlacement.Any;

            f = f.Convert <FramePlacement>(attr);
            return(f);
        }
        // Create a new bitmap that is a resized version of the original's current frame using argb
        static Bitmap Resize(Bitmap original, int width, int height, FramePlacement placement)
        {
            Bitmap b = new Bitmap(width, height, PixelFormat.Format32bppArgb);

            using (Graphics g = Graphics.FromImage(b))
            {
                g.CompositingMode    = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                g.InterpolationMode  = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                Rectangle r = new Rectangle(
                    placement == FramePlacement.TopLeft || placement == FramePlacement.Stretch ?
                    0 : (width - original.Width) / 2,
                    placement == FramePlacement.TopLeft || placement == FramePlacement.Stretch ?
                    0 : (height - original.Height) / 2,
                    placement == FramePlacement.Stretch ? width : original.Width,
                    placement == FramePlacement.Stretch ? height : original.Height
                    );
                g.DrawImage(original, r, new Rectangle(0, 0, original.Width, original.Height), GraphicsUnit.Pixel);
            }
            return(b);
        }
 public AttachGroup(string n, FramePlacement? p, ObservableCollection<TileFrame> s)
 {
     _name = n;
     _placement = p;
     _attachesTo.ReplaceRange(s);
 }
 public AttachGroup(string n, FramePlacement? p, string s)
 {
     _name = n;
     _placement = p;
     TryParse(s, ref _attachesTo);
 }
 private static bool MatchFields(AttachGroup a, FramePlacement m)
 {
     return (a.Placement == m);
 }
        IEnumerable <Bitmap> CreateFrames(Bitmap original, bool opacify, bool transparent, bool smooth, SizingCallback resolve)
        {
            EnsureUndisposed();
            FramePlacement placement = FramePlacement.TopLeft;
            FrameSizing    sizing    = FrameSizing.OriginalSize;

            if (frames.Count == 0)
            {
                sizing = FrameSizing.NewSize;
            }
            else if (original.Width != width || original.Height != height)
            {
                if (resolve != null)
                {
                    resolve(out placement, out sizing);
                }
            }
            bool newsize = false;

            if (sizing == FrameSizing.NewSize)
            {
                width   = original.Width;
                height  = original.Height;
                newsize = true;
            }
            else if (sizing == FrameSizing.MaximumSize)
            {
                if (original.Width * original.Height > width * height)
                {
                    width   = original.Width;
                    height  = original.Height;
                    newsize = true;
                }
            }
            if (newsize)
            {
                for (int i = 0; i < frames.Count; i++)
                {
                    Bitmap b = frames[i];
                    frames[i] = Resize(b, width, height, placement);
                    b.Dispose();
                }
            }
            FrameDimension fd = new FrameDimension(original.FrameDimensionsList[0]);
            int            c  = original.GetFrameCount(fd);

            for (int f = 0; f < c; f++)
            {
                original.SelectActiveFrame(fd, f);
                Bitmap     temp = Resize(original, original.Width, original.Height, FramePlacement.TopLeft); // Don't modify the bitmap, just make a new one from the frame with format32bppargb;
                BitmapData bd   = temp.LockBits(new Rectangle(0, 0, temp.Width, temp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
                if (opacify)
                {
                    for (int i = 0; i < bd.Height; i++)
                    {
                        for (int j = 0; j < bd.Width; j++)
                        {
                            int ofs = bd.Stride * i + j * 4;
                            System.Runtime.InteropServices.Marshal.WriteInt32(bd.Scan0, ofs, unchecked (System.Runtime.InteropServices.Marshal.ReadInt32(bd.Scan0, ofs) & (int)0xFF000000));
                        }
                    }
                }
                if (transparent)
                {
                    int col = System.Runtime.InteropServices.Marshal.ReadInt32(bd.Scan0, bd.Stride * (bd.Height - 1)) & 0xFFFFFF;
                    for (int i = 0; i < bd.Height; i++)
                    {
                        for (int j = 0; j < bd.Width; j++)
                        {
                            int ofs = bd.Stride * i + j * 4;
                            if ((System.Runtime.InteropServices.Marshal.ReadInt32(bd.Scan0, ofs) & 0xFFFFFF) == col)
                            {
                                System.Runtime.InteropServices.Marshal.WriteInt32(bd.Scan0, ofs, col);
                            }
                        }
                    }
                }
                temp.UnlockBits(bd);
                Bitmap bmp = temp;
                if (original.Width != width || original.Height != height)
                {
                    bmp = Resize(temp, width, height, placement);
                    temp.Dispose();
                }
                if (smooth)
                {
                    temp = bmp;
                    bmp  = SmoothEdges(temp);
                    temp.Dispose();
                }
                yield return(bmp); // me likes ;D
            }
        }
Beispiel #8
0
 private static bool MatchFields(AttachGroup a, FramePlacement m)
 {
     return(a.Placement == m);
 }
 // Create a new bitmap that is a resized version of the original's current frame using argb
 static Bitmap Resize(Bitmap original, int width, int height, FramePlacement placement)
 {
     Bitmap b = new Bitmap(width, height, PixelFormat.Format32bppArgb);
     using (Graphics g = Graphics.FromImage(b))
     {
         g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
         g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
         g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
         Rectangle r = new Rectangle(
             placement == FramePlacement.TopLeft || placement == FramePlacement.Stretch ?
             0 : (width-original.Width)/2,
             placement == FramePlacement.TopLeft || placement == FramePlacement.Stretch ?
             0 : (height-original.Height)/2,
             placement == FramePlacement.Stretch ? width : original.Width,
             placement == FramePlacement.Stretch ? height : original.Height
             );
         g.DrawImage(original, r, new Rectangle(0, 0, original.Width, original.Height), GraphicsUnit.Pixel);
     }
     return b;
 }
Beispiel #10
0
        // using FP = FramePlacement;
        private bool ValidatePlacement(PointInt32 location, PointShort size, FramePlacement frame)
        {
            // TODO: Support for attachesTo with placement

            // quick short-circuits
            if (frame.Is(FP.None))   return false;   // hey, if it can't attach to anything, it's invalid
            if (frame.Is(FP.Any))    return true;
            if (frame.Has(FP.Float)) return true;  // ...for now; this behavior may change if we actually get a "float only" object

            /// Look at surrounding area for objects ///
            FramePlacement area   = FP.Float;
            RectI areaRect = new RectI(location - 1, location + size);
            // (only used for x/y loop)
            RectI areaRectBnd = new RectI(areaRect);

            // skip tests that don't apply
            // (frame = multiples)
            if (frame.HasNo(FP.Ceiling))      areaRectBnd.Top++;
            if (frame.HasNo(FP.FloorSurface)) areaRectBnd.Bottom--;
            if (frame.HasNo(FP.Wall)) {
                areaRectBnd.Left++;
                areaRectBnd.Right--;
            }

            // (shorter cuts for singular terms; using HasNoneOf to factor in MustHaveAll)
            if (frame.HasNoneOf(FP.WallCeiling))      areaRectBnd.Top    = areaRect.Bottom;  // Floor/Surface/both
            if (frame.HasNoneOf(FP.WallFloorSurface)) areaRectBnd.Bottom = areaRect.Top;     // Ceiling
                                                                                             // Wall already covered in multiples checks

            // boundary checks
            if ( areaRectBnd.Left  < 0)                  areaRectBnd.Left   = 0;
            if ( areaRectBnd.Top   < 0)                  areaRectBnd.Top    = 0;
            if (areaRectBnd.Right  > Header.WorldBounds.Right)  areaRectBnd.Right  = Header.WorldBounds.Right;
            if (areaRectBnd.Bottom > Header.WorldBounds.Bottom) areaRectBnd.Bottom = Header.WorldBounds.Bottom;

            for (int y = areaRectBnd.Top; y <= areaRectBnd.Bottom; y++)
            {
                for (int x = areaRectBnd.Left; x <= areaRectBnd.Right; x++)
                {
                    // skip dead zone (the item itself) & corners (wow, xor use...)
                    bool valid = (x == areaRect.Left || x == areaRect.Right) ^ (y == areaRect.Top || y == areaRect.Bottom);
                    if (!valid) continue;

                    var t = Tiles[x, y];
                    var w = WorldSettings.Tiles[t.Type];

                    // skip non-solid objects
                    if (! (t.IsActive && w.IsSolid || w.IsSolidTop)) continue;

                    // FIXME: Assuming that a single tile will hold the object //
                    // (Maybe this is true in Terraria as well....)

                    // at this point, only one of these will hit
                    if      (y ==  areaRect.Top) {
                        area = area.Add(FP.Ceiling);
                        break;  // done with this Y-axis
                    }
                    else if (x ==  areaRect.Left || x == areaRect.Right) {
                        area = area.Add(FP.Wall);
                        y = areaRect.Bottom - 1;  // -1 = for loop will re-adjust, or kick out if LRB was truncated
                        break;  // done with all except bottom
                    }
                    else if (y == areaRect.Bottom) {  // special case for floor/surface
                        if (w.IsSolidTop) area = area.Add(FP.Surface);
                        else              area = area.Add(FP.Floor);
                        if (area.HasAllOf(FP.FloorSurface)) break;  // done with everything else
                    }
                }
            }

            // Now let's compare the object in question
            if (frame.Has(FP.MustHaveAll)) {
                area = area.Add(FP.MustHaveAll);  // add bit for bitwise math to work
                if (area.Filter(frame) != frame) return false;
            }
            else {
                if (frame.HasNoneOf(area))       return false;
            }

            return true;
        }
Beispiel #11
0
        private bool ValidatePlacement(PointInt32 location, PointShort size, FramePlacement frame)  // using FP = FramePlacement;
        {
            // TODO: Support for attachesTo with placement

            // quick short-circuits
            if (frame.Is(FP.None))
            {
                return(false);                       // hey, if it can't attach to anything, it's invalid
            }
            if (frame.Is(FP.Any))
            {
                return(true);
            }
            if (frame.Has(FP.Float))
            {
                return(true);                      // ...for now; this behavior may change if we actually get a "float only" object
            }
            /// Look at surrounding area for objects ///
            FramePlacement area     = FP.Float;
            RectI          areaRect = new RectI(location - 1, location + size);
            // (only used for x/y loop)
            RectI areaRectBnd = new RectI(areaRect);

            // skip tests that don't apply
            // (frame = multiples)
            if (frame.HasNo(FP.Ceiling))
            {
                areaRectBnd.Top++;
            }
            if (frame.HasNo(FP.FloorSurface))
            {
                areaRectBnd.Bottom--;
            }
            if (frame.HasNo(FP.Wall))
            {
                areaRectBnd.Left++;
                areaRectBnd.Right--;
            }

            // (shorter cuts for singular terms; using HasNoneOf to factor in MustHaveAll)
            if (frame.HasNoneOf(FP.WallCeiling))
            {
                areaRectBnd.Top = areaRect.Bottom;                                           // Floor/Surface/both
            }
            if (frame.HasNoneOf(FP.WallFloorSurface))
            {
                areaRectBnd.Bottom = areaRect.Top;                                           // Ceiling
            }
            // Wall already covered in multiples checks

            // boundary checks
            if (areaRectBnd.Left < 0)
            {
                areaRectBnd.Left = 0;
            }
            if (areaRectBnd.Top < 0)
            {
                areaRectBnd.Top = 0;
            }
            if (areaRectBnd.Right > Header.WorldBounds.Right)
            {
                areaRectBnd.Right = Header.WorldBounds.Right;
            }
            if (areaRectBnd.Bottom > Header.WorldBounds.Bottom)
            {
                areaRectBnd.Bottom = Header.WorldBounds.Bottom;
            }

            for (int y = areaRectBnd.Top; y <= areaRectBnd.Bottom; y++)
            {
                for (int x = areaRectBnd.Left; x <= areaRectBnd.Right; x++)
                {
                    // skip dead zone (the item itself) & corners (wow, xor use...)
                    bool valid = (x == areaRect.Left || x == areaRect.Right) ^ (y == areaRect.Top || y == areaRect.Bottom);
                    if (!valid)
                    {
                        continue;
                    }

                    var t = Tiles[x, y];
                    var w = WorldSettings.Tiles[t.Type];

                    // skip non-solid objects
                    if (!(t.IsActive && w.IsSolid || w.IsSolidTop))
                    {
                        continue;
                    }

                    // FIXME: Assuming that a single tile will hold the object //
                    // (Maybe this is true in Terraria as well....)

                    // at this point, only one of these will hit
                    if (y == areaRect.Top)
                    {
                        area = area.Add(FP.Ceiling);
                        break;  // done with this Y-axis
                    }
                    else if (x == areaRect.Left || x == areaRect.Right)
                    {
                        area = area.Add(FP.Wall);
                        y    = areaRect.Bottom - 1; // -1 = for loop will re-adjust, or kick out if LRB was truncated
                        break;                      // done with all except bottom
                    }
                    else if (y == areaRect.Bottom)  // special case for floor/surface
                    {
                        if (w.IsSolidTop)
                        {
                            area = area.Add(FP.Surface);
                        }
                        else
                        {
                            area = area.Add(FP.Floor);
                        }
                        if (area.HasAllOf(FP.FloorSurface))
                        {
                            break;                                  // done with everything else
                        }
                    }
                }
            }

            // Now let's compare the object in question
            if (frame.Has(FP.MustHaveAll))
            {
                area = area.Add(FP.MustHaveAll);  // add bit for bitwise math to work
                if (area.Filter(frame) != frame)
                {
                    return(false);
                }
            }
            else
            {
                if (frame.HasNoneOf(area))
                {
                    return(false);
                }
            }

            return(true);
        }