Ejemplo n.º 1
0
        /// <summary>
        /// Creates a new instance of <see cref="Windows.UI.Composition.Visual"/>
        /// described by the given <see cref="WinCompData.Visual"/>.
        /// </summary>
        /// <returns>The <see cref="Windows.UI.Composition.Visual"/>.</returns>
        internal static Wc.Visual CreateVisual(Wc.Compositor compositor, Wd.Visual visual)
        {
            var converter = new Instantiator(compositor);
            var result    = converter.GetVisual(visual);

            return(result);
        }
Ejemplo n.º 2
0
        T CacheAndInitializeVisual <T>(Wd.Visual source, T target)
            where T : Wc.Visual
        {
            CacheAndInitializeCompositionObject(source, target);

            if (source.Clip != null)
            {
                target.Clip = GetCompositionClip(source.Clip);
            }

            if (source.CenterPoint.HasValue)
            {
                target.CenterPoint = source.CenterPoint.Value;
            }

            if (source.Offset.HasValue)
            {
                target.Offset = source.Offset.Value;
            }

            if (source.Opacity.HasValue)
            {
                target.Opacity = source.Opacity.Value;
            }

            if (source.RotationAngleInDegrees.HasValue)
            {
                target.RotationAngleInDegrees = source.RotationAngleInDegrees.Value;
            }

            if (source.RotationAxis.HasValue)
            {
                target.RotationAxis = source.RotationAxis.Value;
            }

            if (source.Scale.HasValue)
            {
                target.Scale = source.Scale.Value;
            }

            if (source.Size.HasValue)
            {
                target.Size = source.Size.Value;
            }

            if (source.TransformMatrix.HasValue)
            {
                target.TransformMatrix = source.TransformMatrix.Value;
            }

            return(target);
        }
Ejemplo n.º 3
0
        internal void SetRootVisual(WinCompData.Visual rootVisual)
        {
            // Save the root visual.
            _wincompDataRootVisual = rootVisual;

            // Find the theming property set, if any.
            var graph = ObjectGraph <Graph.Node> .FromCompositionObject(_wincompDataRootVisual, includeVertices : false);

            _wincompDataThemingPropertySet = graph.
                                             CompositionObjectNodes.
                                             Where(n => n.Object is WinCompData.CompositionPropertySet cps && cps.Owner is null).
                                             Select(n => (WinCompData.CompositionPropertySet)n.Object).FirstOrDefault();
        }
Ejemplo n.º 4
0
        Wc.Visual GetVisual(Wd.Visual obj)
        {
            switch (obj.Type)
            {
            case Wd.CompositionObjectType.ContainerVisual:
                return(GetContainerVisual((Wd.ContainerVisual)obj));

            case Wd.CompositionObjectType.ShapeVisual:
                return(GetShapeVisual((Wd.ShapeVisual)obj));

            default:
                throw new InvalidOperationException();
            }
        }
