/// <summary> /// Deserialize given XML list of vertices of a tile. /// </summary> /// <param name="verticesElement">XML element list of vertices.</param> /// <returns>List of deserialized vertices.</returns> /// <exception cref="MissingXmlAttribute">If some attribute of protein is missing.</exception> /// <exception cref="MissingXmlElement">If some element of protein is missing.</exception> private NamedVertices DeserializeVertices(XElement verticesElement) { List <XElement> vertexElements = verticesElement.Elements("vertex").ToList(); if (vertexElements.Count == 0) { throw new MissingXmlElement("Element vertex of vertices of tile is missing or empty."); } List <NamedPosition> vertices = new List <NamedPosition>(); foreach (XElement vertexElement in vertexElements) { string name = Xmlizer.GetAttributeValueWithException(vertexElement, "vertex", "name"); Point3D position = DeserializePosition(vertexElement); if (position.Z != 0) { throw new InvalidOperationException("3D tiles with 'Z' coordinate not supported in this version."); } NamedPosition vertex = new NamedPosition(name, position); vertices.Add(vertex); } return(new NamedVertices(vertices)); }
public void decodeCssString(string cssString) { Position = new CssPoint(); if (PositionTable.ContainsKey(cssString)) { ScreenPosition = PositionTable[cssString]; } else { //Dealing with a real position //x% y% The first value is the horizontal position and the second value is the vertical. The top left corner is 0% 0%. The right bottom corner is 100% 100%. If you only specify one value, the other value will be 50%. . Default value is: 0% 0% Play it ยป //xpos ypos The first value is the horizontal position and the second value is the vertical. The top left corner is 0 0. Units can be pixels (0px 0px) or any other CSS units. If you only specify one value, the other value will be 50%. You can mix % and positions char[] splitChars = { ' ' }; string[] positionTokens = cssString.Split(splitChars, StringSplitOptions.RemoveEmptyEntries); Regex regex = new Regex("(?<number>\\d)(?<suffix>.*)", RegexOptions.IgnoreCase); if (positionTokens.Count() == 1) { Position.xValue = CssHelpers.decodeCssUnit(positionTokens[0]); Position.yValue.value = 50.0f; Position.yValue.valueType = CssUnit.ValueType.percentage; } else if (positionTokens.Count() == 2) { Position.xValue = CssHelpers.decodeCssUnit(positionTokens[0]); Position.yValue = CssHelpers.decodeCssUnit(positionTokens[1]); } else { Debug.Write("Missing or malformed position definition: "); Debug.WriteLine(cssString); } } }
/// <summary> /// Deserialize tile from a given XML node. /// </summary> /// <param name="name">Tile name .</param> /// <param name="tileElement">XML element of tile.</param> /// <exception cref="MissingXmlAttribute">If some attribute of a tile is missing.</exception> /// <exception cref="MissingXmlElement">If some element of tile is missing.</exception> /// <returns>Deserialized tile.</returns> private Tile DeserializeTile(string name, XElement tileElement) { NamedVertices vertices; XElement verticesElement = tileElement.Element("vertices"); if (verticesElement != null) { vertices = DeserializeVertices(verticesElement); } else { XElement polygonElement = tileElement.Element("polygon"); if (polygonElement == null) { throw new MissingXmlElement("Element 'vertices' or 'polygon' are missing. One of them is required."); } vertices = DeserializePolygon(polygonElement); } XElement positionsElement = tileElement.Element("positions"); List <NamedPosition> namedPositions = new List <NamedPosition>(); if (positionsElement != null) { List <XElement> positionElements = positionsElement.Elements("position").ToList(); if (positionElements.Any()) { namedPositions = DeserializeNamedPositions(positionElements); } } Angle defaultAngle = GetAngle(tileElement.Element("connectingAngle"), default(Angle)); List <Connector> connectors = new List <Connector>(); List <XElement> connectorElements = tileElement.GetElements("connectors/connector"); foreach (XElement connectorElement in connectorElements) { string connectorName = Xmlizer.GetAttributeValueWithException(connectorElement, "connector", "name"); List <XElement> connectorsPositionElements = connectorElement.GetElements("positions/position"); List <Point3D> positions = new List <Point3D>(); foreach (XElement connectorsPositionElement in connectorsPositionElements) { string positionName = Xmlizer.GetAttributeValueWithException(connectorsPositionElement, "position", "name"); NamedPosition vertex = vertices.FirstOrDefault(x => x.Name == positionName); if (vertex.Name != null) { positions.Add(vertex.Position); } else { NamedPosition position = namedPositions.Find(x => x.Name == positionName); if (position.Name != null) { positions.Add(position.Position); } else { throw new MissingXmlElement( $"Vertex/Position '{positionName}' is not defined in input xml file for connector '{connectorName}'."); } } } XElement glueElement = connectorElement.Element("glue"); string glueName = Xmlizer.GetAttributeValueWithException(glueElement, "glue", "name"); if (!Glues.ContainsKey(glueName)) { throw new InvalidOperationException($"Glue named '{glueName}' is not defined in input xml file."); } Glue glue = Glues[glueName]; Angle angle = GetAngle(connectorElement.Element("angle"), defaultAngle); double resistance = GetDouble(connectorElement, "resistance", 0); Connector connector = new Connector(connectorName, positions, glue, angle, resistance); connectors.Add(connector); } XElement surfaceGlueElement = tileElement.Element("surfaceGlue"); string surfaceGlueName = Xmlizer.GetAttributeValueWithException(surfaceGlueElement, "surfaceGlue", "name"); if (!Glues.ContainsKey(surfaceGlueName)) { throw new InvalidOperationException($"Surface glue named '{surfaceGlueName}' is not defined in input xml file."); } Color color; int alpha; DeserializeColor(tileElement.Element("color"), out color, out alpha); List <ProteinOnTile> proteinsOnTile = null; if (v_ProteinsOnTile.ContainsKey(name)) { proteinsOnTile = v_ProteinsOnTile[name]; } double alphaRatio = GetDouble(tileElement, "alphaRatio", 1); if (alphaRatio <= 0) { throw new InvalidOperationException($"Alpha resistance ratio of the tile '{name}' must be positive."); } return(new Tile(name, vertices.Select(vertex => vertex.Position).ToList(), connectors, Glues[surfaceGlueName], proteinsOnTile, color, alpha, alphaRatio)); }