public object Parse(IStyleSystem styleSystem, XElement reader, IStylePropertyContext context) { if (reader == null) { return(new Insets()); } var element = reader.ElementLocal(elementName); if (element == null) { return(new Insets()); } var all = (int?)element.ElementLocal("all"); if (all != null) { return(new Insets(all.GetValueOrDefault())); } var top = (int?)element.ElementLocal("top"); var left = (int?)element.ElementLocal("left"); var bottom = (int?)element.ElementLocal("bottom"); var right = (int?)element.ElementLocal("right"); return(new Insets(top.GetValueOrDefault(), left.GetValueOrDefault(), bottom.GetValueOrDefault(), right.GetValueOrDefault())); }
KeyValuePair <IStyleKey, object> ReadProperty(XElement reader, IStylePropertyContext context) { var name = reader.AttributeLocal("name")?.Value; if (string.IsNullOrWhiteSpace(name)) { throw new StyleParseException("A property element must provide the name of a style-key.", reader); } var key = LookupStyleKey(name, reader); object result; if (string.Equals(reader.AttributeLocal("inherit")?.Value, "true", StringComparison.InvariantCulture)) { result = InheritMarker.Inherited; } else { var type = reader.AttributeLocal("type")?.Value; IStylePropertySerializer serializer = null; if (type != null) { typeParsers.TryGetValue(type, out serializer); } if (serializer == null) { serializer = LookupPropertyParser(key.ValueType, reader); } result = serializer.Parse(StyleSystem, reader, context); } return(new KeyValuePair <IStyleKey, object>(key, result)); }
void ReadStyleRule(XElement reader, IStylePropertyContext context, List <IStyleRule> rules, IStyleSelector parent = null, bool directChild = false) { var element = reader.AttributeLocal("element")?.Value; var selector = CreateSelector(reader, parent, directChild, element, context); var style = new PredefinedStyle(StyleSystem); var hasStyle = false; foreach (var propertyNode in reader.Elements().Where(pn => pn.Name.LocalName == "property")) { var p = ReadProperty(propertyNode, context); style.SetValue(p.Key, p.Value); hasStyle = true; } if (hasStyle) { var rule = new StyleRule(selector, style); rules.Add(rule); } foreach (var propertyNode in reader.Elements().Where(pn => pn.Name.LocalName == "style")) { var attr = propertyNode.AttributeLocal("direct-child"); ReadStyleRule(propertyNode, context, rules, selector, attr?.Value == "true"); } }
IStyleSelector CreateSelector(XElement style, IStyleSelector parent, bool directChild, string element, IStylePropertyContext context) { if (string.IsNullOrWhiteSpace(element) || "*".Equals(element)) { element = null; } ISimpleSelector es = new ElementSelector(element); var c = style.ElementLocal("conditions"); if (c != null) { var cond = ParseAndCondition(c, context); es = new ConditionalSelector(es, cond); } if (parent != null) { return(new DescendantSelector(es, parent, directChild)); } return(es); }
ICondition ParseCondition(XElement s, IStylePropertyContext context) { var localName = s.Name.LocalName; if (localName == "or") { return(ParseOrCondition(s, context)); } if (localName == "not") { return(ParseNotCondition(s, context)); } if (localName == "id") { return(new IdCondition(s.Value)); } if (localName == "class") { return(new ClassCondition(s.Value)); } if (localName == "pseudo-class") { return(new PseudoClassCondition(s.Value)); } if (localName == "attribute") { var name = s.ElementLocal("name")?.Value; if (name == null) { throw new StyleParseException("Attribute 'name' is mandatory when declaring an attribute-condition.", s); } var value = s.ElementLocal("value"); if (value == null) { return(new AttributeCondition(name, null)); } var type = s.ElementLocal("type")?.Value; if (type == null) { throw new StyleParseException( "Attribute 'type' is mandatory when declaring an attribute-condition with a value comparison.", s); } IStylePropertySerializer serializer; if (typeParsers.TryGetValue(type, out serializer)) { return(new AttributeCondition(name, serializer.Parse(StyleSystem, value, context))); } } throw new StyleParseException($"Unable to handle condition type {localName}", s); }
ICondition ParseOrCondition(XElement s, IStylePropertyContext context) { ICondition c = null; foreach (var e in s.Elements()) { var x = ParseCondition(e, context); c = c == null ? x : new OrCondition(c, x); } return(c); }
public object Parse(IStyleSystem styleSystem, XElement property, IStylePropertyContext context) { var reader = property.ElementLocal("font"); if (reader == null) { throw new StyleParseException("Expected a valid font element.", property); } var texture = (string)reader.ElementLocal("name"); if (string.IsNullOrWhiteSpace(texture)) { throw new StyleParseException("Font name cannot be empty.", property); } return(styleSystem.ContentLoader.LoadFont(texture)); }
public object Parse(IStyleSystem styleSystem, XElement reader, IStylePropertyContext context) { string value = reader.Value; if (string.IsNullOrEmpty(value)) { throw new StyleParseException($"Missing value for enum {TargetType}", reader); } try { return(Enum.Parse(TargetType, value)); } catch { throw new StyleParseException($"Unable to parse enum value {value} for {TargetType}.", reader); } }
public object Parse(IStyleSystem styleSystem, XElement reader, IStylePropertyContext context) { var textureElement = reader.ElementLocal("texture"); var texture = (string)textureElement?.ElementLocal("name"); if (string.IsNullOrWhiteSpace(texture)) { texture = null; } var contentLoader = styleSystem.ContentLoader; var tp = (string)reader.ElementLocal("texture-packer") ?? "disabled"; if (tp == "auto") { return(context.ProcessTexture(contentLoader.LoadTexture(texture))); } return(contentLoader.LoadTexture(texture)); }
public object Parse(IStyleSystem styleSystem, XElement reader, IStylePropertyContext context) { var textureElement = reader.ElementLocal("texture"); var texture = (string)textureElement?.ElementLocal("name"); if (string.IsNullOrWhiteSpace(texture)) { texture = null; } var insets = (Insets) new InsetsStylePropertySerializer("corners").Parse(styleSystem, textureElement, context); var margins = (Insets) new InsetsStylePropertySerializer("margins").Parse(styleSystem, textureElement, context); var contentLoader = styleSystem.ContentLoader; var tp = (string)reader.ElementLocal("texture-packer") ?? "disabled"; if (tp == "auto") { return(context.ProcessTexture(contentLoader.LoadTexture(texture, insets, margins))); } return(contentLoader.LoadTexture(texture, insets, margins)); }
public object Parse(IStyleSystem styleSystem, XElement reader, IStylePropertyContext context) { var colorAsText = (string)reader; if (string.IsNullOrWhiteSpace(colorAsText)) { throw new StyleParseException("When providing a color, the text cannot be empty.", reader); } Color c; if (knownColors.TryGetValue(colorAsText, out c)) { return(c); } if (colorAsText.StartsWith("#")) { bool?premultipliedFlag = (bool?)reader.AttributeLocal("premultiplied"); return(ParseFromString(colorAsText, premultipliedFlag ?? false)); } throw new StyleParseException($"The color {colorAsText} is neither a known color or a hex-notation color literal.", reader); }
public object Parse(IStyleSystem styleSystem, XElement reader, IStylePropertyContext context) { return((float)reader); }
NotCondition ParseNotCondition(XElement s, IStylePropertyContext context) { return(new NotCondition(ParseAndCondition(s, context))); }