private void ParseTileImages(Lisp.Parser parser, ArrayList ImagesList) { if(parser.Type == Parser.LispType.END_LIST) return; int d = parser.Depth; do { ImageRegion region = new ImageRegion(); if(parser.Type == Parser.LispType.STRING) { region.ImageFile = parser.StringValue; } else if(parser.Type == Parser.LispType.START_LIST) { ParseImageRegion(parser, region); } else { throw new Exception("unexpected lisp data: " + parser.Type); } ImagesList.Add(region); } while(parser.Parse() && parser.Depth >= d); }
private void ParseImageRegion(Lisp.Parser parser, ImageRegion region) { parser.Parse(); if(parser.Type != Parser.LispType.SYMBOL) throw new Exception("expected symbol"); if(parser.SymbolValue != "region") throw new Exception("expected region symbol"); parser.Parse(); if(parser.Type != Parser.LispType.STRING) throw new Exception("expected string"); region.ImageFile = parser.StringValue; parser.Parse(); if(parser.Type != Parser.LispType.INTEGER) throw new Exception("expected integer"); region.Region.X = parser.IntegerValue; parser.Parse(); if(parser.Type != Parser.LispType.INTEGER) throw new Exception("expected integer"); region.Region.Y = parser.IntegerValue; parser.Parse(); if(parser.Type != Parser.LispType.INTEGER) throw new Exception("expected integer"); region.Region.Width = parser.IntegerValue; parser.Parse(); if(parser.Type != Parser.LispType.INTEGER) throw new Exception("expected integer"); region.Region.Height = parser.IntegerValue; parser.Parse(); if(parser.Type != Parser.LispType.END_LIST) throw new Exception("expected END_LIST"); }
private void OnSelectImageOk(object o, EventArgs args) { FileSelection selection = ((FileSelection.FSButton) o).FileSelection; string file = selection.Filename; selection.Destroy(); ChangeImage(new FileInfo(file).Name); int startid = tileset.Tiles.Count; for(int y = 0; y < TilesY; ++y) { for(int x = 0; x < TilesX; ++x) { int i = y*TilesX+x; Tile tile = new Tile(); tile.ID = startid + i; ImageRegion region = new ImageRegion(); region.ImageFile = currentimage; region.Region = new System.Drawing.Rectangle(x*32, y*32, 32, 32); tile.Images.Add(region); if(Tiles[i] != null) { Console.WriteLine( "Warning Tile in this region already existed..."); } Tiles[i] = tile; tileset.Tiles.Add(tile); } } FillTileList(); }
public void ParseMoreTiles(Lisp.Parser parser) { int blockWidth = 0; int blockHeight = 0; List<int> ids = new List<int>(); List<int> attributes = new List<int>(); List<int> datas = new List<int>(); List<string> imageNames = new List<string>(); float animFps = 0; int d = parser.Depth; while(parser.Parse() && parser.Depth >= d) { if(parser.Depth == d+1) { if(parser.Type != Parser.LispType.SYMBOL) throw new Exception("expected SYMBOL at supertux-tiles---tiles level, but got \"" + parser.StringValue + "\""); string symbol = parser.SymbolValue; parser.Parse(); switch(symbol) { case "width": blockWidth = parser.IntegerValue; break; case "height": blockHeight = parser.IntegerValue; break; case "ids": Parser.ParseIntList(parser, ids); break; case "attributes": Parser.ParseIntList(parser, attributes); break; case "datas": Parser.ParseIntList(parser, datas); break; case "anim-fps": animFps = parser.FloatValue; break; case "image": int subDepth = parser.Depth; while(parser.Depth >= subDepth) { imageNames.Add(parser.StringValue); parser.Parse(); } break; default: Console.WriteLine("Unknown tiles element " + symbol); break; } } } if(ids.Count != blockWidth * blockHeight) throw new ApplicationException("Must have width*height ids in tiles block, but found " + ids.Count.ToString()); if((attributes.Count != blockWidth * blockHeight) && attributes.Count > 0) //missing atributes == all-are-0-attributes throw new ApplicationException("Must have width*height attributes in tiles block"); if((datas.Count != blockWidth * blockHeight) && datas.Count > 0) //missing DATAs == all-are-0-DATAs throw new ApplicationException("Must have width*height DATAs in tiles block"); int id = 0; for(int y = 0; y < blockHeight; ++y) { for(int x = 0; x < blockWidth; ++x) { if (ids[id] != 0) { Tile tile = new Tile(); tile.Images = new ArrayList(); foreach (string str in imageNames) { ImageRegion region = new ImageRegion(); region.ImageFile = str; region.Region.X = x * TILE_WIDTH; region.Region.Y = y * TILE_HEIGHT; region.Region.Width = TILE_WIDTH; region.Region.Height = TILE_HEIGHT; tile.Images.Add(region); } tile.ID = ids[id]; tile.Attributes = (attributes.Count > 0)?attributes[id]:0; //missing atributes == all-are-0-attributes tile.Data = (datas.Count > 0)?datas[id]:0; //missing DATAs == all-are-0-DATAs tile.AnimFps = animFps; while(Tiles.Count <= tile.ID) Tiles.Add(null); Tiles[tile.ID] = tile; } id++; } } }
protected void OnImportImage(object o, EventArgs e) { FileChooserDialog fileChooser = new FileChooserDialog("Select ImageFile", MainWindow, FileChooserAction.Open, new object[] {}); fileChooser.AddButton(Gtk.Stock.Cancel, Gtk.ResponseType.Cancel); fileChooser.AddButton(Gtk.Stock.Ok, Gtk.ResponseType.Ok); fileChooser.DefaultResponse = Gtk.ResponseType.Ok; Gtk.FileFilter all = new Gtk.FileFilter(); all.Name = "All Files"; all.AddPattern("*"); fileChooser.AddFilter( all ); int result = fileChooser.Run(); fileChooser.Hide(); if(result != (int) ResponseType.Ok) return; string file = fileChooser.Filename; string trim = tilesetdir + "/"; if (!file.StartsWith(trim)){ Console.WriteLine( "Imported file must be located inside tileset directory"); return; } ChangeImage(file.TrimStart(trim.ToCharArray())); int startid = tileset.Tiles.Count; for(int y = 0; y < TilesY; ++y) { for(int x = 0; x < TilesX; ++x) { int i = y*TilesX+x; Tile tile = new Tile(); tile.ID = startid + i; ImageRegion region = new ImageRegion(); region.ImageFile = currentimage; region.Region = new System.Drawing.Rectangle(x*TileSet.TILE_WIDTH, y*TileSet.TILE_HEIGHT, TileSet.TILE_WIDTH, TileSet.TILE_HEIGHT); tile.Images.Add(region); if(Tiles[i] != null) { Console.WriteLine( "Warning Tile in this region already existed..."); } Tiles[i] = tile; tileset.Tiles.Add(tile); } } FillTileList(); }
private void parseTilesList(Parser parser) { int width = 0; int height = 0; string image = null; List<int> ids = null; int d = parser.Depth; while (parser.Parse() && parser.Depth >= d) { if (parser.Depth == d + 1) { if (parser.Type != Parser.LispType.SYMBOL) throw new Exception("expected SYMBOL"); string symbol = parser.SymbolValue; parser.Parse(); switch (symbol) { case "width": width = parser.IntegerValue; break; case "height": height = parser.IntegerValue; break; case "ids": ids = parseIdList(parser); break; case "attributes": SkipList(parser); break; case "image": image = parser.StringValue; break; default: throw new ArgumentException("Unexpected \"" + symbol + "\" in list \"tiles\""); } } } if (width < 1) throw new ArgumentException("Read \"tiles\" list without \"width\""); if (height < 1) throw new ArgumentException("Read \"tiles\" list without \"height\""); if (image == null) throw new ArgumentException("Read \"tiles\" list without \"image\""); if (ids.Count < width * height) throw new ArgumentException("Read \"tiles\" list with to few \"ids\""); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { Tile tile = new Tile(); tile.ID = ids[y*width+x]; ImageRegion ir = new ImageRegion(); ir.ImageFile = image; ir.Region = Rectangle.Empty; ir.RelativeRegion = new RectangleF((float)x / width, (float)y / width, 1.0F / width, 1.0F / height); tile.Images.Add(ir); while (tile.ID >= Tiles.Count) Tiles.Add(null); Tiles[tile.ID] = tile; } } }