Example #1
0
 private void ClearGrid()
 {
     CellsCount     = 0;
     LifeBitmap     = new Bitmap(gamePanel.Width, gamePanel.Height);
     BitmapGraphics = Graphics.FromImage(LifeBitmap);
     BitmapGraphics.Clear(gamePanel.BackColor);
 }
        private Bitmap2 CreateOverviewBitmap(int overviewWidth, int overviewHeight)
        {
            BitmapGraphics bg = new BitmapGraphics(Math.Min(main.TotalWidth(), 15000), Math.Min(main.TotalHeight(), 15000));

            main.OnPaintMainView?.Invoke(bg, 0, 0, main.TotalWidth(), main.TotalHeight(), true);
            return(GraphUtils.ToBitmap2(GraphUtils.ResizeImage(bg.Bitmap, overviewWidth, overviewHeight)));
        }
Example #3
0
 private void RedrawDynamicTargets(Dictionary <int, List <Rectangle> > targets, HSBOperation operation)
 {
     foreach (int tileIndex in targets.Keys)
     {
         BitmapGraphics bg = GetBitmapGraphics(tileIndex);
         foreach (Rectangle rect in targets[tileIndex])
         {
             bg.AdjustHSB(rect, operation);
         }
     }
 }
Example #4
0
 public void CommitGraphics()
 {
     if (!_committed)
     {
         foreach (int tile in _tileMap.Keys)
         {
             using (BitmapGraphics bmp = _tileMap[tile])
             {
                 _level.Images16[tile].Pixels = T16Importer.ImportFromBitmap(bmp.Bitmap);
             }
         }
         _committed = true;
     }
 }
