/// <summary> /// Initializes a new instance of the <see cref="SectionDefinition"/> class. /// </summary> /// <param name="element">The XML-element to read the data from.</param> public SectionDefinition(XElement element) : this() { this.SectionString.String = element.TryGetAttributeValue("Text", null); // Parse the aspects... foreach (XElement aspectE in element.Elements("Aspect")) { SectionParserDefinition spDefinition = new SectionParserDefinition(aspectE); if (string.IsNullOrWhiteSpace(spDefinition.Type)) { // TODO: Log warning continue; } this.Parsers.Add(spDefinition); } // Parse the areas... foreach (XElement areaE in element.Elements("Area")) { AreaDefinition areaDefinition = new AreaDefinition(areaE); if (!areaDefinition.IsValidDefinition()) { // TODO: Log warning continue; } this.Areas.Add(areaDefinition); } }
private void WriteValueToCustomData(string value, AreaDefinition area, PropertyInfo piCustomData, Operation operation) { // We know that the CustomData-object is a dictionary with string, object... IDictionary <string, object> customData = (IDictionary <string, object>)piCustomData.GetValue(operation, null); // If the custom data entry already exists, give a warning and overwrite it if (customData.ContainsKey(area.MapToPropertyName)) { Logger.Instance.LogFormat(LogType.Warning, this, "The custom data entry '{0}' does already exist and is being overwritten. Please check your control file if this is the intended behavior!", area.MapToPropertyName); } customData[area.MapToPropertyName] = value; }
Operation IParser.Parse(string[] lines) { Operation operation = new Operation(); lines = Utilities.Trim(lines); // Remember the name of the section we are in SectionDefinition currentSection = null; for (int i = 0; i < lines.Length; i++) { try { string line = lines[i]; if (line.Length == 0) { continue; } // Check if the line denotes a section start. In this case, skip this line but remember the section. SectionDefinition newSection = null; if (IsLineSectionMarker(line, out newSection)) { currentSection = newSection; continue; } // Check if we currently are in no section (this is the case when there is some jabber at the beginning we don't care about). if (currentSection == null) { continue; } AreaDefinition area = null; if (!IsLineAreaRelevant(line, currentSection, out area)) { continue; } AreaLineInformation value = new AreaLineInformation(area, line); WriteValueToOperation(value.Value, area, operation); } catch (Exception ex) { Logger.Instance.LogFormat(LogType.Warning, this, "Error while parsing line '{0}'. The error message was: {1}", i, ex.Message); } } return(operation); }
private void WriteValueToOperation(string value, AreaDefinition area, Operation operation) { // First check: Check if the object has the property we want right away PropertyInfo piLocal = operation.GetType().GetProperty(area.MapToPropertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); if (piLocal != null) { WriteValueToProperty(value, piLocal, operation); } else { PropertyInfo piCustomData = operation.GetType().GetProperty("CustomData"); WriteValueToCustomData(value, area, piCustomData, operation); } }
private bool IsLineAreaRelevant(string line, SectionDefinition currentSection, out AreaDefinition area) { area = currentSection.GetArea(line); return(area != null); }
public void LoadAreaDefinitions(Areas areas, float progressMin, float progressMax) { string areasName = Path.GetFileNameWithoutExtension(areas.Name); Parent.name += " - " + areasName; for (int i = 0; i < areas.AreaDefinitions.Count; i++) { AreaDefinition areaDefinition = areas.AreaDefinitions[i]; AreaObject instance = null; switch (areaDefinition.Shape) { case "sphere": instance = GameObject.CreatePrimitive(PrimitiveType.Sphere).AddComponent <AreaObject>(); TransformData correctedSphereTransform = MathUtils.ConvertTransform(areaDefinition.Pos1, Vector4.zero, new Vector4(areaDefinition.Radius, areaDefinition.Radius, areaDefinition.Radius), false, TransformMode.Area); instance.transform.position = correctedSphereTransform.Position; instance.transform.rotation = Quaternion.Euler(correctedSphereTransform.Rotation); instance.transform.localScale = correctedSphereTransform.Scale; instance.Radius = areaDefinition.Radius; break; case "box": instance = GameObject.CreatePrimitive(PrimitiveType.Cube).AddComponent <AreaObject>(); Vector3 pos1 = areaDefinition.Pos1; Vector3 pos2 = areaDefinition.Pos2; Vector3 fScale = pos2 - pos1; fScale.x = Mathf.Abs(fScale.x); fScale.y = Mathf.Abs(fScale.y); fScale.z = Mathf.Abs(fScale.z); Vector3 fPos = (pos1 + pos2) * 0.5f; Vector4 fRot = areaDefinition.Rot; TransformData correctedBoxMatrix = MathUtils.ConvertTransform(fPos, fRot, fScale, true, TransformMode.Area); instance.transform.position = correctedBoxMatrix.Position; instance.transform.rotation = Quaternion.Euler(correctedBoxMatrix.Rotation); instance.transform.localScale = correctedBoxMatrix.Scale; instance.Pos2 = areaDefinition.Pos2; instance.Rot = areaDefinition.Rot; break; } instance.name = areaDefinition.Name; int layer = LayerMask.NameToLayer("ForgelightAreas"); instance.gameObject.layer = layer; foreach (Transform child in instance.transform) { child.gameObject.layer = layer; } instance.ID = areaDefinition.ID; instance.Name = areaDefinition.Name; instance.Shape = areaDefinition.Shape; instance.Pos1 = areaDefinition.Pos1; instance.transform.SetParent(Parent.transform, true); //We color the area definition based on its parameter type. Renderer renderer = instance.GetComponent <Renderer>(); renderer.shadowCastingMode = ShadowCastingMode.Off; renderer.receiveShadows = false; Color color = new Color(1.0f, 1.0f, 1.0f, 0.5f); if (areaDefinition.Properties != null && areaDefinition.Properties.Count > 0) { instance.Properties = new List <string>(); foreach (Property property in areaDefinition.Properties) { instance.Properties.Add(property.Type + "_" + property.ID + ": " + property.Parameters.Attributes()); } MD5 md5 = MD5.Create(); byte[] hash = md5.ComputeHash(Encoding.UTF8.GetBytes(areaDefinition.Properties[0].Type)); color.r = hash[0] / 255.0f; color.g = hash[1] / 255.0f; color.b = hash[2] / 255.0f; } if (cachedMaterials.ContainsKey(color)) { renderer.sharedMaterial = cachedMaterials[color]; } else { renderer.sharedMaterial = new Material(Shader.Find("Custom/Areas")); renderer.sharedMaterial.color = color; cachedMaterials[color] = renderer.sharedMaterial; } EditorUtility.DisplayProgressBar("Loading " + areasName, "Loading Area Definition: " + areaDefinition.Name, MathUtils.Remap01((float)i / areas.AreaDefinitions.Count, progressMin, progressMax)); } }
public AreaC(AreaDefinition definition) : base(definition) { }
public static AreaDefinitionList XmlToAreaDefinitionsList(string body) { var areas = new AreaDefinitionList(); var doc = XDocument.Parse(body); // Handle all ELLIPSES var ellipses = from node in doc.Descendants(NS + "ellipse") select new { object_type = node.Attribute(MOVENS + "object-type").Value, name = (node.Attribute("title") == null) ? null : node.Attribute("title").Value, active = (node.Attribute(MOVENS + "active") == null) ? null : node.Attribute(MOVENS + "active").Value, id = (node.Attribute(MOVENS + "id") == null) ? null : node.Attribute(MOVENS + "id").Value, fill = node.Attribute("fill").Value, used = ((node.Attribute(MOVENS + "used") != null) ? (node.Attribute(MOVENS + "used").Value.ToLower() == "true") : false), }; foreach (var ellipse in ellipses) { if (ellipse.object_type.ToUpper() != AREA) { continue; } var area = new AreaDefinition(); area.Id = Convert.ToInt32(ellipse.id); area.Name = ellipse.name; area.Active = (ellipse.active.ToLower() == "true"); area.Used = ellipse.used; area.Color = ellipse.fill; areas.AreaDefinitions.Add(area); } // Handle all CIRCLES var circles = from node in doc.Descendants(NS + "circle") select new { object_type = node.Attribute(MOVENS + "object-type").Value, name = (node.Attribute("title") == null) ? null : node.Attribute("title").Value, active = (node.Attribute(MOVENS + "active") == null) ? null : node.Attribute(MOVENS + "active").Value, id = (node.Attribute(MOVENS + "id") == null) ? null : node.Attribute(MOVENS + "id").Value, fill = node.Attribute("fill").Value, used = ((node.Attribute(MOVENS + "used") != null) ? (node.Attribute(MOVENS + "used").Value.ToLower() == "true") : false), }; foreach (var circle in circles) { if (circle.object_type.ToUpper() != AREA) { continue; } var area = new AreaDefinition(); area.Id = Convert.ToInt32(circle.id); area.Name = circle.name; area.Active = (circle.active.ToLower() == "true"); area.Used = circle.used; area.Color = circle.fill; areas.AreaDefinitions.Add(area); } // Handle all RECTANGLES var retangles = from node in doc.Descendants(NS + "rect") select new { object_type = node.Attribute(MOVENS + "object-type").Value, name = (node.Attribute("title") == null) ? null : node.Attribute("title").Value, active = (node.Attribute(MOVENS + "active") == null) ? null : node.Attribute(MOVENS + "active").Value, id = (node.Attribute(MOVENS + "id") == null) ? null : node.Attribute(MOVENS + "id").Value, fill = node.Attribute("fill").Value, used = ((node.Attribute(MOVENS + "used") != null) ? (node.Attribute(MOVENS + "used").Value.ToLower() == "true") : false), }; foreach (var retangle in retangles) { if (retangle.object_type.ToUpper() != AREA) { continue; } var area = new AreaDefinition(); area.Id = Convert.ToInt32(retangle.id); area.Name = retangle.name; area.Active = (retangle.active.ToLower() == "true"); area.Used = retangle.used; area.Color = retangle.fill; areas.AreaDefinitions.Add(area); } // Handle all PATHS var paths = from node in doc.Descendants(NS + "path") select new { object_type = node.Attribute(MOVENS + "object-type").Value, name = (node.Attribute("title") == null) ? null : node.Attribute("title").Value, active = (node.Attribute(MOVENS + "active") == null) ? null : node.Attribute(MOVENS + "active").Value, id = (node.Attribute(MOVENS + "id") == null) ? null : node.Attribute(MOVENS + "id").Value, fill = node.Attribute("fill").Value, used = ((node.Attribute(MOVENS + "used") != null) ? (node.Attribute(MOVENS + "used").Value.ToLower() == "true") : false), }; foreach (var path in paths) { if (path.object_type.ToUpper() != AREA) { continue; } var area = new AreaDefinition(); area.Id = Convert.ToInt32(path.id); area.Name = path.name; area.Active = (path.active.ToLower() == "true"); area.Used = path.used; area.Color = path.fill; areas.AreaDefinitions.Add(area); } return(areas); // <ellipse move:used="true" title="Desk Front" fill="#00FF19" move:active="true" move:id="2" cx="1113" // cy="842" rx="155" ry="80" move:object-type="area" id="2" transform="rotate(-29.9816 1113 842)"/> }
/// <summary> /// Saves the parser definition to a file. /// </summary> /// <param name="fileName">The file to save the parser definition to.</param> public void Save(string fileName) { Assertions.AssertNotEmpty(fileName, "fileName"); ControlInformation ci = new ControlInformation(); ci.FaxName = this.ParserName; foreach (SectionDefinitionViewModel svm in this.Sections) { SectionDefinition sd = new SectionDefinition(); sd.SectionString = new GenericParserString(svm.Name); foreach (SectionParserDefinitionViewModel spvm in svm.Aspects) { SectionParserDefinition spd = new SectionParserDefinition(); spd.Type = spvm.Type; spvm.Parser.OnSave(spd.Options); sd.Parsers.Add(spd); } foreach (AreaDefinitionViewModel avm in svm.Areas) { AreaDefinition ad = new AreaDefinition(); ad.AreaString = new GenericParserString(avm.Name); ad.MapToPropertyExpression = avm.MapToPropertyExpression; sd.Areas.Add(ad); } ci.Sections.Add(sd); } ci.Save(fileName); }
private void WriteValueToOperation(string value, AreaDefinition area, Operation operation) { // First check: Check if the object has the property we want right away bool propertyFound = WriteValueToProperty(value, area.MapToPropertyExpression, operation); if (propertyFound) { Logger.Instance.LogFormat(LogType.Debug, this, "Wrote value '{0}' successfully to property '{1}'.", value, area.MapToPropertyExpression); } else { PropertyInfo piCustomData = operation.GetType().GetProperty("CustomData"); WriteValueToCustomData(value, area, piCustomData, operation); } }
private void WriteValueToCustomData(string value, AreaDefinition area, PropertyInfo piCustomData, Operation operation) { // We know that the CustomData-object is a dictionary with string, object... IDictionary<string, object> customData = (IDictionary<string, object>)piCustomData.GetValue(operation, null); // If the custom data entry already exists, give a warning and overwrite it if (customData.ContainsKey(area.MapToPropertyExpression)) { Logger.Instance.LogFormat(LogType.Warning, this, "The custom data entry '{0}' does already exist and is being overwritten. Please check your control file if this is the intended behavior!", area.MapToPropertyExpression); } customData[area.MapToPropertyExpression] = value; Logger.Instance.LogFormat(LogType.Debug, this, "Wrote value '{0}' successfully to custom data entry '{1}'.", value, area.MapToPropertyExpression); }
private bool IsLineAreaRelevant(string line, SectionDefinition currentSection, out AreaDefinition area) { area = currentSection.GetArea(line); return area != null; }