public void ListAddCorners() { //arrange Room room = new Room("Leslokaal", 3, 4,3); List<Corner> startCorners = new List<Corner>(); startCorners.Add(new Corner(0, 0)); startCorners.Add(new Corner(10, 0)); startCorners.Add(new Corner(10, 10)); startCorners.Add(new Corner(0, 10)); //act room.AddCorners(startCorners.ToArray()); //assert List<Corner> corners = (List<Corner>)room.GetCorners(); Assert.AreEqual(4, corners.Count); Assert.IsNotNull(corners[0]); Assert.IsNotNull(corners[1]); Assert.IsNotNull(corners[2]); Assert.IsNotNull(corners[3]); Assert.IsInstanceOf(typeof(Corner), corners[0]); Assert.IsInstanceOf(typeof(Corner), corners[1]); Assert.IsInstanceOf(typeof(Corner), corners[2]); Assert.IsInstanceOf(typeof(Corner), corners[3]); }
/* With this constructor you can make the dialog for deleteing new items to a room. The handlers used in this class will be set here. */ public DeleteItemDialog(string name, Action parentCallback, Room targetRoom, ItemType itemType) : base(name, parentCallback) { this._callback = parentCallback; this._itemTypeHandler = ItemTypeHandler.GetInstance(); this._itemHandler = ItemHandler.GetInstance(); this._documentHandler = DocumentHandler.GetInstance(); this._targetRoom = targetRoom; this._itemType = itemType; }
/* * Returns the amount of itemtypes that are existent in the given room. * The itemtype that you require is given in the parameter. */ public int AmountInRoom(ItemType type, Room r) { List<Item> matches = ItemHandler.GetInstance().GetItemsBy(delegate (Item i) { return i.RoomID == r.GetID(); }); int rtn = 0; foreach (Item i in matches) if (i.Type.ID == type.ID) rtn++; return rtn; }
/** * 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 Constructor() { //arrange //act Room room = new Room("Leslokaal", 3, 4,4); //assert Assert.IsNotNull(room); Assert.IsInstanceOf(typeof(Room), room); }
public void CornerAddCorner(float x, float y) { //arrange Room room = new Room("Leslokaal", 3, 4,1); //act room.AddCorner(new Corner(x, y)); //assert List<Corner> corners = (List<Corner>)room.GetCorners(); Assert.AreEqual(1, corners.Count); Assert.IsNotNull(corners[0]); Assert.IsInstanceOf(typeof(Corner), corners[0]); }
/* * Returns all of the different itemtypes that are in the given room * This will distinct any duplicate values. */ public List<ItemType> InRoom(Room r) { List<ItemType> itemTypes = new List<ItemType>(); List<Item> matches = ItemHandler.GetInstance().GetItemsBy(delegate (Item i) { return i.RoomID == r.GetID(); }); foreach (Item i in matches) { if (itemTypes.Contains(i.Type)) continue; itemTypes.Add(i.Type); } return itemTypes; }
/** * This event is called when the cursor moves over this control. */ protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); if(this.ActiveBlueprint == null) return; PointF mouseLoc = this.PointToClient(Cursor.Position); PointF mouseOffset = new PointF(mouseLoc.X-this._center.X, mouseLoc.Y-this._center.Y); int? idState = ((int?)this.HoveredRoom?.GetID() ?? -1) + ((int?)this.HoveredCorner?.GetID() ?? -1) + ((int?)this.HoveredWall?.GetID() ?? -1) ; int nullState = (this.HoveredRoom != null ? 1 : 0) + (this.HoveredCorner != null ? 2 : 0) + (this.HoveredWall != null ? 4 : 0); int state = nullState + ((int)idState << 3); this.HoveredCorner = (this._editMode ? this.ActiveBlueprint.GetCornerAtPoint(mouseOffset, this.Renderer.CornerSizeEdit) : null); this.HoveredWall = (this._editMode && this.HoveredCorner == null ? this.ActiveBlueprint.GetWallAtPoint(mouseOffset, this.WallDistance) : null); this.HoveredRoom = (this.HoveredWall == null && this.HoveredCorner == null ? this.ActiveBlueprint.GetRoomAtPoint(mouseOffset) : null); idState = ((int?)this.HoveredRoom?.GetID() ?? -1) + ((int?)this.HoveredCorner?.GetID() ?? -1) + ((int?)this.HoveredWall?.GetID() ?? -1); nullState = (this.HoveredRoom != null ? 1 : 0) + (this.HoveredCorner != null ? 2 : 0) + (this.HoveredWall != null ? 4 : 0); int newState = nullState + ((int)idState << 3); //Panning if(this._panOrigin != PointF.Empty) { this._center = new PointF(this._center.X-(this._panOrigin.X-e.Location.X), this._center.Y-(this._panOrigin.Y-e.Location.Y)); this._panOrigin = e.Location; this.Invalidate(); } //Snapping if(this.SelectedCorner != null && this._movingCorner) { this._snapCorners.Clear(); float snapX, snapY, closestX, closestY; snapX = snapY = closestX = closestY = float.MaxValue; foreach(Room room in this.ActiveBlueprint.Rooms) { foreach(Corner corner in room.GetCorners()) { if(corner.Equals(this.SelectedCorner)) continue; float distX = Math.Abs(corner.GetPoint().X-mouseOffset.X); float distY = Math.Abs(corner.GetPoint().Y-mouseOffset.Y); if(distX <= closestX && distX < this.CornerSnapDistance) { snapX = corner.GetPoint().X; closestX = distX; this._snapCorners.Add(corner); } if(distY <= closestY && distY < this.CornerSnapDistance) { snapY = corner.GetPoint().Y; closestY = distY; this._snapCorners.Add(corner); } this._snapCorners.RemoveAll((c) => (!snapX.Equals(c.GetPoint().X) && !snapY.Equals(c.GetPoint().Y))); } } this.SelectedCorner.SetPoint(new PointF((!snapX.Equals(float.MaxValue) ? snapX : mouseOffset.X), (!snapY.Equals(float.MaxValue) ? snapY : mouseOffset.Y))); this.Invalidate(); } if(state != newState) this.Invalidate(); }
/** * 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(); } } }
/** * This method sets whether or not the panel is in Edit Mode. * The second parameter determines whether or not the original blueprint should be restored. * The second parameter does nothing when the editmode is activated. */ public void SetEditMode(bool editMode, bool restore = false) { if(this._editMode == editMode) return; this._editMode = editMode; if(this._editMode) { this.BackColor = this.Renderer.ColorTable.BackgroundEdit; this.OriginalBlueprint = this.ActiveBlueprint; this.ActiveBlueprint = this.OriginalBlueprint.Clone(); } else { this.BackColor = this.Renderer.ColorTable.BackgroundNormal; if(restore) this.ActiveBlueprint = this.OriginalBlueprint; this.OriginalBlueprint = null; this.HoveredRoom = null; this.HoveredCorner = null; this.HoveredWall = null; this.SelectedRoom = null; this.SelectedCorner = null; this.SelectedWall = null; this._currentRoom = null; } List<Corner> ac = new List<Corner>((IEnumerable<Corner>)WallHandler.GetInstance().GetAllWallCorners()); List<Wall> aw = new List<Wall>((IEnumerable<Wall>)WallHandler.GetInstance().GetAllWalls()); List<Room> ar = new List<Room>((IEnumerable<Room>)RoomHandler.GetInstance().GetAllRooms()); //This first loop saves all corners... foreach(Room room in this.ActiveBlueprint.Rooms) { foreach(Corner corner in room.GetCorners()) { corner.SaveChanges = !editMode; if(!restore && !editMode) { Corner orig = WallHandler.GetInstance().GetWallCornerByID(corner.GetID()); ac.Remove(orig); if(orig != null) { WallHandler.GetInstance().ReplaceWallCorner(orig, corner); } else { WallHandler.GetInstance().GetAllWallCorners().Add(corner); } if(corner.IsChanged) corner.OnChange(EventArgs.Empty); } } } //This second loop is required because of foreign key constraints in the DB... //All corners need to exist before the walls can be saved. foreach(Room room in this.ActiveBlueprint.Rooms) { foreach(Corner corner in room.GetCorners()) { foreach(Wall wall in corner.GetWalls()) { wall.SaveChanges = !editMode; if(!restore && !editMode) { Wall orig = WallHandler.GetInstance().GetWallBy((w) => w.GetID() == wall.GetID()); aw.Remove(orig); if(orig != null) { WallHandler.GetInstance().ReplaceWall(orig, wall); } else { WallHandler.GetInstance().GetAllWalls().Add(wall); } if(wall.IsChanged) wall.OnChange(EventArgs.Empty); } } } } //This third loop is required because of foreign key constraints in the DB... //All walls need to exist before the rooms can be saved. foreach(Room room in this.ActiveBlueprint.Rooms) { room.SaveChanges = !editMode; if(!restore && !editMode) { Room orig = RoomHandler.GetInstance().GetRoomBy((r) => r.GetID() == room.GetID()); ar.Remove(orig); if(orig != null) { RoomHandler.GetInstance().ReplaceRoom(orig, room); } else { RoomHandler.GetInstance().GetAllRooms().Add(room); } if(room.IsChanged) room.OnChange(EventArgs.Empty); } } //Remove removed rooms if(!restore && !editMode) { List<Room> removeR = new List<Room>(); List<Wall> removeW = new List<Wall>(); List<Corner> removeC = new List<Corner>(); foreach(Room room in ar) { if(room.GetFloorID() == DocumentHandler.GetInstance().CurrentFloor.GetID()) removeR.Add(room); } foreach(Wall wall in aw) { foreach(Room room in RoomHandler.GetInstance().GetAllRooms()) { if(room.GetFloorID() == DocumentHandler.GetInstance().CurrentFloor.GetID() && (room.GetCorners().Contains(wall.Left) || room.GetCorners().Contains(wall.Right)) && !removeW.Contains(wall)) removeW.Add(wall); } } foreach(Corner corner in ac) { foreach(Room room in RoomHandler.GetInstance().GetAllRooms()) { if(room.GetFloorID() == DocumentHandler.GetInstance().CurrentFloor.GetID() && room.GetCorners().Contains(corner) && !removeC.Contains(corner)) removeC.Add(corner); } } ((ExtendedObservableCollection<Room>)RoomHandler.GetInstance().GetAllRooms()).RemoveRange(removeR.ToArray()); ((ExtendedObservableCollection<Wall>)WallHandler.GetInstance().GetAllWalls()).RemoveRange(removeW.ToArray()); ((ExtendedObservableCollection<Corner>)WallHandler.GetInstance().GetAllWallCorners()).RemoveRange(removeC.ToArray()); } }
public void NullAddCorner() { //arrange Room room = new Room("Leslokaal", 3, 4,2); //act room.AddCorner(null); //assert List<Corner> corners = (List<Corner>)room.GetCorners(); Assert.AreEqual(0, corners.Count); }
/** * This method replaces a room in the collection with a new one. */ public void ReplaceRoom(Room oldRoom, Room newRoom) { int index = this._rooms.IndexOf(oldRoom); if(index >= 0) this._rooms[index] = newRoom; }
/** * Loads all rooms into cache memory so you can call them from a List<> */ public void LoadFromDatabase() { _rooms.Clear(); DataTable table = DatabaseHandler.GetInstance().SelectSQL("SELECT * FROM Room"); if(table != null) { foreach(DataRow row in table.Rows) { uint roomID = DatabaseUtil.parseInt(row, "ID"); uint floorID = DatabaseUtil.parseInt(row, "FloorID"); string name = row.Field<string>("Name"); uint functionID = DatabaseUtil.parseInt(row, "FunctionID"); Room room = new Room(name, roomID, floorID, functionID); room.IsLoaded = true; _rooms.Add(room); } } }
/** * This method deletes a room from the blueprint. * All corners that aren't included in other rooms will be deleted and with them the walls that connect to them. */ public void DeleteRoom(Room delRoom) { if(!this.Rooms.Contains(delRoom)) return; foreach(Corner corner in delRoom.GetCorners()) { bool contains = false; foreach(Room room in this.Rooms) { if(room.Equals(delRoom)) continue; contains |= room.GetCorners().Contains(corner); } if(!contains) { Wall[] copy = ((List<Wall>)corner.GetWalls()).ToArray(); this.DeleteWall(copy); corner.GetWalls().Clear(); } } this.Rooms.Remove(delRoom); }
public MoveItemDialog(String title, Action callback, Room targetRoom, ItemType targetItem) : base(title, callback) { this._targetRoom = targetRoom; this._targetItem = targetItem; }