00001 using System; 00002 using System.Windows; 00003 using System.Windows.Media; 00004 using System.Windows.Media.Imaging; 00005 using SpaceFlight.Simulation; 00006 00007 namespace SpaceFlight.GUI 00008 { 00009 public class SimulationView : FrameworkElement 00010 { 00011 public TestCase TestCase { get; set; } 00012 00013 private ImageSource planetImage; 00014 private ImageSource shipImage; 00015 private ImageSource explosionImage; 00016 00017 public SimulationView() 00018 { 00019 planetImage = new BitmapImage(new Uri("pack://application:,,,/Images/Planet.png")); 00020 shipImage = new BitmapImage(new Uri("pack://application:,,,/Images/Ship.png")); 00021 explosionImage = new BitmapImage(new Uri("pack://application:,,,/Images/Explosion.png")); 00022 } 00023 00024 protected override void OnRender(DrawingContext context) 00025 { 00026 base.OnRender(context); 00027 if (TestCase != null) 00028 { 00029 context.PushClip(new RectangleGeometry(new Rect(0, 0, ActualWidth, ActualHeight))); 00030 context.PushTransform(GetTransform()); 00031 00032 foreach (Vector2D point in TestCase.Trace) 00033 { 00034 context.DrawEllipse(Brushes.Gray, null, new Point(point.X, point.Y), 2, 2); 00035 } 00036 00037 foreach (Planet planet in TestCase.Planets) 00038 { 00039 context.DrawImage(planetImage, new Rect(planet.Position.X - planet.Radius, 00040 planet.Position.Y - planet.Radius, planet.Radius * 2, planet.Radius * 2)); 00041 } 00042 00043 for (int i = 0; i < TestCase.Waypoints.Count; ++i) 00044 { 00045 Brush brush; 00046 if (i < TestCase.TargetWaypoint) 00047 { 00048 brush = Brushes.Gray; 00049 } 00050 else if (i == TestCase.TargetWaypoint) 00051 { 00052 brush = Brushes.Red; 00053 } 00054 else 00055 { 00056 brush = Brushes.Black; 00057 } 00058 Vector2D waypoint = TestCase.Waypoints[i]; 00059 context.DrawEllipse(null, new Pen(brush, 3), new Point(waypoint.X, waypoint.Y), 10, 10); 00060 context.DrawEllipse(brush, null, new Point(waypoint.X, waypoint.Y), 3, 3); 00061 } 00062 00063 if (TestCase.IsActive || TestCase.TargetWaypoint == TestCase.Waypoints.Count) 00064 { 00065 Vector2D position = TestCase.Ship.Position; 00066 context.PushTransform(new ScaleTransform(0.6, 0.6, position.X, position.Y)); 00067 context.PushTransform(new RotateTransform(TestCase.Ship.Angle / Math.PI * 180, position.X, position.Y)); 00068 context.DrawImage(shipImage, new Rect( 00069 position.X - shipImage.Width / 2, position.Y - shipImage.Height / 2, 00070 shipImage.Width, shipImage.Height)); 00071 } 00072 else 00073 { 00074 context.DrawImage(explosionImage, new Rect( 00075 TestCase.Ship.Position.X - explosionImage.Width / 2, TestCase.Ship.Position.Y - explosionImage.Height / 2, 00076 explosionImage.Width, explosionImage.Height)); 00077 } 00078 } 00079 } 00080 00081 public Transform GetTransform() 00082 { 00083 TransformGroup transform = new TransformGroup(); 00084 double size = Math.Min(ActualWidth, ActualHeight); 00085 transform.Children.Add(new ScaleTransform(size / 1000, size / 1000)); 00086 if (ActualWidth > ActualHeight) 00087 { 00088 transform.Children.Add(new TranslateTransform((ActualWidth - size) / 2, 0)); 00089 } 00090 else 00091 { 00092 transform.Children.Add(new TranslateTransform(0, (ActualHeight - size) / 2)); 00093 } 00094 return transform; 00095 } 00096 } 00097 }