Example #5
0
        private void DrawGrid()
        {
            for (int x = 0; x < LifeGrid.Width; x++)
            {
                for (int y = 0; y < LifeGrid.Height; y++)
                {
                    if (LifeGrid.Cells[x, y].IsAlive)
                    {
                        BitmapGraphics.FillRectangle(BrushCell, x * CellSize, y * CellSize, CellSize, CellSize);
                        CellsCount++;
                    }
                }
            }

            PanelGraphics.DrawImage(LifeBitmap, 0, 0);
        }
            protected void ProduceResponse(HttpContext context, ITypeAccepter accepter, string title, RenderContext ctx, Size tileSize,
                                           AbstractMatrix transform,
                                           bool transparent = false, IDictionary <string, object> queryDefaults = null)
            {
                // New-style Options

                #region URL Parameters
                // TODO: move to ParseOptions (maybe - requires options to be parsed after stylesheet creation?)
                if (GetBoolOption("sscoords", queryDefaults: queryDefaults, defaultValue: false))
                {
                    ctx.Styles.hexCoordinateStyle = HexCoordinateStyle.Subsector;
                }

                if (GetBoolOption("allhexes", queryDefaults: queryDefaults, defaultValue: false))
                {
                    ctx.Styles.numberAllHexes = true;
                }

                if (GetBoolOption("nogrid", queryDefaults: queryDefaults, defaultValue: false))
                {
                    ctx.Styles.parsecGrid.visible = false;
                }

                if (!GetBoolOption("routes", queryDefaults: queryDefaults, defaultValue: true))
                {
                    ctx.Styles.macroRoutes.visible = false;
                    ctx.Styles.microRoutes.visible = false;
                }

                if (!GetBoolOption("rifts", queryDefaults: queryDefaults, defaultValue: true))
                {
                    ctx.Styles.showRiftOverlay = false;
                }

                if (GetBoolOption("po", queryDefaults: queryDefaults, defaultValue: false))
                {
                    ctx.Styles.populationOverlay.visible = true;
                }

                if (GetBoolOption("im", queryDefaults: queryDefaults, defaultValue: false))
                {
                    ctx.Styles.importanceOverlay.visible = true;
                }

                if (GetBoolOption("cp", queryDefaults: queryDefaults, defaultValue: false))
                {
                    ctx.Styles.capitalOverlay.visible = true;
                }

                if (GetBoolOption("stellar", queryDefaults: queryDefaults, defaultValue: false))
                {
                    ctx.Styles.showStellarOverlay = true;
                }

                ctx.Styles.dimUnofficialSectors    = GetBoolOption("dimunofficial", queryDefaults: queryDefaults, defaultValue: false);
                ctx.Styles.colorCodeSectorStatus   = GetBoolOption("review", queryDefaults: queryDefaults, defaultValue: false);
                ctx.Styles.droyneWorlds.visible    = GetBoolOption("dw", queryDefaults: queryDefaults, defaultValue: false);
                ctx.Styles.minorHomeWorlds.visible = GetBoolOption("mh", queryDefaults: queryDefaults, defaultValue: false);
                ctx.Styles.ancientsWorlds.visible  = GetBoolOption("an", queryDefaults: queryDefaults, defaultValue: false);

                // TODO: Return an error if pattern is invalid?
                ctx.Styles.highlightWorldsPattern = HighlightWorldPattern.Parse(
                    GetStringOption("hw", queryDefaults: queryDefaults, defaultValue: String.Empty).Replace(' ', '+'));
                ctx.Styles.highlightWorlds.visible = ctx.Styles.highlightWorldsPattern != null;

                double devicePixelRatio = GetDoubleOption("dpr", defaultValue: 1, queryDefaults: queryDefaults);
                devicePixelRatio = Math.Round(devicePixelRatio, 1);
                if (devicePixelRatio <= 0)
                {
                    devicePixelRatio = 1;
                }
                if (devicePixelRatio > 2)
                {
                    devicePixelRatio = 2;
                }

                bool dataURI = GetBoolOption("datauri", queryDefaults: queryDefaults, defaultValue: false);

                if (GetStringOption("milieu", SectorMap.DEFAULT_MILIEU) != SectorMap.DEFAULT_MILIEU)
                {
                    // TODO: Make this declarative in resource files.
                    if (ctx.Styles.macroBorders.visible)
                    {
                        ctx.Styles.macroBorders.visible = false;
                        ctx.Styles.microBorders.visible = true;
                    }
                    ctx.Styles.macroNames.visible  = false;
                    ctx.Styles.macroRoutes.visible = false;
                }
                #endregion

                MemoryStream ms = null;
                if (dataURI)
                {
                    ms = new MemoryStream();
                }
                Stream outputStream = ms ?? Context.Response.OutputStream;

                if (accepter.Accepts(context, ContentTypes.Image.Svg, ignoreHeaderFallbacks: true))
                {
                    #region SVG Generation
                    using (var svg = new SVGGraphics(tileSize.Width, tileSize.Height))
                    {
                        RenderToGraphics(ctx, transform, svg);

                        using (var stream = new MemoryStream())
                        {
                            svg.Serialize(new StreamWriter(stream));
                            context.Response.ContentType = ContentTypes.Image.Svg;
                            if (!dataURI)
                            {
                                context.Response.AddHeader("content-length", stream.Length.ToString());
                                context.Response.AddHeader("content-disposition", $"inline;filename=\"{Util.SanitizeFilename(title)}.svg\"");
                            }
                            stream.WriteTo(outputStream);
                        }
                    }
                    #endregion
                }

                else if (accepter.Accepts(context, ContentTypes.Application.Pdf, ignoreHeaderFallbacks: true))
                {
                    #region PDF Generation
                    using (var document = new PdfDocument())
                    {
                        document.Version       = 14; // 1.4 for opacity
                        document.Info.Title    = title;
                        document.Info.Author   = "Joshua Bell";
                        document.Info.Creator  = "TravellerMap.com";
                        document.Info.Subject  = DateTime.Now.ToString("F", CultureInfo.InvariantCulture);
                        document.Info.Keywords = "The Traveller game in all forms is owned by Far Future Enterprises. Copyright (C) 1977 - 2019 Far Future Enterprises. Traveller is a registered trademark of Far Future Enterprises.";

                        // TODO: Credits/Copyright
                        // This is close, but doesn't define the namespace correctly:
                        // document.Info.Elements.Add( new KeyValuePair<string, PdfItem>( "/photoshop/Copyright", new PdfString( "HelloWorld" ) ) );

                        PdfPage page = document.AddPage();

                        // NOTE: only PageUnit currently supported in MGraphics is Points
                        page.Width  = XUnit.FromPoint(tileSize.Width);
                        page.Height = XUnit.FromPoint(tileSize.Height);

                        using (var gfx = new PdfSharpGraphics(XGraphics.FromPdfPage(page)))
                        {
                            RenderToGraphics(ctx, transform, gfx);

                            using (var stream = new MemoryStream())
                            {
                                document.Save(stream, closeStream: false);
                                context.Response.ContentType = ContentTypes.Application.Pdf;
                                if (!dataURI)
                                {
                                    context.Response.AddHeader("content-length", stream.Length.ToString());
                                    context.Response.AddHeader("content-disposition", $"inline;filename=\"{Util.SanitizeFilename(title)}.pdf\"");
                                }
                                stream.WriteTo(outputStream);
                            }
                        }
                    }
                    #endregion
                }
                else
                {
                    #region Bitmap Generation
                    int width  = (int)Math.Floor(tileSize.Width * devicePixelRatio);
                    int height = (int)Math.Floor(tileSize.Height * devicePixelRatio);
                    using (var bitmap = TryConstructBitmap(width, height, PixelFormat.Format32bppArgb))
                    {
                        if (bitmap == null)
                        {
                            throw new HttpError(500, "Internal Server Error",
                                                $"Failed to allocate bitmap ({width}x{height}). Insufficient memory?");
                        }

                        if (transparent)
                        {
                            bitmap.MakeTransparent();
                        }

                        using (var g = System.Drawing.Graphics.FromImage(bitmap))
                        {
                            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;

                            using (var graphics = new BitmapGraphics(g))
                            {
                                graphics.ScaleTransform((float)devicePixelRatio);
                                RenderToGraphics(ctx, transform, graphics);
                            }
                        }

                        BitmapResponse(context.Response, outputStream, ctx.Styles, bitmap, transparent ? ContentTypes.Image.Png : null);
                    }
                    #endregion
                }

                if (dataURI)
                {
                    string contentType = context.Response.ContentType;
                    context.Response.ContentType = ContentTypes.Text.Plain;
                    ms.Seek(0, SeekOrigin.Begin);

                    context.Response.Output.Write("data:");
                    context.Response.Output.Write(contentType);
                    context.Response.Output.Write(";base64,");
                    context.Response.Output.Flush();

                    System.Security.Cryptography.ICryptoTransform encoder = new System.Security.Cryptography.ToBase64Transform();
                    using (System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(context.Response.OutputStream, encoder, System.Security.Cryptography.CryptoStreamMode.Write))
                    {
                        ms.WriteTo(cs);
                        cs.FlushFinalBlock();
                    }
                }

                context.Response.Flush();
                context.Response.Close();
                return;
            }
