Exemplo n.º 1
0
        /// <summary>
        /// Tries to save all of the behaviours in the list.
        /// </summary>
        /// <param name="behaviors">The list of the behaviours you want to save.</param>
        /// <param name="result">Holds if a behaviour could be saved or not.</param>
        /// <returns>Returns Failed if a bahevaiour could not be saved. Returns Cancelled if the user cancelled the process.</returns>
        private SaveResult SaveBehaviors(List <BehaviorNode> behaviors, out bool[] result)
        {
            // create anew dialogue
            using (MainWindowCloseDialog dialog = new MainWindowCloseDialog())
            {
                // create the results
                result = new bool[behaviors.Count];

                // check if there is any unsaved behaviour and update the result accordingly
                bool hasUnsavedBehaviors = false;
                for (int i = 0; i < behaviors.Count; ++i)
                {
                    if (behaviors[i].IsModified)
                    {
                        hasUnsavedBehaviors = true;

                        // add the behaviour to the dialogue
                        dialog.AddUnsavedBehavior(behaviors[i]);

                        result[i] = false;
                    }
                    else
                    {
                        result[i] = true;
                    }
                }

                // if there is no unsaved behaviour, we are done
                if (!hasUnsavedBehaviors)
                {
                    return(SaveResult.Succeeded);
                }

                // show the dialogue
                DialogResult dialogResult = dialog.ShowDialog();

                // check if the user cancelled the process
                if (dialogResult == DialogResult.Cancel)
                {
                    return(SaveResult.Cancelled);
                }
                // the user decided to discard all the unsaved changes
                else if (dialogResult == DialogResult.No)
                {
                    for (int i = 0; i < behaviors.Count; ++i)
                    {
                        result[i] = true;
                    }

                    return(SaveResult.Succeeded);
                }
                // the user decided to save some of the unsaved changes
                else if (dialogResult == DialogResult.Yes)
                {
                    // check for all the behaviours
                    bool saveErrorOccured = false;
                    for (int i = 0; i < behaviors.Count; ++i)
                    {
                        // check if the user wants to save the behaviour
                        if (dialog.IsSelected(behaviors[i]))
                        {
                            string saveresult = string.Empty;
                            bool   saveerror  = false;

                            // try to save the behaviour
                            try { saveresult = behaviorTreeList.SaveBehavior(behaviors[i], false); }
                            catch { saveerror = true; }

                            // if there was an exception or the user aborted the save
                            if (saveerror || saveresult == string.Empty)
                            {
                                saveErrorOccured = true;

                                // the user aborted the save
                                if (saveresult == string.Empty)
                                {
                                    return(SaveResult.Cancelled);
                                }
                            }
                            else
                            {
                                // everything is fine
                                result[i] = true;
                            }
                        }
                        else
                        {
                            // this behaviour will be discarded. No error
                            result[i] = true;
                        }
                    }

                    // check if we had an error
                    return(saveErrorOccured ? SaveResult.Failed : SaveResult.Succeeded);
                }
            }

            throw new Exception("undealt dialog return");
        }
Exemplo n.º 2
0
        /// <summary>
        /// Tries to save all of the behaviours in the list.
        /// </summary>
        /// <param name="behaviors">The list of the behaviours you want to save.</param>
        /// <param name="result">Holds if a behaviour could be saved or not.</param>
        /// <returns>Returns Failed if a bahevaiour could not be saved. Returns Cancelled if the user cancelled the process.</returns>
        private SaveResult SaveBehaviors(List<BehaviorNode> behaviors, out bool[] result)
        {
            // create anew dialogue
            using(MainWindowCloseDialog dialog= new MainWindowCloseDialog())
            {
                // create the results
                result= new bool[behaviors.Count];

                // check if there is any unsaved behaviour and update the result accordingly
                bool hasUnsavedBehaviors= false;
                for(int i= 0; i <behaviors.Count; ++i)
                {
                    if(behaviors[i].IsModified)
                    {
                        hasUnsavedBehaviors= true;

                        // add the behaviour to the dialogue
                        dialog.AddUnsavedBehavior(behaviors[i]);

                        result[i]= false;
                    }
                    else
                    {
                        result[i]= true;
                    }
                }

                // if there is no unsaved behaviour, we are done
                if(!hasUnsavedBehaviors)
                    return SaveResult.Succeeded;

                // show the dialogue
                DialogResult dialogResult= dialog.ShowDialog();

                // check if the user cancelled the process
                if(dialogResult ==DialogResult.Cancel)
                {
                    return SaveResult.Cancelled;
                }
                    // the user decided to discard all the unsaved changes
                else if(dialogResult ==DialogResult.No)
                {
                    for(int i= 0; i <behaviors.Count; ++i)
                        result[i]= true;

                    return SaveResult.Succeeded;
                }
                    // the user decided to save some of the unsaved changes
                else if(dialogResult ==DialogResult.Yes)
                {
                    // check for all the behaviours
                    bool saveErrorOccured= false;
                    for(int i= 0; i <behaviors.Count; ++i)
                    {
                        // check if the user wants to save the behaviour
                        if(dialog.IsSelected(behaviors[i]))
                        {
                            string saveresult= string.Empty;
                            bool saveerror= false;

                            // try to save the behaviour
                            try { saveresult= behaviorTreeList.SaveBehavior(behaviors[i], false); }
                            catch { saveerror= true; }

                            // if there was an exception or the user aborted the save
                            if(saveerror || saveresult ==string.Empty)
                            {
                                saveErrorOccured= true;

                                // the user aborted the save
                                if(saveresult ==string.Empty)
                                    return SaveResult.Cancelled;
                            }
                            else
                            {
                                // everything is fine
                                result[i]= true;
                            }
                        }
                        else
                        {
                            // this behaviour will be discarded. No error
                            result[i]= true;
                        }
                    }

                    // check if we had an error
                    return saveErrorOccured ? SaveResult.Failed : SaveResult.Succeeded;
                }
            }

            throw new Exception("undealt dialog return");
        }