private Point mouseOffset; // 鼠标指针与被拖图片位置间的偏差值 /// <summary> /// 对一个RigidbodyUC进行注册,使之能够被拖动、右键旋转 /// </summary> /// <param name="rigidbodyUC"></param> public void Register(RigidbodyUC rigidbodyUC) { rigidbodyUC.MouseLeftButtonDown += (o1, e1) => { e1.Handled = true; draged = (o1 as RigidbodyUC); mouseOffset = e1.GetPosition(draged); lastPosition = draged.TranslatePoint(new Point(0, 0), SceneUC); if (draged.Parent == SceneUC.MainCanvas) { SceneUC.RemoveRigidbodyUC(draged); MainGrid.Children.Add(draged); } draged.SetValue(Panel.ZIndexProperty, 5); }; rigidbodyUC.MouseRightButtonDown += (o1, e1) => (o1 as RigidbodyUC).Rigidbody.Rotate(); }
private void PlayStopButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { // Stop if (SceneUC.IsPlaying) { PlayStopButton.Source = new BitmapImage(new Uri(@"./Images/PlayButton.png", UriKind.Relative)); foreach (var ui in ControlUIs) { ui.IsEnabled = true; } SceneUC.Stop(); } // Play else { if (InputGravity == null) { MessageBox.Show("重力加速度参数不合法"); return; } if (SceneUC.Scene.Ball == null) { MessageBox.Show("未放置小球"); return; } if (SceneUC.Scene.Flipper == null) { MessageBox.Show("未放置挡板"); return; } PlayStopButton.Source = new BitmapImage(new Uri(@"./Images/StopButton.png", UriKind.Relative)); SceneUC.Scene.Gravity = InputGravity.Value; SceneUC.Scene.Ball.Speed = SpeedBoardUC.Speed; if (SceneUC.Scene.Flipper != null) { SceneUC.Scene.Flipper.Speed = Vector2.Zero; } foreach (var ui in ControlUIs) { ui.IsEnabled = false; } SceneUC.Play(); } }
private void Window_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (draged == null) { return; } draged.SetValue(Panel.ZIndexProperty, 0); var pos = draged.TranslatePoint(new Point(0, 0), SceneUC); MainGrid.Children.Remove(draged); draged.Rigidbody.Position = new Vector2((int)(pos.X / SceneUC.BlockWidth + 0.5), (int)(pos.Y / SceneUC.BlockHeight + 0.5)); if (SceneUC.AddRigidbodyUC(draged) == false) { draged.Rigidbody.Position = new Vector2((int)(lastPosition.X / SceneUC.BlockWidth + 0.5), (int)(lastPosition.Y / SceneUC.BlockHeight + 0.5)); SceneUC.AddRigidbodyUC(draged); } draged = null; }