summaryrefslogtreecommitdiff
path: root/Data/Route.cs
blob: 6a8cbf64dcdadbfaa4cd0e9efe807aa668d1e9cf (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Xml.Serialization;
using System.IO;

namespace Data
{
    public class Route
    {
        #region Attributes

        public static float Ratio = Config.coordinatesRatio;

        private string _b1;
        private string _b2;

        private AppDatabase app;

        #endregion

        #region Constructor

        public Route(routesRoute route, AppDatabase app1)
        {
            this._b1 = route.b1;
            this._b2 = route.b2;
            this.app = app1;
        }

        #endregion

        #region Methods

        public void Draw(Graphics g)
        {
            Beacon startBeacon = getCorrespondingBeacon(this.B1);
            Beacon endBeacon = getCorrespondingBeacon(this.B2);

            if ((startBeacon != null) && (endBeacon != null))
            {
                PointF locationPointB1 = startBeacon.toPointF();
                PointF locationPointB2 = endBeacon.toPointF();
                g.DrawLine(Config.drawRoutesPen, (float)PanAndZoomX(locationPointB1.X / Ratio), (float)PanAndZoomY(locationPointB1.Y / Ratio), (float)PanAndZoomX(locationPointB2.X / Ratio), (float)PanAndZoomY(locationPointB2.Y / Ratio));
            }
        }

        public Beacon getCorrespondingBeacon(string bCode)
        {
            Beacon correspondingBeacon = null;
            foreach (Beacon b in app.getBeacons())
            {
                if (b.Code == bCode)
                {
                    correspondingBeacon = b;
                    break;
                }
            }
            return correspondingBeacon;
        }

        #endregion

        #region Getters and Setters

        public string B1
        {
            get { return _b1; }
            set { _b1 = value; }
        }

        public string B2
        {
            get { return _b2; }
            set { _b2 = value; }
        }

        #endregion

        #region Delegate Pan And Zoom

        public delegate double PanAndZoom(double input);

        [NonSerialized]
        static public PanAndZoom PanAndZoomX;
        [NonSerialized]
        static public PanAndZoom PanAndZoomY;

        #endregion

        #region createList
        public static Route[] LoadRoutesFile(string fileName, AppDatabase app)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(routes));
            TextReader reader = new StreamReader(fileName);

            Route[] Routes = null;
            if (reader != null)
            {
                routes routesXml = (routes)serializer.Deserialize(reader);
                reader.Close();
                Routes = new Route[routesXml.Items.Length];
                int indexSec = 0;
                foreach (routesRoute r in routesXml.Items)
                {
                    Routes[indexSec++] = new Route(r, app);
                    //Console.WriteLine("Route : " + r.b1 + " / " + r.b2);
                }
                //Console.WriteLine("Routes : " + routesXml.Items.Length);
            }
            return Routes;
        }
        #endregion
    }
}