Exemplo n.º 1
0
        public static void RenameElement(this IElement elementToRename, string value)
        {
            bool   isValid = true;
            string whyItIsntValid;

            if (elementToRename is ScreenSave)
            {
                isValid = NameVerifier.IsScreenNameValid(value, elementToRename as ScreenSave, out whyItIsntValid);
            }
            else
            {
                isValid = NameVerifier.IsEntityNameValid(value, elementToRename as EntitySave, out whyItIsntValid);
            }

            if (!isValid)
            {
                MessageBox.Show(whyItIsntValid);
            }
            else
            {
                string oldName = elementToRename.Name;
                string newName = oldName.Substring(0, oldName.Length - elementToRename.ClassName.Length) + value;

                DialogResult result = ChangeClassNamesInCodeAndFileName(elementToRename, value, oldName, newName);

                if (result == DialogResult.Yes)
                {
                    // Set the name first because that's going
                    // to be used by code that follows to modify
                    // inheritance.
                    elementToRename.Name = newName;


                    if (elementToRename is EntitySave)
                    {
                        // Change any Entities that depend on this
                        for (int i = 0; i < ProjectManager.GlueProjectSave.Entities.Count; i++)
                        {
                            EntitySave entitySave = ProjectManager.GlueProjectSave.Entities[i];
                            if (entitySave.BaseEntity == oldName)
                            {
                                entitySave.BaseEntity = newName;
                            }
                        }

                        // Change any NamedObjects that use this as their type (whether in Entity, or as a generic class)
                        List <NamedObjectSave> namedObjects = ObjectFinder.Self.GetAllNamedObjectsThatUseEntity(oldName);

                        foreach (NamedObjectSave nos in namedObjects)
                        {
                            if (nos.SourceType == SourceType.Entity && nos.SourceClassType == oldName)
                            {
                                nos.SourceClassType = newName;
                                nos.UpdateCustomProperties();
                            }
                            else if (nos.SourceType == SourceType.FlatRedBallType && nos.SourceClassGenericType == oldName)
                            {
                                nos.SourceClassGenericType = newName;
                            }
                        }
                    }
                    else
                    {
                        // Change any Screens that depend on this
                        for (int i = 0; i < ProjectManager.GlueProjectSave.Screens.Count; i++)
                        {
                            ScreenSave screenSave = ProjectManager.GlueProjectSave.Screens[i];
                            if (screenSave.BaseScreen == oldName)
                            {
                                screenSave.BaseScreen = newName;
                            }
                        }

                        if (ProjectManager.StartUpScreen == oldName)
                        {
                            ProjectManager.StartUpScreen = newName;
                        }


                        // Don't do anything with NamedObjects and Screens since they can't (currently) be named objects
                    }

                    ProjectManager.SaveProjects();
                    GluxCommands.Self.SaveGlux();


                    TreeNode treeNode = GlueState.Self.Find.ElementTreeNode(elementToRename);
                    if (treeNode is ScreenTreeNode)
                    {
                        ((ScreenTreeNode)treeNode).UpdateReferencedTreeNodes();
                    }
                    else if (treeNode is EntityTreeNode)
                    {
                        ((EntityTreeNode)treeNode).UpdateReferencedTreeNodes();
                    }

                    if (elementToRename is EntitySave)
                    {
                        ProjectManager.SortAndUpdateUI(elementToRename as EntitySave);
                    }

                    else if (elementToRename is ScreenSave)
                    {
                        ProjectManager.SortAndUpdateUI(elementToRename as ScreenSave);
                    }
                }
            }
        }
Exemplo n.º 2
0
        public static ReferencedFileSave AddReferencedFileSave(IElement element, string directoryPath, string fileName,
                                                               AssetTypeInfo resultAssetTypeInfo, string option, out string errorMessage)
        {
            char invalidCharacter;
            ReferencedFileSave rfs = null;

            errorMessage = null;

            #region Get directory

            string directoryRelativeToContent = "";

            if (!String.IsNullOrEmpty(directoryPath))
            {
                //string directory = directoryTreeNode.GetRelativePath().Replace("/", "\\");

                directoryRelativeToContent = directoryPath;
            }
            else if (element != null)
            {
                //string directory = elementToAddTo.GetRelativePath().Replace("/", "\\");

                directoryRelativeToContent = element.Name.Replace(@"/", @"\") + "\\";
            }
            else
            {
                directoryRelativeToContent = "GlobalContent/";
            }


            #endregion

            bool userCancelled = false;


            #region If there is some reason why the file name won't, then work show a message box

            string whyIsntValid;

            if (!NameVerifier.IsReferencedFileNameValid(fileName, resultAssetTypeInfo, rfs, element, out whyIsntValid))
            {
                errorMessage = "Invalid file name:\n" + fileName + "\n" + whyIsntValid;
            }
            else if (resultAssetTypeInfo == null)
            {
                errorMessage = "You must select a valid type for your new file.";
            }
            else if (ObjectFinder.Self.GetReferencedFileSaveFromFile(directoryRelativeToContent + fileName + "." + resultAssetTypeInfo.Extension) != null)
            {
                errorMessage = "There is already a file named\n\n" + directoryRelativeToContent + fileName + "." + resultAssetTypeInfo.Extension;
            }
            // TODO:  This currently checks for an exact match, but we should prevent different files (like a .emix and .scnx) from having the same name
            else if (EditorLogic.CurrentElement != null &&
                     EditorLogic.CurrentElement.GetReferencedFileSaveRecursively(directoryRelativeToContent + fileName + "." + resultAssetTypeInfo.Extension) != null)
            {
                errorMessage = "There is already a file named " + fileName;
            }
            // need to check global content for duplicates.  This is not implemented yet.
            #endregion

            #region Else, create the file

            else
            {
                string createdFile = PluginManager.CreateNewFile(resultAssetTypeInfo, option == "2D",
                                                                 directoryRelativeToContent, fileName);

                // let's just re-route this
                // to the code that adds existing
                // files now that we have a file and
                // that's exactly what we're doing.
                rfs = RightClickHelper.AddSingleFile(createdFile, ref userCancelled, option);

                if (rfs == null && !userCancelled)
                {
                    throw new NullReferenceException("The RFS shouldn't be null");
                }
            }

            #endregion



            return(rfs);
        }