/// <summary> /// constructor. /// </summary> /// <param name="canvasModel">used to update the local canvas object</param> /// <param name="position">used to position the drone in the canvas</param> /// <param name="showCoverage">used to set if the coverage map should be enabled</param> public DroneViewModel(MainCanvasModel canvasModel, Point position, bool showCoverage, Canvas rootCanvas, GeometryGroup connectors, CommandCenterViewModel commandModel) { //init component, set data conext for xaml InitializeComponent(); DataContext = this; //update the canvas element, create new drone _canvasModel = canvasModel; DroneModel = new DroneModel(); SetPosition(position); _showCoverageRadius = showCoverage; _rootCanvas = rootCanvas; _connectors = connectors; _commandModel = commandModel; DroneModel.NodeId = canvasModel.NodeIdCount; NodeId = canvasModel.NodeIdCount.ToString(); //check if the new drone is in range of another Node, and if it is, link the two nodes DroneModel.checkCoverageCollision(_rootCanvas, this.DroneModel, _connectors); DroneModel.UpdateLinks(CanvasLeft, CanvasTop, 300, 300, DroneModel); DroneModel.checkCoverageCollision(_rootCanvas, this.DroneModel, _connectors); }
/// <summary> /// Initializes a new instance of the mainWindow class. /// </summary> public MainWindow() { InitializeComponent(); DataContext = this; CanvasModel = new MainCanvasModel(); _commandModel = new CommandCenterViewModel(); //create new drone at point 50, 50 _droneView = new DroneViewModel(_canvasModel, new Point(50, 50), _showCoverageRadius, RootCanvas, connectors, _commandModel); RootCanvas.Children.Add(_droneView); RootCanvas.Children.Add(_commandModel); //subscribe to warning event if path does not exist _droneView.WarningEvent += new DroneViewModel.WarningEventHandler(WarningEventHandler); _droneView.LoadingEvent += new DroneViewModel.LoadingEventHandler(LoadingEventHandler); }
/// <summary> /// Handles the PreviewMouseLeftButtonUp event of the Window control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The instance containing the event data.</param> private void Window_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { //check to see if a new link is being placed if (CanvasModel.IsAddNewLink && CanvasModel.IsLinkStarted) { bool linked = false; //check to see if the source is the command center if (e.Source.GetType() == typeof(CommandCenterViewModel)) { CommandCenterViewModel targetNode = e.Source as CommandCenterViewModel; CanvasModel.Link.Line.EndPoint = e.GetPosition(this); if (CanvasModel.Link.Line.EndPoint != CanvasModel.Link.Line.StartPoint && CanvasModel.LinkedCommandCenter != targetNode) { CanvasModel.LinkedDrone.LinkToCommand(targetNode, CanvasModel.Link.Line); linked = true; } } //check to see if the item being placed is a drone model if (e.Source.GetType() == typeof(DroneViewModel)) { DroneViewModel targetNode = e.Source as DroneViewModel; CanvasModel.Link.Line.EndPoint = e.GetPosition(this); //place the drone on the canvas if (CanvasModel.Link.Line.EndPoint != CanvasModel.Link.Line.StartPoint && CanvasModel.LinkedDrone != targetNode) { CanvasModel.LinkedDrone.LinkToDrone(targetNode, ref CanvasModel.Link.Line); linked = true; } } //link up the new node if (!linked) { connectors.Children.Remove(CanvasModel.Link.Line); CanvasModel.Link = null; } CanvasModel.IsLinkStarted = CanvasModel.IsAddNewLink = false; ResetCursorAndButtons(); e.Handled = true; } //if delete is active if (CanvasModel.IsDeleteActive) { //check if it is a drone model if (e.Source.GetType() == typeof(DroneViewModel)) { var selectedDrone = e.Source as DroneViewModel; //loop through everything on canvas starting at root foreach (var child in RootCanvas.Children) { NodeModel targetChild = null; //checks to see the child is a drone or tower if (child.GetType() == typeof(DroneViewModel)) { if (child.GetType() == typeof(DroneViewModel)) { var view = child as DroneViewModel; targetChild = view.DroneModel as NodeModel; } else { var view = child as TowerViewModel; targetChild = view.TowerModel as NodeModel; } Geometry lineToRemove = null; //remove the start lines from the list foreach (LineGeometry line in selectedDrone.DroneModel.StartLines.ToArray()) { List <Geometry> lines = new List <Geometry>(targetChild.EndLines); lineToRemove = lines.Find(x => x.Bounds == line.Bounds); if (lineToRemove != null) { connectors.Children.Remove(lineToRemove); } var lineData = lineToRemove as LineGeometry; selectedDrone.DroneModel.StartLines.Remove(lineData); selectedDrone.DroneModel.EndLines.Remove(lineData); targetChild.StartLines.Remove(lineData); targetChild.EndLines.Remove(lineData); selectedDrone.DroneModel.connections.Remove(targetChild); targetChild.connections.Remove(selectedDrone.DroneModel); } //remove end lines from the list foreach (LineGeometry line in selectedDrone.DroneModel.EndLines.ToArray()) { List <Geometry> lines = new List <Geometry>(targetChild.StartLines); lineToRemove = lines.Find(x => x.Bounds == line.Bounds); if (lineToRemove != null) { connectors.Children.Remove(lineToRemove); } var lineData = lineToRemove as LineGeometry; selectedDrone.DroneModel.StartLines.Remove(lineData); selectedDrone.DroneModel.EndLines.Remove(lineData); targetChild.StartLines.Remove(lineData); targetChild.EndLines.Remove(lineData); selectedDrone.DroneModel.connections.Remove(targetChild); targetChild.connections.Remove(selectedDrone.DroneModel); } } } //remove the start lines from the canvas foreach (LineGeometry line in selectedDrone.DroneModel.StartLines) { var lineToRemove = connectors.Children.Where(x => x.Bounds == line.Bounds); connectors.Children.Remove(lineToRemove.FirstOrDefault()); } //remove the end lines from the canvas foreach (LineGeometry line in selectedDrone.DroneModel.EndLines) { var lineToRemove = connectors.Children.Where(x => x.Bounds == line.Bounds); connectors.Children.Remove(lineToRemove.FirstOrDefault()); } RootCanvas.Children.Remove(selectedDrone); selectedDrone.DroneModel.connections.Clear(); selectedDrone.DroneModel.StartLines.Clear(); selectedDrone.DroneModel.EndLines.Clear(); } //check to see if it's a tower else if (e.Source.GetType() == typeof(TowerViewModel)) { var selectedDrone = e.Source as TowerViewModel; //loop through all nodes on canvas starting at root foreach (var child in RootCanvas.Children) { NodeModel targetChild = null; //checks to seee if the child is a drone or a tower if (child.GetType() == typeof(DroneViewModel)) { if (child.GetType() == typeof(DroneViewModel)) { var view = child as DroneViewModel; targetChild = view.DroneModel as NodeModel; } else { var view = child as TowerViewModel; targetChild = view.TowerModel as NodeModel; } Geometry lineToRemove = null; //remove start lines from the list foreach (LineGeometry line in selectedDrone.TowerModel.StartLines.ToArray()) { List <Geometry> lines = new List <Geometry>(targetChild.EndLines); lineToRemove = lines.Find(x => x.Bounds == line.Bounds); if (lineToRemove != null) { connectors.Children.Remove(lineToRemove); } var lineData = lineToRemove as LineGeometry; selectedDrone.TowerModel.StartLines.Remove(lineData); selectedDrone.TowerModel.EndLines.Remove(lineData); targetChild.StartLines.Remove(lineData); targetChild.EndLines.Remove(lineData); selectedDrone.TowerModel.connections.Remove(targetChild); targetChild.connections.Remove(selectedDrone.TowerModel); } //remove end lines from the list foreach (LineGeometry line in selectedDrone.TowerModel.EndLines.ToArray()) { List <Geometry> lines = new List <Geometry>(targetChild.StartLines); lineToRemove = lines.Find(x => x.Bounds == line.Bounds); if (lineToRemove != null) { connectors.Children.Remove(lineToRemove); } var lineData = lineToRemove as LineGeometry; selectedDrone.TowerModel.StartLines.Remove(lineData); selectedDrone.TowerModel.EndLines.Remove(lineData); targetChild.StartLines.Remove(lineData); targetChild.EndLines.Remove(lineData); selectedDrone.TowerModel.connections.Remove(targetChild); targetChild.connections.Remove(selectedDrone.TowerModel); } } } //remove the start lines from the canvas foreach (LineGeometry line in selectedDrone.TowerModel.StartLines) { var lineToRemove = connectors.Children.Where(x => x.Bounds == line.Bounds); connectors.Children.Remove(lineToRemove.FirstOrDefault()); } //remove the end lines from the canvas foreach (LineGeometry line in selectedDrone.TowerModel.EndLines) { var lineToRemove = connectors.Children.Where(x => x.Bounds == line.Bounds); connectors.Children.Remove(lineToRemove.FirstOrDefault()); } RootCanvas.Children.Remove(selectedDrone); selectedDrone.TowerModel.connections.Clear(); selectedDrone.TowerModel.StartLines.Clear(); selectedDrone.TowerModel.EndLines.Clear(); } CanvasModel.IsDeleteActive = false; } ResetCursorAndButtons(); }
/// <summary> /// Links the drone to the command center /// </summary> /// <param name="target"></param> /// <param name="line"></param> public void LinkToCommand(CommandCenterViewModel target, LineGeometry line) { DroneModel.LinkToNode(DroneModel, target.CommandModel, ref line, this.ActualWidth, this.ActualHeight, CanvasLeft, CanvasTop, target.ActualWidth, target.ActualHeight, target.CanvasLeft, target.CanvasTop); }