public void SaveMap() { if (Support.listObject != null && Support.IsExportXml == false) { for (int i = 0; i < WorkspaceWorking.Children.Count; ++i) { if (WorkspaceWorking.Children[i] is Image) { Image tempRect = (Image)WorkspaceWorking.Children[i]; if ((int)tempRect.Tag != (int)ObjectType.TILE_MAP) { ++Support.Count; OBJECT temp = new OBJECT((int)ObjectType.NORMAL_OBJECT, (int)tempRect.Tag, Support.Count, new VECTOR2D((float)Canvas.GetLeft(WorkspaceWorking.Children[i]), (float)Canvas.GetTop(WorkspaceWorking.Children[i])), new RECTANGLE((float)Canvas.GetLeft(WorkspaceWorking.Children[i]), (float)Canvas.GetTop(WorkspaceWorking.Children[i]), (int)tempRect.Width, (int)tempRect.Height)); VECTOR2D tempPosition = Support.ConvertCoordination(temp); Support.listObject.Add(new OBJECT((int)ObjectType.NORMAL_OBJECT, (int)tempRect.Tag, Support.Count, tempPosition, new RECTANGLE(tempPosition.cX, tempPosition.cY, (int)tempRect.Width, (int)tempRect.Height))); } } else if (WorkspaceWorking.Children[i] is Rectangle && Support.IsImport == true) { ++Support.Count; Rectangle tempImage = (Rectangle)WorkspaceWorking.Children[i]; if (tempImage.Tag != Support.gridLine) { OBJECT temp = new OBJECT((int)ObjectType.VIRTUAL_OBJECT, (int)tempImage.Tag, Support.Count, new VECTOR2D((float)Canvas.GetLeft(WorkspaceWorking.Children[i]), (float)Canvas.GetTop(WorkspaceWorking.Children[i])), new RECTANGLE((float)Canvas.GetLeft(WorkspaceWorking.Children[i]), (float)Canvas.GetTop(WorkspaceWorking.Children[i]), (int)tempImage.Width, (int)tempImage.Height)); VECTOR2D tempPosition = Support.ConvertCoordination(temp); Support.listObject.Add(new OBJECT((int)ObjectType.VIRTUAL_OBJECT, (int)tempImage.Tag, Support.Count, tempPosition, new RECTANGLE(tempPosition.cX, tempPosition.cY, (int)tempImage.Width, (int)tempImage.Height))); } } } Support.IsImport = false; if (Support.quadTree == null) { Support.quadTree = new CNode(0, PositionOfNode.TopLeft, new RECTANGLE(0, 8192, 8192, 8192)); for (int i = 0; i < Support.listObject.Count; ++i) { Support.quadTree.InsertObject(Support.quadTree, Support.listObject[i]); } } ExportXml.getInstance().MWriter.WriteStartDocument(); ExportXml.getInstance().WriteInfomationForMap(); ExportXml.getInstance().writeQuadtreeToXml(Support.quadTree, ExportXml.getInstance().MWriter); ExportXml.getInstance().MWriter.WriteEndDocument(); ExportXml.getInstance().MWriter.Close(); Support.IsExportXml = true; MessageBox.Show("Map đã được tạo thành công, dùng file map.xml để load game nhé ^^", "Thông báo"); } else { MessageBox.Show("Có map đâu mà đòi lưu ! - Muốn lưu hãy tạo map mới ^^", "Thông báo"); } }
private void click_to_load_file(object sender, RoutedEventArgs e) { Support.IsImport = true; OpenFileDialog openFileDialogSourcePicture = new OpenFileDialog(); openFileDialogSourcePicture.Filter = "XML (*.xml)|*.xml| All File (*.*)|*.*"; openFileDialogSourcePicture.FilterIndex = 1; openFileDialogSourcePicture.RestoreDirectory = true; String pathToImage = ""; if (openFileDialogSourcePicture.ShowDialog() == true) { CTile.CountID = 0; if (Support.map == null) { if (Support.MAPNAME == "1") { pathToImage = @"..\..\Resource\map\map1.png"; } else if (Support.MAPNAME == "2") { pathToImage = @"..\..\Resource\map\map2.png"; } else if (Support.MAPNAME == "3") { pathToImage = @"..\..\Resource\map\map3.png"; } Support.map = new CMap(new BitmapImage(new Uri(pathToImage, UriKind.Relative))); Support.WIDHT_MAP = (int)(Support.map.BitMap.PixelWidth); Support.HEIGHT_MAP = (int)(Support.map.BitMap.PixelHeight); if (Support.GRIDLINE == true) { DestroyGridline(); Support.GRIDLINE = true; CreateGridline(); } if (Support.IsExportXml == true) { Support.IsExportXml = false; ExportXml.getInstance().DestroyWriter(); } WorkspaceWorking.Width = Support.WIDHT_MAP; WorkspaceWorking.Height = Support.HEIGHT_MAP; Support.map.CreateTileMap(); } } if (Support.map != null) { BitmapSource bmpSource; PngBitmapEncoder bmpCreate = new PngBitmapEncoder(); byte[] arrPixel = new byte[((Support.map.TileMap.Count * Support.WIDTH_OF_TILE * Support.map.BitMap.Format.BitsPerPixel) / 8) * Support.HEIGHT_OF_TILE]; int offsetX = 0; int offsetY = 0; for (int i = 0; i < Support.map.TileMap.Count; ++i) { offsetX = Support.map.TileMap[i].ID; Support.map.TileMap[i].ExportBitMap(offsetX, offsetY, arrPixel, (Support.map.TileMap.Count * Support.WIDTH_OF_TILE * Support.map.BitMap.Format.BitsPerPixel / 8)); } FileStream stream = new FileStream(@"..\..\Resource\tilemap\tile_map.png", FileMode.Create); bmpSource = BitmapSource.Create(Support.map.TileMap.Count * Support.WIDTH_OF_TILE, Support.HEIGHT_OF_TILE, 96, 96, Support.map.BitMap.Format, null, arrPixel, ((Support.map.TileMap.Count * Support.WIDTH_OF_TILE * Support.map.BitMap.Format.BitsPerPixel) / 8)); bmpCreate.Frames.Add(BitmapFrame.Create(bmpSource)); bmpCreate.Save(stream); stream.Close(); ConvertFromTileToObject(); AddImageOfObjectToCanvas(); ImportXml.getInstance().loadCanvas(openFileDialogSourcePicture.FileName, WorkspaceWorking); Support.IsBackground = true; } }
public void click_to_load_backgroundmap(object sender, RoutedEventArgs e) { if (Support.map != null) { if (Support.IsSave == false) { MessageBox.Show("Map hiện tại sẽ bị xóa, vui lòng save trước khi tạo mới", "Thông Báo"); } else { Support.IsSave = false; Support.map = null; RemoveAllTileInCanvas(); } } OpenFileDialog openFileDialogSourcePicture = new OpenFileDialog(); openFileDialogSourcePicture.Filter = "PNG (*.png)|*.png| BMP (*.bmp)|*.bmp| All File (*.*)|*.*"; openFileDialogSourcePicture.FilterIndex = 3; openFileDialogSourcePicture.RestoreDirectory = true; if (openFileDialogSourcePicture.ShowDialog() == true) { CTile.CountID = 0; if (Support.map == null) { Support.map = new CMap(new BitmapImage(new Uri(openFileDialogSourcePicture.FileName, UriKind.Absolute))); Support.WIDHT_MAP = (int)(Support.map.BitMap.PixelWidth); Support.HEIGHT_MAP = (int)(Support.map.BitMap.PixelHeight); if (Support.GRIDLINE == true) { DestroyGridline(); Support.GRIDLINE = true; CreateGridline(); } if (Support.IsExportXml == true) { Support.IsExportXml = false; ExportXml.getInstance().DestroyWriter(); } WorkspaceWorking.Width = Support.WIDHT_MAP; WorkspaceWorking.Height = Support.HEIGHT_MAP; Support.map.CreateTileMap(); } } if (Support.map != null) { BitmapSource bmpSource; PngBitmapEncoder bmpCreate = new PngBitmapEncoder(); byte[] arrPixel = new byte[((Support.map.TileMap.Count * Support.WIDTH_OF_TILE * Support.map.BitMap.Format.BitsPerPixel) / 8) * Support.HEIGHT_OF_TILE]; int offsetX = 0; int offsetY = 0; for (int i = 0; i < Support.map.TileMap.Count; ++i) { offsetX = Support.map.TileMap[i].ID; Support.map.TileMap[i].ExportBitMap(offsetX, offsetY, arrPixel, (Support.map.TileMap.Count * Support.WIDTH_OF_TILE * Support.map.BitMap.Format.BitsPerPixel / 8)); } FileStream stream = new FileStream(@"..\..\Resource\tilemap\tile_map.png", FileMode.Create); bmpSource = BitmapSource.Create(Support.map.TileMap.Count * Support.WIDTH_OF_TILE, Support.HEIGHT_OF_TILE, 96, 96, Support.map.BitMap.Format, null, arrPixel, ((Support.map.TileMap.Count * Support.WIDTH_OF_TILE * Support.map.BitMap.Format.BitsPerPixel) / 8)); bmpCreate.Frames.Add(BitmapFrame.Create(bmpSource)); bmpCreate.Save(stream); stream.Close(); ConvertFromTileToObject(); AddImageOfObjectToCanvas(); Support.IsBackground = true; } }