aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Editors/AttachedElementsEditorsUndoRedoStatesProvider.cs
blob: 2ed5260e06726a235874a10fce38b623b3eabc82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Animation;
using Tango.Editors;

namespace Tango.Editors
{
    public class AttachedElementsEditorsUndoRedoStatesProvider : UndoRedoStatesProviderBase
    {
        private ElementsEditorUndoRedoExecutedEventArgs args;

        /// <summary>
        /// Gets or sets the first elements editor.
        /// </summary>
        public ElementsEditor ElementsEditor1 { get; private set; }

        /// <summary>
        /// Gets or sets the second elements editor.
        /// </summary>
        public ElementsEditor ElementsEditor2 { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="AttachedElementsEditorsUndoRedoStatesProvider"/> class.
        /// </summary>
        /// <param name="firstElementsEditor">The first elements editor.</param>
        /// <param name="secondElementsEditor">The second elements editor.</param>
        public AttachedElementsEditorsUndoRedoStatesProvider(ElementsEditor firstElementsEditor,ElementsEditor secondElementsEditor)
        {
            ElementsEditor1 = firstElementsEditor;
            ElementsEditor2 = secondElementsEditor;
        }

        /// <summary>
        /// Creates the a new undo/redo state.
        /// </summary>
        /// <returns></returns>
        public override IUndoRedoState CreateUndoRedoState()
        {
            AttachedElementsEditorsUndoRedoState state = new AttachedElementsEditorsUndoRedoState();

            foreach (var element in ElementsEditor1.Elements)
            {
                var setups = element.GetAnimationSetups(TimeSpan.FromSeconds(0), AnimationSetupMode.Discrete);
                state.Date = DateTime.Now;
                state.ElementsSetups1.Add(new KeyValuePair<IElementEditor, List<AnimationSetup>>(element, setups));
            }

            foreach (var element in ElementsEditor2.Elements)
            {
                var setups = element.GetAnimationSetups(TimeSpan.FromSeconds(0), AnimationSetupMode.Discrete);
                state.Date = DateTime.Now;
                state.ElementsSetups2.Add(new KeyValuePair<IElementEditor, List<AnimationSetup>>(element, setups));
            }

            return state;
        }

        /// <summary>
        /// Executes the an undo/redo state.
        /// </summary>
        /// <param name="state">The state.</param>
        public override void ExecuteState(IUndoRedoState state)
        {
            ExecuteState(state, ElementsEditor1, (state as AttachedElementsEditorsUndoRedoState).ElementsSetups1);
            ExecuteState(state, ElementsEditor2, (state as AttachedElementsEditorsUndoRedoState).ElementsSetups2);
        }

        /// <summary>
        /// Executes the state.
        /// </summary>
        /// <param name="state">The state.</param>
        /// <param name="ElementsEditor">The elements editor.</param>
        /// <param name="elementsSetups">The elements setups.</param>
        private void ExecuteState(IUndoRedoState state,ElementsEditor ElementsEditor, List<KeyValuePair<IElementEditor, List<AnimationSetup>>> elementsSetups)
        {
            AttachedElementsEditorsUndoRedoState elementsEditorState = state as AttachedElementsEditorsUndoRedoState;

            args = new ElementsEditorUndoRedoExecutedEventArgs();
            args.State = elementsEditorState;

            foreach (var elementSetups in elementsSetups)
            {
                var element = elementSetups.Key;
                var setups = elementSetups.Value;

                if (!ElementsEditor.Elements.Contains(element)) //Add elements missing on editor.
                {
                    ElementsEditor.Elements.Add(element);
                    args.RestoredElements.Add(element);
                }

                Storyboard story = new Storyboard();

                foreach (var setup in setups)
                {
                    story.Children.Add(setup.Animation);
                    String name = UIHelper.GetRandomAnimationName();
                    ElementsEditor.RegisterName(name, setup.DependencyObject);
                    Storyboard.SetTargetName(setup.Animation, name);
                    Storyboard.SetTargetProperty(setup.Animation, new System.Windows.PropertyPath(setup.DependencyProperty));
                }

                story.Completed += (x, y) =>
                {
                    List<KeyValuePair<AnimationSetup, object>> setupValues = new List<KeyValuePair<AnimationSetup, object>>();

                    foreach (var setup in setups)
                    {
                        KeyValuePair<AnimationSetup, object> setupvalue = new KeyValuePair<AnimationSetup, object>(setup, setup.DependencyObject.GetValue(setup.DependencyProperty));
                        setupValues.Add(setupvalue);
                    }

                    foreach (var setupValue in setupValues)
                    {
                        if (setupValue.Key.DependencyObject is IAnimatable)
                        {
                            (setupValue.Key.DependencyObject as IAnimatable).BeginAnimation(setupValue.Key.DependencyProperty, null);
                        }
                        setupValue.Key.DependencyObject.SetValue(setupValue.Key.DependencyProperty, setupValue.Value);
                    }

                };

                story.Duration = TimeSpan.FromMilliseconds(10);
                story.Begin(ElementsEditor);
            }

            //Remove elements that does not exist on state.
            var elementsToRemove = ElementsEditor.Elements.Where(x => !elementsSetups.Select(y => y.Key).ToList().Contains(x)).ToList();
            elementsToRemove.ForEach(x =>
            {
                ElementsEditor.RemoveElement(x);
            });

            args.RemovedElements = elementsToRemove;
            args.ModifiedElements = elementsSetups.
                Select(x => x.Key).
                ToList().
                Where(x => !args.RestoredElements.Contains(x) && !args.RemovedElements.Contains(x)).ToList();
        }

        /// <summary>
        /// Raises the <see cref="E:StateExecuted" /> event.
        /// </summary>
        /// <param name="e">The <see cref="T:Tango.Editors.UndoRedoStateExecutedEventArgs" /> instance containing the event data.</param>
        public override void OnStateExecuted(UndoRedoStateExecutedEventArgs e)
        {
            args.Mode = e.Mode;
            base.OnStateExecuted(args);
        }
    }
}