/** * This method returns a copy of the blueprint. * This is a Deep Copy, meaning that all the Rooms, Corners in those rooms and Walls connected to those corners will be copied. * The purpose of this is to make a new copy to work in when editing a blueprint, while preserving the original. */ public Blueprint Clone() { Blueprint newBp = new Blueprint(this.Name); List<Wall> newWalls = new List<Wall>(); List<Corner> newCorners = new List<Corner>(); foreach(Room room in this.Rooms) { Room newRoom = new Room(room.Name, room.GetID(), room.GetFloorID(), room.FunctionID); newBp.Rooms.Add(newRoom); foreach(Corner corner in room.GetCorners()) { Corner newCorner = newCorners.Find( (c) => (c.GetID() == corner.GetID()) ); if(newCorner != null) { newRoom.AddCorner(newCorner); continue; } newCorner = new Corner(corner.GetID(), corner.GetPoint()); newRoom.AddCorner(newCorner); newCorners.Add(newCorner); foreach(Wall wall in corner.GetWalls()) { Wall newWall = newWalls.Find( (w) => (w.GetID() == wall.GetID()) ); if(newWall != null) { if(newWall.Left.GetID() == corner.GetID()) { newWall.Left = newCorner; } else if(newWall.Right.GetID() == corner.GetID()) { newWall.Right = newCorner; } if(newWall.GetType() == typeof(Door)) { ((Door)newWall).Hinge = (((Door)newWall).Hinge.Equals(newWall.Left) ? newWall.Left : newWall.Right); } newCorner.AddWall(newWall); continue; } Corner left = (wall.Left.Equals(newCorner) ? newCorner : wall.Left); Corner right = (wall.Right.Equals(newCorner) ? newCorner : wall.Right); if(wall.GetType() == typeof(Door)) { newWall = new Door(wall.GetID(), left, right, (((Door)wall).Hinge.GetID() == left.GetID() ? left : right), ((Door)wall).Direction); } else { newWall = new Wall(wall.GetID(), left, right); } newWalls.Add(newWall); newCorner.AddWall(newWall); } } newRoom.IsChanged = room.IsChanged; } return newBp; }
public void DoorAddWall() { //Arrange Corner left = new Corner(new PointF(0, 0)); Corner right = new Corner(new PointF(10, 0)); Door door = new Door(left, right); //Act left.AddWall(door); right.AddWall(door); //Assert IList lWalls = left.GetWalls(); Assert.AreEqual(1, lWalls.Count); Assert.IsInstanceOf<Door>(lWalls[0]); Assert.AreEqual(door, lWalls[0]); }
/** * This event is called when the user begins to hold down a mouse button while on this control. */ protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); if(this.ActiveBlueprint == null) return; if(e.Button.HasFlag(MouseButtons.Right)) { this.LoadContextMenu(); } else if(e.Button.HasFlag(MouseButtons.Middle)) { //Panning if(Control.ModifierKeys.HasFlag(Keys.Control)) { this.Size = new Size(this.Parent.ClientSize.Width - 10, this.Parent.ClientSize.Height - 35); this._center = new PointF(this.Size.Width/2, this.Size.Height/2); this.Invalidate(); } else { this._panOrigin = e.Location; } return; } else if(this._editMode && e.Button.HasFlag(MouseButtons.Left)) { if(this._status == Status.CreateCorner) { //New corner PointF mouseLoc = this.PointToClient(Cursor.Position); PointF mouseOffset = new PointF(mouseLoc.X-this._center.X, mouseLoc.Y-this._center.Y); Corner newCorner = new Corner(mouseOffset); if(this._currentRoom != null) { this._currentRoom.AddCorner(newCorner); this._status = Status.CreateRoom; if(this.SelectedCorner != null) { Wall newWall = new Wall(newCorner, this.SelectedCorner); newCorner.AddWall(newWall); this.SelectedCorner.AddWall(newWall); } } this.Cursor = Cursors.Default; this.SelectedCorner = newCorner; this.Invalidate(); } else if(this._status == Status.ConnectCorners) { //Connect 2 corners PointF mouseLoc = this.PointToClient(Cursor.Position); PointF mouseOffset = new PointF(mouseLoc.X-this._center.X, mouseLoc.Y-this._center.Y); Corner otherCorner = this.ActiveBlueprint.GetCornerAtPoint(mouseOffset, this.Renderer.CornerSizeEdit); if(this.SelectedCorner != null && otherCorner != null && !this.SelectedCorner.Equals(otherCorner)) { Wall newWall = new Wall(otherCorner, this.SelectedCorner); otherCorner.AddWall(newWall); this.SelectedCorner.AddWall(newWall); this._status = (this._currentRoom != null ? Status.CreateRoom : Status.None); if(this._currentRoom != null){ if(this._currentRoom.GetCorners().Contains(this.SelectedCorner) && !this._currentRoom.GetCorners().Contains(otherCorner)) { this._currentRoom.AddCorner(otherCorner); } else if(this._currentRoom.GetCorners().Contains(otherCorner) && !this._currentRoom.GetCorners().Contains(this.SelectedCorner)) { this._currentRoom.AddCorner(this.SelectedCorner); } } this.Cursor = Cursors.Default; } } else { //Selects if(this.SelectedCorner != null) this.SelectedCorner.SaveChanges = true; if(this.SelectedWall != null) this.SelectedWall.SaveChanges = true; int? state = ((int?)this.SelectedRoom?.GetID() ?? -1) + ((int?)this.SelectedCorner?.GetID() ?? -1) + ((int?)this.SelectedWall?.GetID() ?? -1); this.SelectedRoom = this.HoveredRoom; this.SelectedCorner = this.HoveredCorner; this.SelectedWall = this.HoveredWall; int? newState = ((int?)this.SelectedRoom?.GetID() ?? -1) + ((int?)this.SelectedCorner?.GetID() ?? -1) + ((int?)this.SelectedWall?.GetID() ?? -1); if(this.SelectedCorner != null) { if(this._status == Status.CreateRoomSelect) { this._currentRoom.GetCorners().Add(this.SelectedCorner); this._status = Status.CreateRoom; } else { this.SelectedCorner.SaveChanges = false; this._movingCorner = true; } } if(this.SelectedWall != null) this.SelectedWall.SaveChanges = false; if(state != newState) this.Invalidate(); } } }
public void NullAddWall() { //Arrange Corner left = new Corner(new PointF(0, 0)); Corner right = new Corner(new PointF(10, 0)); Wall wall = null; //Act left.AddWall(wall); right.AddWall(wall); //Assert IList lWalls = left.GetWalls(); Assert.AreEqual(0, lWalls.Count); }