Subversion Repositories amilis

Rev

Blame | Last modification | View Log | RSS feed

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
    }
}