public static bool WithinBounds(this Shape b, Point s, Point e) { double x, y, width, height; if (b is Line) { var bl = (b as Line); var bo = new Bounds(new Point(bl.X1, bl.Y1), new Point(bl.X2, bl.Y2)); x = bo.LowerX; y = bo.LowerY; width = bo.UpperX - x; height = bo.UpperY - y; } else { x = Canvas.GetLeft(b) - 1; y = Canvas.GetTop(b) - 1; width = b.Width; height = b.Height; } return new Rect(s, e).IntersectsWith(new Rect(x, y, width, height)); }
private void PlaceTilesBetween(Point start, Point end) { var b = new Bounds(start, end); var im = new Image { Width = Gc.TILE_SIZE, Height = Gc.TILE_SIZE, Source = SelectedTileChoice.Image.Source }; for (int x = b.LowerX; x <= b.UpperX; x += Gc.TILE_SIZE) for (int y = b.LowerY; y <= b.UpperY; y += Gc.TILE_SIZE) { var t = new Tile { Rectangle = new Rectangle { Width = Gc.TILE_SIZE, Height = Gc.TILE_SIZE, Fill = new ImageBrush(im.Source) }, X = x / Gc.TILE_SIZE, Y = y / Gc.TILE_SIZE, Name = SelectedTileChoice.Name }; Canvas.SetLeft(t.Rectangle, x); Canvas.SetTop(t.Rectangle, y); Canvas.SetZIndex(t.Rectangle, FLOOR_LAYER); Map.Children.Add(t.Rectangle); TileList.Add(t); } }
//TODO: Future Eric, make this not so duplicated and unreadable public void RemoveWallsBetween(Point start, Point end) { var walls = Map.Children.OfType<Line>() .Where(x => x.WithinBounds(start, end)) .ToList(); var b = new Bounds(start, end); // removal bounds for (int i = walls.Count() - 1; i >= 0; i--) { var w = walls[i]; if (Math.Abs(w.X2 - w.X1) > Math.Abs(w.Y2 - w.Y1)) // is a horizontal wall { b.UpperX += Gc.TILE_SIZE; if (b.LowerX <= w.X1 && b.UpperX <= w.X2) // covers only left side of wall w.X1 = b.UpperX; else if (b.LowerX <= w.X2 && b.LowerX > w.X1 && b.UpperX >= w.X2) // covers only right side of wall w.X2 = b.LowerX; else if (b.LowerX <= w.X1 && b.UpperX >= w.X2) // covers entire wall RemoveWallOrDoorByLine(w); else if (b.LowerX > w.X1 && b.UpperX < w.X2) // covers middle of wall { var oldEnd = w.X2; w.X2 = b.LowerX; PlaceWall(new Point(b.UpperX, w.Y1), new Point(oldEnd - Gc.TILE_SIZE, w.Y1)); } b.UpperX -= Gc.TILE_SIZE; } else { b.UpperY += Gc.TILE_SIZE; if (b.LowerY <= w.Y1 && b.UpperY <= w.Y2) // covers only left side of wall w.Y1 = b.UpperY; else if (b.LowerY <= w.Y2 && b.LowerY > w.Y1 && b.UpperY >= w.Y2) // covers only right side of wall w.Y2 = b.LowerY; else if (b.LowerY <= w.Y1 && b.UpperY >= w.Y2) // covers entire wall RemoveWallOrDoorByLine(w); else if (b.LowerY > w.Y1 && b.UpperY < w.Y2) // covers middle of wall { var oldEnd = w.Y2; w.Y2 = b.LowerY; PlaceWall(new Point(w.X1, b.UpperY), new Point(w.X1, oldEnd - Gc.TILE_SIZE)); } b.UpperY -= Gc.TILE_SIZE; } if (w.X1 - w.X2 == 0 && w.Y1 - w.Y2 == 0) RemoveWallOrDoorByLine(w); } }
public void PlaceWall(Point start, Point end) { start = start.ConvertToTilePosition(Gc.TILE_SIZE); end = end.ConvertToTilePosition(Gc.TILE_SIZE); var b = new Bounds(start, end).KeepBoundsWidthOf1(); //hack to add last tile of wall since walls start at 0,0 but should go the full length of last tile if (b.LowerX == b.UpperX) b.UpperY += Gc.TILE_SIZE; else if (b.LowerY == b.UpperY) b.UpperX += Gc.TILE_SIZE; var w = new Wall { Line = new Line { X1 = b.LowerX, Y1 = b.LowerY, X2 = b.UpperX, Y2 = b.UpperY, Stroke = new SolidColorBrush(Colors.Gray), StrokeThickness = 3 }, }; Canvas.SetZIndex(w.Line, WALL_LAYER); Map.Children.Add(w.Line); WallList.Add(w); }
private void OnMouseMove(object sender, MouseEventArgs e) { var end = e.GetPosition(Map).ConvertToTilePosition(Gc.TILE_SIZE); var start = editor.isDragging ? editor.startPoint.ConvertToTilePosition(Gc.TILE_SIZE) : end; var b = new Bounds(start, end); Canvas.SetLeft(editor.selectionRectangle, b.LowerX); Canvas.SetTop(editor.selectionRectangle, b.LowerY); if (editor.isDragging) { if (editor.selectedTool.IsLine) b = b.KeepBoundsWidthOf1(); editor.selectionRectangle.Width = b.UpperX - b.LowerX + Gc.TILE_SIZE; editor.selectionRectangle.Height = b.UpperY - b.LowerY + Gc.TILE_SIZE; } }