예제 #1
0
    public void Export(string fileName)
    {
        ExtraData extraData = GetComponent <ExtraData>();

        if (extraData == null)
        {
            return;
        }
        var  position = transform.position;
        Body body     = new Body(extraData, name, position.x, position.y, transform.eulerAngles.z);

        Collider2D[] colliders = GetComponents <Collider2D>();
        foreach (Collider2D collider in colliders)
        {
            if (collider is PolygonCollider2D polyCol)
            {
                Polygon polygon = new Polygon();
                foreach (Vector2 p in polyCol.points)
                {
                    var localScale = transform.localScale;
                    polygon.AddNode(p.x * localScale.x, p.y * localScale.y);
                }
                body.AddShape(polygon);
            }
            else if (collider is CircleCollider2D circleCol)
            {
                var    localScale = transform.localScale;
                var    offset     = circleCol.offset;
                Circle circle     = new Circle(offset.x * localScale.x, offset.y * localScale.y, circleCol.radius);
                body.AddShape(circle);
            }
            else if (collider is EdgeCollider2D chainCol)
            {
                Chain chain = new Chain();
                foreach (Vector2 p in chainCol.points)
                {
                    var localScale = transform.localScale;
                    chain.AddNode(p.x * localScale.x, p.y * localScale.y);
                }
                body.AddShape(chain);
            }
        }

        string bodyData = JsonConvert.SerializeObject(body);

        StreamWriter streamWriter = new StreamWriter("Assets/Maps/" + fileName + ".txt");

        streamWriter.Write(bodyData);
        streamWriter.Close();

        #if UNITY_EDITOR
        AssetDatabase.ImportAsset("Assets/Maps/" + fileName + ".txt");
        #endif
    }
예제 #2
0
    void ObjectHandler(GameObject o)
    {
        ExtraData extraData = o.GetComponent <ExtraData>();

        if (extraData == null)
        {
            return;
        }
        var  position = o.transform.position;
        Body body     = new Body(extraData, o.name, position.x, position.y, o.transform.eulerAngles.z);

        Collider2D[] colliders = o.GetComponents <Collider2D>();
        foreach (Collider2D collider in colliders)
        {
            if (collider is PolygonCollider2D polyCol)
            {
                Polygon polygon = new Polygon();
                foreach (Vector2 p in polyCol.points)
                {
                    var localScale = o.transform.localScale;
                    polygon.AddNode(p.x * localScale.x, p.y * localScale.y);
                }
                body.AddShape(polygon);
            }
            else if (collider is CircleCollider2D circleCol)
            {
                var    localScale = o.transform.localScale;
                var    offset     = circleCol.offset;
                Circle circle     = new Circle(offset.x * localScale.x, offset.y * localScale.y, circleCol.radius);
                body.AddShape(circle);
            }
            else if (collider is EdgeCollider2D chainCol)
            {
                Chain chain = new Chain();
                foreach (Vector2 p in chainCol.points)
                {
                    var localScale = o.transform.localScale;
                    chain.AddNode(p.x * localScale.x, p.y * localScale.y);
                }
                body.AddShape(chain);
            }
        }

        _map.AddBody(body);
    }
