/// <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); }
/// <summary> /// Repair the docking connection of this port. /// </summary> public void Repair() { // Two docks if (DockType == DockTypes.Dock) { if (DockedPart == null) { if (Parent is KmlVessel) { KmlVessel vessel = (KmlVessel)Parent; int myIndex = vessel.Parts.IndexOf(this); for (int i = 0; i < vessel.Parts.Count; i++) { KmlPart part = vessel.Parts[i]; if (part is KmlPartDock) { KmlPartDock otherDock = (KmlPartDock)part; if (otherDock.DockedPart == this) { // This will choose the right docker / dockee or switch to same vessel if none is parent RepairDockerChoose(this, otherDock); return; } else if (otherDock.ParentPart == this || ParentPart == otherDock) { if (AttachedToSurfaceIndex == i || AttachedToNodeIndices.Contains(i) || otherDock.AttachedToSurfaceIndex == myIndex || otherDock.AttachedToNodeIndices.Contains(myIndex)) { // It's attached and not docked continue; } else { RepairDockerChoose(this, otherDock); return; } } } } // To avoid getting to here there are returns spread above // Syntax.Warning(this, "Didn't find another part in all parts of the vessel to fix this dock with"); RepairClearDocking(this); } else { Syntax.Warning(this, "Could not search for connected parts, parent vessel is not valid"); } } else if (!(DockedPart is KmlPartDock)) { Syntax.Warning(this, "Don't know how to repair this docking connection, this one is no dock: " + DockedPart); } else { KmlPartDock otherDock = (KmlPartDock)DockedPart; if (otherDock.DockedPart == this || otherDock.ParentPart == this || ParentPart == otherDock) { // They are already linked but there's something to fix unless context menu to this wouldn't be enabled // This will choose the right docker / dockee or switch to same vessel if none is parent RepairDockerChoose(this, otherDock); } else { // Semi-functional same vessel docking would be fixed above, this is no docking RepairClearDocking(this); } } } // One dock and another part else if (DockType == DockTypes.Grapple) { if (DockedPart == null) { if (Parent is KmlVessel) { KmlVessel vessel = (KmlVessel)Parent; int myIndex = vessel.Parts.IndexOf(this); for (int i = 0; i < vessel.Parts.Count; i++) { KmlPart part = vessel.Parts[i]; if (part.ParentPart == this || this.ParentPart == part) { if (AttachedToSurfaceIndex == i || AttachedToNodeIndices.Contains(i) || part.AttachedToSurfaceIndex == myIndex || part.AttachedToNodeIndices.Contains(myIndex)) { // It's attached and not grappled continue; } else { RepairGrappling(this, part); return; } } } // To avoid getting to here there are returns spread above // Syntax.Warning(this, "Didn't find another part in all parts of the vessel to fix this grappling device with"); RepairClearDocking(this); } else { Syntax.Warning(this, "Could not search for connected parts, parent vessel is not valid"); } } else { if (DockedPart.ParentPart == this || ParentPart == DockedPart) { RepairGrappling(this, DockedPart); } else { RepairClearDocking(this); } } } }