示例#1
0
 public static void Regenerate(TileTextureCollection c)
 {
     foreach (var grid in c.Grids)
     {
         Regenerate(grid);
     }
 }
示例#2
0
        public TextureSetFixture CreateTextureSet(int width, int height, TileType tileType)
        {
            var textureSet = new TextureSetFile
            {
                Width    = width,
                Height   = height,
                TileType = tileType
            };

            var gridParent = new TileTextureCollection
            {
                Parent = textureSet
            };

            var grid = new TextureGrid
            {
                MatcherType = MatcherType.Basic
            };

            grid.Tiles.Add(new TextureTile(false, 0, 0));
            grid.Tiles.Add(new TextureTile(false, 1, 0));
            grid.FormattingMetaData.Border                      = 1;
            grid.FormattingMetaData.BorderColor                 = border;
            grid.FormattingMetaData.Padding                     = 1;
            grid.FormattingMetaData.BackgroundColor             = background;
            grid.TextureTileFormattingMetaData.TileOutlineColor = outline;
            grid.CellSpacing = 1;

            gridParent.Grids.Add(grid);
            return(new TextureSetFixture(textureSet, gridParent, grid));
        }
示例#3
0
        public void ValidateEmptyTextureCollectionMetaData()
        {
            var tc   = new TileTextureCollection();
            var root = TextureSetFileWriter.GenerateCollection(tc);

            root.Name.Should().Be(Namespace + "collection");
            root.Should().HaveAttribute("id", "");
            root.Element(Namespace + "metadata").Should().BeNull();
        }
示例#4
0
 public void Produce(SKCanvas graphics,
                     TileTextureCollection c)
 {
     foreach (var g in c.Grids)
     {
         var node    = new TextureGridLayoutNode(prefs, g);
         var painter = new TextureGridPainter(prefs, node);
         painter.Draw(graphics);
     }
 }
示例#5
0
 public void SetUp()
 {
     collection = new TileTextureCollection
     {
         Parent = new TextureSetFile()
         {
             Width  = 64,
             Height = 32
         }
     };
 }
示例#6
0
        public IntDimension ComputeRenderedSize(TileTextureCollection c)
        {
            var width  = 0;
            var height = 0;

            foreach (var grid in c.Grids)
            {
                var node = new TextureGridLayoutNode(prefs, grid);
                var p    = node.Offset + node.Size;
                width  = Math.Max(p.X, width);
                height = Math.Max(p.Y, height);
            }

            return(new IntDimension(width, height));
        }
示例#7
0
        public void SetUp()
        {
            var textureCollection = new TileTextureCollection();

            var tf = new TextureSetFile
            {
                Width    = 64,
                Height   = 32,
                TileType = TileType.Grid
            };

            tf.Collections.Add(textureCollection);

            collection = textureCollection;
        }
示例#8
0
        public SKBitmap CreateBitmap(TileTextureCollection c, int scale = 1, SKBitmap?bmp = null)
        {
            scale = Math.Max(scale, 1);

            var expectedSize = ComputeRenderedSize(c);

            expectedSize = new IntDimension(expectedSize.Width * scale, expectedSize.Height * scale);
            if (bmp == null || bmp.Width != expectedSize.Width || bmp.Height != expectedSize.Height)
            {
                bmp?.Dispose();
                bmp = new SKBitmap(expectedSize.Width, expectedSize.Height);
            }

            using var ctx = new SKCanvas(bmp);
            ctx.Scale(scale, scale);
            ctx.Clear(SKColor.Empty);
            Produce(ctx, c);
            return(bmp);
        }
示例#9
0
        public async Task SaveFile(string fileName)
        {
            var textureSet = SelectedItem switch
            {
                TextureSetFile s => s,
                TileTextureCollection c => c.Parent,
                TextureGrid g => g.Parent?.Parent,
                TextureTile t => t.Parent?.Parent?.Parent,
                _ => null
            };

            if (textureSet == null)
            {
                return;
            }

            GeneratorPreferencesWriter.EnsureParentDirectoryExists(fileName);
            var doc = TextureSetFileWriter.GenerateXml(textureSet);

            await using var stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize: 4096, useAsync: true);
            await doc.SaveAsync(stream, default, default);
