Example #1
0
        private void AddChapterButton()
        {
            GUILayout.Space(VerticalSpace);
            GUILayout.Space(VerticalSpace);

            GUILayout.BeginHorizontal();
            {
                GUILayout.FlexibleSpace();
                if (GUILayout.Button("+Add Chapter", GUILayout.Width(128), GUILayout.Height(32)))
                {
                    RevertableChangesHandler.Do(new TrainingCommand(
                                                    // ReSharper disable once ImplicitlyCapturedClosure
                                                    () =>
                    {
                        Course.Data.Chapters.Add(new Chapter("Chapter " + (Course.Data.Chapters.Count + 1), null));
                        activeChapter = Course.Data.Chapters.Count - 1;
                        EmitChapterChanged();
                    },
                                                    // ReSharper disable once ImplicitlyCapturedClosure
                                                    () =>
                    {
                        RemoveChapterAt(Course.Data.Chapters.Count - 1);
                    }
                                                    ));
                }

                GUILayout.FlexibleSpace();
            }
            GUILayout.EndHorizontal();

            GUILayout.Space(VerticalSpace);
            GUILayout.Space(VerticalSpace);
        }
Example #2
0
        private void CheckForMisconfigurationIssues(GameObject selectedSceneObject, Type valueType, ref Rect originalRect, ref Rect guiLineRect)
        {
            if (selectedSceneObject != null && selectedSceneObject.GetComponent(valueType) == null)
            {
                string       warning = $"{selectedSceneObject.name} is not configured as {valueType.Name}";
                const string button  = "Fix it";

                EditorGUI.HelpBox(guiLineRect, warning, MessageType.Error);
                guiLineRect = AddNewRectLine(ref originalRect);

                if (GUI.Button(guiLineRect, button))
                {
                    // Only relevant for Undoing a Training Property.
                    bool        isAlreadySceneObject      = selectedSceneObject.GetComponent <TrainingSceneObject>() != null && typeof(ISceneObjectProperty).IsAssignableFrom(valueType);
                    Component[] alreadyAttachedProperties = selectedSceneObject.GetComponents(typeof(Component));

                    RevertableChangesHandler.Do(
                        new CourseCommand(
                            () => SceneObjectAutomaticSetup(selectedSceneObject, valueType),
                            () => UndoSceneObjectAutomaticSetup(selectedSceneObject, valueType, isAlreadySceneObject, alreadyAttachedProperties)),
                        undoGroupName);
                }

                guiLineRect = AddNewRectLine(ref originalRect);
            }
        }
Example #3
0
        private void AddRemoveButton(int position, bool isDisabled)
        {
            EditorGUI.BeginDisabledGroup(isDisabled);
            {
                if (FlatIconButton(deleteIcon.Texture))
                {
                    IChapter chapter           = Course.Data.Chapters[position];
                    bool     isDeleteTriggered = EditorUtility.DisplayDialog(string.Format("Delete Chapter '{0}'", chapter.Data.Name),
                                                                             string.Format("Do you really want to delete chapter '{0}'? You will lose all steps stored there.", chapter.Data.Name), "Delete",
                                                                             "Cancel");

                    if (isDeleteTriggered)
                    {
                        RevertableChangesHandler.Do(new TrainingCommand(
                                                        // ReSharper disable once ImplicitlyCapturedClosure
                                                        () =>
                        {
                            RemoveChapterAt(position);
                        },
                                                        // ReSharper disable once ImplicitlyCapturedClosure
                                                        () =>
                        {
                            Course.Data.Chapters.Insert(position, chapter);
                            if (position == activeChapter)
                            {
                                EmitChapterChanged();
                            }
                        }
                                                        ));
                    }
                }
            }
            EditorGUI.EndDisabledGroup();
        }
