示例#1
0
        private static List <KmlItem> ParseFile(System.IO.StreamReader file, KmlNode parent)
        {
            List <KmlItem> list = new List <KmlItem>();

            string line;

            while ((line = file.ReadLine()) != null)
            {
                KmlItem newItem = ParseLine(line);
                if (newItem is KmlBegin)
                {
                    KmlItem lastItem;
                    int     l = list.Count - 1;
                    if (l < 0)
                    {
                        lastItem = new KmlItem("");
                    }
                    else
                    {
                        lastItem = list[l];
                        list.RemoveAt(l);
                    }
                    KmlNode newNode = new KmlNode(lastItem, parent);
                    if (newNode.Tag.ToLower() == "vessel")
                    {
                        newNode = new KmlVessel(newNode);
                    }
                    else if (newNode.Tag.ToLower() == "kerbal")
                    {
                        newNode = new KmlKerbal(newNode);
                    }
                    else if (newNode.Tag.ToLower() == "part")
                    {
                        newNode = new KmlPart(newNode);
                    }
                    else if (newNode.Tag.ToLower() == "resource")
                    {
                        newNode = new KmlResource(newNode);
                    }
                    list.Add(newNode);
                    newNode.AddRange(ParseFile(file, newNode));
                }
                else if (newItem is KmlEnd)
                {
                    Identify(list);
                    return(list);
                }
                else
                {
                    list.Add(newItem);
                }
            }

            Identify(list);
            return(list);
        }
示例#2
0
文件: KmlItem.cs 项目: fat-lobyte/KML
        private static KmlNode ParseNode(KmlItem item)
        {
            KmlNode newNode = new KmlNode(item);

            if (newNode.Tag.ToLower() == "vessel")
            {
                newNode = new KmlVessel(newNode);
            }
            else if (newNode.Tag.ToLower() == "kerbal")
            {
                newNode = new KmlKerbal(newNode);
            }
            else if (newNode.Tag.ToLower() == "part")
            {
                newNode = new KmlPart(newNode);
            }
            else if (newNode.Tag.ToLower() == "resource")
            {
                newNode = new KmlResource(newNode);
            }
            return(newNode);
        }
示例#3
0
        private void AssignTemplate(bool withImage, bool withText)
        {
            // Fit an Image and a TextBlock into a Stackpanel,
            // have the Stackpanel as node Header

            StackPanel pan = new StackPanel();

            pan.Orientation = Orientation.Horizontal;
            if (withImage)
            {
                if (DataNode is KmlResource)
                {
                    KmlResource res = (KmlResource)DataNode;
                    pan.Children.Add(GenerateProgressBar(res.AmountRatio));
                }
                else if (DataNode is KmlPart)
                {
                    KmlPart part = (KmlPart)DataNode;
                    pan.Children.Add(GenerateImage(DataNode));
                    if (part.WorstResourceRatio >= 0.0)
                    {
                        ProgressBar prog = GenerateProgressBar(part.WorstResourceRatio);
                        prog.Height = 4;
                        prog.Margin = new Thickness(-19, 6, 3, 0);
                        pan.Children.Add(prog);
                    }
                }
                else
                {
                    pan.Children.Add(GenerateImage(DataNode));
                }
            }
            if (withText)
            {
                pan.Children.Add(new TextBlock(new Run(DataNode.ToString())));
            }
            Header = pan;
        }
示例#4
0
        /// <summary>
        /// Adds a child KmlItem to this nodes lists of children, depending of its
        /// derived class KmlNode, KmlAttrib or further derived from these.
        /// When an KmlAttrib "Name" is found, its value
        /// will be used for the corresponding property of this node.
        /// </summary>
        /// <param name="item">The KmlItem to add</param>
        public override void Add(KmlItem item)
        {
            KmlItem newItem = item;

            if (item is KmlAttrib)
            {
                KmlAttrib attrib = (KmlAttrib)item;
                if (attrib.Name.ToLower() == "uid")
                {
                    Uid = attrib.Value;
                    attrib.AttribValueChanged += Uid_Changed;
                    attrib.CanBeDeleted        = false;
                }
                else if (attrib.Name.ToLower() == "parent")
                {
                    int p = ParentPartIndex;
                    if (int.TryParse(attrib.Value, out p))
                    {
                        ParentPartIndex = p;
                    }
                    else
                    {
                        Syntax.Warning(this, "Unreadable parent part: " + attrib.ToString());
                    }
                    attrib.AttribValueChanged += ParentPart_Changed;
                    attrib.CanBeDeleted        = false;
                }
                else if (attrib.Name.ToLower() == "attn")
                {
                    // Value looks like "top, 12", "bottom, -1", "left, 1", "top2, 3", etc.
                    string[] items = attrib.Value.Split(new char[] { ',' });
                    int      index = -1;
                    if (items.Count() == 2 && int.TryParse(items[1], out index))
                    {
                        if (index >= 0)
                        {
                            AttachedToNodeIndices.Add(index);
                            attrib.CanBeDeleted = false;
                        }
                    }
                    else
                    {
                        Syntax.Warning(this, "Bad formatted part node attachment: " + attrib.ToString());
                    }
                    attrib.AttribValueChanged += AttachmentNode_Changed;
                }
                else if (attrib.Name.ToLower() == "srfn")
                {
                    // Value looks like "srfAttach, 12"
                    string[] items = attrib.Value.Split(new char[] { ',' });
                    int      index = -1;
                    if (items.Count() == 2 && int.TryParse(items[1], out index))
                    {
                        if (index >= 0)
                        {
                            if (AttachedToSurfaceIndex < 0)
                            {
                                AttachedToSurfaceIndex = index;
                                attrib.CanBeDeleted    = false;
                            }
                            else
                            {
                                Syntax.Warning(this, "More than one surface attachment is not allowed, already attached to [" + AttachedToSurfaceIndex + "], could not attach to [" + index + "]");
                            }
                        }
                    }
                    else
                    {
                        Syntax.Warning(this, "Bad formatted part surface attachment: " + attrib.ToString());
                    }
                    attrib.AttribValueChanged += AttachmentSurface_Changed;
                }
                else if (attrib.Name.ToLower() == "position")
                {
                    // Value looks like "0.1,0,-0.3E-07"
                    string[] items = attrib.Value.Split(new char[] { ',' });
                    double   x     = 0;
                    double   y     = 0;
                    double   z     = 0;
                    if (items.Count() == 3 &&
                        double.TryParse(items[0], NumberStyles.Number | NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out x) &&
                        double.TryParse(items[1], NumberStyles.Number | NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out y) &&
                        double.TryParse(items[2], NumberStyles.Number | NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out z))
                    {
                        Position            = new Point3D(x, y, z);
                        attrib.CanBeDeleted = false;
                    }
                    else
                    {
                        Syntax.Warning(this, "Bad formatted part position: " + attrib.ToString());
                    }
                }
            }
            else if (item is KmlResource)
            {
                KmlResource res = (KmlResource)item;
                Resources.Add(res);
                ResourceTypes.Add(res.Name);

                // Get notified when Resources change
                res.MaxAmount.AttribValueChanged += Resources_Changed;
                res.MaxAmount.CanBeDeleted        = false;
                res.Amount.AttribValueChanged    += Resources_Changed;
                res.Amount.CanBeDeleted           = false;
            }

            /*if (Item is KmlNode)
             * {
             *  KmlNode node = (KmlNode)Item;
             *  if (node.Tag.ToLower() == "resource")
             *  {
             *      newItem = new KmlResource(node);
             *  }
             * }*/
            base.Add(newItem);
        }