using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; using System.Xml.Serialization; using System.IO; namespace SimpleRadar { public partial class UserControlRadarView : UserControl { Dictionary> RadarPlots; List Maps; public String SelectedAircraft = ""; public UserControlRadarView() { InitializeComponent(); this.SetStyle( ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true); this.MouseWheel += new MouseEventHandler(Form1_MouseWheel); } private void UserControl1_Load(object sender, EventArgs e) { LoadSectorsFile(@"sectors.xml"); //Set the drawing deleagate for the Beacon drawing Sector.PanAndZoomX = new Sector.PanAndZoom(PanAndZoomX); Sector.PanAndZoomY = new Sector.PanAndZoom(PanAndZoomY); //Set the drawing deleagate for the Beacon drawing FranceMap.PanAndZoomX = new FranceMap.PanAndZoom(PanAndZoomX); FranceMap.PanAndZoomY = new FranceMap.PanAndZoom(PanAndZoomY); } protected override void OnPaint(PaintEventArgs e) { // we draw the progressbar normally with // the flags sets to our settings // DrawProgressBar(pe.Graphics); Graphics g = e.Graphics; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.FillRectangle(Brushes.White, this.ClientRectangle); //Draw the map FranceMap.Draw(g, Pens.Gray); //Draw the sectors if (Sectors != null) { foreach (Sector s in Sectors) { s.Draw(g, Pens.LightGray); } } //Draw the Radar if (RadarPlots != null) { foreach (var track in RadarPlots) { //Draw the label if (track.Value.Count > 0) { int dx = (int)(10 * Zoom) / 2; AircraftPosition aircraft = track.Value[track.Value.Count - 1]; if (aircraft.Flight == SelectedAircraft) { SizeF s = g.MeasureString(aircraft.CallSign, Font); g.FillRectangle(Brushes.Red, GetScreenCoordinates((float)aircraft.X, (float)aircraft.Y).X + dx, GetScreenCoordinates((float)aircraft.X, (float)aircraft.Y).Y - dx, s.Width, s.Height); } g.DrawString(aircraft.CallSign, Font, Brushes.Black, GetScreenCoordinates((float)aircraft.X, (float)aircraft.Y).X + dx, GetScreenCoordinates((float)aircraft.X, (float)aircraft.Y).Y - dx); g.DrawLine(Pens.Gray, GetScreenCoordinates((float)aircraft.X, (float)aircraft.Y).X + dx, GetScreenCoordinates((float)aircraft.X, (float)aircraft.Y).Y - dx + Font.Height / 2, GetScreenCoordinates((float)aircraft.X, (float)aircraft.Y).X, GetScreenCoordinates((float)aircraft.X, (float)aircraft.Y).Y); } int pos = 0; foreach (var plot in track.Value) { float dx = (pos * Zoom) / 5; float x = GetScreenCoordinates((float)plot.X, (float)plot.Y).X - dx / 2; float y = GetScreenCoordinates((float)plot.X, (float)plot.Y).Y - dx / 2; if (plot.Flight == SelectedAircraft) g.FillRectangle(Brushes.Red, x, y, dx, dx); g.DrawRectangle(Pens.Black, x, y, dx, dx); pos++; } } } // g.FillRectangle(item.Brush, GetScreenCoordinates(item.X, item.Y).X, GetScreenCoordinates(item.X, item.Y).Y, item.Size * Zoom, item.Size * Zoom); } void Form1_MouseWheel(object sender, MouseEventArgs e) { //Store the original position PointF orig = GetImageCoordinates(e.X, e.Y); // Console.WriteLine("orig x " + orig.X + " orig y " + orig.Y); if (e.Delta > 0) Zoom += 0.1f * Zoom; if (e.Delta < 0) Zoom -= 0.1f * Zoom; if (Zoom < 0.1) Zoom = 0.1f; if (Zoom > 20) Zoom = 20.0f; PointF newPos = GetScreenCoordinates(orig.X, orig.Y); // Console.WriteLine("newPos x " + newPos.X + " newPos y " + newPos.Y); //Correct the pan PanX -= (newPos.X - e.X) / Zoom; PanY += (newPos.Y - e.Y) / Zoom; PointF Corrected = GetScreenCoordinates(e.X, e.Y); // Console.WriteLine("Corrected x " + newPos.X + " Corrected y " + newPos.Y); // Console.WriteLine(""); Console.WriteLine(""); this.Invalidate(); } float PanX = 0; float PanY = 0; float Zoom = 1; private double PanAndZoomX(double x) { // return GetImageCoordinates((float)x, 0).X; return GetScreenCoordinates((float)x, 0).X; } private double PanAndZoomY(double y) { // return GetImageCoordinates(0, (float)y).Y; return GetScreenCoordinates(0, (float)y).Y; } PointF GetScreenCoordinates(float Xi, float Yi) { // return new PointF(Zoom * (Xi + PanX), Zoom * (Yi + PanY)); return new PointF(Zoom * (Xi + PanX), -Zoom * (Yi + PanY)); } PointF GetImageCoordinates(float Xs, float Ys) { // return new PointF(Xs / Zoom - PanX, (Ys) / Zoom - PanY); return new PointF(Xs / Zoom - PanX, -Ys / Zoom - PanY); } Point PMouseDown; Boolean BMouseDown = false; private void Form1_MouseDown(object sender, MouseEventArgs e) { PMouseDown = new Point(e.X, e.Y); BMouseDown = true; PointF orig = GetImageCoordinates(e.X, e.Y); Console.WriteLine("orig x " + orig.X + " orig y " + orig.Y); Console.WriteLine(""); } private void Form1_MouseMove(object sender, MouseEventArgs e) { if (BMouseDown) { PanX += (e.X - PMouseDown.X) / Zoom; PanY -= (e.Y - PMouseDown.Y) / Zoom; PMouseDown = new Point(e.X, e.Y); this.Invalidate(); } } private void Form1_MouseUp(object sender, MouseEventArgs e) { BMouseDown = false; } internal void UpdateRadar(Dictionary> radarPlots) { RadarPlots = radarPlots; this.Invalidate(); } #region Sectors Sector[] Sectors; private void LoadSectorsFile(string fileName) { XmlSerializer serializer = new XmlSerializer(typeof(sectors)); TextReader reader = new StreamReader(fileName); if (reader != null) { sectors sectorsXml = (sectors)serializer.Deserialize(reader); reader.Close(); // Sectors = sectorsXml.GetPointList(); Sectors = new Sector[sectorsXml.Items.Length]; int indexSec = 0; foreach (sectorsSector s in sectorsXml.Items) { Sectors[indexSec++] = new Sector(s); } } } #endregion } }