Example #4
0
        private void SetupNode(EditorNode node, Action <Vector2> setPositionInModel)
        {
            Vector2 positionBeforeDrag = node.Position;

            node.GraphicalEventHandler.PointerDown += (sender, args) =>
            {
                positionBeforeDrag = node.Position;
            };

            node.GraphicalEventHandler.PointerUp += (sender, args) =>
            {
                if (Mathf.Abs((positionBeforeDrag - node.Position).sqrMagnitude) < 0.001f)
                {
                    return;
                }

                Vector2 positionAfterDrag          = node.Position;
                Vector2 closuredPositionBeforeDrag = positionBeforeDrag;

                RevertableChangesHandler.Do(new TrainingCommand(() =>
                {
                    setPositionInModel(positionAfterDrag);
                    MarkToRefresh();
                }, () =>
                {
                    setPositionInModel(closuredPositionBeforeDrag);
                    MarkToRefresh();
                }));
            };

            node.GraphicalEventHandler.PointerDrag += (sender, args) =>
            {
                node.RelativePosition += args.PointerDelta;
            };
        }
Example #5
0
        private void OnGUI()
        {
            if (nameable == null || focusedWindow != this)
            {
                Close();
                instance.IsClosed = true;
            }

            GUI.SetNextControlName(textFieldIdentifier.ToString());
            newName = EditorGUILayout.TextField(newName);

            if (isFocusSet == false)
            {
                isFocusSet = true;
                EditorGUI.FocusTextInControl(textFieldIdentifier.ToString());
            }

            if (focusedWindow != this)
            {
                return;
            }

            if (Event.current.isKey == false)
            {
                return;
            }

            if ((Event.current.keyCode == KeyCode.Return || Event.current.keyCode == KeyCode.KeypadEnter))
            {
                if (string.IsNullOrEmpty(newName))
                {
                    return;
                }

                string oldName = nameable.Name;
                RevertableChangesHandler.Do(new CourseCommand(
                                                // ReSharper disable once ImplicitlyCapturedClosure
                                                () =>
                {
                    nameable.Name = newName;
                },
                                                // ReSharper disable once ImplicitlyCapturedClosure
                                                () =>
                {
                    nameable.Name = oldName;
                }
                                                ));
                Close();
                instance.IsClosed = true;
                Event.current.Use();
            }
            else if (Event.current.keyCode == KeyCode.Escape)
            {
                Close();
                instance.IsClosed = true;
                Event.current.Use();
            }
        }
Example #6
0
        /// <inheritdoc />
        public void ChangeValue(Func <object> getNewValueCallback, Func <object> getOldValueCallback, Action <object> assignValueCallback)
        {
            // ReSharper disable once ImplicitlyCapturedClosure
            Action doCallback = () => assignValueCallback(getNewValueCallback());
            // ReSharper disable once ImplicitlyCapturedClosure
            Action undoCallback = () => assignValueCallback(getOldValueCallback());

            RevertableChangesHandler.Do(new TrainingCommand(doCallback, undoCallback));
        }
Example #7
0
        /// <inheritdoc />
        public override Rect Draw(Rect rect, object currentValue, Action <object> changeValueCallback, GUIContent label)
        {
            if (RuntimeConfigurator.Exists == false)
            {
                return(rect);
            }

            isUndoOperation = false;
            UniqueNameReference uniqueNameReference = (UniqueNameReference)currentValue;
            PropertyInfo        valueProperty       = currentValue.GetType().GetProperty("Value");
            Type valueType = ReflectionUtils.GetDeclaredTypeOfPropertyOrField(valueProperty);

            if (valueProperty == null)
            {
                throw new ArgumentException("Only ObjectReference<> implementations should inherit from the UniqueNameReference type.");
            }

            Rect       guiLineRect         = rect;
            string     oldUniqueName       = uniqueNameReference.UniqueName;
            GameObject selectedSceneObject = GetGameObjectFromID(oldUniqueName);

            if (selectedSceneObject == null && string.IsNullOrEmpty(oldUniqueName) == false && missingUniqueNames.Contains(oldUniqueName) == false)
            {
                missingUniqueNames.Add(oldUniqueName);
                Debug.LogError($"The Training Scene Object with the unique name '{oldUniqueName}' cannot be found!");
            }

            CheckForMisconfigurationIssues(selectedSceneObject, valueType, ref rect, ref guiLineRect);
            selectedSceneObject = EditorGUI.ObjectField(guiLineRect, label, selectedSceneObject, typeof(GameObject), true) as GameObject;

            string newUniqueName = GetIDFromSelectedObject(selectedSceneObject, valueType, oldUniqueName);

            if (oldUniqueName != newUniqueName)
            {
                RevertableChangesHandler.Do(
                    new CourseCommand(
                        () =>
                {
                    uniqueNameReference.UniqueName = newUniqueName;
                    changeValueCallback(uniqueNameReference);
                },
                        () =>
                {
                    uniqueNameReference.UniqueName = oldUniqueName;
                    changeValueCallback(uniqueNameReference);
                }),
                    isUndoOperation ? undoGroupName : string.Empty);

                if (isUndoOperation)
                {
                    RevertableChangesHandler.CollapseUndoOperations(undoGroupName);
                }
            }

            return(rect);
        }
