private void AddNewQuest_FormClosing(AddNewQuest form, Box shape)
        {
            if (form.Accepted)
            {
                Command command = null;
                var     quest   = Quest.Default;
                quest.Name       = form.QuestName;
                quest.SectorName = form.Sector.Name;

                var addQuestCommand = CommandsCreation.AddQuest(quest, form.Sector, Context, DiagramWrapper, shape);
                if (form.ActivateByDefault)
                {
                    var c = new CompositeCommand();
                    c.AddCommand(addQuestCommand);
                    c.AddCommand(CommandsCreation.ActivateQuest(quest, form.Sector, Context, DiagramWrapper));
                    command = c;
                }
                else
                {
                    command = addQuestCommand;
                }
                Context.History.Do(command);

                shape.SetCaptionText(0, quest.Name);
            }
            else
            {
                DiagramWrapper.RemoveNodeShape(shape);
            }
        }
Exemple #2
0
        public override void OnShapesDeleted(List <Shape> affectedShapes)
        {
            base.OnShapesDeleted(affectedShapes);

            var questsToDelete = affectedShapes.OfType <Box>().Select(FindQuestForShape).ToList();

            if (questsToDelete.Count > 0)
            {
                var command = new CompositeCommand();
                foreach (var q in questsToDelete)
                {
                    var links = Context.Flow.Graph.GetLinksForNode(Context.Flow.Graph.FindNodeForQuest(q));
                    command.AddCommands(links.Select(l => CommandsCreation.RemoveLink(l, Context, DiagramWrapper)));
                }
                command.AddCommands(questsToDelete.Select(
                                        q => CommandsCreation.DeactivateQuest(q, Context, DiagramWrapper)
                                        ));
                command.AddCommands(questsToDelete.Select(
                                        q => CommandsCreation.RemoveQuest(
                                            q,
                                            Context,
                                            Context.Flow.Graph.FindNodeForQuest(q),
                                            DiagramWrapper,
                                            DiagramWrapper.GetShapeForQuest(q))
                                        )
                                    );
                Context.History.Do(command);
            }
            else
            {
                var commands = affectedShapes.Select(FindLinkForShape)
                               .Where(l => l.HasValue)
                               .Select(l => CommandsCreation.RemoveLink(l.Value, Context, DiagramWrapper))
                               .ToList();
                if (commands.Count > 0)
                {
                    Context.History.Do(new CompositeCommand(commands));
                }
            }
        }
        public override void OnShapesInserted(List <Shape> affectedShapes)
        {
            base.OnShapesInserted(affectedShapes);

            if (affectedShapes.Count == 1 && affectedShapes.First() is Polyline)
            {
                var p   = affectedShapes.First() as Polyline;
                var cps = p.GetControlPointIds(ControlPointCapabilities.All);
                var p1  = p.GetControlPointPosition(cps.First());
                var p2  = p.GetControlPointPosition(cps.Last());

                Box b1 = null;
                Box b2 = null;

                foreach (var s in p.Diagram.Shapes.Except(p))
                {
                    if (b1 == null && s.ContainsPoint(p1.X, p1.Y))
                    {
                        b1 = s as Box;
                    }
                    else if (b2 == null && s.ContainsPoint(p2.X, p2.Y))
                    {
                        b2 = s as Box;
                    }
                }

                if (b1 != null && b2 != null && b1 != b2)
                {
                    var n1 = DiagramWrapper.GetNodeForShape(b1);
                    var n2 = DiagramWrapper.GetNodeForShape(b2);

                    if (!n2.Quest.IsLinksToEditable())
                    {
                        DeleteShapeOnDeselect(p);
                        var msg = $"Couldn't create link to {n2.Quest.Name} quest - links to this quest are editable only through code.";
                        MessageBox.Show(msg);
                        return;
                    }

                    var link = new Link(n1, n2);

                    if (Context.Flow.Graph.ExistsLink(link))
                    {
                        DeleteShapeOnDeselect(p);
                        var msg = $"Couldn't create link from {n1.Quest.Name} to {n2.Quest.Name} quest - link already exists.";
                        MessageBox.Show(msg);
                        return;
                    }

                    (var loopForms, var loop) = GraphValidation.LoopForms(Context.Flow.Graph, link);
                    if (loopForms)
                    {
                        DeleteShapeOnDeselect(p);
                        var loopStringRepresentation = string.Join(" - ", loop.Select(q => q.Quest.Name));
                        var msg = $"Couldn't create link from {n1.Quest.Name} to {n2.Quest.Name} quest - loop forms: {loopStringRepresentation}.";
                        MessageBox.Show(msg);
                        return;
                    }

                    DiagramWrapper.RegisterShapeForLink(p, link);

                    if (!n1.Quest.IsActive() || !n2.Quest.IsActive())
                    {
                        var command = new CompositeCommand();

                        void InitQuestActivationCommand(Quest quest) =>
                        command.AddCommand(
                            CommandsCreation.ActivateQuest(
                                quest,
                                Context.Flow.GetSectorForQuest(quest),
                                Context,
                                DiagramWrapper
                                )
                            );

                        if (!n1.Quest.IsActive())
                        {
                            InitQuestActivationCommand(n1.Quest);
                        }
                        if (!n2.Quest.IsActive())
                        {
                            InitQuestActivationCommand(n2.Quest);
                        }
                        command.AddCommand(CommandsCreation.AddLink(link, Context, DiagramWrapper));

                        Context.History.Do(command);
                    }
                    else
                    {
                        Context.History.Do(CommandsCreation.AddLink(link, Context, DiagramWrapper));
                    }
                }
                else
                {
                    DeleteShapeOnDeselect(p);
                }
            }
            else
            {
                DeleteShapesOnDeselect(affectedShapes);
            }
        }
Exemple #4
0
 private Link?FindLinkForShape(Shape shape)
 {
     return(DiagramWrapper.GetLinkForShape(shape));
 }
Exemple #5
0
 private Quest FindQuestForShape(Shape shape)
 {
     return(DiagramWrapper.GetNodeForShape(shape)?.Quest);
 }