Example #1
0
        /**
         *  Primary WPF-handler called before updateing the user interface
         */
        void CompositionTarget_RenderingHandler(object sender, EventArgs e)
        {
            UpdateTimeSinceLastFrame();

            if (CompositionTargertRenderingEvent != null)
            {
                CompositionTargertRenderingEvent(this, EventArgs.Empty);
            }

#if USE_SOCKETS
            try
            {
                UpdateRequiredAfterUserInteraction |= UndoRedoStack.ProcessReceivedCommands();
            }
            catch (Exception exception)
            {
                Logger.Warn("Error when excecuting a remote command:\n'{0}'", exception.Message);
            }
#endif

            if (UpdateRequiredAfterUserInteraction ||
                (MainWindow != null && Math.Abs(MainWindow.CompositionView.PlaySpeed) > 0.001))
            {
                UpdateGlobalTime();

                if (UpdateAfterUserInteractionEvent != null)
                {
                    UpdateAfterUserInteractionEvent(this, new EventArgs());
                }
                UpdateRequiredAfterUserInteraction = false;

                // Flush the dispatcher queue if too long (this is likely to result in frame drops but will ensure responsiveness of controls)
                if (_queueLength > 1)
                {
                    DoEvents();
                }
            }
        }
        public override void PasteKeyframes()
        {
            var addedNewCurves = false;

            List <ICommand> pasteKeyframeCommands   = new List <ICommand>();
            var             curvesWithSelectedTimes = getSelectedOrAllVDefinitions();

            var parameterNamesWithKeys = new Dictionary <String, List <Keyframe> >();

            try
            {
                parameterNamesWithKeys =
                    JsonConvert.DeserializeObject <Dictionary <String, List <Keyframe> > >(
                        Clipboard.GetText());
            }
            catch (Exception e)
            {
                Logger.Warn("Inserting keyframes failed: {0}", e.Message);
                return;
            }

            _updatingCurveEnabled = false;  // prevent update triggered by Curve.Move

            // Compute start and end times
            var minTime = double.PositiveInfinity;
            var maxTime = double.NegativeInfinity;

            foreach (var keyframe in parameterNamesWithKeys.Select(inputNameAndKeys => inputNameAndKeys.Value)
                     .SelectMany(keyframes => keyframes))
            {
                minTime = Math.Min(minTime, keyframe.Time);
                maxTime = Math.Max(maxTime, keyframe.Time);
            }

            // Collect matching curves
            var curvesWithMatchingInputs = new List <ICurve>();

            foreach (var inputNameAndKeys in parameterNamesWithKeys)
            {
                var inputName = inputNameAndKeys.Key;
                var keys      = inputNameAndKeys.Value;

                foreach (var input in curveInputs)
                {
                    if (input.Name != inputName)
                    {
                        continue;
                    }

                    var curve = curvesByInput[input];
                    curvesWithMatchingInputs.Add(curve);
                }
            }

            var curveMapping = new  List <Tuple <ICurve, List <Tuple <double, VDefinition> > > >();

            if (curvesWithMatchingInputs.Count == 0)
            {
                // Check if non-animated inputs exists.
                var parametersToAnimate = parameterNamesWithKeys.Keys.ToList();

                foreach (var se in m_ObservedOperatorWidgets)
                {
                    var opWidget = se as OperatorWidget;
                    if (opWidget == null)
                    {
                        continue;
                    }

                    foreach (var input in opWidget.Operator.Inputs)
                    {
                        if (parametersToAnimate.IndexOf(input.Name) == -1)
                        {
                            continue;
                        }

                        // Skip if already connected
                        if (input.Connections.Count > 0)
                        {
                            continue;
                        }

                        // Animate parameter
                        var addAnimiationCommand = new SetupAnimationCommand(input, App.Current.Model.GlobalTime);
                        App.Current.UndoRedoStack.AddAndExecute(addAnimiationCommand);

                        // Add to list of curves
                        var animationOpPart = Animation.GetRegardingAnimationOpPart(input);
                        var animationCurve  = animationOpPart.Func as ICurve;
                        curvesByInput[input] = animationCurve;
                        curveInputs.Add(input);
                        parametersToAnimate.Remove(input.Name);
                        addedNewCurves = true;
                    }
                }
            }

            // Insert gap to all animated parameters
            foreach (var curve in curvesByInput.Values)
            {
                var points = curve.GetPoints();
                points.Sort((a, b) => a.Key > b.Key ? -1:1);        // If moving unsorted, later keyframes could be overwritten.

                foreach (var timeWithKey in points)
                {
                    var time = timeWithKey.Key;
                    if (time > App.Current.Model.GlobalTime)
                    {
                        var c = new MoveKeyframeCommand(time, time + maxTime - minTime, curve);
                        pasteKeyframeCommands.Add(c);
                    }
                }
            }

            foreach (var inputNameAndKeys in parameterNamesWithKeys)
            {
                var inputName = inputNameAndKeys.Key;
                var keyframes = inputNameAndKeys.Value;

                foreach (var input in curveInputs)
                {
                    if (input.Name != inputName)
                    {
                        continue;
                    }

                    var curve = curvesByInput[input];

                    foreach (var keyframe in keyframes)
                    {
                        pasteKeyframeCommands.Add(new AddOrUpdateKeyframeCommand(keyframe.Time - minTime + App.Current.Model.GlobalTime, keyframe.VDefinition, curve));
                    }
                }
            }

            var macroCommand = new MacroCommand("Paste Keyframes", pasteKeyframeCommands);

            App.Current.UndoRedoStack.AddAndExecute(macroCommand);
            _updatingCurveEnabled = true;
            if (addedNewCurves)
            {
                ShowCurvesForSelectedOperators();
            }
            else
            {
                RebuildCurrentCurves();
            }
        }