summaryrefslogtreecommitdiff
path: root/SimpleRadar_old/UserControlRadarView.cs
blob: beb4131df89b9a16aa840ee90a14862d45834954 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
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<string, List<AircraftPosition>> RadarPlots;

        List<PointF[]> 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<string, List<AircraftPosition>> 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

    }
}