public void SetColorThemeProperty(string propertyName, Color?color) { if (_colorsBindings.TryGetValue(propertyName, out var existing)) { existing.NextValue = color; } else { _colorsBindings[propertyName] = new ColorBinding { NextValue = color }; } if (_currentDocument == null) { return; // no document to change yet } if (ApplyProperties()) { NotifyCallback(); } }
private void LoadAndParseDocument(IInputStream sourceJson) { JObject document; using (var stream = sourceJson.AsStreamForRead(0)) { using var streamReader = new StreamReader(stream); using var reader = new JsonTextReader(streamReader); document = JObject.Load(reader); } _currentDocument = document; foreach (var colorBinding in _colorsBindings) { colorBinding.Value.Elements.Clear(); } void ParseLayers(JToken layersElement) { if (!(layersElement is JArray layers)) { return; // potentially invalid lottie file } foreach (var layer in layers) { if (layer is JObject l) { var shapesValue = l.GetValue("shapes"); if (shapesValue is JArray shapes) { foreach (var shape in shapes) { if (shape is JObject s) { ParseShape(s); } } } } } } void ParseShape(JObject shapeElement) { var typeValue = shapeElement.GetValue("ty"); if (typeValue.Type != JTokenType.String) { return; // potentially invalid lottie file } var shapeType = typeValue.Value <string>(); if (shapeType != null && shapeType.Equals("gr")) { // That's a group var itemsProperty = shapeElement.GetValue("it"); if (itemsProperty is JArray items) { foreach (var item in items) { if (item is JObject s) { ParseShape(s); } } } return; } var nameProperty = shapeElement.GetValue("nm"); if (nameProperty.Type != JTokenType.String) { return; // No name } var name = nameProperty.Value <string>(); if (!string.IsNullOrWhiteSpace(name)) { var elementBindings = PropertyBindingsParser.ParseBindings(name); if (elementBindings.Length > 0) { foreach (var binding in elementBindings) { if (binding.propertyName.Equals("Color", StringComparison.Ordinal)) { if (_colorsBindings.TryGetValue(binding.bindingName, out var colorBinding)) { colorBinding.Elements.Add(shapeElement); } else { colorBinding = new ColorBinding(); colorBinding.Elements.Add(shapeElement); _colorsBindings[binding.bindingName] = colorBinding; } } } } } } if (document.TryGetValue("layers", out var layers)) { ParseLayers(layers); } }