Example #8
0
 private void AddStepWithUndo(IStep step)
 {
     RevertableChangesHandler.Do(new CourseCommand(() =>
     {
         AddStep(step);
         CurrentChapter.ChapterMetadata.LastSelectedStep = step;
     },
                                                   () =>
     {
         DeleteStep(step);
     }
                                                   ));
 }
Example #9
0
 private void AddMoveUpButton(int position)
 {
     if (FlatIconButton(arrowUpIcon.Texture))
     {
         if (position > 0)
         {
             RevertableChangesHandler.Do(new TrainingCommand(
                                             // ReSharper disable once ImplicitlyCapturedClosure
                                             () =>
             {
                 MoveChapterUp(position);
             },
                                             // ReSharper disable once ImplicitlyCapturedClosure
                                             () =>
             {
                 MoveChapterDown(position - 1);
             }
                                             ));
         }
     }
 }
Example #10
0
 private void AddMoveDownButton(int position)
 {
     if (FlatIconButton(arrowDownIcon.Texture))
     {
         if (position + 1 < Course.Data.Chapters.Count)
         {
             RevertableChangesHandler.Do(new TrainingCommand(
                                             // ReSharper disable once ImplicitlyCapturedClosure
                                             () =>
             {
                 MoveChapterDown(position);
             },
                                             // ReSharper disable once ImplicitlyCapturedClosure
                                             () =>
             {
                 MoveChapterUp(position + 1);
             }
                                             ));
         }
     }
 }
Example #11
0
        private void HandleCanvasContextClick(object sender, PointerGraphicalElementEventArgs e)
        {
            TestableEditorElements.DisplayContextMenu(new List <TestableEditorElements.MenuOption>
            {
                new TestableEditorElements.MenuItem(new GUIContent("Add step"), false, () =>
                {
                    IStep step = new Step("New Step");
                    step.StepMetadata.Position = e.PointerPosition;

                    RevertableChangesHandler.Do(new TrainingCommand(() =>
                    {
                        AddStep(step);
                        chapter.ChapterMetadata.LastSelectedStep = step;
                    },
                                                                    () =>
                    {
                        DeleteStep(step);
                    }
                                                                    ));
                })
            });
        }
Example #12
0
        private void DeleteStepWithUndo(IStep step, StepNode ownerNode)
        {
            IList <ITransition> incomingTransitions = CurrentChapter.Data.Steps.SelectMany(s => s.Data.Transitions.Data.Transitions).Where(transition => transition.Data.TargetStep == step).ToList();

            bool wasFirstStep = step == CurrentChapter.Data.FirstStep;

            RevertableChangesHandler.Do(new CourseCommand(
                                            () =>
            {
                foreach (ITransition transition in incomingTransitions)
                {
                    transition.Data.TargetStep = null;
                }

                DeleteStep(step);

                if (wasFirstStep)
                {
                    CurrentChapter.Data.FirstStep = null;
                }
            },
                                            () =>
            {
                AddStep(step);

                if (wasFirstStep)
                {
                    CurrentChapter.Data.FirstStep = step;
                }

                foreach (ITransition transition in incomingTransitions)
                {
                    transition.Data.TargetStep = step;
                }

                SelectStepNode(ownerNode);
            }
                                            ));
        }