Example #7
0
        public bool Import(TR2CombinedLevel level, TextureLevelMapping mapping)
        {
            // Ensure any changes already made are committed to the level
            mapping.CommitGraphics();

            using (TexturePacker packer = new TexturePacker(level.Data))
            {
                List <TRObjectTexture> textures = level.Data.ObjectTextures.ToList();
                foreach (StaticTextureSource source in mapping.LandmarkMapping.Keys)
                {
                    if (textures.Count == _maxTextures)
                    {
                        break;
                    }

                    if (!source.HasVariants)
                    {
                        continue;
                    }

                    List <Rectangle> segments = source.VariantMap[source.Variants[0]];
                    foreach (int segmentIndex in mapping.LandmarkMapping[source].Keys)
                    {
                        foreach (LandmarkTextureTarget target in mapping.LandmarkMapping[source][segmentIndex])
                        {
                            IndexedTRObjectTexture texture = CreateTexture(segments[segmentIndex]);
                            target.MappedTextureIndex = textures.Count;
                            textures.Add(texture.Texture);

                            Bitmap image;
                            if (target.BackgroundIndex != -1)
                            {
                                IndexedTRObjectTexture indexedTexture = new IndexedTRObjectTexture
                                {
                                    Index   = target.BackgroundIndex,
                                    Texture = level.Data.ObjectTextures[target.BackgroundIndex]
                                };
                                BitmapGraphics tile = GetTile(level.Data, indexedTexture.Atlas);

                                BitmapGraphics clip = new BitmapGraphics(tile.Extract(indexedTexture.Bounds));
                                clip.Overlay(source.Bitmap);
                                image = clip.Bitmap;
                            }
                            else
                            {
                                image = source.Bitmap;
                            }

                            packer.AddRectangle(new TexturedTileSegment(texture, image));
                        }
                    }
                }

                try
                {
                    packer.Pack(true);

                    // Perform the room data remapping
                    foreach (StaticTextureSource source in mapping.LandmarkMapping.Keys)
                    {
                        if (!source.HasVariants)
                        {
                            continue;
                        }

                        foreach (int segmentIndex in mapping.LandmarkMapping[source].Keys)
                        {
                            foreach (LandmarkTextureTarget target in mapping.LandmarkMapping[source][segmentIndex])
                            {
                                if (target.MappedTextureIndex == -1)
                                {
                                    // There wasn't enough space for this
                                    continue;
                                }

                                TR2Room room = level.Data.Rooms[target.RoomNumber];
                                foreach (int rectIndex in target.RectangleIndices)
                                {
                                    room.RoomData.Rectangles[rectIndex].Texture = (ushort)target.MappedTextureIndex;
                                }
                            }
                        }
                    }

                    level.Data.ObjectTextures    = textures.ToArray();
                    level.Data.NumObjectTextures = (uint)textures.Count;

                    return(true);
                }
                catch (PackingException)
                {
                    return(false);
                }
            }
        }