예제 #3
0
파일: Parser.cs 프로젝트: nikoir/GIS
        public static Layer Parse(string FileName)
        {
            Layer lr = new Layer();
            StreamReader sr = new StreamReader((System.IO.Stream)File.OpenRead(FileName), System.Text.Encoding.Default);
            string str = null;
            str = sr.ReadLine();
            while (str.IndexOf("data", StringComparison.CurrentCultureIgnoreCase) == -1)
                str = sr.ReadLine();
            sr.ReadLine();
            while (!sr.EndOfStream)
            {
                str = sr.ReadLine();
                if (str.Contains("Point"))
                {
                    string[] Coords = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    str = sr.ReadLine();
                    Point p;
                    if (str.Contains("Symbol"))
                    {
                        string[] Symbol = str.Split(new char[] { ' ', ',', '(', ')' }, StringSplitOptions.RemoveEmptyEntries);
                        p = new Point(Convert.ToDouble(Coords[1]), Convert.ToDouble(Coords[2]), Convert.ToChar(Convert.ToInt32(Symbol[1]) + 1));
                        Color color = IntToColor(Convert.ToInt32(Symbol[2]));
                        p.SolidBrush = new SolidBrush(color);
                        p.Font = new Font("MapInfo Symbols", Convert.ToInt32(Symbol[3]));
                    }
                    else
                    {
                        p = new Point(Convert.ToDouble(Coords[1]), Convert.ToDouble(Coords[2]), '*');
                        p.SolidBrush = new SolidBrush(Color.Black);
                        p.Font = new Font("MapInfo Symbols", 18);
                    }
                    p.Visibility = true;
                    lr.AddMapObject(p);
                }
                else
                    if (str.Contains("Line"))
                    {
                        string[] Coords = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        str = sr.ReadLine();
                        Line l = new Line(Convert.ToDouble(Coords[1]), Convert.ToDouble(Coords[2]), Convert.ToDouble(Coords[3]), Convert.ToDouble(Coords[1]));
                        if (str.Contains("Pen"))
                        {
                            string[] Pen = str.Split(new char[] { ' ', ',', '(', ')' }, StringSplitOptions.RemoveEmptyEntries);
                            int Width = Convert.ToInt32(Pen[1]);
                            l.Visibility = true;
                            Color color = IntToColor(Convert.ToInt32(Pen[3]));
                            l.Pen = new Pen(color, Width);
                            switch (Pen[2])
                            {
                                case "1":
                                    l.Pen.Color = Color.FromArgb(0);
                                    break;
                                case "2":
                                    l.Pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
                                    break;
                                case "3":
                                    l.Pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
                                    break;
                                case "4":
                                    l.Pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
                                    break;
                                case "5":
                                    l.Pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
                                    break;
                                default:
                                    l.Pen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
                                    break;
                            }
                        }
                        else
                        {
                            l.Pen = new Pen(Color.Black, 5);
                            l.Visibility = true;
                        }
                        lr.AddMapObject(l);
                    }
                    else
                        if (str.Contains("Pline"))
                        {
                            string[] Pline = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                            string[] Coords;
                            List<Polyline> list = new List<Polyline>();
                            Polyline pl = null;
                            Pen p = null;
                            int numsections;
                            if (Pline.Length == 3)
                                numsections = Convert.ToInt32(Pline[2]); //Количество секций мультиполилинии
                            else
                                numsections = 1;
                            int counter = 0;
                            int numpts; //Количество точек в текущей секции
                            string[] ar;
                            while (counter < numsections)
                            {
                                if (Pline.Length == 2)
                                    numpts = Convert.ToInt32(Pline[1]);
                                else
                                {
                                    str = sr.ReadLine();
                                    ar = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                                    numpts = Convert.ToInt32(ar[0]);
                                }
                                counter++;
                                pl = new Polyline();
                                pl.Visibility = true;
                                for (int i = 0; i < numpts; i++)
                                {
                                    str = sr.ReadLine();
                                    Coords = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                                    pl.AddNode(Convert.ToDouble(Coords[0]), Convert.ToDouble(Coords[1]));
                                }
                                list.Add(pl);
                            }
                            str = sr.ReadLine();
                            if (str.Contains("Pen"))
                            {
                                string[] Pen = str.Split(new char[] { ' ', ',', '(', ')' }, StringSplitOptions.RemoveEmptyEntries);
                                int Width = Convert.ToInt32(Pen[1]);
                                Color color = IntToColor(Convert.ToInt32(Pen[3]));
                                p = new Pen(color, Width);
                                switch (Pen[2])
                                {
                                    case "1":
                                        p.Color = Color.FromArgb(0);
                                        break;
                                    case "2":
                                        p.DashStyle= System.Drawing.Drawing2D.DashStyle.Solid;
                                        break;
                                    case "3":
                                        p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
                                        break;
                                    case "4":
                                        p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
                                        break;
                                    case "5":
                                        p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
                                        break;
                                    default:
                                        p.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
                                        break;
                                }
                            }
                            else
                            {
                                p = new Pen(Color.Black, 5);
                            }
                            foreach (Polyline polyline in list)
                            {
                                polyline.Pen = p;
                                lr.AddMapObject(polyline);
                            }
                        }
                        else
                            if (str.Contains("Region"))
                            {
                                Polygon pg = null;
                                System.Drawing.Brush br = null;
                                string[] Region = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                                string[] Coords;
                                string[] Brush;
                                Color ForegroundColor;
                                Color BackgroundColor;
                                Pen p = null;
                                List<Polygon> list = new List<Polygon>();
                                int numsections = Convert.ToInt32(Region[1]); //Количество секций мультиполигона
                                int counter = 0;
                                string[] ar;
                                int numpts; //Количество вершин полигона в текущей секции
                                while (counter < numsections)
                                {
                                    str = sr.ReadLine();
                                    ar = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                                    numpts = Convert.ToInt32(ar[0]);
                                    pg = new Polygon();
                                    pg.Visibility = true;
                                    for (int i = 0; i < numpts; i++)
                                    {
                                        str = sr.ReadLine();
                                        Coords = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                                        pg.AddNode(Convert.ToDouble(Coords[0]), Convert.ToDouble(Coords[1]));

                                    }
                                    counter++;
                                    list.Add(pg);
                                }
                                str = sr.ReadLine();
                                if (str.Contains("Pen"))
                                {
                                    string[] Pen = str.Split(new char[] { ' ', ',', '(', ')' }, StringSplitOptions.RemoveEmptyEntries);
                                    int Width = Convert.ToInt32(Pen[1]);
                                    Color color = IntToColor(Convert.ToInt32(Pen[3]));
                                    p = new Pen(color, Width);
                                    switch (Pen[2])
                                    {
                                        case "1":
                                            p.Color = Color.FromArgb(0);
                                            break;
                                        case "2":
                                            p.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
                                            break;
                                        case "3":
                                            p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
                                            break;
                                        case "4":
                                            p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
                                            break;
                                        case "5":
                                            p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
                                            break;
                                        default:
                                            p.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
                                            break;
                                    }
                                    str = sr.ReadLine();
                                }
                                else
                                    p = new Pen(Color.Black, 5);
                                if (str.Contains("Brush"))
                                {
                                    Brush = str.Split(new char[] { ' ', '(', ')', ',' }, StringSplitOptions.RemoveEmptyEntries);
                                    ForegroundColor = IntToColor(Convert.ToInt32(Brush[2]));
                                    if (Brush.Length == 4)
                                        BackgroundColor = IntToColor(Convert.ToInt32(Brush[3]));
                                    else
                                        BackgroundColor = Color.White;
                                    switch (Brush[1])
                                    {
                                        case "1":
                                            br = new System.Drawing.SolidBrush(Color.FromArgb(0));
                                            break;
                                        case "2":
                                            br = new System.Drawing.SolidBrush(ForegroundColor);
                                            break;
                                        case "3":
                                            br = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.Horizontal, ForegroundColor, BackgroundColor);
                                            break;
                                        case "4":
                                            br = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.Vertical, ForegroundColor, BackgroundColor);
                                            break;
                                        case "5":
                                            br = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.BackwardDiagonal, ForegroundColor, BackgroundColor);
                                            break;
                                        case "6":
                                            br = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.ForwardDiagonal, ForegroundColor, BackgroundColor);
                                            break;
                                        default:
                                            br = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.LargeGrid, ForegroundColor, BackgroundColor);
                                            break;
                                    }
                                }
                                else
                                    br = new System.Drawing.SolidBrush(Color.Green);
                                foreach (Polygon polygon in list)
                                {
                                    polygon.Brush = br;
                                    polygon.Pen = p;
                                    lr.AddMapObject(polygon);
                                }

                            }
                            else
                                if (str.Contains("Text"))
                                {
                                    string[] Text = str.Split(new char[] { ' ', '"' }, StringSplitOptions.RemoveEmptyEntries);
                                    string[] Font;
                                    string text;
                                    string[] Coords;
                                    Text txt;
                                    SolidBrush sb;
                                    Font font;
                                    if (Text.Length == 2)
                                        text = Text[1];
                                    else
                                    {
                                        str = sr.ReadLine();
                                        Text = str.Split(new char[] { '"' }, StringSplitOptions.RemoveEmptyEntries);
                                        text = Text[0];
                                    }
                                    str = sr.ReadLine();
                                    Coords = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                                    str = sr.ReadLine();
                                    if (str.IndexOf("font", StringComparison.CurrentCultureIgnoreCase) != -1)
                                    {
                                        FontStyle fs = FontStyle.Regular;
                                        Font = str.Split(new char[] { ' ', '"', ',', ')', '(', ']', '[' }, StringSplitOptions.RemoveEmptyEntries);
                                        sb = new SolidBrush(IntToColor(Convert.ToInt32(Font[4])));
                                        switch (Font[2])
                                        {
                                            case "1":
                                                fs = FontStyle.Bold;
                                                break;
                                            case "2":
                                                fs = FontStyle.Italic;
                                                break;
                                            case "3":
                                                fs = FontStyle.Underline;
                                                break;
                                        }
                                        font = new Font(Font[1], Convert.ToInt32(Font[3]), fs);
                                    }
                                    else
                                    {
                                        font = new Font("TimesNewRoman", 14);
                                        sb = new SolidBrush(Color.Black);
                                    }
                                    txt = new Text(text, font, Convert.ToDouble(Coords[0]), Convert.ToDouble(Coords[1]));
                                    txt.SolidBrush = sb;
                                    txt.Visibility = true;
                                    lr.AddMapObject(txt);
                                }
            }
            lr.MapObjects.Sort(delegate(MapObject mo1, MapObject mo2)
            { return mo1.Priority.CompareTo(mo2.Priority); });
            return lr;
        }