Example #13
0
        private void SetupNode(EditorNode node, Action <Vector2> setPositionInModel)
        {
            Vector2 positionBeforeDrag = node.Position;
            Vector2 deltaOnPointerDown = Vector2.zero;

            node.GraphicalEventHandler.PointerDown += (sender, args) =>
            {
                positionBeforeDrag = node.Position;
                deltaOnPointerDown = node.Position - args.PointerPosition;
            };

            node.GraphicalEventHandler.PointerUp += (sender, args) =>
            {
                if (Mathf.Abs((positionBeforeDrag - node.Position).sqrMagnitude) < 0.001f)
                {
                    return;
                }

                Vector2 positionAfterDrag          = node.Position;
                Vector2 closuredPositionBeforeDrag = positionBeforeDrag;

                RevertableChangesHandler.Do(new CourseCommand(() =>
                {
                    setPositionInModel(positionAfterDrag);
                    MarkToRefresh();
                }, () =>
                {
                    setPositionInModel(closuredPositionBeforeDrag);
                    MarkToRefresh();
                }));
            };

            node.GraphicalEventHandler.PointerDrag += (sender, args) =>
            {
                SetNewPositionOnGrid(node, args.PointerPosition, deltaOnPointerDown);
            };
        }
Example #14
0
        private StepNode CreateNewStepNode(IStep step)
        {
            StepNode node = new StepNode(Graphics, CurrentChapter, step);

            node.GraphicalEventHandler.ContextClick += (sender, args) =>
            {
                TestableEditorElements.DisplayContextMenu(new List <TestableEditorElements.MenuOption>
                {
                    new TestableEditorElements.MenuItem(new GUIContent("Copy"), false, () =>
                    {
                        CopyStep(step);
                    }),
                    new TestableEditorElements.MenuItem(new GUIContent("Cut"), false, () =>
                    {
                        CutStep(step, node);
                    }),
                    new TestableEditorElements.MenuItem(new GUIContent("Delete"), false, () =>
                    {
                        DeleteStepWithUndo(step, node);
                    })
                });
            };

            node.GraphicalEventHandler.PointerDown += (sender, args) =>
            {
                UserSelectStepNode(node);
            };

            node.RelativePositionChanged += (sender, args) =>
            {
                node.Step.StepMetadata.Position = node.Position;
            };

            node.GraphicalEventHandler.PointerUp += (sender, args) =>
            {
                Graphics.CalculateBoundingBox();
            };

            // ReSharper disable once ImplicitlyCapturedClosure
            node.GraphicalEventHandler.PointerDown += (sender, args) => UserSelectStepNode(node);

            node.CreateTransitionButton.GraphicalEventHandler.PointerClick += (sender, args) =>
            {
                ITransition transition = EntityFactory.CreateTransition();

                RevertableChangesHandler.Do(new CourseCommand(
                                                () =>
                {
                    step.Data.Transitions.Data.Transitions.Add(transition);
                    MarkToRefresh();
                },
                                                () =>
                {
                    step.Data.Transitions.Data.Transitions.Remove(transition);
                    MarkToRefresh();
                }
                                                ));
            };

            if (CurrentChapter.ChapterMetadata.LastSelectedStep == step)
            {
                SelectStepNode(node);
            }

            SetupNode(node, position => node.Step.StepMetadata.Position = position);

            return(node);
        }
