示例#1
0
        public IMask CreateMask(IGameFactory factory, string path, bool transparentMeansMasked = false,
                                AGS.API.Color?debugDrawColor = null, string saveMaskToFile = null, string id = null)
        {
            Bitmap     debugMask     = null;
            FastBitmap debugMaskFast = null;
            int        bitmapWidth   = Width;
            int        bitmapHeight  = Height;

            if (saveMaskToFile != null || debugDrawColor != null)
            {
                debugMask     = new Bitmap(bitmapWidth, bitmapHeight);
                debugMaskFast = new FastBitmap(debugMask, ImageLockMode.WriteOnly, true);
            }

            bool[][]             mask      = new bool[bitmapWidth][];
            System.Drawing.Color drawColor = debugDrawColor != null?debugDrawColor.Value.Convert() : System.Drawing.Color.Black;

            using (FastBitmap bitmapData = new FastBitmap(_bitmap, ImageLockMode.ReadOnly))
            {
                for (int x = 0; x < bitmapWidth; x++)
                {
                    for (int y = 0; y < bitmapHeight; y++)
                    {
                        bool masked = bitmapData.IsOpaque(x, y);

                        if (transparentMeansMasked)
                        {
                            masked = !masked;
                        }

                        if (mask[x] == null)
                        {
                            mask[x] = new bool[bitmapHeight];
                        }
                        mask[x][bitmapHeight - y - 1] = masked;

                        if (debugMask != null)
                        {
                            debugMaskFast.SetPixel(x, y, masked ? drawColor : System.Drawing.Color.Transparent);
                        }
                    }
                }
            }

            if (debugMask != null)
            {
                debugMaskFast.Dispose();
            }

            //Save the duplicate
            if (saveMaskToFile != null)
            {
                debugMask.Save(saveMaskToFile);
            }

            IObject debugDraw = null;

            if (debugDrawColor != null)
            {
                debugDraw        = factory.Object.GetObject(id ?? path ?? "Mask Drawable");
                debugDraw.Image  = factory.Graphics.LoadImage(new DesktopBitmap(debugMask, _graphics), null, path);
                debugDraw.Anchor = new AGS.API.PointF();
            }

            return(new AGSMask(mask, debugDraw));
        }