예제 #1
0
        public override void BeginCapture(ISnapshotProvider snapshotProvider, TankInstance tank, Rect clippingRectangle, Color shadeColor, string outputFilename)
        {
            DialogManager.Instance.ShowProgressAsync(this.CaptureDialogTitle,
                                                     string.Format(this.CaptureDialogMessage, tank.Name),
                                                     isCancellable: true)
            .ContinueWith(t =>
            {
                Thread.Sleep(500);

                DialogManager.AssignTask(
                    t.Result,
                    ActionTask.Create("ExportAnimationCapture",
                                      progress =>
                {
                    Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        var takeSnapshotProgress = progress.AddChildScope("TakeSnapshot", 80);
                        var encodeProgress       = progress.AddChildScope("EncodeGIF", 20);

                        var frameSources = this.CaptureFrames(snapshotProvider, clippingRectangle, shadeColor, takeSnapshotProgress, () => t.Result.IsCanceled);

                        if (t.Result.IsCanceled)
                        {
                            return;
                        }

                        var encoder = new AnimatedGifEncoder();

                        using (var file = File.OpenWrite(outputFilename))
                        {
                            encoder.Start(file);

                            encoder.SetRepeat(0);
                            encoder.SetDelay((int)(this.FrameTime * 1000));
                            for (var i = 0; i < frameSources.Length; ++i)
                            {
                                encodeProgress.ReportProgress((double)i / frameSources.Length);

                                if (t.Result.IsCanceled)
                                {
                                    return;
                                }

                                if (this.IsBackgroundTransparent)
                                {
                                    encoder.SetTransparentColor(shadeColor);
                                }

                                encoder.AddFrame(frameSources[i]);
                            }

                            encoder.Finish();
                        }

                        progress.ReportIsIndetermine();

                        t.Result.SetTitle(this.CaptureCompletedDialogTitle);
                        t.Result.SetMessage(string.Format(this.CaptureCompletedDialogMessage, outputFilename));
                        Thread.Sleep(1000);

                        progress.ReportProgress(1.0);
                    }));
                }));
            });
        }