Beispiel #1
0
 /// <summary>
 /// Initializes a new Instance of BahnEditor.BahnLib.GraphicArchive class
 /// </summary>
 /// <param name="zoomFactor">Zoom factor of the graphics</param>
 public GraphicArchive(ZoomFactor zoomFactor)
 {
     graphics        = new List <ArchiveElement>();
     this.ZoomFactor = zoomFactor;
 }
 public GraphicPanel(ZoomFactor zoomFactor, int zoomlevel)
 {
     this.DoubleBuffered = true;
     this.ZoomFactor     = zoomFactor;
     this.ZoomLevel      = zoomlevel;
 }
Beispiel #3
0
        private static CompressedLayer CompressLayer(uint[,] decompressedLayer, ZoomFactor zoomFactor)
        {
            int layerWidth  = decompressedLayer.GetLength(Width);
            int layerHeight = decompressedLayer.GetLength(Height);
            int minx        = layerWidth;
            int miny        = layerHeight;
            int maxx        = 0;
            int maxy        = 0;

            for (int i = 0; i < layerHeight; i++)
            {
                for (int j = 0; j < layerWidth; j++)
                {
                    if (decompressedLayer[i, j] != Constants.ColorTransparent)
                    {
                        if (minx > j)
                        {
                            minx = j;
                        }
                        if (maxx < j)
                        {
                            maxx = j;
                        }
                        if (miny > i)
                        {
                            miny = i;
                        }
                        if (maxy < i)
                        {
                            maxy = i;
                        }
                    }
                }
            }
            if (maxx == 0 && maxy == 0)
            {
                return(null);
            }
            maxx++;
            maxy++;
            List <uint> pixels        = new List <uint>();
            int         trimmedHeight = maxy - miny;
            int         trimmedWidth  = maxx - minx;

            for (int i = 0; i < trimmedHeight; i++)
            {
                for (int j = 0; j < trimmedWidth; j++)
                {
                    pixels.Add(decompressedLayer[i + miny, j + minx]);
                }
            }
            short x0 = (short)(minx - Constants.ElementWidth * (int)zoomFactor);
            short y0 = (short)(miny - Constants.ElementHeight * (int)zoomFactor);

            List <uint> colors        = new List <uint>();
            int         colorposition = 0;

            while (colorposition < pixels.Count)
            {
                int  length    = 0;
                uint lastcolor = pixels[colorposition];
                for (; colorposition < pixels.Count; colorposition++)
                {
                    if (lastcolor != pixels[colorposition] || length > 256)
                    {
                        break;
                    }
                    length++;
                    lastcolor = pixels[colorposition];
                }
                if (length <= 1)
                {
                    colors.Add(lastcolor);
                }
                else if (lastcolor == Constants.ColorTransparent)
                {
                    colors.Add(Constants.ColorCompressedTransparent | (uint)(length - 2));
                }
                else if (((lastcolor & Constants.ColorLogic) != 0) && lastcolor != (uint)Pixel.PixelProperty.BehindGlass)
                {
                    uint color = lastcolor - Constants.ColorAsMin;
                    color = color << 8;
                    color = color | Constants.ColorCompressedSystemcolor;
                    colors.Add(color | (uint)(length - 2));
                }
                else
                {
                    colors.Add(Constants.ColorCompressed | (uint)(length - 2));
                    colors.Add(lastcolor);
                }
            }
            CompressedLayer layer = new CompressedLayer();

            layer.LayerData = colors.ToArray();
            layer.X0        = x0;
            layer.Y0        = y0;
            layer.Height    = (short)trimmedHeight;
            layer.Width     = (short)trimmedWidth;
            return(layer);
        }
 internal static void DrawElement(Graphics g, uint[,] element, int ZoomLevel, bool withHatchBrush, ZoomFactor zoomFactor)
 {
     if (withHatchBrush)
     {
         using (TextureBrush backgroundBrush = new TextureBrush(GetBackgroundBitmapByZoomlevel(ZoomLevel, zoomFactor)))
         {
             g.FillRectangle(backgroundBrush, 0, 0, element.GetLength(1) * ZoomLevel / (int)zoomFactor, element.GetLength(0) * ZoomLevel / (int)zoomFactor);
         }
     }
     else
     {
         using (SolidBrush backgroundBrush = new SolidBrush(Color.FromArgb(0, 112, 0)))
         {
             g.FillRectangle(backgroundBrush, 0, 0, element.GetLength(1) * ZoomLevel / (int)zoomFactor, element.GetLength(0) * ZoomLevel / (int)zoomFactor);
         }
     }
     for (int i = 0; i < element.GetLength(0); i++)
     {
         for (int j = 0; j < element.GetLength(1); j++)
         {
             if (Pixel.IsTransparent(element[i, j]) != true)
             {
                 Brush brush;
                 if (withHatchBrush && Pixel.IsSpecial(element[i, j]))
                 {
                     brush = new HatchBrush(HatchStyle.Percent20, Color.FromArgb(140, 140, 140), PixelToColor(element[i, j]));
                 }
                 else
                 {
                     brush = new SolidBrush(PixelToColor(element[i, j]));
                 }
                 g.FillRectangle(brush, (j * ZoomLevel) / (int)zoomFactor, ((ZoomLevel * element.GetLength(0)) - (ZoomLevel * (i + 1))) / (int)zoomFactor, ZoomLevel / (float)zoomFactor, ZoomLevel / (float)zoomFactor);
                 brush.Dispose();
             }
         }
     }
 }