Example #15
0
        private void OnGUI()
        {
            if (course == null || focusedWindow != this)
            {
                Close();
                instance.IsClosed = true;
            }

            GUI.SetNextControlName(textFieldIdentifier.ToString());
            newName = EditorGUILayout.TextField(newName);
            newName = newName.Trim();

            if (isFocusSet == false)
            {
                isFocusSet = true;
                EditorGUI.FocusTextInControl(textFieldIdentifier.ToString());
            }

            if ((Event.current.keyCode == KeyCode.Return || Event.current.keyCode == KeyCode.KeypadEnter))
            {
                if (string.IsNullOrEmpty(newName) == false && ValidateCourseName(newName))
                {
                    string oldName   = course.Data.Name;
                    string oldPath   = SaveManager.GetTrainingPath(oldName);
                    string oldFolder = Path.GetDirectoryName(oldPath);
                    string newPath   = SaveManager.GetTrainingPath(newName);
                    string newFolder = Path.GetDirectoryName(newPath);

                    RevertableChangesHandler.Do(new TrainingCommand(
                                                    // ReSharper disable once ImplicitlyCapturedClosure
                                                    () =>
                    {
                        if (ValidateCourseName(newName))
                        {
                            Directory.Move(oldFolder, newFolder);
                            File.Move(string.Format("{0}.meta", oldFolder), string.Format("{0}.meta", newFolder));
                            File.Move(string.Format("{0}/{1}.json", newFolder, oldName), newPath);
                            File.Move(string.Format("{0}/{1}.json.meta", newFolder, oldName), string.Format("{0}.meta", newPath));
                            course.Data.Name = newName;

                            SaveManager.SaveTrainingCourseToFile(course);
                            RuntimeConfigurator.SetSelectedTrainingCourse(newPath.Substring(Application.streamingAssetsPath.Length + 1));
                            TrainingWindow.GetWindow().IsDirty = false;
                        }
                    },
                                                    // ReSharper disable once ImplicitlyCapturedClosure
                                                    () =>
                    {
                        if (Directory.Exists(newFolder) == false)
                        {
                            return;
                        }

                        Directory.Move(newFolder, oldFolder);
                        File.Move(string.Format("{0}.meta", newFolder), string.Format("{0}.meta", oldFolder));
                        File.Move(string.Format("{0}/{1}.json", oldFolder, newName), oldPath);
                        File.Move(string.Format("{0}/{1}.json.meta", oldFolder, newName), string.Format("{0}.meta", oldPath));
                        course.Data.Name = oldName;

                        SaveManager.SaveTrainingCourseToFile(course);
                        RuntimeConfigurator.SetSelectedTrainingCourse(oldPath.Substring(Application.streamingAssetsPath.Length + 1));
                    }
                                                    ));
                }

                Close();
                instance.IsClosed = true;
                Event.current.Use();
            }
            else if (Event.current.keyCode == KeyCode.Escape)
            {
                Close();
                instance.IsClosed = true;
                Event.current.Use();
            }
        }
Example #16
0
        private EntryNode CreateEntryNode(IChapter chapter)
        {
            EntryNode entryNode = new EntryNode(Graphics);

            entryNode.IsDragging = false;

            ExitJoint joint = new ExitJoint(Graphics, entryNode)
            {
                RelativePosition = new Vector2(entryNode.LocalBoundingBox.xMax, entryNode.LocalBoundingBox.center.y),
            };

            entryNode.ExitJoints.Add(joint);

            entryNode.Position = chapter.ChapterMetadata.EntryNodePosition;

            entryNode.RelativePositionChanged += (sender, args) =>
            {
                chapter.ChapterMetadata.EntryNodePosition = entryNode.Position;
            };

            entryNode.GraphicalEventHandler.PointerUp += (sender, args) =>
            {
                entryNode.IsDragging = false;
                Graphics.CalculateBoundingBox();
            };

            entryNode.GraphicalEventHandler.PointerDown += (sender, args) =>
            {
                entryNode.IsDragging = true;
                Graphics.CalculateBoundingBox();
            };

            entryNode.GraphicalEventHandler.ContextClick += (sender, args) =>
            {
                if (chapter.Data.FirstStep == null)
                {
                    return;
                }

                TestableEditorElements.DisplayContextMenu(new List <TestableEditorElements.MenuOption>
                {
                    new TestableEditorElements.MenuItem(new GUIContent("Delete transition"), false, () =>
                    {
                        IStep firstStep = chapter.Data.FirstStep;

                        RevertableChangesHandler.Do(new CourseCommand(() =>
                        {
                            chapter.Data.FirstStep = null;
                            MarkToRefresh();
                        },
                                                                      () =>
                        {
                            chapter.Data.FirstStep = firstStep;
                            MarkToRefresh();
                        }
                                                                      ));
                    })
                });
            };

            joint.GraphicalEventHandler.PointerDrag += (sender, args) =>
            {
                joint.DragDelta = args.PointerPosition - joint.Position;
            };

            joint.GraphicalEventHandler.PointerUp += (sender, args) =>
            {
                joint.DragDelta = Vector2.zero;
                IStep oldStep = chapter.Data.FirstStep;

                if (TryGetStepForTransitionDrag(args.PointerPosition, out IStep target) == false)
                {
                    return;
                }

                RevertableChangesHandler.Do(new CourseCommand(() =>
                {
                    chapter.Data.FirstStep = target;
                    MarkToRefresh();
                },
                                                              () =>
                {
                    chapter.Data.FirstStep = oldStep;
                    MarkToRefresh();
                }
                                                              ));

                joint.DragDelta = Vector2.zero;
            };

            SetupNode(entryNode, position => chapter.ChapterMetadata.EntryNodePosition = position);

            return(entryNode);
        }
