Exemplo n.º 1
0
        /// <summary>
        /// Handles the PreviewMouseLeftButtonDown 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_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (CanvasModel.IsAddNewDrone)
            {
                //place a node at the position of the mouse
                var position = e.GetPosition(this);
                position.X -= _droneView.ActualWidth / 2;
                position.Y -= _droneView.ActualHeight / 2 + 30;
                //increase the amount of nodes that are in the window
                CanvasModel.NodeIdCount++;
                DroneViewModel drone = new DroneViewModel(_canvasModel, position, _showCoverageRadius, RootCanvas, connectors, _commandModel);

                //subscribe to warning event if path does not exist
                drone.WarningEvent += new DroneViewModel.WarningEventHandler(WarningEventHandler);
                drone.LoadingEvent += new DroneViewModel.LoadingEventHandler(LoadingEventHandler);

                //add the drone
                RootCanvas.Children.Add(drone);
                CanvasModel.IsAddNewDrone = false;
                ResetCursorAndButtons();
                e.Handled = true;
            }
            //check to see if tower is being placed
            if (CanvasModel.IsAddTowerActive)
            {
                //get position of click
                var position = e.GetPosition(this);
                position.X -= _droneView.ActualWidth / 2;
                position.Y -= _droneView.ActualHeight / 2 + 30;

                //setup new line
                CanvasModel.Link = new LineModel();


                TowerViewModel tower = new TowerViewModel(position, _canvasModel, _showCoverageRadius, RootCanvas, connectors);

                //link the new tower to the command
                tower.TowerModel.LinkToNode(tower.TowerModel, _commandModel.CommandModel, ref _canvasModel.Link.Line, tower.ActualWidth, tower.ActualHeight, tower.CanvasLeft, tower.CanvasTop,
                                            _commandModel.ActualWidth, _commandModel.ActualHeight, _commandModel.CanvasLeft, _commandModel.CanvasTop);


                tower.TowerModel.connections.Add(_commandModel.CommandModel);

                //add the children to the canvas
                RootCanvas.Children.Add(tower);
                connectors.Children.Add(_canvasModel.Link.Line);

                //reset cursor
                CanvasModel.IsAddTowerActive = false;
                ResetCursorAndButtons();
                CanvasModel.TowerNodeIdCount++;

                e.Handled = true;
            }

            //chck to see if the node is a drone or command ceneter
            if (CanvasModel.IsAddNewLink && e.Source.GetType() == typeof(DroneViewModel) || e.Source.GetType() == typeof(CommandCenterViewModel))
            {
                if (!CanvasModel.IsLinkStarted)
                {
                    if (CanvasModel.Link == null || CanvasModel.Link.Line.EndPoint != CanvasModel.Link.Line.StartPoint)
                    {
                        //get the position of the node
                        Point position = e.GetPosition(this);
                        position.Y -= 30;
                        //create a new link
                        CanvasModel.Link = new LineModel(position, position);
                        connectors.Children.Add(CanvasModel.Link.Line);
                        CanvasModel.IsLinkStarted = true;
                        //check to see if it is a drone
                        if (e.Source.GetType() == typeof(DroneViewModel))
                        {
                            CanvasModel.LinkedDrone         = e.Source as DroneViewModel;
                            CanvasModel.LinkedCommandCenter = null;
                        }
                        //check to see if tis a command center
                        else
                        {
                            CanvasModel.LinkedCommandCenter = e.Source as CommandCenterViewModel;
                            CanvasModel.LinkedDrone         = null;
                        }

                        e.Handled = true;
                    }
                }
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Links to tower.
 /// </summary>
 /// <param name="target">The target.</param>
 /// <param name="line">The line.</param>
 public void LinkToTower(TowerViewModel target, LineGeometry line)
 {
     DroneModel.LinkToNode(DroneModel, target.TowerModel, ref line, this.ActualWidth, this.ActualHeight, CanvasLeft, CanvasTop,
                           target.ActualWidth, target.ActualHeight, target.CanvasLeft, target.CanvasTop);
 }