Ejemplo n.º 1
0
        private SimpleUndoPair InnerAddNodes(IEnumerable <ConversationNode> nodes, IEnumerable <NodeGroup> groups, ILocalizationEngine localization)
        {
            List <Action> undoActions = new List <Action>();
            List <Action> redoActions = new List <Action>();

            //Set up actions for adding/removing the nodes
            foreach (var node in nodes)
            {
                var            n       = node;
                SimpleUndoPair actions = n.GetNodeRemoveActions();

                //Ensure that the localization engine is up to date in terms of usage of localized data
                foreach (var parameter in n.Data.Parameters.OfType <ILocalizedStringParameter>())
                {
                    if (parameter.Value != null)
                    {
                        SimpleUndoPair clearLocalization = localization.ClearLocalizationAction(Id <LocalizedStringType> .ConvertFrom(parameter.TypeId), parameter.Value);
                        undoActions.Add(clearLocalization.Redo);
                        redoActions.Add(clearLocalization.Undo);
                    }
                }

                var containingGroups = m_groups.Where(g => g.Contents.Contains(n.Data.NodeId)).Evaluate();
                redoActions.Add(() =>
                {
                    m_nodes.Add(n);
                    m_audioProvider.UpdateUsage(n);
                    foreach (var group in containingGroups)
                    {
                        group.Contents.Add(n.Data.NodeId);
                    }
                    actions.Undo(); //Undo the node removal
                });
                undoActions.Add(() =>
                {
                    if (CanRemoveFromData(n, PromptNodeDeletion))
                    {
                        m_nodes.Remove(n);
                    }
                    foreach (var group in containingGroups)
                    {
                        group.Contents.Remove(n.Data.NodeId);
                    }
                    actions.Redo(); //Redo the node removal
                    NodesDeleted.Execute();
                });
            }

            //Set up actions for adding/removing nodes from other groups that are gaining/losing their grouping due to removing/adding new groups
            foreach (var group in groups)
            {
                foreach (var node in group.Contents)
                {
                    var n   = node;
                    var old = m_groups.SingleOrDefault(g => g.Contents.Contains(n));
                    if (old != null)
                    {
                        undoActions.Add(() => old.Contents.Add(n));
                        redoActions.Add(() => old.Contents.Remove(n));
                    }
                }
            }

            //Set up actions for adding/removing the groups
            undoActions.Add(() =>
            {
                foreach (var group in groups.Reverse())
                {
                    m_groups.Remove(group);
                }
            });
            redoActions.Add(() =>
            {
                m_groups.AddRange(groups);
            });

            return(new SimpleUndoPair
            {
                Undo = () => { using (m_audioProvider.SuppressUpdates()) foreach (Action action in undoActions)
                                   {
                                       action();
                                   }
                },
                Redo = () => { using (m_audioProvider.SuppressUpdates()) foreach (Action action in redoActions)
                                   {
                                       action();
                                   }
                },
            });
        }