Beispiel #5
0
        private static uint[,] DecompressLayer(CompressedLayer layer, ZoomFactor zoomFactor)
        {
            int         arrayPosition = 0;
            List <uint> colors        = new List <uint>();

            for (int i = 0; i < layer.LayerData.Length; i++)
            {
                uint item  = layer.LayerData[arrayPosition++];
                int  count = 0;
                if ((item & Constants.ColorAdditionalDataMask) == Constants.ColorCompressed)
                {
                    count = (int)(item & Constants.ColorMaskCompressedCount) + 2;
                    if ((item & Constants.ColorMaskCompressedTransparent) != 0)
                    {
                        // item is transparent
                        for (int k = 0; k < count; k++)
                        {
                            colors.Add(Constants.ColorTransparent);
                        }
                    }
                    else if ((item & Constants.ColorMaskCompressedSystemcolor) != 0)
                    {
                        // item is a system-color
                        for (int k = 0; k < count; k++)
                        {
                            colors.Add(((item & Constants.ColorMaskCompressedSFB) >> 8) + Constants.ColorAsMin);
                        }
                    }
                    else
                    {
                        // item is a color, may be a set of colors
                        uint wdhlen = ((item & Constants.ColorMaskCompressedLength) >> 8) + 1;
                        if (wdhlen > Constants.MaxRepeatedColorsLength)
                        {
                            throw new InvalidDataException("color repetition length out of range");
                        }
                        List <uint> buffer = new List <uint>();
                        for (int j = 0; j < wdhlen; j++)
                        {
                            buffer.Add(layer.LayerData[arrayPosition++]);
                            i++;
                        }
                        for (int j = 0; j < count; j++)
                        {
                            foreach (var b in buffer)
                            {
                                colors.Add(b);
                            }
                        }
                    }
                }
                else
                {
                    // not packed, single pixel
                    count = 1;
                    colors.Add(item);
                }
            }
            int height = Constants.ElementHeight * 8 * (int)zoomFactor;
            int width  = Constants.ElementWidth * 3 * (int)zoomFactor;

            uint[,] layerElement = new uint[height, width];
            int x0  = (int)(layer.X0 + Constants.ElementWidth * (int)zoomFactor);
            int y0  = (int)(layer.Y0 + Constants.ElementHeight * (int)zoomFactor);
            int _x0 = x0 + layer.Width;
            int _y0 = y0 + layer.Height;

            int position = 0;

            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    if (i >= y0 && i < _y0 && j >= x0 && j < _x0)
                    {
                        layerElement[i, j] = colors[position++];
                    }
                    else
                    {
                        layerElement[i, j] = Constants.ColorTransparent;
                    }
                }
            }
            return(layerElement);
        }