예제 #4
0
        private void AddRegion(StreamReader sr, ref string tmp, ref string[] line)
        {
            // Количество регионов
            tmp = tmp.Replace(" ", string.Empty).Replace(line[0], string.Empty);
            var regionsNumb = Convert.ToInt32(tmp);
            // Вершины полигонов
            var polygonsList = new List <Polygon>();

            for (int i = 0; i < regionsNumb; ++i)
            {
                line = sr.ReadLine()?.Trim().Split(' ');
                int pointsNumb = Convert.ToInt32(line?[0]);
                var polygon    = new Polygon();
                for (int j = 0; j < pointsNumb; ++j)
                {
                    line = sr.ReadLine()?.Split(' ');
                    if (line == null)
                    {
                        continue;
                    }

                    double x = double.Parse(line[0], CultureInfo.InvariantCulture);
                    double y = double.Parse(line[1], CultureInfo.InvariantCulture);
                    polygon.AddNode(x, y);
                }

                // Добавление полигонов во временный список
                polygonsList.Add(polygon);
            }

            // Pen
            if (!sr.EndOfStream)
            {
                tmp = sr.ReadLine()?.Trim();
            }

            line = tmp?.Split(' ');
            var style = new Style
            {
                Pen   = new Pen(Color.Black),
                Brush = new SolidBrush(Color.Black)
            };
            var hasOwnStyle = false;

            if (line?[0] == "Pen")
            {
                tmp  = tmp?.Replace(" ", string.Empty).Replace(line[0], string.Empty);
                line = tmp?.Split(',');

                style.Pen   = GetPen(line?[0].Substring(1, line[0].Length - 1), line?[1], line?[2].Substring(0, line[2].Length - 1));
                hasOwnStyle = true;

                if (!sr.EndOfStream)
                {
                    tmp = sr.ReadLine()?.Trim();
                }
            }

            line = tmp?.Split(' ');
            if (line?[0] == "Brush")
            {
                tmp  = tmp?.Replace(" ", string.Empty).Replace(line[0], string.Empty);
                line = tmp?.Split(',');

                if (line != null && line.Length == 3)
                {
                    style.Brush = GetBrush(line[0].Substring(1, line[0].Length - 1), line[1], line[2].Substring(0, line[2].Length - 1));
                }

                if (line != null && line.Length == 2)
                {
                    style.Brush = GetBrush(line[0].Substring(1, line[0].Length - 1), line[1].Substring(0, line[1].Length - 1));
                }

                hasOwnStyle = true;
                if (!sr.EndOfStream)
                {
                    tmp = sr.ReadLine()?.Trim();
                }
            }

            // Добавление полигонов в основной список и задание стилей
            foreach (var polygon in polygonsList)
            {
                if (hasOwnStyle)
                {
                    polygon.Style = style;
                }

                Data.Add(polygon);
            }
        }