public static void ResolveVariables(IEnumerable <Rule> rules, Dictionary <string, Property> variables, ResolvingOptions options) { foreach (var rule in rules) { foreach (var property in rule.Properties.Values) { ResolveValues(property, variables, options); } } }
static StyleSheet ConvertToStyleSheet(IEnumerable <Rule> rules, List <StyleSheet.ImportStruct> imports, ResolvingOptions options = null) { var sheet = ScriptableObject.CreateInstance <StyleSheet>(); PopulateSheet(sheet, rules, imports, options); return(sheet); }
public static List <Value> ResolveValues(Property property, Dictionary <string, Property> variables, ResolvingOptions options) { for (var i = 0; i < property.Values.Count; ++i) { var value = property.Values[i]; if (!IsVariableValue(value)) { continue; } IEnumerable <Value> variableValues = null; Property variableProperty; if (value.ValueType == StyleValueType.Function) { var functionValue = value as Function; if (variables.TryGetValue(functionValue.args[0][0].AsString(), out variableProperty)) { variableValues = variableProperty.Values; } else if (functionValue.args.Count > 1) { // Default Value: // env(--my-variable, 23 45); // env(--my-variable, "pow"); variableValues = functionValue.args[1]; } else { throw new Exception($"Cannot resolve variable: \"{functionValue.args[0][0].AsString()}\""); } } else { if (variables.TryGetValue(value.AsString(), out variableProperty)) { variableValues = variableProperty.Values; } } if (variableValues != null) { using (var it = variableValues.GetEnumerator()) { if (it.MoveNext()) { property.Values[i] = it.Current; while (it.MoveNext()) { property.Values.Insert(++i, it.Current); } } else if (options != null && options.ThrowIfCannotResolve) { throw new Exception("Cannot resolve variable: " + property.Values[0].AsString()); } } } else if (options != null && options.ThrowIfCannotResolve) { throw new Exception("Cannot resolve variable: " + property.Values[0].AsString()); } } return(property.Values); }
public static StyleSheet ConvertToStyleSheet(IEnumerable <Rule> rules, ResolvingOptions options = null) { return(ConvertToStyleSheet(rules, new List <StyleSheet.ImportStruct>(), options)); }
public static void PopulateSheet(StyleSheet sheet, IEnumerable <Rule> rules, List <StyleSheet.ImportStruct> imports, ResolvingOptions options = null) { options = options ?? new ResolvingOptions(); var helper = new StyleSheetBuilderHelper(); if (options.SortRules) { rules = rules.OrderBy(rule => rule.SelectorName); } foreach (var rule in rules) { helper.BeginRule(string.Empty, rule.LineNumber); StyleSheetBuilderHelper.BuildSelector(rule.Selector, helper); var propertyValues = rule.Properties.Values.ToList(); if (options.SortProperties) { propertyValues.Sort((p1, p2) => p1.Name.CompareTo(p2.Name)); } foreach (var property in propertyValues) { helper.builder.BeginProperty(property.Name); AddValues(helper, property.Values); helper.builder.EndProperty(); } helper.EndRule(); } foreach (var import in imports) { helper.AddImport(import); } helper.PopulateSheet(sheet); }
public static void ResolveVariables(IEnumerable <Rule> rules, Dictionary <string, Property> variables, ResolvingOptions options) { var graph = BuildVariableDependencies(variables); foreach (var varDepNode in graph) { varDepNode.Tranverse((var, depth) => ResolveValues(var, variables, options)); } foreach (var varDepNode in graph) { varDepNode.Tranverse((var, depth) => { if (IsVariableProperty(var)) { Debug.Log("Not resolved variable found " + var.Name); } }); } foreach (var rule in rules) { foreach (var property in rule.Properties.Values) { ResolveValues(property, variables, options); } } }
public static StyleSheet ConvertToStyleSheet(IEnumerable <Rule> rules, Dictionary <string, Property> variables = null, ResolvingOptions options = null) { options = options ?? new ResolvingOptions(); variables = variables ?? new Dictionary <string, Property>(); var helper = new StyleSheetBuilderHelper(); if (options.SortRules) { rules = rules.OrderBy(rule => rule.SelectorName); } foreach (var rule in rules) { helper.BeginRule(); StyleSheetBuilderHelper.BuildSelector(rule.Selector, helper); var propertyValues = rule.Properties.Values.ToList(); if (options.SortProperties) { propertyValues.Sort((p1, p2) => p1.Name.CompareTo(p2.Name)); } foreach (var property in propertyValues) { helper.builder.BeginProperty(property.Name); // Try to resolve variable var values = ResolveValues(property, variables, options); AddValues(helper, values); helper.builder.EndProperty(); } helper.EndRule(); } helper.PopulateSheet(); return(helper.sheet); }
public static List <Value> ResolveValues(Property property, Dictionary <string, Property> variables, ResolvingOptions options) { if (property.Values.Count == 1 && property.Values[0].ValueType == StyleValueType.Enum && property.Values[0].AsString().StartsWith("--")) { Property varProperty; if (variables.TryGetValue(property.Values[0].AsString(), out varProperty)) { return(varProperty.Values); } else if (options.ThrowIfCannotResolve) { throw new Exception("Cannot resolve variable: " + property.Values[0].AsString()); // Debug.Log("Cannot resolve variable: " + property.Values[0].AsString()); } } return(property.Values); }