/// <summary> /// On Drop event on canvas add new dragged item to the canvas at drop point /// </summary> private void dropNewObject(object sender, DragEventArgs e) { // Get the object that was dropped InterstellaObject DroppedObject = e.Data.GetData(typeof(InterstellaObject)) as InterstellaObject; // If Failed to get object return if (DroppedObject == null) { return; } // Get the point at which the object was dropped as a vector Point DropPoint = e.GetPosition((Canvas)sender); Vector DropPositionVector = new Vector(DropPoint.X, DropPoint.Y); // Find and set the relative position in the model of this object // It is Important that these are done in this order! DropPositionVector = Helpers.Centrlize(DropPositionVector, Helpers.CanvasOrigin.TopLeft); DropPositionVector -= CanvasPageViewModel.PanVector; DropPositionVector = CanvasPageViewModel.InverseSeperationScaler(DropPositionVector); DropPositionVector += CanvasPageViewModel.RadiusScale(DroppedObject.Radius); DroppedObject.Position = DropPositionVector; // Use the View Model to add the object, at this position, to the model _VM.AddObject(new InterstellaObject(DroppedObject)); }
/// <summary> /// On Mouse Up over object if a new object has been dropped put this into orbit around this object /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OjectMouseUp(object sender, MouseButtonEventArgs e) { // Get the view model of the object that mouse was releasd over InterstellaObjectViewModel SenderVM = ((Ellipse)sender).DataContext as InterstellaObjectViewModel; // If drag is active, and object overwhich the mouse was released is not the same as the drag object if (_DragActive && SenderVM != _DraggedObject) { // Calculate the velocity require of the drag object to orbit the other Vector OrbitVelocity = CanvasPageViewModel.GetOrbitVelocity(_DraggedObject.InterstellaObject, SenderVM.InterstellaObject); //set the object's velocity to the calculated above, and end the drag operation _DraggedObject.SetObjectVelocity(OrbitVelocity); _DragActive = false; } }
/// <summary> /// When the mouse is released over the page, record the point of release and if a drag is active change the velocity of the drag object. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void CanvasPage_MouseUp(object sender, MouseButtonEventArgs e) { // Get the position at which the house was released relative to the canvas _MouseReleasePoint = e.GetPosition((ItemsControl)sender); // If a drag drop is in process.. if (_DragActive) { // Find the difference between the mouse up and down points relative to the centre of the canvas Vector CentralReleasePoint = Helpers.Centrlize(new Vector(_MouseReleasePoint.X, _MouseReleasePoint.Y), Helpers.CanvasOrigin.TopLeft); Vector CentralDownPoint = Helpers.Centrlize(new Vector(_MouseDownPoint.X, _MouseDownPoint.Y), Helpers.CanvasOrigin.TopLeft); Vector DownReleaseDifference = CentralReleasePoint - CentralDownPoint; // And use it to set the object's velocity and end the drag operation _DraggedObject.InterstellaObject.Velocity = DownReleaseDifference * 2E2; _DragActive = false; } }
/// <summary> /// On key down check against key bindings /// </summary> private void CanvasPage_KeyDown(object sender, KeyEventArgs e) { // If this is the first tick for which the key is down add it to keys down if (!e.IsRepeat) { _KeysDown.Add(e.Key); } // get a shortened identifer to the canvas page view model instance // (for readability) CanvasPageViewModel CPVM = CanvasPageViewModel.Instance; // If the Key Down is a Pan Key. if (_PanKeys.Contains(e.Key)) { // If key is a repeat, add one to the pan amount if (!e.IsRepeat) { _PanAmount = 1; } else if (_PanAmount < 25) { _PanAmount += 1; } // start the pan vector as 0,0 Vector panVector = new Vector(0, 0); // add pan amount for every pan key that is down. foreach (var Key in _KeysDown) { if (Key == Key.W || Key == Key.Up) { panVector.Y += _PanAmount; } else if (Key == Key.A || Key == Key.Left) { panVector.X -= _PanAmount; } else if (Key == Key.S || Key == Key.Down) { panVector.Y -= _PanAmount; } else if (Key == Key.D || Key == Key.Left) { panVector.X += _PanAmount; } } // Pan the system by the pan vector CanvasPageViewModel.Pan(panVector); } // Switch for other control keys switch (e.Key) { // On Either Plus key, incriment system speed case Key.OemPlus: case Key.Add: if (CPVM.SystemSpeed + 0.5 >= 0.5 && CPVM.SystemSpeed + 0.5 <= 20) { CPVM.SystemSpeed += 0.5; } break; // On Either Minus key, decriment system speed case Key.OemMinus: case Key.Subtract: if (CPVM.SystemSpeed - 0.5 >= 0.5 && CPVM.SystemSpeed - 0.5 <= 20) { CPVM.SystemSpeed -= 0.5; } break; // Toggle System running on P case Key.P: if (!e.IsRepeat && CPVM.SystemRunning) { CPVM.SystemRunning = false; } else if (!e.IsRepeat) { CPVM.SystemRunning = true; } // Close Data Entry Box when play, regardless of data altered or not. if (CanvasPageViewModel.Instance.DataBoxVisible) { CanvasPageViewModel.Instance.HideDataEntryBox.Execute(null); } break; // Pause, and open the canvas escape menu on escape. Or vise verser for closing. case Key.Escape: if (!CanvasPageViewModel.Instance.SystemRunning && CanvasPageViewModel.Instance.EscMenu.Visiblity) { CanvasPageViewModel.Instance.Play(); } else { CanvasPageViewModel.Instance.Pause(); } CanvasPageViewModel.Instance.EscMenu.ToggleEscMenu(); break; } }