Example #8
0
        private void CollateSegments()
        {
            // Rebuild the segment list. We assume the list of IndexedTRObjectTextures has been
            // ordered by area descending to preserve the "master" texture for each segment.
            _importSegments = new Dictionary <TRModelDefinition, List <TexturedTileSegment> >();

            // Track existing sprite sequences to avoid duplication
            List <TRSpriteSequence> spriteSequences = Level.SpriteSequences.ToList();

            foreach (TRModelDefinition definition in Definitions)
            {
                if (!definition.HasGraphics || definition.IsDependencyOnly)
                {
                    continue;
                }

                _importSegments[definition] = new List <TexturedTileSegment>();
                using (BitmapGraphics bg = new BitmapGraphics(definition.Bitmap))
                {
                    foreach (int segmentIndex in definition.ObjectTextures.Keys)
                    {
                        Bitmap segmentClip          = bg.Extract(definition.TextureSegments[segmentIndex]);
                        TexturedTileSegment segment = null;
                        foreach (IndexedTRObjectTexture texture in definition.ObjectTextures[segmentIndex])
                        {
                            if (segment == null)
                            {
                                _importSegments[definition].Add(segment = new TexturedTileSegment(texture, segmentClip));
                            }
                            else
                            {
                                segment.AddTexture(texture);
                            }
                        }
                    }

                    List <TR2Entities> spriteEntities = new List <TR2Entities>(definition.SpriteSequences.Keys);
                    foreach (TR2Entities spriteEntity in spriteEntities)
                    {
                        TRSpriteSequence existingSequence = spriteSequences.Find(s => s.SpriteID == (int)spriteEntity);
                        if (existingSequence != null)
                        {
                            definition.SpriteSequences.Remove(spriteEntity);
                            continue;
                        }
                        else
                        {
                            // Add it to the tracking list in case we are importing 2 or more models
                            // that share a sequence e.g. Dragon/Flamethrower and Flame_S_H
                            spriteSequences.Add(new TRSpriteSequence {
                                SpriteID = (int)spriteEntity
                            });
                        }

                        // The sequence will be merged later when we know the sprite texture offsets.
                        // For now, add the segments we need for packing.
                        Dictionary <int, List <IndexedTRSpriteTexture> > spriteTextures = definition.SpriteTextures[spriteEntity];
                        foreach (int segmentIndex in spriteTextures.Keys)
                        {
                            Bitmap segmentClip          = bg.Extract(definition.TextureSegments[segmentIndex]);
                            TexturedTileSegment segment = null;
                            foreach (IndexedTRSpriteTexture texture in spriteTextures[segmentIndex])
                            {
                                if (segment == null)
                                {
                                    _importSegments[definition].Add(segment = new TexturedTileSegment(texture, segmentClip));
                                }
                                else
                                {
                                    segment.AddTexture(texture);
                                }
                            }
                        }
                    }
                }
            }
        }