Example #17
0
        private StepNode CreateNewStepNode(IStep step)
        {
            StepNode node = new StepNode(Graphics, step);

            node.GraphicalEventHandler.ContextClick += (sender, args) =>
            {
                TestableEditorElements.DisplayContextMenu(new List <TestableEditorElements.MenuOption>
                {
                    new TestableEditorElements.MenuItem(new GUIContent("Delete step"), false, () =>
                    {
                        IList <ITransition> incomingTransitions = chapter.Data.Steps.SelectMany(s => s.Data.Transitions.Data.Transitions).Where(transition => transition.Data.TargetStep == step).ToList();

                        bool wasFirstStep = step == chapter.Data.FirstStep;

                        RevertableChangesHandler.Do(new TrainingCommand(
                                                        () =>
                        {
                            foreach (ITransition transition in incomingTransitions)
                            {
                                transition.Data.TargetStep = null;
                            }

                            DeleteStep(step);

                            if (wasFirstStep)
                            {
                                chapter.Data.FirstStep = null;
                            }
                        },
                                                        () =>
                        {
                            AddStep(step);

                            if (wasFirstStep)
                            {
                                chapter.Data.FirstStep = step;
                            }

                            foreach (ITransition transition in incomingTransitions)
                            {
                                transition.Data.TargetStep = step;
                            }

                            UserSelectStepNode(node);
                        }
                                                        ));
                    })
                });
            };

            node.GraphicalEventHandler.PointerDown += (sender, args) =>
            {
                UserSelectStepNode(node);
            };

            node.RelativePositionChanged += (sender, args) =>
            {
                node.Step.StepMetadata.Position = node.Position;
            };

            node.GraphicalEventHandler.PointerUp += (sender, args) =>
            {
                Graphics.CalculateBoundingBox();
            };

            // ReSharper disable once ImplicitlyCapturedClosure
            node.GraphicalEventHandler.PointerDown += (sender, args) => UserSelectStepNode(node);

            node.CreateTransitionButton.GraphicalEventHandler.PointerClick += (sender, args) =>
            {
                ITransition transition = new Transition();

                RevertableChangesHandler.Do(new TrainingCommand(
                                                () =>
                {
                    step.Data.Transitions.Data.Transitions.Add(transition);
                    MarkToRefresh();
                },
                                                () =>
                {
                    step.Data.Transitions.Data.Transitions.Remove(transition);
                    MarkToRefresh();
                }
                                                ));
            };

            if (chapter.ChapterMetadata.LastSelectedStep == step)
            {
                SelectStepNode(node);
            }

            SetupNode(node, position => node.Step.StepMetadata.Position = position);

            return(node);
        }