示例#10
0
        void OnArrangeTiles()
        {
            var collections = selectedItem switch
            {
                TextureSetFile tf => tf.Collections.ToArray(),
                TileTextureCollection tc => new[] { tc },
                TextureGrid gd => new[] { gd.Parent },
                TextureTile t => new[] { t.Parent?.Parent },
                _ => Array.Empty <TileTextureCollection>()
            };

            foreach (var c in collections)
            {
                if (c != null)
                {
                    TextureGridAutoLayout.ArrangeGrids(UserPreferences.Preferences, c);
                }
            }
        }

        void OnGenerateTiles()
        {
            var grids = selectedItem switch
            {
                TextureSetFile tf => tf.Collections.SelectMany(e => e.Grids),
                TileTextureCollection tc => tc.Grids,
                TextureGrid gd => Enumerable.Repeat(gd, 1),
                TextureTile t => Enumerable.Repeat(t.Parent, 1),
                _ => Array.Empty <TextureGrid>()
            };

            foreach (var g in grids)
            {
                if (g != null)
                {
                    TextureTileGenerator.Regenerate(g);
                }
            }
        }
示例#11
0
        public static bool ArrangeGrids(GeneratorPreferences prefs, TileTextureCollection c)
        {
            _ = prefs ?? throw new ArgumentNullException(nameof(prefs));
            var grids = c.Grids.Select(g => new TextureGridLayoutNode(prefs, g)).OrderBy(e => - e.Size.Width).ToList();

            foreach (var node in grids)
            {
                Logger.Verbose("Layout: {GridName} Weight: {Weight} Size:{Size}", node.Grid.Name, node.Size.Width * node.Size.Height, node.Size);
            }

            // All grids sorted by largest space consumed.
            var root    = new ArrangeNode <TextureGridLayoutNode>(2048, 2048);
            var success = true;

            foreach (var grid in grids)
            {
                if (!root.Insert(grid.Size, grid))
                {
                    success = false;
                }

                root.Print();
            }

            if (success)
            {
                root.Apply(n =>
                {
                    var content = n.Content;
                    if (content != null)
                    {
                        content.Offset = new IntPoint(n.X, n.Y);
                    }
                });
            }

            return(success);
        }
示例#12
0
        void OnTreeSelectionChanged()
        {
            var collection = structureTree.SelectedItem switch
            {
                TextureSetFile f => f.Collections.FirstOrDefault(),
                TileTextureCollection c => c,
                TextureGrid g => g.Parent,
                TextureTile t => t.Parent?.Parent,
                _ => null
            };

            var textureSet = structureTree.SelectedItem switch
            {
                TextureSetFile f => f,
                TileTextureCollection c => c.Parent,
                TextureGrid g => g.Parent?.Parent,
                TextureTile t => t.Parent?.Parent?.Parent,
                _ => null
            };

            Console.WriteLine("Selection changed " + structureTree.SelectedItem + " -> " + collection + " | " + textureSet);
            SelectedTextureCollection = collection;
            SelectedTextureSet        = textureSet;
        }
示例#13
0
        void OnAddCollection()
        {
            var textureSet = SelectedItem switch
            {
                TextureSetFile s => s,
                TileTextureCollection c => c.Parent,
                TextureGrid g => g.Parent?.Parent,
                TextureTile t => t.Parent?.Parent?.Parent,
           _ => null
            };

            textureSet?.Collections.Add(new TileTextureCollection()
            {
                Id = "New Collection"
            }.WithTextureGrid(new TextureGrid()

            {
                Name = "New Texture Grid"
            }));
        }

        void OnAddGrid()
        {
            var textureCollection = SelectedItem switch
            {
                TileTextureCollection c => c,
                TextureGrid g => g.Parent,
                TextureTile t => t.Parent?.Parent,
                _ => null
            };

            textureCollection?.Grids.Add(new TextureGrid()
            {
                Name = "New Texture Grid"
            });
        }

        void OnAddTile()
        {
            var textureGrid = SelectedItem switch
            {
                TextureGrid g => g,
                TextureTile t => t.Parent,
                _ => null
            };

            textureGrid?.Tiles.Add(new TextureTile());
        }

        bool HasAnySelection(object?selectedItem) => selectedItem != null;

        bool CanAddGrid(object?selectedItem) => selectedItem switch
        {
            TileTextureCollection => true,
            TextureGrid => true,
            TextureTile => true,
            _ => false
        };

        bool CanAddTile(object?selectedItem) => selectedItem switch
        {
            TextureGrid => true,
            TextureTile => true,
            _ => false
        };

        public Task OpenFile(string fileName)
        {
            try
            {
                var x = TextureSetFileLoader.Read(fileName);
                TextureFiles.Add(x);
                return(Task.CompletedTask);
            }
            catch (Exception e)
            {
                return(Task.FromException(e));
            }
        }
示例#14
0
 public void Deconstruct(out TextureSetFile file, out TileTextureCollection collection, out TextureGrid grid)
 {
     file       = File;
     collection = Collection;
     grid       = Grid;
 }
示例#15
0
 public TextureSetFixture(TextureSetFile file, TileTextureCollection collection, TextureGrid grid)
 {
     File       = file;
     Collection = collection;
     Grid       = grid;
 }