Ejemplo n.º 5
0
        internal async Task <ContentFactory> LoadAsync(LottieVisualOptions options)
        {
            LottieVisualDiagnostics diagnostics = null;
            var timeMeasurer = TimeMeasurer.Create();

            if (options.HasFlag(LottieVisualOptions.IncludeDiagnostics))
            {
                diagnostics = new LottieVisualDiagnostics {
                    Options = options
                };
            }

            var result = new ContentFactory(diagnostics);

            // Get the file name and JSON contents.
            (var fileName, var jsonStream) = await GetJsonStreamAsync();

            if (diagnostics != null)
            {
                diagnostics.FileName = fileName;
                diagnostics.ReadTime = timeMeasurer.GetElapsedAndRestart();
            }

            if (jsonStream is null)
            {
                // Failed to load ...
                return(result);
            }

            // Parsing large Lottie files can take significant time. Do it on
            // another thread.
            LottieComposition lottieComposition = null;

            await CheckedAwaitAsync(Task.Run(() =>
            {
                lottieComposition =
                    LottieCompositionReader.ReadLottieCompositionFromJsonStream(
                        jsonStream,
                        LottieCompositionReader.Options.IgnoreMatchNames,
                        out var readerIssues);

                if (diagnostics != null)
                {
                    diagnostics.JsonParsingIssues = ToIssues(readerIssues);
                }
            }));

            if (diagnostics != null)
            {
                diagnostics.ParseTime = timeMeasurer.GetElapsedAndRestart();
            }

            if (lottieComposition is null)
            {
                // Failed to load...
                return(result);
            }

            if (diagnostics != null)
            {
                // Save the LottieComposition in the diagnostics so that the xml and codegen
                // code can be derived from it.
                diagnostics.LottieComposition = lottieComposition;

                // Validate the composition and report if issues are found.
                diagnostics.LottieValidationIssues = ToIssues(LottieCompositionValidator.Validate(lottieComposition));
                diagnostics.ValidationTime         = timeMeasurer.GetElapsedAndRestart();
            }

            result.SetDimensions(
                width: lottieComposition.Width,
                height: lottieComposition.Height,
                duration: lottieComposition.Duration);

            // Translating large Lotties can take significant time. Do it on another thread.
            WinCompData.Visual wincompDataRootVisual = null;
            uint requiredUapVersion  = 0;
            var  optimizationEnabled = options.HasFlag(LottieVisualOptions.Optimize);

            TranslationResult translationResult;

            await CheckedAwaitAsync(Task.Run(() =>
            {
                // Generate property bindings only if the diagnostics object was requested.
                // This is because the binding information is output in the diagnostics object
                // so there's no point translating bindings if the diagnostics object
                // isn't available.
                var makeColorsBindable = diagnostics != null && options.HasFlag(LottieVisualOptions.BindableColors);
                translationResult      = LottieToWinCompTranslator.TryTranslateLottieComposition(
                    lottieComposition: lottieComposition,
                    configuration: new TranslatorConfiguration
                {
                    TranslatePropertyBindings = makeColorsBindable,
                    GenerateColorBindings     = makeColorsBindable,
                    TargetUapVersion          = GetCurrentUapVersion(),
                });

                wincompDataRootVisual = translationResult.RootVisual;
                requiredUapVersion    = translationResult.MinimumRequiredUapVersion;

                if (diagnostics != null)
                {
                    diagnostics.TranslationIssues = ToIssues(translationResult.TranslationIssues);
                    diagnostics.TranslationTime   = timeMeasurer.GetElapsedAndRestart();

                    // If there were any property bindings, save them in the Diagnostics object.
                    if (translationResult.SourceMetadata.TryGetValue(s_propertyBindingNamesKey, out var propertyBindingNames))
                    {
                        diagnostics.ThemePropertyBindings = (IReadOnlyList <PropertyBinding>)propertyBindingNames;
                    }
                }

                // Optimize the resulting translation. This will usually significantly reduce the size of
                // the Composition code, however it might slow down loading too much on complex Lotties.
                if (wincompDataRootVisual != null && optimizationEnabled)
                {
                    // Optimize.
                    wincompDataRootVisual = UIData.Tools.Optimizer.Optimize(wincompDataRootVisual, ignoreCommentProperties: true);

                    if (diagnostics != null)
                    {
                        diagnostics.OptimizationTime = timeMeasurer.GetElapsedAndRestart();
                    }
                }
            }));

            if (wincompDataRootVisual is null)
            {
                // Failed.
                return(result);
            }
            else
            {
                if (diagnostics != null)
                {
                    // Save the root visual so diagnostics can generate XML and codegen.
                    diagnostics.RootVisual         = wincompDataRootVisual;
                    diagnostics.RequiredUapVersion = requiredUapVersion;
                }

                result.SetRootVisual(wincompDataRootVisual);
                return(result);
            }
        }