Example #18
0
        private void SetupTransitions(IChapter chapter, EntryNode entryNode, IDictionary <IStep, StepNode> stepNodes)
        {
            if (chapter.Data.FirstStep != null)
            {
                CreateNewTransition(entryNode.ExitJoints.First(), stepNodes[chapter.Data.FirstStep].EntryJoints.First());
            }

            foreach (IStep step in stepNodes.Keys)
            {
                foreach (ITransition transition in step.Data.Transitions.Data.Transitions)
                {
                    ExitJoint joint = stepNodes[step].AddExitJoint();
                    if (transition.Data.TargetStep != null)
                    {
                        StepNode target = stepNodes[transition.Data.TargetStep];
                        CreateNewTransition(joint, target.EntryJoints.First());
                    }

                    IStep       closuredStep       = step;
                    ITransition closuredTransition = transition;
                    int         transitionIndex    = step.Data.Transitions.Data.Transitions.IndexOf(closuredTransition);

                    joint.GraphicalEventHandler.PointerDrag += (sender, args) =>
                    {
                        joint.DragDelta = args.PointerPosition - joint.Position;
                    };

                    joint.GraphicalEventHandler.PointerUp += (sender, args) =>
                    {
                        joint.DragDelta = Vector2.zero;
                        IStep oldStep = closuredTransition.Data.TargetStep;

                        if (TryGetStepForTransitionDrag(args.PointerPosition, out IStep targetStep) == false)
                        {
                            return;
                        }

                        RevertableChangesHandler.Do(new CourseCommand(() =>
                        {
                            closuredTransition.Data.TargetStep = targetStep;
                            SelectStepNode(stepNodes[closuredStep]);
                            MarkToRefresh();
                        },
                                                                      () =>
                        {
                            closuredTransition.Data.TargetStep = oldStep;
                            SelectStepNode(stepNodes[closuredStep]);
                            MarkToRefresh();
                        }
                                                                      ));
                    };

                    joint.GraphicalEventHandler.ContextClick += (sender, args) =>
                    {
                        TestableEditorElements.DisplayContextMenu(new List <TestableEditorElements.MenuOption>
                        {
                            new TestableEditorElements.MenuItem(new GUIContent("Delete transition"), false, () =>
                            {
                                bool isLast = closuredStep.Data.Transitions.Data.Transitions.Count == 1;
                                RevertableChangesHandler.Do(new CourseCommand(() =>
                                {
                                    closuredStep.Data.Transitions.Data.Transitions.Remove(closuredTransition);
                                    if (isLast)
                                    {
                                        closuredStep.Data.Transitions.Data.Transitions.Add(EntityFactory.CreateTransition());
                                    }

                                    MarkToRefresh();
                                },
                                                                              () =>
                                {
                                    if (isLast)
                                    {
                                        closuredStep.Data.Transitions.Data.Transitions.RemoveAt(0);
                                    }

                                    closuredStep.Data.Transitions.Data.Transitions.Insert(transitionIndex, closuredTransition);
                                    MarkToRefresh();
                                }
                                                                              ));
                            })
                        });
                    };
                }
            }
        }
Example #19
0
        private EntryNode CreateEntryNode(IChapter chapter)
        {
            EntryNode entryNode = new EntryNode(Graphics);

            ExitJoint joint = new ExitJoint(Graphics, entryNode)
            {
                RelativePosition = new Vector2(entryNode.LocalBoundingBox.xMax, entryNode.LocalBoundingBox.center.y),
            };

            entryNode.ExitJoints.Add(joint);

            entryNode.Position = chapter.ChapterMetadata.EntryNodePosition;

            entryNode.RelativePositionChanged += (sender, args) =>
            {
                chapter.ChapterMetadata.EntryNodePosition = entryNode.Position;
            };

            entryNode.GraphicalEventHandler.PointerUp += (sender, args) =>
            {
                Graphics.CalculateBoundingBox();
            };

            entryNode.GraphicalEventHandler.ContextClick += (sender, args) =>
            {
                if (chapter.Data.FirstStep == null)
                {
                    return;
                }

                TestableEditorElements.DisplayContextMenu(new List <TestableEditorElements.MenuOption>
                {
                    new TestableEditorElements.MenuItem(new GUIContent("Delete transition"), false, () =>
                    {
                        IStep firstStep = chapter.Data.FirstStep;

                        RevertableChangesHandler.Do(new TrainingCommand(() =>
                        {
                            chapter.Data.FirstStep = null;
                            MarkToRefresh();
                        },
                                                                        () =>
                        {
                            chapter.Data.FirstStep = firstStep;
                            MarkToRefresh();
                        }
                                                                        ));
                    })
                });
            };

            joint.GraphicalEventHandler.PointerDrag += (sender, args) =>
            {
                joint.DragDelta = args.PointerPosition - joint.Position;
            };

            joint.GraphicalEventHandler.PointerUp += (sender, args) =>
            {
                EntryJoint endJoint = Graphics.GetGraphicalElementWithHandlerAtPoint(args.PointerPosition).FirstOrDefault() as EntryJoint;

                if (endJoint == null)
                {
                    joint.DragDelta = Vector2.zero;
                    return;
                }

                StepNode endJointStepNode = endJoint.Parent as StepNode;

                IStep targetStep = null;
                IStep oldStep    = chapter.Data.FirstStep;

                if (endJointStepNode != null)
                {
                    targetStep = endJointStepNode.Step;
                }

                RevertableChangesHandler.Do(new TrainingCommand(() =>
                {
                    chapter.Data.FirstStep = targetStep;
                    MarkToRefresh();
                },
                                                                () =>
                {
                    chapter.Data.FirstStep = oldStep;
                    MarkToRefresh();
                }
                                                                ));

                joint.DragDelta = Vector2.zero;
            };

            SetupNode(entryNode, position => chapter.ChapterMetadata.EntryNodePosition = position);

            return(entryNode);
        }
Example #20
0
        private void SetupTransitions(IChapter chapter, EntryNode entryNode, IDictionary <IStep, StepNode> stepNodes)
        {
            if (chapter.Data.FirstStep != null)
            {
                CreateNewTransition(entryNode.ExitJoints.First(), stepNodes[chapter.Data.FirstStep].EntryJoints.First());
            }

            foreach (IStep step in stepNodes.Keys)
            {
                foreach (ITransition transition in step.Data.Transitions.Data.Transitions)
                {
                    ExitJoint joint = stepNodes[step].AddExitJoint();
                    if (transition.Data.TargetStep != null)
                    {
                        StepNode target = stepNodes[transition.Data.TargetStep];
                        CreateNewTransition(joint, target.EntryJoints.First());
                    }

                    IStep       closuredStep       = step;
                    ITransition closuredTransition = transition;
                    int         transitionIndex    = step.Data.Transitions.Data.Transitions.IndexOf(closuredTransition);

                    joint.GraphicalEventHandler.PointerDrag += (sender, args) =>
                    {
                        joint.DragDelta = args.PointerPosition - joint.Position;
                    };

                    joint.GraphicalEventHandler.PointerUp += (sender, args) =>
                    {
                        GraphicalElement elementUnderCursor = Graphics.GetGraphicalElementWithHandlerAtPoint(args.PointerPosition).FirstOrDefault();

                        EntryJoint endJoint = elementUnderCursor as EntryJoint;

                        if (endJoint == null)
                        {
                            joint.DragDelta = Vector2.zero;

                            if (elementUnderCursor != null)
                            {
                                return;
                            }
                        }

                        StepNode endJointStepNode = endJoint == null ? null : endJoint.Parent as StepNode;

                        IStep targetStep = null;
                        IStep oldStep    = closuredTransition.Data.TargetStep;

                        if (endJointStepNode != null)
                        {
                            targetStep = endJointStepNode.Step;
                        }

                        RevertableChangesHandler.Do(new TrainingCommand(() =>
                        {
                            closuredTransition.Data.TargetStep = targetStep;
                            SelectStepNode(stepNodes[closuredStep]);
                            MarkToRefresh();
                        },
                                                                        () =>
                        {
                            closuredTransition.Data.TargetStep = oldStep;
                            SelectStepNode(stepNodes[closuredStep]);
                            MarkToRefresh();
                        }
                                                                        ));

                        joint.DragDelta = Vector2.zero;
                    };

                    joint.GraphicalEventHandler.ContextClick += (sender, args) =>
                    {
                        TestableEditorElements.DisplayContextMenu(new List <TestableEditorElements.MenuOption>
                        {
                            new TestableEditorElements.MenuItem(new GUIContent("Delete transition"), false, () =>
                            {
                                RevertableChangesHandler.Do(new TrainingCommand(() =>
                                {
                                    closuredStep.Data.Transitions.Data.Transitions.Remove(closuredTransition);
                                    MarkToRefresh();
                                },
                                                                                () =>
                                {
                                    closuredStep.Data.Transitions.Data.Transitions.Insert(transitionIndex, closuredTransition);
                                    MarkToRefresh();
                                }
                                                                                ));
                            })
                        });
                    };
                }
            }
        }